使用R中的列表中的样本运行线性回归

这是我的数据

mydata=structure(list(
    ABEV3 = c(15.2, 14.9, 15.22, 15.15, 15.18, 15.46, 15.49, 15.5, 15.37, 15.49, 15.64, 15.38), 
    AEDU3 = c(9.01, 8.56, 8.66, 8.64, 8.44, 8.52, 8.29, 8.27, 8.33, 8.26, 8.66, 8.49), 
    ALLL3 = c(7.71, 7.81, 7.57, 7.27, 7.29, 7.07, 7.11, 7.17, 7.27, 7.24, 7.1, 7.1), 
    BBAS3 = c(22.85, 22.78, 22.8, 22.22, 22.51, 21.11, 20.84, 20.79, 20.67, 20.9, 19.82, 18.95)), 
        row.names = c(NA,12L), class = "data.frame")

mydata

现在我构建了4个子样本,并将它们组织在一个名为my_samples的列表中:

samples_size = c(5,8,10,12)#the size of each sub sample
my_samples <- lapply(samples_size,function(x) slice(mydata, 1:x))
my_samples

在每个子样本中,我有4个变量:ABEV3 AEDU3 ALLL3 BBAS3.

我想使用每个子样本运行线性回归.

我想为每个子样本运行的模型是:

ABEV3 ~ AEDU3 + Intercept
ABEV3 ~ ALLL3 + Intercept
ABEV3 ~ BBAS3 + Intercept

AEDU3 ~ ABEV3 + Intercept
AEDU3 ~ ALLL3 + Intercept
AEDU3 ~ BBAS3 + Intercept

ALLL3 ~ ABEV3 + Intercept
ALLL3 ~ BBAS3 + Intercept
ALLL3 ~ AEDU3 + Intercept

sub_sample_regression_results<-list()

对于每个子样本,我想在另一个名为sub_sample_regression_results的lis中保存线性回归的结果.

我怎样才能做到这一点?

最佳答案 也许这样的事情会起作用吗?

首先构造子集列表.

samples_size = c(5,8,10,12)#the size of each sub sample
my_samples <- lapply(samples_size, function(x) mydata[1:x, ])

根据您的9个线性模型创建设计矩阵.

df.design <- setNames(expand.grid(names(mydata), names(mydata)), c("x", "y"))
df.design <- df.design[!(df.design$x == df.design$y) & df.design$y != "BBAS3", ]
lst.design <- apply(df.design, 1, function(w) sprintf("%s ~ %s", w[2], w[1]))
names(lst.design) <- sapply(lst.design, c)

将OLS线性模型应用于每个列表条目和每个设计矩阵条目.在这里,我将返回每个线性模型的系数.

res <- lapply(my_samples, function(df) {
    lapply(lst.design, function(w) coef(lm(as.formula(w), data = df))) })
str(res)
#List of 4
# :List of 9
#..$ABEV3 ~ AEDU3: Named num [1:2] 13.405 0.199
#.. ..- attr(*, "names")= chr [1:2] "(Intercept)" "AEDU3"
#..$ABEV3 ~ ALLL3: Named num [1:2] 17.203 -0.275
#.. ..- attr(*, "names")= chr [1:2] "(Intercept)" "ALLL3"
#..$ABEV3 ~ BBAS3: Named num [1:2] 16.5732 -0.0638
#.. ..- attr(*, "names")= chr [1:2] "(Intercept)" "BBAS3"
#..$AEDU3 ~ ABEV3: Named num [1:2] 0.723 0.525
#.. ..- attr(*, "names")= chr [1:2] "(Intercept)" "ABEV3"
#..$AEDU3 ~ ALLL3: Named num [1:2] 5.715 0.391
#.. ..- attr(*, "names")= chr [1:2] "(Intercept)" "ALLL3"
#..$AEDU3 ~ BBAS3: Named num [1:2] 0.919 0.342
#.. ..- attr(*, "names")= chr [1:2] "(Intercept)" "BBAS3"
#..$ALLL3 ~ ABEV3: Named num [1:2] 21.912 -0.951
#.. ..- attr(*, "names")= chr [1:2] "(Intercept)" "ABEV3"
#..$ALLL3 ~ AEDU3: Named num [1:2] 3.086 0.513
#.. ..- attr(*, "names")= chr [1:2] "(Intercept)" "AEDU3"
#..$ALLL3 ~ BBAS3: Named num [1:2] -10.413 0.793
#.. ..- attr(*, "names")= chr [1:2] "(Intercept)" "BBAS3"
# :List of 9
#..$ABEV3 ~ AEDU3: Named num [1:2] 18.973 -0.434
#.. ..- attr(*, "names")= chr [1:2] "(Intercept)" "AEDU3"
#..$ABEV3 ~ ALLL3: Named num [1:2] 19.681 -0.599
#.. ..- attr(*, "names")= chr [1:2] "(Intercept)" "ALLL3"
#..$ABEV3 ~ BBAS3: Named num [1:2] 19.614 -0.198
#.. ..- attr(*, "names")= chr [1:2] "(Intercept)" "BBAS3"
#..$AEDU3 ~ ABEV3: Named num [1:2] 17.074 -0.559
#.. ..- attr(*, "names")= chr [1:2] "(Intercept)" "ABEV3"
#..$AEDU3 ~ ALLL3: Named num [1:2] 4.42 0.56
#.. ..- attr(*, "names")= chr [1:2] "(Intercept)" "ALLL3"
#..$AEDU3 ~ BBAS3: Named num [1:2] 4.37 0.19
#.. ..- attr(*, "names")= chr [1:2] "(Intercept)" "BBAS3"
#..$ALLL3 ~ ABEV3: Named num [1:2] 24.32 -1.11
#.. ..- attr(*, "names")= chr [1:2] "(Intercept)" "ABEV3"
#..$ALLL3 ~ AEDU3: Named num [1:2] 0.48 0.806
#.. ..- attr(*, "names")= chr [1:2] "(Intercept)" "AEDU3"
#..$ALLL3 ~ BBAS3: Named num [1:2] 1.614 0.262
#.. ..- attr(*, "names")= chr [1:2] "(Intercept)" "BBAS3"
# :List of 9
#..$ABEV3 ~ AEDU3: Named num [1:2] 19.437 -0.487
#.. ..- attr(*, "names")= chr [1:2] "(Intercept)" "AEDU3"
#..$ABEV3 ~ ALLL3: Named num [1:2] 19.949 -0.633
#.. ..- attr(*, "names")= chr [1:2] "(Intercept)" "ALLL3"
#..$ABEV3 ~ BBAS3: Named num [1:2] 19.191 -0.179
#.. ..- attr(*, "names")= chr [1:2] "(Intercept)" "BBAS3"
#..$AEDU3 ~ ABEV3: Named num [1:2] 18.9 -0.68
#.. ..- attr(*, "names")= chr [1:2] "(Intercept)" "ABEV3"
#..$AEDU3 ~ ALLL3: Named num [1:2] 3.923 0.622
#.. ..- attr(*, "names")= chr [1:2] "(Intercept)" "ALLL3"
#..$AEDU3 ~ BBAS3: Named num [1:2] 4.271 0.194
#.. ..- attr(*, "names")= chr [1:2] "(Intercept)" "BBAS3"
#..$ALLL3 ~ ABEV3: Named num [1:2] 23.31 -1.04
#.. ..- attr(*, "names")= chr [1:2] "(Intercept)" "ABEV3"
#..$ALLL3 ~ AEDU3: Named num [1:2] 1.102 0.735
#.. ..- attr(*, "names")= chr [1:2] "(Intercept)" "AEDU3"
#..$ALLL3 ~ BBAS3: Named num [1:2] 2.674 0.215
#.. ..- attr(*, "names")= chr [1:2] "(Intercept)" "BBAS3"
# :List of 9
#..$ABEV3 ~ AEDU3: Named num [1:2] 18.475 -0.369
#.. ..- attr(*, "names")= chr [1:2] "(Intercept)" "AEDU3"
#..$ABEV3 ~ ALLL3: Named num [1:2] 20.202 -0.666
#.. ..- attr(*, "names")= chr [1:2] "(Intercept)" "ALLL3"
#..$ABEV3 ~ BBAS3: Named num [1:2] 17.958 -0.123
#.. ..- attr(*, "names")= chr [1:2] "(Intercept)" "BBAS3"
#..$AEDU3 ~ ABEV3: Named num [1:2] 14.839 -0.413
#.. ..- attr(*, "names")= chr [1:2] "(Intercept)" "ABEV3"
#..$AEDU3 ~ ALLL3: Named num [1:2] 4.993 0.481
#.. ..- attr(*, "names")= chr [1:2] "(Intercept)" "ALLL3"
#..$AEDU3 ~ BBAS3: Named num [1:2] 6.8781 0.0765
#.. ..- attr(*, "names")= chr [1:2] "(Intercept)" "BBAS3"
#..$ALLL3 ~ ABEV3: Named num [1:2] 22.47 -0.989
#.. ..- attr(*, "names")= chr [1:2] "(Intercept)" "ABEV3"
#..$ALLL3 ~ AEDU3: Named num [1:2] 1.869 0.639
#.. ..- attr(*, "names")= chr [1:2] "(Intercept)" "AEDU3"
#..$ALLL3 ~ BBAS3: Named num [1:2] 4.021 0.154
#.. ..- attr(*, "names")= chr [1:2] "(Intercept)" "BBAS3"

如果要组合矩阵列表中的系数,可以执行以下操作

lapply(res, function(x) { 
    ret <- do.call(rbind, x); 
    colnames(ret) <- c("Intercept", "Var"); 
    ret; })
#[[1]]
#                Intercept         Var
#ABEV3 ~ AEDU3  13.4050541  0.19913945
#ABEV3 ~ ALLL3  17.2026515 -0.27525253
#ABEV3 ~ BBAS3  16.5731628 -0.06376647
#AEDU3 ~ ABEV3   0.7231483  0.52470930
#AEDU3 ~ ALLL3   5.7146515  0.39141414
#AEDU3 ~ BBAS3   0.9186297  0.34214255
#ALLL3 ~ ABEV3  21.9122965 -0.95058140
#ALLL3 ~ AEDU3   3.0862335  0.51301853
#ALLL3 ~ BBAS3 -10.4133244  0.79282981
#
#[[2]]
#               Intercept        Var
#ABEV3 ~ AEDU3 18.9733098 -0.4340763
#ABEV3 ~ ALLL3 19.6809503 -0.5991119
#ABEV3 ~ BBAS3 19.6142297 -0.1979183
#AEDU3 ~ ABEV3 17.0743951 -0.5586008
#AEDU3 ~ ALLL3  4.4191430  0.5599467
#AEDU3 ~ BBAS3  4.3700611  0.1900484
#ALLL3 ~ ABEV3 24.3232840 -1.1104527
#ALLL3 ~ AEDU3  0.4804499  0.8064980
#ALLL3 ~ BBAS3  1.6144504  0.2619920
#
#[[3]]
#              Intercept        Var
#ABEV3 ~ AEDU3 19.437140 -0.4873076
#ABEV3 ~ ALLL3 19.949331 -0.6330202
#ABEV3 ~ BBAS3 19.190931 -0.1791020
#AEDU3 ~ ABEV3 18.895360 -0.6797437
#AEDU3 ~ ALLL3  3.922880  0.6223806
#AEDU3 ~ BBAS3  4.271256  0.1943599
#ALLL3 ~ ABEV3 23.309838 -1.0433341
#ALLL3 ~ AEDU3  1.101625  0.7353937
#ALLL3 ~ BBAS3  2.673734  0.2150764
#
#[[4]]
#              Intercept         Var
#ABEV3 ~ AEDU3 18.475130 -0.36934846
#ABEV3 ~ ALLL3 20.202213 -0.66636137
#ABEV3 ~ BBAS3 17.958427 -0.12301408
#AEDU3 ~ ABEV3 14.838614 -0.41272623
#AEDU3 ~ ALLL3  4.993001  0.48129045
#AEDU3 ~ BBAS3  6.878071  0.07646404
#ALLL3 ~ ABEV3 22.470287 -0.98887617
#ALLL3 ~ AEDU3  1.869333  0.63916585
#ALLL3 ~ BBAS3  4.020854  0.15399530
点赞