*******************************************************************; * exemple simple 1 variable discrète sur données agrégées ; *******************************************************************; data in ; input x1 y count; cards ; -1 1 61 1 1 38 -1 0 58 1 0 20 ; proc print data=in (obs=10) ; run ; proc freq data=in ; weight count ; tables x1*y /nopercent nocol chisq cmh; run; title1 "sans variable explicative" ; proc logistic data=in; freq count ; model y = ; run ; proc logistic data=in; freq count ; class x1 /param=ref; model y = x1 / lackfit; * lackfit pour Hosmer et Lemeshow ; run ; *******************************************************************; * création d'une base individuelle à partir de données agrégées ; *******************************************************************; data in1 ; set in ; keep x1 y ; do i=1 to count ; output ; end ; proc print data=in1 (obs=10) ; run ; proc freq data=in1 ; tables x1*y /nopercent nocol chisq cmh; run; proc logistic data=in1; class x1 ; model y = x1 / lackfit; * lackfit pour Hosmer et Lemeshow ; run ; *******************************************************************; * autre exemple ; *******************************************************************; data in2 ; input x1 x2 y1 y0 ; do i=1 to y1 ; y=1 ; output ; end ; do i=1 to y0 ; y=0 ; output ; end ; cards ; -1 -1 291 3424 -1 1 138 3210 1 -1 324 2508 1 1 240 2508 ; proc freq data=in2 ; tables x1*y x2*y x1*x2*y /nopercent nocol chisq cmh; run; proc logistic data=in2 descending; class x1 x2 ; model y = x1 x2/ lackfit; run ; ***************************************************************; * exemple simple 2 variables discrètes ; ***************************************************************; data ina ; input sexe ancien achat n; label sexe="Genre" ancien="Récence"; cards ; -1 -1 1 291 -1 1 1 138 1 -1 1 324 1 1 1 240 -1 -1 0 3424 -1 1 0 3210 1 -1 0 2508 1 1 0 2508 ; proc format; value sexe_fmt -1="Femme" 1="Homme" ; value ancien_fmt -1="<=6 mois" 1=">6 mois"; run ; proc tabulate data=ina ; freq n ; var achat ; class sexe ancien ; table (sexe all),(ancien all)* achat*mean*f=4.3; format sexe sexe_fmt. ancien ancien_fmt.; run ; Proc logistic data=ina ; freq n; class sexe (ref='1') / param=ref; model achat (ref='0') = sexe / link=logit rsq lackfit; output out=data_out predprobs=I; run ; Proc logistic data=ina ; freq n; class sexe (ref='1') / param=ref; model achat (ref='0') = sexe ancien sexe*ancien/ link=logit rsq lackfit; output out=data_out predprobs=I; run ; data in_MNL ; input sexe $ age $ type $ count @@ ; cards ; G E A 20 G A A 100 G E B 130 G A B 190 G E C 130 G A C 30 F E A 230 F A A 60 F E B 30 F A B 140 F E C 80 F A C 160 ; proc tabulate data=in_MNL ; freq count ; class sexe age type; table sexe*age. (type)*n (type all)*rowpctn ; run ; Proc logistic data=in_MNL ; freq count; class sexe(ref='G') age (ref='E') / param=ref; model type (ref='A') = sexe age / link=glogit ; output out=data_out predprobs=I; run ; ***************************************************************; * logit ordonné ; ***************************************************************; data in_order; input x y count @@; cards; 1 1 6 1 2 9 1 3 12 1 4 11 1 5 7 1 6 6 1 7 1 1 8 0 1 9 0 2 1 1 2 2 1 2 3 6 2 4 8 2 5 23 2 6 7 2 7 5 2 8 1 2 9 0 3 1 0 3 2 0 3 3 1 3 4 7 3 5 8 3 6 8 3 7 19 3 8 8 3 9 1 4 1 0 4 2 0 4 3 0 4 4 1 4 5 3 4 6 7 4 7 14 4 8 16 4 9 11 ; proc freq data=in_order ; weight count ; table x * y / nocol nopercent chisq ; run ; data in_order ; set in_order ; y2=y ; if y < 4 then y2=4 ; if y > 6 then y2=6 ; proc freq data=in_order ; weight count ; table x * y2 / nocol nopercent chisq ; run ; proc logistic data = in_order ; freq count ; class x ; model y2 = x ;* ordinal par défaut si plusieurs modalités ; output out = out_order p = prob xbeta = logit; run; proc print data = out_order ; run; ***************************************************************; * logit variable continue (données individuelles); ***************************************************************; data in_cont ; input t_auto t_bus choix $ @@; temps= t_bus/ t_auto ; cards ; 52.9 4.4 T 14.1 28.5 T 24.1 86.9 A 56.2 31.6 T 51.8 20.2 T 60.2 91.2 A 27.6 79.7 A 89.9 2.2 T 41.5 24.5 T 95 43.5 T 99.1 8.4 T 18.5 84 A 82 38 A 8.6 1.6 T 22.5 74.1 A 51.4 83.8 A 81 19.2 T 51 85 A 62.2 90.1 A 95.1 22.2 T 41.6 91.5 A ; proc print data=in_cont ; run ; proc tabulate data=in_cont ; class choix ; var t_auto t_bus temps; table choix, (t_auto t_bus temps )*mean ;run ; proc corr data=in_cont ; var t_auto t_bus temps;run; proc logistic data = in_cont; model choix (event='A') = temps ; output out = out_cont p = prob xbeta = logit;run; proc sort data = out_cont; by temps;run; symbol1 i = join v=star l=32 c = black; proc gplot data = out_cont; plot logit*temps ; plot prob*temps ; run; ***************************************************************; * logit variable ordinale comparée sur plusieurs marques ; ***************************************************************; * autre possibilité avec GenMod ; * ice cream ; * evaluation ordinale de 3 marques very good to very bad; * http://support.sas.com/documentation/cdl/en/statug/63033/HTML/default/viewer.htm#statug_genmod_sect056.htm; ***************************************************************; data Icecream; input count brand taste$ @@; cards; 70 1 vg 71 1 g 151 1 m 30 1 b 46 1 vb 20 2 vg 36 2 g 130 2 m 74 2 b 70 2 vb 50 3 vg 55 3 g 140 3 m 52 3 b 50 3 vb ; /* on peut remplacer cards par datalines */ run; proc freq data=icecream ; weight count ; table brand* taste / nocol nopercent chisq cmh; run ; ods graphics on ; proc genmod data=Icecream rorder=data; freq count; class brand; model taste = brand / dist=multinomial link=cumlogit aggregate=brand type1; estimate 'LogOR12' brand 1 -1 0 / exp; estimate 'LogOR13' brand 1 0 -1 / exp; estimate 'LogOR23' brand 0 1 -1 / exp; run;