The metafor Package

A Meta-Analysis Package for R

User Tools

Site Tools


tips:models_with_or_without_intercept

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
tips:models_with_or_without_intercept [2021/10/29 10:55] Wolfgang Viechtbauertips:models_with_or_without_intercept [2024/06/18 19:29] (current) Wolfgang Viechtbauer
Line 64: Line 64:
 By default, ''alternate'' was made the reference level (since the first letter "a" comes before "r" and "s" in the alphabet), so the corresponding dummy variable for this level has been left out.((Since ''alloc'' is a character variable, the coercion to a factor via the ''factor()'' function isn't strictly necessary (since this would have happened automatically anyway), but it's better to be explicit. In some cases, a numeric variable may in fact be used to represent levels of a categorical variable, in which case use of the ''factor()'' function is crucial.)) Therefore, the model intercept (coefficients $b_0$) is the estimated (average) log risk ratio for alternating allocation. The coefficients for ''factor(alloc)random'' (coefficient $b_1$) and ''factor(alloc)systematic'' (coefficient $b_2$) indicate how much lower/higher the estimated (average) log risk ratio is for random and systematic allocation, respectively, compared to alternating allocation. So these coefficients reflect contrasts between these two levels compared to the reference level. Neither coefficient is significantly different from 0 (as indicated by the corresponding p-values), but this isn't a proper test of the factor as a whole. By default, ''alternate'' was made the reference level (since the first letter "a" comes before "r" and "s" in the alphabet), so the corresponding dummy variable for this level has been left out.((Since ''alloc'' is a character variable, the coercion to a factor via the ''factor()'' function isn't strictly necessary (since this would have happened automatically anyway), but it's better to be explicit. In some cases, a numeric variable may in fact be used to represent levels of a categorical variable, in which case use of the ''factor()'' function is crucial.)) Therefore, the model intercept (coefficients $b_0$) is the estimated (average) log risk ratio for alternating allocation. The coefficients for ''factor(alloc)random'' (coefficient $b_1$) and ''factor(alloc)systematic'' (coefficient $b_2$) indicate how much lower/higher the estimated (average) log risk ratio is for random and systematic allocation, respectively, compared to alternating allocation. So these coefficients reflect contrasts between these two levels compared to the reference level. Neither coefficient is significantly different from 0 (as indicated by the corresponding p-values), but this isn't a proper test of the factor as a whole.
  
-The results given under "Test of Moderators" provides a joint or [[wp>Omnibus_test|omnibus test]] of all model coefficients except for the intercept (''coefficients 2:3''). Careful: The numbering of the coefficients here starts at 1, corresponding to the intercept (''intrcpt''), with coefficients 2 and 3 corresponding to ''factor(alloc)random'' and ''factor(alloc)systematic''. The results indicate that we cannot reject the null hypothesis $H_0: \beta_1 = \beta_2 = 0$. Since we cannot reject the null hypothesis that random and systematic allocation are different from alternating allocation, then this implies that the three-level factor as a whole is not significant.+The results given under "Test of Moderators" provides a joint or [[wp>Omnibus_test|omnibus test]] of all model coefficients except for the intercept (''coefficients 2:3''). Careful: The numbering of the coefficients here starts at 1, corresponding to the intercept (''intrcpt''), with coefficients 2 and 3 corresponding to ''factor(alloc)random'' and ''factor(alloc)systematic''. The results indicate that we cannot reject the null hypothesis $\mbox{H}_0{:}\; \beta_1 = \beta_2 = 0$. Since we cannot reject the null hypothesis that random and systematic allocation are different from alternating allocation, then this implies that the three-level factor as a whole is not significant.
  
 We can also test one or multiple model coefficients ourselves (and contrasts between them, as we will see further below) using the ''anova()'' function. The same omnibus test as above can be obtained with: We can also test one or multiple model coefficients ourselves (and contrasts between them, as we will see further below) using the ''anova()'' function. The same omnibus test as above can be obtained with:
Line 78: Line 78:
 A different way of conducting the same test is to use the ''L'' argument, which allows us to specify one or more vectors of numbers, which are multiplied with the model coefficients. In particular, we can use: A different way of conducting the same test is to use the ''L'' argument, which allows us to specify one or more vectors of numbers, which are multiplied with the model coefficients. In particular, we can use:
 <code rsplus> <code rsplus>
-anova(res, L=rbind(c(0,1,0),c(0,0,1)))+anova(res, X=rbind(c(0,1,0),c(0,0,1)))
 </code> </code>
 to test the two hypotheses to test the two hypotheses
Line 112: Line 112:
 so this contrast reflects how different systematic allocation is compared to random allocation. Using the ''anova()'' function, we can obtain this contrast with so this contrast reflects how different systematic allocation is compared to random allocation. Using the ''anova()'' function, we can obtain this contrast with
 <code rsplus> <code rsplus>
-anova(res, L=c(0,-1,1))+anova(res, X=c(0,-1,1))
 </code> </code>
 <code output> <code output>
Line 156: Line 156:
 Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1 Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
 </code> </code>
-(I shortened the names of the coefficients in the output above to make the table under the ''Model Results'' more readable). Now the intercept reflects the estimated (average) log risk ratio for random allocation, while the coefficients for ''alternate'' and ''systematic'' are again contrasts of these two levels compared to random allocation. Note how the coefficient for systematic allocation is the same as we obtained earlier using ''anova(res, L=c(0,-1,1))''. Moreover, as we can see in the output, the results for the omnibus test of these two coefficients is identical to what we obtained earlier.+(I shortened the names of the coefficients in the output above to make the table under the ''Model Results'' more readable). Now the intercept reflects the estimated (average) log risk ratio for random allocation, while the coefficients for ''alternate'' and ''systematic'' are again contrasts of these two levels compared to random allocation. Note how the coefficient for systematic allocation is the same as we obtained earlier using ''anova(res, X=c(0,-1,1))''. Moreover, as we can see in the output, the results for the omnibus test of these two coefficients is identical to what we obtained earlier.
  
 ==== Model Without Intercept ==== ==== Model Without Intercept ====
Line 162: Line 162:
 Now we will fit the model, but remove the intercept, which can be done with: Now we will fit the model, but remove the intercept, which can be done with:
 <code rsplus> <code rsplus>
-res <- rma(yi, vi, mods = ~ factor(alloc) - 1, data=dat)+res <- rma(yi, vi, mods = ~ 0 + factor(alloc), data=dat)
 res res
 </code> </code>
 +Alternatively, one could use ''mods = ~ factor(alloc) - 1'' to remove the intercept. In either case, the output is then:
 <code output> <code output>
 Mixed-Effects Model (k = 13; tau^2 estimator: REML) Mixed-Effects Model (k = 13; tau^2 estimator: REML)
Line 206: Line 207:
 Again, we could use the ''anova()'' function to carry out explicitly the same test with: Again, we could use the ''anova()'' function to carry out explicitly the same test with:
 <code rsplus> <code rsplus>
-anova(res, L=rbind(c(1,0,0),c(0,1,0),c(0,0,1)))+anova(res, X=rbind(c(1,0,0),c(0,1,0),c(0,0,1)))
 </code> </code>
 <code output> <code output>
Line 217: Line 218:
    estimate     se    zval   pval    estimate     se    zval   pval
 1:  -0.5180 0.4412 -1.1740 0.2404 1:  -0.5180 0.4412 -1.1740 0.2404
-2:  -0.9658 0.2672 -3.6138 0.0003+2:  -0.9658 0.2672 -3.6138 0.0003 ***
 3:  -0.4289 0.3449 -1.2434 0.2137 3:  -0.4289 0.3449 -1.2434 0.2137
  
Line 226: Line 227:
 It is important to realize that this does not test whether there are differences between the different forms of allocation (this is what we tested earlier in the model that included the intercept term). However, we can again use contrasts of the model coefficients to test differences between the levels. Let's test all pairwise differences (i.e., between random and alternating allocation, between systematic and alternating allocation, and between systematic and random allocation): It is important to realize that this does not test whether there are differences between the different forms of allocation (this is what we tested earlier in the model that included the intercept term). However, we can again use contrasts of the model coefficients to test differences between the levels. Let's test all pairwise differences (i.e., between random and alternating allocation, between systematic and alternating allocation, and between systematic and random allocation):
 <code rsplus> <code rsplus>
-anova(res, L=rbind(c(-1,1,0),c(-1,0,1), c(0,-1,1)))+anova(res, X=rbind(c(-1,1,0),c(-1,0,1), c(0,-1,1)))
 </code> </code>
 <code output> <code output>
-Hypotheses:                                                          +Hypotheses: 
-1:     -factor(alloc)alternate + factor(alloc)random = 0  +1:     -factor(alloc)alternate + factor(alloc)random = 0 
-2: -factor(alloc)alternate + factor(alloc)systematic = 0  +2: -factor(alloc)alternate + factor(alloc)systematic = 0 
-3:    -factor(alloc)random + factor(alloc)systematic = 0 +3:    -factor(alloc)random + factor(alloc)systematic = 0
  
 Results: Results:
-   estimate     se    zval   pval  +   estimate     se    zval   pval 
-1:  -0.4478 0.5158 -0.8682 0.3853  +1:  -0.4478 0.5158 -0.8682 0.3853 
-2:   0.0890 0.5600  0.1590 0.8737 +2:   0.0890 0.5600  0.1590 0.8737
 3:   0.5369 0.4364  1.2303 0.2186 3:   0.5369 0.4364  1.2303 0.2186
 </code> </code>
Line 244: Line 245:
 Note that the output does not contain an omnibus test for the three contrasts because the matrix with the contrast coefficients (''L'' above) is not of full rank (i.e., one of the three contrasts is redundant). If we only include two of the three contrasts (again, it does not matter which two), then we also get the omnibus test (rest of the output omitted): Note that the output does not contain an omnibus test for the three contrasts because the matrix with the contrast coefficients (''L'' above) is not of full rank (i.e., one of the three contrasts is redundant). If we only include two of the three contrasts (again, it does not matter which two), then we also get the omnibus test (rest of the output omitted):
 <code rsplus> <code rsplus>
-anova(res, L=rbind(c(-1,1,0),c(-1,0,1)))+anova(res, X=rbind(c(-1,1,0),c(-1,0,1)))
 </code> </code>
 <code output> <code output>
Line 289: Line 290:
 Now what happens if we remove the intercept? Now what happens if we remove the intercept?
 <code rsplus> <code rsplus>
-res <- rma(yi, vi, mods = ~ ablat - 1, data=dat)+res <- rma(yi, vi, mods = ~ 0 + ablat, data=dat)
 </code> </code>
 <code output> <code output>
tips/models_with_or_without_intercept.txt · Last modified: 2024/06/18 19:29 by Wolfgang Viechtbauer