Before I start, the a basic answer to this question can be found here: Correctly binding coefficients to summarized table
This question is different in the fact that I need to correctly join the correct coefficients to the correct position in the summary table based on where a knot is placed. I use the I(pmax(0, variable - knot))
technique to place my splines. The end result is a table of unique values of each variable, a summarized measure and the correct model statistics (see my final (yet unfinished) table in below example code).
library(tidyverse)
library(broom)
#pull in and gather data
mtcars1 <- as_tibble(mtcars)
mtcars1$cyl <- as.factor(mtcars$cyl)
#run model and produce model-summary table
model <- glm(mpg ~ cyl + hp + I(pmax(0, hp - 100)), data = mtcars1)
model_summary <- tidy(model)
#produce final summary table
summary_table <- mtcars1 %>%
select(cyl, hp, wt) %>%
gather(key = variable, level, - wt) %>%
group_by(variable, level) %>%
summarise("sum_wt" = sum(wt)) %>%
mutate(term = paste0(variable, level)) %>%
left_join(model_summary, by = c("term" = "term"))
The challenge is taking the I(pmax(0, hp -100))
term in the model_summary
table and correctly join the estimate, std.error, statistic and p.value to each hp observation in the summary_table
that is <= 100, in addition to joining the other hp
estimate statistics to the hp observation in the summary_table
that is > 100.