[R] Smoothing a Time Series

tgramer at z.zgs.de tgramer at z.zgs.de
Wed Jan 18 10:57:49 CET 2017


Dear R-Team,

i only want to smooth a time series with a Kalmen Filter in R (KFAS).

I found code in the Internet, which I had to change a little bit.

Now I get the following error-message. I don’t know what I have

to do now.

Fehler in is.SSModel(do.call(updatefn, args = c(list(inits, model), update_args)),  : 
  Storage mode of some of the model attributes 'p', 'k', 'm', 'n', 'tv' is not integer.

Thank you very much.

Sincerely

Tobias Gramer




 library(KFAS)
 library(tseries)

 library(timeSeries)

 library(timeDate)
 library(zoo)

 library(quantmod)
 library(xts)
 library(TTR)
 
getDailyPrices=function(tickerSym,startDate,endDate)
{
  prices=c(1318,518,2320,6528,10831,5135,2700,687,7499,790,4524,3686,1677,809,9153,2032,3558,1880,2266,7230,3641,7429,3361,3803,2215,2066,709,1695,4061,150,1555,508,6497,563,1944,1600,4428,3325,10971,3253,1274,2915,1128,588,1600,5837,1760,4196,2103,3658,1600,1288)
  prices.ts=ts(prices)
  return(prices.ts) 	
  
}

kalmanFilter=function(x)
{
   t=x
   if (class(t)!="ts") {
     t=ts(t)
   }
   
   ssModel=structSSM(y=t,distribution=„Gaussian")

   ssFit=fitSSM(inits=c(0.5*log(var(t)),0.5*log(var(t))),model=ssModel)
   kfs=KFS(ssFit$model,smoothing="state",nsim=length(t))
   vals=kfs$a
   lastVal=vals[length(vals)]
   return(lastVal)
}


 Start="2011-01-01"
 End=  "2012-12-31"
 SandP="^GSPC"
 windowWidth=20
 tsLength=52
 SAndP.ts=getDailyPrices(SandP,Start,End)

 SAndP.smoothed=rollapply(data=SAndP.ts,width=windowWidth,FUN=kalmanFilter)
 
 par(mfrow=c(1,1))
 prices=coredata(SAndP.ts[windowWidth:length(SAndP.ts)])
 plot(prices,col="blue",type="l")
 lines(coredata(SAndP.smoothed),col="magenta")
 par(mfrow=c(1,1))
 
 
	[[alternative HTML version deleted]]



More information about the R-help mailing list