0

我正在使用pwr包运行功率分析。我编写了一个函数来对不同的 n 值运行分析,并将所有 n 的效果大小 d 保存为 df 中的 2 列。

results_df <- data.frame()

run_power_analysis <- function(n){
  
  power_analysis <- pwr.t.test(n=n,
                               d=NULL,
                               type="two.sample",
                               sig.level=0.05,
                               power=0.8,
                               alternative="two.sided")
  
  results_df <- rbind(results_df, power_analysis[c(1,2)])
  }

我将 n 个值保存为向量,并希望在它们上运行该函数,但出现以下错误:

n_vector <- c(7,14,21,28,30,35,42,49,56,63,70,77,84,91,98)

results <- run_power_analysis(n_vector)

>>>
Error in uniroot(function(d) eval(p.body) - power, c(1e-07, 10)) : 
  f() values at end points not of opposite sign
In addition: Warning messages:
1: In if (is.na(f.lower)) stop("f.lower = f(lower) is NA") :
  the condition has length > 1 and only the first element will be used
2: In if (is.na(f.upper)) stop("f.upper = f(upper) is NA") :
 
 Show Traceback
 
 Rerun with Debug
 Error in uniroot(function(d) eval(p.body) - power, c(1e-07, 10)) : 
  f() values at end points not of opposite sign 

这似乎只有在我为 n 插入多个值时才会发生,比如向量。如果我逐个插入 1,我会得到我需要的结果,但这很乏味吗?

4

0 回答 0