我有一个线性回归,它使用城市作为R中的组:
pop_model <- lmList(Value ~ Year | City, data = df)我可以使用下面的代码创建相应的R-Squared的向量:
r_squareds <- summary(pop_model)$r.squared但这并没有给我城市的名称。所以,我不知道哪个R平方表示哪个回归。我如何制作一个表,将这些R-Squared及其名称记录到一个数据帧中,以获得这样的数据帧:
城市| R-Squared
发布于 2021-08-24 03:55:14
您可以从residuals的names中提取城市名称。
data <- data.frame(city = names(pop_model$residuals),
R_squared = pop_model$r.squared)使用mtcars数据集的示例。
library(nlme)
pop_model <- lmList(mpg ~ am | cyl, data = mtcars)
tmp <- summary(pop_model)
data <- data.frame(cyl = names(tmp$residuals),
R_squared = tmp$r.squared)
data
# cyl R_squared
#1 4 0.287289249
#2 6 0.281055142
#3 8 0.002464789https://stackoverflow.com/questions/68901520
复制相似问题