I have a question about a code I wrote which should update a linear regression.

data<-rnorm(100,mean= 3,sd=1.8)
reg.cuve<-rep(0,length(data)-20)
x<-seq(1:20)
for(i in 20:length(data)){
  reg<-lm(data[i-19:i]~x)
  reg.curve[i]<-tail(fitted(reg),1)
}

the error must happen in the for loop. The error message I get is

Error in model.frame.default(formula = data[i - 19:i] ~ x, drop.unused.levels = TRUE) : 
  variable lengths differ (found for 'x') 

However running all the commands for a fixed i, everything works. The original data is air pollution at hundred consecutive days. Thanks for your help

有帮助吗?

解决方案

The colon in

i-19:i

has higher precedence than the subtraction, so for i=20, you get 20 - c(19,20) rather than (20-19):20 as you were expecting. Suitably placed brackets solve this

data<-rnorm(100,mean= 3,sd=1.8)
reg.curve<-rep(0,length(data)-20)
x<-seq(1:20)
for(i in 20:length(data)){
  reg<-lm(data[(i-19):i]~x)
  reg.curve[i]<-tail(fitted(reg),1)
}

其他提示

The problem is [i-19:i] which doesn't do what you think it does

For example when i = 20

20-19:20
[1] 1 0

What you need is [(i-19):i]

(20-19):20
[1]  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top