如何为R中的预定义向量创建回归模型列表?

要为预定义向量创建回归模型列表,我们可以创建一个空白列表,然后使用for循环创建回归模型列表。例如,如果我们有两个向量分别表示x和y,并且我们想在x和y之间创建回归模型列表,则可以使用空白列表list()并执行for循环特定次数,如以下示例所示。

例1

x<-rnorm(20)
y<-rnorm(20)
List_1=list()
for (i in 1:10) List_1[[i]] = lm(y~x)
List_1
输出结果
[[1]]
Call:
lm(formula = y ~ x)
Coefficients:
(Intercept) x
   -0.1294 0.2600

[[2]]
Call:
lm(formula = y ~ x)
Coefficients:
(Intercept) x
   -0.1294 0.2600

[[3]]
Call:
lm(formula = y ~ x)
Coefficients:
(Intercept) x
   -0.1294 0.2600

[[4]]
Call:
lm(formula = y ~ x)
Coefficients:
(Intercept) x
   -0.1294 0.2600

[[5]]
Call:
lm(formula = y ~ x)
Coefficients:
(Intercept) x
   -0.1294 0.2600

[[6]]
Call:
lm(formula = y ~ x)
Coefficients:
(Intercept) x
   -0.1294 0.2600

[[7]]
Call:
lm(formula = y ~ x)
Coefficients:
(Intercept) x
   -0.1294 0.2600

[[8]]
Call:
lm(formula = y ~ x)
Coefficients:
(Intercept) x
   -0.1294 0.2600

[[9]]
Call:
lm(formula = y ~ x)
Coefficients:
(Intercept) x
   -0.1294 0.2600

[[10]]
Call:
lm(formula = y ~ x)
Coefficients:
(Intercept) x
   -0.1294 0.2600

例子2

x1<-rpois(2000,1)
x2<-rpois(2000,2)
x3<-rpois(2000,2)
y1<-rpois(2000,5)
List_2=list()
for (i in 1:10) List_2[[i]] = lm(y1~x1+x2+x3)
List_2
输出结果
[[1]]
Call:
lm(formula = y1 ~ x1 + x2 + x3)
Coefficients:
(Intercept) x1 x2 x3
   5.065561 -0.039094 0.006488 -0.035636

[[2]]
Call:
lm(formula = y1 ~ x1 + x2 + x3)
Coefficients:
(Intercept) x1 x2 x3
   5.065561 -0.039094 0.006488 -0.035636

[[3]]
Call:
lm(formula = y1 ~ x1 + x2 + x3)
Coefficients:
(Intercept) x1 x2 x3
   5.065561 -0.039094 0.006488 -0.035636

[[4]]
Call:
lm(formula = y1 ~ x1 + x2 + x3)
Coefficients:
(Intercept) x1 x2 x3
 5.065561 -0.039094 0.006488 -0.035636

[[5]]
Call:
lm(formula = y1 ~ x1 + x2 + x3)
Coefficients:
(Intercept) x1 x2 x3
   5.065561 -0.039094 0.006488 -0.035636

[[6]]
Call:
lm(formula = y1 ~ x1 + x2 + x3)
Coefficients:
(Intercept) x1 x2 x3
 5.065561 -0.039094 0.006488 -0.035636

[[7]]
Call:
lm(formula = y1 ~ x1 + x2 + x3)
Coefficients:
(Intercept) x1 x2 x3
 5.065561 -0.039094 0.006488 -0.035636

[[8]]
Call:
lm(formula = y1 ~ x1 + x2 + x3)
Coefficients:
(Intercept) x1 x2 x3
 5.065561 -0.039094 0.006488 -0.035636

[[9]]
Call:
lm(formula = y1 ~ x1 + x2 + x3)
Coefficients:
(Intercept) x1 x2 x3
 5.065561 -0.039094 0.006488 -0.035636

[[10]]
Call:
lm(formula = y1 ~ x1 + x2 + x3)
Coefficients:
(Intercept) x1 x2 x3
 5.065561 -0.039094 0.006488 -0.035636