[英]Extract R^2 (R-squared) value for each regression grouped by a factor
我想知道是否有辦法為每個回歸方程提取R2。
d <- data.frame(
state = rep(c('NY', 'CA'), 10),
year = rep(1:10, 2),
response= rnorm(20)
)
library(plyr)
models <- dlply(d, "state", function(df)
lm(response ~ year, data = df))
ldply(models, coef)
l_ply(models, summary, .print = TRUE)
我試過了
l_ply(models, summary$r.squared, .print = TRUE)
但是這會拋出以下錯誤消息
Error in summary$r.squared : object of type 'closure' is not subsettable
您可以這樣做以獲得R平方值和系數:
ldply(models, function(x) {r.sq <- summary(x)$r.squared
intercept <- summary(x)$coefficients[1]
beta <- summary(x)$coefficients[2]
data.frame(r.sq, intercept, beta)})
# state r.sq intercept beta
#1 CA 0.230696121 0.4915617 -0.12343947
#2 NY 0.003506936 0.1971734 -0.01227367
使用掃帚包將統計分析對象轉換為data.frames和dplyr
for bind_rows
:
library(dplyr) ; library(broom)
cbind(
state = attr(models, "split_labels"),
bind_rows(lapply(models, function(x) cbind(
intercept = tidy(x)$estimate[1],
beta = tidy(x)$estimate[2],
glance(x))))
)
state intercept beta r.squared adj.r.squared sigma statistic p.value df logLik AIC BIC deviance df.residual
1 CA 0.38653551 -0.05459205 0.01427426 -0.10894146 1.434599 0.1158477 0.7423473 2 -16.68252 39.36505 40.27280 16.46460 8
2 NY 0.09028554 -0.08462742 0.04138985 -0.07843642 1.287909 0.3454155 0.5729312 2 -15.60387 37.20773 38.11549 13.26968 8
你可以試試這個
sapply(models, function(x) summary(x)$r.squared)
CA NY
0.05639 0.23751
如果你試試
> typeof( summary )
[1] "closure"
你看到'摘要'是一個功能。 您正在嘗試訪問結果的字段,但是summary$r.squared
嘗試訪問函數/閉包上的該字段。
使用匿名函數,
> l_ply( models, function( m ) summary( m )$r.squared, .print = TRUE )
[1] 0.2319583
[1] 0.01295825
將工作和打印結果。 但是,你說你想“提取結果”。 這可能意味着您想要使用結果而不僅僅是打印它。
從l_ply
的文檔中(你可以在R提示符下鍵入?l_ply
):
對於列表的每個元素,應用函數並丟棄結果。
(因此,如果要掛起結果,此功能將無效。)
使用標准的sapply
/ lapply
將導致
> a <- sapply( models, function( t ) summary( t )$r.squared )
> a
CA NY
0.23195825 0.01295825
> typeof( a )
[1] "double"
> is.vector( a )
[1] TRUE
> # or alternatively
> l <- lapply( models, function( t ) summary( t )$r.squared )
> l
$CA
[1] 0.2319583
$NY
[1] 0.01295825
> typeof( l )
[1] "list"
任何一個應該工作 - 選擇哪個結果(向量或列表)更容易用於你想做的事情。 (如果不確定,只需選擇sapply
。)
(或者,如果你想使用的功能從plyr
包, laply
, ldply
和llply
似乎工作過,但我從來沒有用過這個包,所以我不能說什么是最好的。)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.