Initialize repository

This commit is contained in:
2026-01-09 16:55:02 +01:00
commit e2b548c00c
34 changed files with 3885 additions and 0 deletions
+62
View File
@@ -0,0 +1,62 @@
Exercise: Testing the interaction in two-by-two ANOVA
================
## Anchoring and adjustment
Items and anchor values (Jacowitz and Kahneman 1995)
- How tall is the largest coast redwood in the world? \[20, 168m\]
- How many member states belong to the United Nations? \[14, 127
members\]
- How much km/h is the maximum speed of a house cat? \[11, 48km/h\]
### Research question
- Does time pressure (respond within 7s) increase the anchor effect?
### Suggest a minimum relevant effect
- Go to <https://apps.mathpsy.uni-tuebingen.de/fw/pars2eta/>
- Fix the parameters of the ANOVA model
### Some background
- Open anchoring quest (Röseler et al. 2022), <https://osf.io/ygnvb/>
### Plan the study
- Pick one of the three items
- Parameter recovery
- Make a data frame for the two-by-two design
- With the parameter values determined before, simulate responses
- Re-estimate the parameters
- Power simulation
- Calculate the sample size necessary to detect the time-pressure
effect
### Bonus task: Verify the plausibility of your model
- Download the raw data from the open anchoring quest project
- Estimate $\sigma$ and compare it to your value
### References
<div id="refs" class="references csl-bib-body hanging-indent">
<div id="ref-JacowitzKahneman95" class="csl-entry">
Jacowitz, K. E., and D. Kahneman. 1995. “Measures of Anchoring in
Estimation Tasks.” *Personality and Social Psychology Bulletin* 21 (11):
116166. <https://doi.org/10.1177/01461672952111004>.
</div>
<div id="ref-RoeselerWeber22" class="csl-entry">
Röseler, L., L. Weber, K. A. C. Helgerth, E. Stich, M. Günther, P.
Tegethoff, F. S. Wagner, et al. 2022. “OpAQ: Open Anchoring Quest,
Version 1.1.50.97.” <https://doi.org/10.17605/OSF.IO/YGNVB>.
</div>
</div>
+93
View File
@@ -0,0 +1,93 @@
#' ---
#' title: "Two-by-two ANOVA: Power simulation of the interaction test"
#' author: ""
#' date: "Last modified: 2026-01-09"
#' ---
#' ## Application context
#'
#' Effect of fertilizers
#'
#' In an experiment, two fertilizers (A and B, each either low or high dose)
#' will be combined and the yield of peas (Y) in kg be observed. Goal is to
#' detect an increase of the Fertilizer-A effect by an additional 12 kg when
#' combined with a high dose of Fertilizer B (interaction effect).
#'
#+ echo = FALSE
dat <- data.frame(
A = rep(1:2, each = 2),
B = rep(1:2, times = 2),
y = c(30, 30 + 5, 30 + 30, 30 + 30 + 5 + 12)
)
par(mai = c(.6, .6, .1, .1), mgp = c(2, .7, 0))
plot(y ~ A, dat, type = "n", xlim = c(0.8, 2.2), ylim = c(20, 80),
xlab = "Fertilizer A", ylab = "Yield (kg)", xaxt = "n")
lines(y ~ A, dat[dat$B == 1, ], col = "darkblue")
lines(y ~ A, dat[dat$B == 2, ], col = "darkblue")
lines(1:2, c(30 + 5, 30 + 30 + 5), lty = 2, col = "darkblue")
axis(1, 1:2, c("low", "high"))
arrows(2, 30 + 30 + 6, 2, 30 + 30 + 5 + 11, code = 3, length = 0.1,
col = "darkgray")
text(c(1, 2, 1, 2, 2.07), c(27, 55, 40, 80, 65 + 6),
c(expression(mu), expression(mu + alpha[2]),
expression(mu + beta[2]),
expression(mu + alpha[2] + beta[2] + (alpha * beta)[22]),
"12 kg")
)
text(1.5, 27 + 30/2, "Fertilizer B: low", srt = 21, col = "darkgray")
text(1.5, 38 + (30 + 12)/2, "Fertilizer B: high", srt = 32, col = "darkgray")
#' ## Model
#'
#' Assumptions
#'
#' - $Y_{ijk} = \mu + \alpha_i + \beta_j + (\alpha\beta)_{ij} +
#' \varepsilon_{ijk}$
#' - $\varepsilon_{ijk} \sim N(0, \sigma^2) \text{ i.i.d.}$
#' - $i = 1, \dots, I$; $j = 1, \dots, J$; $k = 1, \dots, K$
#' - $\alpha_1 = \beta_1 := 0$
#'
#' Hypothesis
#'
#' - H$_0^{AB}\colon~ (\alpha\beta)_{ij} = 0 \text{ for all } i,j$
#'
#' ## Setup
set.seed(1704)
n <- 96
dat <- data.frame(
A = factor(rep(1:2, each = n/2), labels = c("low", "high")),
B = factor(rep(rep(1:2, each = n/4), 2), labels = c("low", "high"))
)
X <- model.matrix(~ A*B, dat)
unique(X)
beta <- c(mu = 30, a2 = 30, b2 = 5, ab22 = 12)
means <- X %*% beta
lattice::xyplot(I(means + rnorm(n, sd = 10)) ~ A, dat, groups = B,
type = c("g", "p", "a"), auto.key = TRUE, ylab = "Yield (kg)")
#' ## Parameter recovery
#+ cache = TRUE
out <- replicate(2000, {
y <- means + rnorm(n, sd = 10) # y = mu + a + b + ab + e
m <- aov(y ~ A * B, dat)
c(coef(m), sigma = sigma(m))
})
boxplot(t(out))
#' ## Power simulation
#+ cache = TRUE
pval <- replicate(2000, {
y <- means + rnorm(n, sd = 10)
m <- aov(y ~ A*B, dat)
summary(m)[[1]]$"Pr(>F)"[3] # test of interaction
})
mean(pval < 0.05)
+82
View File
@@ -0,0 +1,82 @@
Two-by-two ANOVA: Power simulation of the interaction test
================
Last modified: 2026-01-09
## Application context
Effect of fertilizers
In an experiment, two fertilizers (A and B, each either low or high
dose) will be combined and the yield of peas (Y) in kg be observed. Goal
is to detect an increase of the Fertilizer-A effect by an additional 12
kg when combined with a high dose of Fertilizer B (interaction effect).
![](powersim-anova_files/figure-gfm/unnamed-chunk-1-1.png)<!-- -->
## Model
Assumptions
- $Y_{ijk} = \mu + \alpha_i + \beta_j + (\alpha\beta)_{ij} + \varepsilon_{ijk}$
- $\varepsilon_{ijk} \sim N(0, \sigma^2) \text{ i.i.d.}$
- $i = 1, \dots, I$; $j = 1, \dots, J$; $k = 1, \dots, K$
- $\alpha_1 = \beta_1 := 0$
Hypothesis
- H$_0^{AB}\colon~ (\alpha\beta)_{ij} = 0 \text{ for all } i,j$
## Setup
``` r
set.seed(1704)
n <- 96
dat <- data.frame(
A = factor(rep(1:2, each = n/2), labels = c("low", "high")),
B = factor(rep(rep(1:2, each = n/4), 2), labels = c("low", "high"))
)
X <- model.matrix(~ A*B, dat)
unique(X)
```
## (Intercept) Ahigh Bhigh Ahigh:Bhigh
## 1 1 0 0 0
## 25 1 0 1 0
## 49 1 1 0 0
## 73 1 1 1 1
``` r
beta <- c(mu = 30, a2 = 30, b2 = 5, ab22 = 12)
means <- X %*% beta
lattice::xyplot(I(means + rnorm(n, sd = 10)) ~ A, dat, groups = B,
type = c("g", "p", "a"), auto.key = TRUE, ylab = "Yield (kg)")
```
![](powersim-anova_files/figure-gfm/unnamed-chunk-2-1.png)<!-- -->
## Parameter recovery
``` r
out <- replicate(2000, {
y <- means + rnorm(n, sd = 10) # y = mu + a + b + ab + e
m <- aov(y ~ A * B, dat)
c(coef(m), sigma = sigma(m))
})
boxplot(t(out))
```
![](powersim-anova_files/figure-gfm/unnamed-chunk-3-1.png)<!-- -->
## Power simulation
``` r
pval <- replicate(2000, {
y <- means + rnorm(n, sd = 10)
m <- aov(y ~ A*B, dat)
summary(m)[[1]]$"Pr(>F)"[3] # test of interaction
})
mean(pval < 0.05)
```
## [1] 0.817
Binary file not shown.

After

Width:  |  Height:  |  Size: 8.4 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 5.5 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 5.1 KiB