优化:在R中给出已知输出值的情况下求解输入值 [英] Optimization: Solve for an input value given a known output value in R

查看:60
本文介绍了优化:在R中给出已知输出值的情况下求解输入值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道我可以做到:

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()`

解决 power (假设未知)的问题,这是我下面的函数( foo )中的输入值之一.

to solve for power (suppose it was unknown) which is currently one of the input values in my below function (foo).

问题:,但现在假设我知道 foo 的其中一个输出( budget ),我现在可以解决力量(这是输入之一)?

Question: But now suppose I know one of the OUTPUTS (budget) of foo, can I now solve back for power (which is one of the inputs) via optimization?

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

推荐答案

我认为您可以轻松地做到这一点.

I thought you could do this yourself, easily.

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 

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

This may not work for all input variables, depending on how much influence they have on the different outputs.

这篇关于优化:在R中给出已知输出值的情况下求解输入值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆