For loop an argument
I want to calculate the power of a full factorial design using the skpr and AlgDesign packages. I included my code below with a picture of the output. I want to do a for loop that iterates .1 from 0-2.5 for the effectsize argument inside the eval_design function. I want to create a table that has the parameter column and then columns of power by .1 of effectsize. I included a picture of a spreadsheet that I am trying to reproduce. Is there a way to use a for loop on an argument?
I appreciate any help.
library(skpr)
library(AlgDesign)
FL = expand.grid(A = as.factor(c("-1", "0", "1")),
B = as.factor(c("-1", "1")),
C = as.factor(c("-1", "1")))
Design = gen_design(candidateset = FL,
model =~A + B + C + A:B + B:C + A:C,
trials = 12)
Powertable = eval_design(design = Design,
alpha = .2,
effectsize = .1,
detailedoutput = TRUE)
Powertable
Output of Code
Spreadsheet
To do this using a loop, just replace the effectsize
argument with the iterator like below.
FL = expand.grid(A = as.factor(c("-1", "0", "1")),
B = as.factor(c("-1", "1")),
C = as.factor(c("-1", "1")))
Design = gen_design(candidateset = FL,
model =~A + B + C + A:B + B:C + A:C,
trials = 12)
result <- data.frame(param = rep(NA, times = 17))
for (i in seq(0, 2.5, by = 0.1)) {
Powertable = eval_design(design = Design,
alpha = .2,
effectsize = i,
detailedoutput = TRUE)
if (all(is.na(result[, "param"]))) {
result[, "param"] <- Powertable$parameter
}
result[, paste("effect", i, sep = "_")] <- Powertable$power
}
> head(result[, 1:5])
param effect_0 effect_0.1 effect_0.2 effect_0.3
1 (Intercept) 0.2 0.2043084 0.2170947 0.2379503
2 A 0.2 0.2015984 0.2063745 0.2142712
3 B 0.2 0.2043084 0.2170947 0.2379503
4 C 0.2 0.2043084 0.2170947 0.2379503
5 A:B 0.2 0.2015984 0.2063745 0.2142712
6 B:C 0.2 0.2043084 0.2170947 0.2379503