This post was kindly contributed by SAS and R - go there to comment and to read the full post. |
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 1
Intercept 0
AGE 0
SUBSTANCE heroin -1
SUBSTANCE cocaine 1
SUBSTANCE alcohol 0
This is the result of the e option, and shows that we’ve correctly specified the difference between heroin and cocaine.
Standard
Parameter 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
This post was kindly contributed by SAS and R - go there to comment and to read the full post. |