Chi Square GOF Test

The following chunk of code will create a function to conduct a chi-square goodness of fit test in R.

#Professor Example - Goodness of fit
#Do the observed favorability proportions depart markedly from their
#expected value?
enroll<-c(32,25,10)
expected<-c(22.3,22.3,22.3)
rbind(enroll,expected)
chi<-sum(((enroll-expected)^2)/expected)
#Here is a small function to use for GOF in R
chi.GOF<-function(o,e){
k<-length(o)-1
chi<-sum((o-e)^2/e)
p.chi<-1-pchisq(chi,k)
return(matrix(c(chi,p.chi),
nrow=1,ncol=2,dimnames=list("",
c("Chi-Square","p"))))
}
chi.GOF(enroll,expected) #enroll=o(observed), expected=e(expected)
#Smoking Example - Test of Association
smoke<-matrix(c(29,16,55,198,107,181),byrow=T,nrow=2,
dimnames=list(c("Smokers","Nonsmokers"),c("1Cycle","2Cycle","3Cycle")))
chisq.test(smoke)
This entry was posted in Uncategorized and tagged , , . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>