【问题标题】:Using R to create linear models for multiple variables at once but getting summaries per variable使用 R 一次为多个变量创建线性模型,但获取每个变量的摘要
【发布时间】:2021-03-15 15:22:36
【问题描述】:

我有一个数据框,其中包含 268 个观察值和 21 个自变量 (screeningq)。我有另一个数据框(firstweekdata),其中还包括 268 个观察值和各种变量,但我只对一个因变量(V474)感兴趣。每个观察(行)包括一个人的结果。 screeningqfirstweekdata 的子集。

我正在尝试进行回归分析,将 21 个自变量中的 每个 与我感兴趣的因变量一一进行比较。我一直在尝试获取线性模型摘要,但由于某种原因,我无法以每个变量都有一个摘要的方式获得结果。

我使用的代码如下:

    nroscreenq<- ncol(screeningq)
    screeninglinearmod <- list()

    par(mar=c(1.5,1,1.5,1),mfrow=c(5,5))
    for (i in 1:nroscreenq) {
      x1 <- screeningq[,i]
      scatter.smooth(x1, y=firstweekdata$V474, main=paste("Question", i), xlab="", cex = 0.5)
      screeninglinearmod[[i]] <- summary(lm(firstweekdata$V474 ~ screeningq[,i]))
    }

我得到以下结果:

Coefficients:
                   Estimate Std. Error t value Pr(>|t|)  
(Intercept)          36.000     21.313   1.689   0.0940 
screeningq[, i]1     15.000     30.141   0.498   0.6197  
screeningq[, i]100   33.333     22.183   1.503   0.1358  
screeningq[, i]17    35.000     30.141   1.161   0.2480  
screeningq[, i]23    26.000     30.141   0.863   0.3902  
screeningq[, i]25    18.000     30.141   0.597   0.5516  
screeningq[, i]29    15.500     26.103   0.594   0.5538  
screeningq[, i]32    52.000     30.141   1.725   0.0873 
screeningq[, i]35    48.000     30.141   1.593   0.1141  
screeningq[, i]37    27.667     24.610   1.124   0.2633  
screeningq[, i]38    33.500     26.103   1.283   0.2020  
screeningq[, i]44    51.000     30.141   1.692   0.0934 
screeningq[, i]46    -9.000     30.141  -0.299   0.7658  
screeningq[, i]49    41.667     24.610   1.693   0.0932 
screeningq[, i]50    19.667     24.610   0.799   0.4259  
screeningq[, i]51    34.250     23.828   1.437   0.1534  
screeningq[, i]52    13.333     24.610   0.542   0.5890  
screeningq[, i]55    41.000     30.141   1.360   0.1765  
screeningq[, i]56     2.333     24.610   0.095   0.9246  
screeningq[, i]58    20.333     24.610   0.826   0.4104  
screeningq[, i]59    14.667     24.610   0.596   0.5524  
screeningq[, i]60    12.333     24.610   0.501   0.6173  
screeningq[, i]61    39.000     26.103   1.494   0.1380  
screeningq[, i]62    16.667     24.610   0.677   0.4997``` 

等等。列表会继续显示更多行

我尝试了多种方法,但最终得到了类似的列表。我做错了什么?

【问题讨论】:

  • 尝试发布一个*最小的、可重现的示例,”参见stackoverflow.com/questions/5963269/… 学习制作这样的示例是一项很棒的技能,可以帮助您提高程序员的水平,提出更好的问题等,所以它绝对是值得!

标签: r


【解决方案1】:

我已经使用 lapply 改编了这个建议 here 中的代码。我认为这就是您正在寻找的输出,列表中单个自变量输出的一系列摘要。

# create a toy dataset with one dependent variable and three dependent variables
DV <- rnorm(20, 10, 3)
IV1 <- rnorm(20, 8, 3)
IV2 <- rnorm(20, 9, 3)
IV3 <- rnorm(20, 9, 3)

df <- data.frame(DV, IV1, IV2, IV3)
cols <- list("IV1", "IV2", "IV3")
forms <- paste('DV ~', cols)
forms

#> [1] "DV ~ IV1" "DV ~ IV2" "DV ~ IV3"

a <- lapply(forms, lm, data = df)
a

#> [[1]]
#> 
#> Call:
#> FUN(formula = X[[i]], data = ..1)
#> 
#> Coefficients:
#> (Intercept)          IV1  
#>     12.1796      -0.3148  
#> 
#> 
#> [[2]]
#> 
#> Call:
#> FUN(formula = X[[i]], data = ..1)
#> 
#> Coefficients:
#> (Intercept)          IV2  
#>   9.8944853   -0.0008378  
#> 
#> 
#> [[3]]
#> 
#> Call:
#> FUN(formula = X[[i]], data = ..1)
#> 
#> Coefficients:
#> (Intercept)          IV3  
#>     11.5488      -0.1798

reprex package (v0.3.0) 于 2021 年 3 月 15 日创建

顺便说一句,我链接的问题和这个答案都是很好的“最小的、可重现的例子”的例子。我已使用 R 包 reprex() 确保示例为 reproducible 并将其复制/粘贴到此处。

【讨论】:

  • 非常感谢您的帮助!我现在实际上注意到问题实际上出在数据类型上,它是字符。在我将数据框更改为数字格式后,原始代码开始工作。 #newbieproblems
  • @KristaK,很高兴你把它整理出来
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2023-03-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-07-07
相关资源
最近更新 更多