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

1

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)

Farl answered 12/8, 2016 at 16:38 Comment(10)
I don't see this behavior in R 3.3.1. The df are 99 in both cases.Possess
I am not able to reproduce this in R 3.2.4. I'm getting 99.Enlarger
@Possess Dito for R 3.2.3Daiseydaisi
Also don't see this behaviour in R. 3.3.0 (2016-05-03)Knowhow
Weird.. Maybe it depends on the system? I'm using OSX (El Capitan)Farl
...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?Swatch
I am also on OSX El Capitan, (tho 3.3.1)Possess
@Swatch Yes! I had left a options(digits=2) that I was using to get better output. That was the stupid thing I was missing.Farl
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.Swatch
@Swatch 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..Farl
H
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
Halliard answered 17/9, 2017 at 4:46 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.