-
Notifications
You must be signed in to change notification settings - Fork 0
/
Supplemental-Material-B-Power-Simulation_between.Rmd
481 lines (398 loc) · 15.6 KB
/
Supplemental-Material-B-Power-Simulation_between.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
---
title: "Supplemental Information C: Power Simulations"
subtitle: "Supplemental Material for 'Need Fulfillment During Intergroup Contact: Three Experience Sampling Studies'"
author:
- ██████ ██████████^1^
- ██████████ ████████^1^
- █████ █████████^1^
- █████ ██ █████^1^
- ███ ███████^1^
- ^1^██████████ ██ █████████, Department of Psychology
- "Author Information:"
- "Correspondence concerning this article should be addressed to ██████ ██████████, ██████████ ██ ██████████, █████████ ██ ██████████, █████ ██████████ ███, ████ ██ █████████ ████████████████. E-mail: █████████████████"
- 'The main manuscript is available at <a href="https://www.doi.org/ToBePublished" target="_blank">doi.org/ToBePublished</a>'
- 'The data repository for this manuscript is available at <a href="https://osf.io/pr9zs/?view_only=1ea47bb646694632a764dead807ef970" target="_blank">osf.io/pr9zs/?view_only=1ea47bb646694632a764dead807ef970</a>'
- 'The GitHub repository for this manuscript is available at <a href="https://github.com/maskedForPeerReview" target="_blank">github.com/maskedForPeerReview</a>'
date: "Last updated: `r format(Sys.time(), '%d %B, %Y')`"
output:
bookdown::html_document2:
fig_caption: yes
md_extensions: +footnotes
code_folding: hide
mathjax: default
theme: yeti
toc: yes
toc_float: yes
number_sections: false
css: style.css
includes:
in_header: "_includes/head-custom-rmd.html"
editor_options:
chunk_output_type: console
bibliography: references.bib
csl: apa.csl
header-includes:
- \usepackage{amsmath, nccmath}
---
<style type="text/css">
.main-container {
max-width: 1300px;
margin-left: auto;
margin-right: auto;
}
.table {
margin-left:auto;
margin-right:auto;
}
</style>
```{r setup, include=FALSE}
# R Studio Clean-Up
cat("\014") # clear console
# rm(list=ls()) # clear workspace - use restart R instead [cmd/alt + shift + F10]
gc() # garbage collector
# Install and Load Packages
# !IMPORTANT!
# BEFORE FIRST RENDER:
# To install all relevant packages please run "renv::restore()" (or renv::init() and then initiate from lockfile) in the console before the first use to ensure that all packages are using the correct version.
# to store the packages in a contained library within the project folder: renv::settings$use.cache(FALSE) and add 'RENV_CONFIG_SANDBOX_ENABLED = FALSE' to an '.Renviron' file
lib <- c(
"rmarkdown",
"knitr",
"remedy",
"bookdown",
"brms",
"psych",
"ggplot2",
"ggthemes",
# "haven",
"RColorBrewer",
# "plotly",
"gridExtra",
"ggpattern",
"ggridges",
"binom",
"iterators",
"pbkrtest",
"plotrix",
"RLRsim",
"stats",
"methods",
"utils",
"graphics",
"grDevices",
"car",
"testthat",
"simr",
"data.table",
"lme4",
"lmerTest",
# "nlme",
# "jtools",
# "gtsummary",
"sessioninfo",
# "tibble",
"pander",
# "devtools",
# "mada",
#"tidyr",
#"tidyverse",
"plyr",
"dplyr",
# "Hmisc",
"kableExtra",
# "papaja",
"stringr"#,
# "stringi",
# "reshape2",
# "lubridate",
# "purrr",
# "metafor"
)
invisible(lapply(lib, library, character.only = TRUE))
rm(lib)
# Load Custom Packages
source("./scripts/functions/fun.panel.R")
source("./scripts/functions/themes.R")
# source("./scripts/functions/binaryCor.R")
# source("./scripts/functions/MlCorMat.R")
# source("./scripts/functions/MlTbl.R")
# source("./scripts/functions/metaLmer.R")
# Markdown Options
knitr::opts_knit$set(root.dir = rprojroot::find_rstudio_root_file()) # set working directory
knitr::opts_knit$get("root.dir") # check working directory
options(
scipen = 999,
digits = 4,
width = 400
) # removes scientific quotation
# knitr::opts_chunk$set(echo = TRUE, cache = F, cache.path = rprojroot::find_rstudio_root_file('cache/')) # cache settings
knitr::knit_hooks$set(
error = function(x, options) {
paste('\n\n<div class="alert alert-danger">',
gsub("##", "\n", gsub("^##\ Error", "**Error**", x)),
"</div>",
sep = "\n"
)
},
warning = function(x, options) {
paste('\n\n<div class="alert alert-warning">',
gsub("##", "\n", gsub("^##\ Warning:", "**Warning**", x)),
"</div>",
sep = "\n"
)
},
message = function(x, options) {
paste('\n\n<div class="alert alert-info">',
gsub("##", "\n", x),
"</div>",
sep = "\n"
)
}
)
htmltools::tagList(rmarkdown::html_dependency_font_awesome())
# Global Chunk Options
knitr::opts_chunk$set(
fig.width = 12,
fig.height = 8,
fig.path = "Figures/",
echo = TRUE,
cache = TRUE,
warning = FALSE,
message = FALSE
)
# set ggplot theme
theme_set(theme_Publication())
```
<br/>
<i class="fas fa-exclamation-circle"></i> Note. Boxplots display the interquartile range (IQR, center box), and the whiskers extend 1.5*IQR from the lower and upper hinge. The white point indicates the mean and the white center line indicates the median.
<br/>
Generally, power considerations of mixed effects model such as repeated measures ANCOVAs and multi-level regressions are difficult to estimate because of the complex covariance structures. We ran simulation-studies based on our initial results following the first study. We found that sufficient sample sizes heavily depended on the type and size of the effect. When we simulated models with the marginalization sub-scale (which was the best distributed with mostly small to medium effect sizes), we found that for between-subjects effects, a sample size of 80 participants would have been sufficient (power above .8) with two measurements (see Figure 9 for an illustration). However, for the same between-subject effects we did not reach a power above .8 with any increase of repeated follow-up measurements (based on model extrapolations with a fixed _n_ = 20; tested up to 12 months). The within time effect, on the other hand, was estimated well above .8 by the seventh measurement (i.e., after six months, see Figure 9). The cross-level interaction effects (within-between) were largely a mixture of their individual effects and changes in measurement numbers and sample size affected the models accordingly. We, therefore, urge researchers to rely on pilot data and to run extensive simulation studies to maximize effort and feasibility (i.e., sufficient power).
# Data Import
```{r}
#| label: import data
load("data/S1_Workers/processed/df.btw.RData")
```
# Extract Effects for Simulation
## Prepare Data
```{r}
#| label: data prep
# prepare data for regression (i.e, put into long format and change variable types)
marginalization = melt(df.btw[!is.na(df.btw$assimilation.post),c("marginalization.pre","marginalization.post",
"CtContactNL_c","AvKeyNeedInt_c",
"ExternalReference")],
id=c("ExternalReference","CtContactNL_c","AvKeyNeedInt_c"))
marginalization$time = ifelse(grepl(".pre", marginalization$variable), "pre", "post")
marginalization$variable = gsub(".pre$|.post$", "", marginalization$variable)
marginalization$time = factor(marginalization$time)
marginalization$time = factor(marginalization$time,levels(marginalization$time)[c(2,1)])
marginalization$id = rep(seq(1:20), 2)
```
# ML Regression
Run multilevel regression to extract the effect sizes and covariance matrices
```{r}
#| label: run regression
# run regresion
lm.marg <- lmer(value~
AvKeyNeedInt_c*time+
CtContactNL_c*time+
(1|id),
data=marginalization)
summary(lm.marg)
fixef(lm.marg)
```
# Post-Hoc Power
Check post-hoc power for comparison.
```{r}
#| label: post hoc simulation
# Post-hoc power analysis for single effects (example: key need)
post_hoc <- powerSim(lm.marg, fixed("AvKeyNeed"), nsim=1000)
post_hoc
```
# Power Simulation Participant Numbers
Simulate different participant numbers for all fixed effects.
## Power of contact effect for different numbers of participants
```{r}
#| label: participant simulation contact
# extrapolate data:
sim.N <- extend(lm.marg, along="id", n=500)
# Run and plot simulations
plt.pwr.contact = powerCurve(fit = sim.N,
test = simr::fixed("CtContactNL_c", "t"),
along="id",
progress = FALSE,
breaks = seq(10,100,10),
nsim=1000)
plot(plt.pwr.contact) +
title("Outgroup Contact Count [participant numbers]")
```
## Power of time effect for different numbers of participants
```{r}
#| label: participant simulation time
plt.pwr.time = powerCurve(sim.N, simr::fixed("time"),
along="id",
progress = FALSE,
breaks = seq(10,100,10),
nsim=1000)
plot(plt.pwr.time) +
title("Time [participant numbers]")
```
## Power of need fulfillment effect for different numbers of participants
```{r}
#| label: participant simulation avg need fulfillment
plt.pwr.Need = powerCurve(sim.N, simr::fixed("AvKeyNeedInt_c", "t"),
along="id",
progress = FALSE,
breaks = seq(10,100,10),
nsim=1000)
plot(plt.pwr.Need) +
title("Avergage Need Fulfillment [participant numbers]")
```
## Power of time by contact effect for different numbers of participants
```{r}
#| label: participant simulation contact by time
plt.pwr.timeCont = powerCurve(sim.N, simr::fixed("time:CtContactNL_c"),
along="id",
progress = FALSE,
breaks = seq(10,500,50),
nsim=1000)
plot(plt.pwr.timeCont) +
title("Time X Outgroup Contact Count [participant numbers]")
```
## Power of time by need fulfillment effect for different numbers of participants
```{r}
#| label: participant simulation need fulfillment by time
plt.pwr.timeNeed = powerCurve(sim.N, simr::fixed("AvKeyNeedInt_c:time"),
along="id",
progress = FALSE,
breaks = seq(10,50,5),
nsim=1000)
plot(plt.pwr.timeNeed) +
title("Time X Avergage Need Fulfillment [participant numbers]")
```
# Power Simulation Measurement Numbers
Simulate different number of within person measurements.
## Power of time effect for different numbers of measurements
```{r}
#| label: measurement simulation time
# extrapolate data:
sim.within <- extend(lm.marg, within="id", n=12)
# Run and plot simulations
plt.pwr.t.time = powerCurve(sim.within, simr::fixed("time"),
within="id",
progress = FALSE,
breaks = seq(2,12,1),
nsim=1000)
plot(plt.pwr.t.time) +
title("Time [measurement numbers]")
```
## Power of contact effect for different numbers of measurements
```{r}
#| label: measurement simulation contact count
plt.pwr.t.contact = powerCurve(sim.within, simr::fixed("CtContactNL_c", "t"),
within="id",
progress = FALSE,
breaks = seq(2,12,1),
nsim=1000)
plot(plt.pwr.t.contact) +
title("Outgroup Contact Count [measurement numbers]")
```
## Power of need fulfillment effect for different numbers of measurements
```{r}
#| label: measurement simulation contact need fulfillment
plt.pwr.t.Need = powerCurve(sim.within, simr::fixed("AvKeyNeedInt_c", "t"),
within="id",
progress = FALSE,
breaks = seq(2,12,1),
nsim=1000)
plot(plt.pwr.t.Need) +
title("Average Need Fulfillment [measurement numbers]")
```
## Power of time by contact effect for different numbers of measurements
```{r}
#| label: measurement simulation contact time by contact count
plt.pwr.t.timeCont = powerCurve(sim.within, simr::fixed("time:CtContactNL_c"),
within="id",
progress = FALSE,
breaks = seq(2,12,1),
nsim=1000)
plot(plt.pwr.t.timeCont) +
title("Time X Outgroup Contact Count [measurement numbers]")
```
## Power of time by need fulfillment effect for different numbers of measurements
```{r}
#| label: measurement simulation need fulfillment by time
plt.pwr.t.timeNeed = powerCurve(sim.within, simr::fixed("AvKeyNeedInt_c:time"),
within="id",
progress = FALSE,
breaks = seq(2,12,1),
nsim=1000)
plot(plt.pwr.t.timeNeed) +
title("Time X Average Need Fulfillment [measurement numbers]")
```
# Export data and plots
```{r}
#| label: export
# save simulations, because this takes for freakin ever.
save(plt.pwr.contact,plt.pwr.Need,plt.pwr.t.contact,plt.pwr.t.Need,
plt.pwr.t.time,plt.pwr.t.timeCont,plt.pwr.t.timeNeed,plt.pwr.time,
plt.pwr.timeCont,plt.pwr.timeNeed,
file = "data/S1_Workers/processed/PowerSimPlots.RData")
#load("PowerSimPlots.RData")
```
# Software Information
The full session information with all relevant system information and all loaded and installed packages is available in the collapsible section below.
<details>
<summary>System Info</summary>
\renewcommand{\arraystretch}{0.8} <!-- decrease line spacing for the table -->
```{r Reproducibility-SessionInfo-R-environment, echo=FALSE, message=FALSE, warning=FALSE, fig.align="center", out.width='100%', results='asis'}
df_session_platform <- devtools::session_info()$platform %>%
unlist(.) %>%
as.data.frame(.) %>%
rownames_to_column(.)
colnames(df_session_platform) <- c("Setting", "Value")
kbl(
df_session_platform,
booktabs = T,
align = "l",
caption = "R environment session info for reproducibility of results" # complete caption for main document
) %>%
kable_classic(
full_width = F,
lightable_options = "hover",
html_font = "Cambria"
)
```
\renewcommand{\arraystretch}{1} <!-- reset row height/line spacing -->
</details>
<br>
<details>
<summary>Package Info</summary>
\renewcommand{\arraystretch}{0.6} <!-- decrease line spacing for the table -->
```{r Reproducibility-SessionInfo-R-packages, echo=FALSE, message=FALSE, warning=FALSE, fig.align="center", out.width='100%', results='asis'}
df_session_packages <- devtools::session_info()$packages %>%
as.data.frame(.) %>%
filter(attached == TRUE) %>%
dplyr::select(loadedversion, date, source) %>%
rownames_to_column()
colnames(df_session_packages) <- c("Package", "Loaded version", "Date", "Source")
kbl(
df_session_packages,
booktabs = T,
align = "l",
caption = "Package info for reproducibility of results" # complete caption for main document
) %>%
kable_classic(
full_width = F,
lightable_options = "hover",
html_font = "Cambria"
)
```
\renewcommand{\arraystretch}{1} <!-- reset row height/line spacing -->
</details>
<br>
<details>
<summary>Full Session Info (including loaded but unattached packages --- for troubleshooting only)</summary>
`r pander(sessionInfo(), compact = FALSE)`
</details>
--------------------------------------------------------------------
</br>