[R] Kalman filter for a time series

Staff rbertematti at gmail.com
Sun Jul 30 06:26:48 CEST 2017


I found an example at
http://www.bearcave.com/finance/random_r_hacks/kalman_smooth.html shown
below.  But it seems the structSSM function has been removed from KFAS
library so it won't run.  Does anyone know how to fix the code so that it
runs?



library(KFAS)
library(tseries)
library(timeSeries)
library(zoo)
library(quantmod)

getDailyPrices = function( tickerSym, startDate, endDate )
{
  prices = get.hist.quote( instrument = tickerSym, start = startDate,
end = endDate,
                       quote="AdjClose", provider="yahoo",
                       compression="d",  quiet=T)

  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 = 100

SAndP.ts = getDailyPrices( SandP, Start, End )
SAndP.ts = SAndP.ts[1:tsLength]
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