Discrete Choice 1
/****************************************************************/
/* S A S S A M P L E L I B R A R Y */
/* */
/* NAME: MR2010F */
/* TITLE: Discrete Choice 1 */
/* PRODUCT: STAT */
/* SYSTEM: ALL */
/* KEYS: marketing research */
/* PROCS: PHREG, TRANSREG, OPTEX, FACTEX, PLAN, IML */
/* DATA: */
/* */
/* SUPPORT: saswfk UPDATE: 01Oct2010 */
/* REF: MR-2010F Discrete Choice */
/* MISC: This file contains SAS code for the "Discrete */
/* Choice" report, the 01Oct2010 edition, for SAS 9.2. */
/* */
/* You must install the following macros from */
/* http://support.sas.com/techsup/tnote/tnote_stat.html#market */
/* */
/* ChoicEff Efficient Choice Designs */
/* MktAllo Process a Choice Allocation Study Data Set */
/* MktBal Balanced Experimental Design */
/* MktBIBD Balanced Incomplete Block Design */
/* MktBlock Block an Experimental Design */
/* MktBSize Balanced Incomplete Block Design Sizes */
/* MktDes Efficient Experimental Designs */
/* MktDups Eliminate Duplicate Runs or Choice Sets */
/* MktEval Evaluate an Experimental Design */
/* MktEx Efficient Experimental Designs */
/* MktKey Aid Making the MktRoll KEY= Data Set */
/* MktLab Change Names, Levels in a Design */
/* MktMDiff MaxDiff (Best-Worst) Choice Modeling */
/* MktMerge Merge a Choice Design with Choice Data */
/* MktOrth List Orthogonal Designs MktEx Can Make */
/* MktPPro Optimal Partial Profile Designs */
/* MktRoll Roll Out a Design Into a Choice Design */
/* MktRuns Experimental Design Sizes */
/* PhChoice Customize PROC PHREG for Choice Models */
/* */
/****************************************************************/
************* Begin PHREG Output Customization **************;
options ls=80 ps=60 nonumber nodate;
%phchoice(on)
***************** Begin Candy Example Code ******************;
/* Uncomment this code if you want it to run
data x;
do u = -2 to 5 by 0.1;
p = exp(u) / 234.707;
output;
end;
label p = 'Probability(Choice)' u = 'Utility';
run;
proc sgplot data=x;
title 'Probability of Choice as a Function of Utility';
series y=p x=u;
run;
*/
title 'Choice of Chocolate Candies';
data chocs;
input Subj c Dark Soft Nuts @@;
Set = 1;
datalines;
1 2 0 0 0 1 2 0 0 1 1 2 0 1 0 1 2 0 1 1
1 1 1 0 0 1 2 1 0 1 1 2 1 1 0 1 2 1 1 1
2 2 0 0 0 2 2 0 0 1 2 2 0 1 0 2 2 0 1 1
2 2 1 0 0 2 1 1 0 1 2 2 1 1 0 2 2 1 1 1
3 2 0 0 0 3 2 0 0 1 3 2 0 1 0 3 2 0 1 1
3 2 1 0 0 3 2 1 0 1 3 1 1 1 0 3 2 1 1 1
4 2 0 0 0 4 2 0 0 1 4 2 0 1 0 4 2 0 1 1
4 1 1 0 0 4 2 1 0 1 4 2 1 1 0 4 2 1 1 1
5 2 0 0 0 5 1 0 0 1 5 2 0 1 0 5 2 0 1 1
5 2 1 0 0 5 2 1 0 1 5 2 1 1 0 5 2 1 1 1
6 2 0 0 0 6 2 0 0 1 6 2 0 1 0 6 2 0 1 1
6 2 1 0 0 6 1 1 0 1 6 2 1 1 0 6 2 1 1 1
7 2 0 0 0 7 1 0 0 1 7 2 0 1 0 7 2 0 1 1
7 2 1 0 0 7 2 1 0 1 7 2 1 1 0 7 2 1 1 1
8 2 0 0 0 8 2 0 0 1 8 2 0 1 0 8 2 0 1 1
8 2 1 0 0 8 1 1 0 1 8 2 1 1 0 8 2 1 1 1
9 2 0 0 0 9 2 0 0 1 9 2 0 1 0 9 2 0 1 1
9 2 1 0 0 9 1 1 0 1 9 2 1 1 0 9 2 1 1 1
10 2 0 0 0 10 2 0 0 1 10 2 0 1 0 10 2 0 1 1
10 2 1 0 0 10 1 1 0 1 10 2 1 1 0 10 2 1 1 1
;
proc print data=chocs noobs;
where subj <= 2;
var subj set c dark soft nuts;
run;
title 'Choice of Chocolate Candies';
* Alternative Form of Data Entry;
data combos; /* Read the design matrix. */
input Dark Soft Nuts;
datalines;
0 0 0
0 0 1
0 1 0
0 1 1
1 0 0
1 0 1
1 1 0
1 1 1
;
data chocs; /* Create the data set. */
input Choice @@; drop choice; /* Read the chosen combo num. */
Subj = _n_; Set = 1; /* Store subj, choice set num. */
do i = 1 to 8; /* Loop over alternatives. */
c = 2 - (i eq choice); /* Designate chosen alt. */
set combos point=i; /* Read design matrix. */
output; /* Output the results. */
end;
datalines;
5 6 7 5 2 6 2 6 6 6
;
proc phreg data=chocs outest=betas;
strata subj set;
model c*c(2) = dark soft nuts / ties=breslow;
label dark = 'Dark Chocolate' soft = 'Soft Center'
nuts = 'With Nuts';
run;
data chocs2;
set chocs;
Milk = 1 - dark; Chewy = 1 - Soft; NoNuts = 1 - nuts;
label dark = 'Dark Chocolate' milk = 'Milk Chocolate'
soft = 'Soft Center' chewy = 'Chewy Center'
nuts = 'With Nuts' nonuts = 'No Nuts';
run;
proc phreg data=chocs2;
strata subj set;
model c*c(2) = dark milk soft chewy nuts nonuts / ties=breslow;
run;
* Estimate the probability that each alternative is chosen;
data p;
retain sum 0;
set combos end=eof;
* On the first pass through the DATA step (_n_ is the pass
number), get the regression coefficients in B1-B3.
Note that they are automatically retained so that they
can be used in all passes through the DATA step.;
if _n_ = 1 then
set betas(rename=(dark=b1 soft=b2 nuts=b3));
keep dark soft nuts p;
array x[3] dark soft nuts;
array b[3] b1-b3;
* For each combination, create x * b;
p = 0;
do j = 1 to 3;
p = p + x[j] * b[j];
end;
* Exponentiate x * b and sum them up;
p = exp(p);
sum = sum + p;
* Output sum exp(x * b) to the macro variable '&sum';
if eof then call symputx('sum', sum);
run;
proc format;
value df 1 = 'Dark' 0 = 'Milk';
value sf 1 = 'Soft' 0 = 'Chewy';
value nf 1 = 'Nuts' 0 = 'No Nuts';
run;
* Divide each exp(x * b) by sum exp(x * b);
data p;
set p;
p = p / (&sum);
format dark df. soft sf. nuts nf.;
run;
proc sort;
by descending p;
run;
proc print;
run;
%phchoice(off)