# [R] Test individual slope for each factor level in ANCOVA

li li hannah.hlx at gmail.com
Thu Mar 16 17:40:10 CET 2017

```Hi John. Thanks much for your help. It is great to know this.
Hanna

2017-03-16 8:02 GMT-04:00 Fox, John <jfox at mcmaster.ca>:

> Dear Hanna,
>
> You can test the slope in each non-reference group as a linear hypothesis.
> You didn’t make the data available for your example, so here’s an example
> using the linearHypothesis() function in the car package with the Moore
> data set in the same package:
>
> - - - snip - - -
>
> > library(car)
> > mod <- lm(conformity ~ fscore*partner.status, data=Moore)
> > summary(mod)
>
> Call:
> lm(formula = conformity ~ fscore * partner.status, data = Moore)
>
> Residuals:
>     Min      1Q  Median      3Q     Max
> -7.5296 -2.5984 -0.4473  2.0994 12.4704
>
> Coefficients:
>                           Estimate Std. Error t value Pr(>|t|)
> (Intercept)               20.79348    3.26273   6.373 1.27e-07 ***
> fscore                    -0.15110    0.07171  -2.107  0.04127 *
> partner.statuslow        -15.53408    4.40045  -3.530  0.00104 **
> fscore:partner.statuslow   0.26110    0.09700   2.692  0.01024 *
> ---
> Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
>
> Residual standard error: 4.562 on 41 degrees of freedom
> Multiple R-squared:  0.2942,    Adjusted R-squared:  0.2426
> F-statistic: 5.698 on 3 and 41 DF,  p-value: 0.002347
>
> > linearHypothesis(mod, "fscore + fscore:partner.statuslow")
> Linear hypothesis test
>
> Hypothesis:
> fscore  + fscore:partner.statuslow = 0
>
> Model 1: restricted model
> Model 2: conformity ~ fscore * partner.status
>
>   Res.Df    RSS Df Sum of Sq      F  Pr(>F)
> 1     42 912.45
> 2     41 853.42  1    59.037 2.8363 0.09976 .
> ---
> Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
>
> - - - snip - - -
>
> In this case, there are just two levels for partner.status, but for a
> multi-level factor you can simply perform more than one test.
>
>
> I hope this helps,
>
>  John
>
> -------------------------------------
> John Fox, Professor
> McMaster University
> Web: http://socserv.mcmaster.ca/jfox/
>
>
>
>
> On 2017-03-15, 9:43 PM, "R-help on behalf of li li"
> <r-help-bounces at r-project.org on behalf of hannah.hlx at gmail.com> wrote:
>
> >Hi all,
> >   Consider the data set where there are a continuous response variable, a
> >continuous predictor "weeks" and a categorical variable "region" with five
> >levels "a", "b", "c",
> >"d", "e".
> >  I fit the ANCOVA model as follows. Here the reference level is region
> >"a"
> >and there are 4 dummy variables. The interaction terms (in red below)
> >represent the slope
> >difference between each region and  the baseline region "a" and the
> >corresponding p-value is for testing whether this slope difference is
> >zero.
> >Is there a way to directly test whether the slope corresponding to each
> >individual factor level is 0 or not, instead of testing the slope
> >difference from the baseline level?
> >  Thanks very much.
> >      Hanna
> >
> >
> >
> >
> >
> >
> >> mod <- lm(response ~ weeks*region,data)> summary(mod)
> >Call:
> >lm(formula = response ~ weeks * region, data = data)
> >
> >Residuals:
> >     Min       1Q   Median       3Q      Max
> >-0.19228 -0.07433 -0.01283  0.04439  0.24544
> >
> >Coefficients:
> >                Estimate Std. Error t value Pr(>|t|)
> >(Intercept)    1.2105556  0.0954567  12.682  1.2e-14 ***
> >weeks         -0.0213333  0.0147293  -1.448    0.156
> >regionb       -0.0257778  0.1349962  -0.191    0.850
> >regionc       -0.0344444  0.1349962  -0.255    0.800
> >regiond       -0.0754444  0.1349962  -0.559    0.580
> >regione       -0.1482222  0.1349962  -1.098    0.280    weeks:regionb
> >-0.0007222  0.0208304  -0.035    0.973
> >weeks:regionc -0.0017778  0.0208304  -0.085    0.932
> >weeks:regiond  0.0030000  0.0208304   0.144    0.886
> >weeks:regione  0.0301667  0.0208304   1.448    0.156    ---
> >Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
> >
> >Residual standard error: 0.1082 on 35 degrees of freedom
> >Multiple R-squared:  0.2678,   Adjusted R-squared:  0.07946
> >F-statistic: 1.422 on 9 and 35 DF,  p-value: 0.2165
> >
> >       [[alternative HTML version deleted]]
> >
> >______________________________________________
> >R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
> >https://stat.ethz.ch/mailman/listinfo/r-help