【发布时间】:2018-06-17 15:58:24
【问题描述】:
在下面的函数中,我想知道为什么当我使用一个向量作为参数d 时,该函数可以正常工作,但是当我使用一个向量作为参数t,函数(来自uniroot)抛出错误:
f() values at end points not of opposite sign
cii <- function(d, t = NA, n1, n2 = NA, conf.level = .95){
ci <- Vectorize(function(d, t, n1, n2, conf.level){
options(warn = -1)
alpha = (1 - conf.level)/2
N = ifelse(is.na(n2), n1, (n1 * n2)/(n1 + n2))
df = ifelse(is.na(n2), n1 - 1, (n1 + n2) - 2)
d.SE = 1/sqrt(N)
q = ifelse(is.na(t), d/d.SE, t)
f <- function(ncp, alpha, q, df){
alpha - suppressWarnings(pt(q, df, ncp, lower.tail = FALSE))
}
CI <- sapply(c(alpha, 1-alpha),
function(x) uniroot(f, interval = c(0, q+2e2), alpha = x, q = q, df = df)[[1]]*d.SE)
CI
})
d <- if(missing(d)) NA else d
data.frame(t(ci(d = d, t = t, n1 = n1, n2 = n2, conf.level = conf.level)))
}
# EXAMPLES OF USE:
cii(d = c(2, 3), n1 = 30) # Works perfectly fine!
cii(t = c(2, 3), n1 = 30) # Throws error: `f() values at end points not of opposite sign`
【问题讨论】:
标签: r function optimization linear-algebra