Auto-suggest helps you quickly narrow down your search results by suggesting possible matches as you type.

Showing results for

- Home
- /
- Analytics
- /
- Stat Procs
- /
- Re: How to calculate difference of means with interaction term using l...

Options

- RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Mute
- Printer Friendly Page

🔒 This topic is **solved** and **locked**.
Need further help from the community? Please
sign in and ask a **new** question.

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

Posted 12-01-2019 06:16 PM
(2012 views)

Hi, I have a this program to calculate the hospital use according to the number of disease and the age. The number of disease has been coded into 5 categories (0 to 4) and the age also has 5 categories (1 (youngest) to 5(oldest)). We also added an interaction term between age and number of disease. I'm trying to calculate the difference between the oldest and the youngest group for each category of disease. I know we van use the lsmestimate procedure but I'm having a hard time to figure out where to place the zeros and ones, etc. This is my code :

Thank you

PROC LOGISTIC desc DATA=exemple;

Class disease (ref='0') age (ref='1')/ param=glm;

Model hospital = disease age disease*age;

lsmeans disease age disease*age/ diff ilink cl e exp;

lsmestimate 'diff age 5 - age 1 for disease = 0' 0 0 0 0 0 1 0 0 0 -1/ ilink cl e exp;

lsmestimate 'diff age 5 - age 1 for disease = 1' 0 1 0 0 0 1 0 0 0 -1/ ilink cl e exp;

lsmestimate 'diff age 5 - age 1 for disease = 2' 0 0 1 0 0 1 0 0 0 -1/ilink cl e exp;

lsmestimate 'diff age 5 - age 1 for disease = 3' 0 0 0 1 0 1 0 0 0 -1/ilink cl e exp;

lsmestimate 'diff age 5 - age 1 for disease = 4' 0 0 0 0 1 1 0 0 0 -1/ilink cl e exp;

RUN;

1 ACCEPTED SOLUTION

Accepted Solutions

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

First, if there are five levels of both variables then there will be 25 mean estimates from the LSMEANS statement. If you want to compare two particular means, look at the table presented by the LSMEANS statement and specify 25 coefficients matching the order of means in that table in your LSMESTIMATE statement. For instance, if you want to compare the first two means (first - second), then specify coefficients 1 -1 followed by 23 zeros (though the trailing zeros can be omitted).

However, if you want an estimate of the difference in means, that cannot be provided by the LSMESTIMATE statement. You can see that in the results of the statements you showed by noting that the difference in two means from the LSMEANS statement (the values in the "Mean" column) is not the same as the value (either in the "Estimate" or "Mean" column) from the LSMESTIMATE statement. The Estimate value is the difference in log odds (not means) and the Mean value is nothing useful (the inverse logit of the difference in log odds). To estimate the difference in means, use the Margins macro to both fit the model and provide the estimate.

` %Margins(data = exemple,`

response = hospital,

class = disease age,

model = disease age disease*age,

dist = binomial,

margins = age,

margwhere= age in (1 5),

at = disease,

options = diff cl)

6 REPLIES 6

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

This is exactly what the SLICE command in PROC LOGISTIC does.

An example of using SLICE is given here (scroll down)

Additionally, you wouldn't normally use LSMESTIMATE or ESTIMATE if all you are doing is comparing one level to another level. Usually, the only time you need LSMESTIMATE or ESTIMATE is if you, for some reason, want to create estimates where the comparisons are not one level to another, for example, if you wanted –1*(level 1)+2*(level 2)–1*(level 3).

--

Paige Miller

Paige Miller

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

Check the syntax for the LSMESTIMATE statement

LSMESTIMATE fixed-effect <'label'> values <divisor=> <, <'label'> values <divisor=>> <, ...>< / options> ;

You will see that your statement fails to include a fixed-effect.

This paper by Kiernan et al. contains a lot of information that you will probably find useful

CONTRAST and ESTIMATE Statements Made Easy: The LSMESTIMATE Statement

In particular, for a contrast involving a 5x5 interaction I would use "non-positional syntax" (p 5 in the paper); it's a lot easier than trying to figure out where to put a "1" and a "-1" and 23 zeros in a set of 25 coefficients.

I hope this helps.

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

First, if there are five levels of both variables then there will be 25 mean estimates from the LSMEANS statement. If you want to compare two particular means, look at the table presented by the LSMEANS statement and specify 25 coefficients matching the order of means in that table in your LSMESTIMATE statement. For instance, if you want to compare the first two means (first - second), then specify coefficients 1 -1 followed by 23 zeros (though the trailing zeros can be omitted).

However, if you want an estimate of the difference in means, that cannot be provided by the LSMESTIMATE statement. You can see that in the results of the statements you showed by noting that the difference in two means from the LSMEANS statement (the values in the "Mean" column) is not the same as the value (either in the "Estimate" or "Mean" column) from the LSMESTIMATE statement. The Estimate value is the difference in log odds (not means) and the Mean value is nothing useful (the inverse logit of the difference in log odds). To estimate the difference in means, use the Margins macro to both fit the model and provide the estimate.

` %Margins(data = exemple,`

response = hospital,

class = disease age,

model = disease age disease*age,

dist = binomial,

margins = age,

margwhere= age in (1 5),

at = disease,

options = diff cl)

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

*mixed* model. (Unfortunately, most of those I work with are mixed.)

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

Thank you @StatDave, and everyone else for the answers. This solution is what i was looking for. However, I am trying to use the Margins macro in sas and it is not working. I entered the code provided and added other covariables that i wanted to adujst for and I got this warning message for both of my main variables. I downloaded the macro from SAS, so i don't know if it ever happened to someone and what they've done to fix it.

WARNING: No variables found beginning with 'disease_' in data set WORK._MODDAT.

WARNING: No variables found beginning with 'age_' in data set WORK._MODDAT.

Thank you

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

Please show the exact Margins macro call that caused those errors.

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

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.