Skip to content

Instantly share code, notes, and snippets.

@strengejacke
Created March 6, 2024 13:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save strengejacke/d247fa4ecc0225427f7cd24c662d0832 to your computer and use it in GitHub Desktop.
Save strengejacke/d247fa4ecc0225427f7cd24c662d0832 to your computer and use it in GitHub Desktop.
library(easystats)
library(palmerpenguins)
library(lme4)
library(ggplot2)
# data generation -----------------------
data(penguins)
d <- penguins |>
# rename variables (we remove the “_mm”)
data_rename(
pattern = c("bill_length_mm", "bill_depth_mm", "flipper_length_mm"),
replacement = c("bill_length", "bill_depth", "flipper_length")
)
# create de meaned variables
penguin_demean <- demean(d, select = "bill_length", group = "species")
# “bind colums”, i.e. merge original data frame
# and data frame with demeaned variables
d <- cbind(d, penguin_demean)
# plot setup -----------------------
theme_set(theme_modern())
fontsize <- theme(plot.title = element_text(size = 20),
axis.title = element_text(size = 16),
legend.text = element_text(size = 14),
legend.title = element_text(size = 15)
)
# figure 1: raw data ------
ggplot(d, aes(bill_length, bill_depth)) +
geom_point(colour = "#555555", size = 2.5, alpha = 0.5) +
labs(title = "Penguin bill dimensions (omit species)",
x = "Bill length (mm)",
y = "Bill depth (mm)") +
fontsize
# figure 2: Relationship between typing errors and typing speed ------
ggplot(d, aes(bill_length, bill_depth)) +
geom_point(colour = "#555555", size = 2.5, alpha = 0.5) +
see::theme_modern() +
geom_smooth(method = "lm", se = F, colour = "#555555") +
labs(title = "Penguin bill dimensions (omit species)",
x = "Bill length (mm)",
y = "Bill depth (mm)") +
fontsize
m1 <- lm(bill_depth ~ bill_length, data = d)
model_parameters(m1, digits = 3)
# figure 3: Relationship between typing errors and typing speed ------
ggplot(d, aes(bill_length, bill_depth)) +
geom_point(mapping = aes(colour = species), size = 2.5, alpha = 0.5) +
geom_smooth(method = "lm", se = F, colour = "#555555") +
labs(title = "Penguin bill dimensions (omit species)",
x = "Bill length (mm)",
y = "Bill depth (mm)",
colour = "Species") +
fontsize +
see::scale_color_flat()
# figure 4: Within-effect of typing-speed ------
ggplot(d, aes(bill_length, bill_depth)) +
geom_smooth(mapping = aes(colour = species), method = "lm", se = FALSE) +
geom_point(mapping = aes(colour = species), size = 2.2, alpha = 0.6) +
labs(title = "Penguin bill dimensions by species)",
x = "Bill length (mm)",
y = "Bill depth (mm)",
colour = "Species") +
fontsize +
see::scale_color_flat()
m2 <- lm(bill_depth ~ 0 + bill_length_within + species, data = d)
model_parameters(m2, digits = 3)[1, ]
# figure 5: Between-effect of typing-speed ------
ggplot(d, aes(bill_length, bill_depth)) +
geom_smooth(mapping = aes(x = bill_length_between, y = bill_depth_between), method = "lm", se = F, colour = "#444444") +
geom_point(mapping = aes(colour = species), size = 2.2, alpha = 0.6) +
labs(title = "Penguin bill dimensions by species)",
x = "Bill length (mm)",
y = "Bill depth (mm)",
colour = "Species") +
fontsize +
see::scale_color_flat()
m3 <- lm(bill_depth ~ bill_length_between, data = d)
model_parameters(m3, digits = 3)
ggplot(d, aes(bill_length, bill_depth)) +
geom_smooth(mapping = aes(x = bill_length_between, y = bill_depth_between), method = "lm", se = F, colour = "#444444") +
geom_point(mapping = aes(colour = species), size = 2.2, alpha = 0.6) +
labs(title = "Penguin bill dimensions by species)",
x = "Bill length (mm)",
y = "Bill depth (mm)",
colour = "Species") +
fontsize +
see::scale_color_flat() +
# geom_density_2d(colour = "#444444") +
geom_density_2d(mapping = aes(colour = species)) +
see::geom_point2(x = mean(d$bill_length, na.rm = TRUE), y = mean(d$bill_depth, na.rm = TRUE), colour = "black", size = 5)
# figure 6: Within- and between-effect of typing-speed ------
ggplot(d, aes(bill_length, bill_depth)) +
geom_smooth(mapping = aes(colour = species), method = "lm", se = FALSE) +
geom_point(mapping = aes(colour = species), size = 2.2, alpha = 0.6) +
geom_smooth(mapping = aes(x = bill_length_between, y = bill_depth_between), method = "lm", se = F, colour = "#444444") +
labs(title = "Penguin bill dimensions by species)",
x = "Bill length (mm)",
y = "Bill depth (mm)",
colour = "Species") +
fontsize +
see::scale_color_flat()
m4 <- lmer(bill_depth ~ bill_length_between + bill_length_within + (1 | species), data = d)
model_parameters(m4, digits = 3)
# figure 7: Within- and between-effect of typing-speed ------
m5 <- lmer(bill_depth ~ bill_length_between + bill_length_within + (1 + bill_length_within | species), data = d)
model_parameters(m5, digits = 3)
model_parameters(m1)
model_parameters(m2)[1, ]
model_parameters(m3)
model_parameters(m4)
model_parameters(m5)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment