#I set the factor loadings according to literature
popModel <- "
#measurament model
sti =~ 0.49*s1 + 0.72*s2 + 0.88*s3
su =~ 0.6*su1 + 0.6*su2 + 0.6*su3
pc =~ 0.85*p1 + 0.87*p2 + 0.79*p3
ic =~ 0.65*i1 + 0.63*i2 + 0.83*i3
att =~ 0.84*a1 + 0.86*a2 + 0.86*a3
sn =~ 0.87*sn1 + 0.91*sn2 + 0.89*sn3
pbc =~ 0.83*pb1 + 0.92*pb2 + 0.80*pb3 + 0.85*pb4
int =~ 0.90*in1 + 0.90*in2 + 0.91*in3
pb =~ 1*pb1
#structural model
att ~ -0.67*sti + 0.19*su + 0.25*pc + 0.25*ic
sn ~ 0.20*su
pbc ~ 0.19*su
int ~ 0.30*att + 0.28*sn + 0.37*pbc + 0.18*pb
"
analysis.mod <- '
# define latent variables
sti =~ s1 + s2 + s3
su =~ su1 + su2 + su3
pc =~ p1 + p2 + p3
ic =~ i1 + i2 + i3
att =~ a1 + a2 + a3
sn =~ sn1 + sn2 + sn3
pbc =~ pb1 + pb2 + pb3 + pb4
int =~ in1 + in2 + in3
pb =~ pb1
# specify structural model
att ~ b8*sti + b1*su + b10*pc + b11*ic
sn ~ b3*su
pbc ~ b5*su
int ~ b2*att + b4*sn + b6*pbc + b7*pb + b12*su
# indirect effects
su.att.int := b1*b2
su.sn.int := b3*b4
su.pbc.int := b5*b6
# total effects
att.int := b12 + b1*b2
sn.int := b12 +b3*b4
pbc.int := b12 + b5*b6'
# A) run simulation (varying sample sizes)
med.n <- simsem::sim(n=rep(seq(100,500,20),100), # varying sample sizes (each 100x)
model=analysis.mod,
generate=pop.mod,
lavaanfun="sem")
summary(med.n)
med.n <- sim(5, n=100, model = analysis.mod, generate = pop.mod, lavaanfun = "sem", seed=1234)
?seq
# B) The varying parameter value that provides the power just over the specified power value
# (the adjacent value of varying parameter provides lower power than the specified power value).
powTable2 <- getPower(med.n)
findPower(powTable2, "N", 0.85)
#
su.att.int --> N1
#
su.sn.int --> N2
#
su.pbc.int --> N3
# C) power curves
plotPower(med.n, powerParam = "
su.att.int")
abline(h = 0.85, lwd = 2, lty = 2)
plotPower(med.n, powerParam = "
su.sn.int ")
abline(h = 0.85, lwd = 2, lty = 2)
plotPower(med.n, powerParam = "
su.pbc.int")
abline(h = 0.85, lwd = 2, lty = 2)
# D) 2 simulations with n=264 (10.000 repetitions) with two different seeds
med.n.neu1 <- simsem::sim(nRep=10000, model=analysis.mod, n=264, generate=pop.mod, lavaanfun="sem", seed=9999)
med.n.neu2 <- simsem::sim(nRep=10000, model=analysis.mod, n=264, generate=pop.mod, lavaanfun="sem", seed=99)
round(summaryParam(med.n.neu1, alpha=0.05, detail=TRUE), digits=3)
round(summaryParam(med.n.neu2, alpha=0.05), digits=3)
# in both cases: power for indirect effects >= .849