用于回归 lm(y~x) 的 r for 循环

r for loop for regression lm(y~x)

示例:

df <- data.frame(A=1:5, B=2:6, C=3:7,D=4:8,E=5:9,F=6:10)

我想做一个回归循环 lm(y,x),像 y 一样使用第 1 列和第 2 列,像 x 一样使用其余的列。

我的想法:

lmf <- function (y,x) {
                         f <- lm(y ~ x, data=df)
                         cbind(summary(f)$r.squared,summary(f)$coefficients)                  
                        }
 for(y in 1:3)
  {
    R<- apply(df[,3:6], 2, lmf(y,x)); R
  }

error: model.frame.default(formula = y ~ x, data = df, drop.unused.levels = TRUE) 错误: 可变长度不同(找到 'x')

我举的这个例子很小,但我的数据是 y 的 50 列和 x 的 300 列。

我要的也是一样:lm(df$1~df$3, data=df); lm(df$1~df$4, data=df),[...], lm(df$2~df$3, data=df)... 但以自动方式。此外,我想提取结果 $coefficients 和 $r.squared。

我只是举了一个 iris 中数值变量的例子,因为你可以把它改成你想使用的任何数据集。

我根据名称构建公式我更喜欢使用数字来索引您感兴趣的列。

我会建议,

 result <- sapply(names(iris)[1 : 4], 
   function(x) { 
     lapply(names(iris)[1 : 4], 
            function(y) {
              if (x != y) {
                model <- lm(as.formula(paste0(y, "~", x)), iris) 
                return(list(x = x, 
                            y = y, 
                            r.squared = summary(model)$r.squared, 
                            coefficients =  summary(model)$coefficients))
              }
              })
            })


 result
 ## Sepal.Length Sepal.Width Petal.Length Petal.Width
 ## [1,] NULL         List,4      List,4       List,4     
 ## [2,] List,4       NULL        List,4       List,4     
 ## [3,] List,4       List,4      NULL         List,4     
 ## [4,] List,4       List,4      List,4       NULL       

 result[1, 2]
 ## $Sepal.Width
 ## $Sepal.Width$x
 ## [1] "Sepal.Width"
 ## 
 ## $Sepal.Width$y
 ## [1] "Sepal.Length"
 ## 
 ## $Sepal.Width$r.squared
 ## [1] 0.01382265
 ## 
 ## $Sepal.Width$coefficients
 ## Estimate Std. Error   t value     Pr(>|t|)
 ## (Intercept)  6.5262226  0.4788963 13.627631 6.469702e-28
 ## Sepal.Width -0.2233611  0.1550809 -1.440287 1.518983e-01

或者,您可以将结果存储在一个列表中,然后编写一个单独的函数来遍历该列表,以创建一个仅包含您感兴趣的信息的矩阵。

我有一个使用 dplyr、tidyr 和 broom 包的替代版本。 这个想法是将您想要处理的变量指定为 Y 和 X。根据这些 Y 和 X 集创建 2 个不同的数据集。然后重塑数据集,以便能够将每个 Y 与一个 X 组合。最后,对于每个组合 运行 线性回归并将模型输出保存为数据集。

# Check whether package name is installed...
check_package <- function(package_name) {
  if (!(package_name %in% rownames(installed.packages()))) {
    install.packages(package_name, dependencies = TRUE)
  }
}

check_package("broom")
check_package("dplyr")
check_package("tidyr")

library(dplyr)
library(broom)
library(tidyr)

# example dataset (picking 4 columns)
dt <- data.frame(mtcars) %>% select(mpg, disp, cyl, wt)

# specify which columns we want as y (dependent) and x (independent)
ynames <- c("disp","mpg")
xnames <- c("cyl","wt")

# create and reshape datasets
dt1 <- dt[,ynames]
dt1 <- gather(dt1,y,yvalue)

dt2 <- dt[,xnames]
dt2 <- gather(dt2, x, xvalue)



dt1 %>% 
  group_by(y) %>%                       # group by dependent variable
  do(data.frame(.,dt2)) %>%             # combine each y with all x
  group_by(y,x)%>%                      # get combinations of y and x to regress
  do(tidy(lm(yvalue~xvalue, data=.)))   # return lm output as dataframe


#      y   x        term    estimate  std.error statistic      p.value
# 1 disp cyl (Intercept) -156.608976 35.1805064 -4.451584 1.090157e-04
# 2 disp cyl      xvalue   62.598925  5.4693168 11.445474 1.802838e-12
# 3 disp  wt (Intercept) -131.148416 35.7165961 -3.671918 9.325668e-04
# 4 disp  wt      xvalue  112.478138 10.6353299 10.575896 1.222320e-11
# 5  mpg cyl (Intercept)   37.884576  2.0738436 18.267808 8.369155e-18
# 6  mpg cyl      xvalue   -2.875790  0.3224089 -8.919699 6.112687e-10
# 7  mpg  wt (Intercept)   37.285126  1.8776273 19.857575 8.241799e-19
# 8  mpg  wt      xvalue   -5.344472  0.5591010 -9.559044 1.293959e-10