BookmarkSubscribeRSS Feed
pquant
Fluorite | Level 6

Hi all,

 

I need to estimate the scale and the shape parameters of the logistic distribution, given data.

 

Any knows about a built-in SAS procedure to do that?

 

Thanks all in advance.

Any help will be appreciated. Smiley Wink

 

7 REPLIES 7
Ksharp
Super User

You could try PROC NLIN . and @FreelanceReinh @Rick_SAS  could give you a hand .

pquant
Fluorite | Level 6
Thanks @Ksharp for your help!!
FreelanceReinh
Jade | Level 19

Hi @pquant,

 

Let me first say that I've used PROC NLIN only very rarely. (My only previous post involving this procedure was one of those exceptions.) So, regular users of PROC NLIN could find my code awkward or inefficient (and then hopefully suggest a better solution), but here it is:

/* Create sample data for demonstration */

data have;
call streaminit(27182818);
do _n_=1 to 100;
  x=rand('logistic',10,1);
  output;
end;
run;

/* Determine initial estimates a0, b0 for     */
/* location parameter a and scale parameter b */

proc summary data=have;
var x;
output out=stats(drop=_:) n=n median=m std=s;
run;

data _null_;
set stats;
call symputx('n',n);
call symputx('a0',m);
call symputx('b0',sqrt(3)/constant('pi')*s);
run;

/* Prepare input dataset for PROC NLIN */

proc transpose data=have out=trans(drop=_:) prefix=x;
var x;
run;

data trans2;
set trans trans;
y=&n/_n_; /* RHS of maximum-likelihood equation (=LHS of "MODEL" */
run; /* 2 obs. */                 /* equation in PROC NLIN step) */

/* Solve for maximum-likelihood estimates */

ods select EstSummary ParameterEstimates;
ods output ParameterEstimates=est(keep=parameter estimate);
proc nlin data=trans2;
parms a=&a0 b=&b0;
array x[&n];
array z[2];
z1=0; z2=0;
do i=1 to &n;
  t=(x[i]-a)/b;
  z1+t*(1-2/(1+exp(t)));
  z2+1/(1+exp(t));
end;
model y=z[_obs_];
run;

proc print data=est noobs;
run;

PROC PRINT output:

Parameter    Estimate

    a          9.9905
    b          1.0044

(The deviations from the "true" parameters 10 and 1 are, of course, primarily due to random fluctuations in the sample of size 100.)

 

The idea was to solve the maximum-likelihood equations (partial derivatives of the log-likelihood function equated to zero) with PROC NLIN. The first observation of input dataset TRANS2 corresponds to the partial derivative with respect to b (more precisely: "b hat") and the second corresponds to the partial derivative with respect to â. I found the two equations in my standard reference [1], but it turned out that one of them contained a mistake (missing minus sign on the RHS: "...=n" should read "...=n", p.128). I recalculated the equations by hand and used my results in the code. Moreover, I double-checked the formula from [1], p. 125, for the variance of the logistic distribution (used in the data _null_ step).

 

Reference:

[1] Evans, M., Hastings, N. and Peacock, B. (2000), Statistical Distributions, 3rd ed., John Wiley & Sons, New York.

pquant
Fluorite | Level 6

Hi @FreelanceReinh,

 

thanks for your answer, but the rand() sas function does not support the logistic distribution.

 

By running the first datastep, SAS gives the following error:

 

 

ERROR: The first argument of the RAND function must be a character string with a value of
       BERNOULLI, BETA, BINOMIAL, CAUCHY, CHISQUARE, ERLANG, EXPONENTIAL, F, GAMMA, GAUSSIAN,
       GEOMETRIC, HYPERGEOMETRIC, LOGNORMAL, NEGB, NORMAL, POISSON, T, TABLE, TRIANGULAR,
       UNIFORM, or WEIBULL.
NOTE: Argument 1 to function RAND at line 5 column 5 is invalid.

So, it does not work!

FreelanceReinh
Jade | Level 19

@pquant wrote:

thanks for your answer, but the rand() sas function does not support the logistic distribution.

 

By running the first datastep, SAS gives the following error ...

Hi @pquant,

 

The RAND function does support the logistic distribution in release SAS 9.4M5: see documentation. Sorry, I wasn't aware that the logistic distribution is among the more recent additions to the RAND function (and that you are working with a different, likely older SAS release). These recent additions also include the convenient "Integer" distribution, which I've been using frequently since I upgraded from 9.4M2 to M5. What is your version and maintenance release?

 

Anyway, you don't need to run that DATA step, which is there just to /* Create sample data for demonstration */. You would apply the steps below that DATA step to your data for which you don't know the distribution parameter values, not to artificial sample data with known parameter values.

pquant
Fluorite | Level 6

Hi @FreelanceReinh,

 

I have SAS Foundation 9.3 (I don't know exactly the maintainance version of the release). 😞

 

Anyway, I own other software as SAS Enterprise Guide 5.1, but it gives the same error...

 

Is there another way to do that??

FreelanceReinh
Jade | Level 19

@pquant wrote:

Is there another way to do that??


 

Yes, there is! You can create a random sample from a logistic distribution by transforming a random sample from a uniform distribution on the unit interval [0, 1], which is available in your SAS version (rand('uniform')😞

 

/* Specify the parameters of a logistic distribution */

%let a=10; /* location parameter (mean) */
%let b=1;  /* scale parameter, must be positive */

/* Create a random sample of size 100 from the logistic distribution 
   with location parameter (mean) &a and scale parameter &b>0 */

data have(drop=r);
call streaminit(27182818);
do _n_=1 to 100;
  r=rand('uniform');
  x=&a+&b*log(r/(1-r));
  output;
end;
run;

 

It turns out that the newer RAND('logistic',a,b) function seems to use the same algorithm internally (at least in my SAS version), so you may even get (almost) the same random numbers with the code shown above.

 

But, again, this is actually not necessary for the purpose we've discussed in this thread. It's only important that none of the other steps, after the first DATA step fails because of unavailable features. It was just me who needed the DATA step because I don't have your data. Just replace "HAVE" by the name of your dataset in the PROC SUMMARY and PROC TRANSPOSE steps.

 

According to this blog entry SAS 9.4 was first released in 2013. Documentation suggests that the current SAS Enterprise Guide version is 8.1. So, I would consider an upgrade to avoid issues like this.

Ready to join fellow brilliant minds for the SAS Hackathon?

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

Register today!
Mastering the WHERE Clause in PROC SQL

SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 7 replies
  • 1360 views
  • 2 likes
  • 3 in conversation