{"id":768,"date":"2019-01-21T12:41:49","date_gmt":"2019-01-21T17:41:49","guid":{"rendered":"http:\/\/www.clayford.net\/statistics\/?p=768"},"modified":"2020-09-13T10:17:54","modified_gmt":"2020-09-13T14:17:54","slug":"most-useful-tests-for-an-ancova-model","status":"publish","type":"post","link":"https:\/\/www.clayford.net\/statistics\/most-useful-tests-for-an-ancova-model\/","title":{"rendered":"Most useful tests for an ANCOVA model"},"content":{"rendered":"<p>In his book <em>Regression Modeling Strategies, 2nd Ed<\/em>, Frank Harrell provides a list of what he calls the \u201cmost useful tests\u201d for a 2-level factor \\(\\times\\) numeric model (Table 2.2, p. 19). This is often called an Analysis of Covariance, or ANCOVA. The basic idea is we have a numeric response with substantial variability and we seek to understand the variability by modeling the mean of the response as a function of a categorical variable and a numeric variable.<\/p>\n<p>Let&#8217;s simulate some data for such a model and then see how we can use R to carry out these tests.<\/p>\n<pre><code class=\"r\">n &lt;- 400\nset.seed(1)\nsex &lt;- factor(sample(x = c(\"f\", \"m\"), size = n, replace = TRUE))\nage &lt;- round(runif(n = n, min = 18, max = 65))\ny &lt;- 1 + 0.8*age + 0.4*(sex == \"m\") - 0.7*age*(sex == \"m\") + rnorm(n, mean = 0, sd = 8)\ndat &lt;- data.frame(y, age, sex)\n<\/code><\/pre>\n<p>The data contain a numeric response, <code>y<\/code>, that is a function of <code>age<\/code> and <code>sex<\/code>. I set the \u201ctrue\u201d coefficient values to 1, 0.8, 0.4, and -0.7. They correspond to \\(\\beta_0\\) through \\(\\beta_3\\) in the following model:<\/p>\n<p>\\[y = \\beta_0 + \\beta_1 age + \\beta_2 sex + \\beta_3 age \\times sex\\]<\/p>\n<p>In addition the error component is a Normal distribution with a standard deviation of 8.<\/p>\n<p>Now let&#8217;s model the data and see how close we get to recovering the true parameter values.<\/p>\n<pre><code class=\"r\">mod &lt;- lm(y ~ age * sex, dat)\nsummary(mod)\n<\/code><\/pre>\n<pre>## \n## Call:\n## lm(formula = y ~ age * sex, data = dat)\n## \n## Residuals:\n##      Min       1Q   Median       3Q      Max \n## -23.8986  -5.8552  -0.2503   6.0507  30.6188 \n## \n## Coefficients:\n##             Estimate Std. Error t value Pr(&gt;|t|)    \n## (Intercept)  0.27268    1.93776   0.141    0.888    \n## age          0.79781    0.04316  18.484   &lt;2e-16 ***\n## sexm         2.07143    2.84931   0.727    0.468    \n## age:sexm    -0.72702    0.06462 -11.251   &lt;2e-16 ***\n## ---\n## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1\n## \n## Residual standard error: 8.661 on 396 degrees of freedom\n## Multiple R-squared:  0.7874, Adjusted R-squared:  0.7858 \n## F-statistic:   489 on 3 and 396 DF,  p-value: &lt; 2.2e-16\n<\/pre>\n<p>While the coefficient estimates for age and the age \\(\\times\\) sex interaction are pretty close to the true values, the same cannot be said for the intercept and sex coefficients. The residual standard error of 8.661 is close to the true value of 8.<\/p>\n<p>We can see in the summary output of the model that four hypothesis tests, one for each coefficient, are carried out for us. Each are testing if the coefficient is equal to 0. Of those four, only one qualifies as one of the most useful tests: the last one for <code>age:sexm<\/code>. This tests if the effect of age is independent of sex and vice versa. Stated two other ways, it tests if age and sex are additive, or if the age effect is the same for both sexes. To get a better understanding of what we&#8217;re testing, let&#8217;s plot the data with fitted age slopes for each sex.<\/p>\n<pre><code class=\"r\">library(ggplot2)\nggplot(dat, aes(x = age, y = y, color = sex)) +\n  geom_point() +\n  geom_smooth(method=\"lm\")\n<\/code><\/pre>\n<p><img decoding=\"async\" src=\"data:image\/png;base64,iVBORw0KGgoAAAANSUhEUgAAAfgAAAH4CAMAAACR9g9NAAAA1VBMVEUAAAAAADoAAGYAOpAAZmYAZrYAv8QzMzM6AAA6ADo6OpA6ZmY6kNs9sLNNTU1NTW5NTY5NbqtNjshmAABmADpmAGZmZgBmtv9uTU1uTY5ubqtuq6tuq+SOTU2OTY6ObquOjk2OjsiOq+SOyP+QOgCQOjqQkDqQ2\/+rbk2rbo6r5P+2ZgC2tma2\/\/\/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+6jrh7il4Lp1w9xQ8l064ewqeSyfcPQXPpRPunoLn0gl3T8Fz6YS7p+C5dMLdU\/BcOuHuKXgunXD3FDyXTrh7Cp5LJ9w9Bc+lE+6egg\/pUl8BIewyEDoFP7TkNwHIugyMTsEPTcEvFLyG+qWCT9UJd0\/Bc+mEu6fguXTC3VPwXDrh7il4Lp1w9xQ8l064ewqeSyfcPQXPpRPunoLn0gl3T8Fz6YS7p+C5dMLdU\/BcOuHuKXgunXD3FDyXTrh7Cp5LJ9w9Bc+lE+6egufSCXdPwXPphLun4Ll0wt0rDj5sZxSFTKc84e7hTMFXL07BT6U84e7hTN9CtVBT8As1Bb9QU\/ALtVzwdz+e\/\/n1l1\/PH5F4s9nsiyIsb7O5\/dsHuvK2JX37gvZyvyN0L8Jywb+72Lx7evN08+qCxJ3NtqjfXxCWt721332gK+\/u583eRyr3drfvgvJy0UYQ6m8u3r\/Y3Qwi+\/I\/rynLe\/dfP32gK+\/2h20LJXTvX\/+gLQ9v+eDvfvrwO6Hndz8+2hCWd\/u3\/6f0b1vM7S+E7m1b+s1TytuHtmzwd39\/vaH9yt48JSzv3fn5OWV5xO796zVteXjLBX\/719fUnd4N5ZhhH5Hoytu5R9knU5eHt1zwr3YtSvQwdwuedFRPOumY7qhebaKm4BdqCn6hpuAXagp+oabgF2oKfqGm4BdqSwH\/5uTk\/mZzfXLvv59s\/7\/7+8JtIeA\/\/untp+9\/+\/Sfzz89frL9++blk9oe1baFgN98\/Obkq+cH5NsGf3LyoLZDtW0h4K\/v7Zr7EbzG+c1ywN\/fXH\/1\/Bjq\/\/j88zMN9bUdKGOfHp\/84fFuWHfvLzq429tCwHf2+Z\/Pa7sgwxYFftvudVR3tEWBV+tNwS\/UFPxCTcEv1BT8Qu3feHQIamWMLl0AAAAASUVORK5CYII=\" alt=\"plot of chunk unnamed-chunk-3\"><\/p>\n<p>Visually it appears the effect of age is <em>not<\/em> independent of sex. It seems more pronounced for females. Is this effect real or maybe due to chance? The hypothesis test in the summary output for <code>age:sexm<\/code> evaluates this. Obviously the effect seems very real. We are not likely to see such a difference in slopes this large if there truly was no difference. It does appear the effect of age is different for each sex. The estimate of -0.72 estimates the difference in slopes (or age effect) for the males and females.<\/p>\n<p>The other three hypothesis tests are not very useful.<\/p>\n<ul>\n<li>Testing if the <code>Intercept<\/code> is 0 is testing whether <code>y<\/code> is 0 for females at age 0.<\/li>\n<li>Testing if <code>age<\/code> is 0 is testing whether <code>age<\/code> is associated with <code>y<\/code> for males.<\/li>\n<li>Testing if <code>sexm<\/code> is 0 is testing whether <code>sex<\/code> is associated with <code>y<\/code> for subjects at age 0.<\/li>\n<\/ul>\n<p>Other more useful tests, as Harrell outlines in Table 2.2, are as follows:<\/p>\n<ul>\n<li>Is <code>age<\/code> associated with <code>y<\/code>?<\/li>\n<li>Is <code>sex<\/code> associated with <code>y<\/code>?<\/li>\n<li>Are either <code>age<\/code> or <code>sex<\/code> associated with <code>y<\/code>?<\/li>\n<\/ul>\n<p>The last one is answered in the model output. That&#8217;s the F-statistic in the last line. It tests whether all coefficients (except the intercept) are equal to 0. The result of this test is conclusive. At least one of the coeffcients is not 0.<\/p>\n<p>To test if <code>age<\/code> is associated with <code>y<\/code>, we need to test if both the <code>age<\/code> and <code>age:sexm<\/code> coefficents are equal to 0. The <code>car<\/code> package by John Fox provides a nice function for this purpose called <code>linearHypothesis<\/code>. It takes at least two arguments. The first is the fitted model object and the second is a vector of hypothesis tests. Below we specify we want to test if \u201cage = 0\u201d and \u201cage:sexm = 0\u201d<\/p>\n<pre><code class=\"r\">library(car)\nlinearHypothesis(mod, c(\"age = 0\", \"age:sexm = 0\"))\n<\/code><\/pre>\n<pre>## Linear hypothesis test\n## \n## Hypothesis:\n## age = 0\n## age:sexm = 0\n## \n## Model 1: restricted model\n## Model 2: y ~ age * sex\n## \n##   Res.Df   RSS Df Sum of Sq      F    Pr(&gt;F)    \n## 1    398 55494                                  \n## 2    396 29704  2     25790 171.91 &lt; 2.2e-16 ***\n## ---\n## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1\n<\/pre>\n<p>The result is once again conclusive. The p-value is virtually 0. It does indeed appear that age is associated with <code>y<\/code>.<\/p>\n<p>Likewise, to test if <code>sex<\/code> is associated with <code>y<\/code>, we need to test if both the <code>sex<\/code> and <code>age:sexm<\/code> coefficents are equal to 0.<\/p>\n<pre><code class=\"r\">linearHypothesis(mod, c(\"sexm = 0\", \"age:sexm = 0\"))\n<\/code><\/pre>\n<pre>## Linear hypothesis test\n## \n## Hypothesis:\n## sexm = 0\n## age:sexm = 0\n## \n## Model 1: restricted model\n## Model 2: y ~ age * sex\n## \n##   Res.Df    RSS Df Sum of Sq     F    Pr(&gt;F)    \n## 1    398 119354                                 \n## 2    396  29704  2     89651 597.6 &lt; 2.2e-16 ***\n## ---\n## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1\n<\/pre>\n<p>As expected this test confirms that <code>sex<\/code> is associated with <code>y<\/code>, just as we specified when we simulated the data.<\/p>\n<p>Now that we have established that <code>age<\/code> is associated with <code>y<\/code>, and that the association differs for each <code>sex<\/code>, what exactly is that association for each sex? In other words what are the slopes of the lines in our plot above?<\/p>\n<p>We can sort of answer that with the model coefficients.<\/p>\n<pre><code class=\"r\">round(coef(mod),3)\n<\/code><\/pre>\n<pre>## (Intercept)         age        sexm    age:sexm \n##       0.273       0.798       2.071      -0.727\n<\/pre>\n<p>That corresponds to the following model:<\/p>\n<p>\\[y = 0.273 + 0.799 age + 2.071 sex &#8211; 0.727 age \\times sex\\]<\/p>\n<p>When <code>sex<\/code> is female, the fitted model is<\/p>\n<p>\\[y = 0.273 + 0.799 age \\]<\/p>\n<p>This says the slope of the <code>age<\/code> is about 0.8 when <code>sex<\/code> is female.<\/p>\n<p>When <code>sex<\/code> is male, the fitted model is<\/p>\n<p>\\[y = (0.273 + 2.071) + (0.797 &#8211; 0.727) age \\]<br \/>\n\\[y = 2.344 + 0.07 age \\]<\/p>\n<p>This says the slope of the <code>age<\/code> is about 0.07 when <code>sex<\/code> is male.<\/p>\n<p>How certain are we about these estimates? That&#8217;s what standard error is for. For the age slope estimate for females the standard error is provided in the model output for the <code>age<\/code> coefficient. It shows about 0.04. Adding and subtracting 2 \\(\\times\\) 0.04 to the coefficient gives us a rough 95% confidence interval. Or we could just use the <code>confint<\/code> function:<\/p>\n<pre><code class=\"r\">confint(mod, parm = \"age\")\n<\/code><\/pre>\n<pre>##         2.5 %    97.5 %\n## age 0.7129564 0.8826672\n<\/pre>\n<p>The standard error of the age slope estimate for males takes a little more work. Another <code>car<\/code> function useful for this is the <code>deltaMethod<\/code> function. It takes at least three arguments: the model object, the quantity expressed as a character phrase that we wish to estimate a standard error for, and the names of the parameters. The function then calculates the standard error using the <em>delta method<\/em>. Here&#8217;s one way to do it for our model<\/p>\n<pre><code class=\"r\">deltaMethod(mod, \"b1 + b3\", parameterNames = paste0(\"b\", 0:3)) \n<\/code><\/pre>\n<pre>##           Estimate         SE       2.5 %    97.5 %\n## b1 + b3 0.07079277 0.04808754 -0.02345709 0.1650426\n<\/pre>\n<p>The standard error is similar in magnitude, but since our estimate is so small the resulting confidence interval overlaps 0. This tells us the effect of age on males is too small for our data to determine if the effect is positive or negative.<\/p>\n<p>Another way to get the estimated age slopes for each sex, along with standard errors and confidence intervals, is to use the <code>margins<\/code> package. We use the <code>margins<\/code> function with our model object and specify that we want to estimate the marginal effect of <code>age<\/code> at each level of <code>sex<\/code>. (\u201cmarginal effect of <code>age<\/code>\u201d is another way of saying the effect of age at each level of <code>sex<\/code>)<\/p>\n<pre><code class=\"r\">library(margins)\nmargins(mod, variables = \"age\", at = list(sex = c(\"f\", \"m\")))\n<\/code><\/pre>\n<pre>## Average marginal effects at specified values\n<\/pre>\n<pre>## lm(formula = y ~ age * sex, data = dat)\n<\/pre>\n<pre>##  at(sex)     age\n##        f 0.79781\n##        m 0.07079\n<\/pre>\n<p>This does the formula work we did above. It plugs in <code>sex<\/code> and returns the estmimated slope coefficient for <code>age<\/code>. If we wrap the call in <code>summary<\/code> we get the standard errors and confidence intervals.<\/p>\n<pre><code class=\"r\">summary(margins(mod, variables = \"age\", at = list(sex = c(\"f\", \"m\"))))\n<\/code><\/pre>\n<pre>##  factor    sex    AME     SE       z      p   lower  upper\n##     age 1.0000 0.7978 0.0432 18.4841 0.0000  0.7132 0.8824\n##     age 2.0000 0.0708 0.0481  1.4722 0.1410 -0.0235 0.1650\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>In his book Regression Modeling Strategies, 2nd Ed, Frank Harrell provides a list of what he calls the \u201cmost useful&#8230; <a class=\"read-more\" href=\"https:\/\/www.clayford.net\/statistics\/most-useful-tests-for-an-ancova-model\/\">Read more<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[22,12,8,13],"tags":[69,71,70,73,72,66],"class_list":["post-768","post","type-post","status-publish","format-standard","hentry","category-hypothesis-testing","category-regression","category-simulation","category-using-r","tag-ancova","tag-car","tag-harrell","tag-linear-modeling","tag-margins","tag-simulation"],"_links":{"self":[{"href":"https:\/\/www.clayford.net\/statistics\/wp-json\/wp\/v2\/posts\/768","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.clayford.net\/statistics\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.clayford.net\/statistics\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.clayford.net\/statistics\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.clayford.net\/statistics\/wp-json\/wp\/v2\/comments?post=768"}],"version-history":[{"count":2,"href":"https:\/\/www.clayford.net\/statistics\/wp-json\/wp\/v2\/posts\/768\/revisions"}],"predecessor-version":[{"id":770,"href":"https:\/\/www.clayford.net\/statistics\/wp-json\/wp\/v2\/posts\/768\/revisions\/770"}],"wp:attachment":[{"href":"https:\/\/www.clayford.net\/statistics\/wp-json\/wp\/v2\/media?parent=768"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.clayford.net\/statistics\/wp-json\/wp\/v2\/categories?post=768"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.clayford.net\/statistics\/wp-json\/wp\/v2\/tags?post=768"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}