3
$\begingroup$

I have a dataset where the primary result is the test results (a binary variable 0 or 1) from doctors. I want to see if a continuous variable X is going to affect the test result.

I think in this dataset, one row represents one test. But in this dataset, there are multiple rows from the same doctor, which means some doctors conducted the tests more than once. So I wanted to build a model to include doctor as a random effect, like this:

glmer (test_result ~ X + (1| doctor_ID),family= binomial)

How is this different from a regular logistic regression where you put doctor ID as a covariate?

glm(test_result ~ X + doctor_ID, family= binomial)

What if I have another random effect variable patient_ID (tests results from the same patient as a cluster). How do I add this to the model?

$\endgroup$

2 Answers 2

3
$\begingroup$

When you put doctor ID as a covariate, then you'd have a fixed effect regression. In contrast, (1| doctor_ID) means that you added a random constant for each doctor in your sample.

Different Mechanical Effect
Fixed effects assume the doctor's identity has a fixed effect on the outcome variable. In other words, the values of the fixed effect do not vary randomly across the observations in the dataset.

Random effects, on the other hand, assume that doctor's identity has a random effect on the outcome variable. In other words, the values of the random effect vary randomly across the observations in the dataset. For example, if we are studying the effect of different hospitals on patient outcomes, the hospital where the patient receives treatment would be considered a random effect because it varies randomly across patients in the study.

When to use each Type of Effect?
Fixed effects are used when the factors of interest are explicitly chosen by the experimenter and are considered to be a fixed part of the experimental design. Fixed effects are appropriate when the goal is to estimate the effects of specific treatments or interventions, such as different doses of a drug or different types of fertilizers. In such cases, the fixed effects capture the systematic variation in the data due to the treatment or intervention.

Random effects, are used when the factors of interest are not explicitly chosen by the experimenter and are considered to be a random sample from a larger population. Random effects are appropriate when the goal is to estimate the variability among a larger population of similar units, such as in your case.

$\endgroup$
1
$\begingroup$

GLM Fit (No RE)

David provides a summary of when you want fixed and random effects. I'd like to add that what you want from here is often of practical interest. Consider for your example that you have a simple regression, doctor experience in years and whether or not somebody's cancer goes into remission during treatment. We can tests this with the data from this tutorial.

#### Load Data ####
hdp <- read.csv("https://stats.idre.ucla.edu/stat/data/hdp.csv")
hdp <- within(hdp, {
  Married <- factor(Married, levels = 0:1, labels = c("no", "yes"))
  DID <- factor(DID)
  HID <- factor(HID)
  CancerStage <- factor(CancerStage)
})
head(hdp)

We can then fit a basic logistic regression without considering the doctor covariate.

#### Fit GLM ####
fit.glm <- glm(
  remission ~ Experience,
  family = binomial,
  data = hdp
)
summary(fit.glm)

The results show that experience seems to have a significant impact on remission rates, though it doesn't seem to be a large effect.

Call:
glm(formula = remission ~ Experience, family = binomial, data = hdp)

Deviance Residuals: 
    Min       1Q   Median       3Q      Max  
-1.1909  -0.8690  -0.7568   1.3757   1.9203  

Coefficients:
             Estimate Std. Error z value Pr(>|z|)    
(Intercept) -2.320558   0.111349  -20.84   <2e-16 ***
Experience   0.081116   0.005985   13.55   <2e-16 ***
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

(Dispersion parameter for binomial family taken to be 1)

    Null deviance: 10353  on 8524  degrees of freedom
Residual deviance: 10164  on 8523  degrees of freedom
AIC: 10168

Number of Fisher Scoring iterations: 4

What if we entered doctors into the regression? Warning: running this will take awhile and the results are likely not even accurate.

#### Fit GLM ####
fit.glm.did <- glm(
  remission ~ Experience + DID,
  family = binomial,
  data = hdp
)

We now have a massive regression summary...more than 200 coefficients that we now have to interpret separately.

Call:
glm(formula = remission ~ Experience + DID, family = binomial, 
    data = hdp)

Deviance Residuals: 
    Min       1Q   Median       3Q      Max  
-2.2300  -0.6685  -0.2522   0.5660   2.6551  

Coefficients:
              Estimate Std. Error z value Pr(>|z|)
(Intercept) -5.287e+10  1.099e+12  -0.048    0.962
Experience   2.115e+09  4.395e+10   0.048    0.962
DID2         2.326e+10  4.834e+11   0.048    0.962
DID3         1.269e+10  2.637e+11   0.048    0.962
DID4         1.480e+10  3.076e+11   0.048    0.962
DID5         1.692e+10  3.516e+11   0.048    0.962
DID6         2.326e+10  4.834e+11   0.048    0.962
DID7         1.903e+10  3.955e+11   0.048    0.962
DID8         8.459e+09  1.758e+11   0.048    0.962
DID9         2.326e+10  4.834e+11   0.048    0.962
DID10        1.480e+10  3.076e+11   0.048    0.962
DID11        3.172e+10  6.592e+11   0.048    0.962
DID12        4.229e+09  8.790e+10   0.048    0.962
DID13        2.326e+10  4.834e+11   0.048    0.962
DID14        4.229e+09  8.790e+10   0.048    0.962
DID15        2.538e+10  5.274e+11   0.048    0.962
DID16        2.326e+10  4.834e+11   0.048    0.962
DID17        2.115e+10  4.395e+11   0.048    0.962
DID18        1.480e+10  3.076e+11   0.048    0.962
DID19        8.459e+09  1.758e+11   0.048    0.962
DID20        6.344e+09  1.318e+11   0.048    0.962
DID21        8.459e+09  1.758e+11   0.048    0.962
DID22        6.344e+09  1.318e+11   0.048    0.962
DID23        2.538e+10  5.274e+11   0.048    0.962
DID24        1.269e+10  2.637e+11   0.048    0.962
DID25        1.269e+10  2.637e+11   0.048    0.962
DID26        2.961e+10  6.153e+11   0.048    0.962
DID27        6.344e+09  1.318e+11   0.048    0.962
DID28        3.383e+10  7.032e+11   0.048    0.962
DID29        8.459e+09  1.758e+11   0.048    0.962
DID30        2.961e+10  6.153e+11   0.048    0.962
DID31        4.229e+09  8.790e+10   0.048    0.962
DID32        8.459e+09  1.758e+11   0.048    0.962
DID33        1.903e+10  3.955e+11   0.048    0.962
DID34        2.115e+10  4.395e+11   0.048    0.962
DID35        1.692e+10  3.516e+11   0.048    0.962
DID36        1.057e+10  2.197e+11   0.048    0.962
DID37        1.269e+10  2.637e+11   0.048    0.962
DID38        2.961e+10  6.153e+11   0.048    0.962
DID39        1.903e+10  3.955e+11   0.048    0.962
DID40        4.229e+09  8.790e+10   0.048    0.962
DID41        8.459e+09  1.758e+11   0.048    0.962
DID42        1.480e+10  3.076e+11   0.048    0.962
DID43        2.749e+10  5.713e+11   0.048    0.962
DID44        2.538e+10  5.274e+11   0.048    0.962
DID45        6.344e+09  1.318e+11   0.048    0.962
DID46        2.749e+10  5.713e+11   0.048    0.962
DID47        8.459e+09  1.758e+11   0.048    0.962
DID48        4.229e+09  8.790e+10   0.048    0.962
DID49        2.115e+10  4.395e+11   0.048    0.962
DID50       -4.229e+09  8.790e+10  -0.048    0.962
DID51        8.459e+09  1.758e+11   0.048    0.962
DID52        1.269e+10  2.637e+11   0.048    0.962
DID53        1.692e+10  3.516e+11   0.048    0.962
DID54        3.595e+10  7.471e+11   0.048    0.962
DID55        8.459e+09  1.758e+11   0.048    0.962
DID56        2.115e+10  4.395e+11   0.048    0.962
DID57        8.459e+09  1.758e+11   0.048    0.962
DID58        8.459e+09  1.758e+11   0.048    0.962
DID59        1.057e+10  2.197e+11   0.048    0.962
DID60        1.903e+10  3.955e+11   0.048    0.962
DID61        2.538e+10  5.274e+11   0.048    0.962
DID62        2.749e+10  5.713e+11   0.048    0.962
DID63        1.480e+10  3.076e+11   0.048    0.962
DID64        4.229e+09  8.790e+10   0.048    0.962
DID65        1.692e+10  3.516e+11   0.048    0.962
DID66        1.903e+10  3.955e+11   0.048    0.962
DID67        2.538e+10  5.274e+11   0.048    0.962
DID68        2.326e+10  4.834e+11   0.048    0.962
DID69        2.115e+10  4.395e+11   0.048    0.962
DID70        2.538e+10  5.274e+11   0.048    0.962
DID71        1.480e+10  3.076e+11   0.048    0.962
DID72        2.115e+09  4.395e+10   0.048    0.962
DID73        2.326e+10  4.834e+11   0.048    0.962
DID74        2.326e+10  4.834e+11   0.048    0.962
DID75        2.115e+10  4.395e+11   0.048    0.962
DID76        1.269e+10  2.637e+11   0.048    0.962
DID77        1.480e+10  3.076e+11   0.048    0.962
DID78        2.749e+10  5.713e+11   0.048    0.962
DID79        6.344e+09  1.318e+11   0.048    0.962
DID80        8.459e+09  1.758e+11   0.048    0.962
DID81        1.269e+10  2.637e+11   0.048    0.962
DID82        2.115e+10  4.395e+11   0.048    0.962
DID83        1.480e+10  3.076e+11   0.048    0.962
DID84        2.326e+10  4.834e+11   0.048    0.962
DID85        1.269e+10  2.637e+11   0.048    0.962
DID86        1.057e+10  2.197e+11   0.048    0.962
DID87        2.115e+10  4.395e+11   0.048    0.962
DID88        2.326e+10  4.834e+11   0.048    0.962
DID89        8.459e+09  1.758e+11   0.048    0.962
DID90        1.903e+10  3.955e+11   0.048    0.962
DID91        1.692e+10  3.516e+11   0.048    0.962
DID92        8.459e+09  1.758e+11   0.048    0.962
DID93        2.538e+10  5.274e+11   0.048    0.962
DID94        6.344e+09  1.318e+11   0.048    0.962
DID95        2.115e+09  4.395e+10   0.048    0.962
DID96        6.344e+09  1.318e+11   0.048    0.962
DID97        1.692e+10  3.516e+11   0.048    0.962
DID98        2.115e+10  4.395e+11   0.048    0.962
DID99        2.326e+10  4.834e+11   0.048    0.962
DID100       2.749e+10  5.713e+11   0.048    0.962
DID101       2.749e+10  5.713e+11   0.048    0.962
DID102       1.057e+10  2.197e+11   0.048    0.962
DID103       1.692e+10  3.516e+11   0.048    0.962
DID104       2.115e+10  4.395e+11   0.048    0.962
DID105       1.903e+10  3.955e+11   0.048    0.962
DID106       1.057e+10  2.197e+11   0.048    0.962
DID107       6.344e+09  1.318e+11   0.048    0.962
DID108       8.459e+09  1.758e+11   0.048    0.962
DID109       2.115e+09  4.395e+10   0.048    0.962
DID110       1.480e+10  3.076e+11   0.048    0.962
DID111       1.057e+10  2.197e+11   0.048    0.962
DID112       1.903e+10  3.955e+11   0.048    0.962
DID113       1.381e+01  7.105e+02   0.019    0.984
DID114       2.115e+09  4.395e+10   0.048    0.962
DID115       1.480e+10  3.076e+11   0.048    0.962
DID116       4.229e+09  8.790e+10   0.048    0.962
DID117       1.480e+10  3.076e+11   0.048    0.962
DID118       1.480e+10  3.076e+11   0.048    0.962
DID119       1.480e+10  3.076e+11   0.048    0.962
DID120       8.459e+09  1.758e+11   0.048    0.962
DID121       4.229e+09  8.790e+10   0.048    0.962
DID122       2.115e+10  4.395e+11   0.048    0.962
DID123       1.057e+10  2.197e+11   0.048    0.962
DID124       2.115e+10  4.395e+11   0.048    0.962
DID125       1.269e+10  2.637e+11   0.048    0.962
DID126       2.749e+10  5.713e+11   0.048    0.962
DID127       1.692e+10  3.516e+11   0.048    0.962
DID128       1.346e+01  7.105e+02   0.019    0.985
DID129       1.692e+10  3.516e+11   0.048    0.962
DID130       2.115e+09  4.395e+10   0.048    0.962
DID131       1.692e+10  3.516e+11   0.048    0.962
DID132       8.459e+09  1.758e+11   0.048    0.962
DID133       1.692e+10  3.516e+11   0.048    0.962
DID134       1.903e+10  3.955e+11   0.048    0.962
DID135       1.480e+10  3.076e+11   0.048    0.962
DID136       3.172e+10  6.592e+11   0.048    0.962
DID137       2.115e+10  4.395e+11   0.048    0.962
DID138       1.269e+10  2.637e+11   0.048    0.962
DID139       1.269e+10  2.637e+11   0.048    0.962
DID140       1.057e+10  2.197e+11   0.048    0.962
DID141       2.115e+10  4.395e+11   0.048    0.962
DID142       2.326e+10  4.834e+11   0.048    0.962
DID143       1.692e+10  3.516e+11   0.048    0.962
DID144      -8.459e+09  1.758e+11  -0.048    0.962
DID145       1.903e+10  3.955e+11   0.048    0.962
DID146       1.480e+10  3.076e+11   0.048    0.962
DID147       1.269e+10  2.637e+11   0.048    0.962
DID148       1.903e+10  3.955e+11   0.048    0.962
DID149       2.326e+10  4.834e+11   0.048    0.962
DID150       4.229e+09  8.790e+10   0.048    0.962
DID151       3.172e+10  6.592e+11   0.048    0.962
DID152       1.480e+10  3.076e+11   0.048    0.962
DID153       1.269e+10  2.637e+11   0.048    0.962
DID154       4.229e+09  8.790e+10   0.048    0.962
DID155       1.480e+10  3.076e+11   0.048    0.962
DID156       1.903e+10  3.955e+11   0.048    0.962
DID157       3.172e+10  6.592e+11   0.048    0.962
DID158       2.538e+10  5.274e+11   0.048    0.962
DID159       2.749e+10  5.713e+11   0.048    0.962
DID160       1.480e+10  3.076e+11   0.048    0.962
DID161       2.326e+10  4.834e+11   0.048    0.962
DID162       2.326e+10  4.834e+11   0.048    0.962
DID163       3.383e+10  7.032e+11   0.048    0.962
DID164       1.057e+10  2.197e+11   0.048    0.962
DID165       1.057e+10  2.197e+11   0.048    0.962
DID166       1.692e+10  3.516e+11   0.048    0.962
DID167       2.326e+10  4.834e+11   0.048    0.962
DID168       1.236e+01  7.105e+02   0.017    0.986
DID169       1.057e+10  2.197e+11   0.048    0.962
DID170       1.692e+10  3.516e+11   0.048    0.962
DID171       1.692e+10  3.516e+11   0.048    0.962
DID172       1.057e+10  2.197e+11   0.048    0.962
DID173       1.903e+10  3.955e+11   0.048    0.962
DID174       2.538e+10  5.274e+11   0.048    0.962
DID175       8.459e+09  1.758e+11   0.048    0.962
DID176       1.480e+10  3.076e+11   0.048    0.962
DID177       1.480e+10  3.076e+11   0.048    0.962
DID178       2.115e+10  4.395e+11   0.048    0.962
DID179       1.692e+10  3.516e+11   0.048    0.962
DID180       6.344e+09  1.318e+11   0.048    0.962
DID181       8.459e+09  1.758e+11   0.048    0.962
DID182       1.269e+10  2.637e+11   0.048    0.962
DID183       1.903e+10  3.955e+11   0.048    0.962
DID184       3.383e+10  7.032e+11   0.048    0.962
DID185       1.057e+10  2.197e+11   0.048    0.962
DID186       2.326e+10  4.834e+11   0.048    0.962
DID187       2.115e+10  4.395e+11   0.048    0.962
DID188       2.115e+09  4.395e+10   0.048    0.962
DID189       2.538e+10  5.274e+11   0.048    0.962
DID190       1.903e+10  3.955e+11   0.048    0.962
DID191       1.480e+10  3.076e+11   0.048    0.962
DID192       6.344e+09  1.318e+11   0.048    0.962
DID193       1.903e+10  3.955e+11   0.048    0.962
DID194       1.480e+10  3.076e+11   0.048    0.962
DID195       1.057e+10  2.197e+11   0.048    0.962
DID196       4.229e+09  8.790e+10   0.048    0.962
DID197       1.269e+10  2.637e+11   0.048    0.962
DID198       2.538e+10  5.274e+11   0.048    0.962
DID199       6.344e+09  1.318e+11   0.048    0.962
DID200       1.903e+10  3.955e+11   0.048    0.962
DID201       1.269e+10  2.637e+11   0.048    0.962
DID202       2.326e+10  4.834e+11   0.048    0.962
DID203       2.538e+10  5.274e+11   0.048    0.962
DID204       6.344e+09  1.318e+11   0.048    0.962
DID205       1.057e+10  2.197e+11   0.048    0.962
DID206      -1.362e+01  9.728e+04   0.000    1.000
DID207       2.115e+10  4.395e+11   0.048    0.962
DID208       1.903e+10  3.955e+11   0.048    0.962
DID209       1.269e+10  2.637e+11   0.048    0.962
DID210       1.269e+10  2.637e+11   0.048    0.962
DID211       1.480e+10  3.076e+11   0.048    0.962
DID212       2.326e+10  4.834e+11   0.048    0.962
DID213       2.115e+10  4.395e+11   0.048    0.962
DID214       2.749e+10  5.713e+11   0.048    0.962
DID215       2.326e+10  4.834e+11   0.048    0.962
DID216       8.459e+09  1.758e+11   0.048    0.962
DID217       1.057e+10  2.197e+11   0.048    0.962
DID218       1.480e+10  3.076e+11   0.048    0.962
DID219       2.538e+10  5.274e+11   0.048    0.962
DID220       1.269e+10  2.637e+11   0.048    0.962
DID221       2.749e+10  5.713e+11   0.048    0.962
DID222       1.903e+10  3.955e+11   0.048    0.962
DID223       1.903e+10  3.955e+11   0.048    0.962
DID224       8.459e+09  1.758e+11   0.048    0.962
DID225       3.383e+10  7.032e+11   0.048    0.962
DID226       2.749e+10  5.713e+11   0.048    0.962
DID227       1.269e+10  2.637e+11   0.048    0.962
DID228       1.692e+10  3.516e+11   0.048    0.962
DID229       1.480e+10  3.076e+11   0.048    0.962
DID230       4.229e+09  8.790e+10   0.048    0.962
DID231       2.538e+10  5.274e+11   0.048    0.962
DID232       1.057e+10  2.197e+11   0.048    0.962
DID233       1.269e+10  2.637e+11   0.048    0.962
DID234       2.326e+10  4.834e+11   0.048    0.962
DID235       1.480e+10  3.076e+11   0.048    0.962
DID236       2.115e+10  4.395e+11   0.048    0.962
DID237       1.057e+10  2.197e+11   0.048    0.962
DID238       2.326e+10  4.834e+11   0.048    0.962
DID239       8.459e+09  1.758e+11   0.048    0.962
DID240       1.480e+10  3.076e+11   0.048    0.962
DID241       1.269e+10  2.637e+11   0.048    0.962
DID242       2.115e+10  4.395e+11   0.048    0.962
DID243       1.903e+10  3.955e+11   0.048    0.962
DID244       2.115e+10  4.395e+11   0.048    0.962
DID245       6.344e+09  1.318e+11   0.048    0.962
DID246       1.903e+10  3.955e+11   0.048    0.962
DID247       6.344e+09  1.318e+11   0.048    0.962
DID248       8.459e+09  1.758e+11   0.048    0.962
DID249       1.057e+10  2.197e+11   0.048    0.962
 [ reached getOption("max.print") -- omitted 158 rows ]

(Dispersion parameter for binomial family taken to be 1)

    Null deviance: 10352.6  on 8524  degrees of freedom
Residual deviance:  6590.8  on 8117  degrees of freedom
AIC: 7406.8

Number of Fisher Scoring iterations: 25

GLMM Fit (RE Included)

What if instead we could just aggregate the variation in doctors and look at their contribution separate from our typical regression summary? This is where something like a mixed model can be really helpful. We can fit one below. For simplicity, I just use a random intercept model and don't include slopes.

#### Fit GLMM ####
library(lmerTest)

fit.glmm <- glmer(remission ~ Experience +
             (1 | DID), 
           data = hdp,
           family = binomial)
summary(fit.glmm)

It seems our estimate has changed now, in that the average effect of experience on remission has increased:

Generalized linear mixed model fit by maximum likelihood (Laplace Approximation) [
glmerMod]
 Family: binomial  ( logit )
Formula: remission ~ Experience + (1 | DID)
   Data: hdp

     AIC      BIC   logLik deviance df.resid 
  7868.6   7889.8  -3931.3   7862.6     8522 

Scaled residuals: 
    Min      1Q  Median      3Q     Max 
-2.6654 -0.4977 -0.2342  0.4656  4.8474 

Random effects:
 Groups Name        Variance Std.Dev.
 DID    (Intercept) 3.439    1.855   
Number of obs: 8525, groups:  DID, 407

Fixed effects:
            Estimate Std. Error z value Pr(>|z|)    
(Intercept) -3.40749    0.45797  -7.440  1.0e-13 ***
Experience   0.11319    0.02508   4.513  6.4e-06 ***
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

Correlation of Fixed Effects:
           (Intr)
Experience -0.975

But what happened to all those doctors? We can see from the regression results that remission rates vary by 1.855 SD between doctors on average. To show this visually, we can run the following code, which plots doctor intercepts onto a caterpillar plot.

library(lattice)
dotplot(ranef(fit.glmm))

enter image description here

We can see that these 200+ doctors here vary a lot.

Including Patients

Including patients here as you mention would be tricky, as it would depend on multiple factors. The main thing is that the subjects need to have repeated observations for this to work (they were seen more than once). Additionally, if they were seen by multiple doctors, this could be a case of crossed random effects. We don't have patient ID in this data, but assuming we had one called PID, a within-doctors random effect (patients are only seen by one doctor), would look like this:

fit.within <- glmer(remission ~ Experience +
             (1 | DID/PID), 
           data = hdp,
           family = binomial)

And a crossed effects design (patients were seen by multiple doctors) would look like this:

fit.crossed <- glmer(remission ~ Experience +
             (1 | DID) + (1|PID), 
           data = hdp,
           family = binomial)
$\endgroup$
2
  • $\begingroup$ Thank you so much! How do I change the code if I want to add random slope as well? $\endgroup$ Mar 27, 2023 at 0:17
  • $\begingroup$ If we say x is a predictor that varies within a cluster, then we simply add it before the intercept such as (1+x|RE) or (1+x||RE) if we don't want slopes to correlate with intercepts. For example, a correlated slopes/intercepts model could be: glmer(remission ~ Experience + (1+x | DID), data = hdp, family = binomial). $\endgroup$ Mar 27, 2023 at 1:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.