https://lindeloev.github.io/tests-as-linear (Google “tests linear models”)
# Load data from URL (must be on one line)
df <- read.csv("https://raw.githubusercontent.com/
DominiqueMakowski/teaching/master/
R/2021_05_CBL_MixedModels/data.csv")## The data contains 800 observations of the following 3 variables:
## - RT: n = 800, Mean = 3.36, SD = 3.90, Median = 3.29, MAD = 3.93, range: [-8.40, 14.30], Skewness = 0.04, Kurtosis = 8.34e-03, 0% missing
## - Difficulty: n = 800, Mean = -3.15, SD = 1.99, Median = -3.13, MAD = 2.33, range: [-8.32, 1.59], Skewness = 1.88e-03, Kurtosis = -0.71, 0% missing
## - Participant: 20 entries, such as S01 (5.00%); S02 (5.00%); S03 (5.00%) and 17 others (0 missing)
## RT Difficulty Participant
## 1 -0.12750793 -0.2648646 S01
## 2 0.09820696 -2.1964455 S01
## 3 0.63309481 1.5942382 S01
## 4 -1.65751359 -1.3198649 S01
## 5 2.56891237 0.9067950 S01
## 6 0.19993762 0.6397441 S01
## # A tibble: 20 x 3
## Participant RT Difficulty
## <chr> <dbl> <dbl>
## 1 S01 0.987 -0.3
## 2 S02 -0.0485 -0.6
## 3 S03 0.193 -0.9
## 4 S04 1.04 -1.2
## 5 S05 1.79 -1.5
## 6 S06 2.69 -1.8
## 7 S07 3.10 -2.1
## 8 S08 2.00 -2.4
## 9 S09 2.90 -2.7
## 10 S10 2.65 -3
## 11 S11 3.91 -3.3
## 12 S12 4.29 -3.6
## 13 S13 4.09 -3.9
## 14 S14 4.10 -4.2
## 15 S15 4.83 -4.5
## 16 S16 5.18 -4.8
## 17 S17 5.49 -5.1
## 18 S18 6.02 -5.4
## 19 S19 5.73 -5.7
## 20 S20 6.25 -6
## The ANOVA (formula: RT ~ Difficulty_Group) suggests that:
##
## - The main effect of Difficulty_Group is statistically significant and large (F(1, 18) = 51.92, p < .001; Eta2 = 0.74, 90% CI [0.54, 0.84])
##
## Effect sizes were labelled following Field's (2013) recommendations.
## The ANOVA suggests that:
##
## - The main effect of Difficulty_Group is statistically significant and large (F(1, 18) = 51.92, p < .001; Eta2 = 0.74, 90% CI [0.54, 0.84])
##
## Effect sizes were labelled following Field's (2013) recommendations.
~ 1 + ...), it’s added by default## We fitted a linear model (estimated using OLS) to predict RT with Difficulty_Group (formula: RT ~ Difficulty_Group). The model explains a statistically significant and substantial proportion of variance (R2 = 0.74, F(1, 18) = 51.92, p < .001, adj. R2 = 0.73). The model's intercept, corresponding to Difficulty_Group = Reported_Easy, is at 4.99 (95% CI [4.32, 5.66], t(18) = 15.61, p < .001). Within this model:
##
## - The effect of Difficulty_Group [Reported_Hard] is statistically significant and negative (beta = -3.26, 95% CI [-4.21, -2.31], t(18) = -7.21, p < .001; Std. beta = -1.68, 95% CI [-2.17, -1.19])
##
## Standardized parameters were obtained by fitting the model on a standardized version of the dataset.
## We fitted a linear model (estimated using OLS) to predict RT with Difficulty (formula: RT ~ Difficulty). The model explains a statistically significant and substantial proportion of variance (R2 = 0.94, F(1, 18) = 284.12, p < .001, adj. R2 = 0.94). The model's intercept, corresponding to Difficulty = 0, is at 0.02 (95% CI [-0.45, 0.50], t(18) = 0.10, p = 0.923). Within this model:
##
## - The effect of Difficulty is statistically significant and negative (beta = -1.06, 95% CI [-1.19, -0.93], t(18) = -16.86, p < .001; Std. beta = -0.97, 95% CI [-1.09, -0.85])
##
## Standardized parameters were obtained by fitting the model on a standardized version of the dataset.
## Parameter | Coefficient | SE | 95% CI | t(18) | p
## -------------------------------------------------------------------
## (Intercept) | 0.02 | 0.23 | [-0.45, 0.50] | 0.10 | 0.923
## Difficulty | -1.06 | 0.06 | [-1.19, -0.93] | -16.86 | < .001
The two parameters are the intercept and the slope.
## Difficulty Predicted SE CI_low CI_high
## 1 -6.0000000 6.3830054 0.2096881 5.9424671 6.8235437
## 2 -5.3666667 5.7115785 0.1768481 5.3400344 6.0831226
## 3 -4.7333333 5.0401516 0.1474954 4.7302754 5.3500279
## 4 -4.1000000 4.3687247 0.1241288 4.1079397 4.6295097
## 5 -3.4666667 3.6972978 0.1106097 3.4649154 3.9296803
## 6 -2.8333333 3.0258710 0.1106097 2.7934885 3.2582534
## 7 -2.2000000 2.3544441 0.1241288 2.0936590 2.6152291
## 8 -1.5666667 1.6830172 0.1474954 1.3731409 1.9928934
## 9 -0.9333333 1.0115903 0.1768481 0.6400462 1.3831344
## 10 -0.3000000 0.3401634 0.2096881 -0.1003749 0.7807017
p <- ggplot(data = viz_data, aes(x = Difficulty, y = Predicted)) +
geom_line() +
geom_ribbon(aes(ymin = CI_low, ymax = CI_high), alpha = 0.2)
pp <- p +
geom_point(data = dfsub, aes(x = Difficulty, y = RT, color = Participant)) +
ylab("RT") +
theme_minimal()
p## We fitted a linear model to predict RT with Difficulty. The model's explanatory power is weak (R2 = 0.11, adj. R2 = 0.11). The model's intercept is at 1.34 (95% CI [0.86, 1.82]). Within this model:
##
## - The effect of Difficulty is statistically significant and negative (beta = -0.64, 95% CI [-0.77, -0.51], t(798) = -9.81, p < .001, Std. beta = -0.33)
Problems:
Solutions:
## We fitted a linear mixed model (estimated using REML and nloptwrap optimizer) to predict RT with Difficulty (formula: RT ~ Difficulty). The model included Participant as random effect (formula: ~1 | Participant). The model's total explanatory power is substantial (conditional R2 = 0.41) and the part related to the fixed effects alone (marginal R2) is of 0.04. The model's intercept, corresponding to Difficulty = 0, is at 4.81 (95% CI [3.41, 6.21], t(796) = 6.74, p < .001). Within this model:
##
## - The effect of Difficulty is statistically significant and positive (beta = 0.46, 95% CI [0.23, 0.68], t(796) = 3.98, p < .001; Std. beta = 0.23, 95% CI [0.12, 0.35])
##
## Standardized parameters were obtained by fitting the model on a standardized version of the dataset. 95% Confidence Intervals (CIs) and p-values were computed using the Wald approximation.
## # Fixed Effects
##
## Parameter | Coefficient | SE | 95% CI | t(796) | p
## -----------------------------------------------------------------
## (Intercept) | 4.81 | 0.71 | [3.41, 6.21] | 6.74 | < .001
## Difficulty | 0.46 | 0.12 | [0.23, 0.68] | 3.98 | < .001
viz_data <- estimate_relation(model)
p <- ggplot(data = viz_data, aes(x = Difficulty, y = Predicted)) +
geom_line() +
geom_ribbon(aes(ymin = CI_low, ymax = CI_high), alpha = 0.3) +
geom_point(data = df, aes(x = Difficulty, y = RT))
pp <- ggplot(data = viz_data, aes(x = Difficulty, y = Predicted)) +
geom_line() +
geom_ribbon(aes(ymin = CI_low, ymax = CI_high), alpha = 0.3) +
geom_point(data = df, aes(x = Difficulty, y = RT, color = Participant))
pviz_data_random <- estimate_relation(model,
include_random = TRUE,
preserve_range = TRUE,
length = 100)
p <- p +
geom_line(data = viz_data_random, aes(color = Participant))
pmodel <- lmer(RT ~ Difficulty + (1 + Difficulty | Participant), data = df)
parameters(model, effects = "fixed")## # Fixed Effects
##
## Parameter | Coefficient | SE | 95% CI | t(794) | p
## -----------------------------------------------------------------
## (Intercept) | 4.98 | 0.81 | [3.39, 6.57] | 6.13 | < .001
## Difficulty | 0.46 | 0.13 | [0.19, 0.72] | 3.41 | < .001
viz_data_fixed <- estimate_relation(model, include_random = FALSE)
viz_data_random <- estimate_relation(model, include_random = TRUE, preserve_range = TRUE, length = 100)
ggplot() +
geom_point(data = df, aes(x = Difficulty, y = RT, color = Participant)) +
geom_line(data = viz_data_fixed, aes(x = Difficulty, y = Predicted)) +
geom_line(data = viz_data_random, aes(x = Difficulty, y = Predicted, color = Participant))## Parameter Level Coefficient SE CI CI_low CI_high Effects
## 1 (Intercept) S01 -3.82551598 0.5396578 0.95 -4.8832259 -2.7678061 random
## 2 (Intercept) S02 -4.69945624 0.5599088 0.95 -5.7968572 -3.6020552 random
## 3 (Intercept) S03 -4.47920880 0.5881192 0.95 -5.6319013 -3.3265163 random
## 4 (Intercept) S04 -3.52154145 0.6238214 0.95 -4.7442088 -2.2988741 random
## 5 (Intercept) S05 -2.61111281 0.6663726 0.95 -3.9171791 -1.3050465 random
## 6 (Intercept) S06 -1.20469907 0.7150524 0.95 -2.6061761 0.1967780 random
## 7 (Intercept) S07 -0.61284903 0.7691335 0.95 -2.1203230 0.8946250 random
## 8 (Intercept) S08 -2.08462904 0.8279229 0.95 -3.7073280 -0.4619301 random
## 9 (Intercept) S09 -0.75416543 0.8907798 0.95 -2.5000618 0.9917309 random
## 10 (Intercept) S10 -0.31067315 0.9571187 0.95 -2.1865913 1.5652450 random
## 11 (Intercept) S11 0.82390451 1.0264033 0.95 -1.1878091 2.8356181 random
## 12 (Intercept) S12 1.44769158 1.0981390 0.95 -0.7046213 3.6000044 random
## 13 (Intercept) S13 0.02635696 1.1718635 0.95 -2.2704532 2.3231671 random
## 14 (Intercept) S14 0.73655110 1.2471398 0.95 -1.7077980 3.1809002 random
## 15 (Intercept) S15 2.25161767 1.3235506 0.95 -0.3424938 4.8457291 random
## 16 (Intercept) S16 4.22916061 1.4006940 0.95 1.4838508 6.9744704 random
## 17 (Intercept) S17 2.44767142 1.4781819 0.95 -0.4495119 5.3448548 random
## 18 (Intercept) S18 4.62830230 1.5556392 0.95 1.5793056 7.6772990 random
## 19 (Intercept) S19 4.02189684 1.6327042 0.95 0.8218553 7.2219384 random
## 20 (Intercept) S20 3.49069803 1.7090310 0.95 0.1410588 6.8403373 random
## 21 Difficulty S01 -0.25792568 0.2255589 0.95 -0.7000131 0.1841617 random
## 22 Difficulty S02 -0.19226750 0.2289301 0.95 -0.6409622 0.2564272 random
## 23 Difficulty S03 -0.26667257 0.2322755 0.95 -0.7219242 0.1885791 random
## 24 Difficulty S04 -0.20381412 0.2355826 0.95 -0.6655475 0.2579193 random
## 25 Difficulty S05 -0.13165332 0.2388377 0.95 -0.5997665 0.3364599 random
## 26 Difficulty S06 0.09574450 0.2420261 0.95 -0.3786179 0.5701069 random
## 27 Difficulty S07 0.11379872 0.2451323 0.95 -0.3666518 0.5942492 random
## 28 Difficulty S08 -0.11431686 0.2481399 0.95 -0.6006621 0.3720283 random
## 29 Difficulty S09 0.01693891 0.2510317 0.95 -0.4750742 0.5089520 random
## 30 Difficulty S10 0.18926859 0.2537902 0.95 -0.3081510 0.6866882 random
## 31 Difficulty S11 0.11644908 0.2563974 0.95 -0.3860805 0.6189787 random
## 32 Difficulty S12 0.14237742 0.2588353 0.95 -0.3649305 0.6496853 random
## 33 Difficulty S13 -0.20733481 0.2610862 0.95 -0.7190544 0.3043848 random
## 34 Difficulty S14 -0.06052691 0.2631329 0.95 -0.5762579 0.4552040 random
## 35 Difficulty S15 0.09416929 0.2649588 0.95 -0.4251405 0.6134791 random
## 36 Difficulty S16 0.39773432 0.2665489 0.95 -0.1246919 0.9201605 random
## 37 Difficulty S17 -0.05226230 0.2678893 0.95 -0.5773156 0.4727910 random
## 38 Difficulty S18 0.23494212 0.2689680 0.95 -0.2922256 0.7621098 random
## 39 Difficulty S19 0.14254604 0.2697754 0.95 -0.3862041 0.6712962 random
## 40 Difficulty S20 -0.05719491 0.2703039 0.95 -0.5869808 0.4725909 random
## Group
## 1 Participant
## 2 Participant
## 3 Participant
## 4 Participant
## 5 Participant
## 6 Participant
## 7 Participant
## 8 Participant
## 9 Participant
## 10 Participant
## 11 Participant
## 12 Participant
## 13 Participant
## 14 Participant
## 15 Participant
## 16 Participant
## 17 Participant
## 18 Participant
## 19 Participant
## 20 Participant
## 21 Participant
## 22 Participant
## 23 Participant
## 24 Participant
## 25 Participant
## 26 Participant
## 27 Participant
## 28 Participant
## 29 Participant
## 30 Participant
## 31 Participant
## 32 Participant
## 33 Participant
## 34 Participant
## 35 Participant
## 36 Participant
## 37 Participant
## 38 Participant
## 39 Participant
## 40 Participant
dfsub$random_coef <- dfrandom$Coefficient[1:20] # Where Parameters == (Intercept)
report(cor.test(dfsub$Difficulty, dfsub$random_coef))## Effect sizes were labelled following Funder's (2019) recommendations.
##
## The Pearson's product-moment correlation between dfsub$Difficulty and dfsub$random_coef is negative, statistically significant, and very large (r = -0.96, 95% CI [-0.98, -0.90], t(18) = -14.49, p < .001)