简体   繁体   English

For循环中的线性回归

[英]Linear Regression in For Loop

I get an error for running the code below. 运行以下代码时出现错误。 I haven not figured out what I am doing wrong - sorry if it is obvious, I am new to R. The idea is to "generate" 100 regressions and output the estimated slope 100 times. 我还没有弄清楚我在做什么错-很显然,我是R的新手。我的想法是“生成” 100个回归并输出估计的斜率100次。

set.seed(21) 
x <- seq(1,40,1) 
for (i in 1:100 ) {
  y[i] = 2*x+1+5*rnorm(length(x))
  reg[i] <- lm(y[i]~x)
  slp[i] <-  coef(reg[i])[2]
  }

You need to create the matrix/vector y , reg , slp first, to be able to write to position i like: y[i] <- . 您需要首先创建矩阵/向量yregslp ,以便能够像这样写到位置iy[i] <- You can do something along: 您可以执行以下操作:

set.seed(21) 
x <- seq(1,40,1) 
slp <- numeric(100)
for (i in 1:100 ) {
  y <- 2*x+1+5*rnorm(length(x))
  reg <- lm(y~x)
  slp[i] <-  coef(reg)[2]
}

   > slp
  [1] 2.036344 1.953487 1.949170 1.961897 2.098186 2.027659 2.002638 2.107278
  [9] 2.036880 1.980800 1.893701 1.925230 1.927503 2.073176 2.101303 1.943719
      ...
 [97] 1.966039 2.041239 2.063801 2.066801

There are several problems with the way you use indexing. 使用索引的方式存在几个问题。 You'll probably need to spend some time again on a short tutorial about R for beginners, and not "rush" to loops and regressions... 您可能需要花一些时间针对初学者编写有关R的简短教程,而不要“急于”尝试循环和回归...

In the end, you want to have a vector containing 100 slope values. 最后,您想要一个包含100个坡度值的向量。 You need to define this (empty) vector 'slp' prior to running the loop and then fill each i th element with its value in the loop. 您需要在运行循环之前定义此(空)向量'slp',然后在循环中为其每个第i个元素填充其值。

On the other hand, 1) at each iteration you don't fill the i th element of y but create a whole new vector y with as many values as there are in x... 2) you don't need to keep every regression so you don't need to "index" your object reg. 另一方面,1)在每次迭代中,您不会填充y的第i个元素,而是创建一个新的向量y,其值与x中的值相同... 2)您不需要保留每个回归,因此您不需要“索引”您的对象reg。

So here it is: 所以这里是:

set.seed(21) 
x <- seq(1,40,1) 
slp=rep(NA,100)
for (i in 1:100) {
    y = 2*x+1+5*rnorm(length(x))
    reg <- lm(y~x)
    slp[i]<-coef(reg)[2]
}
print(slp)

In addition to the other answers, there is a better (more efficient and easier) possibility. 除了其他答案,还有更好(更有效和更容易)的可能性。 lm accepts a matrix as input for y: lm接受一个矩阵作为y的输入:

set.seed(21)
y <- matrix(rep(2*x + 1, 100) + 5 *rnorm(length(x) * 100), ncol = 100)
reg1 <- lm(y ~ x)
slp1 <- coef(reg1)[2,]
all.equal(slp, slp1)
#[1] TRUE

If you had a function other than lm and needed a loop, you should use replicate instead of a for loop: 如果您具有lm以外的功能并需要循环,则应使用replicate而不是for循环:

set.seed(21) 
slp2 <- replicate(100, {
  y = 2*x+1+5*rnorm(length(x))
  reg <- lm(y~x)
  unname(coef(reg)[2])
})
all.equal(slp, slp2)
#[1] TRUE

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM