通过单个模型从循环中提取的多次迭代创建摘要

发布于 2025-01-17 13:22:38 字数 1447 浏览 5 评论 0原文

我创建了一个函数,它使用不同的列作为循环中的因变量来运行 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列)

家人的小鬼朋友的小鬼休闲的小鬼
211
121

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 familyImp of friendsImp of Leisure
211
121

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

旧时浪漫 2025-01-24 13:22:38

由于您没有提供最小工作示例,我们无法正确诊断您的问题。

也就是说,一种选择是确保您的函数返回模型列表,然后将其提供给 < code>modelsummary 函数。 在此示例代码中,请注意末尾的 return() 调用和 modelsummary() 调用:

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 <- list()
  
  for (i in seq_along(a)) {
    lm_fit <- lm_model %>% 
      fit(as.formula(paste(colnames1[i], "~ .")), data = a)

    out_lst[[i]] <- lm_fit
    
    #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
  }

  return(out_lst)
  
}

library(modelsummary)
models <- quantmodel(set1)
modelsummary(dvnames(models))

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 the return() call at the end and the modelsummary() call:

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 <- list()
  
  for (i in seq_along(a)) {
    lm_fit <- lm_model %>% 
      fit(as.formula(paste(colnames1[i], "~ .")), data = a)

    out_lst[[i]] <- lm_fit
    
    #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
  }

  return(out_lst)
  
}

library(modelsummary)
models <- quantmodel(set1)
modelsummary(dvnames(models))
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文