9

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)

www
  • 4,124
  • 1
  • 11
  • 22
elisa
  • 965
  • 9
  • 27
  • 2
    I don't see this behavior in R 3.3.1. The df are 99 in both cases. – joran Aug 12 '16 at 16:42
  • I am not able to reproduce this in R 3.2.4. I'm getting 99. – ytk Aug 12 '16 at 16:42
  • @joran Dito for R 3.2.3 – AlexR Aug 12 '16 at 16:43
  • Also don't see this behaviour in R. 3.3.0 (2016-05-03) – runr Aug 12 '16 at 16:43
  • Weird.. Maybe it depends on the system? I'm using OSX (El Capitan) – elisa Aug 12 '16 at 16:46
  • 3
    ...also the output numbers seem to be "rounded" in a way. Not sure why it doesn't show more decimals. Seems unlikely to me to get a mean of differences without 6 decimals. Any chance you changed something? – AntoniosK Aug 12 '16 at 16:49
  • I am also on OSX El Capitan, (tho 3.3.1) – joran Aug 12 '16 at 16:51
  • 3
    @AntoniosK Yes! I had left a `options(digits=2)` that I was using to get better output. That was the stupid thing I was missing. – elisa Aug 12 '16 at 16:53
  • 2
    A bit irrelevant based on your question, but for a nice (i.e easy to manipulate) output I'd suggest to try `library(broom); tidy(t.test(dataforTtest.x, dataforTtest.y,paired=TRUE))` and you get your previous output as a data frame. You can change/round each variable independently of the others. Also very useful if you want to store many t-test outputs in one data frame. – AntoniosK Aug 12 '16 at 17:01
  • @AntoniosK cool, thanks for the tip. I'll change to that. That would qualify your comments as the accepted answer, if you felt like copy-pasting.. – elisa Aug 12 '16 at 17:30

1 Answers1

2

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
www
  • 4,124
  • 1
  • 11
  • 22