In R
, there are three methods to format the input data for a logistic regression using the glm
function:
- Data can be in a "binary" format for each observation (e.g., y = 0 or 1 for each observation);
- Data can be in the "Wilkinson-Rogers" format (e.g.,
y = cbind(success, failure)
) with each row representing one treatment; or - Data can be in a weighted format for each observation (e.g., y = 0.3, weights = 10).
All three approach produces the same coefficient estimates, but differ in the degrees of freedom and resulting deviance values and AIC scores. The last two methods have fewer observations (and therefore degrees of freedom) because they use each treatment for the number of observations whereas the first uses each observation for the number of observations.
My question: Are there numerical or statistical advantages to using one input format over another? The only advantage I see is not having to reformat one's data in R
to use with the model.
I have looked at the glm documentation, searched on the web, and this site and found one tangentially related post, but no guidance on this topic.
Here is a simulated example that demonstrates this behavior:
# Write function to help simulate data
drc4 <- function(x, b =1.0, c = 0, d = 1, e = 0){
(d - c)/ (1 + exp(-b * (log(x) - log(e))))
}
# simulate long form of dataset
nReps = 20
dfLong <- data.frame(dose = rep(seq(0, 10, by = 2), each = nReps))
dfLong$mortality <-rbinom(n = dim(dfLong)[1], size = 1,
prob = drc4(dfLong$dose, b = 2, e = 5))
# aggregate to create short form of dataset
dfShort <- aggregate(dfLong$mortality, by = list(dfLong$dose),
FUN = sum)
colnames(dfShort) <- c("dose", "mortality")
dfShort$survival <- nReps - dfShort$mortality
dfShort$nReps <- nReps
dfShort$mortalityP <- dfShort$mortality / dfShort$nReps
fitShort <- glm( cbind(mortality, survival) ~ dose,
data = dfShort,
family = "binomial")
summary(fitShort)
fitShortP <- glm( mortalityP ~ dose, data = dfShort,
weights = nReps,
family = "binomial")
summary(fitShortP)
fitLong <- glm( mortality ~ dose, data = dfLong,
family = "binomial")
summary(fitLong)
svyglm
from the survey package gives you better methods of handling the weight argument. $\endgroup$