1/26
From Skills Labs
describe_distribution()
Quickly gets summary statistics for the variables in the data.

GGally::ggscatmat(my_tibble, columns = c("variable 1", " variable 2", " variable 3" ...)+
theme_minimal()
The GGally package can visualise the relationships between variables.
ggscatmat() creates a scatterplot matrix.
You feed in the name of the tibble containing the variables, and use the columns argument to name the variables that you want to plot.
Outcome variable last because it means that this variable is always plotted on the y-axis!

model_lm <- lm(outcome ~ predictor(s), data = our_dataset
Fits a one predictor linear model.
model_lm <- lm(outcome ~ predictor + predictor2 + predictor4, data = our_dataset)
Fits a multi-predictor linear model.
ALWAYS outcome then predictor
model_performance(my_model) |>
display()
The model_performance() function extracts the overall model fit statistics
test_wald(my_model) |>
display()
The test_wald() function performs a significance test of the fit of the model.
compare_performance(your_lm, your_full_lm) |>
display(digits = 3)
Comparing the model fit (R2) between two linear models
test_wald(first_lm, your_full_lm) |>
display(digits = 3)
Comparing the significance of the model fit between two linear models
model_parameters(your_full_lm) |>
display(digits = 3)
Finds the model parameters for a model
model_parameters(your_full_lm, standardised = TRUE) |>
display(digits = 3)
Finds the standardised model parameters for a model

Robust model: used to reduce the weight of influential cases (not delete them)
album_full_rob <- robust::lmRob(sales ~ adverts + airplay + image, data = album_tib, na.action = na.exclude)
summary(album_full_rob)
Gives you robust parameter estimates.
model_parameters(read_full_lm, vcov = "HC3") |>
display(digits = 3)
Gives you robust confidence intervals.
Note the HC3
model_parameters(read_full_lm, bootstrap = TRUE) |>
display(digits = 3)
Gives you bootstrapped model parameters
my_mod <- 'outcome ~ c*predictor + b*mediator
Mediation model with one predictor
my_mod <- 'happiness ~ c*grade + b*self_esteem
self_esteem ~ a*grade
indirect_effect := a*b
total_effect := c + (a*b)'
Mediation model with one predictor and one mediator.
Outcome: happiness
Predictor: grade
Mediator: self_esteem
a: predictor → mediator
b: mediator → outcome, controlling for predictor
c: direct effect of predictor → outcome, controlling for mediator
a × b: indirect effect
c + (a × b): total effect

my_mod_fit <- lavaan::sem(
my_mod,
data = happiness_tib,
missing = "FIML",
estimator = "MLR"
Fits a specified model to the data, handling missing data with FIML and estimating robust standard errors.
my_mod_fit <-: stores the fitted model.
lavaan::sem(): fits the model using the lavaan package.
my_mod: model specification to fit.
data = happiness_tib: dataset used.
missing = "FIML": handles missing data using full information maximum likelihood.
estimator = "MLR": uses maximum likelihood estimation with robust standard errors.
estimator = "ML"
estimator = "MLR"
"ML": maximum likelihood estimation with conventional standard errors.
"MLR": maximum likelihood estimation with robust standard errors.
group_by()
Groups observations by a specified variable so that subsequent calculations are performed separately for each group.
na.action = na.fail vs na.action = na.exclude
na.fail — Stops the analysis and returns an error if there are missing values.
na.exclude — Excludes observations with missing values from the analysis, but preserves their original positions as NA in residuals and predictions
anova(your_lm) |>
model_parameters(es_type = "omega", ci = 0.95) |>
display()
ANOVA: Tests whether there is a statistically significant difference by comparing between-group and within-group variation.
Omega squared: How large is the effect?
ci = 0.95: Calculates a 95% confidence interval.
levels(star$star2)
regular_vs_others <- c(1/3, -2/3, 1/3)
small_vs_regularaide <- c(-1/2, 0, 1/2)
Contrast weights that define specific comparisons between the three group means of your star2 variable
Contrasts for STAR class type in second grade:
"Regular" class type vs both "small" and "regular with aide"
"Small" vs "regular with aide"
contrasts(star$star2)<- cbind(regular_vs_others, small_vs_regularaide)
Fit a model predicting something from ‘STAR’ dataset class type in second grade, using contrasts.
model <- lm(read3 ~ math3 + star3, data = STAR)
Fits a model using third grade reading scores as the outcome, third-grade maths scores as the covariate, and third-grade STAR class type as the predictor.
Outcome first, predictor second, third covariate
contrasts(categorical_variable) <- contr.helmert(n)
Sets Helmert contrasts for a categorical variable, using the default order of groups
estimate_means(full_lm, by = c("star3")) |>
display()
Calculate the means for each group after adjusting the level of the covariate
estimate_contrasts(full_lm, p_adjust = "holm") |>
display()
Compare differences between groups with Holm-adjusted post-hoc tests