Tuesday, October 12, 2010

Example 8.9: Contrasts

In example 8.6 we showed how to change the reference category. This is the natural first thought analysts have when their primary comparisons aren't represented in the default output. But our interest might center on a number of comparisons which don't share a category. Or we might need to compare one group with the mean of the other groups. Today we'll explore tests and estimates of effects like these, which are sometimes called contrasts. In a later entry we'll explore more complex multivariate contrasts.

SAS

Access to this kind of comparison in SAS is provided in many model-fitting procedures using a test, estimate, or contrast statement. The differences among these can be subtle. In general, for simple comparisons, we recommend the estimate statement, where available. It is available for the important glm and genmod procedures, among others. In our example from linear regression, we changed the referent from heroin to alcohol by sorting the data and using the order=data option. This gains us pairwise comparisons between heroin and alcohol and between cocaine and alcohol. Here we show how to get the comparison between heroin and cocaine from the same model fit.
`proc sort data=help_a; by descending substance; run;proc glm data=help_a order=data;   class substance;   model i1 = age substance / solution;   estimate "heroin vs. cocaine" substance -1 1 0 / e;run; quit;`

The syntax of the estimate statement is to optionally name the estimate (between the quotes) then to list the effects whose values we want to assess, followed by the desired values for each level of the effect. The e option requests that the contrast vector be printed-- this is a vital check that the contrast is working as desired. Here are the pieces of output generated by the estimate statement.
`Coefficients for Estimate heroin vs. cocaine                            Row 1Intercept                       0AGE                             0SUBSTANCE heroin               -1SUBSTANCE cocaine               1SUBSTANCE alcohol               0`

This is the result of the e option, and shows that we've correctly specified the difference between heroin and cocaine.
`                                            StandardParameter                   Estimate           Error    t Value    Pr > |t|heroin vs. cocaine        3.00540049      2.15576257       1.39      0.1640`

This is the comparison we want. It displays the difference, which we could confirm by examining the parameter estimates, as well as the standard error of the difference and the p-value, which can't be determined from the standard output.

To compare alcohol with the average of heroin and cocaine, we could use the following syntax (results omitted).
`   estimate "cocaine + alcohol vs heroin" substance -2 1 1 /e divisor=2;`

The divisor option allows us to use portions that are not easily represented as decimals. It's necessary because of the reserved use of the / in SAS. Any number of estimate statements can be issued in a single proc.

R

The fit.contrast() function in the gmodels package will generate contrasts for lm and glm objects. Multivariate contrasts can be found in the contrast() function in the Design package.
`ds = read.csv("http://www.math.smith.edu/r/data/help.csv")lm3 = lm(i1 ~ substance + age, data=ds))library(gmodels)fit.contrast(lm3, "substance", c(0,-1, 1))`

This generates the following output:
`                         Estimate Std. Error t value Pr(>|t|)substance c=( 0 -1 1 )  -3.005400 2.155763 -1.394124 0.1639696`

The simple syntax accepts model objects, the name of the effect, and a vector of contrasts to apply. The function does not replicate the contrast, which would be useful, but it is simple enough to check the parameter estimates from the model to ensure the desired result has been obtained.

The syntax for comparing heroin with the mean of alcohol and cocaine is straightforward.
`> fit.contrast(lm3,"substance", c(-.5, -.5,1))                             Estimate Std. Error t value Pr(>|t|)substance c=( -0.5 -0.5 1 ) -11.09965 1.904192 -5.829059 1.065763e-08 `

Anonymous said...

thanks!

Kriss Harris said...

This is fantastic! Just letting you know that SAS can use the estimate statement to do contrasts on interactions. This is not that counter intuitive but I do have the code.

Craig said...

But how do we get gmodels to calculate more complex contrasts in multifactor models? For Example, from the SAS GLM manual, example 32.5:

data muscles;
do Rep=1 to 2;
do Time=1 to 4;
do Current=1 to 4;
do Number=1 to 3;
input MuscleWeight @@;
output;
end;
end;
end;
end;
datalines;
72 74 69 61 61 65 62 65 70 85 76 61
67 52 62 60 55 59 64 65 64 67 72 60
57 66 72 72 43 43 63 66 72 56 75 92
57 56 78 60 63 58 61 79 68 73 86 71
46 74 58 60 64 52 71 64 71 53 65 66
44 58 54 57 55 51 62 61 79 60 78 82
53 50 61 56 57 56 56 56 71 56 58 69
46 55 64 56 55 57 64 66 62 59 58 88
;

proc glm outstat=summary;
class Rep Current Time Number;
model MuscleWeight = Rep Current|Time|Number;
contrast 'Time in Current 3'
Time 1 0 0 -1 Current*Time 0 0 0 0 0 0 0 0 1 0 0 -1,
Time 0 1 0 -1 Current*Time 0 0 0 0 0 0 0 0 0 1 0 -1,
Time 0 0 1 -1 Current*Time 0 0 0 0 0 0 0 0 0 0 1 -1;
contrast 'Current 1 versus 2' Current 1 -1;
lsmeans Current*Time / slice=Current;
run;

Jeff said...

thanks for this helpful notes.

Harrell's rms package has some nice capabilities for writing contrasts.

In SAS I would recommend looking at the LSMestimate statement for comparisons, particularly for complex contrasts involving interactions. It can be considerably easier to write a contrast in lsmean space that parameter space.

Nick Horton said...

Just to reiterate: Frank Harrell's rms package (Regression Modeling Strategies) is a wonderful resource for contrasts and many other real-world analyses.