通过sapply和uniroot构造逆回归

通过sapply和uniroot构造逆回归,r,inverse,R,Inverse,我有一个函数,如下所示: V <- seq(50, 350, by = 1) > VK Voltage^0 Voltage^1 Voltage^2 Voltage^3 -1.014021e+01 9.319875e-02 -2.738749e-04 2.923875e-07 > plot(x = V, exp(exp(sapply(0:3, function(x) V^x) %*% VK)), type = "l"); grid() c

我有一个函数,如下所示:

V <- seq(50, 350, by = 1)
> VK
    Voltage^0     Voltage^1     Voltage^2     Voltage^3 
-1.014021e+01  9.319875e-02 -2.738749e-04  2.923875e-07 
> plot(x = V, exp(exp(sapply(0:3, function(x) V^x) %*% VK)), type = "l"); grid()
certain_function <- function(x=V) { exp(exp(sapply(0:3, function(x) V^x) %*% VK)) }

inverse = function (f, lower = 50, upper = 350) {
  function (y) uniroot((function (x) f(x) - y), lower = lower, upper = upper)[1]
}

inverse_regression = inverse(certain_function, 50, 350)

inverse_regression(2)
据我所知:这个错误意味着不止一个根(单根只能处理一个根),但不应该有多个根,因为它是严格单调递增函数。 我不明白这些警告

编辑:我正试图支持它。。我去掉了两个指数,得到了下面的图:

这仍然会产生以下错误:

> inverse_regression(0.1)

 Error in uniroot((function(x) f(x) - y), lower = lower, upper = upper) : 
  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") :
  the condition has length > 1 and only the first element will be used

为什么会这样?显然,曲线在两个端点处有相反的符号。。我猜终点是指从左到右到根的点

可能由于“某些函数”的定义,逆回归不起作用。这是一个矩阵向量积,结果又是一个向量。因此,我将其转换为一个常规函数
函数(x)exp(exp(sum(x^0*VK[1],x^1*VK[2],x^2*VK[3],x^3*VK[4])
,现在它开始工作了。
因此,人们通常应该知道每次的界限。

可能由于“某些函数”的定义,逆回归不起作用。这是一个矩阵向量积,结果又是一个向量。因此,我将其转换为正则函数
函数(x)exp(exp(sum(x^0*VK[1],x^1*VK[2],x^2*VK[3],x^3*VK[4]))
现在它正在工作。
因此,人们通常应该知道每次的边界。

好的,我解决了它。不知道它到底是什么,但当我将矩阵向量积转换为正则函数时,它工作:)好的,我解决了它。不知道它到底是什么,但当我将矩阵向量积转换为正则函数时,它工作:)
> inverse_regression(0.1)

 Error in uniroot((function(x) f(x) - y), lower = lower, upper = upper) : 
  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") :
  the condition has length > 1 and only the first element will be used