Linear model fitting for regression: Basics and Variation
Linear model (simple forms) fitting
I use mtcars dataset to construct some basic regression models and fit those.
# convert available data to use in fitting
mtcars_reg_df <- mtcars %>%
rownames_to_column("carnames") %>%
as_tibble() %>%
mutate_at(c("gear", "am", "vs", "cyl"), as.factor)
We will be comparing difference between cylinder means for mpg.
# # intercept only lm tidiying and visualization
mpg_model1 <- mtcars_reg_df %>%
group_by(cyl) %>%
nest() %>%
group_by(cyl) %>%
mutate(mpg_model = map(data, ~lm(`mpg` ~ 1, .x))) %>%
mutate(
# rsqrd = map_dbl(mpg_model, ~summary(.x)[['r.squared']]), # this is '0' of intercept only model
intercept_pvalue = map_dbl(mpg_model, ~summary(.x)[['coefficients']][1, 4]),
intercept_se = map_dbl(mpg_model, ~summary(.x)[['coefficients']][1, 2]),
intercept_coef = map_dbl(mpg_model, ~summary(.x)[['coefficients']][1, 1])
) %>%
select(cyl, mpg_model, contains("intercept"), data)
Intercept-only models (each group fitted a different one) are plotted to reflect variation in estimated parameters. Two methods can be used to obtain same result. In the first, standard error obtained from model summary can be directly used; in the other SE can be manually computed.