Last active
July 4, 2019 08:04
-
-
Save fusaroli/16c0f33d14fb518fd1e1c6bd7e3fd84c to your computer and use it in GitHub Desktop.
Reproducible example for Loo error
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
## Load libraries | |
pacman::p_load(tidyverse,brms) | |
## Load data | |
data <- read_csv("data.csv") | |
## Define formulas | |
Model1_f <- bf(Outcome ~ Diagnosis + (1|ID)) | |
Model2_f <- bf(Outcome ~ Diagnosis + (1|p|ID), | |
sigma ~ Diagnosis + (1|p|ID)) | |
# Define priors | |
SkepticPrior1 <- c( | |
prior(normal(0,.05), class=Intercept), | |
prior(normal(0,.05), class=b), | |
prior(normal(0,.01), class=sd) | |
) | |
SkepticPrior2 <- c( | |
prior(normal(0,.05), class=Intercept), | |
prior(normal(0,.05), class=b), | |
prior(normal(0,.01), class=sd), | |
prior(lkj(5), class=cor), | |
prior(normal(0,.05), class=Intercept, dpar = sigma), | |
prior(normal(0,.05), class=b, dpar = sigma), | |
prior(normal(0,.01), class=sd, dpar = sigma) | |
) | |
# Run models | |
m1 <- brm(Model1_f, | |
data = data, | |
family = gaussian, | |
iter = 2000, | |
chains = 2, | |
cores = 2, | |
prior = SkepticPrior1, | |
sample_prior = T, | |
file = "Model1", | |
control= list(adapt_delta = 0.99, | |
max_treedepth=20)) | |
m1 <- add_criterion(m1, criterion=c("loo"), reloo=F, file = "Model1") | |
m2 <- brm(Model2_f, | |
data = data, | |
family = gaussian, | |
iter = 2000, | |
chains = 2, | |
cores = 2, | |
prior = SkepticPrior1, | |
sample_prior = T, | |
file = "Model2", | |
control= list(adapt_delta = 0.99, | |
max_treedepth=20)) | |
m2 <- add_criterion(m1, criterion=c("loo"), reloo=F, file = "Model2") | |
# Compare, when things stop working | |
w <- loo_model_weights(m1,m2) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment