Modeling Count Time Series with tscount Package
[This article was first published on Yet Another Blog in Statistical Computing » S+/R, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.
The example below shows how to estimate a simple univariate Poisson time series model with the tscount package. While the model estimation is straightforward and yeilds very similar parameter estimates to the ones generated with the acp package (https://statcompute.wordpress.com/2015/03/29/autoregressive-conditional-poisson-model-i), the prediction mechanism is a bit tricky.
1) For the in-sample and the 1-step-ahead predictions:
yhat_[t] = beta0 + beta1 * y_[t – 1] + beta2 * yhat_[t – 1]
2) For the out-of-sample predictions with the observed Y unavailable:
yhat_[t] = beta0 + beta1 * yhat_[t – 1] + beta2 * yhat_[t – 1]
library(tscount) mdl <- tsglm(cnt$y, model = list(past_obs = 1, past_mean = 1), distr = "poisson") summary(mdl) # tsglm(ts = cnt$y, model = list(past_obs = 1, past_mean = 1), # distr = "poisson") # # Coefficients: # Estimate Std. Error # (Intercept) 0.632 0.1774 # beta_1 0.350 0.0687 # alpha_1 0.184 0.1455 # Standard errors obtained by normal approximation. # # Link function: identity # Distribution family: poisson # Number of coefficients: 3 # Log-likelihood: -279.2738 # AIC: 564.5476 # BIC: 573.9195 ### in-sample prediction ### cnt$yhat <- mdl$fitted.values tail(cnt, 3) # y yhat # 166 1 0.8637023 # 167 3 1.1404714 # 168 6 1.8918651 ### manually check ### beta <- mdl$coefficients pv167 <- beta[1] + beta[2] * cnt$y[166] + beta[3] * cnt$yhat[166] # 1.140471 pv168 <- beta[1] + beta[2] * cnt$y[167] + beta[3] * cnt$yhat[167] # 1.891865 ### out-of-sample prediction ### oot <- predict(mdl, n.ahead = 3) # [1] 3.080667 2.276211 1.846767 ### manually check ### ov2 <- beta[1] + beta[2] * oot[[1]][1] + beta[3] * oot[[1]][1] # 2.276211 ov3 <- beta[1] + beta[2] * oot[[1]][2] + beta[3] * oot[[1]][2] # 1.846767
To leave a comment for the author, please follow the link and comment on their blog: Yet Another Blog in Statistical Computing » S+/R.
R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.