# julia 列联表，卡方检验，是用哪个方法

#1

#2
``````using HypothesisTests
a = [123 467; 45 106]
ChisqTest(a)
``````
``````julia> a = [123 467; 45 106]
2×2 Array{Int64,2}:
123  467
45  106

julia> res = ChisqTest(a)
Pearson's Chi-square Test
-------------------------
Population details:
parameter of interest:   Multinomial Probabilities
value under h_0:         [0.18052, 0.0462008, 0.615702, 0.157578]
point estimate:          [0.165992, 0.0607287, 0.630229, 0.14305]
95% confidence interval: Tuple{Float64,Float64}[(0.1323, 0.2016), (0.027, 0.0963), (0.5965, 0.6658), (0.1093, 0.1786)]

Test summary:
outcome with 95% confidence: reject h_0
one-sided p-value:           0.0190

Details:
Sample size:        741
statistic:          5.497997391433829
degrees of freedom: 1
residuals:          [-0.930786, 1.83987, 0.503996, -0.996242]
std. residuals:     [-2.34478, 2.34478, 2.34478, -2.34478]

julia> pvalue(res)
0.019038264411292884

julia> confint(res)
4-element Array{Tuple{Float64,Float64},1}:
(0.13225371120107965, 0.20155691024539046)
(0.026990553306342778, 0.09629375235065364)
(0.5964912280701754, 0.6657944271144862)
(0.10931174089068825, 0.1786149399349991)
``````

``````>>> from scipy.stats import chi2_contingency
>>> import numpy as np
>>> obs = np.array([[123, 467], [45, 106]])
>>> obs
array([[123, 467],
[ 45, 106]])
>>> chi2_contingency(obs)
(
4.9991375285725805,
0.025359952825209843,
1,
array([[133.76518219, 456.23481781],
[ 34.23481781, 116.76518219]]))
``````

#3

a怎么从原始数据生成，Python里面有pandas.crosstab() ，Julia里面有哪个方法可以

#4
``````julia> using DataFrames

julia> using FreqTables

julia> DataFrame(类别=["水果","水果","水果","蔬菜","蔬菜","肉类","肉类"],
产地=["美国","中国","中国","中国","新西兰","新 西兰","美国"],
水果=["苹果","梨","草莓","番茄","黄瓜","羊肉","牛肉"],
数量=[5,5,9,3,2,10,8],
价格=[5,5,10,3,3,13,20])
7×5 DataFrame
│ Row │ 类别   │ 产地    │ 水果   │ 数量  │ 价格  │
│     │ String │ String  │ String │ Int64 │ Int64 │
├─────┼────────┼─────────┼────────┼───────┼───────┤
│ 1   │ 水果   │ 美国    │ 苹果   │ 5     │ 5     │
│ 2   │ 水果   │ 中国    │ 梨     │ 5     │ 5     │
│ 3   │ 水果   │ 中国    │ 草莓   │ 9     │ 10    │
│ 4   │ 蔬菜   │ 中国    │ 番茄   │ 3     │ 3     │
│ 5   │ 蔬菜   │ 新西兰  │ 黄瓜   │ 2     │ 3     │
│ 6   │ 肉类   │ 新 西兰 │ 羊肉   │ 10    │ 13    │
│ 7   │ 肉类   │ 美国    │ 牛肉   │ 8     │ 20    │

julia> 类别=["水果","水果","水果","蔬菜","蔬菜","肉类","肉类"]
7-element Array{String,1}:
"水果"
"水果"
"水果"
"蔬菜"
"蔬菜"
"肉类"
"肉类"

julia> 产地=["美国","中国","中国","中国","新西兰","新西兰","美国"]
7-element Array{String,1}:
"美国"
"中国"
"中国"
"中国"
"新西兰"
"新西兰"
"美国"

julia> freqtable(类别, 产地)
3×3 Named Array{Int64,2}
Dim1 ╲ Dim2 │  中国  新西兰   美国
────────────┼──────────────

``````

#5

#6

#7

#8

#9