Skip to content
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

lmerTest #126

Closed
skanskan opened this issue May 8, 2016 · 11 comments
Closed

lmerTest #126

skanskan opened this issue May 8, 2016 · 11 comments

Comments

@skanskan
Copy link

skanskan commented May 8, 2016

It would be great if broom also showed the output of lmerTest alongside the lme4 output.
This package simply adds a new column with the p-value.

@bbolker
Copy link
Contributor

bbolker commented May 19, 2016

I'm in favor of this in principle. It will only work easily if the original model was fitted with lmerTest, but that seems like a reasonable restriction.

@skanskan
Copy link
Author

Anyway, What would be the difference between coef(summary(mylmerTestmodel)) and tidy(mylmerTestmodel) ?

@bbolker
Copy link
Contributor

bbolker commented May 19, 2016

A couple of small but potentially important differences:

  • column names; term as a column rather than as row names (these differences don't matter for human readers, but do for programmatic interfaces)
  • coef(summary(mylmerTestmodel)) will be very close to tidy(mylmerTestmodel,effects="fixed"), but tidy() offers a wider range of options (report estimates of RE var/cov/sd/cor, conditional modes; report confidence intervals; etc.)

@skanskan
Copy link
Author

OK, thanks @bbolker

@bbolker
Copy link
Contributor

bbolker commented Aug 20, 2017 via email

@niroshar
Copy link

@bbolker I removed the comment since I was able to get it solved using "effects = ran_pars" few minutes after.
Thank you very much.

@bbolker
Copy link
Contributor

bbolker commented Aug 20, 2017 via email

@niroshar
Copy link

niroshar commented Aug 20, 2017 via email

@bbolker
Copy link
Contributor

bbolker commented Dec 29, 2017

I think this works now in broom.mixed. Can someone check? (e.g. see examples of ?tidy.merMod in broom.mixed) If not, probably best to open as a broom.mixed issue (and close this in any case)

@alexpghayes
Copy link
Collaborator

Just ran the examples in tidy.merMod:

library(broom)
library(broom.mixed)
#> 
#> Attaching package: 'broom.mixed'
#> The following object is masked from 'package:broom':
#> 
#>     tidyMCMC
library(lme4)
#> Warning: package 'lme4' was built under R version 3.4.4
#> Loading required package: Matrix
#> Warning: package 'Matrix' was built under R version 3.4.4


lmm1 <- lmer(Reaction ~ Days + (Days | Subject), sleepstudy)
tidy(lmm1)
#>     effect    group                 term     estimate std.error statistic
#> 1    fixed    fixed          (Intercept) 251.40510485  6.824556 36.838311
#> 2    fixed    fixed                 Days  10.46728596  1.545789  6.771485
#> 3 ran_pars  Subject       sd_(Intercept)  24.74044759        NA        NA
#> 4 ran_pars  Subject              sd_Days   5.92213327        NA        NA
#> 5 ran_pars  Subject cor_(Intercept).Days   0.06555133        NA        NA
#> 6 ran_pars Residual       sd_Observation  25.59181589        NA        NA
tidy(lmm1, effects = "fixed")
#>   effect        term  estimate std.error statistic
#> 1  fixed (Intercept) 251.40510  6.824556 36.838311
#> 2  fixed        Days  10.46729  1.545789  6.771485
tidy(lmm1, effects = "fixed", conf.int=TRUE)
#>   effect        term  estimate std.error statistic   conf.low conf.high
#> 1  fixed (Intercept) 251.40510  6.824556 36.838311 238.029221 264.78099
#> 2  fixed        Days  10.46729  1.545789  6.771485   7.437595  13.49698
tidy(lmm1, effects = "fixed", conf.int=TRUE, conf.method="profile")
#> Computing profile confidence intervals ...
#>   effect        term  estimate std.error statistic   conf.low conf.high
#> 1  fixed (Intercept) 251.40510  6.824556 36.838311 237.680695 265.12951
#> 2  fixed        Days  10.46729  1.545789  6.771485   7.358653  13.57592
tidy(lmm1, effects = "ran_modes", conf.int=TRUE)
#> Warning: package 'bindrcpp' was built under R version 3.4.4
#>       effect   group        term level    estimate std.error     conf.low
#> 1  ran_modes Subject (Intercept)   308   2.2585654 12.070837 -21.39983947
#> 2  ran_modes Subject (Intercept)   309 -40.3985769 12.070837 -64.05698171
#> 3  ran_modes Subject (Intercept)   310 -38.9602458 12.070837 -62.61865063
#> 4  ran_modes Subject (Intercept)   330  23.6904985 12.070837   0.03209363
#> 5  ran_modes Subject (Intercept)   331  22.2602027 12.070837  -1.39820213
#> 6  ran_modes Subject (Intercept)   332   9.0395259 12.070837 -14.61887895
#> 7  ran_modes Subject (Intercept)   333  16.8404311 12.070837  -6.81797371
#> 8  ran_modes Subject (Intercept)   334  -7.2325792 12.070837 -30.89098405
#> 9  ran_modes Subject (Intercept)   335  -0.3336958 12.070837 -23.99210066
#> 10 ran_modes Subject (Intercept)   337  34.8903508 12.070837  11.23194592
#> 11 ran_modes Subject (Intercept)   349 -25.2101104 12.070837 -48.86851523
#> 12 ran_modes Subject (Intercept)   350 -13.0699567 12.070837 -36.72836156
#> 13 ran_modes Subject (Intercept)   351   4.5778352 12.070837 -19.08056968
#> 14 ran_modes Subject (Intercept)   352  20.8635924 12.070837  -2.79481241
#> 15 ran_modes Subject (Intercept)   369   3.2754530 12.070837 -20.38295184
#> 16 ran_modes Subject (Intercept)   370 -25.6128694 12.070837 -49.27127428
#> 17 ran_modes Subject (Intercept)   371   0.8070397 12.070837 -22.85136511
#> 18 ran_modes Subject (Intercept)   372  12.3145393 12.070837 -11.34386550
#> 19 ran_modes Subject        Days   308   9.1989719  2.304837   4.68157445
#> 20 ran_modes Subject        Days   309  -8.6197032  2.304837 -13.13710067
#> 21 ran_modes Subject        Days   310  -5.4488799  2.304837  -9.96627739
#> 22 ran_modes Subject        Days   330  -4.8143313  2.304837  -9.33172878
#> 23 ran_modes Subject        Days   331  -3.0698946  2.304837  -7.58729207
#> 24 ran_modes Subject        Days   332  -0.2721707  2.304837  -4.78956813
#> 25 ran_modes Subject        Days   333  -0.2236244  2.304837  -4.74102191
#> 26 ran_modes Subject        Days   334   1.0745761  2.304837  -3.44282140
#> 27 ran_modes Subject        Days   335 -10.7521591  2.304837 -15.26955659
#> 28 ran_modes Subject        Days   337   8.6282840  2.304837   4.11088649
#> 29 ran_modes Subject        Days   349   1.1734142  2.304837  -3.34398323
#> 30 ran_modes Subject        Days   350   6.6142050  2.304837   2.09680752
#> 31 ran_modes Subject        Days   351  -3.0152572  2.304837  -7.53265466
#> 32 ran_modes Subject        Days   352   3.5360133  2.304837  -0.98138416
#> 33 ran_modes Subject        Days   369   0.8722166  2.304837  -3.64518087
#> 34 ran_modes Subject        Days   370   4.8224646  2.304837   0.30506714
#> 35 ran_modes Subject        Days   371  -0.9881551  2.304837  -5.50555257
#> 36 ran_modes Subject        Days   372   1.2840297  2.304837  -3.23336773
#>      conf.high
#> 1   25.9169702
#> 2  -16.7401720
#> 3  -15.3018409
#> 4   47.3489033
#> 5   45.9186076
#> 6   32.6979308
#> 7   40.4988360
#> 8   16.4258257
#> 9   23.3247090
#> 10  58.5487556
#> 11  -1.5517055
#> 12  10.5884481
#> 13  28.2362400
#> 14  44.5219973
#> 15  26.9338579
#> 16  -1.9544646
#> 17  24.4654446
#> 18  35.9729442
#> 19  13.7163694
#> 20  -4.1023057
#> 21  -0.9314824
#> 22  -0.2969338
#> 23   1.4475029
#> 24   4.2452268
#> 25   4.2937730
#> 26   5.5919736
#> 27  -6.2347616
#> 28  13.1456814
#> 29   5.6908117
#> 30  11.1316025
#> 31   1.5021403
#> 32   8.0534108
#> 33   5.3896141
#> 34   9.3398621
#> 35   3.5292424
#> 36   5.8014272
head(augment(lmm1, sleepstudy))
#>   Reaction Days Subject  .fitted     .resid   .fixed      .mu .offset
#> 1 249.5600    0     308 253.6637  -4.103670 251.4051 253.6637       0
#> 2 258.7047    1     308 273.3299 -14.625228 261.8724 273.3299       0
#> 3 250.8006    2     308 292.9962 -42.195586 272.3397 292.9962       0
#> 4 321.4398    3     308 312.6624   8.777356 282.8070 312.6624       0
#> 5 356.8519    4     308 332.3287  24.523198 293.2742 332.3287       0
#> 6 414.6901    5     308 351.9950  62.695140 303.7415 351.9950       0
#>   .sqrtXwt .sqrtrwt .weights     .wtres
#> 1        1        1        1  -4.103670
#> 2        1        1        1 -14.625228
#> 3        1        1        1 -42.195586
#> 4        1        1        1   8.777356
#> 5        1        1        1  24.523198
#> 6        1        1        1  62.695140
glance(lmm1)
#>      sigma    logLik      AIC      BIC deviance df.residual
#> 1 25.59182 -871.8141 1755.628 1774.786 1743.628         174

glmm1 <- glmer(cbind(incidence, size - incidence) ~ period + (1 | herd),
               data = cbpp, family = binomial)
tidy(glmm1)
#>     effect group           term   estimate std.error statistic
#> 1    fixed fixed    (Intercept) -1.3983429 0.2312140 -6.047828
#> 2    fixed fixed        period2 -0.9919250 0.3031506 -3.272053
#> 3    fixed fixed        period3 -1.1282162 0.3228301 -3.494767
#> 4    fixed fixed        period4 -1.5797454 0.4220492 -3.743037
#> 5 ran_pars  herd sd_(Intercept)  0.6420699        NA        NA
#>        p.value
#> 1 1.468113e-09
#> 2 1.067695e-03
#> 3 4.744754e-04
#> 4 1.818098e-04
#> 5           NA
tidy(glmm1, effects = "fixed")
#>   effect        term  estimate std.error statistic      p.value
#> 1  fixed (Intercept) -1.398343 0.2312140 -6.047828 1.468113e-09
#> 2  fixed     period2 -0.991925 0.3031506 -3.272053 1.067695e-03
#> 3  fixed     period3 -1.128216 0.3228301 -3.494767 4.744754e-04
#> 4  fixed     period4 -1.579745 0.4220492 -3.743037 1.818098e-04
head(augment(glmm1, cbpp))
#>   herd incidence size period    .fitted     .resid    .fixed        .mu
#> 1    1         2   14      1 -0.8087134 -1.4377078 -1.398343 0.30816472
#> 2    1         3   12      2 -1.8006384  0.9884720 -2.390268 0.14177337
#> 3    1         4    9      3 -1.9369296  2.3566883 -2.526559 0.12598556
#> 4    1         0    5      4 -2.3884588 -0.9370227 -2.978088 0.08405701
#> 5    2         3   22      1 -1.6974362 -0.2431732 -1.398343 0.15480041
#> 6    2         1   18      2 -2.6893612 -0.1428294 -2.390268 0.06360406
#>   .offset  .sqrtXwt  .sqrtrwt .weights     .wtres       .eta
#> 1       0 1.7276542  8.103473       14 -1.3395656 -0.8087134
#> 2       0 1.2083394  9.930984       12  1.0747970 -1.8006384
#> 3       0 0.9954992  9.040690        9  2.8790881 -1.9369296
#> 4       0 0.6204492  8.058678        5 -0.6773884 -2.3884588
#> 5       0 1.6965905 12.967183       22 -0.2390730 -1.6974362
#> 6       0 1.0354006 17.384575       18 -0.1399198 -2.6893612
glance(glmm1)
#>   sigma    logLik      AIC      BIC deviance df.residual
#> 1     1 -92.02657 194.0531 204.1799 73.47428          51

The next part gives me some warnings that I though were errors at first but otherwise appears fine:

startvec <- c(Asym = 200, xmid = 725, scal = 350)
nm1 <- nlmer(circumference ~ SSlogis(age, Asym, xmid, scal) ~ Asym|Tree,
             Orange, start = startvec)
tidy(nm1)
#> Warning in vcov.merMod(object, use.hessian = use.hessian): variance-covariance matrix computed from finite-difference Hessian is
#> not positive definite or contains NA values: falling back to var-cov estimated from RX
#> Warning in vcov.merMod(object, correlation = correlation, sigm = sig): variance-covariance matrix computed from finite-difference Hessian is
#> not positive definite or contains NA values: falling back to var-cov estimated from RX
#>     effect    group           term   estimate std.error statistic
#> 1    fixed    fixed           Asym 192.052792  15.58385  12.32383
#> 2    fixed    fixed           xmid 727.904473  34.43847  21.13637
#> 3    fixed    fixed           scal 348.072134  26.30812  13.23060
#> 4 ran_pars     Tree        sd_Asym  31.646324        NA        NA
#> 5 ran_pars Residual sd_Observation   7.843009        NA        NA
tidy(nm1, effects = "fixed")
#> Warning in vcov.merMod(object, use.hessian = use.hessian): variance-covariance matrix computed from finite-difference Hessian is
#> not positive definite or contains NA values: falling back to var-cov estimated from RX

#> Warning in vcov.merMod(object, use.hessian = use.hessian): variance-covariance matrix computed from finite-difference Hessian is
#> not positive definite or contains NA values: falling back to var-cov estimated from RX
#>   effect term estimate std.error statistic
#> 1  fixed Asym 192.0528  15.58385  12.32383
#> 2  fixed xmid 727.9045  34.43847  21.13637
#> 3  fixed scal 348.0721  26.30812  13.23060
head(augment(nm1, Orange))
#> Warning in indices[which(stats::complete.cases(original))] =
#> seq_len(nrow(x)): number of items to replace is not a multiple of
#> replacement length
#>   Tree  age circumference   .fitted     .resid       .mu .offset  .sqrtXwt
#> 1    1  118            30  24.01051   5.989486  24.01051       0 0.1477654
#> 2    1  484            58  53.89014   4.109864  53.89014       0 0.3316505
#> 3    1  664            87  73.80812  13.191884  73.80812       0 0.4542296
#> 4    1 1004           115 111.87829   3.121713 111.87829       0 0.6885209
#> 5    1 1231           120 131.50149 -11.501489 131.50149       0 0.8092860
#> 6    1 1372           142 140.42155   1.578450 140.42155       0 0.8641818
#>   .sqrtrwt .weights     .wtres     .gam
#> 1        1        1   5.989486 162.4908
#> 2        1        1   4.109864 162.4908
#> 3        1        1  13.191884 162.4908
#> 4        1        1   3.121713 162.4908
#> 5        1        1 -11.501489 162.4908
#> 6        1        1   1.578450 162.4908
glance(nm1)
#>      sigma    logLik      AIC      BIC deviance df.residual
#> 1 7.843009 -131.5719 273.1438 280.9205 263.1438          30

Closing as all seems to be in order.

@github-actions
Copy link

This issue has been automatically locked. If you believe you have found a related problem, please file a new issue (with a reprex: https://reprex.tidyverse.org) and link to this issue.

@github-actions github-actions bot locked and limited conversation to collaborators Mar 14, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants