Dear R-brains,
I'm rather new to state-space models and would benefit from the extra
confidence in using the excellent package sspir.
In a one-factor model, If I am trying to do a simple regression where
I assume the intercept is constant and the 'Beta' is changing, how do
I do that? How do i Initialize the filter (i.e. what is appropriate to
set m0, and C0 for the example below)?
The model I want is: y = alpha + beta + err1; beta_(t+1) = beta_t + err2
I thought of the following:
library(mvtnorm) # (1)
library(sspir)
# Let's get some data so we can all try this at home
dfrm <- data.frame(
y
c(0.02,0.04,-0.03,0.02,0,0.01,0.04,0.03,-0.01,0.04,-0.01,0.05,0.04,
0.03,0.01,-0.01,-0.01,-0.03,0.02,-0.04,-0.05,-0.02,-0.04,0,0.02,0,
-0.01,-0.01,0.01,0.09,0.03,0.03,0.05,0.04,-0.01,0.05,0.03,0.01,
0.04,0.01,-0.01,-0.02,-0.01,-0.01,
0.06,0.03,0.02,0.03,0.03,0.04,
0.03,0.04,-0.02,-0.03,0.04,0.03,0.05,0.02,0.03,-0.1),
x = c(-0.03,-0.01,0.07,-0.03,-0.07,0.05,0.02,-0.05,-0.04,
-0.02,-0.19,0.07,0.09,0.01,0.01,0,0.05,0,-0.02,-0.09,
-0.12,-0.01,-0.13,0.04,0.04,-0.07,-0.05,-0.03,
-0.01,0.11,0.06,0.03,0.06,0.06,-0.01,0.07,0.01,
0,0.07,0.04,-0.02,0,-0.03,0.04,-0.04,-0.01,0.03,0.02,0.05,0.04,
0.05,0.03,0,-0.04,0.05,0.05,0.06,0.02,0.04,-0.06)
)
ss <- ssm(y ~ tvar(x), time = 1:nrow(dfrm),
family=gaussian(link="identity"),
data=dfrm)
smooth.params <- smoother(kfilter(ss$ss))$m
(1) I read in http://ww.math.aau.dk/~mbn/Teaching/MarkovE05/Lecture3.pdf
that this is requred as there is a bug in sspir.
To what should I set ss$ss$m0 and ss$ss$C0? (I did notice that
smoother() replaces these, but it still matters what I initialize it
to in the first place)
Many thanks!
Tariq Khan
Claus Dethlefsen / Aalborg Sygehus
2005-Dec-01 16:32 UTC
[R] Kalman Smoothing - time-variant parameters (sspir)
Dear Tariq Khan
The initial conditions m0 and C0 can be specified according to your needs. If
you are a Bayesian (as in West&Harrison 1997), you will use m0 and C0 to
express your prior information. If you use a vague prior, you will give a high
weight to your observations in the beginning, and the influence of the prior
will die out fast.
The values of m0 and C0 could also stem from several time-series and express a
random effect of the level of the individual series.
Finally, you may estimate m0 and C0 using maximum likelihood estimation. This is
not done in sspir (but the log-likelihood value is provided from a run of the
filter).
One crude way of specifying m0 and C0 would be to use the estimates from a
static model, i.e.
ss$ss$m0[1:2,] <- coef(lm(y~x,data=dfrm))
ss$ss$C0[1:2,1:2] <- summary(lm(y~x,data=dfrm))$cov.unscaled
smooth.params3 <- kfs(ss)$m
ts.plot(t(smooth.params3))
Note that the 'kfs' function is a shortcut for using
smoother(kfilter()).
Note also, that your variance parameters are both set to unity. Again, you may
discuss how to set these either by previous knowledge or by maximum likelihood
estimation. It is set using
ss$ss$phi[1] <- 2 # observational variance
ss$ss$phi[2] <- .5# variance of the beta-parameter
Hope this helps,
Claus
________________________________
Claus Dethlefsen, Msc, PhD
Statistiker ved Kardiovaskul??rt Forskningscenter
Forskningens Hus
Aalborg Sygehus
Sdr. Skovvej 15
9000 Aalborg
Tlf: 9932 6863
email: aas.claus.dethlefsen at nja.dk <mailto:aas.claus.dethlefsen at
nja.dk>
________________________________
Fra: ??Tariq Khan [mailto:tariq.khan at gmail.com]
Sendt: to 01-12-2005 13:12
Til: R-help at stat.math.ethz.ch; R-sig-finance at stat.math.ethz.ch
Cc: Claus Dethlefsen / Aalborg Sygehus
Emne: Kalman Smoothing - time-variant parameters (sspir)
Dear R-brains,
I'm rather new to state-space models and would benefit from the extra
confidence in using the excellent package sspir.
In a one-factor model, If I am trying to do a simple regression where
I assume the intercept is constant and the 'Beta' is changing, how do
I do that? How do i Initialize the filter (i.e. what is appropriate to
set m0, and C0 for the example below)?
The model I want is: y = alpha + beta + err1; beta_(t+1) = beta_t + err2
I thought of the following:
library(mvtnorm) # (1)
library(sspir)
# Let's get some data so we can all try this at home
dfrm <- data.frame(
y
c(0.02,0.04,-0.03,0.02,0,0.01,0.04,0.03,-0.01,0.04,-0.01,0.05,0.04,
0.03,0.01,-0.01,-0.01,-0.03,0.02,-0.04,-0.05,-0.02,-0.04,0,0.02,0,
-0.01,-0.01,0.01,0.09,0.03,0.03,0.05,0.04,-0.01,0.05,0.03,0.01,
0.04,0.01,-0.01,-0.02,-0.01,-0.01,
0.06,0.03,0.02,0.03,0.03,0.04,
0.03,0.04,-0.02,-0.03,0.04,0.03,0.05,0.02,0.03,-0.1),
x = c(-0.03,-0.01,0.07,-0.03,-0.07,0.05,0.02,-0.05,-0.04,
-0.02,-0.19,0.07,0.09,0.01,0.01,0,0.05,0,-0.02,-0.09,
-0.12,-0.01,-0.13,0.04,0.04,-0.07,-0.05,-0.03,
-0.01,0.11,0.06,0.03,0.06,0.06,-0.01,0.07,0.01,
0,0.07,0.04,-0.02,0,-0.03,0.04,-0.04,-0.01,0.03,0.02,0.05,0.04,
0.05,0.03,0,-0.04,0.05,0.05,0.06,0.02,0.04,-0.06)
)
ss <- ssm(y ~ tvar(x), time = 1:nrow(dfrm),
family=gaussian(link="identity"),
data=dfrm)
smooth.params <- smoother(kfilter(ss$ss))$m
(1) I read in http://ww.math.aau.dk/~mbn/Teaching/MarkovE05/Lecture3.pdf
that this is requred as there is a bug in sspir.
To what should I set ss$ss$m0 and ss$ss$C0? (I did notice that
smoother() replaces these, but it still matters what I initialize it
to in the first place)
Many thanks!
Tariq Khan