Page 1 of 1

PRJMultinomial—predicted probs for a multinomial logit

Posted: Thu Jul 10, 2008 2:01 pm
by TomDoan
This computes the predicted probabilities and marginal effects for the choices in a multinomial logit model given an input set of explanatory variables.

Code: Select all

*
* @PRJMultinomial( options )
* computes predicted probabilities and marginal effects for choices in a
* multinomial logit. This should be only be used after doing DDV with
* TYPE=MULTINOMIAL.
*
* Options:
*  XVECTOR=vector of test values for the explanatory variables. Make sure you include
*    1 for the CONSTANT.
*  LABELS=VECTOR[STRINGS] with labels for the choices. These should correspond to the
*    numeric codings from smallest to largest.
*  TITLE=title for output
*
* Revision Schedule:
*  07/2008 Written by Tom Doan, Estima.
*
procedure prjmultinomial
option vector        xvector
option vect[strings] labels
option string        title
*
local real    p
local vect    ez pz dg m
local rect    betamat
local integer i j row
*
if %rows(%betasys)==0.or.%clock(%rows(%betasys),%nreg)<>%nreg {
   disp "@PRJMULTINOMIAL must follow DDV(TYPE=MULTINOMIAL)"
   return
}
*
if .not.%defined(xvector) {
   disp "Syntax: @PRJMULTINOMIAL(XVECTOR=xvector,other options)"
   return
}
*
* Reformat %betasys into an %NREG x (choices - 1) array
*
compute betamat=%vectorect(%betasys,%nreg)
*
* Compute the probabilities of the choices (other than 1)
*
compute ez=%exp(tr(xvector)*betamat)
compute pz=ez/(1+%sum(ez))
compute dg=betamat*pz
report(action=define)
compute row=0
if %defined(title)
   report(atrow=(row=row+1),atcol=1,span) title

do i=1,%rows(ez)+1
   *
   * Handle the first choice by taking the remainders
   *
   if i==1 {
      compute p=1-%sum(pz)
      compute m=-p*dg
   }
   else {
      compute p=pz(i-1)
      compute m=p*(%xcol(betamat,i-1)-dg)
   }
   compute row=row+1
   if %defined(labels)
      report(atrow=row,atcol=1) "Choice: "+labels(i)
   else
      report(atrow=row,atcol=1) "Choice "+i
   report(atrow=(row=row+1),atcol=1) "Predicted P" p
   do j=1,%nreg
      if %eqnreglabels(0)(j)=="Constant"
         next
      report(atrow=(row=row+1),atcol=1) "Marginal "+%eqnreglabels(0)(j) m(j)
   end do j
   compute row=row+1
end do i
report(action=show)
end

Re: PRJMultinomial—predicted probs for a multinomial logit

Posted: Thu May 23, 2019 9:56 pm
by adrangi
Hi Tom. I'm trying to compute the probabilities for three cases in multinomial logit. I submitted the following. The ddv worked,but got stuck on the second part. I got an error message on the options part. I'm almost positive that I'm not entering the options properly. I would appreciate if you could look at this and let me know how I should change it. Best, B. Adrangi

ddv(type=multinomial) PSECHOICE
# HSCATH GRADES FAMINC FAMSIZ PARCOLL FEMALE BLACK Constant
*
@PRJMultinomial([XVECTOR]=1 10 63 4 1 1 1 1, LABELS=PSECHOICE1=1 PSECHOICE2=2 PSECHOICE3=3,TITLE="SCHOOL CHOICE")

Re: PRJMultinomial—predicted probs for a multinomial logit

Posted: Fri May 24, 2019 6:39 am
by TomDoan
Here's an example of the proper use. You have the syntax wrong on the VECT's in the options. Note that you could have found those yourself with Help-Find in Files.
*
* Hill, Griffiths, & Lim, Principles of Econometrics, 3rd edition
* Example 16.3.4 starting page 429
*
open data nels_small.dat
data(format=free,org=columns) 1 1000 psechoice hscath grades faminc famsiz parcoll female black
*
ddv(type=multinomial,coeffs=betamat) psechoice
# constant grades
*
@prjmultinomial(title="Analysis for Median Student",xvector=||1.00,6.64||,$
labels=||"No College","2-Year College","4-Year College"||)
@prjmultinomial(title="Analysis for 5%-ile Student",xvector=||1.00,2.635||,$
labels=||"No College","2-Year College","4-Year College"||)

Re: PRJMultinomial—predicted probs for a multinomial logit

Posted: Fri May 24, 2019 10:20 am
by adrangi
Thanks so much for the prompt response Tom. Also thanks for pointing me to the help command. Will use it from now on. Didn't even know about it after using RATS for so long!

On a side note, RATS has been a life saver for me. Tried to run this same thing in R with a grad student. We both gave up after about 2.5 hrs!! I had done the DDV part and wanted to compare results! No dice! Thanks again. Best, Bahram Adrangi