I’m a relatively new greta user, and I’m trying to calculate deviance so that I can calculate DIC for a logistic regression model (see this related post).
I get the following error when trying to run my code (appended at the end of this post):
Error in (function (..., deparse.level = 1) :
number of rows of matrices must match (see arg 2)
Any help would be much appreciated. I’m sure it’s something simple…
Thanks,
Vincent
Here is my code:
### Simulate
n <- 1000
beta_gen <- c(-1, 0.25)
covariates <- cbind(1, rnorm(n, 0, 1))
mu_gen <- covariates %*% beta_gen
y_r <- rbinom(n, 1, prob = boot::inv.logit(mu_gen))
y <- as_data(y_r)
# priors
beta <- normal(0, 1.5, dim = ncol(covariates))
# likelihood
prob <- covariates %*% beta
distribution(y) <- bernoulli(prob = ilogit(prob))
# Deviance
deviance <- -2 * sum(dbinom(y, size = 1, prob = boot::inv.logit(prob), log = TRUE))
# model
m <- model(beta, deviance)
n_chains <- 3
# inits
mean_int <- boot::logit(sum(y_r)/length(y_r))
beta_inits <- replicate(
n_chains,
initials(
beta = c(rnorm(1, mean_int, 0.05), rnorm(ncol(covariates) - 1, -0.1, 0.001))
),
simplify = FALSE
)
# Fit model
draws <- mcmc(m, chains = n_chains, initial_values = beta_inits, n_samples = 100, warmup = 100, pb_update = 10)