├── .Rbuildignore
├── .Rhistory
├── .gitignore
├── .travis.yml
├── ANOVApower.Rproj
├── DESCRIPTION
├── LICENSE
├── LICENSE.md
├── Meta
└── vignette.rds
├── NAMESPACE
├── R
├── ANOVA_design.R
├── ANOVA_exact.R
├── ANOVA_power.R
├── jmvpower.b.R
├── jmvpower.h.R
├── mu_from_ES.R
├── plot_power.R
├── power_oneway_between.R
├── power_oneway_within.R
├── power_threeway_between.R
└── power_twoway_between.R
├── README.Rmd
├── README.md
├── README_files
├── figure-gfm
│ ├── mean-plot-1.png
│ ├── sim-interaction-2-1.png
│ ├── sim-interaction-2-2.png
│ ├── sim-interaction-2-3.png
│ ├── sim-interaction-3-1.png
│ ├── sim-interaction-3-2.png
│ ├── unnamed-chunk-1-1.png
│ ├── unnamed-chunk-11-1.png
│ ├── unnamed-chunk-11-2.png
│ ├── unnamed-chunk-11-3.png
│ ├── unnamed-chunk-11-4.png
│ ├── unnamed-chunk-12-1.png
│ ├── unnamed-chunk-13-1.png
│ ├── unnamed-chunk-14-1.png
│ ├── unnamed-chunk-15-1.png
│ ├── unnamed-chunk-17-1.png
│ ├── unnamed-chunk-19-1.png
│ ├── unnamed-chunk-20-1.png
│ ├── unnamed-chunk-22-1.png
│ ├── unnamed-chunk-23-1.png
│ ├── unnamed-chunk-24-1.png
│ ├── unnamed-chunk-26-1.png
│ ├── unnamed-chunk-28-1.png
│ ├── unnamed-chunk-29-1.png
│ ├── unnamed-chunk-30-1.png
│ ├── unnamed-chunk-32-1.png
│ ├── unnamed-chunk-33-1.png
│ ├── unnamed-chunk-35-1.png
│ ├── unnamed-chunk-37-1.png
│ ├── unnamed-chunk-4-1.png
│ ├── unnamed-chunk-6-1.png
│ ├── unnamed-chunk-7-1.png
│ └── unnamed-chunk-8-1.png
└── figure-latex
│ ├── mean-plot-1.pdf
│ ├── sim-interaction-2-1.pdf
│ ├── sim-interaction-2-2.pdf
│ ├── sim-interaction-2-3.pdf
│ ├── sim-interaction-3-1.pdf
│ ├── sim-interaction-3-2.pdf
│ ├── unnamed-chunk-1-1.pdf
│ ├── unnamed-chunk-11-1.pdf
│ ├── unnamed-chunk-11-2.pdf
│ ├── unnamed-chunk-11-3.pdf
│ ├── unnamed-chunk-11-4.pdf
│ ├── unnamed-chunk-12-1.pdf
│ ├── unnamed-chunk-19-1.pdf
│ ├── unnamed-chunk-20-1.pdf
│ ├── unnamed-chunk-22-1.pdf
│ ├── unnamed-chunk-26-1.pdf
│ ├── unnamed-chunk-28-1.pdf
│ ├── unnamed-chunk-29-1.pdf
│ ├── unnamed-chunk-32-1.pdf
│ ├── unnamed-chunk-4-1.pdf
│ ├── unnamed-chunk-6-1.pdf
│ ├── unnamed-chunk-7-1.pdf
│ └── unnamed-chunk-8-1.pdf
├── jamovi
├── 0000.yaml
├── jmvpower.a.yaml
├── jmvpower.r.yaml
├── jmvpower.u.yaml
└── js
│ ├── css.js
│ └── main.js
├── man
├── ANOVA_design.Rd
├── ANOVA_exact.Rd
├── ANOVA_power.Rd
├── jmvpower.Rd
├── mu_from_ES.Rd
├── plot_power.Rd
├── power_oneway_between.Rd
├── power_oneway_within.Rd
├── power_threeway_between.Rd
└── power_twoway_between.Rd
├── mess
├── build and check package script.R
├── old_design_function.R
├── old_functions
│ ├── plot_power_2x2_within.R
│ ├── plot_power_oneway_between.R
│ ├── plot_power_oneway_within.R
│ ├── plot_power_twoway_between.R
│ ├── power_2x2_within.R
│ ├── power_2x2_within_2.R
│ ├── power_twoway_within_2.R
│ ├── test-plot_power_2x2_within.R
│ ├── test-plot_power_oneway_between.R
│ ├── test-plot_power_oneway_within.R
│ ├── test-plot_power_twoway_between.R
│ └── test-power_2x2_within.R
└── try_out.R
├── tests
├── testthat.R
└── testthat
│ ├── test-anova_design.R
│ ├── test-anova_exact.R
│ ├── test-anova_power.R
│ ├── test-mu_from_ES.R
│ ├── test-plot_power.R
│ ├── test-power_oneway_between.R
│ ├── test-power_oneway_within.R
│ ├── test-power_threeway_between.R
│ ├── test-power_twoway_between.R
│ └── test_sim_cor.R
└── vignettes
├── intro_to_ANOVApower.Rmd
├── screenshots
├── PS2000.gif
├── gpower_1.png
├── gpower_10.png
├── gpower_11.png
├── gpower_12.png
├── gpower_14.png
├── gpower_5.png
├── gpower_6.png
└── gpower_9.png
└── sim_data
├── power_result_cross_40.rds
├── power_result_cross_80.rds
├── power_result_ordinal.rds
├── power_result_vig_1.rds
├── power_result_vig_2.rds
├── power_result_vig_3.rds
└── power_result_vig_4.rds
/.Rbuildignore:
--------------------------------------------------------------------------------
1 | ^Meta$
2 | ^mess$
3 | ^doc$
4 | ^README_files$
5 | ^README\.Rmd$
6 | ^README.html$
7 | ^LICENSE\.md$
8 | ^ANOVApower\.Rproj$
9 | ^.travis.yml$
10 | ^\.Rproj\.user$
11 | ^codecov\.yml$
12 |
13 | ^jamovi$
14 | ^build/js$
15 | ^build/R$
16 | ^temp$
17 | ^.*\.jmo$
18 |
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | .Rproj.user
2 | Meta
3 | .Rhistory
4 | doc
5 |
6 | build/js
7 | build/R
8 | temp
9 | *.jmo
10 |
--------------------------------------------------------------------------------
/.travis.yml:
--------------------------------------------------------------------------------
1 | language: r
2 | cache: packages
3 |
--------------------------------------------------------------------------------
/ANOVApower.Rproj:
--------------------------------------------------------------------------------
1 | Version: 1.0
2 |
3 | RestoreWorkspace: No
4 | SaveWorkspace: No
5 | AlwaysSaveHistory: Default
6 |
7 | EnableCodeIndexing: Yes
8 | UseSpacesForTab: Yes
9 | NumSpacesForTab: 2
10 | Encoding: UTF-8
11 |
12 | RnwWeave: Sweave
13 | LaTeX: pdfLaTeX
14 |
15 | AutoAppendNewline: Yes
16 | StripTrailingWhitespace: Yes
17 |
18 | BuildType: Package
19 | PackageUseDevtools: Yes
20 | PackageInstallArgs: --no-multiarch --with-keep.source
21 | PackageRoxygenize: rd,collate,namespace,vignette
22 |
--------------------------------------------------------------------------------
/DESCRIPTION:
--------------------------------------------------------------------------------
1 | Package: ANOVApower
2 | Title: Simulation-Based Power Analysis for ANOVA Designs
3 | Version: 0.0.3
4 | Authors@R:
5 | person(given = "Daniel",
6 | family = "Lakens",
7 | role = c("aut", "cre"),
8 | email = "D.Lakens@tue.nl")
9 | person(given = "Aaron",
10 | family = "Caldwell",
11 | role = c("aut", "cre"),
12 | email = "arcaldwell49@gmail.com")
13 | Description: Functions to perform simulations of ANOVA designs of up to three factors. Calculates the observed power and average observed effect size for all main effects and interactions in the ANOVA, and all simple comparisons between conditions. Includes functions for analytic power calculations and additional helper functions that compute effect sizes for ANOVA designs, observed error rates in the simulations, and functions to plot power curves.
14 | License: MIT + file LICENSE
15 | Encoding: UTF-8
16 | LazyData: true
17 | RoxygenNote: 6.1.1
18 | Imports:
19 | mvtnorm,
20 | MASS,
21 | afex,
22 | emmeans,
23 | ggplot2,
24 | gridExtra,
25 | reshape2,
26 | stats
27 | Suggests:
28 | knitr,
29 | rmarkdown,
30 | pwr,
31 | testthat,
32 | dplyr,
33 | covr,
34 | jmvcore
35 | VignetteBuilder:
36 | knitr
37 |
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | YEAR: 2019
2 | COPYRIGHT HOLDER: Daniel Lakens
3 |
--------------------------------------------------------------------------------
/LICENSE.md:
--------------------------------------------------------------------------------
1 | # MIT License
2 |
3 | Copyright (c) 2019 Daniel Lakens
4 |
5 | Permission is hereby granted, free of charge, to any person obtaining a copy
6 | of this software and associated documentation files (the "Software"), to deal
7 | in the Software without restriction, including without limitation the rights
8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 | copies of the Software, and to permit persons to whom the Software is
10 | furnished to do so, subject to the following conditions:
11 |
12 | The above copyright notice and this permission notice shall be included in all
13 | copies or substantial portions of the Software.
14 |
15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 | SOFTWARE.
22 |
--------------------------------------------------------------------------------
/Meta/vignette.rds:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Lakens/ANOVApower/41a05129aeb70e344471bac691158285192b0fea/Meta/vignette.rds
--------------------------------------------------------------------------------
/NAMESPACE:
--------------------------------------------------------------------------------
1 | # Generated by roxygen2: do not edit by hand
2 |
3 | export(ANOVA_design)
4 | export(ANOVA_exact)
5 | export(ANOVA_power)
6 | export(jmvpower)
7 | export(mu_from_ES)
8 | export(plot_power)
9 | export(power_oneway_between)
10 | export(power_oneway_within)
11 | export(power_threeway_between)
12 | export(power_twoway_between)
13 | import(ggplot2)
14 | importFrom(MASS,mvrnorm)
15 | importFrom(afex,aov_car)
16 | importFrom(grDevices,colorRampPalette)
17 | importFrom(reshape2,melt)
18 | importFrom(stats,as.formula)
19 | importFrom(stats,median)
20 | importFrom(stats,p.adjust)
21 | importFrom(stats,pf)
22 | importFrom(stats,pnorm)
23 | importFrom(stats,power.t.test)
24 | importFrom(stats,pt)
25 | importFrom(stats,qf)
26 | importFrom(stats,qnorm)
27 | importFrom(stats,qt)
28 | importFrom(utils,combn)
29 | importFrom(utils,data)
30 |
--------------------------------------------------------------------------------
/R/ANOVA_exact.R:
--------------------------------------------------------------------------------
1 | #' Simulates on exact empirical data set from the design to calculate power
2 | #' @param design_result Output from the ANOVA_design function
3 | #' @param alpha_level Alpha level used to determine statistical significance
4 | #' @param correction Set a correction of violations of sphericity. This can be set to "none", "GG" Grennhouse-Geisser, and "HF" Huynh-Feldt
5 | #' @param verbose Set to FALSE to not print results (default = TRUE)
6 | #' @return Returns dataframe with simulation data (power and effect sizes), anova results and simple effect results, plot of exact data, and alpha_level.
7 | #' @examples
8 | #' ## Set up a within design with 2 factors, each with 2 levels,
9 | #' ## with correlation between observations of 0.8,
10 | #' ## 40 participants (who do all conditions), and standard deviation of 2
11 | #' ## with a mean pattern of 1, 0, 1, 0, conditions labeled 'condition' and
12 | #' ## 'voice', with names for levels of "cheerful", "sad", amd "human", "robot"
13 | #' design_result <- ANOVA_design(design = "2w*2w", n = 40, mu = c(1, 0, 1, 0),
14 | #' sd = 2, r = 0.8, labelnames = c("condition", "cheerful",
15 | #' "sad", "voice", "human", "robot"))
16 | #' set.seed(252)
17 | #' exact_result <- ANOVA_exact(design_result, alpha_level = 0.05)
18 | #' @section References:
19 | #' to be added
20 | #' @importFrom stats pnorm pt qnorm qt as.formula median qf power.t.test pf
21 | #' @importFrom utils combn
22 | #' @importFrom reshape2 melt
23 | #' @importFrom MASS mvrnorm
24 | #' @importFrom afex aov_car
25 | #' @import ggplot2
26 | #' @export
27 | #'
28 |
29 | ANOVA_exact <- function(design_result, correction = "none", alpha_level, verbose = TRUE) {
30 |
31 | if (is.element(correction, c("none", "GG", "HF")) == FALSE ) {
32 | stop("Correction for sphericity can only be none, GG, or HF")
33 | }
34 |
35 | #Errors with very small sample size; issue with mvrnorm function from MASS package
36 | if (design_result$n < prod(as.numeric(unlist(regmatches(design_result$design,
37 | gregexpr("[[:digit:]]+", design_result$design)))))
38 | ) {
39 | stop("ANOVA_exact cannot handle small sample sizes (n < the product of the factors) at this time; please pass this design_result to the ANOVA_power function to simulate power")
40 | }
41 |
42 | effect_size_d <- function(x, y, conf.level = 0.95){
43 | sd1 <- sd(x) #standard deviation of measurement 1
44 | sd2 <- sd(y) #standard deviation of measurement 2
45 | n1 <- length(x) #number of pairs
46 | n2 <- length(y) #number of pairs
47 | df <- n1 + n2 - 2
48 | m_diff <- mean(y - x)
49 | sd_pooled <- (sqrt((((n1 - 1) * ((sd1^2))) + (n2 - 1) * ((sd2^2))) / ((n1 + n2 - 2)))) #pooled standard deviation
50 | j <- (1 - 3/(4 * (n1 + n2 - 2) - 1)) #Calculate Hedges' correction.
51 | t_value <- m_diff / sqrt(sd_pooled^2 / n1 + sd_pooled^2 / n2)
52 | p_value = 2*pt(-abs(t_value), df = df)
53 | #Calculate power
54 | power = power.t.test(
55 | n = n1,
56 | delta = m_diff,
57 | sd = sd_pooled,
58 | type = "two.sample",
59 | alternative = "two.sided",
60 | strict = TRUE,
61 | sig.level = alpha_level
62 | )$power
63 |
64 | d <- m_diff / sd_pooled #Cohen's d
65 | d_unb <- d*j #Hedges g, of unbiased d
66 |
67 | invisible(list(d = d,
68 | d_unb = d_unb,
69 | p_value = p_value,
70 | power = power))
71 | }
72 |
73 | effect_size_d_paired <- function(x, y, conf.level = 0.95){
74 | sd1 <- sd(x) #standard deviation of measurement 1
75 | sd2 <- sd(y) #standard deviation of measurement 2
76 | s_diff <- sd(x - y) #standard deviation of the difference scores
77 | N <- length(x) #number of pairs
78 | df = N - 1
79 | s_av <- sqrt((sd1 ^ 2 + sd2 ^ 2) / 2) #averaged standard deviation of both measurements
80 |
81 | #Cohen's d_av, using s_av as standardizer
82 | m_diff <- mean(y - x)
83 | d_av <- m_diff / s_av
84 | d_av_unb <- (1 - (3 / (4 * (N - 1) - 1))) * d_av
85 |
86 | #get the t-value for the CI
87 | t_value <- m_diff / (s_diff / sqrt(N))
88 | p_value = 2 * pt(-abs(t_value), df = df)
89 |
90 | power = power.t.test(
91 | n = N,
92 | delta = m_diff,
93 | sd = s_diff,
94 | type = "paired",
95 | alternative = "two.sided",
96 | strict = TRUE,
97 | sig.level = alpha_level
98 | )$power
99 |
100 | #Cohen's d_z, using s_diff as standardizer
101 | d_z <- t_value / sqrt(N)
102 | d_z_unb <- (1 - (3 / (4 * (N - 1) - 1))) * d_z
103 |
104 | invisible(list(
105 | d_z = d_z,
106 | d_z_unb = d_z_unb,
107 | p_value = p_value,
108 | power = power
109 | ))
110 | }
111 |
112 | #Check to ensure there is a within subject factor -- if none --> no MANOVA
113 | run_manova <- grepl("w", design_result$design)
114 |
115 | Roy <- function(eig, q, df.res) {
116 | p <- length(eig)
117 | test <- max(eig)
118 | tmp1 <- max(p, q)
119 | tmp2 <- df.res - tmp1 + q
120 | c(test, (tmp2 * test)/tmp1, tmp1, tmp2)
121 | }
122 |
123 | Wilks <- function(eig, q, df.res)
124 | {
125 | test <- prod(1/(1 + eig))
126 | p <- length(eig)
127 | tmp1 <- df.res - 0.5 * (p - q + 1)
128 | tmp2 <- (p * q - 2)/4
129 | tmp3 <- p^2 + q^2 - 5
130 | tmp3 <- if (tmp3 > 0)
131 | sqrt(((p * q)^2 - 4)/tmp3)
132 | else 1
133 | c(test, ((test^(-1/tmp3) - 1) * (tmp1 * tmp3 - 2 * tmp2))/p/q,
134 | p * q, tmp1 * tmp3 - 2 * tmp2)
135 | }
136 |
137 | HL <- function(eig, q, df.res)
138 | {
139 | test <- sum(eig)
140 | p <- length(eig)
141 | m <- 0.5 * (abs(p - q) - 1)
142 | n <- 0.5 * (df.res - p - 1)
143 | s <- min(p, q)
144 | tmp1 <- 2 * m + s + 1
145 | tmp2 <- 2 * (s * n + 1)
146 | c(test, (tmp2 * test)/s/s/tmp1, s * tmp1, tmp2)
147 | }
148 |
149 | Pillai <- function(eig, q, df.res)
150 | {
151 | test <- sum(eig/(1 + eig))
152 | p <- length(eig)
153 | s <- min(p, q)
154 | n <- 0.5 * (df.res - p - 1)
155 | m <- 0.5 * (abs(p - q) - 1)
156 | tmp1 <- 2 * m + s + 1
157 | tmp2 <- 2 * n + s + 1
158 | c(test, (tmp2/tmp1 * test)/(s - test), s * tmp1, s * tmp2)
159 | }
160 |
161 | #Only utilized if MANOVA output included (see run_manova)
162 | Anova.mlm.table <- function(x, ...)
163 | {
164 | test <- x$test
165 | repeated <- x$repeated
166 | ntests <- length(x$terms)
167 | tests <- matrix(NA, ntests, 4)
168 | if (!repeated)
169 | SSPE.qr <- qr(x$SSPE)
170 | for (term in 1:ntests) {
171 | eigs <- Re(eigen(qr.coef(if (repeated) qr(x$SSPE[[term]])
172 | else SSPE.qr,
173 | x$SSP[[term]]), symmetric = FALSE)$values)
174 | tests[term, 1:4] <- switch(test, Pillai = Pillai(eigs,
175 | x$df[term], x$error.df), Wilks = Wilks(eigs,
176 | x$df[term], x$error.df), `Hotelling-Lawley` =
177 | HL(eigs,
178 | x$df[term], x$error.df), Roy = Roy(eigs,
179 | x$df[term], x$error.df))
180 | }
181 | ok <- tests[, 2] >= 0 & tests[, 3] > 0 & tests[, 4] > 0
182 | ok <- !is.na(ok) & ok
183 | tests <- cbind(x$df, tests, pf(tests[ok, 2], tests[ok, 3],
184 | tests[ok, 4], lower.tail = FALSE))
185 | rownames(tests) <- x$terms
186 | colnames(tests) <- c("df", "test_stat", "approx_F", "num_Df",
187 | "den_Df", "p.value")
188 | tests <- structure(as.data.frame(tests), heading = paste("\nType ",
189 | x$type, if (repeated)
190 | " Repeated Measures", " MANOVA Tests: ", test, " test
191 | statistic",
192 | sep = ""), class = c("anova", "data.frame"))
193 | invisible(tests)
194 | }
195 |
196 | round_dig <- 4 #Set digits to which you want to round the output.
197 |
198 | if (missing(alpha_level)) {
199 | alpha_level <- 0.05
200 | }
201 |
202 | if (alpha_level >= 1 | alpha_level <= 0 ) {
203 | stop("alpha_level must be less than 1 and greater than zero")
204 | }
205 |
206 | #Read in all variables from the design_result object
207 | design <- design_result$design #String used to specify the design
208 | factornames <- design_result$factornames #Get factor names
209 | n <- design_result$n
210 | mu = design_result$mu # population means - should match up with the design
211 | sd <- design_result$sd #population standard deviation (currently assumes equal variances)
212 | r <- design_result$r # correlation between within factors (currently only 1 value can be entered)
213 | factors <- design_result$factors
214 | design_factors <- design_result$design_factors
215 | sigmatrix <- design_result$sigmatrix
216 | dataframe <- design_result$dataframe
217 | design_list <- design_result$design_list
218 |
219 |
220 |
221 | ###############
222 | #Specify factors for formula ----
223 | ###############
224 |
225 | frml1 <- design_result$frml1
226 | frml2 <- design_result$frml2
227 |
228 | aov_result <- suppressMessages({aov_car(frml1, #here we use frml1 to enter formula 1 as designed above on the basis of the design
229 | data = dataframe, include_aov = FALSE,
230 | anova_table = list(es = "pes")) }) #This reports PES not GES
231 |
232 | #Run MANOVA if within subject factor is included; otherwise ignored
233 | if (run_manova == TRUE) {
234 | manova_result <- Anova.mlm.table(aov_result$Anova)
235 | }
236 |
237 | ###############
238 | # Set up dataframe for storing empirical results
239 | ###############
240 |
241 | #How many possible planned comparisons are there (to store p and es)
242 | possible_pc <- (((prod(
243 | as.numeric(strsplit(design, "\\D+")[[1]])
244 | )) ^ 2) - prod(as.numeric(strsplit(design, "\\D+")[[1]])))/2
245 |
246 | #create empty dataframe to store simulation results
247 | #number of columns for ANOVA results and planned comparisons, times 2 (p-values and effect sizes)
248 | sim_data <- as.data.frame(matrix(
249 | ncol = 2 * (2 ^ factors - 1) + 2 * possible_pc,
250 | nrow = 1
251 | ))
252 |
253 | paired_tests <- combn(unique(dataframe$cond),2)
254 | paired_p <- numeric(possible_pc)
255 | paired_d <- numeric(possible_pc)
256 | within_between <- sigmatrix[lower.tri(sigmatrix)] #based on whether correlation is 0 or not, we can determine if we should run a paired or independent t-test
257 |
258 | #Dynamically create names for the data we will store
259 | names(sim_data) = c(paste("anova_",
260 | rownames(aov_result$anova_table),
261 | sep = ""),
262 | paste("anova_es_",
263 | rownames(aov_result$anova_table),
264 | sep = ""),
265 | paste("p_",
266 | paste(paired_tests[1,],paired_tests[2,],sep = "_"),
267 | sep = ""),
268 | paste("d_",
269 | paste(paired_tests[1,],paired_tests[2,], sep = "_"),
270 | sep = ""))
271 |
272 | #We simulate a new y variable, melt it in long format, and add it to the dataframe (surpressing messages)
273 | #empirical set to true to create "exact" dataset
274 |
275 | dataframe$y <- suppressMessages({
276 | melt(as.data.frame(mvrnorm(
277 | n = n,
278 | mu = mu,
279 | Sigma = as.matrix(sigmatrix),
280 | empirical = TRUE
281 | )))$value
282 | })
283 |
284 | # We perform the ANOVA using AFEX
285 | #Can be set to NICE to speed up, but required data grabbing from output the change.
286 | aov_result <- suppressMessages({aov_car(frml1, #here we use frml1 to enter fromula 1 as designed above on the basis of the design
287 | data = dataframe, include_aov = FALSE, #Need development code to get aov_include function
288 | anova_table = list(es = "pes",
289 | correction = correction))}) #This reports PES not GES
290 |
291 |
292 |
293 | #Add additional statistics
294 | #Create dataframe from afex results
295 | anova_table <- as.data.frame(aov_result$anova_table)
296 | colnames(anova_table) <- c("num_Df", "den_Df", "MSE", "F", "pes", "p")
297 |
298 | #Calculate cohen's f
299 | anova_table$f2 <- anova_table$pes/(1 - anova_table$pes)
300 | #Calculate noncentrality
301 | anova_table$lambda <- anova_table$f2*anova_table$den_Df
302 |
303 | #minusalpha<- 1-alpha_level
304 | anova_table$Ft <- qf((1 - alpha_level), anova_table$num_Df, anova_table$den_Df)
305 | #Calculate power
306 | anova_table$power <- (1 - pf(anova_table$Ft, anova_table$num_Df, anova_table$den_Df, anova_table$lambda))*100
307 |
308 | #MANOVA exact results
309 |
310 | # Store MANOVA result if there are within subject factors
311 | if (run_manova == TRUE) {
312 | manova_result <- Anova.mlm.table(aov_result$Anova)
313 |
314 |
315 |
316 | manova_result$f2 <- manova_result$test_stat / (1 - manova_result$test_stat)
317 | manova_result$lambda <- manova_result$f2 * manova_result$den_Df
318 | manova_result$Ft <- qf((1 - alpha_level), manova_result$num_Df, manova_result$den_Df)
319 | manova_result$power <- round(1 - pf(manova_result$Ft,
320 | manova_result$num_Df,
321 | manova_result$den_Df,
322 | manova_result$lambda), 4) * 100
323 |
324 | }
325 | ###
326 |
327 | for (j in 1:possible_pc) {
328 | x <- dataframe$y[which(dataframe$cond == paired_tests[1,j])]
329 | y <- dataframe$y[which(dataframe$cond == paired_tests[2,j])]
330 | #this can be sped up by tweaking the functions that are loaded to only give p and dz
331 | ifelse(within_between[j] == 0,
332 | t_test_res <- effect_size_d(x, y, conf.level = 1 - alpha_level),
333 | t_test_res <- effect_size_d_paired(x, y, conf.level = 1 - alpha_level))
334 | paired_p[j] <- (t_test_res$power*100)
335 | paired_d[j] <- ifelse(within_between[j] == 0,
336 | t_test_res$d,
337 | t_test_res$d_z)
338 | }
339 |
340 | # store p-values and effect sizes for calculations
341 | sim_data[1,] <- c(aov_result$anova_table[[6]], #p-value for ANOVA
342 | aov_result$anova_table[[5]], #partial eta squared
343 | paired_p, #power for paired comparisons, dropped correction for multiple comparisons
344 | paired_d) #effect sizes
345 |
346 | ###############
347 | #Sumary of power and effect sizes of main effects and contrasts ----
348 | ###############
349 | #ANOVA
350 | main_results <- round(data.frame(anova_table$power,
351 | anova_table$pes,
352 | sqrt(anova_table$f2),
353 | anova_table$lambda),
354 | round_dig)
355 | rownames(main_results) <- rownames(anova_table)
356 | colnames(main_results) <- c("power", "partial_eta_squared", "cohen_f", "non_centrality")
357 | main_results$power <- round(main_results$power, 2)
358 | #MANOVA
359 | if (run_manova == TRUE) {
360 | manova_results <- round(data.frame(manova_result$power,
361 | manova_result$test_stat,
362 | sqrt(manova_result$f2),
363 | manova_result$lambda),
364 | round_dig)
365 | rownames(manova_results) <- rownames(manova_result)
366 | colnames(manova_results) <- c("power", "pillai_trace", "cohen_f", "non_centrality")
367 | manova_results$power <- round(manova_results$power, 2)
368 | }
369 |
370 | #Data summary for pairwise comparisons
371 | power_paired = as.data.frame(apply(as.matrix(sim_data[(2 * (2 ^ factors - 1) + 1):(2 * (2 ^ factors - 1) + possible_pc)]), 2,
372 | function(x) round(x, 2)))
373 |
374 | es_paired = as.data.frame(apply(as.matrix(sim_data[(2 * (2 ^ factors - 1) + possible_pc + 1):(2*(2 ^ factors - 1) + 2 * possible_pc)]), 2,
375 | function(x) round(x,round_dig)))
376 |
377 | pc_results <- data.frame(power_paired, es_paired)
378 | names(pc_results) = c("power","effect_size")
379 |
380 | #Create plot
381 |
382 | if (factors == 1) {meansplot = ggplot(dataframe, aes_string(y = "y", x = factornames[1]))}
383 | if (factors == 2) {meansplot = ggplot(dataframe, aes_string(y = "y",
384 | x = factornames[1])) + facet_wrap( paste("~",factornames[2],sep = ""))}
385 | if (factors == 3) {meansplot = ggplot(dataframe, aes_string(y = "y",
386 | x = factornames[1])) + facet_grid( paste(factornames[3],"~",factornames[2], sep = ""))}
387 |
388 | meansplot2 = meansplot +
389 | geom_jitter(position = position_jitter(0.2)) +
390 | stat_summary(
391 | fun.data = "mean_sdl",
392 | fun.args = list(mult = 1),
393 | geom = "crossbar",
394 | color = "red"
395 | ) +
396 | coord_cartesian(ylim = c(min(dataframe$y), max(dataframe$y))) +
397 | theme_bw() + ggtitle("Exact data for each condition in the design")
398 |
399 | #######################
400 | # Return Results ----
401 | #######################
402 | if (verbose == TRUE) {
403 | # The section below should be blocked out when in Shiny
404 | cat("Power and Effect sizes for ANOVA tests")
405 | cat("\n")
406 | print(main_results)
407 | cat("\n")
408 | cat("Power and Effect sizes for contrasts")
409 | cat("\n")
410 | print(pc_results)
411 | }
412 |
413 | if (run_manova == FALSE) {
414 | manova_results = NULL
415 | }
416 |
417 | # Return results in list()
418 | invisible(list(dataframe = dataframe,
419 | aov_result = aov_result,
420 | main_results = main_results,
421 | pc_results = pc_results,
422 | manova_results = manova_results,
423 | alpha_level = alpha_level,
424 | plot = meansplot2))
425 | }
426 |
--------------------------------------------------------------------------------
/R/jmvpower.b.R:
--------------------------------------------------------------------------------
1 |
2 | jmvpowerClass <- if (requireNamespace('jmvcore')) R6::R6Class(
3 | "jmvpowerClass",
4 | inherit = jmvpowerBase,
5 | private = list(
6 | .init = function() {
7 |
8 | design <- self$options$design
9 | factor_levels <- as.numeric(strsplit(design, "\\D+")[[1]])
10 | n_factors <- length(factor_levels)
11 | ncells <- cumprod(factor_levels)
12 | ncells <- ncells[length(ncells)]
13 |
14 | dt <- self$results$design$summary
15 |
16 | afmc <- list(
17 | `none`="None",
18 | `holm`="Holm-Bonferroni",
19 | `bonferroni` = "bonferroni",
20 | `fdr`="False Discovery Rate")[[self$options$p_adjust]]
21 |
22 | dt$setRow(rowNo=1, values=list(name='Design', value=design))
23 | dt$setRow(rowNo=2, values=list(name='Formula', value='.'))
24 | dt$setRow(rowNo=3, values=list(name='Sample size per cell', value=self$options$n))
25 | dt$setRow(rowNo=4, values=list(name='Adjustment for multiple corrections', value=afmc))
26 |
27 | mt <- self$results$design$matrix
28 | for (i in seq_len(ncells))
29 | mt$addColumn(name = paste(i), title = '')
30 | for (i in seq_len(ncells))
31 | mt$addRow(rowKey=i)
32 |
33 | rt <- self$results$sims$results
34 | ut <- self$results$sims$multi
35 |
36 | formula <- as.formula(paste('~', paste(paste0('`', seq_len(n_factors), '`'), collapse='*')))
37 | terms <- attr(stats::terms(formula), 'term.labels')
38 |
39 | for (term in terms)
40 | rt$addRow(rowKey=term)
41 |
42 | n_pc <- (((prod(
43 | as.numeric(strsplit(design, "\\D+")[[1]])
44 | )) ^ 2) - prod(as.numeric(strsplit(design, "\\D+")[[1]])))/2
45 |
46 | for (i in seq_len(n_pc))
47 | ut$addRow(rowKey=i)
48 |
49 | },
50 | .run = function() {
51 |
52 | mu <- self$options$mu
53 | labelnames <- self$options$labelnames
54 |
55 | mu <- as.numeric(strsplit(mu, ',')[[1]])
56 |
57 | if (labelnames == '')
58 | labelnames <- NULL
59 | else
60 | labelnames <- trimws(strsplit(labelnames, ',')[[1]])
61 |
62 | design <- ANOVA_design(
63 | design = self$options$design,
64 | n = self$options$n,
65 | mu = mu,
66 | sd = self$options$sd,
67 | r = self$options$r,
68 | labelnames = labelnames,
69 | plot = FALSE)
70 |
71 | # self$results$text$setContent(design)
72 |
73 | formula <- paste(as.character(design$frml1)[2:3], collapse=' ~ ')
74 | dt <- self$results$design$summary
75 | dt$setRow(rowNo=2, values=list(value=formula))
76 |
77 | mt <- self$results$design$matrix
78 | for (i in seq_len(ncol(design$cor_mat))) {
79 | mt$addColumn(
80 | name = paste(i),
81 | title = ''
82 | )
83 | }
84 |
85 | for (i in seq_len(nrow(design$cor_mat))) {
86 | values <- design$cor_mat[i,]
87 | names(values) <- seq_len(ncol(design$cor_mat))
88 | mt$setRow(rowKey=i, values=values)
89 | }
90 |
91 | pt <- self$results$design$plot
92 | if (self$options$plot && pt$isNotFilled())
93 | pt$setState(design$meansplot)
94 |
95 | private$.checkpoint()
96 |
97 | if ( ! self$options$simulate)
98 | return()
99 |
100 | rt <- self$results$sims$results
101 | ut <- self$results$sims$multi
102 |
103 | if (rt$isFilled())
104 | return()
105 |
106 | results <- ANOVA_power(
107 | design,
108 | alpha_level = self$options$alpha_level,
109 | p_adjust = self$options$p_adjust,
110 | nsims = self$options$nsims)
111 |
112 | rt <- self$results$sims$results
113 | ut <- self$results$sims$multi
114 |
115 | main <- results$main_results
116 | names <- row.names(main)
117 | for (i in seq_len(nrow(main))) {
118 | rt$setRow(rowNo=i, values=list(
119 | name=names[i],
120 | power=main[i,1],
121 | es=main[i,2]
122 | ))
123 | }
124 |
125 | pc <- results$pc_results
126 | names <- row.names(pc)
127 | for (i in seq_len(nrow(pc))) {
128 | ut$setRow(rowNo=i, values=list(
129 | name=names[i],
130 | power=pc[i,1],
131 | es=pc[i,2]
132 | ))
133 | }
134 |
135 | # self$results$text$setContent(results)
136 | },
137 | .plot=function(image, ...) {
138 | image$state
139 | })
140 | )
141 |
--------------------------------------------------------------------------------
/R/jmvpower.h.R:
--------------------------------------------------------------------------------
1 |
2 | # This file is automatically generated, you probably don't want to edit this
3 |
4 | jmvpowerOptions <- if (requireNamespace('jmvcore')) R6::R6Class(
5 | "jmvpowerOptions",
6 | inherit = jmvcore::Options,
7 | public = list(
8 | initialize = function(
9 | design = "2b*2w",
10 | labelnames = "AGE,old,young,SPEED,fast,slow",
11 | n = 80,
12 | sd = 1.03,
13 | r = 0.87,
14 | mu = "1.03, 1.21, 0.98, 1.01",
15 | plot = TRUE,
16 | simulate = FALSE,
17 | alpha_level = 0.05,
18 | nsims = 100,
19 | p_adjust = "holm", ...) {
20 |
21 | super$initialize(
22 | package='ANOVApower',
23 | name='jmvpower',
24 | requiresData=FALSE,
25 | ...)
26 |
27 | private$..design <- jmvcore::OptionString$new(
28 | "design",
29 | design,
30 | default="2b*2w")
31 | private$..labelnames <- jmvcore::OptionString$new(
32 | "labelnames",
33 | labelnames,
34 | default="AGE,old,young,SPEED,fast,slow")
35 | private$..n <- jmvcore::OptionInteger$new(
36 | "n",
37 | n,
38 | default=80,
39 | min=3)
40 | private$..sd <- jmvcore::OptionNumber$new(
41 | "sd",
42 | sd,
43 | default=1.03,
44 | min=0.0001)
45 | private$..r <- jmvcore::OptionNumber$new(
46 | "r",
47 | r,
48 | default=0.87)
49 | private$..mu <- jmvcore::OptionString$new(
50 | "mu",
51 | mu,
52 | default="1.03, 1.21, 0.98, 1.01")
53 | private$..plot <- jmvcore::OptionBool$new(
54 | "plot",
55 | plot,
56 | default=TRUE)
57 | private$..simulate <- jmvcore::OptionBool$new(
58 | "simulate",
59 | simulate,
60 | default=FALSE,
61 | hidden=TRUE)
62 | private$..alpha_level <- jmvcore::OptionNumber$new(
63 | "alpha_level",
64 | alpha_level,
65 | min=0,
66 | max=1,
67 | default=0.05)
68 | private$..nsims <- jmvcore::OptionInteger$new(
69 | "nsims",
70 | nsims,
71 | min=100,
72 | max=10000,
73 | default=100)
74 | private$..p_adjust <- jmvcore::OptionList$new(
75 | "p_adjust",
76 | p_adjust,
77 | options=list(
78 | "none",
79 | "holm",
80 | "bonferroni",
81 | "fdr"),
82 | default="holm")
83 |
84 | self$.addOption(private$..design)
85 | self$.addOption(private$..labelnames)
86 | self$.addOption(private$..n)
87 | self$.addOption(private$..sd)
88 | self$.addOption(private$..r)
89 | self$.addOption(private$..mu)
90 | self$.addOption(private$..plot)
91 | self$.addOption(private$..simulate)
92 | self$.addOption(private$..alpha_level)
93 | self$.addOption(private$..nsims)
94 | self$.addOption(private$..p_adjust)
95 | }),
96 | active = list(
97 | design = function() private$..design$value,
98 | labelnames = function() private$..labelnames$value,
99 | n = function() private$..n$value,
100 | sd = function() private$..sd$value,
101 | r = function() private$..r$value,
102 | mu = function() private$..mu$value,
103 | plot = function() private$..plot$value,
104 | simulate = function() private$..simulate$value,
105 | alpha_level = function() private$..alpha_level$value,
106 | nsims = function() private$..nsims$value,
107 | p_adjust = function() private$..p_adjust$value),
108 | private = list(
109 | ..design = NA,
110 | ..labelnames = NA,
111 | ..n = NA,
112 | ..sd = NA,
113 | ..r = NA,
114 | ..mu = NA,
115 | ..plot = NA,
116 | ..simulate = NA,
117 | ..alpha_level = NA,
118 | ..nsims = NA,
119 | ..p_adjust = NA)
120 | )
121 |
122 | jmvpowerResults <- if (requireNamespace('jmvcore')) R6::R6Class(
123 | inherit = jmvcore::Group,
124 | active = list(
125 | design = function() private$.items[["design"]],
126 | sims = function() private$.items[["sims"]]),
127 | private = list(),
128 | public=list(
129 | initialize=function(options) {
130 | super$initialize(
131 | options=options,
132 | name="",
133 | title="ANOVA power")
134 | self$add(R6::R6Class(
135 | inherit = jmvcore::Group,
136 | active = list(
137 | summary = function() private$.items[["summary"]],
138 | matrix = function() private$.items[["matrix"]],
139 | plot = function() private$.items[["plot"]]),
140 | private = list(),
141 | public=list(
142 | initialize=function(options) {
143 | super$initialize(
144 | options=options,
145 | name="design",
146 | title="Design")
147 | self$add(jmvcore::Table$new(
148 | options=options,
149 | name="summary",
150 | title="Design",
151 | rows=4,
152 | columns=list(
153 | list(
154 | `name`="name",
155 | `title`="",
156 | `type`="text"),
157 | list(
158 | `name`="value",
159 | `title`="",
160 | `type`="text")),
161 | clearWith=list(
162 | "design",
163 | "labelnames",
164 | "n",
165 | "sd",
166 | "r",
167 | "mu")))
168 | self$add(jmvcore::Table$new(
169 | options=options,
170 | name="matrix",
171 | title="Correlation matrix",
172 | columns=list(),
173 | clearWith=list(
174 | "design",
175 | "labelnames",
176 | "n",
177 | "sd",
178 | "r",
179 | "mu")))
180 | self$add(jmvcore::Image$new(
181 | options=options,
182 | name="plot",
183 | title="Means plot",
184 | visible="(plot)",
185 | renderFun=".plot",
186 | clearWith=list(
187 | "design",
188 | "labelnames",
189 | "n",
190 | "sd",
191 | "r",
192 | "mu")))}))$new(options=options))
193 | self$add(R6::R6Class(
194 | inherit = jmvcore::Group,
195 | active = list(
196 | results = function() private$.items[["results"]],
197 | multi = function() private$.items[["multi"]]),
198 | private = list(),
199 | public=list(
200 | initialize=function(options) {
201 | super$initialize(
202 | options=options,
203 | name="sims",
204 | title="Simulation")
205 | self$add(jmvcore::Table$new(
206 | options=options,
207 | name="results",
208 | title="Results",
209 | visible="(simulate)",
210 | columns=list(
211 | list(
212 | `name`="name",
213 | `title`="",
214 | `type`="text"),
215 | list(
216 | `name`="power",
217 | `title`="Power"),
218 | list(
219 | `name`="es",
220 | `title`="Effect-size")),
221 | clearWith=list(
222 | "design",
223 | "labelnames",
224 | "n",
225 | "sd",
226 | "r",
227 | "mu",
228 | "nsims",
229 | "alpha_level",
230 | "p_adjust")))
231 | self$add(jmvcore::Table$new(
232 | options=options,
233 | name="multi",
234 | title="Multiple comparisons",
235 | visible="(simulate)",
236 | columns=list(
237 | list(
238 | `name`="name",
239 | `title`="",
240 | `type`="text"),
241 | list(
242 | `name`="power",
243 | `title`="Power"),
244 | list(
245 | `name`="es",
246 | `title`="Effect-size")),
247 | clearWith=list(
248 | "design",
249 | "labelnames",
250 | "n",
251 | "sd",
252 | "r",
253 | "mu",
254 | "nsims",
255 | "alpha_level",
256 | "p_adjust")))}))$new(options=options))}))
257 |
258 | jmvpowerBase <- if (requireNamespace('jmvcore')) R6::R6Class(
259 | "jmvpowerBase",
260 | inherit = jmvcore::Analysis,
261 | public = list(
262 | initialize = function(options, data=NULL, datasetId="", analysisId="", revision=0) {
263 | super$initialize(
264 | package = 'ANOVApower',
265 | name = 'jmvpower',
266 | version = c(1,0,0),
267 | options = options,
268 | results = jmvpowerResults$new(options=options),
269 | data = data,
270 | datasetId = datasetId,
271 | analysisId = analysisId,
272 | revision = revision,
273 | pause = NULL,
274 | completeWhenFilled = FALSE)
275 | }))
276 |
277 | #' ANOVA power
278 | #'
279 | #'
280 | #' @param design a string describing the design
281 | #' @param labelnames a comma separated string describing the factor and level
282 | #' labels
283 | #' @param n an integer specifying the sample size in each condition
284 | #' @param sd a number specifying the group standard deviations
285 | #' @param r a number specifying the correlation between dependent variables
286 | #' @param mu a comma separated string specifying the group means
287 | #' @param plot \code{TRUE} (default) or \code{FALSE} specifying whether to
288 | #' provide a means plot
289 | #' @param simulate \code{TRUE} or \code{FALSE} (default); perform the
290 | #' simulation
291 | #' @param alpha_level a number specifying the alpha level
292 | #' @param nsims an integer specifying the number of simulations to perform
293 | #' @param p_adjust the p-adjustment method to use
294 | #' @return A results object containing:
295 | #' \tabular{llllll}{
296 | #' \code{results$design$summary} \tab \tab \tab \tab \tab a table \cr
297 | #' \code{results$design$matrix} \tab \tab \tab \tab \tab a table \cr
298 | #' \code{results$design$plot} \tab \tab \tab \tab \tab an image \cr
299 | #' \code{results$sims$results} \tab \tab \tab \tab \tab a table \cr
300 | #' \code{results$sims$multi} \tab \tab \tab \tab \tab a table \cr
301 | #' }
302 | #'
303 | #' @importFrom utils data
304 | #' @export
305 | jmvpower <- function(
306 | design = "2b*2w",
307 | labelnames = "AGE,old,young,SPEED,fast,slow",
308 | n = 80,
309 | sd = 1.03,
310 | r = 0.87,
311 | mu = "1.03, 1.21, 0.98, 1.01",
312 | plot = TRUE,
313 | simulate = FALSE,
314 | alpha_level = 0.05,
315 | nsims = 100,
316 | p_adjust = "holm") {
317 |
318 | if ( ! requireNamespace('jmvcore'))
319 | stop('jmvpower requires jmvcore to be installed (restart may be required)')
320 |
321 |
322 | options <- jmvpowerOptions$new(
323 | design = design,
324 | labelnames = labelnames,
325 | n = n,
326 | sd = sd,
327 | r = r,
328 | mu = mu,
329 | plot = plot,
330 | simulate = simulate,
331 | alpha_level = alpha_level,
332 | nsims = nsims,
333 | p_adjust = p_adjust)
334 |
335 | results <- jmvpowerResults$new(
336 | options = options)
337 |
338 | analysis <- jmvpowerClass$new(
339 | options = options,
340 | data = data)
341 |
342 | analysis$run()
343 |
344 | analysis$results
345 | }
346 |
--------------------------------------------------------------------------------
/R/mu_from_ES.R:
--------------------------------------------------------------------------------
1 | #' Convenience function to calculate the means for between designs with one factor (One-Way ANOVA). Can be used to determine the means that should yield a specified effect sizes (expressed in Cohen's f).
2 | #' @param K Number of groups (2, 3, or 4)
3 | #' @param ES Effect size (eta-squared)
4 | #' @return Returns vector of means
5 | #' @examples
6 | #' ## Medium effect size (eta-squared), 2 groups
7 | #' ES <- 0.0588
8 | #' K <- 2
9 | #' mu_from_ES(K = K, ES = ES)
10 | #' @section References:
11 | #' Albers, C., & Lakens, D. (2018). When power analyses based on pilot data are biased: Inaccurate effect size estimators and follow-up bias. Journal of Experimental Social Psychology, 74, 187–195. https://doi.org/10.1016/j.jesp.2017.09.004
12 | #' @import ggplot2
13 | #' @export
14 |
15 | mu_from_ES <- function(K, ES){ # provides the vector of population means for a given population ES and nr of groups
16 |
17 | if (ES >= 1 | ES <= 0 ) {
18 | stop("the ES (partial eta squared) must be less than 1 and greater than zero")
19 | }
20 |
21 | if (K == 2 | K == 3 | K == 4 ){
22 |
23 | } else{stop("Number of levels (k) must be 2, 3, or 4")}
24 |
25 | f2 <- ES/(1-ES)
26 | if(K == 2){
27 | a <- sqrt(f2)
28 | muvec <- c(-a,a)
29 | }
30 | if(K == 3){
31 | a <- sqrt(3*f2/2)
32 | muvec <- c(-a, 0, a)
33 | }
34 | if(K == 4){
35 | a <- sqrt(f2)
36 | muvec <- c(-a, -a, a, a)
37 | } # note: function gives error when K not 2,3,4. But we don't need other K.
38 | return(muvec)
39 | }
40 |
--------------------------------------------------------------------------------
/R/plot_power.R:
--------------------------------------------------------------------------------
1 | #' Convenience function to plot power across a range of sample sizes.
2 | #' @param design_result Output from the ANOVA_design function
3 | #' @param alpha_level Alpha level used to determine statistical significance
4 | #' @param min_n Minimum sample size in power curve.
5 | #' @param max_n Maximum sample size in power curve.
6 | #' @param plot Should power plot be printed (defaults to TRUE)
7 | #' @return Returns plot with power curves for the ANOVA, and a dataframe with the summary data.
8 | #' @examples
9 | #' design_result <- ANOVA_design(design = "3b",
10 | #' n = 20,
11 | #' mu = c(0,0,0.3),
12 | #' sd = 1,
13 | #' labelnames = c("condition",
14 | #' "cheerful", "neutral", "sad"))
15 | #'
16 | #' plot_power(design_result, min_n = 50, max_n = 70)
17 | #' @section References:
18 | #' too be added
19 | #' @importFrom stats pf qf
20 | #' @importFrom reshape2 melt
21 | #' @importFrom MASS mvrnorm
22 | #' @importFrom afex aov_car
23 | #' @import ggplot2
24 | #' @export
25 |
26 | plot_power <- function(design_result, alpha_level,
27 | min_n = 7, max_n = 100,
28 | plot = TRUE){
29 | design = design_result$design
30 | mu = design_result$mu
31 | sd <- design_result$sd
32 | r <- design_result$r
33 | labelnames <- design_result$labelnames
34 | n <- design_result$n
35 |
36 |
37 | if (missing(alpha_level)) {
38 | alpha_level <- 0.05
39 | }
40 |
41 | if (alpha_level >= 1 | alpha_level <= 0 ) {
42 | stop("alpha_level must be less than 1 and greater than zero")
43 | }
44 |
45 | #Errors with very small sample size; issue with mvrnorm function from MASS package
46 | if (design_result$n < prod(as.numeric(unlist(regmatches(design_result$design,
47 | gregexpr("[[:digit:]]+", design_result$design)))))
48 | ) {
49 | stop("plot_power cannot handle small sample sizes (n < the product of the factors) at this time; please increase the in ANOVA_design function.")
50 | }
51 |
52 | #Check to ensure there is a within subject factor -- if none --> no MANOVA
53 | run_manova <- grepl("w", design_result$design)
54 |
55 | Roy <- function(eig, q, df.res) {
56 | p <- length(eig)
57 | test <- max(eig)
58 | tmp1 <- max(p, q)
59 | tmp2 <- df.res - tmp1 + q
60 | c(test, (tmp2 * test)/tmp1, tmp1, tmp2)
61 | }
62 |
63 | Wilks <- function(eig, q, df.res)
64 | {
65 | test <- prod(1/(1 + eig))
66 | p <- length(eig)
67 | tmp1 <- df.res - 0.5 * (p - q + 1)
68 | tmp2 <- (p * q - 2)/4
69 | tmp3 <- p^2 + q^2 - 5
70 | tmp3 <- if (tmp3 > 0)
71 | sqrt(((p * q)^2 - 4)/tmp3)
72 | else 1
73 | c(test, ((test^(-1/tmp3) - 1) * (tmp1 * tmp3 - 2 * tmp2))/p/q,
74 | p * q, tmp1 * tmp3 - 2 * tmp2)
75 | }
76 |
77 | HL <- function(eig, q, df.res)
78 | {
79 | test <- sum(eig)
80 | p <- length(eig)
81 | m <- 0.5 * (abs(p - q) - 1)
82 | n <- 0.5 * (df.res - p - 1)
83 | s <- min(p, q)
84 | tmp1 <- 2 * m + s + 1
85 | tmp2 <- 2 * (s * n + 1)
86 | c(test, (tmp2 * test)/s/s/tmp1, s * tmp1, tmp2)
87 | }
88 |
89 | Pillai <- function(eig, q, df.res)
90 | {
91 | test <- sum(eig/(1 + eig))
92 | p <- length(eig)
93 | s <- min(p, q)
94 | n <- 0.5 * (df.res - p - 1)
95 | m <- 0.5 * (abs(p - q) - 1)
96 | tmp1 <- 2 * m + s + 1
97 | tmp2 <- 2 * n + s + 1
98 | c(test, (tmp2/tmp1 * test)/(s - test), s * tmp1, s * tmp2)
99 | }
100 |
101 | #Only utilized if MANOVA output included (see run_manova)
102 | Anova.mlm.table <- function(x, ...)
103 | {
104 | test <- x$test
105 | repeated <- x$repeated
106 | ntests <- length(x$terms)
107 | tests <- matrix(NA, ntests, 4)
108 | if (!repeated)
109 | SSPE.qr <- qr(x$SSPE)
110 | for (term in 1:ntests) {
111 | eigs <- Re(eigen(qr.coef(if (repeated)
112 | qr(x$SSPE[[term]])
113 | else
114 | SSPE.qr,
115 | x$SSP[[term]]), symmetric = FALSE)$values)
116 | tests[term, 1:4] <- switch(
117 | test,
118 | Pillai = Pillai(eigs,
119 | x$df[term], x$error.df),
120 | Wilks = Wilks(eigs,
121 | x$df[term], x$error.df),
122 | `Hotelling-Lawley` =
123 | HL(eigs,
124 | x$df[term], x$error.df),
125 | Roy = Roy(eigs,
126 | x$df[term], x$error.df)
127 | )
128 | }
129 | ok <- tests[, 2] >= 0 & tests[, 3] > 0 & tests[, 4] > 0
130 | ok <- !is.na(ok) & ok
131 | tests <- cbind(x$df, tests, pf(tests[ok, 2], tests[ok, 3],
132 | tests[ok, 4], lower.tail = FALSE))
133 | rownames(tests) <- x$terms
134 | colnames(tests) <- c("df", "test_stat", "approx_F", "num_Df",
135 | "den_Df", "p.value")
136 | tests <- structure(as.data.frame(tests), heading = paste("\nType ",
137 | x$type, if (repeated)
138 | " Repeated Measures", " MANOVA Tests: ", test, " test
139 | statistic",
140 | sep = ""), class = c("anova", "data.frame"))
141 | invisible(tests)
142 | }
143 |
144 | #Do one ANOVA to get number of power columns
145 | exact_result <- ANOVA_exact(design_result, alpha_level = alpha_level,
146 | verbose = FALSE)
147 |
148 | length_power <- length(exact_result$main_results$power)
149 |
150 | power_df <- as.data.frame(matrix(0, ncol = length_power + 1,
151 | nrow = max_n + 1 - min_n))
152 | power_df[,1] <- c((min_n):max_n)
153 |
154 | colnames(power_df) <- c("n", row.names(exact_result$main_results))
155 |
156 | if (run_manova == TRUE) {
157 |
158 | length_power_manova <- length(exact_result$manova_results$power)
159 |
160 | power_df_manova <- as.data.frame(matrix(0, ncol = length_power_manova + 1,
161 | nrow = max_n + 1 - min_n))
162 | power_df_manova[, 1] <- c((min_n):max_n)
163 |
164 | colnames(power_df_manova) <- c("n", row.names(exact_result$manova_results))
165 |
166 | }
167 |
168 | for (i in 1:(max_n + 1 - min_n)) {
169 |
170 | design_result <- ANOVA_design(design = design,
171 | n = i + min_n,
172 | mu = mu,
173 | sd = sd,
174 | r = r,
175 | labelnames = labelnames,
176 | plot = FALSE)
177 | dataframe <- design_result$dataframe
178 |
179 | dataframe$y <- suppressMessages({
180 | melt(as.data.frame(
181 | mvrnorm(
182 | n = design_result$n,
183 | mu = design_result$mu,
184 | Sigma = as.matrix(design_result$sigmatrix)
185 | )
186 | ))$value
187 | })
188 |
189 | # We perform the ANOVA using AFEX
190 | #Can be set to NICE to speed up, but required data grabbing from output the change.
191 | aov_result <-
192 | suppressMessages({
193 | aov_car(
194 | design_result$frml1,
195 | #here we use frml1 to enter fromula 1 as designed above on the basis of the design
196 | data = dataframe,
197 | include_aov = FALSE,
198 | #Need development code to get aov include function
199 | anova_table = list(es = "pes",
200 | correction = "none")
201 | )
202 | }) #This reports PES not GES
203 |
204 |
205 | #Add additional statistics
206 | #Create dataframe from afex results
207 | anova_table <- as.data.frame(aov_result$anova_table)
208 | colnames(anova_table) <- c("num_Df", "den_Df", "MSE", "F", "pes", "p")
209 |
210 | anova_table$pes <- exact_result$main_results$partial_eta_squared
211 | #Calculate cohen's f
212 | anova_table$f2 <- anova_table$pes / (1 - anova_table$pes)
213 | #Calculate noncentrality
214 | anova_table$lambda <- anova_table$f2 * anova_table$den_Df
215 |
216 | #minusalpha<- 1-alpha_level
217 | anova_table$Ft <-
218 | qf((1 - alpha_level), anova_table$num_Df, anova_table$den_Df)
219 | #Calculate power
220 | anova_table$power <-
221 | (1 - pf(
222 | anova_table$Ft,
223 | anova_table$num_Df,
224 | anova_table$den_Df,
225 | anova_table$lambda
226 | )) * 100
227 |
228 | power_df[i, 2:(1 + length_power)] <- anova_table$power
229 |
230 | if (run_manova == TRUE) {
231 | manova_result <- Anova.mlm.table(aov_result$Anova)
232 |
233 |
234 |
235 | manova_result$f2 <- exact_result$manova_results$pillai_trace / (1 - exact_result$manova_results$pillai_trace)
236 | manova_result$lambda <- manova_result$f2 * manova_result$den_Df
237 | manova_result$Ft <- qf((1 - alpha_level), manova_result$num_Df, manova_result$den_Df)
238 | manova_result$power <- round(1 - pf(manova_result$Ft,
239 | manova_result$num_Df,
240 | manova_result$den_Df,
241 | manova_result$lambda), 4) * 100
242 |
243 | power_df_manova[i, 2:(1 + length_power_manova)] <- manova_result$power
244 | }
245 |
246 | }
247 |
248 | plot_data <- suppressMessages(melt(power_df, id = c('n')))
249 |
250 | p1 <- ggplot(data = plot_data, aes(x = plot_data$n, y = plot_data$value)) +
251 | geom_line(size = 1.5) +
252 | scale_x_continuous(limits = c(min_n, max_n)) +
253 | scale_y_continuous(limits = c(0, 100), breaks = seq(0, 100, 10)) +
254 | theme_bw() +
255 | labs(x = "Sample size per condition", y = "Power") +
256 | facet_grid(variable ~ .)
257 |
258 | if (run_manova == TRUE) {
259 | plot_data_manova <- suppressMessages(melt(power_df_manova, id = c('n')))
260 |
261 | p2 <- ggplot(data = plot_data_manova,
262 | aes(x = plot_data_manova$n, y = plot_data_manova$value)) +
263 | geom_line(size = 1.5) +
264 | scale_x_continuous(limits = c(min_n, max_n)) +
265 | scale_y_continuous(limits = c(0, 100), breaks = seq(0, 100, 10)) +
266 | theme_bw() +
267 | labs(x = "Sample size per condition", y = "Power") +
268 | facet_grid(variable ~ .)
269 | }
270 |
271 | if (plot == TRUE) {
272 | print(p1)
273 | }
274 |
275 | if (run_manova == FALSE) {
276 | p2 = NULL
277 | power_df_manova = NULL
278 | effect_sizes_manova = NULL
279 | }
280 |
281 | #Save effect sizes
282 | effect_sizes <- exact_result$main_results[,2:3]
283 |
284 | effect_sizes_manova <- exact_result$manova_results[,2:3]
285 |
286 | invisible(list(p1 = p1,
287 | p2 = p2,
288 | power_df = power_df,
289 | power_df_manova = power_df_manova,
290 | effect_sizes = effect_sizes,
291 | effect_sizes_manova = effect_sizes_manova))
292 | }
293 |
--------------------------------------------------------------------------------
/R/power_oneway_between.R:
--------------------------------------------------------------------------------
1 | #' Analytic power calculation for one-way between designs.
2 | #' @param design_result Output from the ANOVA_design function
3 | #' @param alpha_level Alpha level used to determine statistical significance
4 | #' @return
5 | #' mu = means
6 | #'
7 | #' sigma = standard deviation
8 | #'
9 | #' n = sample size
10 | #'
11 | #' alpha_level = alpha level
12 | #'
13 | #' Cohen_f = Cohen f
14 | #'
15 | #' f_2 = Cohen's f^2
16 | #'
17 | #' lambda = lambda
18 | #'
19 | #' F_critical = Criticial F-value
20 | #'
21 | #' power = power
22 | #'
23 | #' df1 = degrees of freedom for the effect
24 | #'
25 | #' df2 = degrees of freedom of the error
26 | #'
27 | #' eta_p_2 = partial eta-squared
28 | #'
29 | #' mean_mat = matrix of the means
30 | #'
31 | #' @examples
32 | #' ## Set up a within design with one factor with 2 levels,
33 | #' ## 40 participants (woh do all conditions), and standard deviation of 2
34 | #' ## with a mean pattern of 1, 0, 1, conditions labeled 'condition'
35 | #' ## with names for levels of "cheerful", "neutral", "sad"
36 | #' design_result <- ANOVA_design(design = "3b", n = 40, mu = c(1, 0, 1),
37 | #' sd = 2, labelnames = c("condition", "cheerful", "neutral", "sad"))
38 | #' power_result <- power_oneway_between(design_result, alpha_level = 0.05)
39 | #' @section References:
40 | #' too be added
41 | #' @importFrom stats pf qf
42 | #' @export
43 | #'
44 | power_oneway_between <- function(design_result, alpha_level=0.05){
45 |
46 | #Error message if design other than 1-way between is input
47 | if(length(design_result$design_factors) != 1 | design_result$design_factors != 0 ){
48 | stop("Only one-way between designs allowed for this function")
49 | }
50 |
51 | mean_mat <- t(matrix(design_result$mu,
52 | nrow = length(design_result$mu)/design_result$factors,
53 | ncol = design_result$factors)) #Create a mean matrix
54 | colnames(mean_mat) <- design_result$design_list
55 | rownames(mean_mat) <- design_result$factornames
56 |
57 | # Using the sweep function to remove rowmeans from the matrix
58 | mean_mat_res <- sweep(mean_mat,2, rowMeans(mean_mat))
59 | mean_mat_res
60 | MS_A <- design_result$n * (sum(mean_mat_res^2)/(length(design_result$mu)-1))
61 | SS_A <- design_result$n * sum(mean_mat_res^2)
62 | MS_error <- design_result$sd^2
63 | SS_error <- MS_error * (design_result$n*length(design_result$mu))
64 | df1 <- length(design_result$mu)-1
65 | df2 <- (design_result$n*length(design_result$mu) - length(design_result$mu))
66 | eta_p_2 <- SS_A/(SS_A+SS_error)
67 | f_2 <- eta_p_2/(1-eta_p_2)
68 | lambda <- f_2 * design_result$n * length(design_result$mu)
69 | # Cohen_f <- sqrt(sum(mean_mat_res^2)/length(design_result$mu))/sd #based on G*power manual page 28
70 | # We just take the sqrt(f_2) because formula above assumes maximum difference of means.
71 | Cohen_f <- sqrt(f_2)
72 | F_critical <- qf(alpha_level, df1, df2, lower.tail=FALSE) # Critical F-Value
73 | power <- pf(F_critical, df1, df2, lambda, lower.tail = FALSE) # power
74 |
75 | invisible(list(mu = design_result$mu,
76 | sigma = design_result$sd,
77 | n = design_result$n,
78 | alpha_level = alpha_level,
79 | Cohen_f = Cohen_f,
80 | f_2 = f_2,
81 | lambda = lambda,
82 | F_critical = F_critical,
83 | power = power,
84 | df1 = df1,
85 | df2 = df2,
86 | eta_p_2 = eta_p_2,
87 | mean_mat = mean_mat))
88 | }
89 |
--------------------------------------------------------------------------------
/R/power_oneway_within.R:
--------------------------------------------------------------------------------
1 | #' Analytic power calculation for one-way within designs.
2 | #' @param design_result Output from the ANOVA_design function
3 | #' @param alpha_level Alpha level used to determine statistical significance
4 | #' @return
5 | #' mu = means
6 | #'
7 | #' sigma = standard deviation
8 | #'
9 | #' n = sample size
10 | #'
11 | #' alpha_level = alpha level
12 | #'
13 | #' Cohen_f = Cohen's f
14 | #'
15 | #' f_2 = Cohen's f squared
16 | #'
17 | #' lambda = lambda
18 | #'
19 | #' F_critical = Critical F-value
20 | #'
21 | #' power = power
22 | #'
23 | #' df1 = degrees of freedom for the effect
24 | #'
25 | #' df2 = degrees of freedom of the error
26 | #'
27 | #' eta_p_2 = partial eta-squared
28 | #'
29 | #' mean_mat = matrix of the means
30 | #'
31 | #' @examples
32 | #' ## Set up a within design with 3 factors,
33 | #' ## with correlation between observations of 0.8,
34 | #' ## 40 participants (who do all conditions), and standard deviation of 2
35 | #' ## with a mean pattern of 1, 0, 1, conditions labeled 'condition' and
36 | #' ## 'voice', with names for levels of "cheerful", "neutral", "sad".
37 | #' design_result <- ANOVA_design(design = "3w", n = 40, r = 0.8,
38 | #' mu = c(1, 0, 1), sd = 2,
39 | #' labelnames = c("condition", "cheerful", "neutral", "sad"))
40 | #' power_result <- power_oneway_within(design_result, alpha_level = 0.05)
41 | #' @section References:
42 | #' too be added
43 | #' @importFrom stats pf qf
44 | #' @export
45 | #'
46 | power_oneway_within <- function(design_result, alpha_level=0.05){
47 |
48 | #Error message if design other than 1-way within is input
49 | if(length(design_result$design_factors) != 1 | design_result$design_factors != 1){
50 | stop("Only one-way within designs allowed for this function")
51 | }
52 |
53 | factor_levels <- as.numeric(strsplit(design_result$design, "\\D+")[[1]])
54 | mean_mat <- t(matrix(design_result$mu,
55 | nrow = length(design_result$mu)/design_result$factors,
56 | ncol = design_result$factors)) #Create a mean matrix
57 | colnames(mean_mat) <- design_result$design_list
58 | rownames(mean_mat) <- design_result$factornames
59 |
60 | # Using the sweep function to remove rowmeans from the matrix
61 | mean_mat_res <- sweep(mean_mat,2, rowMeans(mean_mat))
62 | mean_mat_res
63 | MS_A <- design_result$n * (sum(mean_mat_res^2)/(length(design_result$mu)-1))
64 | SS_A <- design_result$n * sum(mean_mat_res^2)
65 | MS_error <- design_result$sd^2
66 | SS_error <- MS_error * (design_result$n*length(design_result$mu))
67 | df1 <- length(design_result$mu)-1
68 | df2 <- design_result$n*(length(design_result$mu)-1) - (length(design_result$mu)-1)
69 | eta_p_2 <- SS_A/(SS_A+SS_error)
70 | f_2 <- eta_p_2/(1-eta_p_2)
71 | lambda <- (design_result$n * sum(mean_mat_res^2))/(MS_error*(1-design_result$r)) # Formula 2
72 | # From : Park, I., & Schutz, R. W. (1999). “Quick and Easy” Formulae for Approximating Statistical Power in Repeated Measures ANOVA. Measurement in Physical Education and Exercise Science, 3(4), 249–270. https://doi.org/10.1207/s15327841mpee0304_5
73 | # The MS_error is reduced (and the lambda increased) by multiplying it by (1-r) (or 1/(1-r))
74 | Cohen_f <- sqrt(f_2)
75 | #G*power manual second way to calculate lambda from Table 3:
76 | u <- length(design_result$mu)/(1-design_result$r)
77 | lambda_2 <- f_2*u*design_result$n
78 | # Faul, F., Erdfelder, E., Lang, A.-G., & Buchner, A. (2007). G*Power 3: A flexible statistical power analysis program for the social, behavioral, and biomedical sciences. Behavior Research Methods, 39(2), 175–191. https://doi.org/10.3758/BF03193146
79 |
80 | # In G*power Cohen's f is identical in the within and between design.
81 | # In SPSS this is not true. In short, SPSS incorporates the correlation in f (and f^2, and partial eta-sqaured)
82 | # Therefore, from the code above (which equals G*Power) we can calculate the SPSS values.
83 | f_2_SPSS = f_2 * length(design_result$mu)/(length(design_result$mu)-1) * design_result$n/(design_result$n-1) * 1/(1-design_result$r)
84 | eta_p_2_SPSS <- f_2_SPSS/(1+f_2_SPSS)
85 | Cohen_f_SPSS <- sqrt(f_2_SPSS)
86 |
87 | F_critical <- qf(alpha_level, df1, df2, lower.tail=FALSE) # Critical F-Value
88 | power <- pf(F_critical, df1, df2, lambda, lower.tail = FALSE) # power
89 |
90 | invisible(list(mu = design_result$n,
91 | sigma = design_result$sd,
92 | n = design_result$n,
93 | alpha_level = alpha_level,
94 | Cohen_f = Cohen_f,
95 | Cohen_f_SPSS = Cohen_f_SPSS,
96 | f_2 = f_2,
97 | f_2_SPSS = f_2_SPSS,
98 | lambda = lambda,
99 | lambda_2 = lambda_2,
100 | F_critical = F_critical,
101 | power = power,
102 | df1 = df1,
103 | df2 = df2,
104 | eta_p_2 = eta_p_2,
105 | eta_p_2_SPSS = eta_p_2_SPSS,
106 | mean_mat = mean_mat))
107 | }
108 |
--------------------------------------------------------------------------------
/R/power_threeway_between.R:
--------------------------------------------------------------------------------
1 | #' Analytic power calculation for three-way between designs.
2 | #' @param design_result Output from the ANOVA_design function
3 | #' @param alpha_level Alpha level used to determine statistical significance (default to 0.05)
4 | #' @return
5 | #' mu = means
6 | #'
7 | #' sigma = standard deviation
8 | #'
9 | #' n = sample size
10 | #'
11 | #' alpha_level = alpha level
12 | #'
13 | #' Cohen_f_A = Cohen's f for main effect A
14 | #'
15 | #' Cohen_f_B = Cohen's f for main effect B
16 | #'
17 | #' Cohen_f_C = Cohen's f for main effect C
18 | #'
19 | #' Cohen_f_AB = Cohen's f for the A*B interaction
20 | #'
21 | #' Cohen_f_AC = Cohen's f for the A*C interaction
22 | #'
23 | #' Cohen_f_BC = Cohen's f for the B*C interaction
24 | #'
25 | #' Cohen_f_ABC = Cohen's f for the A*B*C interaction
26 | #'
27 | #' f_2_A = Cohen's f squared for main effect A
28 | #'
29 | #' f_2_B = Cohen's f squared for main effect B
30 | #'
31 | #' f_2_C = Cohen's f squared for main effect C
32 | #'
33 | #' f_2_AB = Cohen's f squared for A*B interaction
34 | #'
35 | #' f_2_AC = Cohen's f squared for A*C interaction
36 | #'
37 | #' f_2_BC = Cohen's f squared for B*C interaction
38 | #'
39 | #' f_2_ABC = Cohen's f squared for A*B*C interaction
40 | #'
41 | #' lambda_A = lambda for main effect A
42 | #'
43 | #' lambda_B = lambda for main effect B
44 | #'
45 | #' lambda_C = lambda for main effect C
46 | #'
47 | #' lambda_AB = lambda for A*B interaction
48 | #'
49 | #' lambda_AC = lambda for A*C interaction
50 | #'
51 | #' lambda_BC = lambda for B*C interaction
52 | #'
53 | #' lambda_ABC = lambda for A*B*C interaction
54 | #'
55 | #' critical_F_A = critical F-value for main effect A
56 | #'
57 | #' critical_F_B = critical F-value for main effect B
58 | #'
59 | #' critical_F_C = critical F-value for main effect C
60 | #'
61 | #' critical_F_AB = critical F-value for A*B interaction
62 | #'
63 | #' critical_F_AC = critical F-value for A*C interaction
64 | #'
65 | #' critical_F_BC = critical F-value for B*C interaction
66 | #'
67 | #' critical_F_ABC = critical F-value for A*B*C interaction
68 | #'
69 | #' power_A = power for main effect A
70 | #'
71 | #' power_B = power for main effect B
72 | #'
73 | #' power_C = power for main effect C
74 | #'
75 | #' power_AB = power for A*B interaction
76 | #'
77 | #' power_AC = power for A*C interaction
78 | #'
79 | #' power_BC = power for B*C interaction
80 | #'
81 | #' power_ABC = power for A*B*C interaction
82 | #'
83 | #' df_A = degrees of freedom for main effect A
84 | #'
85 | #' df_B = degrees of freedom for main effect B
86 | #'
87 | #' df_C = degrees of freedom for main effect C
88 | #'
89 | #' df_AB = degrees of freedom for A*B interaction
90 | #'
91 | #' df_AC = degrees of freedom for A*C interaction
92 | #'
93 | #' df_BC = degrees of freedom for B*C interaction
94 | #'
95 | #' df_ABC = degrees of freedom for A*B*C interaction
96 | #'
97 | #' df_error = degrees of freedom for error term
98 | #'
99 | #' eta_p_2_A = partial eta-squared for main effect A
100 | #'
101 | #' eta_p_2_B = partial eta-squared for main effect B
102 | #'
103 | #' eta_p_2_C = partial eta-squared for main effect C
104 | #'
105 | #' eta_p_2_AB = partial eta-squared for A*B interaction
106 | #'
107 | #' eta_p_2_AC = partial eta-squared for A*C interaction
108 | #'
109 | #' eta_p_2_BC = partial eta-squared for B*C interaction
110 | #'
111 | #' eta_p_2_ABC = partial eta-squared for A*B*C interaction
112 | #'
113 | #' mean_mat = matrix of the means
114 | #'
115 | #' @examples
116 | #' design_result <- ANOVA_design(design = "2b*2b*2b", n = 40,
117 | #' mu = c(1, 0, 1, 0, 0, 1, 1, 0), sd = 2,
118 | #' labelnames = c("condition", "cheerful", "sad",
119 | #' "voice", "human", "robot", "color", "green", "red"))
120 | #' power_result <- power_threeway_between(design_result, alpha_level = 0.05)
121 | #' @section References:
122 | #' to be added
123 | #' @importFrom stats pf qf
124 | #' @export
125 | #'
126 | power_threeway_between <- function(design_result, alpha_level=0.05){
127 |
128 | #Error message if design other than 1-way between is input
129 | if(length(design_result$design_factors) != 3 | any(design_result$design_factors != 0)){
130 | stop("Only three-way between designs allowed for this function")
131 | }
132 |
133 | mu_array <- array(design_result$mu, dim = c(length(design_result$labelnameslist[[1]]),
134 | length(design_result$labelnameslist[[2]]),
135 | length(design_result$labelnameslist[[3]])))
136 | #A
137 | mu_A <- apply(mu_array,c(3),mean)
138 | mu_A
139 | #B
140 | mu_B <- apply(mu_array,c(2),mean)
141 | mu_B
142 | #C
143 | mu_C <- apply(mu_array,c(1),mean)
144 | mu_C
145 |
146 | #A*B
147 | mu_AB <- apply(mu_array,c(2,3),mean)
148 | mu_AB
149 | mu_AB <- mu_AB - (mean(design_result$mu) + sweep(mu_AB, 1, rowMeans(mu_AB)) + sweep(mu_AB, 2, colMeans(mu_AB)))
150 | mu_AB
151 |
152 | #A*C
153 | mu_AC <- apply(mu_array,c(1,3),mean)
154 | mu_AC
155 | mu_AC <- mu_AC - (mean(design_result$mu) + sweep(mu_AC, 2, colMeans(mu_AC)) + sweep(mu_AC, 1, rowMeans(mu_AC)))
156 | mu_AC
157 |
158 | #B*C
159 | mu_BC <- apply(mu_array,c(1,2),mean)
160 | mu_BC
161 | mu_BC <- mu_BC - (mean(design_result$mu) + sweep(mu_BC,1, rowMeans(mu_BC)) + sweep(mu_BC,2, colMeans(mu_BC)))
162 | mu_BC
163 |
164 | # Calculate degrees of freedom
165 | df_A <- (length(design_result$labelnameslist[[1]]) - 1) #calculate degrees of freedom 1 - ignoring the * e sphericity correction
166 | df_B <- (length(design_result$labelnameslist[[2]]) - 1) #calculate degrees of freedom 1 - ignoring the * e sphericity correction
167 | df_C <- (length(design_result$labelnameslist[[3]]) - 1) #calculate degrees of freedom 1 - ignoring the * e sphericity correction
168 |
169 | df_AB <- (length(design_result$labelnameslist[[1]]) - 1) * (length(design_result$labelnameslist[[2]]) - 1)
170 | df_AC <- (length(design_result$labelnameslist[[1]]) - 1) * (length(design_result$labelnameslist[[3]]) - 1)
171 | df_BC <- (length(design_result$labelnameslist[[2]]) - 1) * (length(design_result$labelnameslist[[3]]) - 1)
172 |
173 | df_ABC <- (length(design_result$labelnameslist[[1]]) - 1) * (length(design_result$labelnameslist[[2]]) - 1) * (length(design_result$labelnameslist[[3]]) - 1)
174 |
175 | df_error <- (design_result$n * length(design_result$mu)) - (length(design_result$labelnameslist[[1]])) * (length(design_result$labelnames[[2]])) * (length(design_result$labelnames[[3]]))
176 | df_total <- df_error + df_A + df_B + df_C + df_AB + df_AC + df_BC + df_ABC
177 |
178 | # Calculate sum of squares
179 | MS_A <- design_result$n * length(design_result$labelnameslist[[2]]) * length(design_result$labelnameslist[[3]]) * (sum((mu_A - mean(mu_A))^2)/(length(design_result$labelnameslist[[2]])-1))
180 | SS_A <- design_result$n * length(design_result$labelnameslist[[2]]) * length(design_result$labelnameslist[[3]]) * sum((mu_A - mean(mu_A))^2)
181 |
182 | MS_B <- design_result$n * length(design_result$labelnameslist[[1]]) * length(design_result$labelnameslist[[3]]) * (sum((mu_B - mean(mu_B))^2)/(length(design_result$labelnameslist[[2]])-1))
183 | SS_B <- design_result$n * length(design_result$labelnameslist[[1]]) * length(design_result$labelnameslist[[3]]) * sum((mu_B - mean(mu_B))^2)
184 |
185 | MS_C <- design_result$n * length(design_result$labelnameslist[[1]]) * length(design_result$labelnameslist[[2]]) * (sum((mu_C - mean(mu_C))^2)/(length(design_result$labelnameslist[[2]])-1))
186 | SS_C <- design_result$n * length(design_result$labelnameslist[[1]]) * length(design_result$labelnameslist[[2]]) * sum((mu_C - mean(mu_C))^2)
187 |
188 | MS_AB <- design_result$n * length(design_result$labelnameslist[[3]]) * sum(mu_AB^2)/((length(design_result$labelnameslist[[1]])-1) * (length(design_result$labelnameslist[[2]])-1))
189 | SS_AB <- design_result$n * length(design_result$labelnameslist[[3]]) * sum(mu_AB^2)
190 |
191 | SS_AB_between <- design_result$n * length(design_result$labelnameslist[[3]]) * sum((apply(mu_array,c(2,3),mean) - mean(apply(mu_array,c(2,3),mean)))^2)
192 | SS_AB_2 <- SS_AB_between - SS_A - SS_B
193 |
194 | MS_AC <- design_result$n * length(design_result$labelnameslist[[2]]) * sum(mu_AC^2)/((length(design_result$labelnameslist[[1]])-1) * (length(design_result$labelnameslist[[3]])-1))
195 | SS_AC <- design_result$n * length(design_result$labelnameslist[[2]]) * sum(mu_AC^2)
196 |
197 | SS_AC_between <- design_result$n * length(design_result$labelnameslist[[2]]) * sum((apply(mu_array,c(1,3),mean) - mean(apply(mu_array,c(1,3),mean)))^2)
198 | SS_AC_2 <- SS_AC_between - SS_A - SS_C
199 |
200 | MS_BC <- design_result$n * length(design_result$labelnameslist[[1]]) * sum(mu_BC^2)/((length(design_result$labelnameslist[[2]])-1) * (length(design_result$labelnameslist[[3]])-1))
201 | SS_BC <- design_result$n * length(design_result$labelnameslist[[1]]) * sum(mu_BC^2)
202 |
203 | SS_BC_between <- design_result$n * length(design_result$labelnameslist[[1]]) * sum((apply(mu_array,c(1,2),mean) - mean(apply(mu_array,c(1,2),mean)))^2)
204 | SS_BC_2 <- SS_BC_between - SS_B - SS_C
205 |
206 | MS_total <- design_result$sd^2
207 | SS_total <- MS_total * df_total
208 |
209 | SS_ABC_between <- design_result$n * sum((design_result$mu - mean(design_result$mu))^2)
210 | SS_ABC <- SS_ABC_between - SS_A - SS_B - SS_C - SS_AB - SS_AC - SS_BC
211 |
212 | SS_error <- SS_total - SS_A - SS_B - SS_C - SS_AB - SS_AC - SS_BC - SS_ABC
213 | MS_error <- SS_error/df_error
214 |
215 | # Calculate eta-squared
216 | # Note we are using df_total calculating SS_total. eta_p_2 is SS_A/(SS_A + SS_total)
217 | # But ss_total is based on df_total, but we need the total sample size instead.
218 | eta_p_2_A <- SS_A/(SS_A + (MS_total * design_result$n * length(design_result$mu)))
219 | eta_p_2_A
220 | eta_p_2_B <- SS_B/(SS_B + (MS_total * design_result$n * length(design_result$mu)))
221 | eta_p_2_B
222 | eta_p_2_C <- SS_C/(SS_C + (MS_total * design_result$n * length(design_result$mu)))
223 | eta_p_2_C
224 | eta_p_2_AB <- SS_AB/(SS_AB + (MS_total * design_result$n * length(design_result$mu)))
225 | eta_p_2_AB
226 | eta_p_2_AC <- SS_AC/(SS_AC + (MS_total * design_result$n * length(design_result$mu)))
227 | eta_p_2_AC
228 | eta_p_2_BC <- SS_BC/(SS_BC + (MS_total * design_result$n * length(design_result$mu)))
229 | eta_p_2_BC
230 | eta_p_2_ABC <- SS_ABC/(SS_ABC + (MS_total * design_result$n * length(design_result$mu)))
231 | eta_p_2_ABC
232 |
233 | # Cohen f and squared
234 |
235 | f_2_A <- eta_p_2_A/(1-eta_p_2_A)
236 | f_2_B <- eta_p_2_B/(1-eta_p_2_B)
237 | f_2_C <- eta_p_2_C/(1-eta_p_2_C)
238 | f_2_AB <- eta_p_2_AB/(1-eta_p_2_AB)
239 | f_2_AC <- eta_p_2_AC/(1-eta_p_2_AC)
240 | f_2_BC <- eta_p_2_BC/(1-eta_p_2_BC)
241 | f_2_ABC <- eta_p_2_ABC/(1-eta_p_2_ABC)
242 |
243 | Cohen_f_A <- sqrt(f_2_A)
244 | Cohen_f_B <- sqrt(f_2_B)
245 | Cohen_f_C <- sqrt(f_2_C)
246 | Cohen_f_AB <- sqrt(f_2_AB)
247 | Cohen_f_AC <- sqrt(f_2_AC)
248 | Cohen_f_BC <- sqrt(f_2_BC)
249 | Cohen_f_ABC <- sqrt(f_2_ABC)
250 |
251 | # Calculate Lambda
252 | lambda_A <- design_result$n * length(design_result$mu) * f_2_A
253 | lambda_B <- design_result$n * length(design_result$mu) * f_2_B
254 | lambda_C <- design_result$n * length(design_result$mu) * f_2_C
255 |
256 | lambda_AB <- design_result$n * length(design_result$mu) * f_2_AB
257 | lambda_AC <- design_result$n * length(design_result$mu) * f_2_AC
258 | lambda_BC <- design_result$n * length(design_result$mu) * f_2_BC
259 |
260 | lambda_ABC <- design_result$n * length(design_result$mu) * f_2_ABC
261 |
262 | # Calculate Critical F
263 |
264 | F_critical_A <- qf(alpha_level, df_A, df_error, lower.tail=FALSE)
265 | F_critical_B <- qf(alpha_level, df_B, df_error, lower.tail=FALSE)
266 | F_critical_C <- qf(alpha_level, df_C, df_error, lower.tail=FALSE)
267 | F_critical_AB <- qf(alpha_level, df_AB, df_error, lower.tail=FALSE)
268 | F_critical_AC <- qf(alpha_level, df_AC, df_error, lower.tail=FALSE)
269 | F_critical_BC <- qf(alpha_level, df_BC, df_error, lower.tail=FALSE)
270 | F_critical_ABC <- qf(alpha_level, df_ABC, df_error, lower.tail=FALSE)
271 |
272 | #Calculate Power
273 |
274 | power_A <- pf(F_critical_A, df_A, df_error, lambda_A, lower.tail = FALSE)
275 | power_B <- pf(F_critical_B, df_B, df_error, lambda_B, lower.tail = FALSE)
276 | power_C <- pf(F_critical_C, df_C, df_error, lambda_C, lower.tail = FALSE)
277 | power_AB <- pf(F_critical_AB, df_AB, df_error, lambda_AB, lower.tail = FALSE)
278 | power_AC <- pf(F_critical_AC, df_AC, df_error, lambda_AC, lower.tail = FALSE)
279 | power_BC <- pf(F_critical_BC, df_BC, df_error, lambda_BC, lower.tail = FALSE)
280 | power_ABC <- pf(F_critical_ABC, df_ABC, df_error, lambda_ABC, lower.tail = FALSE)
281 |
282 | # F-Value
283 | # F_A <- MS_A/MS_error
284 | # F_B <- MS_B/MS_error
285 | # F_C <- MS_C/MS_error
286 | # F_AB <- MS_AB/MS_error
287 | # F_AC <- MS_AC/MS_error
288 | # F_BC <- MS_BC/MS_error
289 | # F_ABC <- MS_ABC/MS_error
290 | #
291 |
292 | invisible(list(mu = design_result$mu,
293 | sigma = design_result$sd,
294 | n = design_result$n,
295 | alpha_level = alpha_level,
296 | Cohen_f_A = Cohen_f_A,
297 | Cohen_f_B = Cohen_f_B,
298 | Cohen_f_C = Cohen_f_C,
299 | Cohen_f_AB = Cohen_f_AB,
300 | Cohen_f_AC = Cohen_f_AC,
301 | Cohen_f_BC = Cohen_f_BC,
302 | Cohen_f_ABC = Cohen_f_ABC,
303 | f_2_A = f_2_A,
304 | f_2_B = f_2_B,
305 | f_2_C = f_2_C,
306 | f_2_AB = f_2_AB,
307 | f_2_AC = f_2_AC,
308 | f_2_BC = f_2_BC,
309 | f_2_ABC = f_2_ABC,
310 | lambda_A = lambda_A,
311 | lambda_B = lambda_B,
312 | lambda_C = lambda_C,
313 | lambda_AB = lambda_AB,
314 | lambda_AC = lambda_AC,
315 | lambda_BC = lambda_BC,
316 | lambda_ABC = lambda_ABC,
317 | F_critical_A = F_critical_A,
318 | F_critical_B = F_critical_B,
319 | F_critical_C = F_critical_C,
320 | F_critical_AB = F_critical_AB,
321 | F_critical_AC = F_critical_AC,
322 | F_critical_BC = F_critical_BC,
323 | F_critical_ABC = F_critical_ABC,
324 | power_A = power_A,
325 | power_B = power_B,
326 | power_C = power_C,
327 | power_AB = power_AB,
328 | power_AC = power_AC,
329 | power_BC = power_BC,
330 | power_ABC = power_ABC,
331 | df_A = df_A,
332 | df_B = df_B,
333 | df_C = df_C,
334 | df_AB = df_AB,
335 | df_AC = df_AC,
336 | df_BC = df_BC,
337 | df_error = df_error,
338 | eta_p_2_A = eta_p_2_A,
339 | eta_p_2_B = eta_p_2_B,
340 | eta_p_2_C = eta_p_2_C,
341 | eta_p_2_AB = eta_p_2_AB,
342 | eta_p_2_AC = eta_p_2_AC,
343 | eta_p_2_BC = eta_p_2_BC,
344 | eta_p_2_ABC = eta_p_2_ABC,
345 | mean_mat = mu_array))
346 | }
347 |
348 |
--------------------------------------------------------------------------------
/R/power_twoway_between.R:
--------------------------------------------------------------------------------
1 | #' Analytic power calculation for two-way between designs.
2 | #' @param design_result Output from the ANOVA_design function
3 | #' @param alpha_level Alpha level used to determine statistical significance
4 | #' @return
5 | #' mu = means
6 | #'
7 | #' sigma = standard deviation
8 | #'
9 | #' n = sample size
10 | #'
11 | #' alpha_level = alpha level
12 | #'
13 | #' Cohen_f_A = Cohen's f for main effect A
14 | #'
15 | #' Cohen_f_B = Cohen's f for main effect B
16 | #'
17 | #' Cohen_f_AB = Cohen's f for the A*B interaction
18 | #'
19 | #' f_2_A = Cohen's f squared for main effect A
20 | #'
21 | #' f_2_B = Cohen's f squared for main effect B
22 | #'
23 | #' f_2_AB = Cohen's f squared for A*B interaction
24 | #'
25 | #' lambda_A = lambda for main effect A
26 | #'
27 | #' lambda_B = lambda for main effect B
28 | #'
29 | #' lambda_AB = lambda for A*B interaction
30 | #'
31 | #' critical_F_A = critical F-value for main effect A
32 | #'
33 | #' critical_F_B = critical F-value for main effect B
34 | #'
35 | #' critical_F_AB = critical F-value for A*B interaction
36 | #'
37 | #' power_A = power for main effect A
38 | #'
39 | #' power_B = power for main effect B
40 | #'
41 | #' power_AB = power for A*B interaction
42 | #'
43 | #' df_A = degrees of freedom for main effect A
44 | #'
45 | #' df_B = degrees of freedom for main effect B
46 | #'
47 | #' df_AB = degrees of freedom for A*B interaction
48 | #'
49 | #' df_error = degrees of freedom for error term
50 | #'
51 | #' eta_p_2_A = partial eta-squared for main effect A
52 | #'
53 | #' eta_p_2_B = partial eta-squared for main effect B
54 | #'
55 | #' eta_p_2_AB = partial eta-squared for A*B interaction
56 | #'
57 | #' mean_mat = matrix of the means
58 | #'
59 | #' @examples
60 | #' design_result <- ANOVA_design(design = "2b*2b", n = 40, mu = c(1, 0, 1, 0),
61 | #' sd = 2, labelnames = c("condition", "cheerful", "sad",
62 | #' "voice", "human", "robot"))
63 | #' power_result <- power_twoway_between(design_result, alpha_level = 0.05)
64 | #' @section References:
65 | #' too be added
66 | #' @importFrom stats pf qf
67 | #' @export
68 | #'
69 | power_twoway_between <- function(design_result, alpha_level=0.05){
70 |
71 | #Error message if design other than 1-way between is input
72 | if(any(design_result$design_factors != 0) | length(design_result$design_factors) != 2 ){
73 | stop("Only two-way between designs allowed for this function")
74 | }
75 |
76 | mean_mat <- t(matrix(design_result$mu,
77 | nrow = length(design_result$mu)/length(design_result$labelnameslist[[2]]),
78 | ncol = length(design_result$labelnameslist[[1]]))) #Create a mean matrix
79 | colnames(mean_mat) <- design_result$labelnameslist[[1]]
80 | rownames(mean_mat) <- design_result$labelnameslist[[2]]
81 |
82 | mean_mat_AB <- mean_mat - (mean(mean_mat) + sweep(mean_mat,1, rowMeans(mean_mat)) + sweep(mean_mat,2, colMeans(mean_mat)))
83 |
84 | MS_A <- design_result$n * length(design_result$labelnameslist[[1]]) * (sum((colMeans(mean_mat) - mean(mean_mat))^2)/(length(design_result$labelnameslist[[1]])-1))
85 | SS_A <- design_result$n * length(design_result$labelnameslist[[1]]) * sum((colMeans(mean_mat) - mean(mean_mat))^2)
86 |
87 | MS_B <- design_result$n * length(design_result$labelnameslist[[2]]) * (sum((rowMeans(mean_mat) - mean(mean_mat))^2)/(length(design_result$labelnameslist[[2]])-1))
88 | SS_B <- design_result$n * length(design_result$labelnameslist[[2]]) * sum((rowMeans(mean_mat) - mean(mean_mat))^2)
89 |
90 | MS_AB <- design_result$n * sum(mean_mat_AB^2)/((length(design_result$labelnameslist[[1]])-1) * (length(design_result$labelnameslist[[2]])-1))
91 | SS_AB <- design_result$n * sum(mean_mat_AB^2)
92 |
93 | MS_error <- design_result$sd^2
94 | SS_error <- MS_error * (design_result$n*length(design_result$mu))
95 |
96 | # Power calculations
97 | df_A <- (length(design_result$labelnameslist[[1]]) - 1) #calculate degrees of freedom 1 - ignoring the * e sphericity correction
98 | df_B <- (length(design_result$labelnameslist[[2]]) - 1) #calculate degrees of freedom 1 - ignoring the * e sphericity correction
99 | df_AB <- (length(design_result$labelnameslist[[1]])-1) * (length(design_result$labelnameslist[[2]])-1)
100 | df_error <- (design_result$n*length(design_result$mu) - length(design_result$mu))
101 |
102 | eta_p_2_A <- SS_A/(SS_A+SS_error)
103 | eta_p_2_B <- SS_B/(SS_B+SS_error)
104 | eta_p_2_AB <- SS_AB/(SS_AB+SS_error)
105 |
106 | f_2_A <- eta_p_2_A/(1-eta_p_2_A)
107 | Cohen_f_A <- sqrt(f_2_A)
108 | f_2_B <- eta_p_2_B/(1-eta_p_2_B)
109 | Cohen_f_B <- sqrt(f_2_B)
110 | f_2_AB <- eta_p_2_AB/(1-eta_p_2_AB)
111 | Cohen_f_AB <- sqrt(f_2_AB)
112 |
113 | lambda_A <- design_result$n * length(design_result$labelnameslist[[1]]) * sum((rowMeans(mean_mat)-mean(rowMeans(mean_mat)))^2)/design_result$sd^2
114 | lambda_B <- design_result$n * length(design_result$labelnameslist[[2]]) * sum((colMeans(mean_mat)-mean(colMeans(mean_mat)))^2)/design_result$sd^2
115 | lambda_AB <- design_result$n * length(design_result$labelnameslist[[1]]) * length(design_result$labelnameslist[[2]]) * f_2_AB
116 |
117 | F_critical_A <- qf(alpha_level, df_A, df_error, lower.tail=FALSE)
118 | power_A <- pf(F_critical_A, df_A, df_error, lambda_A, lower.tail = FALSE)
119 | F_critical_B <- qf(alpha_level, df_B, df_error, lower.tail=FALSE)
120 | power_B <- pf(F_critical_B, df_B, df_error, lambda_B, lower.tail = FALSE)
121 | F_critical_AB <- qf(alpha_level, df_AB, df_error, lower.tail=FALSE)
122 | power_AB <- pf(F_critical_AB, df_AB, df_error, lambda_AB, lower.tail = FALSE)
123 |
124 | invisible(list(mu = design_result$n,
125 | sigma = design_result$sd,
126 | n = design_result$n,
127 | alpha_level = alpha_level,
128 | Cohen_f_A = Cohen_f_A,
129 | Cohen_f_B = Cohen_f_B,
130 | Cohen_f_AB = Cohen_f_AB,
131 | f_2_A = f_2_A,
132 | f_2_B = f_2_B,
133 | f_2_AB = f_2_AB,
134 | lambda_A = lambda_A,
135 | lambda_B = lambda_B,
136 | lambda_AB = lambda_AB,
137 | F_critical_A = F_critical_A,
138 | F_critical_B = F_critical_B,
139 | F_critical_AB = F_critical_AB,
140 | power_A = power_A,
141 | power_B = power_B,
142 | power_AB = power_AB,
143 | df_A = df_A,
144 | df_B = df_B,
145 | df_AB = df_AB,
146 | df_error = df_error,
147 | eta_p_2_A = eta_p_2_A,
148 | eta_p_2_B = eta_p_2_B,
149 | eta_p_2_AB = eta_p_2_AB,
150 | mean_mat = mean_mat))
151 | }
152 |
--------------------------------------------------------------------------------
/README.Rmd:
--------------------------------------------------------------------------------
1 | ---
2 | output:
3 | github_document:
4 | pandoc_args: --webtex
5 | editor_options:
6 | chunk_output_type: console
7 | ---
8 |
9 | ---
10 | output: github_document
11 | ---
12 |
13 | ```{r setup, include=FALSE}
14 | knitr::opts_chunk$set(echo = TRUE)
15 | ```
16 |
17 | # This project is depracated
18 |
19 | The alpha version of ANOVApower has been transformed into Superpower together with Aaron Caldwell. The new GitHub repository is at: https://github.com/arcaldwell49/Superpower.
20 | An extensive manual for Superpower can be found at: https://aaroncaldwell.us/SuperpowerBook/.
21 | ANOVApower will not be updated. All future development will happen at the SUperpower site.
22 |
23 | The manuscript introducing Superpower can now be found at: https://github.com/Lakens/Simulation_Based_Power_Analysis_For_Factorial_ANOVA_Designs
24 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 |
2 | # This project is depracated
3 |
4 | The alpha version of ANOVApower has been transformed into Superpower
5 | together with Aaron Caldwell. The new GitHub repository is at:
6 | . An extensive manual for
7 | Superpower can be found at: .
8 | ANOVApower will not be updated. All future development will happen at
9 | the SUperpower site.
10 |
11 | The manuscript introducing Superpower can now be found at:
12 |
13 |
--------------------------------------------------------------------------------
/README_files/figure-gfm/mean-plot-1.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Lakens/ANOVApower/41a05129aeb70e344471bac691158285192b0fea/README_files/figure-gfm/mean-plot-1.png
--------------------------------------------------------------------------------
/README_files/figure-gfm/sim-interaction-2-1.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Lakens/ANOVApower/41a05129aeb70e344471bac691158285192b0fea/README_files/figure-gfm/sim-interaction-2-1.png
--------------------------------------------------------------------------------
/README_files/figure-gfm/sim-interaction-2-2.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Lakens/ANOVApower/41a05129aeb70e344471bac691158285192b0fea/README_files/figure-gfm/sim-interaction-2-2.png
--------------------------------------------------------------------------------
/README_files/figure-gfm/sim-interaction-2-3.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Lakens/ANOVApower/41a05129aeb70e344471bac691158285192b0fea/README_files/figure-gfm/sim-interaction-2-3.png
--------------------------------------------------------------------------------
/README_files/figure-gfm/sim-interaction-3-1.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Lakens/ANOVApower/41a05129aeb70e344471bac691158285192b0fea/README_files/figure-gfm/sim-interaction-3-1.png
--------------------------------------------------------------------------------
/README_files/figure-gfm/sim-interaction-3-2.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Lakens/ANOVApower/41a05129aeb70e344471bac691158285192b0fea/README_files/figure-gfm/sim-interaction-3-2.png
--------------------------------------------------------------------------------
/README_files/figure-gfm/unnamed-chunk-1-1.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Lakens/ANOVApower/41a05129aeb70e344471bac691158285192b0fea/README_files/figure-gfm/unnamed-chunk-1-1.png
--------------------------------------------------------------------------------
/README_files/figure-gfm/unnamed-chunk-11-1.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Lakens/ANOVApower/41a05129aeb70e344471bac691158285192b0fea/README_files/figure-gfm/unnamed-chunk-11-1.png
--------------------------------------------------------------------------------
/README_files/figure-gfm/unnamed-chunk-11-2.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Lakens/ANOVApower/41a05129aeb70e344471bac691158285192b0fea/README_files/figure-gfm/unnamed-chunk-11-2.png
--------------------------------------------------------------------------------
/README_files/figure-gfm/unnamed-chunk-11-3.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Lakens/ANOVApower/41a05129aeb70e344471bac691158285192b0fea/README_files/figure-gfm/unnamed-chunk-11-3.png
--------------------------------------------------------------------------------
/README_files/figure-gfm/unnamed-chunk-11-4.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Lakens/ANOVApower/41a05129aeb70e344471bac691158285192b0fea/README_files/figure-gfm/unnamed-chunk-11-4.png
--------------------------------------------------------------------------------
/README_files/figure-gfm/unnamed-chunk-12-1.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Lakens/ANOVApower/41a05129aeb70e344471bac691158285192b0fea/README_files/figure-gfm/unnamed-chunk-12-1.png
--------------------------------------------------------------------------------
/README_files/figure-gfm/unnamed-chunk-13-1.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Lakens/ANOVApower/41a05129aeb70e344471bac691158285192b0fea/README_files/figure-gfm/unnamed-chunk-13-1.png
--------------------------------------------------------------------------------
/README_files/figure-gfm/unnamed-chunk-14-1.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Lakens/ANOVApower/41a05129aeb70e344471bac691158285192b0fea/README_files/figure-gfm/unnamed-chunk-14-1.png
--------------------------------------------------------------------------------
/README_files/figure-gfm/unnamed-chunk-15-1.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Lakens/ANOVApower/41a05129aeb70e344471bac691158285192b0fea/README_files/figure-gfm/unnamed-chunk-15-1.png
--------------------------------------------------------------------------------
/README_files/figure-gfm/unnamed-chunk-17-1.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Lakens/ANOVApower/41a05129aeb70e344471bac691158285192b0fea/README_files/figure-gfm/unnamed-chunk-17-1.png
--------------------------------------------------------------------------------
/README_files/figure-gfm/unnamed-chunk-19-1.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Lakens/ANOVApower/41a05129aeb70e344471bac691158285192b0fea/README_files/figure-gfm/unnamed-chunk-19-1.png
--------------------------------------------------------------------------------
/README_files/figure-gfm/unnamed-chunk-20-1.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Lakens/ANOVApower/41a05129aeb70e344471bac691158285192b0fea/README_files/figure-gfm/unnamed-chunk-20-1.png
--------------------------------------------------------------------------------
/README_files/figure-gfm/unnamed-chunk-22-1.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Lakens/ANOVApower/41a05129aeb70e344471bac691158285192b0fea/README_files/figure-gfm/unnamed-chunk-22-1.png
--------------------------------------------------------------------------------
/README_files/figure-gfm/unnamed-chunk-23-1.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Lakens/ANOVApower/41a05129aeb70e344471bac691158285192b0fea/README_files/figure-gfm/unnamed-chunk-23-1.png
--------------------------------------------------------------------------------
/README_files/figure-gfm/unnamed-chunk-24-1.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Lakens/ANOVApower/41a05129aeb70e344471bac691158285192b0fea/README_files/figure-gfm/unnamed-chunk-24-1.png
--------------------------------------------------------------------------------
/README_files/figure-gfm/unnamed-chunk-26-1.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Lakens/ANOVApower/41a05129aeb70e344471bac691158285192b0fea/README_files/figure-gfm/unnamed-chunk-26-1.png
--------------------------------------------------------------------------------
/README_files/figure-gfm/unnamed-chunk-28-1.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Lakens/ANOVApower/41a05129aeb70e344471bac691158285192b0fea/README_files/figure-gfm/unnamed-chunk-28-1.png
--------------------------------------------------------------------------------
/README_files/figure-gfm/unnamed-chunk-29-1.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Lakens/ANOVApower/41a05129aeb70e344471bac691158285192b0fea/README_files/figure-gfm/unnamed-chunk-29-1.png
--------------------------------------------------------------------------------
/README_files/figure-gfm/unnamed-chunk-30-1.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Lakens/ANOVApower/41a05129aeb70e344471bac691158285192b0fea/README_files/figure-gfm/unnamed-chunk-30-1.png
--------------------------------------------------------------------------------
/README_files/figure-gfm/unnamed-chunk-32-1.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Lakens/ANOVApower/41a05129aeb70e344471bac691158285192b0fea/README_files/figure-gfm/unnamed-chunk-32-1.png
--------------------------------------------------------------------------------
/README_files/figure-gfm/unnamed-chunk-33-1.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Lakens/ANOVApower/41a05129aeb70e344471bac691158285192b0fea/README_files/figure-gfm/unnamed-chunk-33-1.png
--------------------------------------------------------------------------------
/README_files/figure-gfm/unnamed-chunk-35-1.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Lakens/ANOVApower/41a05129aeb70e344471bac691158285192b0fea/README_files/figure-gfm/unnamed-chunk-35-1.png
--------------------------------------------------------------------------------
/README_files/figure-gfm/unnamed-chunk-37-1.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Lakens/ANOVApower/41a05129aeb70e344471bac691158285192b0fea/README_files/figure-gfm/unnamed-chunk-37-1.png
--------------------------------------------------------------------------------
/README_files/figure-gfm/unnamed-chunk-4-1.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Lakens/ANOVApower/41a05129aeb70e344471bac691158285192b0fea/README_files/figure-gfm/unnamed-chunk-4-1.png
--------------------------------------------------------------------------------
/README_files/figure-gfm/unnamed-chunk-6-1.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Lakens/ANOVApower/41a05129aeb70e344471bac691158285192b0fea/README_files/figure-gfm/unnamed-chunk-6-1.png
--------------------------------------------------------------------------------
/README_files/figure-gfm/unnamed-chunk-7-1.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Lakens/ANOVApower/41a05129aeb70e344471bac691158285192b0fea/README_files/figure-gfm/unnamed-chunk-7-1.png
--------------------------------------------------------------------------------
/README_files/figure-gfm/unnamed-chunk-8-1.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Lakens/ANOVApower/41a05129aeb70e344471bac691158285192b0fea/README_files/figure-gfm/unnamed-chunk-8-1.png
--------------------------------------------------------------------------------
/README_files/figure-latex/mean-plot-1.pdf:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Lakens/ANOVApower/41a05129aeb70e344471bac691158285192b0fea/README_files/figure-latex/mean-plot-1.pdf
--------------------------------------------------------------------------------
/README_files/figure-latex/sim-interaction-2-1.pdf:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Lakens/ANOVApower/41a05129aeb70e344471bac691158285192b0fea/README_files/figure-latex/sim-interaction-2-1.pdf
--------------------------------------------------------------------------------
/README_files/figure-latex/sim-interaction-2-2.pdf:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Lakens/ANOVApower/41a05129aeb70e344471bac691158285192b0fea/README_files/figure-latex/sim-interaction-2-2.pdf
--------------------------------------------------------------------------------
/README_files/figure-latex/sim-interaction-2-3.pdf:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Lakens/ANOVApower/41a05129aeb70e344471bac691158285192b0fea/README_files/figure-latex/sim-interaction-2-3.pdf
--------------------------------------------------------------------------------
/README_files/figure-latex/sim-interaction-3-1.pdf:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Lakens/ANOVApower/41a05129aeb70e344471bac691158285192b0fea/README_files/figure-latex/sim-interaction-3-1.pdf
--------------------------------------------------------------------------------
/README_files/figure-latex/sim-interaction-3-2.pdf:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Lakens/ANOVApower/41a05129aeb70e344471bac691158285192b0fea/README_files/figure-latex/sim-interaction-3-2.pdf
--------------------------------------------------------------------------------
/README_files/figure-latex/unnamed-chunk-1-1.pdf:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Lakens/ANOVApower/41a05129aeb70e344471bac691158285192b0fea/README_files/figure-latex/unnamed-chunk-1-1.pdf
--------------------------------------------------------------------------------
/README_files/figure-latex/unnamed-chunk-11-1.pdf:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Lakens/ANOVApower/41a05129aeb70e344471bac691158285192b0fea/README_files/figure-latex/unnamed-chunk-11-1.pdf
--------------------------------------------------------------------------------
/README_files/figure-latex/unnamed-chunk-11-2.pdf:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Lakens/ANOVApower/41a05129aeb70e344471bac691158285192b0fea/README_files/figure-latex/unnamed-chunk-11-2.pdf
--------------------------------------------------------------------------------
/README_files/figure-latex/unnamed-chunk-11-3.pdf:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Lakens/ANOVApower/41a05129aeb70e344471bac691158285192b0fea/README_files/figure-latex/unnamed-chunk-11-3.pdf
--------------------------------------------------------------------------------
/README_files/figure-latex/unnamed-chunk-11-4.pdf:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Lakens/ANOVApower/41a05129aeb70e344471bac691158285192b0fea/README_files/figure-latex/unnamed-chunk-11-4.pdf
--------------------------------------------------------------------------------
/README_files/figure-latex/unnamed-chunk-12-1.pdf:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Lakens/ANOVApower/41a05129aeb70e344471bac691158285192b0fea/README_files/figure-latex/unnamed-chunk-12-1.pdf
--------------------------------------------------------------------------------
/README_files/figure-latex/unnamed-chunk-19-1.pdf:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Lakens/ANOVApower/41a05129aeb70e344471bac691158285192b0fea/README_files/figure-latex/unnamed-chunk-19-1.pdf
--------------------------------------------------------------------------------
/README_files/figure-latex/unnamed-chunk-20-1.pdf:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Lakens/ANOVApower/41a05129aeb70e344471bac691158285192b0fea/README_files/figure-latex/unnamed-chunk-20-1.pdf
--------------------------------------------------------------------------------
/README_files/figure-latex/unnamed-chunk-22-1.pdf:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Lakens/ANOVApower/41a05129aeb70e344471bac691158285192b0fea/README_files/figure-latex/unnamed-chunk-22-1.pdf
--------------------------------------------------------------------------------
/README_files/figure-latex/unnamed-chunk-26-1.pdf:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Lakens/ANOVApower/41a05129aeb70e344471bac691158285192b0fea/README_files/figure-latex/unnamed-chunk-26-1.pdf
--------------------------------------------------------------------------------
/README_files/figure-latex/unnamed-chunk-28-1.pdf:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Lakens/ANOVApower/41a05129aeb70e344471bac691158285192b0fea/README_files/figure-latex/unnamed-chunk-28-1.pdf
--------------------------------------------------------------------------------
/README_files/figure-latex/unnamed-chunk-29-1.pdf:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Lakens/ANOVApower/41a05129aeb70e344471bac691158285192b0fea/README_files/figure-latex/unnamed-chunk-29-1.pdf
--------------------------------------------------------------------------------
/README_files/figure-latex/unnamed-chunk-32-1.pdf:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Lakens/ANOVApower/41a05129aeb70e344471bac691158285192b0fea/README_files/figure-latex/unnamed-chunk-32-1.pdf
--------------------------------------------------------------------------------
/README_files/figure-latex/unnamed-chunk-4-1.pdf:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Lakens/ANOVApower/41a05129aeb70e344471bac691158285192b0fea/README_files/figure-latex/unnamed-chunk-4-1.pdf
--------------------------------------------------------------------------------
/README_files/figure-latex/unnamed-chunk-6-1.pdf:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Lakens/ANOVApower/41a05129aeb70e344471bac691158285192b0fea/README_files/figure-latex/unnamed-chunk-6-1.pdf
--------------------------------------------------------------------------------
/README_files/figure-latex/unnamed-chunk-7-1.pdf:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Lakens/ANOVApower/41a05129aeb70e344471bac691158285192b0fea/README_files/figure-latex/unnamed-chunk-7-1.pdf
--------------------------------------------------------------------------------
/README_files/figure-latex/unnamed-chunk-8-1.pdf:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Lakens/ANOVApower/41a05129aeb70e344471bac691158285192b0fea/README_files/figure-latex/unnamed-chunk-8-1.pdf
--------------------------------------------------------------------------------
/jamovi/0000.yaml:
--------------------------------------------------------------------------------
1 | ---
2 | title: Simulation-Based Power Analysis for ANOVA Designs
3 | name: ANOVApower
4 | version: 0.0.3
5 | jms: '1.0'
6 | authors:
7 | - Aaron Caldwell
8 | - Daniel Lakens
9 | maintainer: Aaron Caldwell
10 | type: R
11 | description: >-
12 | Functions to perform simulations of ANOVA designs of up to three factors.
13 | Calculates the observed power and average observed effect size for all main
14 | effects and interactions in the ANOVA, and all simple comparisons between
15 | conditions. Includes functions for analytic power calculations and additional
16 | helper functions that compute effect sizes for ANOVA designs, observed error
17 | rates in the simulations, and functions to plot power curves.
18 | analyses:
19 | - title: ANOVA power
20 | name: jmvpower
21 | ns: ANOVApower
22 | menuGroup: ANOVApower
23 | menuTitle: ANOVA power
24 |
25 | ...
26 |
--------------------------------------------------------------------------------
/jamovi/jmvpower.a.yaml:
--------------------------------------------------------------------------------
1 | ---
2 | name: jmvpower
3 | title: ANOVA power
4 | menuGroup: ANOVApower
5 | version: '1.0.0'
6 | jas: '1.2'
7 |
8 | options:
9 | - name: design
10 | title: Design
11 | type: String
12 | default: 2b*2w
13 | description:
14 | R: a string describing the design
15 |
16 | - name: labelnames
17 | title: Factor labels
18 | type: String
19 | default: "AGE,old,young,SPEED,fast,slow"
20 | description:
21 | R: a comma separated string describing the factor and level labels
22 |
23 | - name: n
24 | title: Sample size in each condition
25 | type: Integer
26 | default: 80
27 | min: 3
28 | description:
29 | R: an integer specifying the sample size in each condition
30 |
31 | - name: sd
32 | title: Standard deviation
33 | type: Number
34 | default: 1.03
35 | min: .0001
36 | description:
37 | R: a number specifying the group standard deviations
38 |
39 | - name: r
40 | title: Correlation
41 | type: Number
42 | default: 0.87
43 | description:
44 | R: a number specifying the correlation between dependent variables
45 |
46 | - name: mu
47 | title: Group means
48 | type: String
49 | default: "1.03, 1.21, 0.98, 1.01"
50 | description:
51 | R: a comma separated string specifying the group means
52 |
53 | - name: plot
54 | title: Plot means
55 | type: Bool
56 | default: true
57 | description:
58 | R: >
59 | `TRUE` (default) or `FALSE` specifying whether to provide a means plot
60 |
61 | - name: simulate
62 | title: Run simulation
63 | type: Bool
64 | default: false
65 | hidden: true
66 | description:
67 | R: >
68 | `TRUE` or `FALSE` (default); perform the simulation
69 |
70 | - name: alpha_level
71 | title: Alpha level
72 | type: Number
73 | min: 0
74 | max: 1
75 | default: 0.05
76 | description:
77 | R: a number specifying the alpha level
78 |
79 | - name: nsims
80 | title: Number of simulations
81 | type: Integer
82 | min: 100
83 | max: 10000
84 | default: 100
85 | description:
86 | R: an integer specifying the number of simulations to perform
87 |
88 | - name: p_adjust
89 | title: Adjustment method for multiple comparisons
90 | type: List
91 | options:
92 | - name: none
93 | title: None
94 | - name: holm
95 | title: Holm-Bonferroni
96 | - name: bonferroni
97 | title: Bonferroni
98 | - name: fdr
99 | title: False discovery rate
100 | default: holm
101 | description:
102 | R: the p-adjustment method to use
103 | ...
104 |
--------------------------------------------------------------------------------
/jamovi/jmvpower.r.yaml:
--------------------------------------------------------------------------------
1 | ---
2 | name: jmvpower
3 | title: ANOVA power
4 | jrs: '1.1'
5 |
6 | items:
7 | - name: design
8 | title: Design
9 | type: Group
10 | items:
11 | - name: summary
12 | title: Design
13 | type: Table
14 | rows: 4
15 | columns:
16 | - name: name
17 | title: ''
18 | type: text
19 | - name: value
20 | title: ''
21 | type: text
22 | clearWith:
23 | - design
24 | - labelnames
25 | - n
26 | - sd
27 | - r
28 | - mu
29 |
30 | - name: matrix
31 | title: Correlation matrix
32 | type: Table
33 | columns: []
34 | clearWith:
35 | - design
36 | - labelnames
37 | - n
38 | - sd
39 | - r
40 | - mu
41 |
42 | - name: plot
43 | title: Means plot
44 | type: Image
45 | visible: (plot)
46 | renderFun: .plot
47 | clearWith:
48 | - design
49 | - labelnames
50 | - n
51 | - sd
52 | - r
53 | - mu
54 |
55 | - name: sims
56 | title: Simulation
57 | type: Group
58 | items:
59 | - name: results
60 | title: Results
61 | type: Table
62 | visible: (simulate)
63 | columns:
64 | - name: name
65 | title: ''
66 | type: text
67 | - name: power
68 | title: Power
69 | - name: es
70 | title: Effect-size
71 | clearWith:
72 | - design
73 | - labelnames
74 | - n
75 | - sd
76 | - r
77 | - mu
78 | - nsims
79 | - alpha_level
80 | - p_adjust
81 |
82 | - name: multi
83 | title: Multiple comparisons
84 | type: Table
85 | visible: (simulate)
86 | columns:
87 | - name: name
88 | title: ''
89 | type: text
90 | - name: power
91 | title: Power
92 | - name: es
93 | title: Effect-size
94 | clearWith:
95 | - design
96 | - labelnames
97 | - n
98 | - sd
99 | - r
100 | - mu
101 | - nsims
102 | - alpha_level
103 | - p_adjust
104 |
105 | # - name: text
106 | # title: debug
107 | # type: Preformatted
108 |
109 |
110 | ...
111 |
--------------------------------------------------------------------------------
/jamovi/jmvpower.u.yaml:
--------------------------------------------------------------------------------
1 | title: ANOVA power
2 | name: jmvpower
3 | jus: '2.0'
4 | stage: 0
5 | compilerMode: tame
6 | events:
7 | loaded: './main::loaded'
8 | children:
9 | - type: Label
10 | label: Design
11 | margin: large
12 | children:
13 | - type: TextBox
14 | name: design
15 | format: string
16 | events:
17 | change: './main::onChange_option'
18 | - type: TextBox
19 | name: labelnames
20 | format: string
21 | width: largest
22 | events:
23 | change: './main::onChange_option'
24 | - type: TextBox
25 | name: 'n'
26 | format: number
27 | events:
28 | change: './main::onChange_option'
29 | - type: TextBox
30 | name: sd
31 | format: number
32 | events:
33 | change: './main::onChange_option'
34 | - type: TextBox
35 | name: r
36 | format: number
37 | events:
38 | change: './main::onChange_option'
39 | - type: TextBox
40 | name: mu
41 | format: string
42 | width: largest
43 | events:
44 | change: './main::onChange_option'
45 | - type: CheckBox
46 | name: plot
47 | - type: Label
48 | label: Simulation
49 | margin: large
50 | children:
51 | - type: TextBox
52 | name: alpha_level
53 | format: number
54 | events:
55 | change: './main::onChange_option'
56 | - type: TextBox
57 | name: nsims
58 | format: number
59 | events:
60 | change: './main::onChange_option'
61 | - type: ComboBox
62 | name: p_adjust
63 | events:
64 | change: './main::onChange_option'
65 |
--------------------------------------------------------------------------------
/jamovi/js/css.js:
--------------------------------------------------------------------------------
1 |
2 | const css = `
3 |
4 | #sim {
5 | display: inline-block ;
6 | margin-left: 12px ;
7 | padding: 12px ;
8 | background: #3498db;
9 | background-image: linear-gradient(to bottom, #3498db, #2980b9);
10 | box-shadow: 0px 1px 3px #666666;
11 | color: white
12 | }
13 |
14 | #sim:hover {
15 | background: #3cb0fd;
16 | background-image: linear-gradient(to bottom, #3cb0fd, #3498db);
17 | }
18 |
19 | #sim:active {
20 | background: #3498db;
21 | background-image: linear-gradient(to bottom, #3498db, #2980b9);
22 | }
23 |
24 | `;
25 | let node = document.createElement('style');
26 | node.innerHTML = css;
27 | document.body.appendChild(node);
28 |
29 | module.exports = undefined;
30 |
--------------------------------------------------------------------------------
/jamovi/js/main.js:
--------------------------------------------------------------------------------
1 |
2 | require('./css');
3 |
4 | const events = {
5 | loaded(ui) {
6 |
7 | let $contents = ui.view.$el;
8 |
9 | let $button = $(`Run simulation
`);
10 | $button.on('click', () => {
11 | ui.simulate.setValue(true);
12 | });
13 | $button.appendTo($contents);
14 | },
15 |
16 | onChange_option(ui, event) {
17 | ui.simulate.setValue(false);
18 | },
19 | };
20 |
21 | module.exports = events;
22 |
--------------------------------------------------------------------------------
/man/ANOVA_design.Rd:
--------------------------------------------------------------------------------
1 | % Generated by roxygen2: do not edit by hand
2 | % Please edit documentation in R/ANOVA_design.R
3 | \name{ANOVA_design}
4 | \alias{ANOVA_design}
5 | \title{Design function used to specify the parameters used in the simulation}
6 | \usage{
7 | ANOVA_design(design, n, mu, sd, r = 0, labelnames = NULL,
8 | plot = TRUE)
9 | }
10 | \arguments{
11 | \item{design}{String specifying the ANOVA design.}
12 |
13 | \item{n}{Sample size in each condition}
14 |
15 | \item{mu}{Vector specifying mean for each condition}
16 |
17 | \item{sd}{standard deviation for all conditions}
18 |
19 | \item{r}{Correlation between dependent variables (single value or matrix)}
20 |
21 | \item{labelnames}{Optional vector to specifying factor and condition names (recommended, if not used factors and levels are indicated by letters and numbers)}
22 |
23 | \item{plot}{Should means plot be printed (defaults to TRUE)}
24 | }
25 | \value{
26 | Returns Single data-frame with simulated data, design, design list, factor names, formulas for ANOVA, means, sd, correlation, sample size per condition, correlation matrix, covariance matrix, design string, labelnames, labelnameslist, factor names, meansplot
27 | }
28 | \description{
29 | Design function used to specify the parameters used in the simulation
30 | }
31 | \section{References}{
32 |
33 | too be added
34 | }
35 |
36 | \examples{
37 | ## Set up a within design with 2 factors, each with 2 levels,
38 | ## with correlation between observations of 0.8,
39 | ## 40 participants (who do all conditions), and standard deviation of 2
40 | ## with a mean pattern of 1, 0, 1, 0, conditions labeled 'condition' and
41 | ## 'voice', with names for levels of "cheerful", "sad", and "human", "robot"
42 | ANOVA_design(design = "2w*2w", n = 40, mu = c(1, 0, 1, 0), sd = 2, r = 0.8,
43 | labelnames = c("condition", "cheerful", "sad", "voice", "human", "robot"))
44 | }
45 |
--------------------------------------------------------------------------------
/man/ANOVA_exact.Rd:
--------------------------------------------------------------------------------
1 | % Generated by roxygen2: do not edit by hand
2 | % Please edit documentation in R/ANOVA_exact.R
3 | \name{ANOVA_exact}
4 | \alias{ANOVA_exact}
5 | \title{Simulates on exact empirical data set from the design to calculate power}
6 | \usage{
7 | ANOVA_exact(design_result, correction = "none", alpha_level,
8 | verbose = TRUE)
9 | }
10 | \arguments{
11 | \item{design_result}{Output from the ANOVA_design function}
12 |
13 | \item{correction}{Set a correction of violations of sphericity. This can be set to "none", "GG" Grennhouse-Geisser, and "HF" Huynh-Feldt}
14 |
15 | \item{alpha_level}{Alpha level used to determine statistical significance}
16 |
17 | \item{verbose}{Set to FALSE to not print results (default = TRUE)}
18 | }
19 | \value{
20 | Returns dataframe with simulation data (power and effect sizes), anova results and simple effect results, plot of exact data, and alpha_level.
21 | }
22 | \description{
23 | Simulates on exact empirical data set from the design to calculate power
24 | }
25 | \section{References}{
26 |
27 | to be added
28 | }
29 |
30 | \examples{
31 | ## Set up a within design with 2 factors, each with 2 levels,
32 | ## with correlation between observations of 0.8,
33 | ## 40 participants (who do all conditions), and standard deviation of 2
34 | ## with a mean pattern of 1, 0, 1, 0, conditions labeled 'condition' and
35 | ## 'voice', with names for levels of "cheerful", "sad", amd "human", "robot"
36 | design_result <- ANOVA_design(design = "2w*2w", n = 40, mu = c(1, 0, 1, 0),
37 | sd = 2, r = 0.8, labelnames = c("condition", "cheerful",
38 | "sad", "voice", "human", "robot"))
39 | set.seed(252)
40 | exact_result <- ANOVA_exact(design_result, alpha_level = 0.05)
41 | }
42 |
--------------------------------------------------------------------------------
/man/ANOVA_power.Rd:
--------------------------------------------------------------------------------
1 | % Generated by roxygen2: do not edit by hand
2 | % Please edit documentation in R/ANOVA_power.R
3 | \name{ANOVA_power}
4 | \alias{ANOVA_power}
5 | \title{Simulation function used to perform the simulation}
6 | \usage{
7 | ANOVA_power(design_result, alpha_level = 0.05, correction = "none",
8 | p_adjust = "none", nsims = 1000, seed = NULL, verbose = TRUE)
9 | }
10 | \arguments{
11 | \item{design_result}{Output from the ANOVA_design function}
12 |
13 | \item{alpha_level}{Alpha level used to determine statistical significance}
14 |
15 | \item{correction}{Set a correction of violations of sphericity. This can be set to "none", "GG" Grennhouse-Geisser, and "HF" Huynh-Feldt}
16 |
17 | \item{p_adjust}{Correction for multiple comparisons}
18 |
19 | \item{nsims}{number of simulations to perform}
20 |
21 | \item{seed}{Set seed for reproducible results}
22 |
23 | \item{verbose}{Set to FALSE to not print results (default = TRUE)}
24 | }
25 | \value{
26 | Returns dataframe with simulation data (p-values and effect sizes), anova results and simple effect results, plots of p-value distribution, p_adjust = p_adjust, nsims, and alpha_level.
27 | }
28 | \description{
29 | Simulation function used to perform the simulation
30 | }
31 | \section{References}{
32 |
33 | too be added
34 | }
35 |
36 | \examples{
37 | ## Set up a within design with 2 factors, each with 2 levels,
38 | ## with correlation between observations of 0.8,
39 | ## 40 participants (who do all conditions), and standard deviation of 2
40 | ## with a mean pattern of 1, 0, 1, 0, conditions labeled 'condition' and
41 | ## 'voice', with names for levels of "cheerful", "sad", amd "human", "robot"
42 | design_result <- ANOVA_design(design = "2w*2w", n = 40, mu = c(1, 0, 1, 0),
43 | sd = 2, r = 0.8, labelnames = c("condition", "cheerful",
44 | "sad", "voice", "human", "robot"))
45 | power_result <- ANOVA_power(design_result, alpha_level = 0.05,
46 | p_adjust = "none", seed = 2019, nsims = 10)
47 | }
48 |
--------------------------------------------------------------------------------
/man/jmvpower.Rd:
--------------------------------------------------------------------------------
1 | % Generated by roxygen2: do not edit by hand
2 | % Please edit documentation in R/jmvpower.h.R
3 | \name{jmvpower}
4 | \alias{jmvpower}
5 | \title{ANOVA power}
6 | \usage{
7 | jmvpower(design = "2b*2w",
8 | labelnames = "AGE,old,young,SPEED,fast,slow", n = 80, sd = 1.03,
9 | r = 0.87, mu = "1.03, 1.21, 0.98, 1.01", plot = TRUE,
10 | simulate = FALSE, alpha_level = 0.05, nsims = 100,
11 | p_adjust = "holm")
12 | }
13 | \arguments{
14 | \item{design}{a string describing the design}
15 |
16 | \item{labelnames}{a comma separated string describing the factor and level
17 | labels}
18 |
19 | \item{n}{an integer specifying the sample size in each condition}
20 |
21 | \item{sd}{a number specifying the group standard deviations}
22 |
23 | \item{r}{a number specifying the correlation between dependent variables}
24 |
25 | \item{mu}{a comma separated string specifying the group means}
26 |
27 | \item{plot}{\code{TRUE} (default) or \code{FALSE} specifying whether to
28 | provide a means plot}
29 |
30 | \item{simulate}{\code{TRUE} or \code{FALSE} (default); perform the
31 | simulation}
32 |
33 | \item{alpha_level}{a number specifying the alpha level}
34 |
35 | \item{nsims}{an integer specifying the number of simulations to perform}
36 |
37 | \item{p_adjust}{the p-adjustment method to use}
38 | }
39 | \value{
40 | A results object containing:
41 | \tabular{llllll}{
42 | \code{results$design$summary} \tab \tab \tab \tab \tab a table \cr
43 | \code{results$design$matrix} \tab \tab \tab \tab \tab a table \cr
44 | \code{results$design$plot} \tab \tab \tab \tab \tab an image \cr
45 | \code{results$sims$results} \tab \tab \tab \tab \tab a table \cr
46 | \code{results$sims$multi} \tab \tab \tab \tab \tab a table \cr
47 | }
48 | }
49 | \description{
50 | ANOVA power
51 | }
52 |
--------------------------------------------------------------------------------
/man/mu_from_ES.Rd:
--------------------------------------------------------------------------------
1 | % Generated by roxygen2: do not edit by hand
2 | % Please edit documentation in R/mu_from_ES.R
3 | \name{mu_from_ES}
4 | \alias{mu_from_ES}
5 | \title{Convenience function to calculate the means for between designs with one factor (One-Way ANOVA). Can be used to determine the means that should yield a specified effect sizes (expressed in Cohen's f).}
6 | \usage{
7 | mu_from_ES(K, ES)
8 | }
9 | \arguments{
10 | \item{K}{Number of groups (2, 3, or 4)}
11 |
12 | \item{ES}{Effect size (eta-squared)}
13 | }
14 | \value{
15 | Returns vector of means
16 | }
17 | \description{
18 | Convenience function to calculate the means for between designs with one factor (One-Way ANOVA). Can be used to determine the means that should yield a specified effect sizes (expressed in Cohen's f).
19 | }
20 | \section{References}{
21 |
22 | Albers, C., & Lakens, D. (2018). When power analyses based on pilot data are biased: Inaccurate effect size estimators and follow-up bias. Journal of Experimental Social Psychology, 74, 187–195. https://doi.org/10.1016/j.jesp.2017.09.004
23 | }
24 |
25 | \examples{
26 | ## Medium effect size (eta-squared), 2 groups
27 | ES <- 0.0588
28 | K <- 2
29 | mu_from_ES(K = K, ES = ES)
30 | }
31 |
--------------------------------------------------------------------------------
/man/plot_power.Rd:
--------------------------------------------------------------------------------
1 | % Generated by roxygen2: do not edit by hand
2 | % Please edit documentation in R/plot_power.R
3 | \name{plot_power}
4 | \alias{plot_power}
5 | \title{Convenience function to plot power across a range of sample sizes.}
6 | \usage{
7 | plot_power(design_result, alpha_level, min_n = 7, max_n = 100,
8 | plot = TRUE)
9 | }
10 | \arguments{
11 | \item{design_result}{Output from the ANOVA_design function}
12 |
13 | \item{alpha_level}{Alpha level used to determine statistical significance}
14 |
15 | \item{min_n}{Minimum sample size in power curve.}
16 |
17 | \item{max_n}{Maximum sample size in power curve.}
18 |
19 | \item{plot}{Should power plot be printed (defaults to TRUE)}
20 | }
21 | \value{
22 | Returns plot with power curves for the ANOVA, and a dataframe with the summary data.
23 | }
24 | \description{
25 | Convenience function to plot power across a range of sample sizes.
26 | }
27 | \section{References}{
28 |
29 | too be added
30 | }
31 |
32 | \examples{
33 | design_result <- ANOVA_design(design = "3b",
34 | n = 20,
35 | mu = c(0,0,0.3),
36 | sd = 1,
37 | labelnames = c("condition",
38 | "cheerful", "neutral", "sad"))
39 |
40 | plot_power(design_result, min_n = 50, max_n = 70)
41 | }
42 |
--------------------------------------------------------------------------------
/man/power_oneway_between.Rd:
--------------------------------------------------------------------------------
1 | % Generated by roxygen2: do not edit by hand
2 | % Please edit documentation in R/power_oneway_between.R
3 | \name{power_oneway_between}
4 | \alias{power_oneway_between}
5 | \title{Analytic power calculation for one-way between designs.}
6 | \usage{
7 | power_oneway_between(design_result, alpha_level = 0.05)
8 | }
9 | \arguments{
10 | \item{design_result}{Output from the ANOVA_design function}
11 |
12 | \item{alpha_level}{Alpha level used to determine statistical significance}
13 | }
14 | \value{
15 | mu = means
16 |
17 | sigma = standard deviation
18 |
19 | n = sample size
20 |
21 | alpha_level = alpha level
22 |
23 | Cohen_f = Cohen f
24 |
25 | f_2 = Cohen's f^2
26 |
27 | lambda = lambda
28 |
29 | F_critical = Criticial F-value
30 |
31 | power = power
32 |
33 | df1 = degrees of freedom for the effect
34 |
35 | df2 = degrees of freedom of the error
36 |
37 | eta_p_2 = partial eta-squared
38 |
39 | mean_mat = matrix of the means
40 | }
41 | \description{
42 | Analytic power calculation for one-way between designs.
43 | }
44 | \section{References}{
45 |
46 | too be added
47 | }
48 |
49 | \examples{
50 | ## Set up a within design with one factor with 2 levels,
51 | ## 40 participants (woh do all conditions), and standard deviation of 2
52 | ## with a mean pattern of 1, 0, 1, conditions labeled 'condition'
53 | ## with names for levels of "cheerful", "neutral", "sad"
54 | design_result <- ANOVA_design(design = "3b", n = 40, mu = c(1, 0, 1),
55 | sd = 2, labelnames = c("condition", "cheerful", "neutral", "sad"))
56 | power_result <- power_oneway_between(design_result, alpha_level = 0.05)
57 | }
58 |
--------------------------------------------------------------------------------
/man/power_oneway_within.Rd:
--------------------------------------------------------------------------------
1 | % Generated by roxygen2: do not edit by hand
2 | % Please edit documentation in R/power_oneway_within.R
3 | \name{power_oneway_within}
4 | \alias{power_oneway_within}
5 | \title{Analytic power calculation for one-way within designs.}
6 | \usage{
7 | power_oneway_within(design_result, alpha_level = 0.05)
8 | }
9 | \arguments{
10 | \item{design_result}{Output from the ANOVA_design function}
11 |
12 | \item{alpha_level}{Alpha level used to determine statistical significance}
13 | }
14 | \value{
15 | mu = means
16 |
17 | sigma = standard deviation
18 |
19 | n = sample size
20 |
21 | alpha_level = alpha level
22 |
23 | Cohen_f = Cohen's f
24 |
25 | f_2 = Cohen's f squared
26 |
27 | lambda = lambda
28 |
29 | F_critical = Critical F-value
30 |
31 | power = power
32 |
33 | df1 = degrees of freedom for the effect
34 |
35 | df2 = degrees of freedom of the error
36 |
37 | eta_p_2 = partial eta-squared
38 |
39 | mean_mat = matrix of the means
40 | }
41 | \description{
42 | Analytic power calculation for one-way within designs.
43 | }
44 | \section{References}{
45 |
46 | too be added
47 | }
48 |
49 | \examples{
50 | ## Set up a within design with 3 factors,
51 | ## with correlation between observations of 0.8,
52 | ## 40 participants (who do all conditions), and standard deviation of 2
53 | ## with a mean pattern of 1, 0, 1, conditions labeled 'condition' and
54 | ## 'voice', with names for levels of "cheerful", "neutral", "sad".
55 | design_result <- ANOVA_design(design = "3w", n = 40, r = 0.8,
56 | mu = c(1, 0, 1), sd = 2,
57 | labelnames = c("condition", "cheerful", "neutral", "sad"))
58 | power_result <- power_oneway_within(design_result, alpha_level = 0.05)
59 | }
60 |
--------------------------------------------------------------------------------
/man/power_threeway_between.Rd:
--------------------------------------------------------------------------------
1 | % Generated by roxygen2: do not edit by hand
2 | % Please edit documentation in R/power_threeway_between.R
3 | \name{power_threeway_between}
4 | \alias{power_threeway_between}
5 | \title{Analytic power calculation for three-way between designs.}
6 | \usage{
7 | power_threeway_between(design_result, alpha_level = 0.05)
8 | }
9 | \arguments{
10 | \item{design_result}{Output from the ANOVA_design function}
11 |
12 | \item{alpha_level}{Alpha level used to determine statistical significance (default to 0.05)}
13 | }
14 | \value{
15 | mu = means
16 |
17 | sigma = standard deviation
18 |
19 | n = sample size
20 |
21 | alpha_level = alpha level
22 |
23 | Cohen_f_A = Cohen's f for main effect A
24 |
25 | Cohen_f_B = Cohen's f for main effect B
26 |
27 | Cohen_f_C = Cohen's f for main effect C
28 |
29 | Cohen_f_AB = Cohen's f for the A*B interaction
30 |
31 | Cohen_f_AC = Cohen's f for the A*C interaction
32 |
33 | Cohen_f_BC = Cohen's f for the B*C interaction
34 |
35 | Cohen_f_ABC = Cohen's f for the A*B*C interaction
36 |
37 | f_2_A = Cohen's f squared for main effect A
38 |
39 | f_2_B = Cohen's f squared for main effect B
40 |
41 | f_2_C = Cohen's f squared for main effect C
42 |
43 | f_2_AB = Cohen's f squared for A*B interaction
44 |
45 | f_2_AC = Cohen's f squared for A*C interaction
46 |
47 | f_2_BC = Cohen's f squared for B*C interaction
48 |
49 | f_2_ABC = Cohen's f squared for A*B*C interaction
50 |
51 | lambda_A = lambda for main effect A
52 |
53 | lambda_B = lambda for main effect B
54 |
55 | lambda_C = lambda for main effect C
56 |
57 | lambda_AB = lambda for A*B interaction
58 |
59 | lambda_AC = lambda for A*C interaction
60 |
61 | lambda_BC = lambda for B*C interaction
62 |
63 | lambda_ABC = lambda for A*B*C interaction
64 |
65 | critical_F_A = critical F-value for main effect A
66 |
67 | critical_F_B = critical F-value for main effect B
68 |
69 | critical_F_C = critical F-value for main effect C
70 |
71 | critical_F_AB = critical F-value for A*B interaction
72 |
73 | critical_F_AC = critical F-value for A*C interaction
74 |
75 | critical_F_BC = critical F-value for B*C interaction
76 |
77 | critical_F_ABC = critical F-value for A*B*C interaction
78 |
79 | power_A = power for main effect A
80 |
81 | power_B = power for main effect B
82 |
83 | power_C = power for main effect C
84 |
85 | power_AB = power for A*B interaction
86 |
87 | power_AC = power for A*C interaction
88 |
89 | power_BC = power for B*C interaction
90 |
91 | power_ABC = power for A*B*C interaction
92 |
93 | df_A = degrees of freedom for main effect A
94 |
95 | df_B = degrees of freedom for main effect B
96 |
97 | df_C = degrees of freedom for main effect C
98 |
99 | df_AB = degrees of freedom for A*B interaction
100 |
101 | df_AC = degrees of freedom for A*C interaction
102 |
103 | df_BC = degrees of freedom for B*C interaction
104 |
105 | df_ABC = degrees of freedom for A*B*C interaction
106 |
107 | df_error = degrees of freedom for error term
108 |
109 | eta_p_2_A = partial eta-squared for main effect A
110 |
111 | eta_p_2_B = partial eta-squared for main effect B
112 |
113 | eta_p_2_C = partial eta-squared for main effect C
114 |
115 | eta_p_2_AB = partial eta-squared for A*B interaction
116 |
117 | eta_p_2_AC = partial eta-squared for A*C interaction
118 |
119 | eta_p_2_BC = partial eta-squared for B*C interaction
120 |
121 | eta_p_2_ABC = partial eta-squared for A*B*C interaction
122 |
123 | mean_mat = matrix of the means
124 | }
125 | \description{
126 | Analytic power calculation for three-way between designs.
127 | }
128 | \section{References}{
129 |
130 | to be added
131 | }
132 |
133 | \examples{
134 | design_result <- ANOVA_design(design = "2b*2b*2b", n = 40,
135 | mu = c(1, 0, 1, 0, 0, 1, 1, 0), sd = 2,
136 | labelnames = c("condition", "cheerful", "sad",
137 | "voice", "human", "robot", "color", "green", "red"))
138 | power_result <- power_threeway_between(design_result, alpha_level = 0.05)
139 | }
140 |
--------------------------------------------------------------------------------
/man/power_twoway_between.Rd:
--------------------------------------------------------------------------------
1 | % Generated by roxygen2: do not edit by hand
2 | % Please edit documentation in R/power_twoway_between.R
3 | \name{power_twoway_between}
4 | \alias{power_twoway_between}
5 | \title{Analytic power calculation for two-way between designs.}
6 | \usage{
7 | power_twoway_between(design_result, alpha_level = 0.05)
8 | }
9 | \arguments{
10 | \item{design_result}{Output from the ANOVA_design function}
11 |
12 | \item{alpha_level}{Alpha level used to determine statistical significance}
13 | }
14 | \value{
15 | mu = means
16 |
17 | sigma = standard deviation
18 |
19 | n = sample size
20 |
21 | alpha_level = alpha level
22 |
23 | Cohen_f_A = Cohen's f for main effect A
24 |
25 | Cohen_f_B = Cohen's f for main effect B
26 |
27 | Cohen_f_AB = Cohen's f for the A*B interaction
28 |
29 | f_2_A = Cohen's f squared for main effect A
30 |
31 | f_2_B = Cohen's f squared for main effect B
32 |
33 | f_2_AB = Cohen's f squared for A*B interaction
34 |
35 | lambda_A = lambda for main effect A
36 |
37 | lambda_B = lambda for main effect B
38 |
39 | lambda_AB = lambda for A*B interaction
40 |
41 | critical_F_A = critical F-value for main effect A
42 |
43 | critical_F_B = critical F-value for main effect B
44 |
45 | critical_F_AB = critical F-value for A*B interaction
46 |
47 | power_A = power for main effect A
48 |
49 | power_B = power for main effect B
50 |
51 | power_AB = power for A*B interaction
52 |
53 | df_A = degrees of freedom for main effect A
54 |
55 | df_B = degrees of freedom for main effect B
56 |
57 | df_AB = degrees of freedom for A*B interaction
58 |
59 | df_error = degrees of freedom for error term
60 |
61 | eta_p_2_A = partial eta-squared for main effect A
62 |
63 | eta_p_2_B = partial eta-squared for main effect B
64 |
65 | eta_p_2_AB = partial eta-squared for A*B interaction
66 |
67 | mean_mat = matrix of the means
68 | }
69 | \description{
70 | Analytic power calculation for two-way between designs.
71 | }
72 | \section{References}{
73 |
74 | too be added
75 | }
76 |
77 | \examples{
78 | design_result <- ANOVA_design(design = "2b*2b", n = 40, mu = c(1, 0, 1, 0),
79 | sd = 2, labelnames = c("condition", "cheerful", "sad",
80 | "voice", "human", "robot"))
81 | power_result <- power_twoway_between(design_result, alpha_level = 0.05)
82 | }
83 |
--------------------------------------------------------------------------------
/mess/build and check package script.R:
--------------------------------------------------------------------------------
1 | #devtools::build()
2 |
3 | #Run the code below to ensure the package can be installed and passes all tests.
4 |
5 | #Possibly remove objects from environment rm(list = ls(all = TRUE))
6 | #Also detach all packages to ensure tests will run w/o issue on Travis CI
7 | devtools::install()
8 | library(ANOVApower)
9 | devtools::test()
10 | #devtools::check()
11 | covr::package_coverage()
12 |
13 |
14 |
15 |
16 |
--------------------------------------------------------------------------------
/mess/old_functions/plot_power_2x2_within.R:
--------------------------------------------------------------------------------
1 | #' Convenience function to plot power across a range of sample sizes.
2 | #' @param design_result Output from the ANOVA_design function
3 | #' @param max_n Maximum sample size in power curve.
4 | #' @return Returns plots with power curves for the effects, and a dataframe with the summary data.
5 | #' @examples
6 | #' design_result <- ANOVA_design(design = "2w*2w",
7 | #' n = 20,
8 | #' mu = c(0,0,0,0.3),
9 | #' sd = 1,
10 | #' r = c(
11 | #' 0.9, 0.4, 0.4,
12 | #' 0.4, 0.4,
13 | #' 0.9),
14 | #' labelnames = c("condition", "cheerful", "sad",
15 | #' "voice", "human", "robot"))
16 | #'
17 | #' plot_power_2x2_within(design_result, max_n = 30)
18 | #' @section References:
19 | #' to be added
20 | #' @import ggplot2
21 | #' @export
22 | #'
23 | plot_power_2x2_within <- function(design_result, max_n){
24 |
25 | design = design_result$design
26 | mu = design_result$mu
27 | sd <- design_result$sd
28 | r <- design_result$r
29 | labelnames = c("A", "a1", "a2", "B", "b1", "b2")
30 |
31 | n_vec <- seq(from = 5, to = max_n)
32 |
33 | power_A <- numeric(length(n_vec))
34 | power_B <- numeric(length(n_vec))
35 | power_AB <- numeric(length(n_vec))
36 |
37 | for (i in 1:length(n_vec)){
38 | design_result <- ANOVA_design(design = design,
39 | n = n_vec[i],
40 | mu = mu,
41 | sd = sd,
42 | r = r,
43 | labelnames = labelnames)
44 |
45 | power_res <- power_2x2_within(design_result)
46 |
47 | power_A[i] <- power_res$power_A*100
48 | power_B[i] <- power_res$power_B*100
49 | power_AB[i] <- power_res$power_AB*100
50 | }
51 |
52 | res_df <- data.frame(n_vec, power_A, power_B, power_AB)
53 |
54 | p1 <- ggplot(data=res_df, aes(x = n_vec, y = power_A)) +
55 | geom_line( size=1.5) +
56 | scale_x_continuous(limits = c(0, max(n_vec))) +
57 | scale_y_continuous(limits = c(0, 100)) +
58 | theme_bw() +
59 | labs(x="Sample size", y = "Power Factor A")
60 |
61 | p2 <- ggplot(data=res_df, aes(x = n_vec, y = power_AB)) +
62 | geom_line( size=1.5) +
63 | scale_x_continuous(limits = c(0, max(n_vec))) +
64 | scale_y_continuous(limits = c(0, 100)) +
65 | theme_bw() +
66 | labs(x="Sample size", y = "Power Factor B")
67 |
68 | p3 <- ggplot(data=res_df, aes(x = n_vec, y = power_AB)) +
69 | geom_line( size=1.5) +
70 | scale_x_continuous(limits = c(0, max(n_vec))) +
71 | scale_y_continuous(limits = c(0, 100)) +
72 | theme_bw() +
73 | labs(x="Sample size", y = "Power Factor AB")
74 |
75 | invisible(list(p1 = p1,
76 | p2 = p2,
77 | p3 = p3,
78 | power_df = data.frame(paste("f = ",
79 | round(power_res$Cohen_f_A,2),
80 | " ",
81 | round(power_res$Cohen_f_B,2),
82 | " ",
83 | round(power_res$Cohen_f_AB,2),
84 | "\n",
85 | "r = ",
86 | power_res$rho_A,
87 | " ",
88 | power_res$rho_B,
89 | " ",
90 | power_res$rho_AB),
91 | n_vec,
92 | power_A,
93 | power_B,
94 | power_AB)))
95 | }
96 |
--------------------------------------------------------------------------------
/mess/old_functions/plot_power_oneway_between.R:
--------------------------------------------------------------------------------
1 | #' Convenience function to plot power across a range of sample sizes.
2 | #' @param design_result Output from the ANOVA_design function
3 | #' @param max_n Maximum sample size in power curve.
4 | #' @return Returns plots with power curves for the effects, and a dataframe with the summary data.
5 | #' @examples
6 | #' design_result <- ANOVA_design(design = "3b",
7 | #' n = 20,
8 | #' mu = c(0,0,0.3),
9 | #' sd = 1,
10 | #' labelnames = c("condition",
11 | #' "cheerful", "neutral", "sad"))
12 | #'
13 | #' plot_power_oneway_between(design_result, max_n = 30)
14 | #' @section References:
15 | #' too be added
16 | #' @import ggplot2
17 | #' @export
18 |
19 | plot_power_oneway_between <- function(design_result, max_n){
20 |
21 |
22 |
23 | design = design_result$design
24 | mu = design_result$mu
25 | sd <- design_result$sd
26 | r <- design_result$r
27 | labelnames = c(design_result$factornames[[1]], design_result$labelnames[[1]])
28 |
29 | n_vec <- seq(from = 5, to = max_n)
30 |
31 | power_A <- numeric(length(n_vec))
32 |
33 | for (i in 1:length(n_vec)){
34 | design_result <- ANOVA_design(design = design,
35 | n = n_vec[i],
36 | mu = mu,
37 | sd = sd,
38 | r = r,
39 | labelnames = labelnames)
40 |
41 | power_res <- power_oneway_between(design_result)
42 |
43 | power_A[i] <- power_res$power*100
44 | }
45 |
46 | res_df <- data.frame(n_vec, power_A)
47 |
48 | p1 <- ggplot(data=res_df, aes(x = n_vec, y = power_A)) +
49 | geom_line( size=1.5) +
50 | scale_x_continuous(limits = c(0, max(n_vec))) +
51 | scale_y_continuous(limits = c(0, 100)) +
52 | theme_bw() +
53 | labs(x="Sample size", y = "Power Factor A")
54 |
55 | invisible(list(p1 = p1,
56 | power_df = data.frame(paste("f = ",
57 | round(power_res$Cohen_f,2)),
58 | n_vec,
59 | power_A)))
60 | }
61 |
--------------------------------------------------------------------------------
/mess/old_functions/plot_power_oneway_within.R:
--------------------------------------------------------------------------------
1 | #' Convenience function to plot power across a range of sample sizes.
2 | #' @param design_result Output from the ANOVA_design function
3 | #' @param max_n Maximum sample size in power curve.
4 | #' @return Returns plots with power curves for the effects, and a dataframe with the summary data.
5 | #' @examples
6 | #' design_result <- ANOVA_design(design = "3w",
7 | #' n = 20,
8 | #' mu = c(0,0,0.3),
9 | #' sd = 1,
10 | #' r = 0.7,
11 | #' labelnames = c("condition",
12 | #' "cheerful", "neutral", "sad"))
13 | #'
14 | #' plot_power_oneway_within(design_result, max_n = 30)
15 | #' @section References:
16 | #' too be added
17 | #' @import ggplot2
18 | #' @export
19 |
20 | plot_power_oneway_within <- function(design_result, max_n){
21 |
22 |
23 |
24 | design = design_result$design
25 | mu = design_result$mu
26 | sd <- design_result$sd
27 | r <- design_result$r
28 | labelnames = c(design_result$factornames[[1]], design_result$labelnames[[1]])
29 |
30 |
31 | n_vec <- seq(from = 5, to = max_n)
32 |
33 | power_A <- numeric(length(n_vec))
34 |
35 | for (i in 1:length(n_vec)){
36 | design_result <- ANOVA_design(design = design,
37 | n = n_vec[i],
38 | mu = mu,
39 | sd = sd,
40 | r = r,
41 | labelnames = labelnames)
42 |
43 | power_res <- power_oneway_within(design_result)
44 |
45 | power_A[i] <- power_res$power*100
46 | }
47 |
48 | res_df <- data.frame(n_vec, power_res$power)
49 |
50 | p1 <- ggplot(data=res_df, aes(x = n_vec, y = power_A)) +
51 | geom_line( size=1.5) +
52 | scale_x_continuous(limits = c(0, max(n_vec))) +
53 | scale_y_continuous(limits = c(0, 100)) +
54 | theme_bw() +
55 | labs(x="Sample size", y = "Power Factor A")
56 |
57 | invisible(list(p1 = p1,
58 | power_df = data.frame(paste("f = ",
59 | round(power_res$Cohen_f,2)),
60 | n_vec,
61 | power_A)))
62 | }
63 |
--------------------------------------------------------------------------------
/mess/old_functions/plot_power_twoway_between.R:
--------------------------------------------------------------------------------
1 | #' Convenience function to plot power across a range of sample sizes.
2 | #' @param design_result Output from the ANOVA_design function
3 | #' @param max_n Maximum sample size in power curve.
4 | #' @return Returns plots with power curves for the effects, and a dataframe with the summary data.
5 | #' @examples
6 | #' design_result <- ANOVA_design(design = "2b*2b",
7 | #' n = 20,
8 | #' mu = c(0,0,0,0.3),
9 | #' sd = 1,
10 | #' labelnames = c("condition", "cheerful", "sad",
11 | #' "voice", "human", "robot"))
12 | #'
13 | #' plot_power_twoway_between(design_result, max_n = 30)
14 | #' @section References:
15 | #' too be added
16 | #' @import ggplot2
17 | #' @export
18 |
19 | plot_power_twoway_between <- function(design_result, max_n){
20 |
21 | design = design_result$design
22 | mu = design_result$mu
23 | sd <- design_result$sd
24 | r <- design_result$r
25 | labelnames = c("A", "a1", "a2", "B", "b1", "b2")
26 |
27 | n_vec <- seq(from = 5, to = max_n)
28 |
29 | power_A <- numeric(length(n_vec))
30 | power_B <- numeric(length(n_vec))
31 | power_AB <- numeric(length(n_vec))
32 |
33 | for (i in 1:length(n_vec)){
34 | design_result <- ANOVA_design(design = design,
35 | n = n_vec[i],
36 | mu = mu,
37 | sd = sd,
38 | r = r,
39 | labelnames = labelnames)
40 |
41 | power_res <- power_twoway_between(design_result)
42 |
43 | power_A[i] <- power_res$power_A*100
44 | power_B[i] <- power_res$power_B*100
45 | power_AB[i] <- power_res$power_AB*100
46 | }
47 |
48 | res_df <- data.frame(n_vec, power_A, power_B, power_AB)
49 |
50 | p1 <- ggplot(data=res_df, aes(x = n_vec, y = power_A)) +
51 | geom_line( size=1.5) +
52 | scale_x_continuous(limits = c(0, max(n_vec))) +
53 | scale_y_continuous(limits = c(0, 100)) +
54 | theme_bw() +
55 | labs(x="Sample size", y = "Power Factor A")
56 |
57 | p2 <- ggplot(data=res_df, aes(x = n_vec, y = power_AB)) +
58 | geom_line( size=1.5) +
59 | scale_x_continuous(limits = c(0, max(n_vec))) +
60 | scale_y_continuous(limits = c(0, 100)) +
61 | theme_bw() +
62 | labs(x="Sample size", y = "Power Factor B")
63 |
64 | p3 <- ggplot(data=res_df, aes(x = n_vec, y = power_AB)) +
65 | geom_line( size=1.5) +
66 | scale_x_continuous(limits = c(0, max(n_vec))) +
67 | scale_y_continuous(limits = c(0, 100)) +
68 | theme_bw() +
69 | labs(x="Sample size", y = "Power Factor AB")
70 |
71 | invisible(list(p1 = p1,
72 | p2 = p2,
73 | p3 = p3,
74 | power_df = data.frame(paste("f = ",
75 | round(power_res$Cohen_f_A,2),
76 | " ",
77 | round(power_res$Cohen_f_B,2),
78 | " ",
79 | round(power_res$Cohen_f_AB,2),
80 | "\n",
81 | "r = ",
82 | r,
83 | " ",
84 | r,
85 | " ",
86 | r),
87 | n_vec,
88 | power_A,
89 | power_B,
90 | power_AB)))
91 | }
92 |
--------------------------------------------------------------------------------
/mess/old_functions/power_2x2_within.R:
--------------------------------------------------------------------------------
1 | #' Analytic power calculation for 2x2 within designs (needs updating).
2 | #' @param design_result Output from the ANOVA_design function
3 | #' @param alpha_level Alpha level used to determine statistical significance
4 | #' @return
5 | #' mu = means
6 | #'
7 | #' sigma = standard deviation
8 | #'
9 | #' n = sample size
10 | #'
11 | #' rho_A = correlation between dependent factors for effect A
12 | #'
13 | #' rho_B = correlation between dependent factors for effect B
14 | #'
15 | #' rho_AB = correlation between dependent factors for A*B interaction
16 | #'
17 | #' alpha_level = alpha level
18 | #'
19 | #' Cohen_f_A = Cohen's f for main effect A
20 | #'
21 | #' Cohen_f_B = Cohen's f for main effect B
22 | #'
23 | #' Cohen_f_AB = Cohen's f for the A*B interaction
24 | #'
25 | #' lambda_A = lambda for main effect A
26 | #'
27 | #' lambda_B = lambda for main effect B
28 | #'
29 | #' lambda_AB = lambda for A*B interaction
30 | #'
31 | #' critical_F_A = critical F-value for main effect A
32 | #'
33 | #' critical_F_B = critical F-value for main effect B
34 | #'
35 | #' critical_F_AB = critical F-value for A*B interaction
36 | #'
37 | #' power_A = power for main effect A
38 | #'
39 | #' power_B = power for main effect B
40 | #'
41 | #' power_AB = power for A*B interaction
42 | #'
43 | #' mean_mat = matrix of the means
44 | #'
45 | #' @examples
46 | #' design_result <- ANOVA_design(design = "2w*2w", n = 40, mu = c(1, 0, 1, 0),
47 | #' sd = 2, r = 0.8, labelnames = c("condition", "cheerful", "sad",
48 | #' "voice", "human", "robot"))
49 | #' power_result <- power_2x2_within(design_result)
50 | #' @section References:
51 | #' to be added
52 | #' @importFrom stats pf qf
53 | #' @export
54 | #'
55 | power_2x2_within <- function(design_result, alpha_level = 0.05){
56 |
57 | #Error message if design other than 2x2 within is input
58 | if(length(design_result$design_factors) != 2 | any(design_result$design_factors != 1) ){
59 | stop("Only 2x2 within designs allowed for this function")
60 | }
61 |
62 | mu <- design_result$mu
63 | m_A <- length(design_result$labelnames[[1]])
64 | m_B <- length(design_result$labelnames[[2]])
65 | sigma <- design_result$sd
66 | n <- design_result$n
67 | rho_A <- design_result$cor_mat[1,3]
68 | rho_B <- design_result$cor_mat[1,2]
69 | rho_AB <- design_result$cor_mat[1,4]
70 |
71 | mean_mat <- t(matrix(mu,
72 | nrow = 2,
73 | ncol = 2)) #Create a mean matrix
74 | rownames(mean_mat) <- c("a1", "a2")
75 | colnames(mean_mat) <- c("b1", "b2")
76 |
77 | a1 <- mean_mat[1,1] - (mean(mean_mat) + (mean(mean_mat[1,]) - mean(mean_mat)) + (mean(mean_mat[,1]) - mean(mean_mat)))
78 | a2 <- mean_mat[1,2] - (mean(mean_mat) + (mean(mean_mat[1,]) - mean(mean_mat)) + (mean(mean_mat[,2]) - mean(mean_mat)))
79 | b1 <- mean_mat[2,1] - (mean(mean_mat) + (mean(mean_mat[2,]) - mean(mean_mat)) + (mean(mean_mat[,1]) - mean(mean_mat)))
80 | b2 <- mean_mat[2,2] - (mean(mean_mat) + (mean(mean_mat[2,]) - mean(mean_mat)) + (mean(mean_mat[,2]) - mean(mean_mat)))
81 |
82 | k <- 1 #one group (because all factors are within)
83 |
84 | variance_A <- sigma^2 * (1 - rho_A) + sigma^2 * (m_A - 1) * (rho_B - rho_AB) #Variance A
85 |
86 | variance_B <- sigma^2 * (1 - rho_B) + sigma^2 * (m_B - 1) * (rho_A - rho_AB) #Variance B
87 |
88 | variance_AB <- sigma^2 * (1 - max(rho_A, rho_B)) - sigma^2 * (min(rho_A, rho_B) - rho_AB) #Variance AB
89 |
90 |
91 | # For main effect A
92 | f_A <- sqrt(sum((rowMeans(mean_mat)-mean(rowMeans(mean_mat)))^2))/sigma
93 | lambda_A <- n * m_A * sum((rowMeans(mean_mat)-mean(rowMeans(mean_mat)))^2)/variance_A
94 | df1 <- (m_A - 1) #calculate degrees of freedom 1 - ignoring the * e sphericity correction
95 | df2 <- (n - k) * (m_A - 1) #calculate degrees of freedom 2
96 | F_critical_A <- qf(alpha_level, # critical F-vaue
97 | df1,
98 | df2,
99 | lower.tail=FALSE)
100 |
101 | power_A <- pf(qf(alpha_level, #powerer
102 | df1,
103 | df2,
104 | lower.tail = FALSE),
105 | df1,
106 | df2,
107 | lambda_A,
108 | lower.tail = FALSE)
109 |
110 | # For main effect B
111 | f_B <- sqrt(sum((colMeans(mean_mat)-mean(colMeans(mean_mat)))^2))/sigma
112 | lambda_B <- n * m_B * sum((colMeans(mean_mat)-mean(colMeans(mean_mat)))^2)/variance_B
113 | df1 <- (m_B - 1) #calculate degrees of freedom 1
114 | df2 <- (n - k) * (m_B - 1) #calculate degrees of freedom 2
115 | F_critical_B <- qf(alpha_level, # critical F-vaue
116 | df1,
117 | df2,
118 | lower.tail=FALSE)
119 |
120 | power_B <- pf(qf(alpha_level, #powerer
121 | df1,
122 | df2,
123 | lower.tail = FALSE),
124 | df1,
125 | df2,
126 | lambda_B,
127 | lower.tail = FALSE)
128 |
129 | # For the interaction
130 | f_AB <- sqrt(sum(c(a1, a2, b1, b2)^2)/length(mu))/sigma #based on G*powerer manual page 28
131 | lambda_AB <- n * sqrt(sum(c(a1, a2, b1, b2)^2)/length(mu))/ variance_AB
132 | df1 <- (m_A - 1)*(m_B - 1) #calculate degrees of freedom 1
133 | df2 <- (n - k) * (m_A - 1) * (m_B - 1) #calculate degrees of freedom 2
134 | F_critical_AB <- qf(alpha_level, # critical F-vaue
135 | df1,
136 | df2,
137 | lower.tail=FALSE)
138 |
139 | power_AB <- pf(qf(alpha_level, #powerer
140 | df1,
141 | df2,
142 | lower.tail = FALSE),
143 | df1,
144 | df2,
145 | lambda_AB,
146 | lower.tail = FALSE)
147 | invisible(list(mu = mu,
148 | sigma = sigma,
149 | n = n,
150 | rho_A = rho_A,
151 | rho_B = rho_B,
152 | rho_AB = rho_AB,
153 | alpha_level = alpha_level,
154 | Cohen_f_A = f_A,
155 | Cohen_f_B = f_B,
156 | Cohen_f_AB = f_AB,
157 | lambda_A = lambda_A,
158 | lambda_B = lambda_B,
159 | lambda_AB = lambda_AB,
160 | F_critical_A = F_critical_A,
161 | F_critical_B = F_critical_B,
162 | F_critical_AB = F_critical_AB,
163 | power_A = power_A,
164 | power_B = power_B,
165 | power_AB = power_AB,
166 | mean_mat = mean_mat))
167 | }
168 |
--------------------------------------------------------------------------------
/mess/old_functions/power_2x2_within_2.R:
--------------------------------------------------------------------------------
1 | #' Analytic power calculation for twoway within designs.
2 | #' @param design_result Output from the ANOVA_design function
3 | #' @param alpha_level Alpha level used to determine statistical significance
4 | #' @return
5 | #' mu = means
6 | #'
7 | #' sigma = standard deviation
8 | #'
9 | #' n = sample size
10 | #'
11 | #' rho_A = correlation between dependent factors for effect A
12 | #'
13 | #' rho_B = correlation between dependent factors for effect B
14 | #'
15 | #' rho_AB = correlation between dependent factors for A*B interaction
16 | #'
17 | #' alpha_level = alpha level
18 | #'
19 | #' Cohen_f_A = Cohen's f for main effect A
20 | #'
21 | #' Cohen_f_B = Cohen's f for main effect B
22 | #'
23 | #' Cohen_f_AB = Cohen's f for the A*B interaction
24 | #'
25 | #' lambda_A = lambda for main effect A
26 | #'
27 | #' lambda_B = lambda for main effect B
28 | #'
29 | #' lambda_AB = lambda for A*B interaction
30 | #'
31 | #' critical_F_A = critical F-value for main effect A
32 | #'
33 | #' critical_F_B = critical F-value for main effect B
34 | #'
35 | #' critical_F_AB = critical F-value for A*B interaction
36 | #'
37 | #' power_A = power for main effect A
38 | #'
39 | #' power_B = power for main effect B
40 | #'
41 | #' power_AB = power for A*B interaction
42 | #'
43 | #' mean_mat = matrix of the means
44 | #'
45 | #' @examples
46 | #' design_result <- ANOVA_design(string = "2w*2w", n = 40, mu = c(1, 0, 1, 0),
47 | #' sd = 2, r = 0.8, labelnames = c("condition", "cheerful", "sad",
48 | #' "voice", "human", "robot"))
49 | #' power_result <- power_2x2_within(design_result)
50 | #' @section References:
51 | #' to be added
52 | #' @importFrom stats pf qf
53 | #' @export
54 | #'
55 | power_2x2_within_2 <- function(design_result, alpha_level = 0.05){
56 |
57 | mu <- design_result$mu
58 | m_A <- length(design_result$labelnames[[1]])
59 | m_B <- length(design_result$labelnames[[2]])
60 | sigma <- design_result$sd
61 | n <- design_result$n
62 | rho_A <- design_result$r
63 | rho_B <- design_result$r
64 | rho_AB <- design_result$r
65 |
66 | mean_mat <- t(matrix(mu,
67 | nrow = 2,
68 | ncol = 2)) #Create a mean matrix
69 |
70 | mean_mat_AB <- mean_mat - (mean(mean_mat) + sweep(mean_mat,1, rowMeans(mean_mat)) + sweep(mean_mat,2, colMeans(mean_mat)))
71 |
72 | k <- 1 #one group (because all factors are within)
73 |
74 | variance_A <- sigma^2 * (1 - rho_A) + sigma^2 * (m_A - 1) * (rho_B - rho_AB) #Variance A
75 | variance_B <- sigma^2 * (1 - rho_B) + sigma^2 * (m_B - 1) * (rho_A - rho_AB) #Variance B
76 | variance_AB <- sigma^2 * (1 - max(rho_A, rho_B)) - sigma^2 * (min(rho_A, rho_B) - rho_AB) #Variance AB
77 |
78 | # Power calculations
79 | f_A <- sqrt(sum((rowMeans(mean_mat)-mean(rowMeans(mean_mat)))^2))/sigma
80 | f_B <- sqrt(sum((colMeans(mean_mat)-mean(colMeans(mean_mat)))^2))/sigma
81 | f_AB <- sqrt(sum(mean_mat_AB^2)/length(mu))/sigma #based on G*power manual page 28
82 |
83 |
84 | lambda_A <- n * m_A * sum((rowMeans(mean_mat)-mean(rowMeans(mean_mat)))^2)/variance_A
85 | lambda_B <- n * m_B * sum((colMeans(mean_mat)-mean(colMeans(mean_mat)))^2)/variance_B
86 | lambda_AB <- n * sqrt(sum(mean_mat_AB^2)/length(mu))/ variance_AB
87 |
88 | df1_A <- (m_A - 1) #calculate degrees of freedom 1 - ignoring the * e sphericity correction
89 | df2_A <- (n - k) * (m_A - 1) #calculate degrees of freedom 2
90 | df1_B <- (m_B - 1) #calculate degrees of freedom 1
91 | df2_B <- (n - k) * (m_B - 1) #calculate degrees of freedom 2
92 | df1_AB <- (m_A - 1)*(m_B - 1) #calculate degrees of freedom 1
93 | df2_AB <- (n - k) * (m_A - 1) * (m_B - 1) #calculate degrees of freedom 2
94 |
95 | F_critical_A <- qf(alpha_level, df1_A, df2_A, lower.tail=FALSE)
96 | F_critical_B <- qf(alpha_level, df1_B, df2_B, lower.tail=FALSE)
97 | F_critical_AB <- qf(alpha_level, df1_AB, df2_AB, lower.tail = FALSE)
98 |
99 | power_A <- pf(F_critical_A, df1_A, df2_A, lambda_A, lower.tail = FALSE)
100 | power_B <- pf(F_critical_B, df1_B, df2_B, lambda_B, lower.tail = FALSE)
101 | power_AB <- pf(F_critical_AB, df1_AB, df2_AB, lambda_AB, lower.tail = FALSE)
102 |
103 | # For the interaction
104 | invisible(list(mu = mu,
105 | sigma = sigma,
106 | n = n,
107 | rho_A = rho_A,
108 | rho_B = rho_B,
109 | rho_AB = rho_AB,
110 | alpha_level = alpha_level,
111 | Cohen_f_A = f_A,
112 | Cohen_f_B = f_B,
113 | Cohen_f_AB = f_AB,
114 | lambda_A = lambda_A,
115 | lambda_B = lambda_B,
116 | lambda_AB = lambda_AB,
117 | F_critical_A = F_critical_A,
118 | F_critical_B = F_critical_B,
119 | F_critical_AB = F_critical_AB,
120 | power_A = power_A,
121 | power_B = power_B,
122 | power_AB = power_AB,
123 | mean_mat = mean_mat))
124 | }
125 |
--------------------------------------------------------------------------------
/mess/old_functions/power_twoway_within_2.R:
--------------------------------------------------------------------------------
1 | #' Analytic power calculation for twoway within designs.
2 | #' @param design_result Output from the ANOVA_design function
3 | #' @param alpha_level Alpha level used to determine statistical significance
4 | #' @return
5 | #' mu = means
6 | #'
7 | #' sigma = standard deviation
8 | #'
9 | #' n = sample size
10 | #'
11 | #' rho_A = correlation between dependent factors for effect A
12 | #'
13 | #' rho_B = correlation between dependent factors for effect B
14 | #'
15 | #' rho_AB = correlation between dependent factors for A*B interaction
16 | #'
17 | #' alpha_level = alpha level
18 | #'
19 | #' Cohen_f_A = Cohen's f for main effect A
20 | #'
21 | #' Cohen_f_B = Cohen's f for main effect B
22 | #'
23 | #' Cohen_f_AB = Cohen's f for the A*B interaction
24 | #'
25 | #' lambda_A = lambda for main effect A
26 | #'
27 | #' lambda_B = lambda for main effect B
28 | #'
29 | #' lambda_AB = lambda for A*B interaction
30 | #'
31 | #' critical_F_A = critical F-value for main effect A
32 | #'
33 | #' critical_F_B = critical F-value for main effect B
34 | #'
35 | #' critical_F_AB = critical F-value for A*B interaction
36 | #'
37 | #' power_A = power for main effect A
38 | #'
39 | #' power_B = power for main effect B
40 | #'
41 | #' power_AB = power for A*B interaction
42 | #'
43 | #' mean_mat = matrix of the means
44 | #'
45 | #' @examples
46 | #' design_result <- ANOVA_design(string = "2w*2w", n = 40, mu = c(1, 0, 1, 0),
47 | #' sd = 2, r = 0.8, labelnames = c("condition", "cheerful", "sad",
48 | #' "voice", "human", "robot"))
49 | #' power_result <- power_2x2_within(design_result)
50 | #' @section References:
51 | #' to be added
52 | #' @importFrom stats pf qf
53 | #' @export
54 | #'
55 | power_twoway_within_2 <- function(design_result, alpha_level = 0.05){
56 |
57 | mu <- design_result$mu
58 | m_A <- length(design_result$labelnames[[1]])
59 | m_B <- length(design_result$labelnames[[2]])
60 | sigma <- design_result$sd
61 | n <- design_result$n
62 | rho_A <- design_result$r
63 | rho_B <- design_result$r
64 | rho_AB <- design_result$r
65 |
66 | mean_mat <- t(matrix(mu,
67 | nrow = m_A,
68 | ncol = m_B)) #Create a mean matrix
69 |
70 | mean_mat_AB <- mean_mat - (mean(mean_mat) + sweep(mean_mat,1, rowMeans(mean_mat)) + sweep(mean_mat,2, colMeans(mean_mat)))
71 |
72 | k <- 1 #one group (because all factors are within)
73 |
74 | variance_A <- sigma^2 * (1 - rho_A) + sigma^2 * (m_A - 1) * (rho_B - rho_AB) #Variance A
75 | variance_B <- sigma^2 * (1 - rho_B) + sigma^2 * (m_B - 1) * (rho_A - rho_AB) #Variance B
76 | variance_AB <- sigma^2 * (1 - max(rho_A, rho_B)) - sigma^2 * (min(rho_A, rho_B) - rho_AB) #Variance AB
77 |
78 | # Power calculations
79 | f_A <- sqrt(sum((rowMeans(mean_mat)-mean(rowMeans(mean_mat)))^2))/sigma
80 | f_B <- sqrt(sum((colMeans(mean_mat)-mean(colMeans(mean_mat)))^2))/sigma
81 | f_AB <- sqrt(sum(mean_mat_AB^2)/length(mu))/sigma #based on G*power manual page 28
82 |
83 |
84 | lambda_A <- n * m_A * sum((rowMeans(mean_mat)-mean(rowMeans(mean_mat)))^2)/variance_A
85 | lambda_B <- n * m_B * sum((colMeans(mean_mat)-mean(colMeans(mean_mat)))^2)/variance_B
86 | lambda_AB <- n * sqrt(sum(mean_mat_AB^2)/length(mu))/ variance_AB
87 |
88 | df1_A <- (m_A - 1) #calculate degrees of freedom 1 - ignoring the * e sphericity correction
89 | df2_A <- (n - k) * (m_A - 1) #calculate degrees of freedom 2
90 | df1_B <- (m_B - 1) #calculate degrees of freedom 1
91 | df2_B <- (n - k) * (m_B - 1) #calculate degrees of freedom 2
92 | df1_AB <- (m_A - 1)*(m_B - 1) #calculate degrees of freedom 1
93 | df2_AB <- (n - k) * (m_A - 1) * (m_B - 1) #calculate degrees of freedom 2
94 |
95 | F_critical_A <- qf(alpha_level, df1_A, df2_A, lower.tail=FALSE)
96 | F_critical_B <- qf(alpha_level, df1_B, df2_B, lower.tail=FALSE)
97 | F_critical_AB <- qf(alpha_level, df1_AB, df2_AB, lower.tail = FALSE)
98 |
99 | power_A <- pf(F_critical_A, df1_A, df2_A, lambda_A, lower.tail = FALSE)
100 | power_B <- pf(F_critical_B, df1_B, df2_B, lambda_B, lower.tail = FALSE)
101 | power_AB <- pf(F_critical_AB, df1_AB, df2_AB, lambda_AB, lower.tail = FALSE)
102 |
103 | # For the interaction
104 | invisible(list(mu = mu,
105 | sigma = sigma,
106 | n = n,
107 | rho_A = rho_A,
108 | rho_B = rho_B,
109 | rho_AB = rho_AB,
110 | alpha_level = alpha_level,
111 | Cohen_f_A = f_A,
112 | Cohen_f_B = f_B,
113 | Cohen_f_AB = f_AB,
114 | lambda_A = lambda_A,
115 | lambda_B = lambda_B,
116 | lambda_AB = lambda_AB,
117 | F_critical_A = F_critical_A,
118 | F_critical_B = F_critical_B,
119 | F_critical_AB = F_critical_AB,
120 | power_A = power_A,
121 | power_B = power_B,
122 | power_AB = power_AB,
123 | mean_mat = mean_mat))
124 | }
125 |
--------------------------------------------------------------------------------
/mess/old_functions/test-plot_power_2x2_within.R:
--------------------------------------------------------------------------------
1 | context("test-plot_power_2x2_within")
2 |
3 |
4 |
5 | # error messages
6 | test_that("error messages", {
7 |
8 | design_result1 <- ANOVA_design(design = "2w*2w",
9 | n = 100,
10 | mu = c(24,26,25,27),
11 | sd = 6.4,
12 | plot = FALSE)
13 |
14 |
15 | expect_error(plot_power_2x2_within(), "argument \"design_result\" is missing, with no default" )
16 | expect_error(plot_power_2x2_within(design_result1), "argument \"max_n\" is missing, with no default" )
17 |
18 | })
19 |
--------------------------------------------------------------------------------
/mess/old_functions/test-plot_power_oneway_between.R:
--------------------------------------------------------------------------------
1 | context("test-plot_power_oneway_between")
2 |
3 |
4 |
5 | # error messages
6 | test_that("error messages", {
7 |
8 | design_result1 <- ANOVA_design(design = "2b",
9 | n = 100,
10 | mu = c(24,26.2),
11 | sd = 6.4,
12 | plot = FALSE)
13 |
14 |
15 | expect_error(plot_power_oneway_between(), "argument \"design_result\" is missing, with no default" )
16 | expect_error(plot_power_oneway_between(design_result1), "argument \"max_n\" is missing, with no default" )
17 |
18 | })
19 |
--------------------------------------------------------------------------------
/mess/old_functions/test-plot_power_oneway_within.R:
--------------------------------------------------------------------------------
1 | context("test-plot_power_oneway_within")
2 |
3 |
4 |
5 | # error messages
6 | test_that("error messages", {
7 |
8 | design_result1 <- ANOVA_design(design = "2w",
9 | n = 100,
10 | mu = c(24,26.2),
11 | sd = 6.4,
12 | r= 0.5,
13 | plot = FALSE)
14 |
15 | expect_error(plot_power_oneway_within(), "argument \"design_result\" is missing, with no default" )
16 | expect_error(plot_power_oneway_within(design_result1), "argument \"max_n\" is missing, with no default" )
17 |
18 | })
19 |
--------------------------------------------------------------------------------
/mess/old_functions/test-plot_power_twoway_between.R:
--------------------------------------------------------------------------------
1 | context("test-plot_power_twoway_between")
2 |
3 |
4 |
5 | # error messages
6 | test_that("error messages", {
7 |
8 | design_result1 <- ANOVA_design(design = "2b*2b",
9 | n = 100,
10 | mu = c(24,26,25,27),
11 | sd = 6.4,
12 | plot = FALSE)
13 |
14 |
15 | expect_error(plot_power_twoway_between(), "argument \"design_result\" is missing, with no default" )
16 | expect_error(plot_power_twoway_between(design_result1), "argument \"max_n\" is missing, with no default" )
17 |
18 | })
19 |
--------------------------------------------------------------------------------
/mess/old_functions/test-power_2x2_within.R:
--------------------------------------------------------------------------------
1 | context("test-power_2x2_within")
2 |
3 | # error messages
4 | test_that("error messages", {
5 |
6 |
7 | expect_error(power_2x2_within(), "argument \"design_result\" is missing, with no default" )
8 |
9 | design_result1 <- ANOVA_design(design = "2b*2b*2w",
10 | n = 100,
11 | mu = c(24, 26.2, 27, 28,
12 | 24, 26.2, 27, 28),
13 | sd = 6.4,
14 | plot = FALSE)
15 |
16 |
17 |
18 | expect_error(power_2x2_within(design_result1), "Only 2x2 within designs allowed for this function")
19 |
20 | design_result2 <- ANOVA_design(design = "2w*2b",
21 | n = 100,
22 | mu = c(24, 26.2, 27, 28),
23 | sd = 6.4,
24 | r=0.5,
25 | plot = FALSE)
26 |
27 | expect_error(power_2x2_within(design_result2), "Only 2x2 within designs allowed for this function")
28 |
29 | })
30 |
31 | #Function check ###function not working yet
32 | #test_that("2x2 design", {
33 |
34 | #From ANOVA_power_simulation validation file https://github.com/Lakens/ANOVA_power_simulation/blob/master/validation_files/3.2_validation_power_within_within_2x2_Amsel.md
35 |
36 | # design_result <- ANOVA_design(design = "2w*2w",
37 | # n = 25,
38 | # mu = c(700, 670, 670, 700),
39 | # sd = 150,
40 | # r = 0.75)
41 |
42 |
43 | # power <- power_2x2_within(design_result)
44 |
45 |
46 |
47 | # expect_equal(c(power$power_A,power$power_A,power$power_AB), c(.05,.05,.49), tolerance = .01)
48 | #
49 | #})
50 |
--------------------------------------------------------------------------------
/mess/try_out.R:
--------------------------------------------------------------------------------
1 | library(afex)
2 | library(MASS)
3 | library(ggplot2)
4 | library(reshape2)
5 | library(ANOVApower)
6 |
7 | devtools::install_github("Lakens/ANOVApower")
8 | devtools::build_vignettes()
9 | devtools::document()
10 | devtools::build_manual()
11 |
12 |
13 | design = "2b*2w"
14 | n = 40
15 | mu = c(1, 0, 1, 0)
16 | sd = 2
17 | r = 0.8
18 | labelnames = c("condition", "cheerful", "sad", "voice", "human", "robot")
19 |
20 | design_result <- ANOVA_design(design = "2w*2w", n = 40, mu = c(1, 0, 0, 0.5), sd = 2, r = 0.8, labelnames = c("condition", "cheerful", "sad", "voice", "human", "robot"))
21 | design_result$cor_mat
22 |
23 | power_result <- ANOVA_power(design_result, alpha_level = 0.05,
24 | p_adjust = "none", nsims = 1000)
25 |
26 | power_result$plot1
27 |
28 | design_result <- ANOVA_design(design = "2w*2b", n = 40, mu = c(0, 0, 0, 0), sd = 2, r = 0.8, labelnames = c("condition", "cheerful", "sad", "voice", "human", "robot"))
29 | design_result$cor_mat
30 |
31 | power_result <- ANOVA_power(design_result, alpha_level = 0.05,
32 | p_adjust = "none", nsims = 1000)
33 |
34 | power_result$plot1
35 |
36 | design_result_new$frml1 == design_result_old$frml1
37 | design_result_new$frml2 == design_result_old$frml2
38 |
39 | design_result_new$cor_mat == design_result_old$cor_mat
40 |
41 | xxx <- data.frame(matrix(NA, nrow = prod(factor_levels), ncol = 0))
42 | for(j in 1:factors){
43 | xxx <- cbind(xxx, as.factor(unlist(rep(as.list(paste(labelnameslist[[j]],
44 | sep="_")),
45 | each = prod(factor_levels)/prod(factor_levels[1:j]),
46 | times = prod(factor_levels)/prod(factor_levels[j:factors])
47 | ))))
48 | }
49 | xxx$cond <- as.character(interaction(xxx[, 1:factors], sep = "_")) #create a new condition variable combine 2 columns (interaction is a cool function!)
50 |
51 |
52 | xxx <- data.frame(matrix(NA, nrow = prod(factor_levels), ncol = 0))
53 | for(j in 1:factors){
54 | xxx <- cbind(xxx, labelnameslist[[j]])
55 | }
56 | xxx$cond <- as.character(interaction(xxx[, 1:factors], sep = "_")) #create a new condition variable combine 2 columns (interaction is a cool function!)
57 |
58 | library(ANOVApower)
59 | #test twoway within function
60 | design_result <- ANOVA_design(design = "2w*2w", n = 20, mu = c(1, 0, 0, 1), sd = 2, r = 0.0)
61 | power_res <- power_2x2_within_2(design_result)
62 | power_res <- power_2x2_within(design_result)
63 | power_res$mean_mat
64 | power_res$power_A
65 | power_res$power_B
66 | power_res$power_AB
67 | power_result <- ANOVA_power(design_result, alpha_level = 0.05,
68 | p_adjust = "none", nsims = 1000)
69 |
70 |
71 |
72 |
73 |
74 |
75 |
76 | mu = c(2,1,4,2)
77 | n <- 20
78 | sd <- 5
79 | r <- 0.8
80 |
81 | string = "2w*2w"
82 | labelnames = c("A", "a1", "a2", "B", "b1", "b2")
83 | design_result <- ANOVA_design(string = string,
84 | n = n,
85 | mu = mu,
86 | sd = sd,
87 | r = r,
88 | labelnames = labelnames)
89 | simulation_result <- ANOVA_power(design_result, nsims = 1000)
90 |
91 | power_res <- power_2x2_within(design_result = design_result)
92 | power_res$power_A
93 | power_res$power_B
94 | power_res$power_AB
95 |
96 | power_res <- power_twoway_within(design_result = design_result)
97 | power_res$power_A
98 | power_res$power_B
99 | power_res$power_AB
100 |
101 | design_result <- ANOVA_design(string = "2w*2w", n = 20, mu = c(1, 0, 0, 0), sd = 2, r = 0.6)
102 | power_res <- power_twoway_within_2(design_result)
103 | power_res$mean_mat
104 | power_res$power_A
105 | power_res$power_B
106 | power_res$power_AB
107 | power_result <- ANOVA_power(design_result, alpha_level = 0.05, p_adjust = "none", nsims = 1000)
108 |
109 | ANOVA_exact(design_result)
110 |
111 | library(ANOVApower)
112 | design_result <- ANOVA_design(design = "2w*2w", n = 20, mu = c(1, 0, 0, 0), sd = 2, r = 0.6)
113 | ANOVA_exact(design_result)
114 | power_res <- power_2x2_within(design_result)
115 | power_res$power_A
116 | power_res$power_B
117 | power_res$power_AB
118 | set.seed(2019)
119 | ANOVA_power(design_result, alpha_level = 0.05, p_adjust = "none", nsims = 100)
120 | ANOVA_power(design_result, alpha_level = 0.05, p_adjust = "none", nsims = 100, seed = 2019)
121 |
122 |
123 |
--------------------------------------------------------------------------------
/tests/testthat.R:
--------------------------------------------------------------------------------
1 | library(testthat)
2 | library(ANOVApower)
3 |
4 |
5 | test_check("ANOVApower")
6 |
--------------------------------------------------------------------------------
/tests/testthat/test-anova_design.R:
--------------------------------------------------------------------------------
1 | context("test-anova_design")
2 |
3 | # error messages ----
4 | test_that("errors", {
5 | # missing arguments
6 | expect_error(ANOVA_design(),
7 | "argument \"design\" is missing, with no default")
8 | expect_error(ANOVA_design("2w*2b", n =20, sd = 1),
9 | "argument \"mu\" is missing, with no default")
10 | expect_error(ANOVA_design("2w*2b", mu = c(0, 0, 0, 0)),
11 | "argument \"sd\" is missing, with no default")
12 | expect_error(ANOVA_design("2w*2b", mu = c(0, 0, 0, 0), sd = 1),
13 | "argument \"n\" is missing, with no default")
14 |
15 | # passing bad arguments: *SHOULD BE AN ERROR*
16 | expect_error(ANOVA_design("2w*2b", n = 100, mu = c(0, 0, 0, 0), sd = -1),
17 | "Standard deviation (sd) is less than or equal to zero; input a value greater than zero", fixed = TRUE)
18 | expect_error(ANOVA_design("2F", n = 10, mu = 1:2, sd = 1),
19 | "Problem in the design argument: must input number of levels as integer (2-999) and factor-type (between or within) as lower case b (between) or w (within)",
20 | fixed = TRUE)
21 | expect_error(ANOVA_design("1001b", n = 10, mu = 1:1001, sd = 1),
22 | "Problem in the design argument: must input number of levels as integer (2-999) and factor-type (between or within) as lower case b (between) or w (within)",
23 | fixed = TRUE)
24 |
25 | # bad arguments
26 | expect_error(ANOVA_design("wrong design"),
27 | "Problem in the design argument: must input number of levels as integer (2-999) and factor-type (between or within) as lower case b (between) or w (within)",
28 | fixed = TRUE)
29 | expect_error(ANOVA_design("2w*2b", n = "A", mu = 1:4, sd = 1),
30 | "non-numeric argument to binary operator")
31 | expect_error(ANOVA_design("2w*2b", n = 100, mu = c("A", "B", "C", "D"), sd = 1),
32 | "non-numeric argument to binary operator")
33 | expect_error(ANOVA_design("2w*2b", n = 100, mu = 1:4, sd = "A"),
34 | "requires numeric/complex matrix/vector arguments")
35 | expect_error(ANOVA_design("2w*2b", n = "A", mu = 1:4, sd = 1),
36 | "non-numeric argument to binary operator")
37 | expect_error(ANOVA_design("2w*2b", n = 100, mu = 1:4, sd = 1, r = "A"),
38 | "Correlation must be greater than -1 and less than 1",
39 | fixed = TRUE)
40 | expect_error(ANOVA_design("2w*2b", n = 100, mu = 1:4, sd = 1, labelnames = c("A", "B")),
41 | "Variable 'design' does not match the length of the labelnames")
42 | expect_error(ANOVA_design("2w*2b", n = 100, mu = 1:4, sd = 1,
43 | labelnames = c("W factor", "W 1", "W 2", "B factor", "B 1", "B 2")),
44 | "unexpected symbol")
45 |
46 |
47 | # inconsistent arguments
48 | expect_error(ANOVA_design("2w*2b", n = 100, mu = 0, sd = 1),
49 | "the length of the vector with means does not match the study design")
50 | #expect_error(ANOVA_design("2w*2b", n = 100, mu = 1:4, sd = c(1,2)),
51 | # "The SD must be a length of 1 or match the length of the study design")
52 | expect_error(ANOVA_design("2w*2b", n = 100, mu = 1:4, sd = c(1,1)),
53 | "the length of sd_for_sigma must be 1 or vars")
54 | expect_error(ANOVA_design("2w*2b", n = 100, mu = 1:4, sd = 1, r = c(.5, 0, .4)),
55 | "object 'cor_mat' not found")
56 | })
57 |
58 |
59 | # test_that("2W", {
60 | # # fix uppercase letters are design = 0
61 | # d <- ANOVA_design("2W", n = 100, mu = c(0,0), sd = 1)
62 | # expect_equal(d$design, 1)
63 | # })
64 | #
65 | # # test grep for strings ----
66 | # test_that("test grep for strings", {
67 | # pattern <- "^([2-9](w|b)\\*){0,2}[2-9](w|b)$"
68 | #
69 | # # should work
70 | # good_strings <- c("2w", "2b", "2W", "2B", "2w*2b", "3w*3b*2w")
71 | # for (x in good_strings) {
72 | # find <- grep(pattern, x, ignore.case = TRUE, perl = TRUE)
73 | # #expect_equal(find, 1)
74 | # if (length(find) == 0 || find != 1) { print(x) }
75 | # }
76 | #
77 | #
78 | # # should not work
79 | # bad_strings <- c("2a", "w2", "b2", "0w", "0b", "1w", "1b", "2b+2w", "2b*2b*2b*2b")
80 | # for (x in bad_strings) {
81 | # find <- grep(pattern, x, ignore.case = TRUE, perl = TRUE)
82 | # #expect_equal(find, integer(0))
83 | # if (length(find) != 0) { print(x) }
84 | # }
85 | # })
86 |
87 | # 2w defaults ----
88 | test_that("2w defaults", {
89 | d <- ANOVA_design("2w", n = 100, mu = c(0,0), sd = 1, plot = FALSE)
90 | expect_equal(d$design_factors, 1)
91 | expect_equal(d$design_list, c("a1", "a2"))
92 | expect_equal(d$factors, 1)
93 | expect_equal(d$frml1, y ~ a + Error(subject/a))
94 | expect_equal(d$frml2, ~a)
95 | expect_equal(d$mu, c(0, 0))
96 | expect_equal(d$sd, 1)
97 | expect_equal(d$r, 0)
98 | expect_equal(d$n, 100)
99 |
100 | mat <- data.frame(
101 | "a1" = c(1, 0),
102 | "a2" = c(0, 1),
103 | row.names = c("a1", "a2")
104 | )
105 | expect_true(dplyr::all_equal(d$cor_mat, mat))
106 | expect_true(dplyr::all_equal(d$sigmatrix, mat))
107 |
108 | expect_equal(d$design, "2w")
109 | expect_equal(d$labelnameslist, list(c("a1", "a2")))
110 | expect_equal(d$factornames, "a")
111 | })
112 |
113 | # 2b defaults----
114 | test_that("2b defaults", {
115 | d <- ANOVA_design("2b", n = 100, mu = c(0,0), sd = 1, plot = FALSE)
116 | expect_equal(d$design_factors, 0)
117 | expect_equal(d$design_list, c("a1", "a2"))
118 | expect_equal(d$factors, 1)
119 | expect_equal(d$frml1, y ~ a + Error(1 | subject))
120 | expect_equal(d$frml2, ~a)
121 | expect_equal(d$mu, c(0, 0))
122 | expect_equal(d$sd, 1)
123 | expect_equal(d$r, 0)
124 | expect_equal(d$n, 100)
125 |
126 | mat <- data.frame(
127 | "a1" = c(1, 0),
128 | "a2" = c(0, 1),
129 | row.names = c("a1", "a2")
130 | )
131 | expect_true(dplyr::all_equal(d$cor_mat, mat))
132 | expect_true(dplyr::all_equal(d$sigmatrix, mat))
133 |
134 | expect_equal(d$design, "2b")
135 | expect_equal(d$labelnameslist, list(c("a1", "a2")))
136 | expect_equal(d$factornames, "a")
137 | })
138 |
139 | # 2w set r & labels ----
140 | test_that("2w set r & labels", {
141 | d <- ANOVA_design("2w", n = 100, mu = c(0,0), sd = 1, r = 0.5, labelnames = c("W", "W1", "W2"), plot = FALSE)
142 | expect_equal(d$design_factors, 1)
143 | expect_equal(d$design_list, c("W1", "W2"))
144 | expect_equal(d$factors, 1)
145 | expect_equal(d$frml1, y ~ W + Error(subject/W))
146 | expect_equal(d$frml2, ~W)
147 | expect_equal(d$mu, c(0, 0))
148 | expect_equal(d$sd, 1)
149 | expect_equal(d$r, 0.5)
150 | expect_equal(d$n, 100)
151 |
152 | mat <- data.frame(
153 | "W1" = c(1, 0.5),
154 | "W2" = c(0.5, 1),
155 | row.names = c("W1", "W2")
156 | )
157 | expect_true(dplyr::all_equal(d$cor_mat, mat))
158 | expect_true(dplyr::all_equal(d$sigmatrix, mat))
159 |
160 | expect_equal(d$design, "2w")
161 | expect_equal(d$labelnameslist, list(c("W1", "W2")))
162 | expect_equal(d$factornames, "W")
163 | })
164 |
165 | # 2b set r & labels----
166 | test_that("2b set r & labels", {
167 | d <- ANOVA_design("2b", n = 100, mu = c(0,0), sd = 1, r = 0.5, labelnames = c("B", "B1", "B2"), plot = FALSE)
168 | expect_equal(d$design_factors, 0)
169 | expect_equal(d$design_list, c("B1", "B2"))
170 | expect_equal(d$factors, 1)
171 | expect_equal(d$frml1, y ~ B + Error(1 | subject))
172 | expect_equal(d$frml2, ~B)
173 | expect_equal(d$mu, c(0, 0))
174 | expect_equal(d$sd, 1)
175 | expect_equal(d$r, 0.5)
176 | expect_equal(d$n, 100)
177 |
178 | mat <- data.frame(
179 | "B1" = c(1, 0),
180 | "B2" = c(0, 1),
181 | row.names = c("B1", "B2")
182 | )
183 | expect_true(dplyr::all_equal(d$cor_mat, mat))
184 | expect_true(dplyr::all_equal(d$sigmatrix, mat))
185 |
186 | expect_equal(d$design, "2b")
187 | expect_equal(d$labelnameslist, list(c("B1", "B2")))
188 | expect_equal(d$factornames, "B")
189 | })
190 |
191 | # 4w
192 | test_that("4w", {
193 | d <- ANOVA_design("4w", n = 100, mu = 1:4, sd = 1:4, r = 0.5,
194 | labelnames = c("W", "W1", "W2", "W3", "W4"), plot = FALSE)
195 | expect_equal(d$design_factors, 1)
196 | expect_equal(d$design_list, c("W1", "W2", "W3", "W4"))
197 | expect_equal(d$factors, 1)
198 | expect_equal(d$frml1, y ~ W + Error(subject/W))
199 | expect_equal(d$frml2, ~W)
200 | expect_equal(d$mu, 1:4)
201 | expect_equal(d$sd, 1:4)
202 | expect_equal(d$r, 0.5)
203 | expect_equal(d$n, 100)
204 |
205 | mat <- data.frame(
206 | "W1" = c(1, 0.5, 0.5, 0.5),
207 | "W2" = c(0.5, 1, 0.5, 0.5),
208 | "W3" = c(0.5, 0.5, 1, 0.5),
209 | "W4" = c(0.5, 0.5, 0.5, 1),
210 | row.names = c("W1", "W2", "W3", "W4")
211 | )
212 | expect_true(dplyr::all_equal(d$cor_mat, mat))
213 | sigma <- as.matrix(mat) * (1:4 %*% t(1:4))
214 | expect_true(dplyr::all_equal(d$sigmatrix, as.data.frame(sigma)))
215 |
216 | expect_equal(d$design, "4w")
217 | expect_equal(d$labelnameslist, list(c("W1", "W2", "W3", "W4")))
218 | expect_equal(d$factornames, "W")
219 | })
220 |
221 | # 4b
222 | test_that("4b", {
223 | d <- ANOVA_design("4b", n = 100, mu = 1:4, sd = 1:4, r = 0.5,
224 | labelnames = c("B", "B1", "B2", "B3", "B4"), plot = FALSE)
225 | expect_equal(d$design_factors, 0)
226 | expect_equal(d$design_list, c("B1", "B2", "B3", "B4"))
227 | expect_equal(d$factors, 1)
228 | expect_equal(d$frml1, y ~ B + Error(1 | subject))
229 | expect_equal(d$frml2, ~B)
230 | expect_equal(d$mu, 1:4)
231 | expect_equal(d$sd, 1:4)
232 | expect_equal(d$r, 0.5)
233 | expect_equal(d$n, 100)
234 |
235 | mat <- data.frame(
236 | "B1" = c(1, 0, 0, 0),
237 | "B2" = c(0, 1, 0, 0),
238 | "B3" = c(0, 0, 1, 0),
239 | "B4" = c(0, 0, 0, 1),
240 | row.names = c("B1", "B2", "B3", "B4")
241 | )
242 | expect_true(dplyr::all_equal(d$cor_mat, mat))
243 | sigma <- as.matrix(mat) * (1:4 %*% t(1:4))
244 | expect_true(dplyr::all_equal(d$sigmatrix, as.data.frame(sigma)))
245 |
246 | expect_equal(d$design, "4b")
247 | expect_equal(d$labelnameslist, list(c("B1", "B2", "B3", "B4")))
248 | expect_equal(d$factornames, "B")
249 | })
250 |
251 |
252 | # 2w*2b----
253 | test_that("2w*2b", {
254 | d <- ANOVA_design("2w*2b", n = 100, mu = 1:4, sd = 1, r = 0.5,
255 | labelnames = c("W", "W1", "W2", "B", "B1", "B2"), plot = FALSE)
256 | expect_equal(d$design_factors, c(1, 0))
257 | expect_equal(d$design_list, c("W1_B1", "W1_B2", "W2_B1", "W2_B2"))
258 | expect_equal(d$factors, 2)
259 | expect_equal(d$frml1, y ~ W * B + Error(subject/W))
260 | expect_equal(d$frml2, ~W + B)
261 | expect_equal(d$mu, 1:4)
262 | expect_equal(d$sd, 1)
263 | expect_equal(d$r, 0.5)
264 | expect_equal(d$n, 100)
265 |
266 | mat <- data.frame(
267 | "W1_B1" = c(1, .5, 0, 0),
268 | "W2_B1" = c(.5, 1, 0, 0),
269 | "W1_B2" = c(0, 0, 1, .5),
270 | "W2_B2" = c(0, 0, .5, 1),
271 | row.names = c("W1_B1", "W2_B1", "W1_B2", "W2_B2")
272 | )
273 | expect_true(dplyr::all_equal(d$cor_mat, mat))
274 | expect_true(dplyr::all_equal(d$sigmatrix, mat))
275 |
276 | expect_equal(d$design, "2w*2b")
277 | expect_equal(d$labelnameslist, list(c("W1", "W2"), c("B1", "B2")))
278 | expect_equal(d$factornames, c("W", "B"))
279 | })
280 |
281 | # 2b*2w----
282 | test_that("2b*2w", {
283 | d <- ANOVA_design("2b*2w", n = 100, mu = 1:4, sd = 1, r = 0.5,
284 | labelnames = c("B", "B1", "B2", "W", "W1", "W2"), plot = FALSE)
285 | expect_equal(d$design_factors, c(0, 1))
286 | expect_equal(d$design_list, c("B1_W1", "B1_W2", "B2_W1", "B2_W2"))
287 | expect_equal(d$factors, 2)
288 | expect_equal(d$frml1, y ~ B * W + Error(subject/W))
289 | expect_equal(d$frml2, ~B + W)
290 | expect_equal(d$mu, 1:4)
291 | expect_equal(d$sd, 1)
292 | expect_equal(d$r, 0.5)
293 | expect_equal(d$n, 100)
294 |
295 | mat <- data.frame(
296 | "B1_W1" = c(1, 0, .5, 0),
297 | "B2_W1" = c(0, 1, 0, .5),
298 | "B1_W2" = c(.5, 0, 1, 0),
299 | "B2_W2" = c(0, .5, 0, 1),
300 | row.names = c("B1_W1", "B2_W1", "B1_W2", "B2_W2")
301 | )
302 | expect_true(dplyr::all_equal(d$cor_mat, mat))
303 | expect_true(dplyr::all_equal(d$sigmatrix, mat))
304 |
305 | expect_equal(d$design, "2b*2w")
306 | expect_equal(d$labelnameslist, list(c("B1", "B2"), c("W1", "W2")))
307 | expect_equal(d$factornames, c("B", "W"))
308 | })
309 |
310 | #Add three way designs
311 | test_that("2b*2b*2b", {
312 | d <- ANOVA_design("2b*2b*2b", n = 100, mu = 1:8, sd = 1, plot = FALSE)
313 | expect_equal(d$design_factors, c(0, 0, 0))
314 | expect_equal(d$design_list, c("a1_b1_c1", "a1_b1_c2", "a1_b2_c1", "a1_b2_c2", "a2_b1_c1", "a2_b1_c2", "a2_b2_c1", "a2_b2_c2"))
315 | expect_equal(d$factors, 3)
316 | expect_equal(d$frml1, y ~ a * b * c + Error(1 | subject))
317 | expect_equal(d$frml2, ~ a + b + c)
318 | expect_equal(d$mu, 1:8)
319 | expect_equal(d$sd, 1)
320 | expect_equal(d$n, 100)
321 |
322 |
323 | mat <- data.frame(
324 | "a1_b1_c1" = c(1, 0, 0, 0, 0, 0, 0, 0),
325 | "a1_b1_c2" = c(0, 1, 0, 0, 0, 0, 0, 0),
326 | "a1_b2_c1" = c(0, 0, 1, 0, 0, 0, 0, 0),
327 | "a1_b2_c2" = c(0, 0, 0, 1, 0, 0, 0, 0),
328 | "a2_b1_c1" = c(0, 0, 0, 0, 1, 0, 0, 0),
329 | "a2_b1_c2" = c(0, 0, 0, 0, 0, 1, 0, 0),
330 | "a2_b2_c1" = c(0, 0, 0, 0, 0, 0, 1, 0),
331 | "a2_b2_c2" = c(0, 0, 0, 0, 0, 0, 0, 1),
332 | row.names = c("a1_b1_c1", "a1_b1_c2", "a1_b2_c1", "a1_b2_c2", "a2_b1_c1", "a2_b1_c2", "a2_b2_c1", "a2_b2_c2"))
333 |
334 | expect_true(dplyr::all_equal(d$cor_mat, mat))
335 | expect_true(dplyr::all_equal(d$sigmatrix, mat))
336 |
337 | expect_equal(d$design, "2b*2b*2b")
338 |
339 | })
340 |
341 | test_that("2w*2w*2w", {
342 | d <- ANOVA_design("2w*2w*2w", n = 100, mu = 1:8, sd = 2, r = .65, plot = FALSE)
343 | expect_equal(d$design_factors, c(1, 1, 1))
344 | expect_equal(d$design_list, c("a1_b1_c1", "a1_b1_c2", "a1_b2_c1", "a1_b2_c2", "a2_b1_c1", "a2_b1_c2", "a2_b2_c1", "a2_b2_c2"))
345 | expect_equal(d$factors, 3)
346 | expect_equal(d$frml1, y ~ a * b * c + Error(subject/a * b * c))
347 | expect_equal(d$frml2, ~ a + b + c)
348 | expect_equal(d$mu, 1:8)
349 | expect_equal(d$sd, 2)
350 | expect_equal(d$n, 100)
351 | expect_equal(d$r, 0.65)
352 |
353 | mat <- data.frame(
354 | "a1_b1_c1" = c(1.00, 0.65, 0.65, 0.65, 0.65, 0.65, 0.65, 0.65),
355 | "a1_b1_c2" = c(0.65, 1.00, 0.65, 0.65, 0.65, 0.65, 0.65, 0.65),
356 | "a1_b2_c1" = c(0.65, 0.65, 1.00, 0.65, 0.65, 0.65, 0.65, 0.65),
357 | "a1_b2_c2" = c(0.65, 0.65, 0.65, 1.00, 0.65, 0.65, 0.65, 0.65),
358 | "a2_b1_c1" = c(0.65, 0.65, 0.65, 0.65, 1.00, 0.65, 0.65, 0.65),
359 | "a2_b1_c2" = c(0.65, 0.65, 0.65, 0.65, 0.65, 1.00, 0.65, 0.65),
360 | "a2_b2_c1" = c(0.65, 0.65, 0.65, 0.65, 0.65, 0.65, 1.00, 0.65),
361 | "a2_b2_c2" = c(0.65, 0.65, 0.65, 0.65, 0.65, 0.65, 0.65, 1.00),
362 | row.names = c("a1_b1_c1", "a1_b1_c2", "a1_b2_c1", "a1_b2_c2", "a2_b1_c1", "a2_b1_c2", "a2_b2_c1", "a2_b2_c2"))
363 |
364 | expect_true(dplyr::all_equal(d$cor_mat, mat))
365 |
366 | var <- (2)^2
367 | covar <- var*as.numeric(0.65)
368 |
369 | sig_mat <- data.frame(
370 | "a1_b1_c1" = c(var, covar, covar, covar, covar, covar, covar, covar),
371 | "a1_b1_c2" = c(covar, var, covar, covar, covar, covar, covar, covar),
372 | "a1_b2_c1" = c(covar, covar, var, covar, covar, covar, covar, covar),
373 | "a1_b2_c2" = c(covar, covar, covar, var, covar, covar, covar, covar),
374 | "a2_b1_c1" = c(covar, covar, covar, covar, var, covar, covar, covar),
375 | "a2_b1_c2" = c(covar, covar, covar, covar, covar, var, covar, covar),
376 | "a2_b2_c1" = c(covar, covar, covar, covar, covar, covar, var, covar),
377 | "a2_b2_c2" = c(covar, covar, covar, covar, covar, covar, covar, var),
378 | row.names = c("a1_b1_c1", "a1_b1_c2", "a1_b2_c1", "a1_b2_c2", "a2_b1_c1", "a2_b1_c2", "a2_b2_c1", "a2_b2_c2"))
379 | expect_true(dplyr::all_equal(d$sigmatrix, sig_mat))
380 |
381 | expect_equal(d$design, "2w*2w*2w")
382 |
383 | })
384 |
385 | test_that("2b*2b*2w", {
386 | d <- ANOVA_design("2b*2b*2w", n = 100, mu = 1:8, sd = 1.5, r = .68, plot = FALSE)
387 | expect_equal(d$design_factors, c(0, 0, 1))
388 | expect_equal(d$design_list, c("a1_b1_c1", "a1_b1_c2", "a1_b2_c1", "a1_b2_c2", "a2_b1_c1", "a2_b1_c2", "a2_b2_c1", "a2_b2_c2"))
389 | expect_equal(d$factors, 3)
390 | expect_equal(d$frml1, y ~ a * b * c + Error(subject/c))
391 | expect_equal(d$frml2, ~ a + b + c)
392 | expect_equal(d$mu, 1:8)
393 | expect_equal(d$sd, 1.5)
394 | expect_equal(d$n, 100)
395 | expect_equal(d$r, 0.68)
396 |
397 | mat <- data.frame(
398 | "a1_b1_c1" = c(1.00, 0.68, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00),
399 | "a1_b1_c2" = c(0.68, 1.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00),
400 | "a1_b2_c1" = c(0.00, 0.00, 1.00, 0.68, 0.00, 0.00, 0.00, 0.00),
401 | "a1_b2_c2" = c(0.00, 0.00, 0.68, 1.00, 0.00, 0.00, 0.00, 0.00),
402 | "a2_b1_c1" = c(0.00, 0.00, 0.00, 0.00, 1.00, 0.68, 0.00, 0.00),
403 | "a2_b1_c2" = c(0.00, 0.00, 0.00, 0.00, 0.68, 1.00, 0.00, 0.00),
404 | "a2_b2_c1" = c(0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 1.00, 0.68),
405 | "a2_b2_c2" = c(0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.68, 1.00),
406 | row.names = c("a1_b1_c1", "a1_b1_c2", "a1_b2_c1", "a1_b2_c2", "a2_b1_c1", "a2_b1_c2", "a2_b2_c1", "a2_b2_c2"))
407 |
408 | expect_true(dplyr::all_equal(d$cor_mat, mat))
409 |
410 | sig_mat <- mat
411 | sig_mat[mat == 0.68] <- (1.5^2)*0.68
412 | sig_mat[mat == 1.00] <- (1.5^2)
413 |
414 | expect_true(dplyr::all_equal(d$sigmatrix, sig_mat))
415 |
416 | expect_equal(d$design, "2b*2b*2w")
417 |
418 | })
419 |
--------------------------------------------------------------------------------
/tests/testthat/test-anova_exact.R:
--------------------------------------------------------------------------------
1 | context("test-anova_exact")
2 |
3 |
4 |
5 | # error messages
6 | test_that("error messages", {
7 | design <- ANOVA_design(design = "2b*4w",
8 | n = 7,
9 | mu = c(0,0,0,0,0.5,0.5,0.5,0.5),
10 | sd = 1,
11 | plot = FALSE)
12 |
13 | expect_error(ANOVA_exact(), "argument \"design_result\" is missing, with no default")
14 | expect_error(ANOVA_exact(design, verbose = FALSE),
15 | "ANOVA_exact cannot handle small sample sizes (n < the product of the factors) at this time; please pass this design_result to the ANOVA_power function to simulate power",
16 | fixed = TRUE)
17 |
18 | })
19 |
20 |
21 | #2w null
22 | test_that("2w null", {
23 | design <- ANOVA_design(design = "2w", n = 100, mu = c(0, 0), sd = 1, r = 0.5, plot = FALSE)
24 | p <- ANOVA_exact(design, verbose = FALSE)
25 |
26 | expect_equal(p$main_results$power, 5)
27 | expect_equal(p$pc_results$power, 5)
28 |
29 | })
30 |
31 | #2b null
32 | test_that("2b null", {
33 | design <- ANOVA_design(design = "2b", n = 100, mu = c(0, 0), sd = 1, plot = FALSE)
34 | p <- ANOVA_exact(design, verbose = FALSE)
35 |
36 | expect_equal(p$main_results$power, 5)
37 | expect_equal(p$pc_results$power, 5)
38 |
39 | })
40 |
41 |
42 | #2w moderate effect
43 | test_that("2w", {
44 | design <- ANOVA_design(design = "2w", n = 21, mu = c(0, 0.65), sd = 1, r = 0.55, plot = FALSE)
45 | p <- ANOVA_exact(design, verbose = FALSE)
46 |
47 | expect_equal(p$main_results$power, 84.7, tolerance = 0.1)
48 | expect_equal(p$pc_results$power, 84.7, tolerance = 0.1)
49 |
50 | })
51 |
52 | #2b moderate effect
53 | test_that("2b", {
54 | design <- ANOVA_design(design = "2b", n = 22, mu = c(0, 0.65), sd = 1, plot = FALSE)
55 | p <- ANOVA_exact(design, verbose = FALSE)
56 |
57 | expect_equal(p$main_results$power, 55.8, tolerance = 0.1)
58 | expect_equal(p$pc_results$power, 55.8, tolerance = 0.1)
59 |
60 | })
61 |
62 | #3w null
63 | test_that("3w null", {
64 | design <- ANOVA_design(design = "3w", n = 100,
65 | mu = c(0, 0, 0), sd = 1, r = 0.5, plot = FALSE)
66 | p <- ANOVA_exact(design, verbose = FALSE)
67 |
68 | expect_equal(p$main_results$power, 5)
69 | expect_equal(p$pc_results$power, c(5,5,5))
70 |
71 | })
72 |
73 | #4b low power
74 | test_that("4b", {
75 | design <- ANOVA_design(design = "4b", n = 15,
76 | mu = c(0, 0.25, 0.33, 0.44),
77 | sd = 1, plot = FALSE)
78 |
79 | p <- ANOVA_exact(design, verbose = FALSE)
80 |
81 | expect_equal(p$main_results$power, 15, tolerance = 0.1)
82 | expect_equal(p$pc_results$power,
83 | c(10.14,14.08,21.39,5.51,7.94,5.98),
84 | tolerance = 0.1)
85 |
86 | })
87 |
88 | #2x4 repeated measures
89 | test_that("2b*4w", {
90 | design <- ANOVA_design(design = "2b*4w", n = 8,
91 | mu = c(0.0, 0.0, 0.0, 0.0,
92 | 0.5, 0.5, 0.5, 0.5),
93 | r = 0.71,
94 | sd = 2, plot = FALSE)
95 |
96 |
97 | set.seed(7224)
98 | p <- ANOVA_exact(design, verbose = FALSE)
99 |
100 | expect_equal(p$main_results$power, c(8.24, 5, 5), tolerance = 0.1)
101 |
102 |
103 | })
104 |
105 | #3w
106 | test_that("3w", {
107 |
108 | design <- ANOVA_design(design = "3w", n = 20,
109 | mu = c(-0.3061862, 0.0000000, 0.3061862),
110 | r = 0.8,
111 | sd = 1, plot = FALSE)
112 |
113 |
114 | p <- ANOVA_exact(design, verbose = FALSE)
115 |
116 | expect_equal(p$main_results$power, 96.9, tolerance = 0.1)
117 |
118 |
119 | })
120 |
--------------------------------------------------------------------------------
/tests/testthat/test-anova_power.R:
--------------------------------------------------------------------------------
1 | context("test-anova_power")
2 |
3 |
4 |
5 | # error messages
6 | test_that("error messages", {
7 | design <- ANOVA_design(design = "2w", n = 10, mu = c(0, 0), sd = 1, plot = FALSE)
8 |
9 | expect_error(ANOVA_power(), "argument \"design_result\" is missing, with no default")
10 | expect_error(ANOVA_power(design, nsims = -1), "The number of repetitions in simulation must be at least 10; suggested at least 1000 for accurate results")
11 | expect_error(ANOVA_power(design, nsims = 10, p_adjust = "BEEFERONNI"), "p_adjust must be of an acceptable adjustment method: see ?p.adjust",
12 | fixed = TRUE)
13 | })
14 |
15 | #2w
16 | test_that("2w", {
17 | design <- ANOVA_design(design = "2w", n = 100, mu = c(0, 0.25), sd = 1, r = 0.5, plot = FALSE)
18 | set.seed(86753)
19 | p <- ANOVA_power(design, nsims = 50, verbose = FALSE)
20 |
21 | comp <- list()
22 | comp$main_results <- data.frame(
23 | power = c(70),
24 | effect_size = c(0.06913817),
25 | row.names = c("anova_a")
26 | )
27 |
28 | comp$pc_results <- data.frame(
29 | power = c(70),
30 | effect_size = c(0.2545745),
31 | row.names = c("p_a_a1_a_a2")
32 | )
33 |
34 | expect_equal(p$main_results, comp$main_results, tolerance = .02)
35 | expect_equal(p$pc_results, comp$pc_results, tolerance = .02)
36 | expect_equal(p$p_adjust, "none")
37 | expect_equal(p$nsims, 50)
38 | })
39 |
40 | # 2w*2w
41 | test_that("2w*2w", {
42 | design <- ANOVA_design(design = "2w*2w", n = 40, mu = c(1, 0, 1, 0), sd = 2, r = 0.8,
43 | labelnames = c("condition", "cheerful", "sad", "voice", "human", "robot"),
44 | plot = FALSE)
45 |
46 | set.seed(8675309)
47 |
48 | p <- ANOVA_power(design, nsims = 50, verbose = FALSE)
49 |
50 | comp <- list()
51 | comp$main_results <- data.frame(
52 | power = c(6, 100, 6),
53 | effect_size = c(0.02226488, 0.54204284, 0.02393630),
54 | row.names = c("anova_condition", "anova_voice", "anova_condition:voice")
55 | )
56 |
57 | comp$pc_results <- data.frame(
58 | power = c(100, 4, 100, 100, 4, 100),
59 | effect_size = c(-0.766498975, 0.009485897, -0.750025879, 0.803554808, 0.024381057, -0.794985335),
60 | row.names = c(
61 | "p_condition_cheerful_voice_human_condition_cheerful_voice_robot",
62 | "p_condition_cheerful_voice_human_condition_sad_voice_human",
63 | "p_condition_cheerful_voice_human_condition_sad_voice_robot",
64 | "p_condition_cheerful_voice_robot_condition_sad_voice_human",
65 | "p_condition_cheerful_voice_robot_condition_sad_voice_robot",
66 | "p_condition_sad_voice_human_condition_sad_voice_robot"
67 | )
68 | )
69 |
70 | expect_equal(p$main_results, comp$main_results)
71 | expect_equal(p$pc_results, comp$pc_results)
72 | expect_equal(p$p_adjust, "none")
73 | expect_equal(p$nsims, 50)
74 | })
75 |
76 | #2w long
77 | test_that("2w long", {
78 | skip_on_cran()
79 |
80 | design <- ANOVA_design(design = "2w", n = 100, mu = c(0, 0.25), sd = 1, r = 0.5, plot = FALSE)
81 |
82 | set.seed(86753)
83 |
84 | system.time(
85 | p <- ANOVA_power(design, nsims = 1000, verbose = FALSE)
86 | )
87 | pe <- ANOVA_exact(design, verbose = FALSE)
88 |
89 | p2 <- pwr::pwr.t.test(n = 100, d = 0.25, type = "paired")
90 |
91 | expect_equal(p$main_results$power/100, p2$power, tolerance = .02)
92 | expect_equal(pe$main_results$power/100, p2$power, tolerance = .02)
93 | expect_equal(p$pc_results$power/100, p2$power, tolerance = .02)
94 | expect_equal(pe$pc_results$power/100, p2$power, tolerance = .02)
95 | expect_equal(p$pc_results$effect_size, p2$d, tolerance = .02)
96 | expect_equal(pe$pc_results$effect_size, p2$d, tolerance = .02)
97 | })
98 |
99 | #2b long simulation
100 | test_that("2b long", {
101 | skip_on_cran()
102 | design <- ANOVA_design(design = "2b",
103 | n = 100,
104 | mu = c(24, 26.2),
105 | sd = 6.4,
106 | labelnames = c("condition", "control", "pet"),
107 | plot = FALSE)
108 | set.seed(644)
109 | system.time(
110 | p <- ANOVA_power(design, alpha_level = 0.05, nsims = 1000, verbose = FALSE)
111 | )
112 |
113 | pe <- ANOVA_exact(design, verbose = FALSE)
114 |
115 | p2 <- pwr::pwr.t.test(d = 2.2/6.4,
116 | n = 100,
117 | sig.level = 0.05,
118 | type = "two.sample",
119 | alternative = "two.sided")$power
120 |
121 | expect_equal(p$main_results$power/100, p2, tolerance = .02)
122 |
123 | expect_equal(pe$main_results$power/100, p2, tolerance = .02)
124 | })
125 |
126 | #3b long simulation
127 | test_that("3b long", {
128 | skip_on_cran()
129 | design <- ANOVA_design(design = "3b",
130 | n = 50,
131 | mu = c(24, 26.2, 26.6),
132 | sd = 6.4,
133 | labelnames = c("condition", "control", "cat", "dog"),
134 | plot = FALSE)
135 | set.seed(123)
136 | system.time(
137 | p <- ANOVA_power(design, alpha_level = 0.05, nsims = 5000, verbose = FALSE)
138 | )
139 | pe <- ANOVA_exact(design, alpha_level = 0.05, verbose = FALSE)
140 | pc_1 <- pwr::pwr.t.test(d = 2.2/6.4,
141 | n = 50,
142 | sig.level = 0.05,
143 | type = "two.sample",
144 | alternative = "two.sided")$power
145 |
146 | pc_2 <- pwr::pwr.t.test(d = 2.6 / 6.4,
147 | n = 50,
148 | sig.level = 0.05,
149 | type = "two.sample",
150 | alternative = "two.sided")$power
151 |
152 | pc_3 <- pwr::pwr.t.test(d = 0.4/6.4,
153 | n = 50,
154 | sig.level = 0.05,
155 | type = "two.sample",
156 | alternative = "two.sided")$power
157 |
158 | pmain <- pwr::pwr.anova.test(k = 3, n = 50, f = 0.1786086, sig.level = 0.05)$power #f obtained from GPower
159 |
160 | expect_equal(p$main_results$power/100, pmain, tolerance = .02)
161 | expect_equal(p$main_results$power/100, pe$main_results$power/100, tolerance = .02)
162 | expect_equal(p$pc_results$power/100, pe$pc_results$power/100, tolerance = .02)
163 | expect_equal(p$pc_results$power/100, c(pc_1, pc_2, pc_3), tolerance = .02)
164 | })
165 |
166 | #3 way between
167 | test_that("3 way between long", {
168 | skip_on_cran()
169 |
170 | labelnames <- c("Size", "b", "s", "Color", "g", "r",
171 | "Load", "pres", "abs") #
172 |
173 | design <- ANOVA_design(design = "2b*2b*2b",
174 | n = 80,
175 | mu = c(2, 2, 6, 1, 6, 6, 1, 8),
176 | sd = 10,
177 | labelnames = labelnames,
178 | plot = FALSE)
179 |
180 | set.seed(8224)
181 |
182 | p <- ANOVA_power(design, alpha_level = 0.05, nsims = 4000, verbose = FALSE)
183 |
184 | pe <- ANOVA_exact(design, alpha_level = 0.05, verbose = FALSE)
185 |
186 | power_analytic <- power_threeway_between(design)
187 |
188 |
189 |
190 | expect_equal(p$main_results$power/100,
191 | c(power_analytic$power_A, power_analytic$power_B, power_analytic$power_C,
192 | power_analytic$power_AB, power_analytic$power_AC, power_analytic$power_BC,
193 | power_analytic$power_ABC), tolerance = .02)
194 | expect_equal(p$main_results$power/100,
195 | pe$main_results$power/100, tolerance = .02)
196 | expect_equal(p$pc_results$power/100,
197 | pe$pc_results$power/100, tolerance = .02)
198 |
199 | expect_equal(rownames(p$pc_results),
200 | rownames(pe$pc_results))
201 | })
202 |
203 |
204 | #2 way mixed
205 | test_that("2x2 mixed long", {
206 | skip_on_cran()
207 |
208 | mu <- c(-0.25, 0.25, 0.25, -0.25)
209 | n <- 23
210 | sd <- 1
211 | r <- 0.5
212 | design = "2w*2b"
213 | alpha_level <- 0.05
214 | p_adjust = "none"
215 | labelnames = c("age", "old", "young", "color", "blue", "red")
216 | design <- ANOVA_design(design = design,
217 | n = n,
218 | mu = mu,
219 | sd = sd,
220 | r = r,
221 | labelnames = labelnames,
222 | plot = FALSE)
223 |
224 | set.seed(435)
225 |
226 | p <- ANOVA_power(design, alpha_level = 0.05, nsims = 1000, verbose = FALSE)
227 | pe <- ANOVA_exact(design, verbose = FALSE)
228 |
229 |
230 |
231 | p_inter <- 0.9124984 #power obtained from GPower https://github.com/Lakens/ANOVA_power_simulation/blob/master/validation_files/3.1_validation_power_between_within_2x2.md
232 |
233 | expect_equal(p$main_results$power[3]/100, p_inter, tolerance = .02)
234 | expect_equal(pe$main_results$power[3]/100, p$main_results$power[3]/100, tolerance = .02)
235 | })
236 |
--------------------------------------------------------------------------------
/tests/testthat/test-mu_from_ES.R:
--------------------------------------------------------------------------------
1 | context("test-mu_from_ES")
2 |
3 | # error messages
4 | test_that("error messages", {
5 |
6 | expect_error(mu_from_ES(K=11, ES=0.05), "Number of levels (k) must be 2, 3, or 4", fixed = TRUE)
7 | expect_error(mu_from_ES(K=3, ES=5), "the ES (partial eta squared) must be less than 1 and greater than zero", fixed = TRUE)
8 | expect_error(mu_from_ES(), "argument \"ES\" is missing, with no default" )
9 |
10 | })
11 |
12 | #Function check
13 | test_that("2b and 3b", {
14 | expect_equal(mu_from_ES(K=2, ES=0.0503911)*6.4, c(-1.474295, 1.474295), tolerance = .001) #example from validation files
15 | expect_equal(mu_from_ES(K=3, ES=0.07928127)*6.4, c(-2.300104, 0.00000, 2.300104), tolerance = .001) #example from validation files
16 | })
17 |
--------------------------------------------------------------------------------
/tests/testthat/test-plot_power.R:
--------------------------------------------------------------------------------
1 | context("test-plot_power")
2 |
3 |
4 |
5 | # error messages
6 | test_that("error messages", {
7 | design <- ANOVA_design(design = "2b*4w",
8 | n = 7,
9 | mu = c(0,0,0,0,0.5,0.5,0.5,0.5),
10 | sd = 1,
11 | plot = FALSE)
12 |
13 | expect_error(plot_power(), "argument \"design_result\" is missing, with no default")
14 | expect_error(plot_power(design),
15 | "plot_power cannot handle small sample sizes (n < the product of the factors) at this time; please increase the in ANOVA_design function.",
16 | fixed = TRUE)
17 |
18 | })
19 |
--------------------------------------------------------------------------------
/tests/testthat/test-power_oneway_between.R:
--------------------------------------------------------------------------------
1 | context("test-power_oneway_between")
2 |
3 | # error messages
4 | test_that("error messages", {
5 |
6 | design <- "2b"
7 | n <- 100
8 | mu <- c(24, 26.2)
9 | sd <- 6.4
10 |
11 | design_result <- ANOVA_design(design = design,
12 | n = n,
13 | mu = mu,
14 | sd = sd,
15 | plot = FALSE)
16 |
17 |
18 | expect_error(power_oneway_between(), "argument \"design_result\" is missing, with no default" )
19 |
20 | })
21 |
22 | #Function check
23 | test_that("2b and 3b", {
24 |
25 | design_result1 <- ANOVA_design(design = "2b",
26 | n = 100,
27 | mu = c(24,26.2),
28 | sd = 6.4, plot = FALSE)
29 |
30 | expect_equal(power_oneway_between(design_result1, alpha_level = 0.05)$power,
31 | pwr::pwr.t.test(d = 2.2/6.4,
32 | n = 100,
33 | sig.level = 0.05,
34 | type="two.sample",
35 | alternative="two.sided")$power,
36 | tolerance = .001) #example from validation files
37 |
38 | design_result2 <- ANOVA_design(design = "3b",
39 | n = 50,
40 | mu = c(24, 26.2, 26.6),
41 | sd = 6.4, plot = FALSE)
42 | expect_equal(power_oneway_between(design_result2, alpha_level = 0.05)$power,
43 | pwr::pwr.anova.test(n = 50,
44 | k = 3,
45 | f = 0.1786086, #From Gpower
46 | sig.level = .05)$power,
47 | tolerance = .001) #example from validation files
48 | })
49 |
--------------------------------------------------------------------------------
/tests/testthat/test-power_oneway_within.R:
--------------------------------------------------------------------------------
1 | context("test-power_oneway_within")
2 |
3 | # error messages
4 | test_that("error messages", {
5 |
6 |
7 | expect_error(power_oneway_within(), "argument \"design_result\" is missing, with no default" )
8 |
9 | })
10 |
11 | #Function check
12 | test_that("2w and 3w", {
13 | K <- 2
14 | n <- 34
15 | sd <- 1
16 | r <- 0.5
17 | alpha = 0.05
18 | f <- 0.25
19 | f2 <- f^2
20 | ES <- f2/(f2+1)
21 |
22 | mu <- mu_from_ES(K = K, ES = ES)
23 |
24 | design = paste(K,"w",sep="")
25 |
26 | design_result1 <- ANOVA_design(design = design,
27 | n = n,
28 | mu = mu,
29 | sd = sd,
30 | r = r, plot = FALSE)
31 |
32 | expect_equal(power_oneway_within(design_result1, alpha_level = 0.05)$power,
33 | pwr::pwr.t.test(d = 0.5,
34 | n = 34,
35 | sig.level = 0.05,
36 | type = "paired",
37 | alternative = "two.sided")$power,
38 | tolerance = .001) #example from validation files
39 |
40 | K <- 3
41 | n <- 20
42 | sd <- 1
43 | r <- 0.8
44 | f <- 0.25
45 | f2 <- f^2
46 | ES <- f2/(f2+1)
47 | mu <- mu_from_ES(K = K, ES = ES)
48 | design = paste(K,"w",sep = "")
49 |
50 | design_result2 <- ANOVA_design(design = design,
51 | n = n,
52 | mu = mu,
53 | sd = sd,
54 | r = r, plot = FALSE)
55 |
56 | #Formula used by G*Power for within design
57 | f <- 0.25 #Cohen's f
58 | k <- 1 #number of groups
59 | m <- 3 #number of measures
60 | n <- 20 #total sample size
61 | e <- 1 #non-spericity correction
62 | r <- 0.8 #correlation between dependent variables
63 | alpha <- 0.05 #alpha level
64 |
65 | df1 <- (m - 1) * e #calculate degrees of freedom 1
66 | df2 <- (n - k) * (m - 1) * e #calculate degrees of freedom 2
67 |
68 | lambda <- (n * m * f^2) / (1 - r) # lambda for within ANOVA
69 |
70 | F_critical <- qf(alpha, # critical F-vaue
71 | df1,
72 | df2,
73 | lower.tail = FALSE)
74 |
75 | pow <- pf(qf(alpha, #power
76 | df1,
77 | df2,
78 | lower.tail = FALSE),
79 | df1,
80 | df2,
81 | lambda,
82 | lower.tail = FALSE)
83 |
84 |
85 | expect_equal(power_oneway_within(design_result2, alpha_level = 0.05)$power,
86 | pow,
87 | tolerance = .01) #example from validation files
88 | })
89 |
--------------------------------------------------------------------------------
/tests/testthat/test-power_threeway_between.R:
--------------------------------------------------------------------------------
1 | context("test-power_threeway_between")
2 |
3 | # error messages
4 | test_that("error messages", {
5 |
6 |
7 | expect_error(power_twoway_between(), "argument \"design_result\" is missing, with no default" )
8 |
9 | design_result1 <- ANOVA_design(design = "2b*2b*2w",
10 | n = 100,
11 | mu = c(24, 26.2, 27, 28,
12 | 24, 26.2, 27, 28),
13 | sd = 6.4,
14 | plot = FALSE)
15 |
16 |
17 |
18 | expect_error(power_threeway_between(design_result1), "Only three-way between designs allowed for this function")
19 |
20 | design_result2 <- ANOVA_design(design = "2b*2b",
21 | n = 100,
22 | mu = c(24, 26.2, 27, 28),
23 | sd = 6.4,
24 | plot = FALSE)
25 |
26 | expect_error(power_threeway_between(design_result2), "Only three-way between designs allowed for this function")
27 |
28 | })
29 |
30 | #Function check
31 | test_that("3-way design", {
32 |
33 | #From ANOVA_power_simulation validation file https://github.com/Lakens/ANOVA_power_simulation/blob/master/validation_files/4.6_threeway_interactions.md
34 |
35 | design_result <- ANOVA_design(design = "2b*2b*2b",
36 | n = 50,
37 | mu = c(2, 2, 6, 1, 6, 6, 1, 8),
38 | sd = 10,
39 | plot = FALSE)
40 |
41 |
42 | power <- power_threeway_between(design_result)
43 |
44 | expect_equal(power$power_ABC, .849, tolerance = .01)
45 |
46 | })
47 |
--------------------------------------------------------------------------------
/tests/testthat/test-power_twoway_between.R:
--------------------------------------------------------------------------------
1 | context("test-power_twoway_between")
2 |
3 | # error messages
4 | test_that("error messages", {
5 |
6 | design <- "2b*2w"
7 | n <- 100
8 | mu <- c(24, 26.2, 27, 28)
9 | sd <- 6.4
10 |
11 | design_result <- ANOVA_design(design = design,
12 | n = n,
13 | mu = mu,
14 | sd = sd,
15 | plot = FALSE)
16 |
17 |
18 | expect_error(power_twoway_between(), "argument \"design_result\" is missing, with no default" )
19 | expect_error(power_twoway_between(design_result), "Only two-way between designs allowed for this function")
20 |
21 | })
22 |
23 | #Function check
24 | test_that("2x2 design", {
25 |
26 | #From Data Colada validation file https://github.com/Lakens/ANOVA_power_simulation/blob/master/validation_files/4.2_power_for_interactions.pdf
27 | design <- "2b*2b"
28 | n <- 150
29 | mu <- c(20, 20, 20, 25) #All means are equal - so there is no real difference.
30 | sd <- 20
31 | labelnames <- c("fruit", "apple", "banana", "hunger", "no hunger", "very hungry") #
32 | # the label names should be in the order of the means specified above.
33 | design_result <- ANOVA_design(design = design,
34 | n = n,
35 | mu = mu,
36 | sd = sd,
37 | labelnames = labelnames,
38 | plot = FALSE)
39 |
40 |
41 |
42 | power <- power_twoway_between(design_result)
43 | expect_equal(c(power$power_A, power$power_B, power$power_AB), c(.33,.33,.33), tolerance = .009)
44 |
45 | })
46 |
--------------------------------------------------------------------------------
/tests/testthat/test_sim_cor.R:
--------------------------------------------------------------------------------
1 | context("Simulated Data")
2 | library(ANOVApower)
3 | library(reshape2)
4 |
5 | test_that("simulated correlations fit expected values", {
6 | design_result <- ANOVA_design(design = "2b*2w",
7 | n = 1000000,
8 | mu = c(0, 0, 0, 0),
9 | sd = 2,
10 | r = 0.8,
11 | labelnames = c("condition", "cheerful", "sad", "voice", "human", "robot"),
12 | plot = FALSE)
13 |
14 | design_result$cor_mat
15 |
16 | data_wide <- dcast(design_result$dataframe, subject ~ cond, value.var="y")
17 | a1 <- data_wide[,2][!is.na(data_wide[,2])]
18 | a2 <- data_wide[,3][!is.na(data_wide[,3])]
19 | b1 <- data_wide[,4][!is.na(data_wide[,4])]
20 | b2 <- data_wide[,5][!is.na(data_wide[,5])]
21 |
22 | res1 <- cor(a1, a2)
23 | res2 <- cor(a1, b1)
24 | res3 <- cor(a1, b2)
25 | res4 <- cor(a2, b1)
26 | res5 <- cor(a2, b2)
27 | res6 <- cor(b1, b2)
28 |
29 | res1i <- cor(a1, a1)
30 | res2i <- cor(a2, a2)
31 | res3i <- cor(b1, b1)
32 | res4i <- cor(b2, b2)
33 |
34 | expect_equal(res1, 0.8, tolerance = .003)
35 | expect_equal(res2, 0, tolerance = .003)
36 | expect_equal(res3, 0, tolerance = .003)
37 | expect_equal(res4, 0, tolerance = .003)
38 | expect_equal(res5, 0, tolerance = .003)
39 | expect_equal(res6, 0.8, tolerance = .003)
40 | expect_equal(res1i, 1, tolerance = .003)
41 | expect_equal(res2i, 1, tolerance = .003)
42 | expect_equal(res3i, 1, tolerance = .003)
43 | expect_equal(res4i, 1, tolerance = .003)
44 | })
45 |
46 |
--------------------------------------------------------------------------------
/vignettes/screenshots/PS2000.gif:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Lakens/ANOVApower/41a05129aeb70e344471bac691158285192b0fea/vignettes/screenshots/PS2000.gif
--------------------------------------------------------------------------------
/vignettes/screenshots/gpower_1.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Lakens/ANOVApower/41a05129aeb70e344471bac691158285192b0fea/vignettes/screenshots/gpower_1.png
--------------------------------------------------------------------------------
/vignettes/screenshots/gpower_10.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Lakens/ANOVApower/41a05129aeb70e344471bac691158285192b0fea/vignettes/screenshots/gpower_10.png
--------------------------------------------------------------------------------
/vignettes/screenshots/gpower_11.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Lakens/ANOVApower/41a05129aeb70e344471bac691158285192b0fea/vignettes/screenshots/gpower_11.png
--------------------------------------------------------------------------------
/vignettes/screenshots/gpower_12.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Lakens/ANOVApower/41a05129aeb70e344471bac691158285192b0fea/vignettes/screenshots/gpower_12.png
--------------------------------------------------------------------------------
/vignettes/screenshots/gpower_14.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Lakens/ANOVApower/41a05129aeb70e344471bac691158285192b0fea/vignettes/screenshots/gpower_14.png
--------------------------------------------------------------------------------
/vignettes/screenshots/gpower_5.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Lakens/ANOVApower/41a05129aeb70e344471bac691158285192b0fea/vignettes/screenshots/gpower_5.png
--------------------------------------------------------------------------------
/vignettes/screenshots/gpower_6.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Lakens/ANOVApower/41a05129aeb70e344471bac691158285192b0fea/vignettes/screenshots/gpower_6.png
--------------------------------------------------------------------------------
/vignettes/screenshots/gpower_9.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Lakens/ANOVApower/41a05129aeb70e344471bac691158285192b0fea/vignettes/screenshots/gpower_9.png
--------------------------------------------------------------------------------
/vignettes/sim_data/power_result_cross_40.rds:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Lakens/ANOVApower/41a05129aeb70e344471bac691158285192b0fea/vignettes/sim_data/power_result_cross_40.rds
--------------------------------------------------------------------------------
/vignettes/sim_data/power_result_cross_80.rds:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Lakens/ANOVApower/41a05129aeb70e344471bac691158285192b0fea/vignettes/sim_data/power_result_cross_80.rds
--------------------------------------------------------------------------------
/vignettes/sim_data/power_result_ordinal.rds:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Lakens/ANOVApower/41a05129aeb70e344471bac691158285192b0fea/vignettes/sim_data/power_result_ordinal.rds
--------------------------------------------------------------------------------
/vignettes/sim_data/power_result_vig_1.rds:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Lakens/ANOVApower/41a05129aeb70e344471bac691158285192b0fea/vignettes/sim_data/power_result_vig_1.rds
--------------------------------------------------------------------------------
/vignettes/sim_data/power_result_vig_2.rds:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Lakens/ANOVApower/41a05129aeb70e344471bac691158285192b0fea/vignettes/sim_data/power_result_vig_2.rds
--------------------------------------------------------------------------------
/vignettes/sim_data/power_result_vig_3.rds:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Lakens/ANOVApower/41a05129aeb70e344471bac691158285192b0fea/vignettes/sim_data/power_result_vig_3.rds
--------------------------------------------------------------------------------
/vignettes/sim_data/power_result_vig_4.rds:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Lakens/ANOVApower/41a05129aeb70e344471bac691158285192b0fea/vignettes/sim_data/power_result_vig_4.rds
--------------------------------------------------------------------------------