I have to apply regression function progressively to a time series data (vector "time" and "tm" and I'm using a For Loop as follow:
top<-length(time)
for(k in 2:top){
lin.regr<-lm(tm[1:k] ~ log(time[1:k]))
slope[k]<-coef(lin.regr)[2]
}
But for vectors' length of about 10k it becomes very slow. Is there a faster alternative (maybe using apply function)?
In a more easy problem: if I have a vector like x<-c(1:10) how can I build a y vector containing (for example) the progressive sum of x values? Like:
x
1 2 3 4 5 6 7 8 9 10
y
1 3 6 10 15 21 28 36 45 55
results <- sapply(2:top,function (k) coef(lm(tm[1:k] ~ log(time[1:k])))[2])
~apply family of functions is the fastest way to iterate in R.
can also look at using lm.fit() to speed up your regrssion a bit
cumsum(1:10)
is how to do the second question