优化:求解给定R中已知输出值的输入值

优化:求解给定R中已知输出值的输入值,r,function,math,mathematical-optimization,nonlinear-optimization,R,Function,Math,Mathematical Optimization,Nonlinear Optimization,我知道我能做到: p0 = foo() ; fn1 = function(x) sum((foo(power=x) - p0)^2) optimize(fn1, c(0, 100))[[1]] ### >[1] 79.8817 almost 80 as `power` in input of `foo()` 要解决当前是我下面函数(foo)中的输入值之一的power(假设未知) 问题:但是现在假设我知道foo的一个输出(budget),我现在可以通过优化来解决power(这是输入之

我知道我能做到:

p0 = foo()  ;  fn1 = function(x) sum((foo(power=x) - p0)^2) 
optimize(fn1, c(0, 100))[[1]] ### >[1] 79.8817 almost 80 as `power` in input of `foo()`
要解决当前是我下面函数(
foo
)中的输入值之一的
power
(假设未知)

问题:但是现在假设我知道
foo
的一个输出(
budget
),我现在可以通过优化来解决
power
(这是输入之一)的问题吗

foo <- function(A = 200, As = 15, B = 100,Bs = 10, power = 80, iccmax = 0.15,mdes = .25,SD = 1.2)
{
  tail <- 2
  alpha <- 5
  inv_d <- function(mdes) {
    c(mean_dif = 1, Vmax = 2/mdes^2)
  }
  SDr <- 1/SD
  pars <- inv_d(mdes)
  mean_dif <- pars[[1]]
  Vmax <- pars[[2]]
  zbeta <- qnorm((power/100))
  zalpha <- qnorm(1-(alpha/(100*tail)))
  maxvarmean_difhat <- (mean_dif / (zbeta + zalpha))**2
  ntreat <- sqrt((A/As)*((1-iccmax)/iccmax))
  ncont <- sqrt((B/Bs)*((1-iccmax)/iccmax))
  costpertreatcluster <- A + (As*ntreat)
  costperconcluster <- B + (Bs*ncont)
  gtreat <- (sqrt(A*iccmax) + sqrt(As*(1-iccmax)))**2
  gcon <- (sqrt(B*iccmax) + sqrt(Bs*(1-iccmax)))**2
  pratio <- sqrt(gtreat/gcon)
  budgetratio <- 99999
  budgetratio <- ifelse( ((pratio <= SD) & (pratio >= SDr)), pratio**2, ifelse((pratio > SD), pratio*SD, pratio*SDr))
  fraction <- budgetratio/(1 + budgetratio)
  mmvnumer <- 99999
  mmvnumer <- ifelse( ((pratio <= SD) & (pratio >= SDr)),
                      gcon*Vmax*(1+(pratio**2)),
                      ifelse((pratio > SD),
                             gcon*Vmax*(((pratio*SD)+1)**2/((SD**2)+1)),
                             gcon*Vmax*(((pratio*SDr)+1)**2/((SDr**2) + 1))) )
  budget <- mmvnumer/maxvarmean_difhat
  treatbudget <- fraction*budget
  conbudget <- (1-fraction)*budget
  ktreat <- treatbudget/costpertreatcluster
  kcont <- conbudget/costperconcluster
  ktreatrup <- ceiling(ktreat)
  kcontrup <- ceiling(kcont)
  ktreatplus <- ifelse(pmin(ktreatrup,kcontrup) < 8, ktreatrup + 3, ktreatrup + 2)
  kcontplus <- ifelse(pmin(ktreatrup,kcontrup) < 8, kcontrup + 3, kcontrup + 2)
  budgetplus <- (ktreatplus*costpertreatcluster) + (kcontplus*costperconcluster)
  
  return(c(ncont = ncont, kcont = kcontplus,
    ntreat = ntreat, ktreat = ktreatplus, budget = budgetplus))
}
#--------------------------------------------------------------------------------
# EXAMPLE OF USE:
foo()

       ncont        kcont       ntreat       ktreat       budget 
    7.527727    73.000000     8.692270    62.000000 33279.051347

foo我以为你可以自己做,很容易

b0 = foo()[5]               # budget: 33279.051347
fn2 = function(x) {
    foo(power = x)["budget"] - b0
}
uniroot(fn2, c(70, 90))
## $root
## [1] 79.99041
## $f.root
## budget 
##      0 

这可能不适用于所有输入变量,这取决于它们对不同输出的影响程度。

您的意思是:保持除功率以外的所有输入恒定?当然,即使应用
uniroot
,您也可以这样做。如果您想改变所有输入以仅重建一个输出,这很可能是不可能的,因为所有的局部极小值(也因为您的函数产生了许多NAs)。假设我知道一个输出,
budget
,这是否有助于确定可能未知的输入值之一,如
power
mdes
?是的,保持其他一切不变。