Skip to content

Instantly share code, notes, and snippets.

@debruine
Last active February 22, 2022 16:04
Show Gist options
  • Save debruine/a7d8d4c4b73d33613ff54c66efc8b8ef to your computer and use it in GitHub Desktop.
Save debruine/a7d8d4c4b73d33613ff54c66efc8b8ef to your computer and use it in GitHub Desktop.
Find r given inferential stat and descriptives for a within-subject design
library(faux)
library(afex)
f <- function(r, reported) {
# simulate data for 3-level within design (change for your design)
dat <- faux::sim_design(
# you can add these parameters to f() arguments
# if you need to check varying analyses
n = 100, within = 3,
mu = c(100, 105, 110),
sd = c(10, 11, 12),
r = r, # assume all r are the same
empirical = TRUE, # paramaters describe sample, not population
long = TRUE, # table format for analysis
plot = FALSE
)
# analyse data (change for your design/analysis)
a <- afex::aov_ez(
# generic column names from faux;
# change if you customise the simulation code
id = "id",
dv = "y",
within = "W1",
data = dat)
# get the relevant statistic
stat <- a$anova_table$F
# compare it to the reported stat
abs(reported - stat)
}
# search over a plausible range for the reported stat
op <- optimise(f,
interval = c(-.99, .99),
reported = 30.6)
r <- op$minimum
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment