UCLA Academic Technology Services HomeServicesClassesContactJobs

SAS Code Fragment
Logistic, random intercept, and random slope regression models using nlmixed

This page shows how to run logistic, random intercept, and random slope regression models using proc nlmixed. This is done to demonstrate the use and flexibility of proc nlmixed, and is not meant to suggest you should run these models using nlmixed. In  many cases it would be easier to run the first model in proc logistic, and the subsequent models in proc glimmix.

You can download the data used in this example by clicking here.

/*	create a binary variable   */
data hsb;
  set "d:\data\hsb";
  if mathach >= 13 then himath=1;
  if mathach <13 then himath =0;
run;

/*	Logistic regression with multi-level data, model does not account for clustering.   
	The parms statement is not required.  */
proc nlmixed data=hsb;
  parms b0=0 b1=1;
  xb =  b0 + female*b1
  p = exp(xb)/(1+exp(xb));
  model himath ~ binary(p);
run;

/*	must sort first    */
proc sort data=hsb;
	by id;
run;

/*	Model with a random intercept.
	s2u is the variance of the random intercept.    */
proc nlmixed data=hsb;
  xb = b0 + u + female*b1;
  p = exp(xb)/(1+exp(xb));
  model himath ~ binary(p);
  random u ~ normal(0,s2u) subject=id;
run;

/*	Model with a random intercept and a random slope. 
	The random effects are uncorrelated.
	s2f is the random variance of the coefficient for female.    */
proc nlmixed data=hsb;
  xb = b0 + u + female*(b1+rb1);
  p = exp(xb)/(1+exp(xb));
  model himath ~ binary(p);
  random u rb1 ~ normal([0,0],[s2u,0,s2f]) subject=id;
run;

/*	Model with random intercept and a random slope.
	The covariance of the random effects is estimated.
	cuf is the covariance of the two random effects.   
	Without user specified starting values this model encounters problems. 
	Estimates from the model with uncorrelated random effects are used as starting values.
	The covariance of the random effects (cuf) has a starting value of zero.     */
proc nlmixed data=hsb;
  parms b0=0.1922 b1=-0.370
  s2u = 0.634  s2f=0.0943 cuf=0;
  xb = b0 + u + female*(b1+rb1);
  p = exp(xb)/(1+exp(xb));
  model himath ~ binary(p);
  random u rb1 ~ normal([0,0],[s2u,cuf,s2f]) subject=id;
run;

How to cite this page

Report an error on this page

UCLA Researchers are invited to our Statistical Consulting Services
We recommend others to our list of Other Resources for Statistical Computing Help
These pages are Copyrighted (c) by UCLA Academic Technology Services


The content of this web site should not be construed as an endorsement of any particular web site, book, or software product by the University of California.