我在R中建立了贝叶斯回归模型,并试图用它从不在训练集中的记录X进行预测。问题是,无论我如何更改X中自变量之一的值,预测都将保持不变!有人可以阐明使用贝叶斯回归模型如何进行预测吗?这是一些代码,可让您对我正在做的事情有所了解:
install.packages("BMA")
library(BMA)
D_for_Bayes_tweaked <- D_for_Bayes
bfit1 <- bic.glm(f = as.formula('freq_iso_chng2 ~ cpi_gasoline_proj + vhcl_age_proj + vhcl_sale_ltruck_proj',), data = D_for_Bayes[D_for_Bayes$year >= 2003 & D_for_Bayes$year <= 2019,], glm.family = Gamma(link = "log"))
(p1 <- predict(bfit1, newdata = D_for_Bayes[D_for_Bayes$year >= 2003 & D_for_Bayes$year <= 2020,], type = "response"))
cpi_gasoline_proj_change <- 0.025
new_gas_cpi <- D_for_Bayes[D_for_Bayes$year == 2020,]$cpi_gasoline_proj + cpi_gasoline_proj_change
D_for_Bayes_tweaked[D_for_Bayes_tweaked$year == 2020,]$cpi_gasoline_proj <- new_gas_cpi
bfit2 <- bic.glm(f = as.formula('freq_iso_chng2 ~ cpi_gasoline_proj + vhcl_age_proj + vhcl_sale_ltruck_proj',), data = D_for_Bayes_tweaked[D_for_Bayes_tweaked$year >= 2003 & D_for_Bayes_tweaked$year <= 2019,], glm.family = Gamma(link = "log"))
(p2 <- predict(bfit2, newdata = D_for_Bayes_tweaked[D_for_Bayes_tweaked$year >= 2003 & D_for_Bayes_tweaked$year <= 2020,], type = "response"))
答案 0 :(得分:0)
好的,我想出了解决问题的方法。这是潜在的问题:我试图预测2020年的freq_iso_chng2值,但最初,freq_iso_chng2的2020年值为NA。这影响了预测。如果将freq_iso_chng2的“ NA”值替换为任何特定数字,则预测DO会响应预测变量的变化。不知道为什么这样做,但确实如此。由于某些原因,R使用贝叶斯模型的预测取决于目标是开始丢失还是以数字开始而不同。