Taste of R
2011-Jun-03 20:55 UTC
[R] How to reconcile Kalman filter result (by package dlm) with linear regression?
Hello All, I am working with dlm for the purpose of estimating and forecasting with a Kalman filter model. I have succesfully set up the model and started generating results. Of course, I need to somehow be sure that the results make sense. Without any apparent target to compare with, my natural selection is the results by odinary least square. The idea being that if I choose a diffuse prior, the result should approach whatever results that lm() would generates. However, the results turns out to be very different, even though the signs of the estimates coincide. Linear regression gives the following results lm1 = lm(Y~0+X) ######################################################### #> lm1$coef # XV1 XV2 XV3 XV4 #-0.008293796 0.002183550 -0.004053199 0.003030834 ######################################################### and Kalman filter generate this set of results:> m[,1] [,2] [,3] [,4] [24,] -0.06046868 0.002829377 -0.01569903 0.03599957 I am pretty troubled by what I see here. So if anyone would offer me some condolence, as well as helpful advice, I am greatly grateful. Thanks a lot. Here are the code and the data. Thanks a lot. Wei nTotal = nMatrix + 1 BuildMod <- function(x){ L1 = matrix(0,nFactor,nFactor) L1[upper.tri(L1,T)] <- x[1:nMatrix] return(dlm( m0 = rep(0,nFactor), C0 = diag(nFactor)*10, FF = matrix(1,1,nFactor), GG = diag(nFactor), V = tail(x,1)^2, W = crossprod(L1), JFF = matrix(1:4,nr=1), X = X )) } ModFit <- dlmMLE(Y,rep(0.1,nTotal),BuildMod,debug=T) dlmMod <- BuildMod(ModFit$par) V = dlmMod$V W = dlmMod$W m0 = dlmMod$m0 C0 = dlmMod$C0 ModFilt <- dlmFilter(Y,dlmMod) v <- tail(dlmSvd2var(ModFilt$U.C,ModFilt$D.C),1) m <- tail(ModFilt$m,1) Here is the value of Y: 0.0125678739370109 -0.00241285475528163 0.00386919876129071 -0.00352839097011217 0.00285344714211614 0.00374266510625097 0.00797807743013259 -0.00543459628953192 -0.0138447399853609 -0.0102614592879934 -0.0225111772602310 -0.0127304918143123 -0.00730849659351113 -0.0206703167742092 0.0228898867615212 -0.000489089315662759 0.00760340725359960 -0.0138647393981477 -0.00919507833321138 -0.00471195180636822 -0.0146605156203797 -0.00827583539820271 -0.0249602009928649 The value of X is a 23 by 4 matrix, values are seperated by a space. The first column is always 1. 1 0.481797086735748 -0.725213272802754 1.09390587373642 1 -0.336701996049702 -0.631488034316504 1.18588655278069 1 -0.403677908907445 -0.709311690414421 1.40641758335925 1 -0.660006432637389 -0.473151167286555 1.47881106259977 1 -0.598885119922226 -0.669037137010852 1.61035313130471 1 -0.386026586737966 -0.402669473854140 1.72603754245568 1 -0.861884498592061 0.0351890847929021 1.70321193556938 1 -0.575467614543903 -0.0234300742003617 1.81371984835956 1 -1.09697129563504 -0.111460569992454 1.98698301700280 1 0.156970856187597 0.422525286064322 2.07436343379059 1 0.816563280464663 0.716962192825498 1.97867715330570 1 0.223472270913202 0.798985394320738 2.12679959513465 1 -0.901251445288375 1.08970588591384 2.31314336543201 1 0.402507298840520 0.264576256562399 2.45731905177217 1 0.655744153537491 0.745015749090142 2.46883455441981 1 1.97576880567968 2.00737408265316 2.57606000649382 1 1.05416962424537 3 2.47721380452455 1 0.128426774782304 2.85471948506395 2.29259168958684 1 0.0930179549205073 2.03239322655372 2.21711568710261 1 -0.120974956861827 0.458838879419343 1.77364638064773 1 0.446329808606182 0.873634084428291 1.65560019466879 1 1.15541338624045 1.70193933014399 1.53951603064753 1 0.658459632511477 1.42875343279004 1.55839474231335 [[alternative HTML version deleted]]