Created
March 12, 2020 09:20
-
-
Save Lakens/06c2d69da5fa5200c19ef825bbff7f01 to your computer and use it in GitHub Desktop.
scienceverse example for family-wise error control v1
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Scienceverse Sim | |
# install scienceverse | |
# devtools::install_github("scienceverse/scienceverse") | |
library(scienceverse) | |
library(faux) | |
set.seed(2) # set.seed(2) is a random draw where H1 is corroborated. | |
nsim <- 1 | |
alpha_level <- 0.05 | |
res_sim_1 <-numeric(nsim) #set up empty container for all results | |
for (i in 1:nsim) { | |
#Set up the study | |
sim_study <- study("Simulating Null Effects", | |
author = c("Daniel Lakens", "Lisa DeBruine")) | |
#Add a hypothesis | |
sim_study <- add_hypothesis( | |
study = sim_study, | |
description = "Something will happen", | |
id = "H1" | |
) | |
# Add an independent t-test as analysis | |
# Note we know our dataframe is called dat, and has a column condition, with values control & treatment | |
# We also have columns dv1 dv2 dv3 which are our dvs. | |
sim_study <- add_analysis(sim_study, | |
id = "ttest_1", | |
code = t.test(dat[which(dat$condition == "control"),]$dv1, | |
dat[which(dat$condition == "treatment"),]$dv1, | |
paired = FALSE, | |
conf.level = (1-alpha_level)), | |
software = R.version.string) | |
sim_study <- add_analysis(sim_study, | |
id = "ttest_2", | |
code = t.test(dat[which(dat$condition == "control"),]$dv2, | |
dat[which(dat$condition == "treatment"),]$dv2, | |
paired = FALSE, | |
conf.level = (1-alpha_level)), | |
software = R.version.string) | |
sim_study <- add_analysis(sim_study, | |
id = "ttest_3", | |
code = t.test(dat[which(dat$condition == "control"),]$dv3, | |
dat[which(dat$condition == "treatment"),]$dv3, | |
paired = FALSE, | |
conf.level = (1-alpha_level)), | |
software = R.version.string) | |
# Add criterion | |
sim_study <- add_criterion( | |
sim_study, | |
id = "p_t_1", | |
hypothesis_id = "H1", | |
analysis_id = "ttest_1", | |
result = "p.value", | |
operator = "<", | |
comparator = alpha_level) | |
sim_study <- add_criterion( | |
sim_study, | |
id = "p_t_2", | |
hypothesis_id = "H1", | |
analysis_id = "ttest_2", | |
result = "p.value", | |
operator = "<", | |
comparator = alpha_level) | |
sim_study <- add_criterion( | |
sim_study, | |
id = "p_t_3", | |
hypothesis_id = "H1", | |
analysis_id = "ttest_3", | |
result = "p.value", | |
operator = "<", | |
comparator = alpha_level) | |
# Add evaluation. | |
sim_study <- add_eval(sim_study, | |
hypothesis_id = "H1", | |
"corroboration", | |
description = "The hypothesis is corroborated if anything is significant.", | |
evaluation = "p_t_1 | p_t_2 | p_t_3") | |
sim_study <- add_eval(sim_study, | |
hypothesis_id = "H1", | |
"falsification", | |
description = "The hypothesis is falsified if nothing is significant.", | |
evaluation = "!p_t_1 & !p_t_2 & !p_t_3") | |
# Simulate some data and add it to the study | |
sim_study <- add_sim_data( | |
sim_study, | |
data_id = "dat", | |
within = list(dv = c("dv1", "dv2", "dv3")), | |
between = list(condition = c("control", "treatment")), | |
n = 100, | |
mu = c(100, 100, 100, 100, 100, 100), | |
sd = 10) | |
# Take a look at the data | |
# sim_study$data[[1]]$data | |
# Analyze the results | |
sim_study <- study_analyze(sim_study) | |
res_sim_1[i] <- sim_study[["hypotheses"]][[1]][["conclusion"]] == "corroborate" | |
if(nsim == 1){study_report(sim_study, template = "postreg", filename = "study_1.html")} | |
} | |
sum(res_sim_1/nsim) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment