I am using the R package brms as the interface to Stan. And I assume that I should get similar fit using Stan as that from using lme4 package with not so tight priors and similar model structures. I am asking the question here because I am not sure if the “nonlinear” way to construct the linear model in brms would affect the fitting dramatically.
The model basically assumes a linear relationship between y (log of drift deposition) and x (log of distance) for each trial. One trial can have multiple lines along the x(distance). The project is related to pesticide drift deposition for regulatory use.
The model structure can be written as as following:
y \sim b1 + b2 * x + \epsilon
Intercept: b1 \sim 1 + Speed + Pressure + Temp + Winds peed + Boom Height + \epsilon_{Trial} + \epsilon_{Line}
Slope: b2 \sim 1 + Speed + Pressure + Temp + Wind speed + \epsilon_{Trial}
Although not completely the same, the above structure can be fitted by linear mixed model directly, the R code (with a reduced model of no random slopes) would be:
lmeRes <- lmer(y~x + Speed + Pressure + Temp_1 + Windspeed + Boom+ x:Speed + x:Temp_1 + x:Windspeed+(1|TrialG/LineG),data=dat)
The code I used in R with bmrs is as below. It was specified more in a nonlinear model way with the coefficient in the linear model being fitted by a second level linear model.
priors1 <- c(set_prior("student_t(5,0,2)", nlpar = "b1"),
set_prior("student_t(5,0,2)", nlpar = "b2"),
set_prior("cauchy(0, 1)", class= "sd", nlpar = "b1", group = "TrialG"),
#set_prior("cauchy(0, 1)", class= "sd", nlpar = "b1", group = "LineG"),
set_prior("cauchy(0, 1)", class= "sd", nlpar = "b2", group = "TrialG"))
fit <- brm( bf(y ~ b1+b2*x,
b1 ~ 1+Speed+Pressure+Temp_1+Windspeed+Boom+Crop.Height+(1|TrialG/LineG),
b2 ~ 1+Speed+Pressure+Temp_1+Windspeed+Boom+Crop.Height+(1|TrialG),
nl = TRUE),
data = dat, prior = priors1,
iter =2000, chains = 3, #warmup = 1000,
control = list(adapt_delta = 0.88,max_treedepth=maxTree), cores = 4,
save_model = paste0(filename,".txt"))
An example part of the data would look like
y x Speed Pressure Temp_1 Windspeed Boom TrialG LineG TrialG:LineG Using
1 -3.329807 -0.6931472 8 300 23.84 3.144205 0.5 1 1 1_1 FALSE
2 -4.816369 0.0000000 8 300 23.84 3.144205 0.5 1 1 1_1 FALSE
3 -4.852973 0.6931472 8 300 23.84 3.144205 0.5 1 1 1_1 FALSE
4 -5.008920 1.0986123 8 300 23.84 3.144205 0.5 1 1 1_1 FALSE
5 -5.614826 1.6094379 8 300 23.84 3.144205 0.5 1 1 1_1 FALSE
6 -6.835159 2.3025851 8 300 23.84 3.144205 0.5 1 1 1_1 FALSE
I cannot provide the full data directly without permission here but I can ask for the permission if necessary.
Thank you for any hints and suggestions.