gpt4 book ai didi

r - 为按因子分组的每个回归提取 R^2(R 平方)值

转载 作者:行者123 更新时间:2023-12-02 05:32:59 25 4
gpt4 key购买 nike

我想知道是否有办法为每个回归方程提取 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

关于r - 为按因子分组的每个回归提取 R^2(R 平方)值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29805423/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com