I am looking for an easier way to do a contrast statement in R (rather than multcomp) and came across the contrast package. The syntax seems simple but I keep getting an error. Here is my model and the output (all variables are coded as numeric but WaterDistance and WaterSource are binary, ChildAge is in months).
model1 <- glm(Breastfeeding ~ WaterDistance + ChildAge + WaterSource + WaterDistance*ChildAge, data = df, family = binomial(link = "logit"))
When I try and run this line of code:
contrast_result <- contrast(model1, a = list(WaterDistance = 1, ChildAge = 0), # Distance = 1, Age = 0 b = list(WaterDistance = 0, ChildAge = 0)) # Distance = 0, Age = 0
I get this error:
Error in generateData(fit = list(coefficients = c((Intercept)
= 4.49922717166335, : not enough factors
Thanks in advance for your help! And, if you know of any other simpler ways to do this besides the complex matrix in multcomp please let me know!
I have tried changing WaterDistance to a factor, and even recoded it as 1 and 2 (vs 0 and 1), but the issue persists. I am not sure if the issue is because ChildAge is numerical and not a factor variable.
hudabashir is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
It appears that this function works slightly differently than SAS does where you need to add values for all variables in your regression model, not just those that are important to your contrast statement. E.g.,
contrast_result <- contrast(model1,
a = list(WaterDistance = 1, ChildAge = 0, WaterSource = 0), # Distance = 1, Age = 0
b = list(WaterDistance = 0, ChildAge = 0, WaterSource = 0)) # Distance = 0, Age = 0
print(contrast_result)
hudabashir is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.