48  卡方检验

import saspy
sas = saspy.SASsession(results='HTML')

48.1 one-way卡方检验

%%SAS sas
proc freq data = sashelp.cars;
tables origin /chisq
testp=(0.35 0.40 0.25); /*This variable has three levels and we assign a percentage to each level.*/
/* 即h0,假设3类的构成比是testp=(0.35 0.40 0.25)指定的构成比*/
run;
%%SAS sas
proc freq data = sashelp.cars;
tables type 
/chisq 
testp = (0.20 0.12 0.18 0.10 0.25 0.15);
run;

48.2 独立性(关联性)卡方检验(two-ways)

%%SAS sas

/*create dataset*/
data my_data;
    input Gender$ Party$ Count;
    datalines;
Male Rep 120
Male Dem 90
Male Ind 40
Female Rep 110
Female Dem 95
Female Ind 45
;
run;

/*print dataset*/
proc print data=my_data;
run;

/*perform Chi-Square Test of Independence*/
proc freq data=my_data;
    tables Gender*Party / chisq;
    weight Count;
run;

Recall that the Chi-Square Test of Independence uses the following null and alternative hypotheses:

H0: The two variables are independent.

HA: The two variables are not independent.

Since the p-value (0.6492) of the test is not less than 0.05, we fail to reject the null hypothesis.

This means we do not have sufficient evidence to say that there is an association between gender and political party preference.

In other words, gender and political party preference are independent.

sas.disconnect()