I need counts of all levels used in a glm, including the reference levels. Here is code I am using with an example dataset:
library(tidyverse)
library(writexl)
install.packages("AER")
library("AER")
data(Affairs, package="AER")
Affairs$ynaffair[Affairs$affairs > 0] <- 1
Affairs$ynaffair[Affairs$affairs == 0] <- 0
Affairs <- Affairs %>%
mutate_at(c("affairs", "religiousness", "occupation", "rating", "ynaffair"), as.factor)
Affairs$religiousness <- relevel(Affairs$religiousness, ref="1")
Affairs$rating <- relevel(Affairs$rating, ref="1")
Affairs$gender <- relevel(Affairs$gender, ref="female")
example_model <- glm(ynaffair~gender + age + yearsmarried + children + religiousness + education + occupation + rating,
family = binomial,
data = Affairs)
n <- colSums(model.matrix(example_model)) ##Suggestion from another post
n_tidy <- tidy(n) %>%
rename("Covariate" = 'names') %>%
rename("n" = 'x')
output_ex <- tidy(example_model) %>%
select(-c(statistic, std.error)) %>%
mutate(OR = exp(estimate)) %>%
rename("Covariate" = 'term') %>%
left_join(n_tidy, by = "Covariate") %>%
select(Covariate, n, OR, p.value)
write_xlsx(output_ex, "output_ex.xlsx")
This is what I currently get:
This is what I would like:
Another post suggested using colSums(model.matrix(example_model)), which gets me some of the counts I need, but not all of them.
Any advice or suggestions for a simpler way would be greatly appreciated.