本文是小编为大家收集整理的关于在 R 中回求函数或目标值的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。
问题描述
我正在尝试学习r的方法,我需要一些帮助.以下是我正在处理的问题的一些示例.
myFunction <- function(price1) { prices <- c(1:50) prices[1] <- price1 recursiveA <- vector(length = 51) recursiveA[1] <- 100 for (i in 1:50) { recursiveA[i+1] <- 30*prices[i] + recursiveA[i] } target <- recursiveA[51] return(target) }
我想做的是创建一个新功能,该功能将找到产生target值所需的price1值.例如,在此新功能中,我可以将47320设置为参数,并且它将返回300.在第一个函数中,myFunction,300的值返回47320的值.
如何在R中编写功能来执行此操作? R中有现有功能吗?我通过在此站点上搜索并搜索搜索,很多人推荐uniroot()函数或optimize().我不知道如何将其用于代数四边形以外的其他方法.
如果有帮助,我知道在Excel中我可以使用目标寻求工具轻松解决此问题.您可以设置所需的输出,并从您定义的公式中找到所需的输入.
请让我知道是否有什么不清楚的事情,我会尽力进一步解释.
任何帮助都非常感谢.谢谢.
推荐答案
您实际上不需要递归功能.
这是一种矢量化方法:
f <- function(x) tail(cumsum(c(100, 30*c(x, 2:50))), 1) f(123) # 42010
并逆转操作:
anti_f <- function(x) (x - 30*50*51/2 + 30 - 100)/30 anti_f(42010) # 123
当然,当您实际需要重复时,这会不太有用.我的观点是,您应该在可能的情况下寻找机会.
如果您想使用optimize进行此操作,则可以:
f <- function(x) abs(myFunction(x) - 42010) optimize(f, lower=-1000, upper=1000) # $minimum # [1] 123 # # $objective # [1] 2.512278e-05
r将搜索[-1000,1000],以尝试找到一个值x,以最大程度地降低myFunction(x) - 42010的绝对值.在这种情况下,它找到了123,myFunction(123)返回42010,so abs(myFunction(x) - 42010)返回0.
如果要将其包装在功能中,则可以:
unfunction <- function(x, lower, upper) { optimize(function(y) abs(myFunction(y) - x), lower=lower, upper=upper) } unfunction(42010, -1000, 1000) # $minimum # [1] 123 # # $objective # [1] 2.512278e-05 unfunction(47320, -1000, 1000) # $minimum # [1] 300 # # $objective # [1] 0.0002383182
在我们的函数中unfunction,lower和upper指定要搜索的空间.
问题描述
I'm trying to learn my way around R and I need a little help. Below is a little sample of the kind of problem I am working on.
myFunction <- function(price1) { prices <- c(1:50) prices[1] <- price1 recursiveA <- vector(length = 51) recursiveA[1] <- 100 for (i in 1:50) { recursiveA[i+1] <- 30*prices[i] + recursiveA[i] } target <- recursiveA[51] return(target) }
What I want to do is create a new function that will find the price1 value needed to yield a target value. For example in this new function I would be able to set 47320 as a parameter and it would return 300. In the first function, myFunction, a value of 300 returns a value of 47320.
How can I write a function in R to do this? Is there an existing function in R? I see through my Googling and searching on this site, a lot of people recommend the uniroot() function or optimize(). I can't figure out how to use that for something other than algebraic quadratics.
If it helps, I know that in excel I can solve this easily by using the goal seek tool. You can set a desired output and it finds the needed input from the formula(s) you define.
Please let me know if anything's unclear and I will try my best to explain further.
Any help is much appreciated. Thank you.
推荐答案
You don't actually need a recursive function here.
Here's a vectorised approach:
f <- function(x) tail(cumsum(c(100, 30*c(x, 2:50))), 1) f(123) # 42010
And to reverse the operation:
anti_f <- function(x) (x - 30*50*51/2 + 30 - 100)/30 anti_f(42010) # 123
Of course, this is less helpful when you actually do need to recurse. My point is just that you should look for opportunities to vectorise when possible.
If you want to do this with optimize, you can do:
f <- function(x) abs(myFunction(x) - 42010) optimize(f, lower=-1000, upper=1000) # $minimum # [1] 123 # # $objective # [1] 2.512278e-05
R will search [-1000, 1000] in an attempt to find a value, x, that minimises the absolute value of myFunction(x) - 42010. In this case, it finds 123, for which myFunction(123) returns 42010 and so abs(myFunction(x) - 42010) returns 0.
If you want to wrap this in a function, you can do:
unfunction <- function(x, lower, upper) { optimize(function(y) abs(myFunction(y) - x), lower=lower, upper=upper) } unfunction(42010, -1000, 1000) # $minimum # [1] 123 # # $objective # [1] 2.512278e-05 unfunction(47320, -1000, 1000) # $minimum # [1] 300 # # $objective # [1] 0.0002383182
In our function unfunction, lower and upper specify the space to be searched.