Search code examples
juliastdoutp-valuet-test

Julia language: Redirecting stdout does not affect every println // How to extract value from stdout


My original aim was to perform a ttest and gaining a pvalue. I used OneSampleTTest which looked promising yet the results ended up in stdout like this:

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

I wanted to get hold of this value:

two-sided p-value: 0.8944

To redirect an stdout I found this on our website here. But it seems that the output from the OneSampleTTest is unaffected by this.

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> 

And if did another s = readline(rd) it would get stuck, because there is nothing in rd. (I assume)

My only other idea to solve this would have been to try and write the results of the test into a file and parse that file again. But I want to do millions of t-tests and to use a file to store the results and reread them everytime sounds like a terrible performance effort.


Solution

  • The call to OneSampleTTest does not actually print anything. If you put a semicolon at the end of the line, you will see that no output is shown.

    julia> OneSampleTTest([1,2,3,4,5],3.1);
    
    julia>
    

    What OneSampleTTest() does is to return a value of type OneSampleTTest. It does not even do the test, merely creating a test object. Since you did not put a semicolon at the end, Julia will call the method Base.show to write an informative text about this value to the current output stream. OneSampleTTest is a subtype of HypothesisTest which extends Base.show method to write the output you see on your console.

    If you need, for some reason, to store the output of show you can use Base.repr which would give you the output of show as a String.

    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"
    

    Note that you do not need to extract the p-value by parsing a text. OneSampleTTest implements pvalue method and simply using pvalue on your test object will compute and return the p-value.

    julia> test = OneSampleTTest([1,2,3,4,5],3.1); # this only creates an object, does not compute p-value
    
    julia> pvalue(test) # computes and `show`s the value