我的问题如下:我尝试从R的调查中计算一个参数(这里称为" seuil
"),这是一个等式的解。解决方案不能明确,但我知道它是定义的。
我写了这个程序
d<-Base_SP_final
f<-function(x) abs(-(1-d$shortA1-d$middleA1)*((d$timeA1)^(1+x))-d$shortA1*((d$timeA1+d$minshort1)^(1+x))-d$middleA1*((d$timeA1+d$minmiddle1)^(1+x))+(1-d$shortB1-d$middleB1)*((d$timeB1)^(1+x))+d$shortB1*((d$timeB1+d$minshort1)^(1+x))+d$middleB1*((d$timeB1+d$minmiddle1)^(1+x)))
d$seuil<-optimize(f, c(-10000, 10000), maximum=FALSE, tol=0.0001)
我收到此错误消息:"invalid function value in 'optimize'"
。是因为我使用变量而不是直接使用值(&#34; seuil
&#34;必须计算8016次!)?
我还尝试使用nlm
和uniroot
,但没有成功。
答案 0 :(得分:0)
我相信以下内容可以帮助您了解您的目标。
这会编写一个执行单optimize
的函数。然后,您可以将该函数传递给mapply
以迭代所有输入值。
optim_f <- function(x, shortA1, middleA1, timeA1,
minshort1, minmiddle1,
shortB1, middleB1, timeB1, ...){
optimize(
function(x, shortA1, middleA1, timeA1,
minshort1, minmiddle1,
shortB1, middleB1, timeB1){
abs(-(1 - shortA1 - middleA1) *
((timeA1) ^ (1 + x)) -
shortA1 *
((timeA1 + minshort1) ^ (1 + x)) -
middleA1 *
((timeA1 + minmiddle1) ^ (1 + x)) +
(1 - shortB1 - middleB1) *
((timeB1)^(1+x)) +
shortB1 *
((timeB1 + minshort1) ^ (1 + x)) +
middleB1 *
((timeB1 + minmiddle1) ^ (1 + x)))$minimum
},
shortA1 = shortA1,
middleA1 = middleA1,
timeA1 = timeA1,
minshort1 = minshort1,
minmiddle1 = minmiddle1,
shortB1 = shortB1,
middleB1 = middleB1,
timeB1 = timeB1, ...)
}
df$seuil <-
mapply(FUN = optime_f,
shortA1 = df$shortA1,
middleA1 = df$middleA1,
timeA1 = df$timeA1,
minshort1 = df$minshort1,
minmiddle1 = df$minmiddle1,
shortB1 = df$shortB1,
middleB1 = df$middleB1,
timeB1 = df$timeB1,
MoreArgs = list(interval = c(-10000, 10000),
maximum = FALSE,
tol = 0.0001),
SIMPLIFY = FALSE)