When estimating a treatment effect with a cluster design, you need to include varying slopes, even if the fit gives warning messages.

Nick Brown and I recently published this paper, How statistical challenges and misreadings of the literature combineto produce unreplicable science: An example from psychology, in the journal, Advances in Methods and Practices in Psychological Science, about a published article that claimed to find that healing of bruises could be sped or slowed by manipulating people’s subjective sense of time. The article under discussion had major problems–including weak theory, a flawed data analysis, and multiple errors in summarizing the literature–to the extent that we did not find their claims to be supported by their evidence.

That sort of thing happens–we call it everyday, bread-and-butter pseudoscience. The point of the new paper by Nick and me was not so much to shoot down one particular claim of mind-body healing but rather to explore some general reasons for scientific error and overconfidence and to provide a template for future researchers to avoid these problems in the future.

We posted this the other day on the blog (under the heading, 7 steps to junk science that can achieve worldly success), and something came up in comments, something I hadn’t thought of before and I wanted to share with you. The commenter, Jean-Paul, pointed to a misunderstanding that can arise when fitting multilevel models using some software.

Need for multilevel modeling or some equivalent adjustment for clustering in data

Here was the problem. The article in question estimated treatment effects with a cluster design: they had three treatments applied to 33 research participants (in psychology jargon, “subjects”) with data provided by 25 raters. The total number of measurements was 2425 (not quite 3*33*25 because of some missing data, which is not the point in this story).

When you want to estimate treatment effects from a clustered design, you need to fit a multilevel model or make some equivalent adjustment. Otherwise your standard errors will be too small. In this case you should include effects for participants (some have more prominent bruises than others in this study) and for raters (who will have systematic differences in how they characterize the severity of a bruise using a numerical rating).

The researchers knew to do this, so they fit a multilevel model. Fortunately for me, they used the R program lmer, which I’m familiar with. Here’s the output, which for clarity I show using the display function in the arm package:

lmer(formula = Healing ~ Condition + (1 | Subject) + (1 | ResponseId), 
    data = DFmodel, REML = FALSE)
            coef.est coef.se
(Intercept) 6.20     0.31   
Condition28 0.23     0.09   
Condition56 1.05     0.09   

Error terms:
 Groups     Name        Std.Dev.
 Subject    (Intercept) 1.07    
 ResponseId (Intercept) 1.22    
 Residual               1.87    
---
number of obs: 2425, groups: Subject, 33; ResponseId, 25
AIC = 10131.4, DIC = 10119.4
deviance = 10119.4 

So, fine. As expected, participant and rater effects are large–they vary by more than the magnitudes of the estimated treatment effects!–so it’s a good thing we adjusted for them. And the main effects are a stunning 2.4 and 11.1 standard errors away from zero, a big win!

Need for varying slopes, not just varying intercepts

But . . . wait a minute. The point of this analysis is not just to estimate average responses; it’s to estimate treatment effects. And, for that, we need to allow the treatment effects to vary by participant and rater–in multilevel modeling terms, we should be allowing slopes as well as intercepts to vary. This is in accordance with the well-known general principle of the design and analysis of experiments that the error term for any comparison should be at the level of analysis of the comparison.

No problem, lmer can do that, and we do so! here’s the result:

lmer(formula = Healing ~ Condition + (1 + Condition | Subject) + 
    (1 + Condition | ResponseId), data = DFmodel, REML = FALSE)
            coef.est coef.se
(Intercept) 6.18     0.39   
Condition28 0.25     0.36   
Condition56 1.09     0.37   

Error terms:
 Groups     Name        Std.Dev. Corr        
 Subject    (Intercept) 1.71                 
            Condition28 1.99     -0.71       
            Condition56 2.03     -0.72  0.65 
 ResponseId (Intercept) 1.24                 
            Condition28 0.07      1.00       
            Condition56 0.13     -1.00 -1.00 
 Residual               1.51                 
---
number of obs: 2425, groups: Subject, 33; ResponseId, 25
AIC = 9317.9, DIC = 9285.9
deviance = 9285.9 

The estimated average treatment effects have barely changed, but the standard errors are much bigger. The fitted models shows that treatment effects vary a lot by participant, not so much by rater.

You might be happy because the t-statistic for one of the treatment comparisons is still 3 standard errors away from zero, but other problems remain, both with multiple comparisons and with the interpretation of the data, as we discuss in section 2.4 of our paper.

There’s one thing you might notice if you look carefully at the above output, which is that the estimated covariance matrix for the rater effects is degenerate: the correlations are at 1 and -1. This sort of thing happens a lot with multilevel models when data are noisy and the number of groups is small. It’s an issue we discussed in this paper from 2014.

In practice, it’s no big deal here: the degeneracy is a consequence of a very noisy estimate, which itself arises because the noise in the data is much larger than the signal, which itself arises because the variation in treatment effects across raters is indistinguishable from zero (look at those small estimated standard deviations of the varying slopes for rater). But that’s all pretty subtle, not something that’s in any textbook, even ours!

That scary warning on the computer

Here’s the problem. When you fit that varying slopes model, R displays a warning in scary red type:

It’s possible that the researchers tried this varying-slope fit, saw the singularity, got scared, and retreated to the simpler varying-intercept model, which had the incidental benefit of giving them an estimated treatment effect that was 11 standard error from zero.

Just to be clear, I’m not saying this is a problem with R, or with lme4. Indeed, if you type help(“isSingular”) in the R console, you’ll get some reasonable advice, none of which is to throw out all the varying slopes. But the first option suggested there is to “avoid fitting overly complex models in the first place,” which could be misunderstood by users.

If you fit the model using stan_lmer on its default settings, everything works fine:

 family:       gaussian [identity]
 formula:      Healing ~ Condition + (1 + Condition | Subject) + (1 + Condition | 
	   ResponseId)
 observations: 2425
------
            Median MAD_SD
(Intercept) 6.22   0.40  
Condition28 0.26   0.34  
Condition56 1.09   0.37  

Auxiliary parameter(s):
      Median MAD_SD
sigma 1.51   0.02  

Error terms:
 Groups     Name        Std.Dev. Corr       
 Subject    (Intercept) 1.731               
            Condition28 2.015    -0.63      
            Condition56 2.056    -0.66  0.57
 ResponseId (Intercept) 1.225               
            Condition28 0.158     0.33      
            Condition56 0.172    -0.51  0.04
 Residual               1.509               
Num. levels: Subject 33, ResponseId 25 

These estimates average over the posterior distribution, so nothing on the boundary, no problem. And, no surprise, the estimates and standard errors of the treatment effects are basically unchanged.

That said, when you run stan_lmer, it gives warnings in red too!

These warning messages are a big problem! On one hand, yeah, you want to warn people; on the other hand, it would be just horrible if the warnings are so scary that they send users to use bad models that don’t happen to spit out warnings.

I also tried fitting the model using blmer, which I thought would work fine, but that produced some warning messages that scared even me:

By default, blme uses a degeneracy-avoiding prior, so this sort of thing shouldn’t happen at all. We should figure out what’s going on here!

Summary

1. If you are estimating treatment effects using clustered data, you should be fitting a multilevel model with varying intercepts and slopes (or do the equivalent adjustment in some other way, for example by boostrapping according to the cluster structure). Varying intercepts is not enough. If you do it wrong, your standard errors can be way off. This is all consistent with the general principle to use all design information in the analysis.

2. If you apply a more complicated method on the computer and it gives you a warning message, this does not mean that you should go back to a simpler method. More complicated models can be harder to fit. This is something you might have to deal with. If it really bothers you, then be more careful in your data collection; you can design studies that can be analyzed more simply. Or you can do some averaging of your data, which might lose some statistical efficiency but will allow you to use simpler statistical methods (for example, see section 2.3, “Simple paired-comparisons analysis,” of our paper).

21 thoughts on “When estimating a treatment effect with a cluster design, you need to include varying slopes, even if the fit gives warning messages.”

  1. Isn’t the actual lesson to learn that if your data are so noisy and your model so fragile that you need to worry about all this, you should do find something better to do with your life?

    (“Your” referring to the researchers, not Andrew.)

    Does anyone actually expect meaningful insight into the important topic of wound healing to come from a study like this?

    • I’m afraid I’m going to start getting nightmares that one day a paper of mine will show up on this blog because I made some mistake that no one else caught before getting published, I’ll be called a peddler of pseudoscience, and people will start telling me to try another career.

      • Will:

        I don’t know if this will reassure you, but . . . the problem with the paper in question is not just that it had a subtle statistical error regarding multilevel modeling that was not caught by reviewers. The paper also had several other problems with its theory, data analysis, and interpretation of results (as discussed in section 2.4 of our article) and it had a very misleading summary of the literature (as discussed in our section 3). I assume that if you are trying your best to do good science, that these things won’t happen to you by accident!

  2. I was first going to write “Look at that very large difference in DIC in favor of including varying slopes!”, but then noticed DIC values are equal to deviance values, which indicates DIC is failing here. AIC values are also strange, but maybe it is some modification and not using the total number of parameters in case of varying effect models?

    Can you rerun both models with stan_lmer and provide the loo() output? It looks like the varying slopes model has so much better performance that it really should not be just dropped with those warnings without even mentioning the possibility that it’s a much better model.

    • Aki:

      Indeed, when reporting the regression results in the published paper, I didn’t even include AIC or deviance as this would just add confusion.

      The varying-slopes model does fit much better. I ran this code:

      library("rstanarm")
      Model_stan <- stan_lmer(Healing ~ Condition + (1 | Subject) + (1 | ResponseId), data = DFmodel)
      Model_varying_slopes_stan <- stan_lmer(Healing ~ Condition + (1 + Condition | Subject) + (1 + Condition | ResponseId), data = DFmodel)
      print(Model_stan)
      print(Model_varying_slopes_stan)
      loo(Model_stan)
      loo(Model_varying_slopes_stan)
      loo_compare(loo(Model_stan), loo(Model_varying_slopes_stan))
      

      Both models give warning messages when I run them.

      Here's the output for the varying-intercept model:

      stan_lmer
       family:       gaussian [identity]
       formula:      Healing ~ Condition + (1 | Subject) + (1 | ResponseId)
       observations: 2425
      ------
                  Median MAD_SD
      (Intercept) 6.2    0.3   
      Condition28 0.2    0.1   
      Condition56 1.0    0.1   
      
      Auxiliary parameter(s):
            Median MAD_SD
      sigma 1.9    0.0   
      
      Error terms:
       Groups     Name        Std.Dev.
       Subject    (Intercept) 1.1     
       ResponseId (Intercept) 1.3     
       Residual               1.9     
      Num. levels: Subject 33, ResponseId 25 
      

      Here's the varying-intercept, varying-slope model:

      stan_lmer
       family:       gaussian [identity]
       formula:      Healing ~ Condition + (1 + Condition | Subject) + (1 + Condition | 
      	   ResponseId)
       observations: 2425
      ------
                  Median MAD_SD
      (Intercept) 6.2    0.4   
      Condition28 0.2    0.4   
      Condition56 1.1    0.4   
      
      Auxiliary parameter(s):
            Median MAD_SD
      sigma 1.5    0.0   
      
      Error terms:
       Groups     Name        Std.Dev. Corr       
       Subject    (Intercept) 1.77                
                  Condition28 2.05     -0.65      
                  Condition56 2.09     -0.67  0.59
       ResponseId (Intercept) 1.23                
                  Condition28 0.16      0.33      
                  Condition56 0.17     -0.50  0.05
       Residual               1.51                
      Num. levels: Subject 33, ResponseId 25 
      

      And here are the loo results:

      > loo(Model_stan)
      
      Computed from 4000 by 2425 log-likelihood matrix.
      
               Estimate   SE
      elpd_loo  -4993.1 40.9
      p_loo        58.5  2.0
      looic      9986.2 81.8
      ------
      MCSE of elpd_loo is 0.1.
      MCSE and ESS estimates assume MCMC draws (r_eff in [0.4, 2.4]).
      
      All Pareto k estimates are good (k < 0.7).
      See help('pareto-k-diagnostic') for details.
      
      > loo(Model_varying_slopes_stan)
      
      Computed from 4000 by 2425 log-likelihood matrix.
      
               Estimate   SE
      elpd_loo  -4503.9 47.7
      p_loo       124.4  4.9
      looic      9007.7 95.5
      ------
      MCSE of elpd_loo is 0.2.
      MCSE and ESS estimates assume MCMC draws (r_eff in [0.4, 1.8]).
      
      All Pareto k estimates are good (k < 0.7).
      See help('pareto-k-diagnostic') for details.
      
      > loo_compare(loo(Model_stan), loo(Model_varying_slopes_stan))
                                elpd_diff se_diff
      Model_varying_slopes_stan    0.0       0.0 
      Model_stan                -489.2      29.7 
      
  3. I wish I had read this before!

    This “It’s possible that the researchers tried this varying-slope fit, saw the singularity, got scared, and retreated to the simpler varying-intercept model,.. ” describes *exactly* my experience.

    Thanks a lot Andrew and Aki for expanding the topic.

  4. I also find it scary that a fairly subtle analysis error could make a big difference. Would this have been noticed if an identical analysis had been made on simulated data generated from a known model? Would it have been necessary for analyses to be run on multiple simulated datasets for the incorrect estimate of standard error to show up? Would the simulated data need to be generated with varying slopes for it to replicate the problem due to varying slopes in the real data?

  5. Now do one where missing data *is* the point of the story. When we did field studies (real fields, as in tomatoes or soybeans) we always had a great deal of missing data and there was a lot of hemming and hawing from the statisticians.

  6. If you don’t want to be scared by singular fit warnings nor bulk ESS warnings, you can also use priors with glmmTMB, imposing it on the random effects parameters is often sufficient:

    “`
    prior <- data.frame(prior = "gamma(1, 2.5)", class = "ranef")
    glmmTMB(
    y ~ condition + (1 + condition | Subject) + (1 + condition | ReponseID),
    data = data,
    priors = prior
    )
    “`

  7. Andrew, your recommendation in this post is wrong even given your own data, and part of the problem is that I think that you don’t know about some of the underlying problems in lmer.

    I think there is a further problem that I cannot reproduce your display output that you show at the very beginning of your post. My output differs dramatically from yours (look at the fixed effects slopes’ SEs in my vs your output):

    > display(Model_varying_slopes)
    lmer(formula = Healing ~ Condition + (1 + Condition | Subject) +
    (1 + Condition | ResponseId), data = DFmodel, REML = FALSE)
    coef.est coef.se
    (Intercept) 6.18 0.39
    Condition28 0.25 0.36
    Condition56 1.09 0.37

    There is something seriously wrong either in my code (which is just running your osf code without changes) or
    you have made a major mistake somewhere, or the display function is misbehaving on your machine (you are Windows user so I would not surprised if that were the reason :).

    A second point:

    In general, I get very worried when you make blanket statements like these, because then at least in my psycholinguistics circles, everyone starts blindly following your recommendations (“Andrew Gelman said this”). The “recommendation” gets entrenched in the field and the downsides of following the recommendation get lost. For example, it has been attributed to you that one should never log transform reading times. People repeatedly tell me that Andrew Gelman recommends not log transforming reading times because the residual’s normality does not matter (it does for frequentist null hypothesis significance testing).

    In the present case, your recommendation is highly misleading at best. This is apart from the fact that i cannot reproduce your output, as I explain above. Maybe I have done something wrong here, please correct me if I got this wrong.

    In your own paper, there were important problems in your own code that you just ignored, and I suspect that you are not even aware of how to avoid those problems. Here is the random effects structure of your varying slopes model as produced by me (so it may not correspond to your irreproducible example in your post):

    Random effects:
    Groups Name Variance Std.Dev. Corr
    Subject (Intercept) 2.920927 1.7091
    Condition28 3.954939 1.9887 -0.71
    Condition56 4.106190 2.0264 -0.72 0.65
    ResponseId (Intercept) 1.540464 1.2412
    Condition28 0.004748 0.0689 1.00
    Condition56 0.016546 0.1286 -1.00 -1.00
    Residual 2.271559 1.5072

    The problem here is the +/- 1 correlations in the ResponseID grouping variable. How to get rid of them? The || syntax for ResponseID will not work given the way the data frame is currently set up:

    Model_varying_slopes2 <- lmer(Healing ~ Condition +
    (1 + Condition | Subject) +
    (1 + Condition || ResponseId), data = DFmodel, REML = FALSE)

    summary(Model_varying_slopes2)

    The reason the correlations are still showing up in the ResponseID grouping factor despite the || syntax is that you are using the default treatment coding defined on the Condition vector:

    > contrasts(DFmodel$Condition)
    28 56
    14 0 0
    28 1 0
    56 0 1

    In lmer, the only way I know of getting rid of correlations with || syntax is to define two vectors for each of those comparisons:

    DFmodel$c28<-ifelse(DFmodel$Condition=="14",0,
    ifelse(DFmodel$Condition=="28",1,0))

    DFmodel$c56<-ifelse(DFmodel$Condition=="14",0,
    ifelse(DFmodel$Condition=="56",1,0))

    Interestingly, now the model continues to give a singularity warning, but the correlations are gone as they should be. What has changed is that the varying slopes in ResponseID cannot be estimated:

    Model_varying_slopes3 <- lmer(Healing ~ c28 + c56 +
    (1 + c28 + c56 | Subject) +
    (1 + c28 + c56 || ResponseId),
    data = DFmodel, REML = FALSE)

    Look at the random effects structure in this model:

    Random effects:
    Groups Name Variance Std.Dev. Corr
    Subject (Intercept) 2.921 1.709
    c28 3.956 1.989 -0.71
    c56 4.107 2.026 -0.71 0.65
    ResponseId (Intercept) 1.493 1.222
    ResponseId.1 c28 0.000 0.000
    ResponseId.2 c56 0.000 0.000
    Residual 2.279 1.510

    The solution here is to remove those 0 estimates–they are the ones causing the singularity and removing them will have zero impact on the t-values of interest. This is an important point that you ignore in your post. Now I have no singularity warning, but I did keep the varying slopes by subject; they do not cause any problems.

    > summary(Model_varying_slopes4)
    Linear mixed model fit by maximum likelihood [‘lmerMod’]
    Formula:
    Healing ~ c28 + c56 + (1 + c28 + c56 | Subject) + (1 | ResponseId)
    Data: DFmodel

    AIC BIC logLik deviance df.resid
    9314.9 9378.7 -4646.5 9292.9 2414

    Scaled residuals:
    Min 1Q Median 3Q Max
    -4.8417 -0.5035 0.0483 0.5971 3.1591

    Random effects:
    Groups Name Variance Std.Dev. Corr
    Subject (Intercept) 2.921 1.709
    c28 3.956 1.989 -0.71
    c56 4.107 2.027 -0.71 0.65
    ResponseId (Intercept) 1.493 1.222
    Residual 2.279 1.509
    Number of obs: 2425, groups: Subject, 33; ResponseId, 25

    Fixed effects:
    Estimate Std. Error t value
    (Intercept) 6.1845 0.3920 15.775
    c28 0.2459 0.3579 0.687
    c56 1.0885 0.3668 2.968

    Now, let’s compare the fixed effects estimates of your original model as computed by me (IMO incorrectly specified) with mine:

    ## Original Gelman fixed effects model with incorrectly specified model:
    Fixed effects:
    Estimate Std. Error t value
    (Intercept) 6.1846 0.3944 15.680
    Condition28 0.2458 0.3581 0.686
    Condition56 1.0885 0.3676 2.961
    ## My model’s fixed effects:
    Fixed effects:
    Estimate Std. Error t value
    (Intercept) 6.1845 0.3920 15.775
    c28 0.2459 0.3579 0.687
    c56 1.0885 0.3668 2.968

    Apart from minor numerical differences in the decimal places, the estimates are basically unchanged, but in my model I simplified the model without any overenthusiastic t-values turning up, and there are no singularity warnings.

    I see no upside to ignoring the singularity warnings; what they are telling us is that the vcov matrix in question is degenerate, and if as statisticians we care about fitting a model that makes any kind of sense, we should try to fix that problem.

    Your main point in the post was:

    > When you want to estimate treatment effects from a clustered design, you need to fit a multilevel model or make some equivalent adjustment. Otherwise your standard errors will be too small.

    That is absolutely not true in your example, that the SEs end up being too small. I can’t understand how you got this display output:

    lmer(formula = Healing ~ Condition + (1 | Subject) + (1 | ResponseId),
    data = DFmodel, REML = FALSE)
    coef.est coef.se
    (Intercept) 6.20 0.31
    Condition28 0.23 0.09
    Condition56 1.05 0.09

    Apart from some possible bug in your display function, this obscure problem in lmer is the real reason for all this confusion. I don’t know why Ben Bolker or whoever is in charge of lmer these days doesn’t fix the issue that if you define contrasts on the factor levels like you did, the model will continue to produce singularity warnings because it continues to fit correlation terms even if one uses a || syntax to remove them.

    A second point:

    Maybe you clarified this somewhere and I missed it, but I think your post will confuse people who have already aggregated the data by stimulusID (using your code and data as an example). In those cases, they cannot add a varying slope. I saw someone getting confused by your post on mastodon regarding this point.

    So, in your data, this output will show multiple data points per condition and subject, and per condition and item.

    “`
    xtabs(~Subject+Condition,DFmodel)
    xtabs(~ResponseId+Condition,DFmodel)
    “`

    If the output of xtabs is 1 in every cell, one cannot fit varying slopes. This is not obvious to everyone and is a continuing source of confusion among end-users.

    • Shravan:

      At the first page of our linked paper, it says, “Data and code are at
      https://osf.io/rh6g5/.”

      If you go to https://osf.io/rh6g5/, you can download healing.R. If you look at healing.R, you’ll see that it uses the datasets 41598_2023_50009_MOESM3_ESM.RData, MindsetMattersData_8.08.sav, and MindsetMatters_2007.sav. So download those three files in the same directory where you have downloaded healing.R.

      To check, I just did all of this, downloading these files into a newly-created directory. I then opened a fresh Rstudio session, went to this directory, and copied the code from healing.R into the console, one paragraph at a time. I just ran the first part of the code, the reanalysis of the Aungle and Langer data. (The second part of the code is the reanalysis of the Crum and Langer data, which is discussed in our paper but not in the above post, so I won’t get into it here.)

      Below is the complete console output. It looks exactly like what is in the above post, so I don’t see why you say our example is irreproducible. You write, “I can’t understand how you got this display output: . . .” Just run the code as is and you’ll see it right there. Or just look at the output below. And it’s not a bug in our display function. If you do summary(Model), you’ll see the exact same results. My recommendation is, before saying someone’s code is irreproducible, try reproducing it exactly as it is written.

      Regarding your other point: yes, it is possible for a user to get rid of the warning messages by including varying slopes for the participants but not for the raters. We didn’t get into this in our paper because I think it’s better to follow the general practice of allowing the slopes to vary for both factors, as in some settings the treatment effect could vary by rater. Keeping only one set of varying slopes would work for this example, but as general advice, it raises its own problems..

      R version 4.4.2 (2024-10-31) -- "Pile of Leaves"
      Copyright (C) 2024 The R Foundation for Statistical Computing
      Platform: aarch64-apple-darwin20
      
      R is free software and comes with ABSOLUTELY NO WARRANTY.
      You are welcome to redistribute it under certain conditions.
      Type 'license()' or 'licence()' for distribution details.
      
        Natural language support but running in an English locale
      
      R is a collaborative project with many contributors.
      Type 'contributors()' for more information and
      'citation()' on how to cite R or R packages in publications.
      
      Type 'demo()' for some demos, 'help()' for on-line help, or
      'help.start()' for an HTML browser interface to help.
      Type 'q()' to quit R.
      
      > setwd("~/AndrewFiles/research/healing/shravan_test")
      > ## Reanalyze part of "Physical healing as a function of perceived time," by Aungle and Langer (2023)
      > 
      > if(!require("pacman")) install.packages("pacman")
      Loading required package: pacman
      > pacman::p_load('tidyverse', 'svMisc', 'Rmisc','lme4','nlme', 'effects','rstanarm',
      +                'sjPlot','car','lattice', 'emmeans','ggstatsplot','yarrr','cowplot','gridGraphics', 'corrplot', 'broom.mixed','knitr')
      > 
      > load("41598_2023_50009_MOESM3_ESM.RData")   # Data from the published paper
      > subjects <- unique(DFmodel[,"Subject"])
      > n_subjects <- length(subjects)
      > conditions <- unique(DFmodel[,"Condition"])
      > raters <- unique(DFmodel[,"ResponseId"])
      > # Basic "mixed-effects" analysis taken from their file "41598_2023_50009_MOESM2_ESM.r"
      > library("lme4")
      > Model <- lmer(Healing ~ Condition + (1 | Subject) + (1 | ResponseId), data = DFmodel, REML = FALSE)
      > 
      > # Display the fit in a convenient form
      > library("arm")
      Loading required package: MASS
      
      Attaching package: ‘MASS’
      
      The following object is masked from ‘package:dplyr’:
      
          select
      
      
      arm (Version 1.14-4, built: 2024-4-1)
      
      Working directory is /Users/andrew/AndrewFiles/research/healing/shravan_test
      
      
      Attaching package: ‘arm’
      
      The following object is masked from ‘package:corrplot’:
      
          corrplot
      
      The following object is masked from ‘package:coda’:
      
          traceplot
      
      The following object is masked from ‘package:car’:
      
          logit
      
      The following objects are masked from ‘package:rstanarm’:
      
          invlogit, logit
      
      > display(Model)
      lmer(formula = Healing ~ Condition + (1 | Subject) + (1 | ResponseId), 
          data = DFmodel, REML = FALSE)
                  coef.est coef.se
      (Intercept) 6.20     0.31   
      Condition28 0.23     0.09   
      Condition56 1.05     0.09   
      
      Error terms:
       Groups     Name        Std.Dev.
       Subject    (Intercept) 1.07    
       ResponseId (Intercept) 1.22    
       Residual               1.87    
      ---
      number of obs: 2425, groups: Subject, 33; ResponseId, 25
      AIC = 10131.4, DIC = 10119.4
      deviance = 10119.4 
      > # Include varying slopes
      > Model_varying_slopes <- lmer(Healing ~ Condition + (1 + Condition | Subject) + (1 + Condition | ResponseId), data = DFmodel, REML = FALSE)
      boundary (singular) fit: see help('isSingular')
      > display(Model_varying_slopes)
      lmer(formula = Healing ~ Condition + (1 + Condition | Subject) + 
          (1 + Condition | ResponseId), data = DFmodel, REML = FALSE)
                  coef.est coef.se
      (Intercept) 6.18     0.39   
      Condition28 0.25     0.36   
      Condition56 1.09     0.37   
      
      Error terms:
       Groups     Name        Std.Dev. Corr        
       Subject    (Intercept) 1.71                 
                  Condition28 1.99     -0.71       
                  Condition56 2.03     -0.72  0.65 
       ResponseId (Intercept) 1.24                 
                  Condition28 0.07      1.00       
                  Condition56 0.13     -1.00 -1.00 
       Residual               1.51                 
      ---
      number of obs: 2425, groups: Subject, 33; ResponseId, 25
      AIC = 9317.9, DIC = 9285.9
      deviance = 9285.9 
      > # Print t statistics
      > print(fixef(Model)/se.fixef(Model))
      (Intercept) Condition28 Condition56 
        19.765707    2.438166   11.106316 
      > print(fixef(Model_varying_slopes)/se.fixef(Model_varying_slopes))
      (Intercept) Condition28 Condition56 
       15.6796175   0.6864973   2.9609342 
      > # Compare conditions c1 and c2
      > compare_paired <- function(c1, c2){
      +   diff <- rep(NA, n_subjects)
      +   for (i in 1:n_subjects){
      +     ok <- DFmodel[,"Subject"]==subjects[i]
      +     ok1 <- ok & DFmodel[,"Condition"]==conditions[c1]
      +     ok2 <- ok & DFmodel[,"Condition"]==conditions[c2]
      +     diff[i] <- mean(DFmodel[ok2,"Healing"]) - mean(DFmodel[ok1,"Healing"])
      +   }
      +   n <- sum(!is.na(diff))
      +   est <- mean(diff, na.rm=TRUE)
      +   se <- sd(diff, na.rm=TRUE)/sqrt(n)
      +   c(n, est, se)
      + }
      > compare_paired(2, 3)
      [1] 32.000000  0.789500  0.308623
      > compare_paired(1, 3)
      [1] 31.0000000  1.1003871  0.3833149
      > compare_paired(1, 2)
      [1] 32.0000000  0.1967500  0.3635985
      >
      
  8. > My recommendation is, before saying someone’s code is irreproducible, try reproducing it exactly as it is written.

    That is exactly what I did. I just downloaded the osf repo like you said, and ran the file without any changes. But I see the mistake now. The code is reproducible–my bad. I was looking at the wrong model output. Apologies for mixing up the model outputs.

    However, my point stands: the recommendation to ignore the singularity warnings seems wrong in this example. Why fit an overparameterized model with a degenerate vcov matrix? Fixing the singularity has no impact on the NHST inferences; at least in this example you provide on osf, I see no effect on the SE when one ignores the singularity warning vs when one gets rid of it. So while it’s true that the SE is artificially small in the original no-varying slopes model and changes in the varying slopes model with singularities that you fit (this is the point of Barr et al 2013), one can find a model that has a more realistic SE but has no singularity warnings. So it makes no sense to tell people to ignore singularity warnings–why not fit a model that makes sense?

    A related point I made here is that one should define the contrasts as standalone vectors and not use the contrasts function to assign contrasts (e.g., treatment contrasts) in linear mixed models, because that makes removing the singularity warnings impossible, e.g., when one tries to remove the correlation terms in the random effects.

    • Shravan:

      Thank you for apologizing.

      Regarding the analysis: in our paper we did not try to propose an ideal analysis of these data, as that way lies madness. Instead, our goal was to figure out what the authors did wrong that led them to those ridiculous t-statistics. Before seeing the authors’ code, my first guess had been that they’d not accounted for the multilevel structure of the data at all. Then I saw their code, they were using lme4, and at first I couldn’t figure out what they’d done wrong. It took a bit of playing around for me to realize that they hadn’t allowed for varying slopes. The necessity to include varying slopes is a subtle point that we did not make clear in our multilevel modeling book–so I thought it was worth a post.

      You write, “the recommendation to ignore the singularity warnings seems wrong in this example.” That’s fine. I did not recommend that anyone ignore the singularity warnings. It’s good to understand the singularity–indeed, this is the topic of our above-linked paper from 2014. As noted in my above comment, in this particular example it would work to keep the varying slopes for participants but not for raters, but that was a subtlety I didn’t want to get into. In general, I’d rather that researchers include all the varying slopes–getting that degenerate covariance estimate is no big deal–, rather than have them fumbling around letting error messages make their modeling decisions for them.

      Again, you say, “it makes no sense to tell people to ignore singularity warnings.” Fortunately, I never told people to ignore singularity warnings. I’d rather have people fit these models using boundary-avoiding priors or full Bayes–either of these options would remove the singularity entirely–but, here, the most important point is to include the varying slopes for participants. It was scary to see this problem where researchers had a multilevel structure and they knew enough to fit the multilevel model but not enough to allow varying slopes.

      Stepping back, I agree with the commenters who said that, given that the researchers didn’t know how to analyze this sort of data, they should’ve designed a cleaner experiment or analyzed the data in a cleaner way (as discussed in section 2.3 of our paper). And, in any case, it was not appropriate for them to have mapped their data analysis results into the substantive claims that they were making (as discussed in section 2.4 of our paper). So, lots of problems, one of which was their use of a multilevel model with varying intercepts but not varying slopes, which was the subject of the above post.

      • > I did not recommend that anyone ignore the singularity warnings.

        Oh, I posted on this because I suspect that the title “even if the fit gives warning messages” will invite the inference that it’s OK to ignore singularity warnings. Am I the only one who reads that part of the title as a recommendation?

        One important question:

        > I’d rather that researchers include all the varying slopes–getting that degenerate covariance estimate is no big deal–, rather than have them fumbling around letting error messages make their modeling decisions for them.

        This point (getting that degenerate covariance estimate is no big deal) is something I genuinely never really understood. I can see that it’s just a warning (but we do pay attention to warnings in Stan for example), so maybe OK to ignore. Plus the model output doesn’t change regardless of the singularity warning. So I always thought the objection to singularities is just conceptual and in principle: in principle, we want to fit models without degenerate vcov matrices. It doesn’t seem to matter as regards the NHST and estimation. It would be useful to spell out at some point, maybe in a future post) the pros and cons of ignoring singularities. We are so strict with divergent transitions even though sometimes they don’t really matter much–are they also not a big deal sometimes? I’m not clear on this.

        Although all this discussion is moot: just fit a Stan/brms model with a full vcov matrix and regularizing priors. Fertig. I don’t even know why people fit frequentist models any more.

        • +1. Sometimes those divergences are clearly about pathologies like ‘Neal’s Funnel’ in hierarchical models, or multimodality. In those cases, reparameterizing often pays off big time, in both better inference and faster computation. Other times, they seem pretty inexplicable and getting rid of them doesn’t matter at all, and sometimes is not readily apparent how to, but yet everything else looks fine…

  9. “The article in question estimated treatment effects with a cluster design: they had three treatments applied to 33 research participants (in psychology jargon, “subjects”) with data provided by 25 raters.”

    This in itself raises a huge red flag to me. If one of my students had come to me with such a design I would say “don’t bother”.

Leave a Reply

Your email address will not be published. Required fields are marked *