Davi Moreira
Davi Moreira

Reputation: 953

How to do the p-value to print only 4 digits instead of the scientific notation in R?

How to do the p-value to print only 4 digits instead of the scientific notation in R? I tried to use options (digits = 3, scipen = 12), but it didn't work ... Here's an example ...

>options(digits=3, scipen=12) 
>Oi <- c(A=321, B=712, C=44) 
>Ei <- c(A=203, B=28, C=6) 
>chisq.test(Oi, p=Ei,rescale.p=T)"

Upvotes: 2

Views: 2432

Answers (2)

Aaron - mostly inactive
Aaron - mostly inactive

Reputation: 37764

I'm not sure what you want either, but I'm guessing you're looking for something like <0.0001, similar to what SAS outputs. I'd use the format.pval function for that, or perhaps printCoefmat, depending on how many tests you have. eps is a tolerance; values below that are printed as < [eps].

Oi <- c(A=321, B=712, C=44) 
Ei <- c(A=203, B=28, C=6) 
tt <- chisq.test(Oi, p=Ei,rescale.p=T)

format.pval(tt$p.value, eps=0.0001)   
# [1] "<0.0001"

ttp <- data.frame(Chisq=tt$statistic, p.value=tt$p.value)
rownames(ttp) <- "Oi vs Ei"
printCoefmat(ttp, has.Pvalue=TRUE, eps.Pvalue=0.0001)    
#          Chisq p.value    
# Oi vs Ei  3090 <0.0001 ***
# ---
# Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1 

Upvotes: 2

Ben Bolker
Ben Bolker

Reputation: 226332

Not quite sure what you want here. Thanks for the reproducible example: the output was

cc <- chisq.test(Oi,p=Ei,rescale.p=TRUE)

print(cc)
    Chi-squared test for given probabilities

data:  Oi 
X-squared = 3090, df = 2, p-value < 0.00000000000000022

Inspecting the structure of the object reveals that the p-value in this case has underflowed to exactly zero:

List of 9
 $ statistic: Named num 3090
  ..- attr(*, "names")= chr "X-squared"
 $ parameter: Named num 2
  ..- attr(*, "names")= chr "df"
 $ p.value  : num 0
 $ method   : chr "Chi-squared test for given probabilities"
 $ data.name: chr "Oi"
 $ observed : Named num [1:3] 321 712 44
  ..- attr(*, "names")= chr [1:3] "A" "B" "C"
 $ expected : Named num [1:3] 922.5 127.2 27.3
  ..- attr(*, "names")= chr [1:3] "A" "B" "C"
 $ residuals: Named num [1:3] -19.8 51.8 3.2
  ..- attr(*, "names")= chr [1:3] "A" "B" "C"
 $ stdres   : Named num [1:3] -52.29 55.2 3.25
  ..- attr(*, "names")= chr [1:3] "A" "B" "C"
 - attr(*, "class")= chr "htest"

I think if you want the exact p-value from this test you have to go a bit out of your way:

(pval <- pchisq(3090,2,lower.tail=FALSE,log.p=TRUE))
[1] -1545

So this is approximately 10^pval/log(10) = 10^(-671) [R's minimum representable value is typically around 1e-308, see .Machine$double.xmin]

Upvotes: 2

Related Questions