srlanalytics / bdfm

Bayesian dynamic factor model estimation and predictive statistics including nowcasting and forecasting
MIT License
5 stars 6 forks source link

Some examples and some errors #13

Closed christophsax closed 6 years ago

christophsax commented 6 years ago

Some minimal examples that I ran before. I get errors when:

library(BDFM)
packageVersion("BDFM")
#> [1] '0.0.1'
library(tsbox)

fdeaths0 <- fdeaths
fdeaths0[length(fdeaths0)] <- NA
dta <- ts_c(fdeaths0, mdeaths)

# forecasting single series
dfm(fdeaths, forecast = 2)
#> Error in DSmooth(B, q, H, R, Y[, -(1:m)]): Not a matrix.

# multiple series (this works)
ts_plot(ts_c(dta, predict(dfm(dta))))


# but this doesn't
dfm(dta, forecast = 2)
#> Error in attr(x, "tsp") <- value: invalid time series parameters specified

fdeaths0[c(1:10, length(fdeaths0))] <- NA

dta.mixed <- ts_c(ts_pc(austres), mdeaths)

# why do these values don't ad up
fct <- predict(dfm(dta.mixed))
ts_plot(ts_c(dta.mixed, fct))

Created on 2018-10-22 by the reprex package (v0.2.1)

srlanalytics commented 6 years ago

OK, all these little issues should be fixed.

For the first, forecasting a single series: the C++ code requires that variable types are declared. Y should be a matrix, but for a single series the default in R is to convert Y to a vector. We now keep Y as a matrix using Y[,-(1:m), drop = FALSE]

For the second, the ts stuff should now be OK (class(Y)[1] was mts which was not in tsobjs. I changed this to any(class(Y)%in%tsobjs) and added mts to tsobjs.)

For the third, the default is dfm(Y, intercept = TRUE), but the bit of code that adds intercept terms back in to predicted values got lost in the formatting somewhere. Anyhow, it's back now.

ps --- I'd not used ts_plot() before... very slick. Much nicer that ts.plot()!