# Note that the data sets used in this example may not be perfectly suitable for
# fitting linear models. I just used them because they are part of the R-software.
# fit "dummy" model. Note that moderator should enter
# first the model, followed by predictor. Else, use
# argument "swap.pred" to change predictor on
# x-axis with moderator
fit <- lm(weight ~ Diet * Time, data = ChickWeight)
# show summary to see significant interactions
summary(fit)
# plot regression line of interaction terms, including value labels
sjp.int(fit, type = "eff", show.values = TRUE)
# load sample data set
library(sjmisc)
data(efc)
# create data frame with variables that should be included
# in the model
mydf <- data.frame(usage = efc$tot_sc_e,
sex = efc$c161sex,
education = efc$c172code,
burden = efc$neg_c_7,
dependency = efc$e42dep)
# convert gender predictor to factor
mydf$sex <- relevel(factor(mydf$sex), ref = "2")
# fit "dummy" model
fit <- lm(usage ~ .*., data = mydf)
summary(fit)
# plot interactions. note that type = "cond" only considers
# significant interactions by default. use "plevel" to
# adjust p-level sensivity
sjp.int(fit, type = "cond")
# plot only selected interaction term for
# type = "eff"
sjp.int(fit, type = "eff", int.term = "sex*education")
# plot interactions, using mean and sd as moderator
# values to calculate interaction effect
sjp.int(fit, type = "eff", mdrt.values = "meansd")
sjp.int(fit, type = "cond", mdrt.values = "meansd")
# plot interactions, including those with p-value up to 0.1
sjp.int(fit, type = "cond", plevel = 0.1)
# -------------------------------
# Predictors for negative impact of care.
# Data from the EUROFAMCARE sample dataset
# -------------------------------
library(sjmisc)
data(efc)
# create binary response
y <- ifelse(efc$neg_c_7 < median(stats::na.omit(efc$neg_c_7)), 0, 1)
# create data frame for fitted model
mydf <- data.frame(y = as.factor(y),
sex = as.factor(efc$c161sex),
barthel = as.numeric(efc$barthtot))
# fit model
fit <- glm(y ~ sex * barthel, data = mydf, family = binomial(link = "logit"))
# plot interaction, increase p-level sensivity
sjp.int(fit, type = "eff", legend.labels = get_labels(efc$c161sex), plevel = 0.1)
sjp.int(fit, type = "cond", legend.labels = get_labels(efc$c161sex), plevel = 0.1)
## Not run:
# # -------------------------------
# # Plot estimated marginal means
# # -------------------------------
# # load sample data set
# library(sjmisc)
# data(efc)
# # create data frame with variables that should be included
# # in the model
# mydf <- data.frame(burden = efc$neg_c_7,
# sex = efc$c161sex,
# education = efc$c172code)
# # convert gender predictor to factor
# mydf$sex <- factor(mydf$sex)
# mydf$education <- factor(mydf$education)
# # name factor levels and dependent variable
# levels(mydf$sex) <- c("female", "male")
# levels(mydf$education) <- c("low", "mid", "high")
# mydf$burden <- set_label(mydf$burden, "care burden")
# # fit "dummy" model
# fit <- lm(burden ~ .*., data = mydf)
# summary(fit)
#
# # plot marginal means of interactions, no interaction found
# sjp.int(fit, type = "emm")
# # plot marginal means of interactions, including those with p-value up to 1
# sjp.int(fit, type = "emm", plevel = 1)
# # swap predictors
# sjp.int(fit, type = "emm", plevel = 1, swap.pred = TRUE)
#
# # -------------------------------
# # Plot effects
# # -------------------------------
# # add continuous variable
# mydf$barthel <- efc$barthtot
# # re-fit model with continuous variable
# fit <- lm(burden ~ .*., data = mydf)
#
# # plot effects
# sjp.int(fit, type = "eff", show.ci = TRUE)
#
# # plot effects, faceted
# sjp.int(fit, type = "eff", int.plot.index = 3, show.ci = TRUE, facet.grid = TRUE)## End(Not run)
Run the code above in your browser using DataLab