BookmarkSubscribeRSS Feed
Wafflecakes
Calcite | Level 5

Is anybody here familiar with writing contrast statements for two-way interactions?  I have read the SAS Class, Contrast, and Hazardratio Statements document but im not sure how they came up with the design matrix especially the 1s and 0s. 

 

Variables in the Model

Design Matrix

  

β1

β2

β3

β4

β5

GROUP

SEX

groupA

groupB

sexMale

groupAsexMale

groupBsexMale

A

Male

1

0

1

1

0

A

Female

1

0

0

0

0

B

Male

0

1

1

0

1

B

Female

0

1

0

0

0

C

Male

0

0

1

0

0

C

Female

0

0

0

0

0

 

What are the steps that I would follow to come up with a design matrix for a two-way interaction between two 2-level variables or any combination of variables for that matter?

5 REPLIES 5
Reeza
Super User

I use this paper as my reference each time I need to write estimate/contrast statements:

https://support.sas.com/resources/papers/proceedings11/351-2011.pdf

Wafflecakes
Calcite | Level 5

I am not sure that explains how to write contrast statements clearly at all...

Wafflecakes
Calcite | Level 5

Can someone explain how I would do an interaction between two 2-level factors?

StatsMan
SAS Super FREQ

The paper that Reeza refers to shows you how to write contrasts and estimates in terms of LS-means, using the LSMESTIMATE statement. If you have a very complex contrast, then expressing the contrast in terms of LS-means should be much easier than expressing the contrast in terms of the parameter estimates.

 

The FAQ posted here shows quite a few examples of writing CONTRAST and ESTIMATE statements. These examples walk you through forming the individual pieces of the contrast, in terms of the model parameters, and then forming the complete contrast from those pieces.

 

The most common mistake made in writing ESTIMATE statements involving interaction terms is not involving the main effects.  Many contrasts that difference two levels of an interaction will involve coefficients on the main effects as well.

 

A grad school trick for forming a contrast involving an interaction is to use a table.  Suppose we have a two-level factor, A, and a three-level factor, B. We want to form the contrast between A1B1 and A2B2.  Create a table that has the six cells for that 2x3 interaction and put a 1 in the cell for A1B1 and a -1 in the cell for A2B2:

 

                   B

           1      2      3

     1    1

A

     2           -1

 

Then add across the rows and columns to get the marginal totals for the table:

 

                   B

           1      2      3    Total

     1    1                       1

A

     2           -1              -1

 

Total    1    -1

 

The entries in the body of the table give you the coefficients for the interaction term on the ESTIMATE statement.  The marginal totals of the rows and columns give you the coefficients for the main effects.  For this example, you would have

 

   estimate 'A1B1 vs A2B2' a 1 -1 b 1 -1 0 a*b 1 0 0 0 -1 0 / e;

 

This parameterization assumes GLM coding and that the factor A is listed before the factor B on the CLASS statement.  

 

Another way to build out your estimate statement is to do so in pieces.  For the same example above (again assuming GLM coding), the estimate for A1B1 would involve the first level of A, the first level of B, and the A1B1 interaction level.  That ESTIMATE statement would look like

 

   estimate 'A1B1' int 1 a 1 0 b 1 0 a*b 1 0 0 0 0 0 / e;

 

Now do the same thing for A2B2:

 

   estimate 'A2B2' int 1 a 0 1 b 0 1 a*b 0 0 0 0 1 0 / e;

 

If you want to create the estimate for the difference of those two interaction cells, then difference the elements of the two ESTIMATE statements.  Put minus signs in front of the terms of the second ESTIMATE statement (the one for A2B2).  Combine those with the terms from the first ESTIMATE statement.  The terms for the intercept cancel, you should get A 1 -1 for the terms on A, B 1 -1 0 for the terms on B, and A*B 1 0 0 0 -1 0 for the terms on the interaction.  Combine those on an ESTIMATE statement, and you duplicate the result we see from the table approach above.

 

You can follow this approach for any estimate you need.  The details in that FAQ here should help with more complicated contrasts and estimates.

StatDave
SAS Super FREQ

This note discusses the process for determining the contrast coefficients for a given hypothesis and shows examples involving interactions. However, as noted there, you can often avoid the need to use the CONTRAST statement by instead using the LSMEANS, SLICE, or LSMESTIMATE statements, particularly when interest centers on pairwise comparison of levels. For interaction models, the SLICE statement is especially useful.

 

For the way the design matrix columns are constructed, see the first two sections of the Shared Concepts and Topics chapter of the SAS/STAT User's Guide.

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

What is ANOVA?

ANOVA, or Analysis Of Variance, is used to compare the averages or means of two or more populations to better understand how they differ. Watch this tutorial for more.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 5 replies
  • 3714 views
  • 1 like
  • 4 in conversation