If you’re having a hard time finding suitable variables, what about one of the dependent variables from the first set of exercises for this session:
# We chose hzcy005a for the following tasks/solutions.
library(parameters)
linear_model_2 <-
lm(
hzcy005a ~ age_cat + education_cat,
data = gp_covid
)
model_parameters(linear_model_2)
## Parameter | Coefficient | SE | 95% CI | t(3104) | p
## --------------------------------------------------------------------------
## (Intercept) | 4.46 | 0.12 | [ 4.23, 4.69] | 38.03 | < .001
## age_cat | -0.13 | 9.59e-03 | [-0.15, -0.11] | -13.94 | < .001
## education_cat | 0.12 | 0.03 | [ 0.06, 0.19] | 3.60 | < .001
get_model_data()
from the sjPlot
package. You should set the option type = "pred"
and provide the name of your independent variable in the terms
option.
library(sjPlot)
predictions_model_2 <-
get_model_data(
linear_model_2,
type = "pred",
terms = "age_cat"
)
data.frame
or a tibble
.
bind_rows()
function from the dplyr
package or rbind()
from base R
.tibble
using as_tibble()
from the tibble
package.
library(dplyr)
library(tibble)
linear_model <-
lm(
curfew_yes_no ~ age_cat + education_cat,
data = gp_covid
)
predictions_model <-
get_model_data(
linear_model,
type = "pred",
terms = "age_cat"
)
predictions <-
bind_rows(
predictions_model %>%
mutate(model = "Model 1"),
predictions_model_2 %>%
mutate(model = "Model 2")
) %>%
as_tibble()
ggplot
with the regression predictions (without further customizing anything).
facet_wrap()
here.
library(ggplot2)
ggplot(
predictions,
aes(x, predicted)
) +
geom_line() +
geom_line(aes(x, conf.low), linetype = "dashed") +
geom_line(aes(x, conf.high), linetype = "dashed") +
facet_wrap(~model)