I use the below data (not original) & code to do structural bayesian prediction
df1=structure(list(Year = c(1985, 1986, 1987, 1988, 1989, 1990, 1991,
1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002,
2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013,
2014, 2015, 2016, 2017, 2018, 2019), `MPs(ton)` = c(1, 2, 3,
4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20,
21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35),
`MPs(items/kg)` = c(10, 9, 11, 11, 11.5, 12, 12.5, 13, 13.5,
14, 14.5, 15, 15.5, 16, 16.5, 17, 17.5, 18, 18.5, 19, 19.5,
20, 20.5, 21, 21.5, 22, 22.5, 23, 23.5, 24, 24.5, 25, 25.5,
26, 26.5), GDP = c(22, 24, 26, 28, 30, 32, 34, 36, 38, 40,
42, 44, 46, 48, 50, 52, 54, 56, 58, 60, 62, 64, 66, 68, 70,
72, 74, 76, 78, 80, 82, 84, 86, 88, 90), MC = c(33, 35, 37,
39, 41, 43, 45, 47, 49, 51, 53, 55, 57, 59, 61, 63, 65, 67,
69, 71, 73, 75, 77, 79, 81, 83, 85, 87, 89, 91, 93, 95, 97,
99, 101), Population = c(3, 8, 13, 18, 23, 28, 33, 38, 43,
48, 53, 58, 63, 68, 73, 78, 83, 88, 93, 98, 103, 108, 113,
118, 123, 128, 133, 138, 143, 148, 153, 158, 163, 168, 173
)), class = c("spec_tbl_df", "tbl_df", "tbl", "data.frame"
), row.names = c(NA, -35L), spec = structure(list(cols = list(
Year = structure(list(), class = c("collector_double", "collector"
)), `MPs(ton)` = structure(list(), class = c("collector_double",
"collector")), `MPs(items/kg)` = structure(list(), class = c("collector_double",
"collector")), GDP = structure(list(), class = c("collector_double",
"collector")), MC = structure(list(), class = c("collector_double",
"collector")), Population = structure(list(), class = c("collector_double",
"collector"))), default = structure(list(), class = c("collector_guess",
"collector")), skip = 1L), class = "col_spec"))
Code I am using for the analysis are
MPs <- df1[,-1]
names(MPs) <- c("MPs (ton)", "MPs (items/kg)",
"GDP", "Mariculture", "Population")
MPs_train <- MPs[1:25,]
MPs_test <- MPs[26:35,-1]
nseasons = 11
ss <- list()
ss <- AddLocalLinearTrend(ss, y = MPs_train$`MPs (ton)`)
ss <- AddSeasonal(ss, MPs_train$`MPs (ton)`, nseasons = nseasons)
rlls_model <- bsts(`MPs (ton)`~., state.specification = ss,
data = MPs_train, niter = 1000, ping = 0,
expected.model.size = 1)
plot(rlls_model, 'components',
xlab = 'Year', ylab = 'MPs(ton)')
plot(rlls_model, 'coefficients')
rlls_model_pred <- predict(rlls_model, newdata = MPs_test,
horizon = 50)
plot(rlls_model_pred, plot.original = 90,
main = 'Seasonal local linear trend forcast with regression',
xlab = 'Year',
ylab = 'MPs (ton)')
But when I tried to save the data by the below code it shows error
write.csv(rlls_model_pred, file = "Model.csv")
The error is:
Error in as.data.frame.default(x[[i]], optional = TRUE, stringsAsFactors = stringsAsFactors) :
cannot coerce class ‘"bsts.prediction"’ to a data.frame