问题描述
由于我只是在学习R,所以我不确定如何解决这个问题.我正在尝试获取一个显示以下内容的数据框:
As I am just learning R, I am not sure how to solve this. I am trying to get a data frame that shows me the following:
Model Number | adj.r.squared | sigma | statistic | df
------------------------------------------------------
Model 1 | 0.465 | 0.437 | 459.0. | 8
Model 2 | 0.0465 | 0.0437 | 659.0. | 7
我正在使用 broom 包,以便通过glance()获取这些统计信息,并为此创建了一个函数:
I am using the broom package in order to get these statistics with glance() and created a function for it:
glancing <- function(x) {
glance(x)[c("adj.r.squared", "sigma", "statistic", "df")]
}
我使用的数据集具有9个变量(跳舞能力",能量",响度",言语",声学",活力",价",节奏",工具性"),我需要所有可能的组合以进行线性回归来预测受欢迎程度得分
I am using a dataset that has 9 variables ("danceability","energy", "loudness", "speechiness", "acousticness", "liveness", "valence", "tempo", "instrumentalness) and I needed all the combination possible for linear regression to predict the popularity score
我找到了一种将所有公式放在列表中的方法:
I found a way to put all the formulas in a list:
characteristics <- c("popularity","danceability","energy", "loudness", "speechiness", "acousticness", "liveness", "valence", "tempo", "instrumentalness")
N <- list(1,2,3,4,5,6,7,8,9)
COMB <- sapply(N, function(m) combn(x=characteristics[2:10], m))
formulas <- list()
k=0
for(i in seq(COMB)){
tmp <- COMB[[i]]
for(j in seq(ncol(tmp))){
k <- k + 1
formulas[[k]] <- formula(paste("popularity", "~", paste(tmp[,j], collapse=" + ")))
}
}
我还能够将列表中的每个公式分配给具有线性模型的对象:
I was also able to assign each formula in the list to an object with the linear model:
#Assign each model to a variables
for(i in 1:length(formulas)) {
assign(paste0("model",i),lm(formulas[[i]], data=training_data))
}
这留下了511个模型(对象),我必须手动将它们放入 glancing函数,然后合并到一个数据框中.
This leaves me with 511 models (objects), which I have to put into the glancing function manually, and then combine into a data frame.
是否有更简单的方法可以完全做到这一点?
我已经尝试过将列表转换为数据帧或向量,但是由于该类是公式"这一事实而似乎失败了.
I already tried to convert the list into a data frame or vector, but it seems to fail due to the fact the class is a "formula".
感谢您的帮助!
推荐答案
使用 assign
替换此循环:
for(i in 1:length(formulas)) {
assign(paste0("model",i),lm(formulas[[i]], data=training_data))
}
使用列表进行此循环:
model_list = list()
for(i in 1:length(formulas)) {
model_list[[i]] = lm(formulas[[i]], data=training_data)
}
然后,如果您想一目了然
所有这些内容:
Then if you want to glance
all of them:
library(dplyr)
library(broom)
glance_results = bind_rows(lapply(model_list, glance))
这篇关于将具有9个变量的lm()模型组合的所有可能的broom :: glance统计信息放入R中的数据框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!