如何在 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
广告