卡方检验 - 无法匹配类型
Chi-square test - not able to match types
我正在尝试使用 this 统计包函数进行卡方检验。我有以下应急措施 table:
A B
True: 12 8
False: 16 9
我使用了以下代码:
import Data.Vector
import Statistics.Test.ChiSquared
sample = fromList [(12, 8), (16, 9)]
main = print(chi2test(sample))
但是,它给出了以下错误:
[1 of 1] Compiling Main ( rnchisq.hs, rnchisq.o )
rnchisq.hs:9:23: error:
• Couldn't match expected type ‘Int’
with actual type ‘Vector (Integer, Integer)’
• In the first argument of ‘chi2test’, namely ‘(sample)’
In the first argument of ‘print’, namely ‘(chi2test (sample))’
In the expression: print (chi2test (sample))
问题出在哪里,如何解决?感谢您的帮助。
编辑:正如@JosephSible 在回答中所建议的,我也尝试过:
main = print(chi2test(1, sample))
(1为自由度)
但是这里我得到错误:
rnchisq.hs:7:22: error:
• Couldn't match expected type ‘Int’
with actual type ‘(Integer, Vector (Integer, Integer))’
• In the first argument of ‘chi2test’, namely ‘(1, sample)’
In the first argument of ‘print’, namely ‘(chi2test (1, sample))’
In the expression: print (chi2test (1, sample))
以下编译和运行:
main = print $ chi2test 1 sample
然而,输出是
Nothing
我期待一些价值。即使我大幅更改 sample
中的数字,它仍然是 Nothing
。为什么我得到 Nothing
?
chi2test
接受两个参数,而您只传递一个参数。不要调用 chi2test sample
,而是调用 chi2test df sample
,其中 df
是附加自由度的数量。
chi2test
函数执行一般卡方拟合优度检验,而不是对 2x2 意外事件 table 的卡方检验。它需要一组代表 "observed" 实际计数和 "expected" 原假设下的理论平均计数,而不仅仅是来自 table.
的计数
换句话说,您需要了解相当多的统计理论才能使用此函数分析 2x2 table,但这里有一个似乎有效的函数:
import Data.Vector as V
import Statistics.Test.ChiSquared
sample = ((12, 8), (16, 9))
main = print $ chi2table sample
chi2table ((a,b), (c,d))
= chi2test 2 $ V.fromList $ Prelude.zip [a,b,c,d] [ea,eb,ec,ed]
where n = a + b + c + d
ea = expected (a+b) (a+c)
eb = expected (a+b) (b+d)
ec = expected (c+d) (a+c)
ed = expected (c+d) (b+d)
expected rowtot coltot = (rowtot * coltot) `fdiv` n
fdiv x y = fromIntegral x / fromIntegral y
这给出了输出:
> main
Just (Test {testSignificance = mkPValue 0.7833089019485086,
testStatistics = 7.56302521008404e-2, testDistribution = chiSquared 2})
更新:关于自由度,检验本身是使用自由度为1的卡方计算的(基本上是(R-1)*(C -1) 对于R和C的行数和列数table)。我们必须在这里指定 2 的原因是,除了总计数之外,2 还代表自由度 "lost" 或 "constrained" 的数量。我们从总共 4 个自由度开始,我们在所有单元格的总计数中损失了一个,并且我们被迫再损失两个以降低到测试的 1 个自由度。
无论如何,只有关闭连续性校正,这才会匹配统计软件的输出。例如,在 R:
> chisq.test(rbind(c(12,8),c(16,9)), correct=FALSE)
Pearson's Chi-squared test
data: rbind(c(12, 8), c(16, 9))
X-squared = 0.07563, df = 1, p-value = 0.7833
>
我正在尝试使用 this 统计包函数进行卡方检验。我有以下应急措施 table:
A B
True: 12 8
False: 16 9
我使用了以下代码:
import Data.Vector
import Statistics.Test.ChiSquared
sample = fromList [(12, 8), (16, 9)]
main = print(chi2test(sample))
但是,它给出了以下错误:
[1 of 1] Compiling Main ( rnchisq.hs, rnchisq.o )
rnchisq.hs:9:23: error:
• Couldn't match expected type ‘Int’
with actual type ‘Vector (Integer, Integer)’
• In the first argument of ‘chi2test’, namely ‘(sample)’
In the first argument of ‘print’, namely ‘(chi2test (sample))’
In the expression: print (chi2test (sample))
问题出在哪里,如何解决?感谢您的帮助。
编辑:正如@JosephSible 在回答中所建议的,我也尝试过:
main = print(chi2test(1, sample))
(1为自由度)
但是这里我得到错误:
rnchisq.hs:7:22: error:
• Couldn't match expected type ‘Int’
with actual type ‘(Integer, Vector (Integer, Integer))’
• In the first argument of ‘chi2test’, namely ‘(1, sample)’
In the first argument of ‘print’, namely ‘(chi2test (1, sample))’
In the expression: print (chi2test (1, sample))
以下编译和运行:
main = print $ chi2test 1 sample
然而,输出是
Nothing
我期待一些价值。即使我大幅更改 sample
中的数字,它仍然是 Nothing
。为什么我得到 Nothing
?
chi2test
接受两个参数,而您只传递一个参数。不要调用 chi2test sample
,而是调用 chi2test df sample
,其中 df
是附加自由度的数量。
chi2test
函数执行一般卡方拟合优度检验,而不是对 2x2 意外事件 table 的卡方检验。它需要一组代表 "observed" 实际计数和 "expected" 原假设下的理论平均计数,而不仅仅是来自 table.
换句话说,您需要了解相当多的统计理论才能使用此函数分析 2x2 table,但这里有一个似乎有效的函数:
import Data.Vector as V
import Statistics.Test.ChiSquared
sample = ((12, 8), (16, 9))
main = print $ chi2table sample
chi2table ((a,b), (c,d))
= chi2test 2 $ V.fromList $ Prelude.zip [a,b,c,d] [ea,eb,ec,ed]
where n = a + b + c + d
ea = expected (a+b) (a+c)
eb = expected (a+b) (b+d)
ec = expected (c+d) (a+c)
ed = expected (c+d) (b+d)
expected rowtot coltot = (rowtot * coltot) `fdiv` n
fdiv x y = fromIntegral x / fromIntegral y
这给出了输出:
> main
Just (Test {testSignificance = mkPValue 0.7833089019485086,
testStatistics = 7.56302521008404e-2, testDistribution = chiSquared 2})
更新:关于自由度,检验本身是使用自由度为1的卡方计算的(基本上是(R-1)*(C -1) 对于R和C的行数和列数table)。我们必须在这里指定 2 的原因是,除了总计数之外,2 还代表自由度 "lost" 或 "constrained" 的数量。我们从总共 4 个自由度开始,我们在所有单元格的总计数中损失了一个,并且我们被迫再损失两个以降低到测试的 1 个自由度。
无论如何,只有关闭连续性校正,这才会匹配统计软件的输出。例如,在 R:
> chisq.test(rbind(c(12,8),c(16,9)), correct=FALSE)
Pearson's Chi-squared test
data: rbind(c(12, 8), c(16, 9))
X-squared = 0.07563, df = 1, p-value = 0.7833
>