[R] Dummy variables model
Hi, all! Anyone know an easy way to specify the following model. Panel dataset, with stock through time, by firm. I want to run a model of y on a bunch of explanatory variables, and one dummy for each firm, which is 1 for observations that come from firm i, and 0 everywhere else. I have over 200 firms (and a factor variable that contains a firm identifier). Any easy way of going about this, without having to define all these dummies? I checked lme() with random = ~ 1|firm, but the problem is that these are random effects, i.e. that there are firm-by-firm disturbance terms and overall disturbance terms, whereas I want just overall disturbance terms. This is generally called a fixed effects model, although it seems like the term fixed effects is being used somewhat differently in the context of the nlme package. Toby -- ** When Thomas Edison invented the light bulb he tried over 2000 experiments before he got it to work. A young reporter asked him how it felt to have failed so many times. He said I never failed once. I invented the light bulb. It just happened to be a 2000-step process. __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Re: [R] Dummy variables model
So are you guys saying to me that if I have variable firm which is the factor of all firm identifiers, I could just go lm(y ~ x + firm) and that will implicitly include a dummy for each level of factor firm, thus making this a fixed effects (aka LSDV) model? T Jean Eid wrote: You can turn the identity vector of the firms into a factor and do lm Jean On Mon, 5 Sep 2005, Tobias Muhlhofer wrote: Hi, all! Anyone know an easy way to specify the following model. Panel dataset, with stock through time, by firm. I want to run a model of y on a bunch of explanatory variables, and one dummy for each firm, which is 1 for observations that come from firm i, and 0 everywhere else. I have over 200 firms (and a factor variable that contains a firm identifier). Any easy way of going about this, without having to define all these dummies? I checked lme() with random = ~ 1|firm, but the problem is that these are random effects, i.e. that there are firm-by-firm disturbance terms and overall disturbance terms, whereas I want just overall disturbance terms. This is generally called a fixed effects model, although it seems like the term fixed effects is being used somewhat differently in the context of the nlme package. Toby -- ** When Thomas Edison invented the light bulb he tried over 2000 experiments before he got it to work. A young reporter asked him how it felt to have failed so many times. He said I never failed once. I invented the light bulb. It just happened to be a 2000-step process. __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html -- ** When Thomas Edison invented the light bulb he tried over 2000 experiments before he got it to work. A young reporter asked him how it felt to have failed so many times. He said I never failed once. I invented the light bulb. It just happened to be a 2000-step process. __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Re: [R] Dummy variables model
Dang! That's awesome! Being at the end of an empirical PhD in which all the econometrics was done in R, I was already a longtime R enthusiast, but you never stop learning more neat features!!! YAY to everyone involved in R's development Toby Adaikalavan Ramasamy wrote: You will need to ensure that firm is a factor and not numerical (i.e. continuous). Here is an example firm - factor( sample(1:3, 20, replace=T) ) x1 - runif(20) y- rnorm(20) summary( fit - lm( y ~ -1 + x1 + firm ) ) ... Coefficients: Estimate Std. Error t value Pr(|t|) x1-0.049640.74861 -0.0660.948 firm1 0.107320.48269 0.2220.827 firm2 0.275480.48781 0.5650.580 firm3 -0.076510.53384 -0.1430.888 NB : The -1 in the formula forces each firm to have its own intercept. Use model.matrix, you will see the dummy variables created within lm(). model.matrix( fit ) x1 firm1 firm2 firm3 1 0.6641647 0 1 0 2 0.5142712 1 0 0 3 0.2197956 1 0 0 4 0.3211675 0 1 0 5 0.1892449 1 0 0 6 0.7740754 0 0 1 7 0.3486932 0 1 0 8 0.2116816 0 0 1 9 0.2426825 0 1 0 10 0.2219768 1 0 0 11 0.9328514 1 0 0 12 0.7880405 0 0 1 13 0.8673492 0 1 0 14 0.1777998 0 1 0 15 0.3178498 1 0 0 16 0.3379726 0 0 1 17 0.9193359 1 0 0 18 0.6998152 0 1 0 19 0.2825702 0 0 1 20 0.6139586 1 0 0 Regards, Adai On Mon, 2005-09-05 at 15:53 +0100, Tobias Muhlhofer wrote: So are you guys saying to me that if I have variable firm which is the factor of all firm identifiers, I could just go lm(y ~ x + firm) and that will implicitly include a dummy for each level of factor firm, thus making this a fixed effects (aka LSDV) model? T Jean Eid wrote: You can turn the identity vector of the firms into a factor and do lm Jean On Mon, 5 Sep 2005, Tobias Muhlhofer wrote: Hi, all! Anyone know an easy way to specify the following model. Panel dataset, with stock through time, by firm. I want to run a model of y on a bunch of explanatory variables, and one dummy for each firm, which is 1 for observations that come from firm i, and 0 everywhere else. I have over 200 firms (and a factor variable that contains a firm identifier). Any easy way of going about this, without having to define all these dummies? I checked lme() with random = ~ 1|firm, but the problem is that these are random effects, i.e. that there are firm-by-firm disturbance terms and overall disturbance terms, whereas I want just overall disturbance terms. This is generally called a fixed effects model, although it seems like the term fixed effects is being used somewhat differently in the context of the nlme package. Toby -- ** When Thomas Edison invented the light bulb he tried over 2000 experiments before he got it to work. A young reporter asked him how it felt to have failed so many times. He said I never failed once. I invented the light bulb. It just happened to be a 2000-step process. __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html -- ** When Thomas Edison invented the light bulb he tried over 2000 experiments before he got it to work. A young reporter asked him how it felt to have failed so many times. He said I never failed once. I invented the light bulb. It just happened to be a 2000-step process. __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
[R] clustering of disturbances
Hi! I have a dataset of properties that are owned by different firms, each firm owning multiple properties. I am running a regression of holding period (how long a property was held in a firm's portfolio) on the left, and a bunch of factors on the right. When calculating standard errors, I would like to cluster my disturbance terms by firm. Any ideas on how to do this? I'm guessing gls() from nlme, but what sorts of options? Toby __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Re: [R] Garch in a model with explanatory variables
The Ox interface in fSeries is quite an easy way to accomplish this, although it produces some garbage, both in your current environment within R, as well as in the directory in which you are running R. You have to be careful also if you're on a Linux or other UNIX system, as the function has Windows pathnames hard coded into it, which you need to alter to the ones where your Ox resides. Tobias __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Re: [R] r equivalent of egen? Not tapply
Thomas, I usually use the aggregate() function, proceeding as follows. Construct a vector of 1s that is the same length as your entire data. Then use aggregate() with sum as a function on this vector and your grouping variable as a by argument (note the use of na.rm in sum). This will create a data.frame with the counts and the panel markers that represent the subcategories (one row for each). Then use merge() to put these back into your original data.frame, as this will create the necessary duplicates. Toby __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
[R] Function environments lm() weights
I am writing a function of weighted regression, as a procedure for heteroskedasticity. The function runs an auxiliary regression whose fitted values I assign to fit, and then I go: w - 1/(exp(fit/2)) ## Rerun the old regression ## if(gls) { wtd.model - glm(model, weights=w) } if(!gls) { wtd.model - lm(model, weights=w, x=TRUE) } In this version, R complains that it can't find w. How can I tell it to look for w in the function's environment, rather than in environment 1 or whatever? An easy workaround, of course, is to superassign w and remove it afterwards, but that's a little messy, in case the user already has a variable called w in his environment. Thanks, Tobias Muhlhofer __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Re: [R] Chars as numbers
Josef, Not sure if this is exactly what you mean, but there is a generic function as() to which you can then specify numeric as an argument and which then coerces stuff into numeric format. Tobias Josef Eschgfaeller wrote: Is there a proper function for transforming a character to a number instead of using i=match('c',letters) # 3 Thanks. Josef Eschgfäller __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html -- ** When Thomas Edison invented the light bulb he tried over 2000 experiments before he got it to work. A young reporter asked him how it felt to have failed so many times. He said I never failed once. I invented the light bulb. It just happened to be a 2000-step process. __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
[R] Simultaneous estimation of mean and garch eq'n
Is it possible to simultaneously estimate mean and GARCH parameters in R? In other words, I would like to estimate the normal regression equation Y = b X + u and simultaneously do a garch process on the u's to correct the standard errors. I was thinking maybe something with systemfit(), but I can't quite come up with it. Thanks, Tobias -- __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
[R] GARCH
Hi, everyone! Is there a function to do single-variable GARCH in R? If yes, what library is it in? Thanks! Toby -- ** When Thomas Edison invented the light bulb he tried over 2000 experiments before he got it to work. A young reporter asked him how it felt to have failed so many times. He said I never failed once. I invented the light bulb. It just happened to be a 2000-step process. __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
[R] Panel methods, implied var/cov structure
Hi! I have a (fairly narrow and long) panel dataset of returns across three portfolios over 100-odd time-series observations. I have reason to believe that there is heteroskedasticity in the error terms, but that this heteroskedasticity is only through time, i.e. that the three portfolios have the same underlying covariance structure over time, which of course is unknown to me and about which I do not want to make assumptions as to functional form. Greene says there are GLS methods to handle this type of situation. What R functions am I looking for? Is there another way of estimating this? Perhaps SUR, or something like that? Thanks, Tobias __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
[R] Omitting constant in ols() from Design
Hi! I need to run ols regressions with Huber-White sandwich estimators and the correponding standard errors, without an intercept. What I'm trying to do is create an ols object and then use the robcov() function, on the order of: f - ols(depvar ~ ind1 + ind2, x=TRUE) robcov(f) However, when I go f - ols(depvar ~ ind1 + ind2 -1, x=TRUE) I get the following error: Error in ols(nareit ~ SnP500 + d3yrtr - 1) : length of dimnames [2] not equal to array extent same with +0 instead of -1. Is there a different way to create an ols object without a constant? I can't use lm(), because robcov() needs an object from the Design() series. Or is there a different way to go about this? Tobias Muhlhofer __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
[R] dropping rows
Hi! Sorry for asking a trivial questions, but I can't seem to figure this out. I have a dataframe called master containing 30-odd variables. In this dataframe, I have observations across these 30 variables from 1930 to 2003 (I've made a year variable). How can I drop all rows for which the year is less than 1960? I'm assuming something with ifelse() but I can't quite figure it out. I would appreciate a suggestion of some syntax. Thanks! Toby __ [EMAIL PROTECTED] mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Re: [R] dropping rows
Thanks. The problem is that there is extremely little on dataframes or matrices in An Intro to R, which I did read and I frankly don't know where else to go. Once I know a function like subset() exists, I can then read the help files on it and that's fine, but I would never dream this function up myself... As for indexing, I DID read An Introduction to R and I did NOT catch the part where it says you can use any variable in the dataframe to index it, nor would I have thought of it by myself. From that documentation, I only learned about using row-labels to index things... But I am definitely thankful for the quick help given to me by people on this list, and so I guess being RTFM'ed is a small price to pay for figuring out how to solve the problem I need to solve. Toby Jeff Laake wrote: Here's an example: earlydata=data[data$year1960,] Lookup help and read manuals on manipulating dataframes. Tobias Muhlhofer wrote: Hi! Sorry for asking a trivial questions, but I can't seem to figure this out. I have a dataframe called master containing 30-odd variables. In this dataframe, I have observations across these 30 variables from 1930 to 2003 (I've made a year variable). How can I drop all rows for which the year is less than 1960? I'm assuming something with ifelse() but I can't quite figure it out. I would appreciate a suggestion of some syntax. Thanks! Toby __ [EMAIL PROTECTED] mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html -- ** When Thomas Edison invented the light bulb he tried over 2000 experiments before he got it to work. A young reporter asked him how it felt to have failed so many times. He said I never failed once. I invented the light bulb. It just happened to be a 2000-step process. __ [EMAIL PROTECTED] mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
[R] Relative subscripting
Hi! I'm trying to do the following. I have monthly a dataset with, among other things, marketcap, and return. I want to multiply return by the marketcap of the previous month. How do I do this? In STATA (which I have used frequently in the past) I would simply use an expression of the form return[_n]*marketcap[_n-1] I believe they call this relative subscripting. Is there something like this in R? On a completely different note, are there books on R? I read the Getting Started notes, but, while helpful, I would need more than those. Thanks, Toby __ [EMAIL PROTECTED] mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Re: [R] Relative subscripting
OK, yeah. I did think of that in the meantime, but I was also wondering if there was some other convenience function to do it. But this will do in any case. Toby Jean Eid wrote: wouldn't it be return[2:NROW(return)]*marketcap[1:(NROW(return)-1)] (note that return is also a function in R so maybe you should stay away from calling your variable return. Anyways if you have a data frame you can add another variable to it but attach an NA to the first element (since you are lagging variables) i.e. mydata$myvar-c(NA, mydata$return[2:nrow(mydata)]*mydata$marketcap[1:(nrow(mydata)-1)] Jean On Tue, 30 Nov 2004, Tobias Muhlhofer wrote: Hi! I'm trying to do the following. I have monthly a dataset with, among other things, marketcap, and return. I want to multiply return by the marketcap of the previous month. How do I do this? In STATA (which I have used frequently in the past) I would simply use an expression of the form return[_n]*marketcap[_n-1] I believe they call this relative subscripting. Is there something like this in R? On a completely different note, are there books on R? I read the Getting Started notes, but, while helpful, I would need more than those. Thanks, Toby __ [EMAIL PROTECTED] mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html -- ** When Thomas Edison invented the light bulb he tried over 2000 experiments before he got it to work. A young reporter asked him how it felt to have failed so many times. He said I never failed once. I invented the light bulb. It just happened to be a 2000-step process. __ [EMAIL PROTECTED] mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
[R] Combined variable names
I am trying to define a large number of variables through a loop construct. I have my loop variable i being cycled through 1:100 and I would like the variables produced by this to be called vi (i.e. v1 v2 v3 etc) so, for example I'm going: for(i in 1:100) { blank - a[i:N] # or whatever else you want to put on the right side } where N is previously defined. What goes in for blank? Thanks, Toby __ [EMAIL PROTECTED] mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html