[R] Vectorize linear autoregression with variable coefficients

Sandor Benczik sandor.benczik at crabel.ro
Tue Jun 23 11:13:59 CEST 2009


This might be obvious to some, but I can't find a neat way to do it:

Say I have two (very long) numerical vectors a & b of the same length
representing variable coefficients of a linear autoregression.

I want to calculate vector x defined by
x[1] <- b[1]
for (n in 2:length(a)) x[n] <- a[n]*x[n-1] + b[n]

Is there a way to do this vectorially, i.e. without using the 'for'
loop?

Thanks,
Sandor




More information about the R-help mailing list