[R] Extract r.squared using cbind in lm

Petr PIKAL petr.pikal at precheza.cz
Thu Sep 8 15:18:41 CEST 2011


Hi
> 
> Hello,
> 
> I am using cbind in a lm-model. For standard lm-models
> the r.squared can be easily extracted with summary(model)$r.squared,
> but that is not working in in the case with cbind.
> 
> Here an example to illustrate the problem:
> a  <- c(1,3,5,2,5,3,1,6,7,2,3,2,6)
> b <- c(12,15,18,10,18,22,9,7,9,23,12,17,13)
> c  <- c(22,26,32,33,32,28,29,37,34,29,30,32,29)
> 
> data  <- data.frame(a,b,c)
> 
> model_a <-lm(b~a,data=data)
> model_b <-lm(cbind(b,c)~a,data=data)
> 
> summary(model_a)$r.squared
> summary(model_b)$r.squared
> 
> 
> How can I access r.squared in my case? Is there any option?

> summary(model_b)[[1]]$r.squared
[1] 0.03650572

The result is list of 2 models, one for response b and one for response c

you can get values for both by lapply or sapply

sapply(summary(model_b), "[", 8)

putting all together in some data frame is a matter of unlisting results 
and transforming them according to your wish.

see ?unlist, ?coef

Regards
Petr

 In the end, I want a dataframe containing the the intercept,
> slope, p-value and r.squared for all Y's of my regression.
> 
> thank you
> Johannes
> --
> 
> ______________________________________________
> R-help at 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.



More information about the R-help mailing list