-
Notifications
You must be signed in to change notification settings - Fork 0
/
neg_binomial_2_trials.R
57 lines (51 loc) · 1.57 KB
/
neg_binomial_2_trials.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
stan_funs <- "
real neg_binomial_2_trials_lpmf(int[] y, vector mu, real phi, int[] T) {
int N = size(mu);
vector[N] mu2;
vector[N] phi2;
for (i in 1:N) {
mu2[i] = mu[i] * T[i];
phi2[i] = phi * T[i];
}
return neg_binomial_2_lpmf(y | mu2, phi2);
}
int[] neg_binomial_2_trials_rng(vector mu, real phi, int[] T) {
int N = size(mu);
vector[N] mu2;
vector[N] phi2;
for (i in 1:N) {
mu2[i] = mu[i] * T[i];
phi2[i] = phi * T[i];
}
return neg_binomial_2_rng(mu2, phi2);
}
"
stanvars <- stanvar(scode = stan_funs, block = "functions")
log_lik_neg_binomial_2_trials <- function(i, prep){
mu <- prep$dpars$mu[, i]
phi <- prep$dpars$phi
trials <- prep$data$trials[i]
y <- prep$data$Y[i]
neg_binomial_2_trials_lpmf(y, mu, phi, trials)
}
posterior_predict_neg_binomial_2_trials <- function(i, prep, ...) {
mu <- pmax(prep$dpars$mu[, i], 1E-10)
phi <- pmax(prep$dpars$phi, 1E-10)
trials <- pmax(prep$data$trials[i], 1)
return(neg_binomial_2_trials_rng(mu, phi, trials))
}
posterior_epred_neg_binomial_2_trials <- function(prep) {
return(brms:::posterior_epred_negbinomial2(prep))
}
neg_binomial_2_trials <- custom_family(
name = "neg_binomial_2_trials",
dpars = c("mu", "phi"),
links = c("log", "log"),
type = "int",
lb = c(0, 0),
vars = "trials",
loop = FALSE,
log_lik = log_lik_neg_binomial_2_trials,
posterior_predict = posterior_predict_neg_binomial_2_trials,
posterior_epred = posterior_epred_neg_binomial_2_trials
)