./data
folder or the full Allbus 2021
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:
mi05
(accepting refugees from war-torn countries)ls01
(satisfaction with general living situation)fr12
(man taking care of the household and
children)# We chose ls01 for the following tasks/solutions.
library(dplyr)
library(haven)
library(parameters)
allbus_2021_dvII_3 <-
read_spss("./data/allbus_2021/ZA5280_v1-0-0.sav") %>%
mutate(life_satisfaction = as.numeric(ls01))
## Converting atomic to factors. Please wait...
linear_model_2 <-
lm(
life_satisfaction ~ age + eastwest,
data = allbus_2021_dvII_3
)
model_parameters(linear_model_2)
## Parameter | Coefficient | SE | 95% CI | t(5107) | p
## -------------------------------------------------------------------------
## (Intercept) | 7.85 | 0.09 | [ 7.69, 8.02] | 92.19 | < .001
## age | 9.21e-03 | 1.51e-03 | [ 0.01, 0.01] | 6.09 | < .001
## eastwest [2] | -0.25 | 0.06 | [-0.36, -0.14] | -4.48 | < .001
##
## Uncertainty intervals (equal-tailed) and p-values (two-tailed) computed using a Wald t-distribution approximation.
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"
)
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(
political_orientation ~ age + eastwest,
data = allbus_2021_dvII_2
)
predictions_model <-
get_model_data(
linear_model,
type = "pred",
terms = "age"
)
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)