<div>title ’Choice of Chocolate Candies’;</div><div>data chocs;</div><div>input Subj c Dark Soft Nuts @@;</div><div>Set = 1;</div><div>datalines;</div><div>1 2 0 0 0 1 2 0 0 1 1 2 0 1 0 1 2 0 1 1</div><div>1 1 1 0 0 1 2 1 0 1 1 2 1 1 0 1 2 1 1 1</div><div>2 2 0 0 0 2 2 0 0 1 2 2 0 1 0 2 2 0 1 1</div><div>2 2 1 0 0 2 1 1 0 1 2 2 1 1 0 2 2 1 1 1</div><div>3 2 0 0 0 3 2 0 0 1 3 2 0 1 0 3 2 0 1 1</div><div>3 2 1 0 0 3 2 1 0 1 3 1 1 1 0 3 2 1 1 1</div><div>4 2 0 0 0 4 2 0 0 1 4 2 0 1 0 4 2 0 1 1</div><div>4 1 1 0 0 4 2 1 0 1 4 2 1 1 0 4 2 1 1 1</div><div>5 2 0 0 0 5 1 0 0 1 5 2 0 1 0 5 2 0 1 1</div><div>5 2 1 0 0 5 2 1 0 1 5 2 1 1 0 5 2 1 1 1</div><div>6 2 0 0 0 6 2 0 0 1 6 2 0 1 0 6 2 0 1 1</div><div>6 2 1 0 0 6 1 1 0 1 6 2 1 1 0 6 2 1 1 1</div><div>7 2 0 0 0 7 1 0 0 1 7 2 0 1 0 7 2 0 1 1</div><div>7 2 1 0 0 7 2 1 0 1 7 2 1 1 0 7 2 1 1 1</div><div>8 2 0 0 0 8 2 0 0 1 8 2 0 1 0 8 2 0 1 1</div><div>8 2 1 0 0 8 1 1 0 1 8 2 1 1 0 8 2 1 1 1</div><div>9 2 0 0 0 9 2 0 0 1 9 2 0 1 0 9 2 0 1 1</div><div>9 2 1 0 0 9 1 1 0 1 9 2 1 1 0 9 2 1 1 1</div><div>10 2 0 0 0 10 2 0 0 1 10 2 0 1 0 10 2 0 1 1</div><div>10 2 1 0 0 10 1 1 0 1 10 2 1 1 0 10 2 1 1 1</div><div>;</div><div>proc print data=chocs noobs;</div><div>where subj <= 2;</div><div>var subj set c dark soft nuts;</div><div>run;</div><div>* Alternative Form of Data Entry;</div><div>data combos; /* Read the design matrix. */</div><div>input Dark Soft Nuts;</div><div>datalines;</div><div>0 0 0</div><div>0 0 1</div><div>0 1 0</div><div>0 1 1</div><div>1 0 0</div><div>1 0 1</div><div>1 1 0</div><div>1 1 1</div><div>;</div><div>data chocs; /* Create the data set. */</div><div>input Choice @@; drop choice; /* Read the chosen combo num. */</div><div>Subj = _n_; Set = 1; /* Store subj, choice set num. */</div><div>do i = 1 to 8; /* Loop over alternatives. */</div><div>c = 2 - (i eq choice); /* Designate chosen alt. */</div><div>set combos point=i; /* Read design matrix. */</div><div>output; /* Output the results. */</div><div>end;</div><div>datalines;</div><div>5 6 7 5 2 6 2 6 6 6</div><div>;</div><div>proc phreg data=chocs outest=betas;</div><div>strata subj set;</div><div>model c*c(2) = dark soft nuts / ties=breslow;</div><div>label dark = ’Dark Chocolate’ soft = ’Soft Center’</div><div>nuts = ’With Nuts’;</div><div>run;</div><div>data chocs2;</div><div>set chocs;</div><div>Milk = 1 - dark; Chewy = 1 - Soft; NoNuts = 1 - nuts;</div><div>label dark = ’Dark Chocolate’ milk = ’Milk Chocolate’</div><div>soft = ’Soft Center’ chewy = ’Chewy Center’</div><div>nuts = ’With Nuts’ nonuts = ’No Nuts’;</div><div>run;</div><div>proc phreg data=chocs2;</div><div>strata subj set;</div><div>model c*c(2) = dark milk soft chewy nuts nonuts / ties=breslow;</div><div>run;</div><div><span class="Apple-style-span" style="color: rgb(255, 0, 0); ">* Estimate the probability that each alternative will be chosen; (在这之前的应该问题不是很大,就是在求probability中出不来结果)</span></div><div>data p;<br/></div><div>retain sum 0;</div><div>set combos end=eof;</div><div>* On the first pass through the DATA step (_n_ is the pass</div><div>number), get the regression coefficients in B1-B3.</div><div>Note that they are automatically retained so that they</div><div>can be used in all passes through the DATA step.;</div><div>if _n_ = 1 then</div><div><span class="Apple-style-span" style="color: rgb(255, 0, 0); ">set betas(rename=(dark=b1 soft=b2 nuts=b3));</span></div><div>keep dark soft nuts p;</div><div>array x[3] dark soft nuts;</div><div>array b[3] b1-b3;</div><div>* For each combination, create x * b;</div><div>p = 0;</div><div>do j = 1 to 3;</div><div>p = p + x[j] * b[j];</div><div>end;</div><div>* Exponentiate x * b and sum them up;</div><div>p = exp(p);</div><div>sum = sum + p;</div><div>* Output sum exp(x * b) in the macro variable ’&sum’;</div><div><span class="Apple-style-span" style="color: rgb(255, 0, 0); ">if eof then call symput(’sum’,put(sum,best12.));</span></div><div>run;</div><div>proc format;</div><div>value df 1 = ’Dark’ 0 = ’Milk’;</div><div>value sf 1 = ’Soft’ 0 = ’Chewy’;</div><div>value nf 1 = ’Nuts’ 0 = ’No Nuts’;</div><div>run;</div><div>* Divide each exp(x * b) by sum exp(x * b);</div><div>data p;</div><div>set p;</div><div><span class="Apple-style-span" style="color: rgb(255, 0, 0); ">p = p / (&sum);</span></div><div>format dark df. soft sf. nuts nf.;</div><div>run;</div><div>proc sort;</div><div><span class="Apple-style-span" style="color: rgb(255, 0, 0); ">by descending p;</span></div><div>run;</div><div>proc print;</div><div>run;</div><div><br/></div><div>红字的部分是有问题的部分,怎样修改呢? 谢谢</div><div><br/></div>
[此贴子已经被作者于2008-12-4 14:05:30编辑过]