Skip to content

Instantly share code, notes, and snippets.

@derekpowell
Last active May 21, 2019 22:08
Show Gist options
  • Save derekpowell/4880591f088623ee17d9637eed3aff1f to your computer and use it in GitHub Desktop.
Save derekpowell/4880591f088623ee17d9637eed3aff1f to your computer and use it in GitHub Desktop.
Create priors on intercepts for cumulative brms ordinal regression model
cumulative_intercept_prior <- function(k, sd = 2, alpha = 1, beta = 1,
shape = c("flat", "middle", "rightskewed", "leftskewed")) {
## Creates priors on intercepts for cumulative() family regression.
## Assumes that probability of response options follow cumulative beta
## distribution specified by a and b or by "shape" argument.
##
## k = number of categories
## sd = std dev of normal over intercept
## a, b = alpha and beta specifying shape of distribution (defaults to uniform)
## shape = string specifying pre-defined distribution shape
shapes <- list("rightskewed" = c(2, 4),
"leftskewed" = c(4, 2),
"middle" = c(3, 3),
"flat" = c(1, 1))
if (length(shape) == 1) {
alpha <- shapes[[shape]][1]
beta <- shapes[[shape]][2]
}
intercepts <- seq(1, k - 1)
prior_list <- lapply(intercepts, function(x) {
center <- qlogis(pbeta(x / k, alpha, beta))
p <- paste0("normal(", center, ",", sd, ")")
return(set_prior(p, class = "Intercept", coef = as.character(x)))
})
return(Reduce(c,prior_list))
}
@derekpowell
Copy link
Author

Generates priors (in the form of brmsprior object) on intercept terms for ordinal regressions using cumulative family and default logit link. For use with brms package.

To source in a script, run:

devtools::source_gist(id = "4880591f088623ee17d9637eed3aff1f", filename="cumulative-intercept-prior.R")

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