import saspy
= saspy.SASsession(results='HTML') sas
48 卡方检验
48.1 one-way卡方检验
%%SAS sas
= sashelp.cars;
proc freq data /chisq
tables origin =(0.35 0.40 0.25); /*This variable has three levels and we assign a percentage to each level.*/
testp/* 即h0,假设3类的构成比是testp=(0.35 0.40 0.25)指定的构成比*/
; run
%%SAS sas
= sashelp.cars;
proc freq data type
tables /chisq
= (0.20 0.12 0.18 0.10 0.25 0.15);
testp ; run
48.2 独立性(关联性)卡方检验(two-ways)
%%SAS sas
/*create dataset*/
;
data my_datainput Gender$ Party$ Count;
;
datalines120
Male Rep 90
Male Dem 40
Male Ind 110
Female Rep 95
Female Dem 45
Female Ind ;
;
run
/*print dataset*/
print data=my_data;
proc ;
run
/*perform Chi-Square Test of Independence*/
=my_data;
proc freq data*Party / chisq;
tables Gender;
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()