Chris J
Chris J

Reputation: 19

How does R make predictions using Bayesian regression models?

I built a Bayesian regression model in R and I'm trying to use it to make predictions from a record X that's not in the training set. The problem is, no matter how I change the values of one of the independent variables in X, the prediction remains the same! Can somebody shed some light on how predictions are made using Bayesian regression models? Here's some code to give you some idea of what I'm doing:

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"))

Upvotes: 0

Views: 589

Answers (1)

Chris J
Chris J

Reputation: 19

OK, I figured out how to solve my problem. Here's the underlying problem: I was trying to predict the value of freq_iso_chng2 for the year 2020, but originally, the 2020 value of freq_iso_chng2 was NA. This affects the prediction. If you replace the "NA" value of freq_iso_chng2 with any specific number, then the predictions DO respond to changes in predictor variables. Don't know why this works, but it does. For some reason, R's predictions using a Bayes model differ depending on whether the target started out missing, or started out as a number.

Upvotes: 0

Related Questions