*************************************************
*Nonclassical measurement error in price
*************************************************

*predicting common factor for price and retailP, price observation error correlated with factor

OPEN DATA "C:\Users\chimai\school\region_iowamn.xls"
CALENDAR(W) 1988:10:28
DATA(FORMAT=XLS,ORG=COLUMNS) 1988:10:28 2016:11:11 price retailP mpr


nonlin c1=1.0 c2 mu1=0.0 mu2 sigp sigc sigb sigbp sigcp

compute sw=%zeros(3,3)
function %%DLMStart
compute sw(1,1)=sigp, sw(2,2)=sigc, sw(3,3)=sigb, sw(1,2)=sw(2,1)=sigcp, sw(1,3)=sw(3,1)=sigbp
compute c=(||c1,c2||)~~%identity(2)
end %%DLMStart

dec rect af(3,3)
input af
1  0  0
0  0  0
0  0  0

*starting values, guesses
set combined = price+retailP
linreg combined
# constant combined{1}
compute sigp=%seesq*0.5
prj testc

ar1 price
# constant testc
compute mu1=%beta(1),c1=%beta(2), sigc=sigcp=%seesq*0.5


ar1 retailP
# constant testc
compute mu2=%beta(1), c2=%beta(2), sigb=sigbp=%seesq*0.5



dlm(start=%%DLMStart(),type=smooth,y=||price, retailP||,sw=sw,c=c,a=af,$
    mu=||mu1,mu2||,presample=ergodic,pmethod=simplex,piters=10,$
    method=bfgs, iter=1000) 1988:10:28 2016:11:11 xstates


set Trueprice 1988:10:28 2016:11:11 = xstates(t)(1)
graph(footer="Estimated State of True price") 2
# PRICE
# TRUEPRICE

