Comments (4)
There are multiply issues here.
- In order to get same results as with arima function, you need to set H to zero, i.e. you do not want additional noise.
- Your initial values to Q throws an error as you are starting from value exp(100)=2.688117e+43 which is way too large for any practical application (it would cause severe numerical problems, so model checking throws error without even attempting to run Kalman filter).
- You need to restrict the AR coefficients to stationary region. See for example function
artransform
(note that I just found a memory leak there, I will upload new version to CRAN today). - You need to also update P1, the initial covariance matrix of the states.
Here's an example with Nile data, I omit the predifferencing and fit ARIMA(2,1,1) model:
data(Nile)
model <- SSModel(Nile ~ SSMarima(ar=c(0.1,0.2),ma=c(0.1),d=1), H=0)
#due to stationarity checks it's easier to use own objective function and optim directly
likfn <- function(pars, model, estimate=TRUE){
# use artransform to transform parameters so the model is stationary and invertible
tmp <- try(SSMarima(artransform(pars[1:2]),
artransform(pars[3]),d=1,Q = exp(pars[4])),silent=TRUE)
if(!inherits(tmp,"try-error")){
model["T","arima"] <- tmp$T
model["R","arima"] <- tmp$R
model["P1","arima"] <- tmp$P1
model["Q","arima"] <- tmp$Q
if(estimate){
-logLik(model)
} else model
} else {
if(estimate){
1e100
} else model
}
}
inits <- c(0.1,0.5,0.5,log(15000))
fit <- optim(inits, likfn, model=model, method='BFGS')
model <- likfn(fit$par,model,FALSE)
model$T
model$R
model$Q
#very similar results as with arima function:
arima(x=Nile, order = c(2,1,1))
Note that in some cases we get wildly different results due to different starting values, numerical instabilities near the stationary region, and because sigma^2 is estimated different way in arima
(if I remember correctly it is computed from standardized residuals after running Kalman filter with unit variance).
from kfas.
Thank you and I updated my KFAS package. The example code worked for me, but there is a new question. When I change the initial parameters in the above example, e.g. inits <- c(0.1,0.5,0.5,log(15000)) changed to inits <- c(0.1,0.5,0.5,log(3000)), the estimated model parameters also changed greatly. Is there a reason why this happens? Is there a rule on prescribing the initial conditions?
from kfas.
The initial value log(3000) is so far off that during the optimization routine the variance Q starts to go towards zero, which causes the model to become degenerate (as the H=0 as well). There is a check in logLik.SSModel against cases where all values of H and Q are smaller than machine epsilon, but it doesn't always help, like here where adding stricter check in likfn
(like if(exp(pars[4])<1e-5) return(1e100)
) still results a case where parameter estimates go towards the boundary.
In general state space models there isn't really rules for initial conditions, and the likelihood contains multiple maximums, so basically you try to guess something and test different set-ups and see what gives you best likelihood (and that the values are not insensible like in above example). For simple models you can try to infer the initial values from the data, for example here you have just one variance component, so good initial value for that could be log(var(Nile))
. For ARMA parameters, zero is often a good first try, or some other small values around zero (using artransform
for checking the values after transformation).
from kfas.
I guess this can be closed now.
from kfas.
Related Issues (20)
- Handle intercepts in the model HOT 6
- Model non-gaussian multivariate outcome HOT 5
- Positive log likelihood for Poisson and Binomial distribution HOT 5
- How to predict more time series HOT 1
- Wrong convergence without warnings or errors HOT 15
- Requesting State Offset Term or Control Input specification HOT 2
- KFS function HOT 4
- Change .f95 suffix to .f90 HOT 1
- Exact diffuse intialisation HOT 3
- Exogenous variable
- Documentation: "more complex model" example HOT 2
- Returning Importance Samples in predict.SSModel HOT 3
- How to extract time varying coefficients and CIs corresponding to each variable in SSMregression HOT 2
- How to extract the overdispersion parameters in regression models assuming negative binomial distribution HOT 7
- Poor time variability of time-varying regression coefficients and Inconsistency of param estimates among methods in fitSSM HOT 9
- Standardized residuals HOT 6
- Difficulty with setting a1 and P1 when using SSMseasonal and reduced number of harmonics HOT 1
- Question building an Ar(2) model HOT 6
- same Log Likelihood of different SSModel HOT 6
- Kalman filter HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from kfas.