R 中的 samplesize 包,了解参数

samplesize package in R, understanding the parameters

小免责声明:我考虑过在交叉验证上发布这个,但我觉得这与软件实现更相关。如果您不同意,可以迁移该问题。

我正在试用包 samplesize。我试图破译函数 n.ttestk 参数是什么。文档中说明如下:

k Sample fraction k

这不是很有帮助。这个参数到底是什么?

我正在执行以下计算,所有基本值都在 vals 变量中,我在下面提供:

power <- 0.90
alpha <- 0.05
vals <- ??? # These values are provided below
mean.diff <- vals[1,2]-vals[2,2]
sd1 <- vals[1,3]
sd2 <- vals[2,3]
k <- vals[2,4]/(vals[1,4]+vals[2,4])
design <- "unpaired"
fraction <- "unbalanced"
variance <- "equal"

# Get the sample size
n.ttest(power = power, alpha = alpha, mean.diff = mean.diff, 
        sd1 = sd1, sd2 = sd2, k = k, design = design, 
        fraction = fraction, variance = variance)

vals 包含以下值:

> vals
  affected       mean       sd length
1        1 -0.8007305 7.887657     57
2        2  4.5799913 6.740781     16

k一组在观察总数中所占的比例吗?或者是别的什么?如果我是正确的,那么该比例是否对应于 sd1sd2 组?

您的第一直觉是正确的 - 这属于 stats.SE 而不是 SO。参数 k 具有统计解释,可以在任何有关功率分析的参考资料中找到。它基本上设置了第二个样本的样本大小,在双样本测试的情况下,第二个样本被限制为第一个样本的一定比例。

您可以在此处查看相关代码行(n.ttest 的第 106 至 120 行):

unbalanced = {
                  df <- n.start - 2
                  c <- (mean.diff/sd1) * (sqrt(k)/(1 + k))
                  tkrit.alpha <- qt(conf.level, df = df)
                  tkrit.beta <- qt(power, df = df)
                  n.temp <- ((tkrit.alpha + tkrit.beta)^2)/(c^2)
                  while (n.start <= n.temp) {
                    n.start <- n.start + 1
                    tkrit.alpha <- qt(conf.level, df = n.start - 
                      2)
                    tkrit.beta <- qt(power, df = n.start - 2)
                    n.temp <- ((tkrit.alpha + tkrit.beta)^2)/(c^2)
                  }
                  n1 <- n.start/(1 + k)
                  n2 <- k * n1

你的情况:

library(samplesize)

vals = data.frame(
  affected = c(1, 2), 
  mean = c(-0.8007305, 4.5799913), 
  sd = c(7.887657, 6.740781), 
  length = c(57, 16))

power <- 0.90
alpha <- 0.05
mean.diff <- vals[1,2]-vals[2,2]
sd1 <- vals[1,3]
sd2 <- vals[2,3]
k <- vals[2,4]/(vals[1,4]+vals[2,4])
k <- vals[2,4]/vals[1,4]

design <- "unpaired"
fraction <- "unbalanced"
variance <- "equal"

# Get the sample size
tt1 = n.ttest(power = power, 
        alpha = alpha, 
        mean.diff = mean.diff, 
        sd1 = sd1, 
        sd2 = sd2, 
        k = k, 
        design = design, 
        fraction = fraction, 
        variance = variance)

你可以看到:

assertthat::are_equal(ceiling(tt1$`Sample size group 1`*tt1$Fraction), 
                      tt1$`Sample size group 2`)