New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
AR1 in glmmTMB vs nlme #986
Comments
For the convergence problem, this is just
As for the df increasing by 2: you need to set
This gives the same AR1 correlation estimate ( |
Okay, so But you're also saying there's an issue here that |
In this example you're trying to fit a random slopes model (the |
Thanks @bbolker, that was very silly of me 🤦♂️. Here is a more complete example with the library(glmmTMB)
#> Warning: package 'glmmTMB' was built under R version 4.3.2
library(nlme)
# Data ------------------------------------
data(sleepstudy, package = "lme4")
mb1 <- lme(Reaction ~ Days,
random = ~ Days | Subject,
correlation = corAR1(form = ~ Days | Subject),
data = sleepstudy,
method = "REML",
control = lmeControl(opt = "optim"))
mb1[["modelStruct"]][["corStruct"]]
#> Correlation structure of class corAR1 representing
#> Phi
#> 0.4870368
VarCorr(mb1)
#> Subject = pdLogChol(Days)
#> Variance StdDev Corr
#> (Intercept) 221.39047 14.879196 (Intr)
#> Days 22.65628 4.759861 0.897
#> Residual 930.29232 30.500694
mb2 <- glmmTMB(Reaction ~ Days + (1 + Days | Subject) +
ar1(factor(Days) + 0 | Subject),
dispformula = ~0,
REML = TRUE,
data = sleepstudy,
control = glmmTMBControl(optimizer = optim))
#> Warning in finalizeTMB(TMBStruc, obj, fit, h, data.tmb.old): Model convergence
#> problem; non-positive-definite Hessian matrix. See vignette('troubleshooting')
VarCorr(mb2)
#>
#> Conditional model:
#> Groups Name Std.Dev. Corr
#> Subject (Intercept) 2.072765
#> Days 0.090318 -0.927
#> Subject.1 factor(Days)0 49.355403 0.799 (ar1) 0.799 (ar1) Created on 2024-02-03 with reprex v2.0.2 The fixed effects are virtually the same, but the SE for the intercept is substatially different: parameters::compare_parameters(mb1, mb2, select = "{estimate}, ({se})")
#> Parameter | mb1 | mb2
#> -----------------------------------------------
#> (Intercept) | 252.24, (6.85) | 253.73, (11.22)
#> Days | 10.47, (1.53) | 10.47, ( 1.69)
#> -----------------------------------------------
#> Observations | 180 | 180 (I am trying to "convert" some users from SPSS to glmmTMB, but can only find SPSS -> nlme guides...) |
I don't know exactly. I did a little bit of digging, with the following conclusions:
More thoughts
|
This is very interesting... Thanks for taking the time to look into / think about this. I will run the analysis in SPSS and report back. |
While I work on the SPSS results, just as a point of reference, it seems like library(brms)
# Data ------------------------------------
data(sleepstudy, package = "lme4")
mb3 <- brm(Reaction ~ Days + (Days | Subject) + ar(time = Days, gr = Subject),
data = sleepstudy,
backend = "cmdstanr")
variables(mb3)
pars <- as.matrix(mb3, variable = c("b_Intercept", "b_Days",
"sd_Subject__Intercept", "sd_Subject__Days", "cor_Subject__Intercept__Days",
"sigma", "ar[1]")) |>
posterior::rvar(nchains = 4)
bayestestR::describe_posterior(pars, centrality = "map", ci_method = "hdi", test = NULL)
#> Summary of Posterior Distribution
#>
#> Parameter | MAP | 95% CI
#> -----------------------------------------------------------
#> x[b_Intercept] | 254.22 | [239.45, 268.61]
#> x[b_Days] | 10.24 | [ 7.13, 13.98]
#> x[sd_Subject__Intercept] | 18.96 | [ 1.04, 34.70]
#> x[sd_Subject__Days] | 5.07 | [ 1.51, 9.52]
#> x[cor_Subject__Intercept__Days] | 0.82 | [ -0.32, 1.00]
#> x[sigma] | 27.16 | [ 24.00, 31.09]
#> x[ar[1]] | 0.52 | [ 0.29, 0.81] |
Any further comments on this? |
I'm having a hard time tracking down someone who is proficient enough in SPSS :/ |
Alright, we go it! (SPSS syntax and output: We can now compare:
|
OK, |
SPSS is using REML. Yes, this is a very "tight" model, but it is used in the growth models literature (might work better with more time points?). Happy to help in any way that I can. |
SPSS has a lot of quirks that aren't immediately clearly visible. E.g., SPSS uses PQL approximation in GLMMs by default, therefore, coefficients differ from what you get from R packages. There are too many hidden default settings in SPSS that make it almost useless to try to get exact results between R and SPSS (at least for everything beyond simple linear models). |
I'm trying to fix a growth model such that:
in
nlme::lme()
I would do it like this:I expect to do this in
glmmTMB
like the following, but I run into the following issues:nlme
.I also tried without level 1 variance (
dispformula
), but still get different results.What am I doing wrong?
The text was updated successfully, but these errors were encountered: