如何使用 PROC MDC 来预测测试样本的选择概率? - n选择错误

问题描述 投票:0回答:1

我正在尝试使用以下代码通过 proc mdc 预测选择概率:

/*  Reformat data as necessary */
data brand_choice(keep=Brand Price Displ Feature Chosen PurchaseID);
array prices[4] priceprivate pricesunshine pricekeebler pricenabisco;
array displays[4] displprivate displsunshine displkeebler displnabisco;
array features[4] featprivate featsunshine featkeebler featnabisco;
array chosenbrand[4] private sunshine keebler nabisco;
array allbrands[4] $8 _temporary_ ('Private' 'Sunshine' 'Keebler' 'Nabisco');

 set reg.crackers_hw5;

 PurchaseID = _N_;

do i = 1 to 4;
     Brand = allbrands[i];
     Price = prices[i];
     Displ = displays[i];
     Feature = features[i];
     Chosen = chosenbrand[i];
                output;
end;

/* Step 1: Prepare the Dataset */
proc surveyselect data=brand_choice
    out=brand_choice_sampled
    outall
    samprate=0.75
    seed=1
    method=srs;
run;

data brand_choice_sampled;
    set brand_choice_sampled;
    if selected = 1 then choiceT = chosen;
    else choiceT = .;
run;

/* Step 2: Estimate the Model */
proc mdc data=brand_choice_sampled;
   class displ feature;
    model choiceT = price displ feature displ*feature / type = mprobit nchoice = 4;
    id PurchaseID;
    restrict Displ1 = 0, Feature1 = 0, Displ1Feature1 = 0;
    output out=pred_probs p=predicted_prob; 
run;

/* Step 3: View the Predicted Probabilities */
proc print data=pred_probs;
    var predicted_prob; 
    id PurchaseID;
run;

并收到此错误:

错误:当每个人(ID)的选择数量不相同时,不允许使用 NCHOICE= 选项。

这是我的数据集在重新格式化后的样子:

a snapshot of my dataset after transformation

sas
1个回答
0
投票

我不确定到底发生了什么。但是,这是产生预期结果的更正代码:

/* c. Reformat data as necessary */
data brand_choice(keep=Brand Price Displ Feature Chosen PurchaseID);
array prices[4] priceprivate pricesunshine pricekeebler pricenabisco;
array displays[4] displprivate displsunshine displkeebler displnabisco;
array features[4] featprivate featsunshine featkeebler featnabisco;
array chosenbrand[4] private sunshine keebler nabisco;
array allbrands[4] $8 _temporary_ ('Private' 'Sunshine' 'Keebler' 'Nabisco');

 set reg.crackers_hw5;

 PurchaseID = _N_;

do i = 1 to 4;
     Brand = allbrands[i];
     Price = prices[i];
     Displ = displays[i];
     Feature = features[i];
     Chosen = chosenbrand[i];
                output;
end;

/* Prepare the Dataset */
proc surveyselect data=brand_choice
    out=brand_choice_sampled
    outall
    samprate=0.75
    seed=1
    method=srs;
run;

data brand_choice_sampled;
    set brand_choice_sampled;
    if selected = 1 then choiceT = chosen;
    else choiceT = .;
run;

/* Estimate the Model */
proc mdc data=brand_choice_sampled;
   class displ feature;
    model chosen = price displ feature displ*feature / type = clogit nchoice = 4;
    id PurchaseID;
    restrict Displ1 = 0, Feature1 = 0, Displ1Feature1 = 0;
    output out=pred_probs p=predicted_prob; 
run;


/* View the Predicted Probabilities */
proc print data=pred_probs;
    var predicted_prob; 
    id PurchaseID;
run;
© www.soinside.com 2019 - 2024. All rights reserved.