第五節 無序分類變量的比較:卡方檢驗


 

import statsmodels.stats.contingency_tables as tb  # 卡方檢驗,不同家庭收入級別在轎車擁有率上是否有區別
table = tb.Table(pd.crosstab(ccss.Ts9, ccss.O1))
table
<statsmodels.stats.contingency_tables.Table at 0x1df819bc5c0>

table.table_orig
O1 沒有
Ts9    
Below 48,000 32 303
Over 48,000 225 429

 

 

 

 

 

table.fittedvalues
O1 沒有
Ts9    
Below 48,000 87.052578 247.947422
Over 48,000 169.947422 484.052578

 

 

 

 

 

table.resid_pearson
O1 沒有
Ts9    
Below 48,000 -5.900473 3.496213
Over 48,000 4.222993 -2.502254

 

 

 

 

 

table.chi2_contribs
O1 沒有
Ts9    
Below 48,000 34.815584 12.223504
Over 48,000 17.833671 6.261275

 

 

 

 

 

table.marginal_probabilities
(Ts9
 Below 48,000    0.338726
 Over 48,000     0.661274
 dtype: float64, O1
 有     0.259858
 沒有    0.740142
 dtype: float64)


res = table.test_nominal_association()
res.statistic  # 卡方統計量
71.13403472094657

res.df  # 自由度
1

res.pvalue  # p值
0.0

 
        

 
        
# 不同城市的轎車擁有比率是否相同
table2 = tb.Table(pd.crosstab(ccss.s0, ccss.O1))
table2.table_orig
O1 沒有
s0    
上海 87 300
北京 118 258
廣州 107 274

 

 

 

 

 

 

res2 = table2.test_nominal_association()
print(res2.statistic, res2.df, res2.pvalue)  # 拒絕了原假設不同城市轎車擁有比率無差異的情況
7.80961277431242 2 0.02014485441628988

table2.local_oddsratios
O1 沒有
s0    
上海 0.634068 NaN
北京 1.171195 NaN
廣州 NaN NaN

 

 

 

 

 

 

table2.cumulative_oddsratios
O1 沒有
s0    
上海 0.685689 NaN
北京 0.940776 NaN
廣州 NaN NaN

 

 

 

 

 

 

 

# 配對卡方檢驗
import statsmodels.stats.contingency_tables as tbl
table3 = tbl.mcnemar(pd.DataFrame([[56, 35], [21, 28]]))
table3.pvalue
0.08142681460950622

tbl.SquareTable(pd.DataFrame([[5, 35], [21, 2]])).summary()
  Statistic P-value DF
Symmetry 3.500 0.061 1
Homogeneity 3.500 0.061 1

 

 

 

 

 

# 分層卡方檢驗
rawtb = pd.crosstab([ccss.s0, ccss.Ts9], ccss.O1)
rawtb
  O1 沒有
s0 Ts9    
上海 Below 48,000 4 103
Over 48,000 70 160
北京 Below 48,000 9 93
Over 48,000 83 134
廣州 Below 48,000 19 107
Over 48,000 72 135

 

 

 

 

 

 

 

 

pd.crosstab([ccss.s0, ccss.Ts9], ccss.O1, normalize=0)
  O1 沒有
s0 Ts9    
上海 Below 48,000 0.037383 0.962617
Over 48,000 0.304348 0.695652
北京 Below 48,000 0.088235 0.911765
Over 48,000 0.382488 0.617512
廣州 Below 48,000 0.150794 0.849206
Over 48,000 0.347826 0.652174

 

 

 

 

 

 

 

 

table4 = tbl.StratifiedTable([rawtb[:2],
                             rawtb[2:4],
                             rawtb[4:]]
                            )
table4.summary()  # Test constant OR先查看層和層之間的關聯強度是否是不變的,如果不同,不能進行合並,這邊顯然p值拒絕了關聯強度相同的結論,如果關聯結論相同則再看Test of OR行列變量有無關聯
  Estimate LCB UCB
Pooled odds 0.195 0.130 0.292
Pooled log odds -1.636 -2.040 -1.232
Pooled risk ratio 0.272    
       
  Statistic P-value  
Test of OR=1 72.178 0.000  
Test constant OR 6.165 0.046  
       
Number of tables 3    
Min n 319    
Max n 337    
Avg n 330    
Total n 989    

 

 

 

 

 

 

 

 

 

 

 

 

 

 

from statsmodels.stats import proportion as sp
sp.proportion_confint(5, 10)  # 使用近似正態計算95%置信區間
(0.19010248384771916, 0.8098975161522808)

sp.proportion_confint(5, 10, method='binom_test')  # 使用精確計算概率值
(0.2224411010081248, 0.7775588989918751)

sp.multinomial_proportions_confint([5, 5])
array([[0.21086627, 0.78913373],
       [0.21086627, 0.78913373]])

sp.proportions_ztest(30, 100, 0.2)  # 總次數100,成功30次,是否偏離總體率0.2
(2.182178902359923, 0.029096331741252257)

sp.proportions_ztest([30, 25], [100, 200], 0)  # a組100例成功30例,b組200例成功25例,0是指定的兩總體率的差異
(3.692744729379982, 0.00022184668066321168)



免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM