[英]Back solving a function or goal seek in R
I'm trying to learn my way around R and I need a little help.我正在尝试学习 R 的方法,我需要一些帮助。 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.我想要做的是创建一个新函数,它将找到产生target
所需的price1
值。 For example in this new function I would be able to set 47320
as a parameter and it would return 300
.例如,在这个新函数中,我可以将47320
设置为参数,它会返回300
。 In the first function, myFunction
, a value of 300
returns a value of 47320
.在第一个函数myFunction
,值300
返回值47320
。
How can I write a function in R to do this?如何在 R 中编写函数来执行此操作? Is there an existing function in R? R 中是否存在现有函数? I see through my Googling and searching on this site, a lot of people recommend the uniroot()
function or optimize()
.我通过我在这个网站上的谷歌搜索和搜索看到,很多人推荐uniroot()
函数或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.如果有帮助,我知道在 excel 中我可以通过使用目标搜索工具轻松解决这个问题。 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:如果你想用optimize
做到这一点,你可以这样做:
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
. R 将搜索 [-1000, 1000] 以试图找到一个值x
,该值最小化myFunction(x) - 42010
。 In this case, it finds 123
, for which myFunction(123)
returns 42010
and so abs(myFunction(x) - 42010)
returns 0
.在这种情况下,它找到123
, myFunction(123)
返回42010
,因此abs(myFunction(x) - 42010)
返回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.在我们的函数unfunction
, lower
和upper
指定要搜索的空间。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.