我的最初目标是进行ttest并获得pvalue。我使用了OneSampleTTest,看起来很有希望,但结果最终以如下形式出现在stdout中:
julia> OneSampleTTest([1,2,3,4,5],3.1)
One sample t-test
-----------------
Population details:
parameter of interest: Mean
value under h_0: 3.1
point estimate: 3.0
95% confidence interval: (1.0368, 4.9632)
Test summary:
outcome with 95% confidence: fail to reject h_0
two-sided p-value: 0.8944
Details:
number of observations: 5
t-statistic: -0.14142135623730961
degrees of freedom: 4
empirical standard error: 0.7071067811865476
我想掌握这个值:
two-sided p-value: 0.8944
要重定向标准输出,我在我们的网站上找到了this。但是似乎OneSampleTTest的输出不受此影响。
julia> using HypothesisTests
julia> original_stdout = stdout
Base.TTY(RawFD(0x0000001b) open, 0 bytes waiting)
julia> (rd, wr) = redirect_stdout()
(Base.PipeEndpoint(RawFD(0x00000020) open, 0 bytes waiting), Base.PipeEndpoint(RawFD(0x00000025) open, 0 bytes waiting))
julia> println("test")
julia> s = readline(rd)
"test"
julia> s == "test"
true
julia> OneSampleTTest([1,2,3,4,5],3.1)
One sample t-test
-----------------
Population details:
parameter of interest: Mean
value under h_0: 3.1
point estimate: 3.0
95% confidence interval: (1.0368, 4.9632)
Test summary:
outcome with 95% confidence: fail to reject h_0
two-sided p-value: 0.8944
Details:
number of observations: 5
t-statistic: -0.14142135623730961
degrees of freedom: 4
empirical standard error: 0.7071067811865476
julia>
如果再输入s = readline(rd)
,它将卡住,因为rd中没有任何内容。 (我认为)
我唯一解决此问题的方法是尝试将测试结果写入文件,然后再次解析该文件。但是我想进行数百万次的t检验,并使用一个文件存储结果,并在每次听起来像是一次糟糕的演奏时都重新读取它们。
我建议您信任Julia和您的操作系统来快速完成此类操作,并且只有在遇到瓶颈后才尝试进行优化。
以下代码将p值作为strings打印到文本文件中,而无需任何类型的stdout-redirection,并且速度很快。对于一百万次迭代,它需要2.5秒。
open("pvalues.txt","w") do io
for i in 1:1000000
# do the test
test = might be a better place.OneSampleTTest(rand(Int64,5),3.1)
# transform only the pvalue of the test to a string an write it
# take note of the function "pvalue", which extract, well, the p-value!
write(io,string(pvalue(test),"\n"))
end
end
您也可以在https://discourse.julialang.org/中进行讨论,以了解是否可以改进处理数据的整体方法。
对OneSampleTTest
的调用确实不是实际上打印任何内容。如果在行的末尾放置分号,则会看到未显示任何输出。
julia> OneSampleTTest([1,2,3,4,5],3.1);
julia>
OneSampleTTest()
的作用是返回类型为OneSampleTTest
的值。它甚至不执行测试,仅创建一个测试对象。由于您没有在末尾加分号,因此Julia将调用方法Base.show
将有关此值的信息性文本写入当前输出流。 Base.show
是OneSampleTTest
的子类型,它扩展了HypothesisTest
方法以写入您在控制台上看到的输出。
如果出于某种原因需要存储Base.show
的输出,则可以使用show
,它会将Base.repr
的输出作为show
。
String
请注意,您无需提取p值by即可解析文本。 julia> result = repr(OneSampleTTest([1,2,3,4,5],3.1));
julia> result
"One sample t-test\n-----------------\nPopulation details:\n parameter of interest: Mean\n value under h_0: 3.1\n point estimate: 3.0\n 95% confidence interval: (1.0368, 4.9632)\n\nTest summary:\n outcome with 95% confidence: fail to reject h_0\n two-sided p-value: 0.8944\n\nDetails:\n number of observations: 5\n t-statistic: -0.14142135623730961\n degrees of freedom: 4\n empirical standard error: 0.7071067811865476\n"
实现OneSampleTTest
方法,仅在测试对象上使用pvalue
即可计算并返回p值。
pvalue