View Full Version : Logistic Regression


statsboi
02-12-2009, 11:59 PM
Hi ,

I'd like to run logistic regression on a data set. However there are more than 20 covariates. I need to have 20 logistic regressions. Instead of writing the same code for 20 times. I am wondering whether macro would help to make the process more efficient.

J

Masteras
02-13-2009, 03:49 AM
you run one logistic regression with all 20 covariates and see which ones are significant, stepwise regression.

vinux
02-13-2009, 07:40 AM
Using do loop you can do this..

Or the following way also you can do it.


%macro logit(var);
proc logistic;
model a=b c &var.;
run;
%mend;
%logit(d1);
%logit(d2); ... etc

statsboi
02-13-2009, 09:03 AM
Hi Richie, thanks for the help. Is there a way to invoke %logit to read each of the 20 variable using DO loops? I have 50 covariates in another dataset. =] hoping i dont have to write %logit 50 times

J

vinux
02-13-2009, 09:27 AM
ok you can try this..
I hope you have 50 variable and it is say d1,d2,..d50


%macro logit;
%do i=1 %to 50;
proc logistic;
model a=b c d&i.;
run;
%end;
%mend ;

%logit;



I hope this will work..

Eleni Econ
02-14-2009, 05:38 PM
Please can anyone tell me how can i test the goodness of fit after a logistic regression?
Please please please...
pseudo R2 is 0.09 and i am worried and i dont know what to do!!!!

Masteras
02-14-2009, 06:39 PM
Eleni, live the R squared. Take the residual deviance and compare it with the chi square distribution at the 95% and degrees of freedom the same as the deviance. For example, suppose you have a residual deviance equal to 4 at 3 degrees of freedom. Find the 95% chi square value with df=3. If this value is larger than the 4 you obtained, this provides evidence that your model fits well.

Eleni Econ
02-15-2009, 06:28 AM
Dear Masteras,
my model has employed=1 unemployed=0 as a dependent variable and independent variables are dummies and countinious variables and a used a probabilty weight too. Pseudo R2 is 0.09 please can you tell me again what i have to do with the goodness of fit, because i tried but i am using pw and stata didt let me to take the residuals.
Please if you can tell me exactly what to do because is my first time that i come with stata and genarally data analysis for example what is the command in order to get the residual deviance, thank you very very much!!!!!!!!

Masteras
02-15-2009, 10:47 AM
Listen i can tell you tomorrow from the uni lab computers what is the command, since I removed stata from my lapitop. This is in general though. In your case since the R squared is too low, i do not think the fit will be not rejected.

Eleni Econ
02-15-2009, 11:17 AM
Thank you very much!!!

Masteras
02-15-2009, 06:16 PM
parakalw, cu tomorrow.

Masteras
02-16-2009, 06:33 AM
listen, it has a bit of hand maths. When you stick with the model that has the significant parameters in the equation do this. Say you have logit(p)=a+bX, ok?
find the estimated pis by this: piest=[exp(a+bxi)]/[1+exp(a+bxi)].
then find the sum of (pireal-piest)^2. What you find is the deviance. You have for instance 10 measures, sample size=10. Find df=10-number of parameters in the equation. Then find from excel the 95% of Chi-square with degrees of freedom the df you found (df=10-number of parameters in the equation). If this value is larger that the sum you found then the model has a non-rejected fit. clear?

Eleni Econ
02-16-2009, 07:53 AM
euxaristo poly elpizo na ta katafero:)!!

statsboi
02-18-2009, 11:19 PM
[QUOTE=vinux;19877]ok you can try this..
I hope you have 50 variable and it is say d1,d2,..d50

[code]
%macro logit;
%do i=1 %to 50;
proc logistic;
model a=b c d&i.;
run;
%end;
%mend ;

%logit;

vinux
02-19-2009, 09:41 AM
Hi Richie, thanks once again. I wrote the code as follow (because the 50 variables are of different names, how do i go about that?).

%macro logistic();
%let Varlist = a, b, c, d...etc;
%let i = 1;
%let CurrVar = %scan(&Varlist, &i.);
%do %until (&CurrVar= );
proc logisitc data=fit descending;
model a= b &CurrVar;
run;
%let i = %eval(&i + 1);
%end;
%mend;
%logistic;

I get this error message, the submitted procedure wont stop.. i have to end the session mannualy.

NOTE: The SAS System stopped processing this step because of errors.
NOTE: Line generated by the invoked macro "LOGISTIC".
707 proc logisitc data=fit descending; model a = b &CurrVar; run;
--------
1

WARNING 1-322: Assuming the symbol LOGISTIC was misspelled as logisitc.


Silly mistake happend in the code..
Change it in this way..

%macro logistic();
%let Varlist = a b c d...etc; /* no commas */
%let i = 1;
%do %until (&CurrVar= );
%let CurrVar = %scan(&Varlist, &i.); /* keep it in the loop*/
proc logisitc data=fit descending;
model a= b &CurrVar;
run;
%let i = %eval(&i + 1);
%end;
%mend;
%logistic;

vinux
02-20-2009, 01:56 PM
what is the error you are getting?..
Change the name of the macro also.. ( i don't think that will be an issue)

statsboi
02-20-2009, 04:05 PM
what is the error you are getting?..
Change the name of the macro also.. ( i don't think that will be an issue)
I had the SAS process running non-stop producing only the outputs of first logistic regression model a1=b2 iw

what name/label shall i change to make it work?

Thanks

J

vinux
02-21-2009, 04:07 AM
I had the SAS process running non-stop producing only the outputs of first logistic regression model a1=b2 iw

what name/label shall i change to make it work?

Thanks

J


Ok ... use %put and see the values


%macro logit();
%let Varlist = iw iv sw ; /* no commas */
%let i = 1;
%do %until (&CurrVar= );
%let CurrVar = %scan(&Varlist, &i.); /* keep it in the loop*/

%put &i. &CurrVar.;
/*
proc logisitc data=fit descending;
model a= b &CurrVar;
run;
*/
%let i = %eval(&i + 1);
%end;
%mend;
%logit;See the values of i and currvar values in the log ....
If it is working fine.. then you can uncomment the logistic part..

vinux
02-23-2009, 12:09 AM
Check the spelling.
It is not
logisitc . It is
logistic


Is there a difference between a " . " and without "." in the macro variable?


It is always a good practice put '.' in the end of macro variable.
Otherwise also Sas take the values of the macro variable.

statsboi
02-24-2009, 12:21 AM
Check the spelling.
It is not
logisitc . It is
logistic




It is always a good practice put '.' in the end of macro variable.
Otherwise also Sas take the values of the macro variable.
Big THANKS Richie for your help. You solved my many queries on SAS!!!

vinux
08-16-2009, 10:28 AM
I don't have SAS right now..

but add these lines to test your code
%put &i &j &k ;
%put &CurrVar &CurrVar1 &CurrVar2;
so that you can ensure the logic.