turn on suggestions

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

Showing results for

Find a Community

- Home
- /
- SAS Programming
- /
- Base SAS Programming
- /
- SAS predicted probabilities vs. hand calculation

Topic Options

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

- Mark as New
- Bookmark
- Subscribe
- RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

02-13-2018 05:46 PM - edited 02-13-2018 05:51 PM

Hi,

I have a follow up question to the original posted earlier today (listed below in bold).

How come there are slight differences in the predicted probabilities between those calculated by SAS:

proc logistic data=temp desc;

class EDU (ref='0') SEX (REF='0') MRT(REF='0');

model outcome=AGE SEX EDU MRT;

output out=pp pred=pred;

run;

and using the standard formula:

pred=1/(1+exp(-1*(Intercept+ (AGE_P*AGE)+(SEX_P*SEX)+(EDU_P*EDU)+(MRT_P*MRT);

Assuming Intercept, AGE_P, SEX_P, EDU_P, AND MRT_P are the coefficients provided by SAS, multiplied by the actual values per person.

The min. difference between the predicted probabilities produced by SAS and by hand is .00003, the max, .0815, and the mean .0219.

Thanks!

Emily

**Hello,**

** **

**I am trying to use the %str function to modify my code. Here it is:**

** **

**%macro predictor;**

** **

**data new;**

**set old;**

**pred=1/(1+exp(-1*(Intercept+ (AGE_P*AGE)+(SEX_P*SEX)+(EDU_P*EDU)**

**%if MRT=1 %then %str(**

**+ (MRT_P1))));**

**);**

**%else %if MRT=2 %then %str(**

**+ (MRT_P2))));**

**);**

**%else %str(**

**)));**

**);**

**run;**

** **

**%mend predictor;**

**%predictor;**

** **

**When I plug what's in the string functions individually, it works, so I know it doesn't have anything to do with how I coded the content (i.e. missing parentheses/semicolons). I've also used this function before in similar contexts, and never had a problem. **

** **

**I'm getting the dreaded "There is no matching %IF statement for the %ELSE...A dummy macro will be compiled," error.**

** **

**Any help would be appreciated.**

** **

**Thanks in advance!**

** **

**Emily**

Accepted Solutions

Solution

02-14-2018
02:14 PM

- Mark as New
- Bookmark
- Subscribe
- RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

Posted in reply to epstewart1110

02-13-2018 06:03 PM

99.9% of time the error is on the user side. In this case, I suspect you're getting small rounding errors from possibly typing out your variables.

Either way, you can use the CODE statement in PROC LOGISTIC to see the code SAS would generate to calculate the predicted probabilities.

Or you can see this example that illustrates how to check it exactly:

The Neuralgia2 data set is part of the PROC LOGISTIC Examples.

Oh and you probably want PARAM =REF on your CLASS statement.

epstewart1110 wrote:

Hi,

I have a follow up question to the original posted earlier today (listed below in bold).

How come there are slight differences in the predicted probabilities between those calculated by SAS:

proc logistic data=temp desc;

class EDU (ref='0') SEX (REF='0') MRT(REF='0');

model outcome=AGE SEX EDU MRT;

output out=pp pred=pred;

run;

and using the standard formula:

pred=1/(1+exp(-1*(Intercept+ (AGE_P*AGE)+(SEX_P*SEX)+(EDU_P*EDU)+(MRT_P*MRT);

Assuming Intercept, AGE_P, SEX_P, EDU_P, AND MRT_P are the coefficients provided by SAS, multiplied by the actual values per person.

The min. difference between the predicted probabilities produced by SAS and by hand is .00003, the max, .0815, and the mean .0219.

Thanks!

Emily

Hello,

I am trying to use the %str function to modify my code. Here it is:

%macro predictor;

data new;

set old;

pred=1/(1+exp(-1*(Intercept+ (AGE_P*AGE)+(SEX_P*SEX)+(EDU_P*EDU)

%if MRT=1 %then %str(

+ (MRT_P1))));

);

%else %if MRT=2 %then %str(

+ (MRT_P2))));

);

%else %str(

)));

);

run;

%mend predictor;

%predictor;

When I plug what's in the string functions individually, it works, so I know it doesn't have anything to do with how I coded the content (i.e. missing parentheses/semicolons). I've also used this function before in similar contexts, and never had a problem.

I'm getting the dreaded "There is no matching %IF statement for the %ELSE...A dummy macro will be compiled," error.

Any help would be appreciated.

Thanks in advance!

Emily

All Replies

Solution

02-14-2018
02:14 PM

- Mark as New
- Bookmark
- Subscribe
- RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

Posted in reply to epstewart1110

02-13-2018 06:03 PM

99.9% of time the error is on the user side. In this case, I suspect you're getting small rounding errors from possibly typing out your variables.

Either way, you can use the CODE statement in PROC LOGISTIC to see the code SAS would generate to calculate the predicted probabilities.

Or you can see this example that illustrates how to check it exactly:

The Neuralgia2 data set is part of the PROC LOGISTIC Examples.

Oh and you probably want PARAM =REF on your CLASS statement.

epstewart1110 wrote:

Hi,

I have a follow up question to the original posted earlier today (listed below in bold).

How come there are slight differences in the predicted probabilities between those calculated by SAS:

proc logistic data=temp desc;

class EDU (ref='0') SEX (REF='0') MRT(REF='0');

model outcome=AGE SEX EDU MRT;

output out=pp pred=pred;

run;

and using the standard formula:

pred=1/(1+exp(-1*(Intercept+ (AGE_P*AGE)+(SEX_P*SEX)+(EDU_P*EDU)+(MRT_P*MRT);

Assuming Intercept, AGE_P, SEX_P, EDU_P, AND MRT_P are the coefficients provided by SAS, multiplied by the actual values per person.

The min. difference between the predicted probabilities produced by SAS and by hand is .00003, the max, .0815, and the mean .0219.

Thanks!

Emily

Hello,

I am trying to use the %str function to modify my code. Here it is:

%macro predictor;

data new;

set old;

pred=1/(1+exp(-1*(Intercept+ (AGE_P*AGE)+(SEX_P*SEX)+(EDU_P*EDU)

%if MRT=1 %then %str(

+ (MRT_P1))));

);

%else %if MRT=2 %then %str(

+ (MRT_P2))));

);

%else %str(

)));

);

run;

%mend predictor;

%predictor;

When I plug what's in the string functions individually, it works, so I know it doesn't have anything to do with how I coded the content (i.e. missing parentheses/semicolons). I've also used this function before in similar contexts, and never had a problem.

I'm getting the dreaded "There is no matching %IF statement for the %ELSE...A dummy macro will be compiled," error.

Any help would be appreciated.

Thanks in advance!

Emily

- Mark as New
- Bookmark
- Subscribe
- RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

Posted in reply to Reeza

02-13-2018 06:45 PM

Thanks Reeza!

I accounted for rounding errors by outputting the coefficients (outest=coefficients), and merging them with the predicted probabilities (pred=pred). I then applied the formula in SAS, so both the coefficients and predicted probabilities come from the same model and are unrounded.

I did, however, alter my model using param=ref:

proc logistic data=temp desc **outest=coefficients**;

class EDU (ref='0') SEX (REF='0') MRT(REF='0') / **param=ref**;

model outcome=AGE SEX EDU MRT;

output out=pp **pred=pred**;

run;

Is this correct?

Despite still not identical, using param=ref greatly reduced discrepancies between SAS's predicted probabilities and those obtained by hand. The min. diff is now 0, the max, 0.0295, and the mean, 0.0097480.

There must be some reason for why they are still not exactly the same, but regardless, thanks for your help!

Emily

- Mark as New
- Bookmark
- Subscribe
- RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

Posted in reply to epstewart1110

02-13-2018 09:53 PM

Post the code you used to do the newest comparison.

epstewart1110 wrote:

Thanks Reeza!

I accounted for rounding errors by outputting the coefficients (outest=coefficients), and merging them with the predicted probabilities (pred=pred). I then applied the formula in SAS, so both the coefficients and predicted probabilities come from the same model and are unrounded.

I did, however, alter my model using param=ref:

proc logistic data=temp desc

outest=coefficients;class EDU (ref='0') SEX (REF='0') MRT(REF='0') /

param=ref;model outcome=AGE SEX EDU MRT;

output out=pp

pred=pred;run;

Is this correct?

Despite still not identical, using param=ref greatly reduced discrepancies between SAS's predicted probabilities and those obtained by hand. The min. diff is now 0, the max, 0.0295, and the mean, 0.0097480.

There must be some reason for why they are still not exactly the same, but regardless, thanks for your help!

Emily

- Mark as New
- Bookmark
- Subscribe
- RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

Posted in reply to Reeza

02-14-2018 02:13 PM - edited 02-14-2018 02:16 PM

Originally, I was struggling with how to multiply the class level to the correct class dependent coefficient (in my first post). There are three levels for MRT, so I needed to multiply each one with the corresponding class-dependent coefficient.

I ended up doing this:

**%macro predictor;**

** **

**data new;**

**set old;**

**%if MRT=1 %then %do;**

**pred=1/(1+exp(-1*(Intercept+ (AGE_P*AGE)+(SEX_P*SEX)+(EDU_P*EDU)+(MRT*MRT_P1))));**

**%end;**

**%else %if MRT=2 %then %do;**

**pred=1/(1+exp(-1*(Intercept+ (AGE_P*AGE)+(SEX_P*SEX)+(EDU_P*EDU)+(MRT*MRT_P2))));**

**%end;**

**%else %do;**

**pred=1/(1+exp(-1*(Intercept+ (AGE_P*AGE)+(SEX_P*SEX)+(EDU_P*EDU))));**

**%end;**

** **

**%mend predictor;**

**%predictor;**

Astounding showed me a much more straightforward, simple way:

if mrt=1 then temp=mrt_p1;

else if mrt=2 then temp=mrt_p2;

else temp=0;

pred=1/(1+exp(-1*(Intercept+ (AGE_P*AGE)+(SEX_P*SEX)+(EDU_P*EDU) + (temp) )));

Once I did this, discrepancies between SAS predicted probabilities and those using the formula were minuscule.

Thanks Reeza!