[R] Regressions with fixed-effect in R

2010-05-12 Thread Millo Giovanni
Dear Liviu,

we're still working on measures of fit for panels. If I get you right,
what you mean is the R^2 of the demeaned, or within, regression. A
quick and dirty function to do this is:

pmodel.response-plm:::pmodel.response.plm # needs this to make the
method accessible

r2-function(x, adj=TRUE) {
  ## fetch response and residuals
  y - pmodel.response(x, model=within)
  myres - resid(x)
  n - length(myres)

  if(adj) {
adjssr - x$df.residual
adjtss - n-1
} else {
adjssr - 1
adjtss - 1
}

  ssr - sum(myres^2)/adjssr
  tss - sum(y^2)/adjtss
  return(1-ssr/tss)
  }

and then

 r2(yourmodel)

Hope this helps
Giovanni

--

Message: 13
Date: Tue, 11 May 2010 13:21:02 +0100
From: Liviu Andronic landronim...@gmail.com
To: Daniel Malter dan...@umd.edu
Cc: r-help@r-project.org
Subject: Re: [R] Regressions with fixed-effect in R
Message-ID:
aanlktikvi6_-qvh-odr31eiippuq1sra0-qconqzh...@mail.gmail.com
Content-Type: text/plain; charset=UTF-8

Dear Daniel

On 5/11/10, Daniel Malter dan...@umd.edu wrote:
  R-squared of interest is typically the within R-squared, not the
overall or

Could you point to an example on how to compute the within R-squared
in R, either via lm() or plm()?
Thank you
Liviu



Giovanni Millo
Research Dept.,
Assicurazioni Generali SpA
Via Machiavelli 4, 
34132 Trieste (Italy)
tel. +39 040 671184 
fax  +39 040 671160 

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] Regressions with fixed-effect in R

2010-05-11 Thread Liviu Andronic
On 5/11/10, chen jia chen_1...@fisher.osu.edu wrote:
  Are there any functions that specifically deal with fixed-effects?

Other than plm and its vignette, you may want to check this document [1].
Liviu

[1] http://cran.r-project.org/doc/contrib/Farnsworth-EconometricsInR.pdf

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] Regressions with fixed-effect in R

2010-05-11 Thread Liviu Andronic
Dear Daniel

On 5/11/10, Daniel Malter dan...@umd.edu wrote:
  R-squared of interest is typically the within R-squared, not the overall or

Could you point to an example on how to compute the within R-squared
in R, either via lm() or plm()?
Thank you
Liviu

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] Regressions with fixed-effect in R

2010-05-11 Thread chen jia
Thanks. I have the PDF document that you suggest. It is very brief on
fixed-effect in panel regressions.

I will look into plm package.

Best,
Jia

On Tue, May 11, 2010 at 8:19 AM, Liviu Andronic landronim...@gmail.com wrote:
 On 5/11/10, chen jia chen_1...@fisher.osu.edu wrote:
  Are there any functions that specifically deal with fixed-effects?

 Other than plm and its vignette, you may want to check this document [1].
 Liviu

 [1] http://cran.r-project.org/doc/contrib/Farnsworth-EconometricsInR.pdf




-- 
 Ohio State University - Finance
   248 Fisher Hall
2100 Neil Ave.
  Columbus, Ohio  43210
 Telephone: 614-292-2830
   http://www.fisher.osu.edu/~chen_1002/

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] Regressions with fixed-effect in R

2010-05-11 Thread Daniel Malter

if the plm function only puts out one r-squared, it should be the within
r-squared, but I could be wrong. Stata, for example, gives you a within, a
between, and an overall r-squared. Here is what they do.

set.seed(1)
x=rnorm(100)
fe=rep(rnorm(10),each=10)
id=rep(1:10,each=10)
ti=rep(1:10,10)
e=rnorm(100)
y=x+fe+e

data=data.frame(y,x,id,ti)

library(plm)
reg=plm(y~x,model=within,index=c(id,ti),data=data)
summary(reg)

cat(R-squared: , 1-83.908/178.5)

#Let's compute the squared residuals of this regression
SSR=sum(residuals(reg)^2)

#let's compute the total squares of the ys
SS0=sum((y-mean(y))^2)
SS0 #Note that this is not the TSS given by plm

#Now, let's demean y and x for each individual separately
y.dem=y-tapply(y,id,mean)[id]
x.dem=x-tapply(x,id,mean)[id]

#and regress them
#note that we do not estimate the intercept because we have demeaned the
data
reg.fe=lm(y.dem~-1+x.dem)
summary(reg.fe)
#The coefficient is correct, i.e., the same as in plm
#Note that the standard error is wrong, however. We would need to account
for
#that we are losing degrees of freedom by taking out the fixed effects.


#now let's look at the sum of squares after demeaning y
SSR.y.dem=sum((y.dem-mean(y.dem))^2)
SSR.y.dem #Note, this is the Total sum of squares given by plm

#Now, we know that the total sum of squares
#not accounting for fixed effects is

# TSS=SS0=331.7986

#However, we know that after taking out the fixed effects (demeaning y)
# the total sum of squares is
# SSR.y.dem=178.5050

#The within R-squared is then the variance explained by x AFTER having taken
out the fixed effects
# So the R-squared computable from the plm output is in fact the within
R-squared
cat(Within R-squared: , 1-SSR/SSR.y.dem)

#which is identical to the r-squared in our hand-computed FE regression
summary(reg.fe)$r.squared


#The two other R-squareds Stata would give you are:

#The overall r-squared
#which is the r-squared of a pooled OLS of y on x WITHOUT accounting for the
fixed effects

#Pooled OLS
reg1=lm(y~x)
summary(reg1)

#This is what Stata shows as overall R-squared
summary(reg1)$r.squared

#The second R-squared Stata shows is the between R-squared
# which is the R-squared of regressing the mean of the individual y(i)
# on the mean(s) of the individual X(i)

#Get the means of y and x for each individual
y.means=tapply(y,id,mean)[id]
x.means=tapply(x,id,mean)[id]

#Regress them on each other
reg2=lm(y.means~x.means)

#This is what Stata shows as between R-squared
summary(reg2)$r.squared


So you see that the R-squared computable from the plm output is indeed the
within R-squared.

For comparison, look at the Stata output:

Fixed-effects (within) regression   Number of obs  =  
100
Group variable: id  Number of groups   =   
10

R-sq:  within  = 0.5299 Obs per group: min =   
10
   between = 0.1744avg = 
10.0
   overall = 0.3367max =   
10

F(1,89)=   
100.34
corr(u_i, Xb)  = 0.0547 Prob  F   =   
0.

--
   y |  Coef.   Std. Err.  tP|t| [95% Conf.
Interval]
-+
   x |   1.87   .110931910.02   0.000 .8907682   
1.331607
   _cons |   .3155321   .0978457 3.22   0.002 .1211147   
.5099495
-+
 sigma_u |  1.2318621
 sigma_e |  .97097297
 rho |  .61679513   (fraction of variance due to u_i)
--
F test that all u_i=0: F(9, 89) =16.05   Prob  F =
0.

HTH,
Daniel



-- 
View this message in context: 
http://r.789695.n4.nabble.com/Regressions-with-fixed-effect-in-R-tp2173314p2183703.html
Sent from the R help mailing list archive at Nabble.com.

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


[R] Regressions with fixed-effect in R

2010-05-10 Thread chen jia
Hi there,

Maybe people who know both R and econometrics will be able to answer
my questions.

I want to run panel regressions in R with fixed-effect. I know two
ways to do it.
First, I can include factor(grouping_variable) in my regression equation.
Second, I plan to subtract group mean from my variables and run OLS
panel regression with function lm().

I plan to do it with the second way because the number of groups is
large, which incur computational problems inverting large
model-matrix.

I am interested in the R-squared and adjusted R-squared out of these
regressions.

Do I need to adjust my R-squared after I run OLS regressions with
demeaned variables?

Are there any functions that specifically deal with fixed-effects?

Thanks.

Best,
Jia

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] Regressions with fixed-effect in R

2010-05-10 Thread Daniel Malter

There is the plm package for linear panel models. Further, since estimation
of fixed effects models rests on the within-subject or -object variance, the
R-squared of interest is typically the within R-squared, not the overall or
between R-squared. Read up about it before you use it though.

Daniel

-- 
View this message in context: 
http://r.789695.n4.nabble.com/Regressions-with-fixed-effect-in-R-tp2173314p2173326.html
Sent from the R help mailing list archive at Nabble.com.

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.