added slides for code review and simulation

This commit is contained in:
debruine
2023-09-30 18:31:20 +01:00
parent 1f76dcd5c8
commit 2fba884cb7
32 changed files with 875 additions and 119 deletions

Binary file not shown.

After

Width:  |  Height:  |  Size: 936 KiB

View File

@@ -0,0 +1 @@
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 18 18"><path d="M3 3H15V15H3ZM4 4H14V14H4ZM5 9.2L8 12.2L13 7.2L11.6 5.8L8 9.4L6.4 7.8Z" style="fill-rule: evenodd; fill: green" /></svg>

After

Width:  |  Height:  |  Size: 190 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 105 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 89 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 114 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 82 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 166 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 171 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 188 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.3 MiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 19 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 128 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 264 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 90 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 62 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 66 KiB

View File

@@ -0,0 +1,223 @@
---
title: "Fake It Until You Make It"
subtitle: "How and why to simulate research data"
author: "Lisa DeBruine"
format:
revealjs:
logo: images/psyteachr_hex.png
theme: [dark, style.scss]
transition: none
transition-speed: fast
---
# Abstract
[debruine.github.io/talks/EMPSEB-fake-it-2023/](https://debruine.github.io/talks/EMPSEB-fake-it-2023/)
```{r, include = FALSE}
library(tidyverse)
library(ggdark)
library(gt)
library(faux)
knitr::opts_chunk$set(echo = FALSE)
theme_set(dark_theme_gray(base_size = 17))
faux_options(plot = FALSE)
```
::: {style="font-size: 18px;"}
Being able to simulate data allows you to prep analysis scripts for pre-registration, calculate power and sensitivity for analyses that dont have empirical methods, create reproducible examples when your data are too big or confidential to share, enhance your understanding of statistical concepts, and create demo data for teaching and tutorials. This workshop will cover the basics of simulation using the R package {faux}. We will simulate data with factorial designs by specifying the within and between-subjects factor structure, each cell mean and standard deviation, and correlations between cells where appropriate. This can be used to create simulated data sets to be used in preparing the analysis code for pre-registrations or registered reports. We will also create data sets for simulation-based power analyses.
:::
# Why Simulate Data?
## Pre-Registration
![Prep analysis scripts for pre-registration](images/lego-registered-reports.png)
## Power
![Calculate power and sensitivity for analyses that don't have empirical methods](images/power.jpg)
## Reproducible Examples
![Create reproducible examples when your data are too big or confidential to share](images/big-data.png)
<!-- Pete Linforth/ Pixabay -->
## Enhance Understanding
![Enhance your understanding of statistical concepts](images/stats.jpg)
<!-- Stanford Online -->
## Teaching Data
![Create demo data for teaching and tutorials](images/teaching-stats.jpg)
# Faux
::: {#fauxapp .panel-tabset}
### Plot
[![rstudio-connect.psy.gla.ac.uk/faux/](images/faux_plot.png)](https://shiny.psy.gla.ac.uk/debruine/fauxapp/)
### Data
[![rstudio-connect.psy.gla.ac.uk/faux/](images/faux_data.png)](https://shiny.psy.gla.ac.uk/debruine/fauxapp/)
### Code
[![rstudio-connect.psy.gla.ac.uk/faux/](images/faux_code.png)](https://shiny.psy.gla.ac.uk/debruine/fauxapp/)
:::
## Faux Code
```{r, echo = TRUE}
sim_data <- faux::sim_design(
within = list(version = c(V1 = "Version 1", V2 = "Version 2"),
condition = c(ctl = "Control", exp = "Experimental")),
between = list(age_group = c(young = "Age 20-29", old = "Age 70-79")),
n = 30,
mu = c(100, 100, 100, 100, 100, 90, 110, 110),
sd = 20,
r = 0.5,
dv = c(score = "Score"),
id = c(id = "Subject ID"),
vardesc = list(version = "Task Version",
condition = "Experiment Condition",
age_group = "Age Group"),
long = TRUE
)
```
## Faux Design Parameters
```{r}
# sim_data[c(1, 31, 61, 91, 121, 151, 181, 211), ] |>
get_design(sim_data)$params |>
gt() |>
gtExtras::gt_theme_dark(table.font.size = px(20)) |>
tab_style(style = cell_text(size = px(25)),
locations = cells_column_labels(everything()))
```
## Faux Design Plot
```{r}
sim_data |> get_design() |> plot() +
dark_theme_gray(base_size = 17)
```
## Faux Data Plot
```{r}
sim_data |> plot(geoms = c("violin", "pointrangeSE")) +
dark_theme_gray(base_size = 17)
```
## Power Simulation: Replicate Data
```{r, echo = TRUE}
sim_data <- faux::sim_design(
within = list(version = c(V1 = "Version 1", V2 = "Version 2"),
condition = c(ctl = "Control", exp = "Experimental")),
between = list(age_group = c(young = "Age 20-29", old = "Age 70-79")),
n = 30,
mu = c(100, 100, 100, 100, 100, 90, 110, 110),
sd = 20,
r = 0.5,
dv = c(score = "Score"),
id = c(id = "Subject ID"),
vardesc = list(version = "Task Version",
condition = "Experiment Condition",
age_group = "Age Group"),
long = TRUE,
rep = 100
)
```
## Power Simulation: Analysis Function
```{r, echo = TRUE}
# setup options to avoid annoying afex message & run faster
afex::set_sum_contrasts()
afex::afex_options(include_aov = FALSE)
analysis <- function(data) {
a <- afex::aov_ez(
id = "id",
dv = "score",
between = "age_group",
within = c("version", "condition"),
data = data)
as_tibble(a$anova_table, rownames = "term") |>
rename(p = `Pr(>F)`)
}
```
## Power Simulation: Analysis Result
```{r, echo = TRUE, eval = FALSE}
# test on first data set
analysis(sim_data$data[[1]])
```
::: {style="font-size: 70%;"}
```{r}
analysis(sim_data$data[[1]]) |>
gt() |>
gt::fmt_number(MSE, decimals = 1) |>
gt::fmt_number(F, decimals = 2) |>
gt::fmt_number(ges:p, decimals = 3) |>
gtExtras::gt_theme_dark(table.font.size = px(15)) |>
tab_style(style = cell_text(size = px(15)),
locations = cells_column_labels(everything()))
```
:::
## Power Simulation
```{r, echo = TRUE}
power <- sim_data |>
mutate(analysis = purrr::map(data, analysis)) |>
select(-data) |>
unnest(analysis) |>
group_by(term) |>
summarise(power = mean(p < .05))
```
::: {style="font-size: 80%;"}
```{r}
power |>
gt() |>
gtExtras::gt_theme_dark(table.font.size = px(15)) |>
tab_style(style = cell_text(size = px(20)),
locations = cells_column_labels(everything()))
```
:::
# Further Resources
:::{layout-nrow=1}
[![PsyPag Simulation Summer School](images/simsummerschool.jpg)](https://simsummerschool.github.io/)
[![Data Simulation Workshops](images/dsw.png)](https://debruine.github.io/data-sim-workshops/)
:::
# Thank You!
[debruine.github.io/talks/EMPSEB-fake-it-2023/](https://debruine.github.io/talks/EMPSEB-fake-it-2023/)
Workshop Materials: [tinyurl.com/data-sim](https://debruine.github.io/data-sim-workshops/)
Prerequisites: Students will need to have very basic knowledge of R and familiarity with R Markdown, and have installed R and RStudio on their laptop and installed the packages {faux}, {afex}, {broom} and {tidyverse} from CRAN.

View File

@@ -0,0 +1,62 @@
/*-- scss:defaults --*/
$body-bg: #222;
/*-- scss:rules --*/
:root {
--red: #983E82; /* hsl(315, 42%, 42%) */
--orange: #E2A458; /* hsl( 33, 70%, 62%) */
--yellow: #F5DC70; /* hsl( 49, 87%, 70%) */
--green: #59935B; /* hsl(122, 25%, 46%) */
--blue: #467AAC; /* hsl(209, 42%, 47%) */
--purple: #61589C; /* hsl(248, 28%, 48%) */
}
/* rainbow borders */
.slide-background-content {
margin-top: 24px;
box-shadow: 0 -4px 0 0px var(--purple),
0 -8px 0 0px var(--blue),
0 -12px 0 0px var(--green),
0 -16px 0 0px var(--yellow),
0 -20px 0 0px var(--orange),
0 -24px 0 0px var(--red);
}
.checklist li {
list-style: none;
}
.checklist li::before {
content: '';
display: inline-block;
margin: 0 0 -.25em -1.3em;
height: 1.2em;
width: 1.2em;
background-image: url(images/checkbox.svg);
}
.plotcode {
font-size: .8em;
}
#figtwitter img {
border-radius: 50%;
}
#figtwitter .quarto-figure {
margin: 0;
}
#figtwitter figcaption {
position: relative;
top: -1em;
text-align: center;
}
#fauxapp li {
font-size: 50%;
}
.gt_col_heading { font-size: 50px; }