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;
* 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 will be chosen; (在這之前的應(yīng)該問(wèn)題不是很大,就是在求probability中出不來(lái)結(jié)果)
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) in the macro variable ’&sum’;
if eof then call symput(’sum’,put(sum,best12.));
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;
紅字的部分是有問(wèn)題的部分,怎樣修改呢? 謝謝
[此貼子已經(jīng)被作者于2008-12-4 14:05:30編輯過(guò)]