\[\hat{Y_i} = b_0 + b_1X_i + \epsilon_i\]
\[\hat{Y_{ij}} = b_{0j} + b_{1j}X_{ij} + \epsilon_{ij}\]
\[b_{0j} = \gamma_{00} + \gamma_{01}X_j + u_{0j}\]
\[b_{1j} = \gamma_{10} + \gamma_{11}X_j + u_{1j}\]
library(rio) # importing data
library(nlme) # other people love lmer4
library(dplyr) # best data manipulation package
library(performance) # easy stats is great
library(parameters) # easy stats is great
library(report) # easy stats is great
# also recommend library(see)
library(MuMIn) # effect sizes DF <- import("data/sr_prime_trials.csv") %>%
# only variables we need
select(observation, target_duration, word_combo,
keep_target, keep_participant, type) %>%
# remove trials and people who don't meet inclusion rules
filter(keep_target == "keep") %>%
filter(keep_participant == "keep") %>%
na.omit()
head(DF) observation target_duration word_combo keep_target keep_participant
1 5c86ebfb2-e609 575.544 авенијаручак keep keep
2 be68c9e68-9040 662.900 авенијаручак keep keep
3 c70a8bb1f-d793 488.900 авенијаручак keep keep
4 77c9da84f-a097 400.000 авенијаручак keep keep
5 c4244b8de-0681 609.000 авенијаручак keep keep
6 e9b8b9c54-a976 576.962 авенијаручак keep keep
type
1 unrelated
2 unrelated
3 unrelated
4 unrelated
5 unrelated
6 unrelated
gls is akin to lm using generalized least squaresintercept.model <- gls(target_duration ~ 1,
data = DF,
method = "ML",
na.action = "na.omit")
summary(intercept.model)Generalized least squares fit by maximum likelihood
Model: target_duration ~ 1
Data: DF
AIC BIC logLik
1362406 1362425 -681201.1
Coefficients:
Value Std.Error t-value p-value
(Intercept) 725.0498 0.9731638 745.044 0
Standardized residuals:
Min Q1 Med Q3 Max
-1.8659624 -0.6086932 -0.2800019 0.2603637 7.4749990
Residual standard error: 300.8902
Degrees of freedom: 95598 total; 95597 residual
nlme::lme versus lme4::lmer
target_duration ~ (1|observation) + (1|word_combo)random.intercept.model <- lme(target_duration ~ 1,
data = DF,
method = "ML",
na.action = "na.omit",
# this is the random intercept part
random = list(~1|observation,
~1|word_combo))
summary(random.intercept.model)Linear mixed-effects model fit by maximum likelihood
Data: DF
AIC BIC logLik
1343359 1343397 -671675.5
Random effects:
Formula: ~1 | observation
(Intercept)
StdDev: 135.5622
Formula: ~1 | word_combo %in% observation
(Intercept) Residual
StdDev: 141.7564 228.5829
Fixed effects: target_duration ~ 1
Value Std.Error DF t-value p-value
(Intercept) 725.6796 5.273002 94343 137.6217 0
Standardized Within-Group Residuals:
Min Q1 Med Q3 Max
-3.3912147 -0.4694673 -0.1845673 0.2214105 7.1559565
Number of Observations: 95598
Number of Groups:
observation word_combo %in% observation
681 95024
fixed.model <- lme(target_duration ~ 1 + type,
data = DF,
method = "ML",
na.action = "na.omit",
random = list(~1|observation,
~1|word_combo))
summary(fixed.model)Linear mixed-effects model fit by maximum likelihood
Data: DF
AIC BIC logLik
1342544 1342591 -671266.9
Random effects:
Formula: ~1 | observation
(Intercept)
StdDev: 135.6001
Formula: ~1 | word_combo %in% observation
(Intercept) Residual
StdDev: 139.747 228.4602
Fixed effects: target_duration ~ 1 + type
Value Std.Error DF t-value p-value
(Intercept) 700.9499 5.343982 94342 131.16623 0
typeunrelated 49.7186 1.735545 94342 28.64723 0
Correlation:
(Intr)
typeunrelated -0.162
Standardized Within-Group Residuals:
Min Q1 Med Q3 Max
-3.3414581 -0.4729406 -0.1829942 0.2230444 7.2947095
Number of Observations: 95598
Number of Groups:
observation word_combo %in% observation
681 95024
# Comparison of Model Performance Indices
Name | Model | AIC (weights) | BIC (weights) | RMSE | Sigma | AICc (weights) | R2
--------------------------------------------------------------------------------------------------------------------
intercept.model | gls | 1.4e+06 (<.001) | 1.4e+06 (<.001) | 300.890 | 300.890 | (>.999) | 4.441e-16
random.intercept.model | lme | 1.3e+06 (<.001) | 1.3e+06 (<.001) | 193.686 | 228.583 | 1.3e+06 (>.999) |
fixed.model | lme | 1.3e+06 (>.999) | 1.3e+06 (>.999) | 194.311 | 228.460 | 1.3e+06 (>.999) |
[1] 1362406
[1] 1343359
[1] 1342544
# Fixed Effects
Parameter | Coefficient | SE | 95% CI | t(94342) | p
----------------------------------------------------------------------------
(Intercept) | 700.95 | 5.34 | [690.48, 711.42] | 131.17 | < .001
type [unrelated] | 49.72 | 1.74 | [ 46.32, 53.12] | 28.65 | < .001
# Random Effects
Parameter | Coefficient
-----------------------------------------
SD (Intercept: observation) | 135.60
SD (Intercept: word_combo) | 139.75
SD (Residual) | 228.46
Model: target_duration ~ 1 + type (95598 Observations)
Residual standard deviation: 228.460 (df = 94342)
:
# Standardization method: refit
Parameter | Std. Coef. | 95% CI
----------------------------------------------
(Intercept) | -0.08 | [-0.11, -0.05]
type [unrelated] | 0.17 | [ 0.15, 0.18]
We fitted a linear mixed model (estimated using ML and nlminb optimizer) to
predict target_duration with type (formula: target_duration ~ 1 + type). The
model included observation as random effects (formula: list(~1 | observation,
~1 | word_combo)). The model's intercept, corresponding to type = related, is
at 700.95 (95% CI [690.48, 711.42], t(94342) = 131.17, p < .001). Within this
model:
- The effect of type [unrelated] is statistically significant and positive
(beta = 49.72, 95% CI [46.32, 53.12], t(94342) = 28.65, p < .001; Std. beta =
0.17, 95% CI [0.15, 0.18])
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 a Wald t-distribution approximation.
progress_total panas_negative stress_total partno
1 23 15 1 dc990a2ce52ba0b2d2d766e43c5071d6
2 23 15 1 dc990a2ce52ba0b2d2d766e43c5071d6
3 25 14 1 dc990a2ce52ba0b2d2d766e43c5071d6
4 26 14 1 dc990a2ce52ba0b2d2d766e43c5071d6
5 25 16 1 dc990a2ce52ba0b2d2d766e43c5071d6
6 30 15 1 dc990a2ce52ba0b2d2d766e43c5071d6
realtime
1 0.00
2 1.00
3 1.99
4 2.99
5 3.98
6 4.99
[1] 113
# average number of time points per participant
DF %>%
group_by(partno) %>%
summarize(n_times = n()) %>%
ungroup() %>%
summarize(mean_times = mean(n_times),
sd_times = sd(n_times),
min_times = min(n_times),
max_times = max(n_times))# A tibble: 1 × 4
mean_times sd_times min_times max_times
<dbl> <dbl> <int> <int>
1 12.6 2.64 1 15
intercept.model <- gls(stress_total ~ 1,
data = DF,
method = "ML",
na.action = "na.omit")
summary(intercept.model)Generalized least squares fit by maximum likelihood
Model: stress_total ~ 1
Data: DF
AIC BIC logLik
4876.129 4886.646 -2436.065
Coefficients:
Value Std.Error t-value p-value
(Intercept) 1.528169 0.03571252 42.79085 0
Standardized residuals:
Min Q1 Med Q3 Max
-1.1359513 -0.3926099 -0.3926099 -0.3926099 7.7841455
Residual standard error: 1.345277
Degrees of freedom: 1420 total; 1419 residual
random.intercept.model <- lme(stress_total ~ 1,
data = DF,
method = "ML",
na.action = "na.omit",
# this is the random intercept part
random = list(~1|partno))
summary(random.intercept.model)Linear mixed-effects model fit by maximum likelihood
Data: DF
AIC BIC logLik
4456.026 4471.802 -2225.013
Random effects:
Formula: ~1 | partno
(Intercept) Residual
StdDev: 0.8244337 1.06616
Fixed effects: stress_total ~ 1
Value Std.Error DF t-value p-value
(Intercept) 1.55362 0.08321221 1307 18.67058 0
Standardized Within-Group Residuals:
Min Q1 Med Q3 Max
-4.30454994 -0.29479819 -0.11525706 -0.05541002 8.60744658
Number of Observations: 1420
Number of Groups: 113
fixed.model <- lme(stress_total ~ 1 + negative.mean + negative.cnt,
data = DF,
method = "ML",
na.action = "na.omit",
random = list(~1|partno))
summary(fixed.model)Linear mixed-effects model fit by maximum likelihood
Data: DF
AIC BIC logLik
4316.113 4342.405 -2153.056
Random effects:
Formula: ~1 | partno
(Intercept) Residual
StdDev: 0.7572297 1.016058
Fixed effects: stress_total ~ 1 + negative.mean + negative.cnt
Value Std.Error DF t-value p-value
(Intercept) 0.5313282 0.24342615 1306 2.182708 0.0292
negative.mean 0.0556071 0.01257117 111 4.423381 0.0000
negative.cnt 0.0657026 0.00572279 1306 11.480883 0.0000
Correlation:
(Intr) ngtv.m
negative.mean -0.949
negative.cnt 0.000 0.000
Standardized Within-Group Residuals:
Min Q1 Med Q3 Max
-4.0022728 -0.3932228 -0.0945899 0.1009836 8.5236045
Number of Observations: 1420
Number of Groups: 113
random.slope.model <- lme(stress_total ~ 1 + negative.mean + negative.cnt,
data = DF,
method = "ML",
na.action = "na.omit",
random = list(~realtime|partno))
summary(random.slope.model)Linear mixed-effects model fit by maximum likelihood
Data: DF
AIC BIC logLik
4174.756 4211.565 -2080.378
Random effects:
Formula: ~realtime | partno
Structure: General positive-definite, Log-Cholesky parametrization
StdDev Corr
(Intercept) 1.3277461 (Intr)
realtime 0.1075518 -0.893
Residual 0.9187873
Fixed effects: stress_total ~ 1 + negative.mean + negative.cnt
Value Std.Error DF t-value p-value
(Intercept) 0.6321300 0.21288561 1306 2.969341 3e-03
negative.mean 0.0377227 0.01102278 111 3.422248 9e-04
negative.cnt 0.0603304 0.00550876 1306 10.951731 0e+00
Correlation:
(Intr) ngtv.m
negative.mean -0.949
negative.cnt 0.028 -0.016
Standardized Within-Group Residuals:
Min Q1 Med Q3 Max
-3.02732595 -0.38036935 -0.05720288 0.12664612 8.92243129
Number of Observations: 1420
Number of Groups: 113
# Comparison of Model Performance Indices
Name | Model | AIC (weights) | BIC (weights) | RMSE | Sigma | AICc (weights) | R2 (cond.) | R2 (marg.) | ICC | R2
-----------------------------------------------------------------------------------------------------------------------------------------------
intercept.model | gls | 4876.1 (<.001) | 4886.6 (<.001) | 1.345 | 1.345 | (>.999) | | | | 3.331e-15
random.intercept.model | lme | 4456.0 (<.001) | 4471.8 (<.001) | 1.029 | 1.066 | 4456.0 (>.999) | 0.374 | 0.000 | 0.374 |
fixed.model | lme | 4316.1 (<.001) | 4342.4 (<.001) | 0.981 | 1.016 | 4316.2 (>.999) | 0.432 | 0.116 | 0.357 |
random.slope.model | lme | 4174.8 (>.999) | 4211.6 (>.999) | 0.863 | 0.919 | 4174.8 (>.999) | 0.692 | 0.049 | 0.676 |
[1] 4876.129
[1] 4456.026
[1] 4316.113
[1] 4174.756
OK: No outliers detected.
- Based on the following method and threshold: ().
- For variable: (Whole model)
# Fixed Effects
Parameter | Coefficient | SE | 95% CI | t | df | p
-----------------------------------------------------------------------------
(Intercept) | 0.63 | 0.21 | [0.21, 1.05] | 2.97 | 1306 | 0.003
negative mean | 0.04 | 0.01 | [0.02, 0.06] | 3.42 | 111 | < .001
negative cnt | 0.06 | 5.51e-03 | [0.05, 0.07] | 10.95 | 1306 | < .001
# Random Effects
Parameter | Coefficient
----------------------------------------------
SD (Intercept: partno) | 1.33
SD (realtime: partno) | 0.11
Cor (Intercept~realtime: partno) | -0.89
SD (Residual) | 0.92
Model: stress_total ~ 1 + negative.mean + negative.cnt (1420 Observations)
Residual standard deviation: 0.919 (df = 1306)
Conditional R2: 0.692; Marginal R2: 0.049
We fitted a linear mixed model (estimated using ML and nlminb optimizer) to
predict stress_total with negative.mean and negative.cnt (formula: stress_total
~ 1 + negative.mean + negative.cnt). The model included realtime as random
effects (formula: list(~realtime | partno)). The model's total explanatory
power is substantial (conditional R2 = 0.69) and the part related to the fixed
effects alone (marginal R2) is of 0.05. The model's intercept, corresponding to
negative.mean = 0 and negative.cnt = 0, is at 0.63 (95% CI [0.21, 1.05],
t(1306) = 2.97, p = 0.003). Within this model:
- The effect of negative mean is statistically significant and positive (beta =
0.04, 95% CI [0.02, 0.06], t(111) = 3.42, p < .001; Std. beta = 0.17, 95% CI
[0.07, 0.27])
- The effect of negative cnt is statistically significant and positive (beta =
0.06, 95% CI [0.05, 0.07], t(1306) = 10.95, p < .001; Std. beta = 0.21, 95% CI
[0.17, 0.25])
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 a Wald t-distribution approximation.
easystats is still awesome