我想知道是否有办法为每个回归方程提取R2。
l_ply(models, summary$r.squared, .print = TRUE)
我试过
Error in summary$r.squared : object of type 'closure' is not subsettable
但是这会抛出以下错误消息
{{1}}
答案 0 :(得分:6)
您可以这样做以获得R平方值和系数:
Using
答案 1 :(得分:5)
使用broom包将统计分析对象转换为data.frames,将dplyr
转换为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
答案 2 :(得分:2)
你可以试试这个
<div class="input-group date">
<input type="text" class="form-control"><span class="input-group-addon"><i class="glyphicon glyphicon-th"></i></span>
</div>
答案 3 :(得分:2)
如果您尝试
> 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
似乎也可以使用。但我从未使用过那个包,所以我不能说什么是最好的。)