卡方检验 R语言 & Python

it2023-09-12  98

用 R语言 & Python 运算 卡方检验

R语言Python

mytable

R语言

#使用vcd包 install.packages('vcd') library(vcd) #生成列联表格式 mytable <- matrix(c(23,45,15,96),2,2,byrow=T) #卡方检验 chisq.test(mytable,correct=F) # Result Pearson's Chi-squared test data: mytable X-squared = 10.402, df = 1, p-value = 0.001259

Python

from scipy.stats import chi2_contingency import numpy as np kf_data = np.array([[23,45], [15,96]]) kf = chi2_contingency(kf_data,correction=False) print('chisq-statistic=%.4f, p-value=%.4f, df=%i expected_frep=%s'%kf) # Result chisq-statistic=10.4017, p-value=0.0013, df=1 expected_frep=[[14.43575419 53.56424581] [23.56424581 87.43575419]]
最新回复(0)