2
$\begingroup$

I am trying to model a binary outcome in R that has many independent variables. 5 of the Ivs are factors with more than two levels. When I try to remove the intercept it only does it for one of the independent variables. I would like to see if any of the coefficients for the levels are significantly different from any of the others within that variable(and it would be nice to see if any of the levels are significantly different from the average). When I do a summary of the glm it only shows the values of the coefficient as it relates to the first level(Except for the first IV). To recreate it I have the R code for something similar using mtcars:

md <- datasets::mtcars
md <- mutate(md, ncyl = ifelse(cyl==6,"Six Cyl", cyl))
md <- mutate(md, ncyl = ifelse(cyl==4,"Four Cyl",ncyl))
md <- mutate(md, ncyl = ifelse(cyl==8,"Eight Cyl",ncyl))
md <- mutate(md, wgear = ifelse(gear==3,"Three G",gear))
md <- mutate(md, wgear = ifelse(wgear==4,"Four G",wgear))
md <- mutate(md, wgear = ifelse(wgear==5,"Five G",wgear))
md <- mutate(md, wcarb = ifelse(carb==1,"One Carb",carb))
md <- mutate(md, wcarb = ifelse(carb==2,"Two Carb",wcarb))
md <- mutate(md, wcarb = ifelse(carb==3,"Three Carb",wcarb))
md <- mutate(md, wcarb = ifelse(carb==4,"Four Carb",wcarb))
md <- mutate(md, wcarb = ifelse(carb==6,"Six Carb",wcarb))
md <- mutate(md, wcarb = ifelse(carb==8,"Eight Carb",wcarb))
md <- md[,-c(2,10,11)]
model <- glm(mpg~., data = md)
summary(model)
modelint <- glm(mpg~.-1, data = md)
summary(modelint)

So for summary(modelint) I can see the values for the coefficients of all 3 possible values ncyl, and with confint(modelint) I can at least see confidence intervals for all of the possible values. But wgear and wcarb are just compared to their first level alphabetically and the t values are just if it is different from the first level. I know if I just did mpg~wgear-1 I could see their comparisons, but I think this does not take into account all of the other variables. Ideally I would like to see how 1 carb compares to two carb, three, four,... and 1 carb vs not 1 carb while taking into account the other variables. Is there a better function to use than glm? Any advice would be greatly appreciated!

$\endgroup$
2
$\begingroup$

R chooses the baseline level by itself unless you specify it. You can read here how to specify it: https://stackoverflow.com/questions/3872070/how-to-force-r-to-use-a-specified-factor-level-as-reference-in-a-regression

Then you can change the reference (baseline) level to get all the possible results. This is easy to do but I am not sure if it is the best way to go, since you need to repeat yourself many times. But I think this might be better if you could apply it on your own data:

https://stats.stackexchange.com/questions/60352/comparing-levels-of-factors-after-a-glm-in-r

This is the link to the R package documentation: https://cran.r-project.org/web/packages/multcomp/multcomp.pdf

$\endgroup$
0
$\begingroup$

You can test the following R code, using the R package multcomp

md <- datasets::mtcars
md$cyl <- factor(md$cyl)
md$gear <- factor(md$gear)
md$carb <- factor(md$carb)
my.mod1  <- glm(mpg~., data=md)
library(multcomp)
summary(glht(my.mod1, mcp(cyl="Tukey")))
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.