【发布时间】:2023-03-10 16:50:01
【问题描述】:
对于一组输入(见下文),当我运行以下 R 代码时,我得到两个存储在 ncp 中的答案。但我想知道df2 应该是什么,以便ncp(即abs(ncp[2] - ncp[1]))中这两个答案之间的区别是.15?
所以,其他一切都已解决,df2 应该是什么,以便abs(ncp[2] - ncp[1]) = .15?这可以在 R 中完成吗?
alpha = c(.025, .975); df1 = 3; peta = .3 # The input
f <- function(alpha, q, df1, df2, ncp){ # Notice `ncp` is the unknown
alpha - suppressWarnings(pf(q = (peta / df1) / ((1 - peta)/df2), df1, df2, ncp, lower = FALSE))
}
ncp <- function(df2){ # Root finding: finds 2 `ncp` for a given `df2`
b <- sapply(c(alpha[1], alpha[2]),
function(x) uniroot(f, c(0, 1e7), alpha = x, q = peta, df1 = df1, df2 = df2)[[1]])
b / (b + (df2 + 4))
}
# Example of use:
ncp(df2 = 108) # Answers: 0.1498627 0.4100823
# What should `df2` be so that the difference between 2 answers is `.15`
【问题讨论】:
标签: r function math optimization linear-algebra