srlanalytics / bdfm

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

Filling in missing values #52

Closed srlanalytics closed 5 years ago

srlanalytics commented 5 years ago

I included an example at the end of the us_gdp.R example of filling in missing values. This is already the default behavior when series are differenced; level observations are left as the observed values in the output values and missing values are filled in using estimated differences. For series that were not differenced (i.e. stationary series), however, values contains only estimated values. If our dfm object is called est and our raw data is data then we can fill in missing values in data as:

data[!is.finite(data)] <- est$values[!is.finite(data)]

Note that if we have specified forecast periods (say forecast = 3) then est$values will have more rows than data (3 in this case). Thus to fill in missing values in data we would want to use something like

data[!is.finite(data)] <- est$values[1:NROW(data), ][!is.finite(data)]

To make things easier for filling in missing values I've set the default forecast = 0.

christophsax commented 5 years ago

Adressed by the adjusted() function:

library(tsbox)
library(bdfm)

dta0 <- ts_seas(cbind(mdeaths, fdeaths))  # seasonally adjust
dta <- dta0
dta[1:10, 2] <- NA
m <- dfm(dta)
ts_plot(predict(m)[, 'fdeaths'], dta0[, 'fdeaths'])

ts_plot(adjusted(m)[, 'fdeaths'], dta0[, 'fdeaths'])

Created on 2019-06-15 by the reprex package (v0.2.1)