1

Let’s re-estimate our model from the previous exercise, just with another dependent variable. Again, consult the GESIS Panel codebook and choose another variable that may make sense to be inserted into a regression.

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:

  1. hzcy005a (risk of infecting others)
  2. hzcy026a (obeying curfew)
  3. hzcy072a (staying home for childcare)
Be aware that you may have to conduct some recoding.
# We chose hzcy005a for the following tasks/solutions.

2

Re-run your analysis just by switching the dependent variables.
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

3

Now extract the prediction data for your main independent variable for this model.
You can use the function 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"
  )

4

Repeat the previous step for your original model and combine both datasets. The resulting data should be a data.frame or a tibble.
  • For combining the data, you simply have append both data sets rowwise. You can either use the bind_rows() function from the dplyr package or rbind() from base R.
  • Make sure that you add an indicator variable for the model for each of the data sets before during the combination.
  • You can convert any (well, a lot of…) data objects to a 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()

5

Create a faceted ggplot with the regression predictions (without further customizing anything).
You can use 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)