View source: R/sim.experiment.r
sim.experiment | R Documentation |
Simulate data of an experiment with a given linear design
sim.experiment(design, subj, repetitions, random_seed = F, return_data = T)
design |
an object of the class mu.exp.design produced with the function design.experiment |
subj |
number of subjects |
repetitions |
number of repetitions per subject |
random_seed |
logical. When FALSE (default) the data is simulated using the seed passed via the design object. Set to TRUE to generate a random seed (ignores the seed parameter passed by the design object) |
return_data |
default to TRUE. Use FALSE to produce just a trials sequence |
An object of the class mu.exp.design with the following values
seed the seed that was used during the simulation
exp the simulated data
coef.matrix the subj-by-subj coefficient matrix
libraries()
# set a seed
trial.seed <- 9000
# initialize the factors list
lIV <- list(
Var1 = factor(1:2, labels = c("a","b"))
,
Var2 = factor(1:2)
)
tIV <- c("w","b")
# describe the model
exp.formula <- ~ Var1*Var2
# initialize the coefficients
cus.beta <- c(3, 6, 9, 12) # means of the fixed effects
cus.theta <- c(1, 2, 3, 4) # standard errors of the random components
cus.sigma <- 3 # sigma of the model
# few examples
(r <- design.experiment(lIV, tIV, betas = cus.beta, thetas = cus.theta, s = cus.sigma, exp.formula, seed=trial.seed))
# start the simulation
exp.info <- sim.experiment(design = r,
subj = 10,
repetitions = 10)
# extract the simulated data
exp <- exp.info$exp
# plot the results
ggplot(aes(Var1, y, color=Var2, group=Var2), data=exp) + facet_wrap(~subjName) +
geom_point(size=3, alpha=.4) + # draw raw scores
stat_smooth(method=lm, level=.68, se=F, linewidth=1, formula = y ~ x) + # draw fit line
stat_summary(fun.data = mean_se, geom='point', size=3, color='black') + # draw mean points
stat_summary(fun.data = mean_se, geom='errorbar', width=.2, linewidth=1.2) + # draw errorbars
theme_bw()
# check a fit of the data (with more subjects)
exp.info <- sim.experiment(design = r,
subj = 100,
repetitions = 10)
exp <- exp.info$exp
(lmod1 <- lme4::lmer(y ~ Var1*Var2 + (1 + Var1*Var2|subjName), data=exp,
control = lmerControl(optimizer = "bobyqa")))
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.