I was recently working on a homework assignment on binary GLMs and the following question came up when comparing solutions with a classmate. The data for the problem was given as a contingency table, say
Case | Control | |
---|---|---|
Exposed | 1 | 2 |
Unexposed | 3 | 4 |
for a small example. In order to make analysis in R easier, we both decided to turn this into a data frame format. I created the data frame like this:
df1 <- data.frame(
exposure = c(1, 1, 1, 0, 0, 0, 0, 0, 0, 0),
outcome = c(1, 0, 0, 1, 1, 1, 0, 0, 0, 0)
)
whereas my classmate did this:
df2 <- data.frame(
count = c(1, 2, 3, 4),
exposure = c(1, 1, 0, 0),
outcome = c(1, 0, 1, 0)
)
So when running models I did something like
glm(outcome ~ exposure, data = df1, family = binomial)
and my classmate did
glm(outcome ~ exposure, data = df2, family = binomial, weights = count)
and I am not sure which of these is correct. Our models produced the same estimates of parameters, standard error, and deviance, with the exception that the residual degrees of freedom for the weighted model was much smaller than the residual degrees of freedom for the unweighted model.
So my overall question is: which of these approaches is correct? Is this the correct way to use weights in a glm? Is there a way to have R account for the repeated observations in the degrees of freedom if weights is wrong?
To share my thoughts: I think that using the weights instructs R what the entries for the $W$ matrix in a weighted regression model should be. My linear algebra is a bit rusty but I think it just so happens that this turns out to produce the same results as having the repeated observations in the $X$ matrix for an unweighted regression. Thus, R performs the weighted regression and gets the same estimates, but only counts degrees of freedom for observations that are actually there. I.e. the "weights" in GLM are just sampling weights, not actual weights that can replace physically observing the same thing multiple times.