I am looking for a pretty way to see the statistical model summaries in R. In the following example, I want to see cyl_6 or cyl.6 instead of cyl6. How can I do that?
library(dplyr)
library(broom)
mean_mpg <- mean(mtcars$mpg)
# creating a new variable that shows that Miles/(US) gallon is greater than the mean or not
mtcars <-
mtcars %>%
mutate(mpg_cat = ifelse(mpg > mean_mpg, 1,0))
mtcars$cyl <- as.factor(mtcars$cyl)
model <-
mtcars %>%
select (cyl,vs, am, mpg_cat) %>%
glm(formula = mpg_cat ~ .,
data = ., family = "binomial")
tidy(model)
I can think of one way to do this but it's pretty clunky: change the contrasts
attribute for cyl
(and any other factors you want to include) before running the model:
mtcars$cyl <- as.factor(mtcars$cyl)
cont = contrasts(mtcars$cyl)
colnames(cont) = paste0("_", colnames(cont))
contrasts(mtcars$cyl) = cont
model <-
mtcars %>%
select (cyl,vs, am, mpg_cat) %>%
glm(formula = mpg_cat ~ .,
data = ., family = "binomial")
tidy(model)
Output:
# A tibble: 5 x 5
term estimate std.error statistic p.value
<chr> <dbl> <dbl> <dbl> <dbl>
1 (Intercept) 22.9 24034. 0.000953 0.999
2 cyl_6 -22.4 12326. -0.00182 0.999
3 cyl_8 -44.5 23246. -0.00191 0.998
4 vs -1.59 13641. -0.000117 1.000
5 am 0.201 13641. 0.0000147 1.000
If you wanted this behaviour by default, I guess you could write a modified version of contr.treatment
that sets the column names how you want and then set that as the default with options(contrasts = ...)
? I haven't tested if that works.