Skip to content

Instantly share code, notes, and snippets.

@pgilad
Last active April 15, 2018 10:28
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pgilad/0101146e527452d3d7aa994e500d1e89 to your computer and use it in GitHub Desktop.
Save pgilad/0101146e527452d3d7aa994e500d1e89 to your computer and use it in GitHub Desktop.
Find minimum sample size to obtain minimum power test in non-centralized chi-square
u <- c(0.1, 0.3, 0.5)
chi_df <- 3
target_power <- 0.9
chi_base <- qchisq(0.95, chi_df)
get_power <- function(n) {
return (pchisq(q = chi_base, df = chi_df, ncp = n * u %*% u, lower.tail = FALSE))
}
get_power(20)
get_power(30)
n_target <- optimize(function(n) { return(abs(get_power(n) - target_power))}, lower = 1, upper = 300)
print(ceiling(n_target$minimum))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment