I try to fit the same model (sort of logistic) using or a random or a fixed effect. It seems to be ok for random effect but is weird with fixed effect. Probably I do something wrong but I don’t understand where… if someone could help me (This is a toy example, true model is more complicated). I add also toy data (in dta).
dta <- data.frame(Te = c(27.85, 28.45, 28.65, 28.95, 29.65, 30.25,
30.85, 28.05, 28.4, 29.55, 28, 28.5, 29, 29.5, 30, 28.7, 29,
29.2, 30, 31, 32, 33, 27.2, 27.7, 28.2, 28.7, 29, 29.2, 29.5,
30, 31, 32, 33, 27.2, 27.7, 28.2, 28.7, 29, 29.2, 29.5, 29.7,
30, 31, 32, 33),
M = c(18, 11, 9, 8, 8, 2, 0, 19, 17, 11, 26,
27, 24, 13, 11, 2, 2, 1, 0, 0, 0, 0, 9, 9, 10, 6, 10, 7, 5, 4,
1, 0, 0, 7, 7, 6, 6, 6, 5, 2, 3, 3, 0, 0, 0),
S = c(18, 12, 11,
13, 15, 4, 5, 28, 28, 31, 30, 34, 39, 27, 31, 5, 5, 5, 6, 6,
7, 5, 9, 9, 10, 10, 10, 10, 8, 10, 10, 10, 10, 7, 7, 9, 10, 10,
10, 10, 10, 7, 10, 10, 6),
R = c("B", "B", "B", "B", "B", "B",
"B", "B", "B", "B", "A", "A", "A", "A", "A", "A", "A", "A", "A",
"A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A",
"A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A"
))
The random effect model… works fine (R is the random effect):
prior_ini <- set_prior(prior = "uniform(20, 35)", class = "b", coef="",
nlpar = "P", lb = 20, ub = 35, check = TRUE) +
set_prior(prior = "uniform(0, 12)", class = "b", coef="",
nlpar = "Sp", lb = 0, ub = 12, check = TRUE) +
set_prior(prior = "student_t(3, 0, 5.9)", class = "sd",
lb = 0, nlpar = "P")
lRE <- brm(bf(M | trials(S) ~ 1/(1+exp(-5.9*(P-Te) / Sp)),
P ~ (1 | R),
Sp ~ 1,
nl = TRUE),
family = binomial(link="identity"),
data = dta,
prior = prior_ini,
warmup = 1000,
thin = 10,
iter = 20000,
chains = 1,
init=rep(list(list(b_P_Intercept=29.5,
b_Sp_Intercept=1.4,
sd_R__P_Intercept=0.5)),
1))
plot(lRE)
conditions <- make_conditions(lRE, vars="R")
plot(conditional_effects(lRE, categorical = FALSE, effects='Te',
condition=conditions, robust = TRUE,
re_formula=NULL, spaghetti = FALSE, ndraws = 100))
Now the same with R as fixed effect:
prior_ini <- set_prior(prior = "uniform(20, 32)", class = "b", coef="",
nlpar = "P", lb = 20, ub = 32, check = TRUE) +
set_prior(prior = "uniform(0, 100)", class = "b", coef="",
nlpar = "Sp", lb = 0, ub = 100, check = TRUE) +
set_prior(prior = "uniform(-32, 32)", class = "b", coef="RB",
nlpar = "P", check = TRUE)
lFE <- brm(bf(M | trials(S) ~ 1/(1+exp(-5.9*(P-Te) / Sp)),
P ~ R,
Sp ~ 1,
nl = TRUE),
family = binomial(link="identity"),
data = dta,
prior = prior_ini,
warmup = 1000,
thin = 10,
iter = 20000,
chains = 1,
init=rep(list(list(b_P_Intercept=29.5,
b_P_RB=0.5,
b_Sp_Intercept=1.4)),
1))
plot(lFE)
conditions <- make_conditions(lFE, vars="R")
plot(conditional_effects(lFE, categorical = FALSE, effects='Te',
condition=conditions, robust = TRUE,
re_formula=NULL, spaghetti = FALSE, ndraws = 100))
Result is clearly wrong.
Thanks
Marc