# How do I fit a simple filter with R?

I have two timeseries, y[t] and x[t]. A domain expert tells me that y[t] should be a linear combination of past values of x[t] up to a certain horizon, i.e.

y[t] = a[0]*x[t] + a[1]*x[t-1] + ... + a[n]*x[t-n]

How do I estimate the a[i] coefficients in R? Indeed, what is the proper name for this kind of model?

-

It is called distributed lag model. The model from of your example can be estimated with simple linear regression. You can use dynlm package for easier lag handling:

> library(dynlm)
> n<-4 ##Number of lag used in a model
> data("USDistLag", package = "lmtest")
> dynlm(consumption~L(gnp,0:4))

Time series regression with "ts" data:
Start = 1967, End = 1982

Call:
dynlm(formula = consumption ~ L(gnp, 0:4), data = USDistLag)

Coefficients:
(Intercept)  L(gnp, 0:4)Series 1  L(gnp, 0:4)Series 2  L(gnp, 0:4)Series 3
-30.54113              0.46253              0.09632             -0.06309
L(gnp, 0:4)Series 4  L(gnp, 0:4)Series 5
0.13511              0.03176
-