Skip to content

Instantly share code, notes, and snippets.

@Stan125
Last active November 27, 2018 16:26
Show Gist options
  • Save Stan125/133e343ccfff5844456b6a0af4dcd8f3 to your computer and use it in GitHub Desktop.
Save Stan125/133e343ccfff5844456b6a0af4dcd8f3 to your computer and use it in GitHub Desktop.

R Notebook

knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>",
  fig.path = "figures/"
)
library(dplyr)
library(bamlss)

This script serves to understand if predictions are sample-based or not

art_data <- GAMart()
model <- bamlss(list(num ~ x1 + x2 + x3,
                     sigma ~ x1 + x2 + x3),
                data = art_data)
coefficients <- as.data.frame(model$parameters)
colnames(coefficients) <- c("mu", "sigma")
pred <- art_data %>% select(x1:x3) %>% sample_n(1)
pred1 <- c(1, as.numeric(pred))

This first part just transforms the covariates (Plug-In):

# manually transformed mu prediction
manpred <- sum(coefficients$mu * pred1)
manpred
#> [1] -0.2763655

This second part transforms MCMC samples:

mcmc <- as.data.frame(model$samples[[1]]) %>%
  select(contains("mu")) %>%
  filter(mu.p.accepted == 1) %>%
  select(1:4)
tf_preds <- as.matrix(mcmc) %*% pred1
mean(tf_preds)
#> [1] -0.2790666

They are almost the same, but not completely!

Now check with the built-in predict.bamlss() function

predict(model, as.data.frame(pred))$mu
#> [1] -0.2790666

We can now conclude that the predictions of bamlss models are sample-based. Yay!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment