[R] how to calculate Rsquare
Spencer Graves
spencer.graves at pdf.com
Mon Jul 21 18:41:10 CEST 2003
It should be easy to get a separate R^2 from "lm" for each level of a
factor: Just split the data and run "lm" once for each level of f.
I've done this with a "for" loop something like the following:
dat <- data.frame(x=x, y=y, f=f)
for(i.f in 1:2){
sel <- (f == c("a", "b")[i.f])
fit <- lm(y~x, data=dat[sel])
...
}
where "..." is replaced by appropriate commands to get R^2 from
summary(fit). It may also be possible to do with with tapply.
Getting an R^2 from glm and lme is harder and controversial: Some
people say simply, "Don't do it, because it doesn't mean anything." For
those who want to do it anyway, the following papers discuss alternate
definitions of R^2 outside the standard normal linear model:
NagelKerke, N. J. D. (1991) "A note on a general definition of the
coefficient of determination", Biometrika 78: 691-2.
Cox, D. R. and Wermuth, N. (1992) "A comment on the coefficient of
determination for binary responses", The American Statistician 46: 1-4.
Cameron, A. Colin and Windmeijer, F. A. G. (1997) "An R-squared
measure of goodness of fit for some common nonlinear regression models",
Journal of Econometrics 77: 329-342.
I'd be pleased to hear other comments on this issue. Hope this
helps. spencer graves
Ronaldo Reis Jr. wrote:
> Hi,
>
> I have something like this:
>
>
>>x <- 1:10
>>y2 <- 30+5*x+rnorm(x,sd=3)
>>y <- c(y1,y2)
>>x <- c(x,x)
>>plot(x,y)
>>x <- 1:10
>>y1 <- 1+5*x+rnorm(x,sd=2)
>>y2 <- 30+5*x+rnorm(x,sd=5)
>>y <- c(y1,y2)
>>x <- c(x,x)
>>f <- factor(rep(c("a","b"),c(10,10)))
>>m <- lm(y~x+f)
>>anova(m)
>
> Analysis of Variance Table
>
> Response: y
> Df Sum Sq Mean Sq F value Pr(>F)
> x 1 4062.9 4062.9 400.04 2.990e-13 ***
> f 1 4421.5 4421.5 435.35 1.496e-13 ***
> Residuals 17 172.7 10.2
> ---
> Signif. codes: 0 `***' 0.001 `**' 0.01 `*' 0.05 `.' 0.1 ` ' 1
>
>>#rsquare of model
>>(4062.9+4421.5)/(4062.9+4421.5+172.7)
>
> [1] 0.980051
>
> In this way I calculate the model rsquare, but how to calculate the rsquare of
> each levels "a" and "b"?
>
> This is only an example, the model maybe glm, lme etc.
>
> Thanks
> Ronaldo
More information about the R-help
mailing list