sim.coef.experiment: Generate coefficients given desired data and vice versa

View source: R/sim.coef.experiment.R

sim.coef.experimentR Documentation

Generate coefficients given desired data and vice versa

Description

Generate coefficients given desired data and vice versa

Usage

sim.coef.experiment(list.IV, formula, coefs = NA, results = NA)

Examples

# set a seed
trial.seed <- 9000

# initialize the factors list
lIV <- list(
  Var1 = factor(1:2, labels = c("a","b"))
  ,
  Var2 = factor(1:3)
)

tIV <- c("w","w")

# describe the model
exp.formula <- ~ Var1+Var2

# simply builds the dataset from the model formula
sim.coef.experiment(lIV, exp.formula)
# calculates results given desired coefficients
sim.coef.experiment(lIV, exp.formula, coefs = c(1,1,1,1))
# calculate coefficients given desired results
sim.coef.experiment(lIV, exp.formula, results = c(10,20,15,25,30,40))

(r <- design.experiment(lIV, tIV, exp.formula, calculate.coef.num = T))

# initialize the coefficients
cus.theta <- c(1, 1, 1, 1)  # standard errors of the random components
cus.sigma <- 1 # sigma of the model

# few examples
(r <- design.experiment(lIV, tIV, exp.formula,
                        betas = sim.coef.experiment(lIV, exp.formula, results = c(10,20,15,25,30,40))$coef[1:4],
                        thetas = cus.theta,
                        s = cus.sigma,
                        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()

(lmod1 <- lme4::lmer(y ~ Var1*Var2 + (1 + Var1*Var2|subjName), data=exp,
                     control = lmerControl(optimizer = "bobyqa")))

ccamp83/mu documentation built on Nov. 7, 2024, 5:17 p.m.