Skip to content

Instantly share code, notes, and snippets.

@plogacev
Created December 11, 2018 10:53
Show Gist options
  • Save plogacev/641a95c0a2d90d2edd2ae9a50e84235d to your computer and use it in GitHub Desktop.
Save plogacev/641a95c0a2d90d2edd2ae9a50e84235d to your computer and use it in GitHub Desktop.
# based on the great vignette on specifying custom response distributions in brms:
# https://cran.r-project.org/web/packages/brms/vignettes/brms_customfamilies.html
library(languageR)
library(brms)
danish$RT <- exp(danish$LogRT)
danish$cSex <- ifelse(danish$Sex == "M", -1, 1) * 0.5
lognormal2 <- custom_family(
"lognormal2", dpars = c("mu", "sigma"),
links = c("identity", "log"), lb = c(0, 0),
type = "real"
)
stan_funs <- "
real lognormal2_lpdf(real y, real mu, real sigma) {
real real_mu = log(mu) - sigma^2/2;
return lognormal_lpdf(y | real_mu, sigma);
}
real lognormal2_rng(real mu, real sigma) {
real real_mu = log(mu) - sigma^2/2;
return lognormal_rng(real_mu, sigma);
}
"
stanvars <- stanvar(scode = stan_funs, block = "functions")
m <- brm(RT ~ cSex, data = danish, family = lognormal2, stanvars = stanvars, chains = 1)
summary(m)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment