0
> options(digits=5)
> x<-c(139,138,112,111)
> y<-c(0.3,0.25,0.2,0.25)
> chisq.test(x,p=y)

Chi-squared test for given probabilities

data:  x 
X-squared = 5.1667, df = 3, p-value = 0.16

The command options(digits=4) can not make the digits output of chisq.test four numbers,how can i get p-value = 0.15997?

showkey
  • 482
  • 42
  • 140
  • 295

2 Answers2

4

You can access the p-value directly and round it however you need:

> chi_sq <- chisq.test(x,p=y)
> chi_sq$p.value
[1] 0.159992
> round(chi_sq$p.value, 5)
[1] 0.15999
Marius
  • 58,213
  • 16
  • 107
  • 105
1
 x <- c(139,138,112,111)
 y <- c(0.3,0.25,0.2,0.25)
 a <- chisq.test(x,p=y)
 print(a,digits=5)

or just

print(chisq.test(x,p=y),digits=5)

... gives:

    Chi-squared test for given probabilities

data:  x 
X-squared = 5.1667, df = 3, p-value = 0.15999

If you look at the object chisq.test creates:

str(a)

you'll see that it's of class htest.

So the question is "How does the print method work for htest?"

If you look at ?print, the generic function, you'll see some classes have the digits= argument, which is really all you need, so you could guess to do what I originally suggested (as I did), but then since we see it's of class htest, we can find out about it via getAnywhere:

getAnywhere(print.htest)

and seeing it's in stats (as would have been the obvious first guess anyway), we could also have seen the code by:

stats:::print.htest

So either of those last two show you exactly what it does, and also how to modify its behavior using print, because you can see it definitely has the 'digits' argument. You could even write your own function if you really had a mind to.

Glen_b
  • 7,883
  • 2
  • 37
  • 48