通过 sapply 和 uniroot 构建逆回归
Construct an inverse regression via sapply and uniroot
我有一个函数如下:
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()
现在我想对这个函数做一个逆回归。我看过 Solving for the inverse of a function in R :
inverse = function (f, lower = -100, upper = 100) {
function (y) uniroot((function (x) f(x) - y), lower = lower, upper = upper)1
}
square_inverse = inverse(function (x) x^2, 0.1, 100)
square_inverse(4)
我正在尝试根据我的目的对其进行如下调整:
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)
不幸的是,这会产生:
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
据我了解:错误意味着根数多于一个根数(uniroot 只能处理一个根数)但不应该有多个根数,因为它是一个严格单调递增的函数。
我不明白的警告..
编辑:我试图支持它。我删除了两个指数,产生了以下情节:
这仍然会产生以下错误:
> 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
这是为什么?显然曲线在两个端点都有相反的符号。我猜端点是指根的左边和右边的点?
可能由于 "certain_function" 的定义,逆回归不起作用。这是一个矩阵向量乘积,结果又是一个向量。因此,我将其转换为常规函数 function(x) exp(exp(sum(x^0*VK[1],x^1*VK[2],x^2*VK[3],x^3*VK[4]))
,现在它可以正常工作了。
因此,一般情况下每次都要注意界限。
我有一个函数如下:
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()
现在我想对这个函数做一个逆回归。我看过 Solving for the inverse of a function in R :
inverse = function (f, lower = -100, upper = 100) { function (y) uniroot((function (x) f(x) - y), lower = lower, upper = upper)1 }
square_inverse = inverse(function (x) x^2, 0.1, 100)
square_inverse(4)
我正在尝试根据我的目的对其进行如下调整:
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)
不幸的是,这会产生:
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
据我了解:错误意味着根数多于一个根数(uniroot 只能处理一个根数)但不应该有多个根数,因为它是一个严格单调递增的函数。 我不明白的警告..
编辑:我试图支持它。我删除了两个指数,产生了以下情节:
这仍然会产生以下错误:
> 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
这是为什么?显然曲线在两个端点都有相反的符号。我猜端点是指根的左边和右边的点?
可能由于 "certain_function" 的定义,逆回归不起作用。这是一个矩阵向量乘积,结果又是一个向量。因此,我将其转换为常规函数 function(x) exp(exp(sum(x^0*VK[1],x^1*VK[2],x^2*VK[3],x^3*VK[4]))
,现在它可以正常工作了。
因此,一般情况下每次都要注意界限。