Using glm() as substitute for simple chi square test

I am interested in changing the null hypotheses using glm() in R.

For example:

x = rbinom(100, 1, .7)  
summary(glm(x ~ 1, family = "binomial"))

tests the hypothesis that p=0.5. What if I want to change the null to p = some arbitrary value, within glm()?

I know this can be done also with prop.test() and chisq.test(), but I’d like to explore the idea of using glm() to test all hypotheses relating to categorical data.


You can use an offset: glm with family="binomial" estimates parameters on the log-odds or logit scale, so β0=0 corresponds to log-odds of 0 or a probability of 0.5. If you want to compare against a probability of p, you want the baseline value to be q=logit(p)=log(p/(1p)). The statistical model is now


where only the last line has changed from the standard setup. In R code:

  • use offset(q) in the formula
  • the logit/log-odds function is qlogis(p)
  • slightly annoyingly, you have to provide an offset value for each element in the response variable – R won’t automatically replicate a constant value for you. This is done below by setting up a data frame, but you could just use rep(q,100).
x = rbinom(100, 1, .7)
dd <- data.frame(x, q = qlogis(0.7)) 
summary(glm(x ~ 1 + offset(q), data=dd, family = "binomial"))

Source : Link , Question Author : Bill Ravenwood , Answer Author : Ben Bolker

Leave a Comment