请求计算qnorm值的逆及基于Walter公式的一致性研究功效计算
2. Adjusting Walter's Formula for Power Calculation (Given Number of Subjects)
Your original R function calculates the required sample size for a concordance study using an adapted Walter's formula. To modify it for power calculation when the number of subjects is known, we need to rearrange the formula to solve for puissance (power) instead of sample size.
Step-by-Step Derivation
The original sample size formula (NSN) is:
[ NSN = 1 + \frac{2 \times (U_\alpha + U_\beta)^2 \times nb}{(\log(C))^2 \times (nb - 1)} ]
Where:
- ( U_\alpha = |qnorm(\alpha)| ) (critical value for the significance level)
- ( U_\beta = qnorm(puissance) ) (critical value for power)
- ( A = \frac{ccc0}{1-ccc0} ), ( B = \frac{ccc1}{1-ccc1} )
- ( C = \frac{1 + nb \times A}{1 + nb \times B} )
We rearrange this to solve for power:
- Isolate the term containing ( U_\beta ):
[ (U_\alpha + U_\beta)^2 = \frac{(NSN - 1) \times (\log(C))^2 \times (nb - 1)}{2 \times nb} ] - Take the square root of both sides:
[ U_\alpha + U_\beta = \sqrt{\frac{(NSN - 1) \times (\log(C))^2 \times (nb - 1)}{2 \times nb}} ] - Solve for ( U_\beta ):
[ U_\beta = \sqrt{\frac{(NSN - 1) \times (\log(C))^2 \times (nb - 1)}{2 \times nb}} - U_\alpha ] - Finally, compute power as ( pnorm(U_\beta) )
R Function for Power Calculation
Here's the adapted function that takes the known number of subjects and returns the calculated power:
calcul_power <- function(alpha, n_subjects, ccc0, ccc1, nb_replicates) { # Parameter explanations: # alpha: Significance level (e.g., 0.05) # n_subjects: Known number of study subjects # ccc0: Concordance coefficient to compare against # ccc1: Target concordance coefficient to detect # nb_replicates: Number of replicate measurements per subject if (n_subjects <= 1) { stop("Number of subjects must be greater than 1") } U_alpha <- abs(qnorm(alpha)) A <- ccc0 / (1 - ccc0) B <- ccc1 / (1 - ccc1) C <- (1 + nb_replicates * A) / (1 + nb_replicates * B) # Calculate the term under the square root sqrt_term <- ((n_subjects - 1) * (log(C))^2 * (nb_replicates - 1)) / (2 * nb_replicates) if (sqrt_term < 0) { warning("Square root term is negative: power cannot be calculated with these parameters") return(NA) } U_beta <- sqrt(sqrt_term) - U_alpha puissance <- pnorm(U_beta) # Print context for clarity cat("--- Power Calculation Results ---\n") cat(paste("Significance level (alpha):", alpha, "\n")) cat(paste("Number of subjects:", n_subjects, "\n")) cat(paste("Baseline concordance (ccc0):", ccc0, "\n")) cat(paste("Target concordance (ccc1):", ccc1, "\n")) cat(paste("Replicates per subject:", nb_replicates, "\n")) cat(paste("Calculated power:", round(puissance, 4), "\n")) return(puissance) }
Example Usage
Let’s use parameters similar to your original sample size call, but input a known number of subjects (e.g., 100) to find the corresponding power:
# Calculate power with 100 subjects, alpha=0.05, ccc0=0.6, ccc1=0.75, 2 replicates per subject calcul_power(alpha = 0.05, n_subjects = 100, ccc0 = 0.6, ccc1 = 0.75, nb_replicates = 2)
Original Sample Size Function (For Reference)
To keep things clear, here’s your original function that calculates sample size from a specified power:
calcul_f <- function(alpha, puissance, ccc0, ccc1, nb){ cat("ccc0 corresponds to the concordance to compare, ccc1 the new concordance, and nb the number of replicates\n") U_alpha <- abs(qnorm(alpha)) U_beta <- qnorm(puissance) A <- ccc0/(1-ccc0) B <- ccc1/(1-ccc1) C <- (1+(nb*A)) / (1+(nb*B)) NSN <- 1+((2*((U_alpha+U_beta)^2)*nb)/(((log(C))^2)*(nb-1))) return(NSN) } # Original call example calcul_f(alpha = 0.05, puissance = 0.8, ccc0 = 0.6, ccc1 = 0.75, nb = 2)
内容的提问来源于stack exchange,提问作者user4686711

