I have started coding a Kalman filter in R. I am using dlmModreg to build an object of class dlm, which I am planning to use as my input to dlmFilter. But, I am stuck in the dlmModreg step itself. I am providing the parameters for dlmModreg as described in 1. This is my code
linear.reg <- lm(y.learn$hospitalizations ~ y.learn$google);
flu.model <- dlmModReg(y$google,dV=25.66,dW=0.001016);
I get the following error
Error in dlmModReg(y.learn$google, dV = 25.66, dW = 0.001016) :
Inconsistent dimensions of arguments
For dV I used the residual variance obtained from the linear model(linear.reg)
However, this error vanishes when I remove dW.
Also if I change my code as
dlmModReg(y.learn$google, dV = 25.66, m0 = 0.023711, c0 = 0.001016)
I get the error below
Error in dlmModReg(y.learn$google, dV = 25.66, m0 = 0.023711, c0 = 0.001016) :
unused argument(s) (c0 = 0.001016)
Here m0 is the Beta(google) estimate and c0 is the Beta(google) variance.
I am not sure what is the right value to be used for parameters dW, dV, m0, c0?
Best Answer
Your
dlmModReg
has two states in it, an intercept and a slope, sodW
needs to be two elements long. A quick way to check what you need is to dodlmModReg (y$google)
and see what prints out. (Or perhapsstr (dlmModReg (y$google))
for a more compact view.)That would show you that
W
is a 2x2 matrix, whileV
is 1x1, and hence you can specify it as a single number.As for your other error, remember that R is case-sensitive and the parameter is
C0
notc0
.DLMs are wonderful, and the
dlm
package is the easiest to use, but you really do have to poke around under the hood to understand what it's doing for you.