0

Using the dlm package in R I fit a dynamic linear model to a time series data set, consisting of 20 observations. I then use the dlmForecast function to predict future values (which I can validate against the genuine data for said period).

I use the following code to create a prediction interval;

ciTheory <- (outer(sapply(fut1$Q, FUN=function(x) sqrt(diag(x))), qnorm(c(0.05,0.95))) +
               as.vector(t(fut1$f)))

However my data does not follow a normal distribution and I wondered whether it would be possible to adapt the qnorm function for other distributions. I have tried qt, but am unable to apply qgamma.......

Just wondered if anyone knew how you would go about sorting this.....

Below is a reproduced version of my code...

library(dlm)

data <- c(20.68502, 17.28549, 12.18363, 13.53479, 15.38779, 16.14770, 20.17536, 43.39321, 42.91027, 49.41402, 59.22262, 55.42043)

    mod.build <- function(par) {
  dlmModPoly(1, dV = exp(par[1]), dW = exp(par[2]))
}

# Returns most likely estimate of relevant values for parameters
mle <- dlmMLE(a2, rep(0,2), mod.build); #nileMLE$conv
if(mle$convergence==0) print("converged") else print("did not converge")

mod1 <- dlmModPoly(dV = v, dW = c(0, w))
mod1Filt <- dlmFilter(a1, mod1)
fut1 <- dlmForecast(mod1Filt, n = 7)

Cheers

j.rahilly
  • 33
  • 5
  • 1
    Why can't you apply `qgamma`? Do any errors occure? Please give a small [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) including some sample data. It's easier to help you out. – Martin Gal Jul 14 '20 at 13:26
  • Updated code above @Martin Gal..... – j.rahilly Jul 14 '20 at 15:33
  • Object `a2` is undefined. Let me ask you this way: Does the `qgamma` code fail because of an error message like "argument shape is missing"? – Martin Gal Jul 14 '20 at 16:09
  • Cheers for the response @Martin Gal.......''a2'' should read ''data'' (apologies). I've not actually been able to try using qgamma as I can't figure out appropriate parameters to be honest.... – j.rahilly Jul 14 '20 at 17:09

0 Answers0