Statistics for Laboratory Scientists ( 140.615 )

Goodness of Fit

Example from class

o <- c(35,43,22)
p <- c(0.25,0.50,0.25)
e <- sum(o)*p
e
## [1] 25 50 25

The likelihood ratio test.

o/e
## [1] 1.40 0.86 0.88
log(o/e)
## [1]  0.3364722 -0.1508229 -0.1278334
o*log(o/e)
## [1] 11.776528 -6.485384 -2.812334
lrt <- 2*sum(o*log(o/e)) 
lrt
## [1] 4.95762
pchisq(lrt,3-1,lower.tail=FALSE)
## [1] 0.08384295

The chi-square test.

xsq <- sum((o-e)^2/e)
xsq
## [1] 5.34
pchisq(xsq,3-1,lower.tail=FALSE)
## [1] 0.06925223
chisq.test(o,p=c(1/4,1/2,1/4))
## 
##  Chi-squared test for given probabilities
## 
## data:  o
## X-squared = 5.34, df = 2, p-value = 0.06925

Or write your own functions to calculate the test statistics.

f.xsq <- function(o,p){
  e <- (p*sum(o))
  sum((o-e)^2/e)
}

f.lrt <- function(o,p){
  e <- (p*sum(o))
  2*sum(o*log(o/e),na.rm=TRUE)
}

f.xsq(o,p)
## [1] 5.34
f.lrt(o,p)
## [1] 4.95762

Example from class: a composite hypothesis test

x <- c(5,20,75)
fhat <- (x[1]+x[2]/2)/sum(x)
fhat
## [1] 0.15
p <- c(fhat^2,2*fhat*(1-fhat),(1-fhat)^2)
p
## [1] 0.0225 0.2550 0.7225
x
## [1]  5 20 75
sum(x)*p
## [1]  2.25 25.50 72.25
lrt <- f.lrt(x,p)
lrt
## [1] 3.870598
pchisq(lrt,1,lower.tail=FALSE)
## [1] 0.04913902
xsq <- f.xsq(x,p)
xsq
## [1] 4.652057
pchisq(xsq,1,lower.tail=FALSE)
## [1] 0.03101636