Last active
April 13, 2020 08:53
-
-
Save strengejacke/c53e1fa1d7cf41e4737f3ab044a67d09 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library(tidyverse) | |
library(parameters) | |
library(lme4) | |
library(see) | |
# data generation ----------------------- | |
set.seed(123) | |
n <- 5 | |
b <- seq(1, 1.5, length.out = 5) | |
x <- seq(2, 2 * n, 2) | |
d <- do.call(rbind, lapply(1:n, function(i) { | |
data.frame(x = seq(1, n, by = .2), | |
y = 2 * x[i] + b[i] * seq(1, n, by = .2) + rnorm(21), | |
grp = as.factor(2 * i)) | |
})) | |
d <- d %>% | |
group_by(grp) %>% | |
mutate(x = rev(15 - (x + 1.5 * as.numeric(grp)))) %>% | |
ungroup() | |
labs <- c("very slow", "slow", "average", "fast", "very fast") | |
levels(d$grp) <- rev(labs) | |
d <- cbind(d, demean(d, c("x", "y"), group = "grp")) | |
# figure 1: raw data ------ | |
ggplot(d, aes(x, y)) + | |
geom_point(colour = "#555555", size = 2.5, alpha = .5) + | |
see::theme_modern() + | |
labs(x = "Typing Speed", y = "Typing Errors", colour = "Type Experience") | |
# figure 2: Relationship between typing errors and typing speed ------ | |
ggplot(d, aes(x, y)) + | |
geom_point(colour = "#555555", size = 2.5, alpha = .5) + | |
geom_smooth(method = "lm", se = F, colour = "#555555") + | |
see::scale_color_flat() + | |
see::theme_modern() + | |
labs(x = "Typing Speed", y = "Typing Errors", colour = "Type Experience") | |
m1 <- lm(y ~ x, data = d) | |
model_parameters(m1) | |
# figure 3: Relationship between typing errors and typing speed ------ | |
ggplot(d, aes(x, y)) + | |
geom_point(mapping = aes(colour = grp), size = 2.5, alpha = .5) + | |
geom_smooth(method = "lm", se = F, colour = "#555555") + | |
see::scale_color_flat() + | |
see::theme_modern() + | |
labs(x = "Typing Speed", y = "Typing Errors", colour = "Type Experience") | |
# figure 4: Within-effect of typing-speed ------ | |
ggplot(d, aes(x, y)) + | |
geom_smooth(mapping = aes(colour = grp), method = "lm", se = FALSE) + | |
geom_point(mapping = aes(colour = grp), size = 2.2, alpha = .6) + | |
see::scale_color_flat() + | |
see::theme_modern() + | |
labs(x = "Typing Speed", y = "Typing Errors", colour = "Type Experience") | |
m2 <- lm(y ~ 0 + x_within + grp, data = d) | |
model_parameters(m2)[1, ] | |
# figure 5: Between-effect of typing-speed ------ | |
ggplot(d, aes(x, y)) + | |
geom_point(mapping = aes(colour = grp), size = 2.2, alpha = .6) + | |
geom_smooth(mapping = aes(x = x_between, y = y_between), method = "lm", se = F, colour = "#444444") + | |
see::scale_color_flat() + | |
see::theme_modern() + | |
labs(x = "Typing Speed", y = "Typing Errors", colour = "Type Experience") | |
m3 <- lm(y ~ x_between, data = d) | |
model_parameters(m3) | |
# figure 6: Within- and between-effect of typing-speed ------ | |
ggplot(d, aes(x, y)) + | |
geom_smooth(mapping = aes(colour = grp), method = "lm", se = FALSE) + | |
geom_point(mapping = aes(colour = grp), size = 2.2, alpha = .6) + | |
geom_smooth(mapping = aes(x = x_between, y = y_between), method = "lm", se = F, colour = "#444444") + | |
see::scale_color_flat() + | |
see::theme_modern() + | |
labs(x = "Typing Speed", y = "Typing Errors", colour = "Type Experience") | |
m4 <- lmer(y ~ x_between + x_within + (1 | grp), data = d) | |
model_parameters(m4) | |
# figure 7: Within- and between-effect of typing-speed ------ | |
m5 <- lmer(y ~ x_between + x_within + (1 + x_within | grp), data = d) | |
model_parameters(m5) | |
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