Turn on suggestions

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

Showing results for

- Home
- /
- Analytics
- /
- Stat Procs
- /
- Trying to recreate linear mixed models and corresponding graphs in SAS

Options

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

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

Posted 07-11-2024 09:31 AM
(967 views)

Hello SAS community!

I am very new to SAS and I have used R to run some linear mixed models. I now have to run the exact same in SAS but I have a few questions.

*For context, I am looking at interest variables to see the level of interest in using herbs and spices (interest1 variable) before and after (time variable) watching short and long (randomization variable) nutrition videos.*

1) After running this model in R using the code below, I tried to run it in SAS. I am not sure if this code is right, but the p values aren't the same (in R and in SAS) and some significant variables in R are not significant in SAS. Does anyone have recommendations on how to correct my code in SAS so I am able to get the same outputs in both softwares?

**proc mixed data=results.cormick;****class record_id education time income randomization;****model interest1=time randomization age education sex income time*randomization /solution;****random record_id;****repeated /subject=record_id type=un;****run;**

2) Also, I am not sure which p-values I should be reporting from these two tables. Does anyone have recommendations?

3) I am unable to get these graphs in SAS, does anyone know what code I could use?

Thanks so much!

16 REPLIES 16

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

Check @Rick_SAS blogs:

https://blogs.sas.com/content/iml/2016/06/22/sas-effectplot-statement.html

```
proc mixed data=sashelp.heart(obs=1000);
class bp_status sex status;
model weight=height sex status/s;
random int/subject=bp_status;
/*repeated /subject=bp_status type=un;*/
store out=mixedmodel;
run;
proc plm source=mixedmodel;
effectplot INTERACTION(x=sex sliceby=status /*plotby=CigsPerDay*/ )/limits connect;
run;
```

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

Look at the R table of parameter estimates. The R model appears to treat all variables as continuous, whereas in SAS you are treating many variables as factors. Resolve that issue first or you'll never get the same estimates.

Also, I suspect you want to add Sex to the CLASS statement so that the Sex variable is treated as a categorical variable.

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

Hi Rick,

Thanks for the advice. When you say treat the variables as continuous in SAS, do you mean input them all in the model statement even if I inputted them in the class statement?

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

@gdr1 wrote:

Hi Rick,

Thanks for the advice. When you say treat the variables as continuous in SAS, do you mean input them all in the model statement even if I inputted them in the class statement?

It means: put the continuous variables in the model statement and NOT in the class statement.

--

Paige Miller

Paige Miller

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

Thank you! It worked.

I am still unable to get the graphs. Do you have any recommendations on how to get the graphs? When I run the bottom code, I get the fixed effects but then the plm procedure doesn't work. "time" and "randomization" are not continuous variables.

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

I think you want to use CONTOURPLOT instead of INTERACTION

--

Paige Miller

Paige Miller

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

I think @Rick_SAS is saying you need to specify the same model in both R and SAS. As stated by @PaigeMiller, one way to do that is to treat all of the predictors in your SAS model as continuous by specifying them in the model statement and not the class statement.

The other way is to treat the class variables from the SAS model as factors in your R model. That is:

m1 <- lmer(interest1 ~ time*randomization + age + factor(education) + sex + factor(income) + (1|record_id)

The choice will depend on how education and income are coded, and whether it is reasonable to assume a linear relationship between those variables and the model outcome. Treating those variables as categorical does not require that kind of assumption but will usually decrease the power of statistical tests (i.e., you will get higher p-values) compared to treating those variables as continuous.

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

Thank you! I added the class statement back in and it makes more sense now since education, sex and income are categorical variables.

I am still unable to get the graphs. Currently getting this error.

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

Use CONTOUR (you have a typo)

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

Thanks Rick.

Still getting an error after I run this:

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

There is no SLICEBY= for contour plots. You need a Y= option however.

Or maybe the whole idea of a CONTOUR plot is going down the wrong path. Somehow, this won't get the plots you showed from R. So tell us, using the SAS variable names, what plots you want, what is on the x- axis, what is on the y-axis, and what lines or curves should be drawn on the plot?

Also, what is wrong with the solution from @Ksharp ??

--

Paige Miller

Paige Miller

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

When trying ksharps solution, I get the below error:

My goal is to create the below plot:

where randomization = short or long videos, time = pre or post, and the lines across the graph are the mean values of the variable of interest (interest1)

So far I used the code below :

and got the fixed effect model results:

I am just trying to create the graphs now . Thanks!

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

This is the source of all of your problems in this thread. You need to put RANDOMIZATION into the CLASS statement of PROC MIXED, and also into the model statement.

--

Paige Miller

Paige Miller

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

Unfortunately, I am getting this Java error now.

Also, can proc gplot be used instead of proc plm?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. **Registration is now open through August 30th**. Visit the SAS Hackathon homepage.

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.