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
+59
View File
@@ -0,0 +1,59 @@
Exercise: Power simulation and power curves for t-test
================
## Temporal value asymmetry
“participants … were asked to imagine that they had agreed to spend 5 hr
entering data into a computer and to indicate how much money it would be
fair for them to receive. Some participants imagined that they had
completed the work 1 month previously, and others imagined that they
would complete the work 1 month in the future . . . Participants
believed that they should receive 101% more money for work they would do
1 month later ($M = \$125.04$) than for identical work that they had
done 1 month previously ($M = \$62.20$), $t(119) = 2.22$, $p = .03$,
$d = 0.41$” (Caruso, Gilbert, and Wilson 2008, 797)
### Plan a direct replication
1. What is a plausible standard deviation? Hint: $d = (M1 M2)/SD$
2. What is an interesting minimal effect size (in \$, Euro, or min)?
3. Simulate responses for 120 participants in both the *past* and the
*future* condition, assuming normal distributions with the same
variance. Use the standard deviation and the minimal effect size
from 1. and 2.
4. Parameter recovery: Repeat the simulation from 3. 2000 times to
re-estimate the parameters ($\mu_1, \mu_2, \sigma$) from the
simulated responses. Visualize the recovered parameters in box
plots.
Hint: $SE = 2/\sqrt{n} \cdot SD$, where $n$ is the total sample
size.
``` r
t <- t.test(x, y, mu = 0, var.equal = TRUE)
c(t$estimate, sd.pool = sqrt(n) / 2 * t$stderr)
```
5. Power simulation: Increase the total sample size to find out the `n`
necessary for 80% power for the t-test.
6. Power curves:
- Write an `R` function that takes sample size `n`, minimal effect
`d`, standard deviation `sd`, and number of replications `nrep` as
arguments. It should return the simulated power.
- Use this function to simulate the power for each combination of 4
different standard deviations and 4 sample sizes.
- Visualize these power curves in a single plot.
### References
<div id="refs" class="references csl-bib-body hanging-indent">
<div id="ref-CarusoGilbert08" class="csl-entry">
Caruso, E. M., D. T. Gilbert, and T. D. Wilson. 2008. “A Wrinkle in
Time: Asymmetric Valuation of Past and Future Events.” *Psychological
Science* 19 (8): 796801.
<https://doi.org/10.1111/j.1467-9280.2008.02159.x>.
</div>
</div>
+31
View File
@@ -0,0 +1,31 @@
Exercise: Power simulation and power curves for t-test
================
## Directed reading activities
Plan a replication of the “directed reading activities” study on
<https://jasp-stats.github.io/jasp-data-library/myChapters/chapter_2.html>
1. Which parameter value represents the minimum relevant effect that
the test should be able to detect? How large should this effect be?
Briefly say what it means in the context of the study.
2. What would be a plausible value for the standard deviation?
3. Simulate and draw power curves that depend on the effect size and
$n$. Use at least five different effect sizes and at least four
different sample sizes. The minimum relevant effect size should be
among the simulation conditions.
4. From inspecting the power curves, what $n$ in each group is required
to detect the minimum relevant effect with at least 80% power?
5. Create a renderable R script or an R Markdown file that includes
- a header with title, author, date
- at least one section head line
- the homework questions and your answers
- the simulation code and output
- the plot of the power curves.
Render the R or Rmd file to HTML.
+90
View File
@@ -0,0 +1,90 @@
#' ---
#' title: "Independent t-test: Power simulation and power curves"
#' author: ""
#' date: "Last modified: 2026-01-09"
#' ---
#' ## Application context
#'
#' Listening experiment
#'
#' - Task of each participant is to repeatedly adjust the frequency of a
#' comparison tone to sound equal in pitch to a 1000-Hz standard tone
#' - Mean adjustment estimates the point of subjective equality $\mu$
#' - Two participants will take part in the experiment providing adjustments
#' $X$ and $Y$
#' - Goal is to detect a difference between their points of subjective
#' equality $\mu_x$ and $\mu_y$ of 4 Hz
#'
#' ## Model
#'
#' Assumptions
#'
#' - $X_1, \ldots, X_n \sim N(\mu_x, \sigma_x^2)$ i.i.d.
#' - $Y_1, \ldots, Y_m \sim N(\mu_y, \sigma_y^2)$ i.i.d.
#' - both samples independent
#' - $\sigma_x^2 = \sigma_y^2$ but unknown
#'
#' Hypothesis
#'
#' - H$_0\colon~ \mu_x - \mu_y = \delta = 0$
#'
#' ## Power simulation
#+ cache = TRUE
n <- 110; m <- 110
pval <- replicate(2000, {
x <- rnorm(n, mean = 1000 + 4, sd = 10) # Participant 1 responses
y <- rnorm(m, mean = 1000, sd = 10) # Participant 2 responses
t.test(x, y, mu = 0, var.equal = TRUE)$p.value
})
mean(pval < 0.05)
#' ## Power curves
#'
#' Turn into a function of n and effect size
pwrFun <- function(n = 30, d = 4, sd = 10, nrep = 50) {
n <- n; m <- n
pval <- replicate(nrep, {
x <- rnorm(n, mean = 1000 + d, sd = sd)
y <- rnorm(m, mean = 1000, sd = sd)
t.test(x, y, mu = 0, var.equal = TRUE)$p.value
})
mean(pval < 0.05)
}
#'
#' Set up conditions and call power function
#+ cache = TRUE
cond <- expand.grid(d = 0:5,
n = c(50, 75, 100, 125))
cond$pwr <- mapply(pwrFun, n = cond$n, d = cond$d, MoreArgs = list(nrep = 500))
## Plot results
lattice::xyplot(pwr ~ d, cond, groups = n, type = c("g", "b"),
auto.key = list(corner = c(0, 1)))
#' ## Parameter recovery
n <- 100
out <- replicate(2000, {
x <- rnorm(n, mean = 1000 + 4, sd = 10)
y <- rnorm(n, mean = 1000, sd = 10)
t <- t.test(x, y, mu = 0, var.equal = TRUE)
c(
effect = -as.numeric(diff(t$estimate)),
sd.pool = t$stderr * sqrt(2*n) / 2
)
})
rowMeans(out)
boxplot(t(out))
+99
View File
@@ -0,0 +1,99 @@
Independent t-test: Power simulation and power curves
================
Last modified: 2026-01-09
## Application context
Listening experiment
- Task of each participant is to repeatedly adjust the frequency of a
comparison tone to sound equal in pitch to a 1000-Hz standard tone
- Mean adjustment estimates the point of subjective equality $\mu$
- Two participants will take part in the experiment providing
adjustments $X$ and $Y$
- Goal is to detect a difference between their points of subjective
equality $\mu_x$ and $\mu_y$ of 4 Hz
## Model
Assumptions
- $X_1, \ldots, X_n \sim N(\mu_x, \sigma_x^2)$ i.i.d.
- $Y_1, \ldots, Y_m \sim N(\mu_y, \sigma_y^2)$ i.i.d.
- both samples independent
- $\sigma_x^2 = \sigma_y^2$ but unknown
Hypothesis
- H$_0\colon~ \mu_x - \mu_y = \delta = 0$
## Power simulation
``` r
n <- 110; m <- 110
pval <- replicate(2000, {
x <- rnorm(n, mean = 1000 + 4, sd = 10) # Participant 1 responses
y <- rnorm(m, mean = 1000, sd = 10) # Participant 2 responses
t.test(x, y, mu = 0, var.equal = TRUE)$p.value
})
mean(pval < 0.05)
```
## [1] 0.831
## Power curves
Turn into a function of n and effect size
``` r
pwrFun <- function(n = 30, d = 4, sd = 10, nrep = 50) {
n <- n; m <- n
pval <- replicate(nrep, {
x <- rnorm(n, mean = 1000 + d, sd = sd)
y <- rnorm(m, mean = 1000, sd = sd)
t.test(x, y, mu = 0, var.equal = TRUE)$p.value
})
mean(pval < 0.05)
}
```
Set up conditions and call power function
``` r
cond <- expand.grid(d = 0:5,
n = c(50, 75, 100, 125))
cond$pwr <- mapply(pwrFun, n = cond$n, d = cond$d, MoreArgs = list(nrep = 500))
## Plot results
lattice::xyplot(pwr ~ d, cond, groups = n, type = c("g", "b"),
auto.key = list(corner = c(0, 1)))
```
![](powersim-ttest_files/figure-gfm/unnamed-chunk-3-1.png)<!-- -->
## Parameter recovery
``` r
n <- 100
out <- replicate(2000, {
x <- rnorm(n, mean = 1000 + 4, sd = 10)
y <- rnorm(n, mean = 1000, sd = 10)
t <- t.test(x, y, mu = 0, var.equal = TRUE)
c(
effect = -as.numeric(diff(t$estimate)),
sd.pool = t$stderr * sqrt(2*n) / 2
)
})
rowMeans(out)
```
## effect sd.pool
## 3.978400 9.993142
``` r
boxplot(t(out))
```
![](powersim-ttest_files/figure-gfm/unnamed-chunk-4-1.png)<!-- -->
Binary file not shown.

After

Width:  |  Height:  |  Size: 7.8 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.3 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.0 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.9 KiB