Skip to main content
Statistics LibreTexts

28.7: Appendix

  • Page ID
    8869
  • 28.7.1 The paired t-test as a linear model

    We can also define the paired t-test in terms of a general linear model. To do this, we include all of the measurements for each subject as data points (within a tidy data frame). We then include in the model a variable that codes for the identity of each individual (in this case, the ID variable that contains a subject ID for each person). This is known as a mixed model, since it includes effects of independent variables as well as effects of individuals. The standard model fitting procedure lm() can’t do this, but we can do it using the lmer() function from a popular R package called lme4, which is specialized for estimating mixed models. The (1|ID) in the formula tells lmer() to estimate a separate intercept (which is what the 1 refers to) for each value of the ID variable (i.e. for each individual in the dataset), and then estimate a common slope relating timepoint to BP.

    # compute mixed model for paired test
    
    lmrResult <- lmer(BPsys ~ timepoint + (1 | ID), 
                      data = NHANES_sample_tidy)
    summary(lmrResult)
    ## Linear mixed model fit by REML. t-tests use Satterthwaite's method [
    ## lmerModLmerTest]
    ## Formula: BPsys ~ timepoint + (1 | ID)
    ##    Data: NHANES_sample_tidy
    ## 
    ## REML criterion at convergence: 2895
    ## 
    ## Scaled residuals: 
    ##     Min      1Q  Median      3Q     Max 
    ## -2.3843 -0.4808  0.0076  0.4221  2.1718 
    ## 
    ## Random effects:
    ##  Groups   Name        Variance Std.Dev.
    ##  ID       (Intercept) 236.1    15.37   
    ##  Residual              13.9     3.73   
    ## Number of obs: 400, groups:  ID, 200
    ## 
    ## Fixed effects:
    ##                 Estimate Std. Error      df t value Pr(>|t|)    
    ## (Intercept)      121.370      1.118 210.361  108.55   <2e-16 ***
    ## timepointBPSys2   -1.020      0.373 199.000   -2.74   0.0068 ** 
    ## ---
    ## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
    ## 
    ## Correlation of Fixed Effects:
    ##             (Intr)
    ## tmpntBPSys2 -0.167

    You can see that this shows us a p-value that is very close to the result from the paired t-test computed using the t.test() function.