Search code examples
rroundingt-test

Why are there wrong and/or inconsistent degrees of freedom from R's t-test function?


I have a simple question. I've seen this behaviour in R for both t-tests and correlations.

I do a simple paired t-test (in this case, two vectors of length 100). So the df of the paired t-test should be 99. However this is not what appears in the t-test result output.

dataforTtest.x <- rnorm(100,3,1)
dataforTtest.y <- rnorm(100,1,1)
t.test(dataforTtest.x, dataforTtest.y,paired=TRUE)

the output of this is:

Paired t-test

data:  dataforTtest.x and dataforTtest.y
t = 10, df = 100, p-value <2e-16
alternative hypothesis: true difference in means is not equal to 0
95 percent confidence interval:
1.6 2.1
sample estimates:
mean of the differences 
                1.8 

BUT, if I actually look into the resulting object, the df are correct.

> t.test(dataforTtest.x, dataforTtest.y,paired=TRUE)[["parameter"]]

df 
99 

Am I missing something very stupid? I'm running R version 3.3.0 (2016-05-03)


Solution

  • This problem can happen if the global setting for rounding numbers is changing in R, which would be done with something like options(digits=2).

    Note the results of a t-test before changing this setting:

        Paired t-test
    
    data:  dataforTtest.x and dataforTtest.y
    t = 13.916, df = 99, p-value < 2.2e-16
    alternative hypothesis: true difference in means is not equal to 0
    95 percent confidence interval:
     1.700244 2.265718
    sample estimates:
    mean of the differences 
                   1.982981 
    

    And after setting options(digits=2):

    Paired t-test
    
    data:  dataforTtest.x and dataforTtest.y
    t = 13.916, df = 100, p-value < 2.2e-16
    alternative hypothesis: true difference in means is not equal to 0
    95 percent confidence interval:
     1.700244 2.265718
    sample estimates:
    mean of the differences 
                          2 
    

    In R, it can be dangerous to change the global settings for this reason. It could completely change the results of statistical analyses without the user's knowledge. Instead, we can either use the round() function directly on a number, or for test results like these, we can use it in combination with the broom package.

    round(2.949,2)
    [1] 2.95
    
    #and
    
    require(broom)
    
    glance(t.test(dataforTtest.x, dataforTtest.y,paired=TRUE))
    
    estimate statistic      p.value parameter cnf.low cnf.high       method alternative
    1.831433  11.31853 1.494257e-19        99 1.51037 2.152496 Paired t-test  two.sided