通过单个模型从循环中提取的多次迭代创建摘要
我创建了一个函数,它使用不同的列作为循环中的因变量来运行 lm 回归的多次迭代。我正在提取每次迭代的摘要和相关图,但我无法创建所有迭代结果的单个摘要表。 由于我只有8列,所以我认为可以做到。 这是我的函数,
quantmodel<-function(a){
i<-1
a <- janitor::clean_names(a)
colnames1 <- colnames(a)
lm_model <- linear_reg() %>%
set_engine('lm') %>%
set_mode('regression')
out_lst <- vector('list', ncol(a))
for (i in seq_along(a)) {
lm_fit <- lm_model %>%
fit(as.formula(paste(colnames1[i], "~ .")), data = a)
#Saving relevance plot of each parameter
temp_plot = vip(lm_fit ,geom = "col", aesthetics = list(color = "black", fill = "black"))
ggsave(temp_plot, file=paste0("plot_", i,".png"), width = 14, height = 10, units = "cm")
#Saving pdf of individual summaries
pdf(paste0("summary_", colnames1[i],".pdf"), width = 10,height = 3)
grid.table(coef(summary(lm_fit$fit)))
dev.off()
paste0("m",i)<-lm_fit$fit
}
}
quantmodel(set1)
数据如下: Set1(前3列)
家人的小鬼 | 朋友的小鬼 | 休闲的小鬼 |
---|---|---|
2 | 1 | 1 |
1 | 2 | 1 |
I have created a function which is running multiple iteration of lm regression using different columns as the dependent variable over a loop. I am extracting the summary of each iteration and the relavance graph, but I am not able to create a single summary table of all the iteration results.
Since I have only 8 columns, I think it can be done.
Here's my function with data below
quantmodel<-function(a){
i<-1
a <- janitor::clean_names(a)
colnames1 <- colnames(a)
lm_model <- linear_reg() %>%
set_engine('lm') %>%
set_mode('regression')
out_lst <- vector('list', ncol(a))
for (i in seq_along(a)) {
lm_fit <- lm_model %>%
fit(as.formula(paste(colnames1[i], "~ .")), data = a)
#Saving relevance plot of each parameter
temp_plot = vip(lm_fit ,geom = "col", aesthetics = list(color = "black", fill = "black"))
ggsave(temp_plot, file=paste0("plot_", i,".png"), width = 14, height = 10, units = "cm")
#Saving pdf of individual summaries
pdf(paste0("summary_", colnames1[i],".pdf"), width = 10,height = 3)
grid.table(coef(summary(lm_fit$fit)))
dev.off()
paste0("m",i)<-lm_fit$fit
}
}
quantmodel(set1)
Data:
Set1(first 3 columns)
Imp of family | Imp of friends | Imp of Leisure |
---|---|---|
2 | 1 | 1 |
1 | 2 | 1 |
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
由于您没有提供最小工作示例,我们无法正确诊断您的问题。
也就是说,一种选择是确保您的函数返回模型列表,然后将其提供给 < code>modelsummary 函数。 在此示例代码中,请注意末尾的
return()
调用和modelsummary()
调用:Since you do not provide a MINIMAL WORKING EXAMPLE, it is impossible for us to diagnose your problem correctly.
That said, one option would be to ensure that your function returns a list of models, and then feed that to the
modelsummary
function. In this example code, note thereturn()
call at the end and themodelsummary()
call: