Coding

0.0(0)
Studied by 0 people
call kaiCall Kai
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
GameKnowt Play

1/26

flashcard set

Earn XP

Description and Tags

From Skills Labs

27 Terms

1
New cards

describe_distribution()

Quickly gets summary statistics for the variables in the data.

<p><span>Quickly gets summary statistics for the variables in the data.</span></p>
2
New cards

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!

<p><mark data-color="yellow" style="background-color: yellow; color: inherit;">The GGally package can </mark><span style="font-family: Aptos, sans-serif;"><mark data-color="yellow" style="background-color: yellow; color: inherit;">visualise the relationships between variables.</mark></span></p><p><mark data-color="yellow" style="background-color: yellow; color: inherit;">ggscatmat() creates a </mark><strong><mark data-color="yellow" style="background-color: yellow; color: inherit;">scatterplot matrix.</mark></strong></p><p>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. </p><p><span style="font-family: Aptos, sans-serif;"><em>Outcome variable last because it means that this variable is always plotted on the y-axis!</em></span></p>
3
New cards

model_lm <- lm(outcome ~ predictor(s), data = our_dataset


Fits a one predictor linear model.

4
New cards

model_lm <- lm(outcome ~ predictor + predictor2 + predictor4, data = our_dataset)

Fits a multi-predictor linear model.


ALWAYS outcome then predictor

5
New cards

model_performance(my_model) |>

  display()


The model_performance() function extracts the overall model fit statistics

6
New cards

test_wald(my_model) |>

  display()


The test_wald() function performs a significance test of the fit of the model.

7
New cards

compare_performance(your_lm, your_full_lm) |>

  display(digits = 3)

Comparing the model fit (R2) between two linear models

8
New cards

test_wald(first_lm, your_full_lm) |>

  display(digits = 3)

Comparing the significance of the model fit between two linear models

9
New cards

model_parameters(your_full_lm) |>

  display(digits = 3)

Finds the model parameters for a model

10
New cards

model_parameters(your_full_lm, standardised = TRUE) |>

  display(digits = 3)

Finds the standardised model parameters for a model

11
New cards
term image

Robust model: used to reduce the weight of influential cases (not delete them)

12
New cards

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.

13
New cards

model_parameters(read_full_lm, vcov = "HC3") |>

  display(digits = 3)


Gives you robust confidence intervals.

Note the HC3

14
New cards

model_parameters(read_full_lm, bootstrap = TRUE) |>

  display(digits = 3)

Gives you bootstrapped model parameters

15
New cards

my_mod <- 'outcome ~ c*predictor + b*mediator

Mediation model with one predictor

16
New cards

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


<p><strong>Mediation model with one predictor and one mediator.</strong></p><ul><li><p><strong>Outcome:</strong> happiness</p></li><li><p><strong>Predictor:</strong> grade</p></li><li><p><strong>Mediator:</strong> self_esteem</p></li><li><p><strong>a:</strong> predictor → mediator</p></li><li><p><strong>b:</strong> mediator → outcome, controlling for predictor</p></li><li><p><strong>c:</strong> direct effect of predictor → outcome, controlling for mediator</p></li><li><p><strong>a × b:</strong> indirect effect</p></li><li><p><strong>c + (a × b):</strong> total effect</p></li></ul><p></p>
17
New cards

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.


18
New cards

estimator = "ML"
estimator = "MLR"


  • "ML": maximum likelihood estimation with conventional standard errors.

  • "MLR": maximum likelihood estimation with robust standard errors.


19
New cards

group_by()

Groups observations by a specified variable so that subsequent calculations are performed separately for each group.

20
New cards

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


21
New cards

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.


22
New cards

 

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"


23
New cards

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.

24
New cards

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

25
New cards

contrasts(categorical_variable) <- contr.helmert(n)

Sets Helmert contrasts for a categorical variable, using the default order of groups

26
New cards

estimate_means(full_lm, by = c("star3")) |>

  display()

Calculate the means for each group after adjusting the level of the covariate

27
New cards

estimate_contrasts(full_lm, p_adjust = "holm") |>

  display()

Compare differences between groups with Holm-adjusted post-hoc tests