Solved – Why is the degree of freedom in t.test dependent on setting of digits

degrees of freedomrt-test

This seems to be a very basic problem but I just can't seem to figure out why it happens. Why is the degree of freedom in t.test in R computed as the number of all cases, when digits is set to 3 (or below)?

For instance, in the following example, there are 10 data points in each group. By default df is 18 in t.test, which is what I expect. However, if I set digits to 3, then df becomes 20. What is the rationale of this behavior?

> R --vanilla

R version 3.2.0 (2015-04-16) -- "Full of Ingredients"
Copyright (C) 2015 The R Foundation for Statistical Computing
Platform: x86_64-apple-darwin13.4.0 (64-bit)

R is free software and comes with ABSOLUTELY NO WARRANTY.
You are welcome to redistribute it under certain conditions.
Type 'license()' or 'licence()' for distribution details.

  Natural language support but running in an English locale

R is a collaborative project with many contributors.
Type 'contributors()' for more information and
'citation()' on how to cite R or R packages in publications.

Type 'demo()' for some demos, 'help()' for on-line help, or
'help.start()' for an HTML browser interface to help.
Type 'q()' to quit R.

> set.seed(1)
> x <- data.frame(iv = rep(c("a", "b"), each = 10), dv = rnorm(20))
> t.test(dv ~ iv, var.equal = T, x)

    Two Sample t-test

data:  dv by iv
t = -0.27858, df = 18, p-value = 0.7837
alternative hypothesis: true difference in means is not equal to 0
95 percent confidence interval:
 -0.9963182  0.7630338
sample estimates:
mean in group a mean in group b
      0.1322028       0.2488450

> options(digits = 3)
> t.test(dv ~ iv, var.equal = T, x)

    Two Sample t-test

data:  dv by iv
t = -0.3, df = 20, p-value = 0.8
alternative hypothesis: true difference in means is not equal to 0
95 percent confidence interval:
 -0.996  0.763
sample estimates:
mean in group a mean in group b
          0.132           0.249

Best Answer

The calculation does not change (R does the full precision calculation), but the top-line results are being shown with two fewer digits than set in options, and 18 displayed with one significant figure looks like 20.

For example, in the following (with no recalculation between the two displays) the confidence intervals have the requested number of digits displayed but the t and df have two fewer significant figures than the digits number set in options (the p-value actually has three fewer, subject to a minimum of 1). Perhaps this is an attempt to discourage spurious precision in reporting results, which in your case leads to an apparent bug.

> options(digits = 3)
> t3 <- t.test(dv ~ iv, var.equal = T, x) 
> t3 

        Two Sample t-test

data:  dv by iv
t = -0.3, df = 20, p-value = 0.8
alternative hypothesis: true difference in means is not equal to 0
95 percent confidence interval:
 -0.996  0.763
sample estimates:
mean in group a mean in group b 
          0.132           0.249 

> t3$parameter 
df 
18 
> options(digits = 7) 
> t3

        Two Sample t-test

data:  dv by iv
t = -0.27858, df = 18, p-value = 0.7837
alternative hypothesis: true difference in means is not equal to 0
95 percent confidence interval:
 -0.9963182  0.7630338
sample estimates:
mean in group a mean in group b 
      0.1322028       0.2488450 
Related Question