Skip to content
Snippets Groups Projects
Commit dcfd5ca2 authored by Boulakis Paradeisios Alexandros's avatar Boulakis Paradeisios Alexandros
Browse files

eff plotter

parent 9f8b8727
No related branches found
No related tags found
No related merge requests found
library(ggplot2)
# specify how many simulations you want to run
nSimul <- 10000
# specify how many effects size you want to test
effects <- seq(.1,.9,.2)
# specify how many sample sizes you want to test
samples <- seq(10,150,10)
# Storage for plotting
results <- list()
# Simulation loop
for (effect in effects) {
sample.storage <- numeric(length(samples))
for (ii in seq_along(samples)) {
p.storage <- numeric(nSimul)
for (simulation in seq(nSimul)) {
# run statistical test
x <- rnorm(n = samples[ii], mean = 0, sd = 1)
y <- rnorm(n = samples[ii], mean = effect, sd = 1)
#extract significance
z <- t.test(x, y)
p.storage[simulation] <- z$p.value
}
achieved.power <- mean(p.storage < 0.05)
sample.storage[ii] <- achieved.power
}
# Store results for the current effect size
results[[paste0("effect_", effect)]] <- data.frame(
sample_size = samples,
achieved_power = sample.storage
)
}
# Combine results into a single data frame for plotting
plot_data <- do.call(rbind, lapply(names(results), function(effect) {
data <- results[[effect]]
data$effect <- effect
data
}))
# Convert effect to numeric for better plotting
plot_data$effect <- as.numeric(gsub("effect_", "", plot_data$effect))
# Create the plot
ggplot(plot_data, aes(x = sample_size, y = achieved_power, color = factor(effect))) +
geom_line(linewidth = 1) +
labs(
title = "Achieved Power vs. Sample Size",
x = "Sample Size",
y = "Achieved Power",
color = "Effect Size"
) +
theme_minimal()
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment