I have these 4 functions of 2 variables:
我有两个变量的4个函数
inner.a0b0<- function(tvar,gamma){
return(gamma^(1/2)*(1+tvar)^(-(gamma+1)/2)*exp(-tvar/2))}
K0.inte.func <- function(tvar,gam){return((1-tvar)*(exp(-tvar)-gam^(-1/2)*(1+tvar)^(-(gam+1)/2)*exp(-tvar/2)))}
unit.l.func <- function(tvar,gam){return((1+tvar)^((gam+1)/2)*exp(-tvar/2))}
beta.G <- function(tvar,gam){(1+tvar)^((gam+1)/2)*exp(-tvar/2)*(1-tvar))}
I have 2 different samples from which I get the estimated lambda.es = c(lambda.es.1, lambda.es.2).
我有两个不同的样本,我得到了估计的lambda.es = c(lambda.1, lambda.2)。
Then I have 2 vectors of 2 components by integrating as follows:
然后我有2个向量由2个分量组成,积分如下:
K0.deno <- sapply(lambda.es, function(x) integrate(inner.a0b0,lower=0,upper=Inf,gam=x)$value)
K0.inte.val<- sapply(lambda.es, function(x) integrate(K0.inte.func,lower=0,upper=Inf,gam=x)$value)
My aim now is: First, I define 2 functions (each corresponding to a component of lambda.es) from unit.l.func and beta.G and vectors K0.deno and K0.inte.val, so I write this:
我现在的目标是:首先,我从unit.l定义了两个函数(每个对应于lambda.es的一个组件)。函数和β。G和向量k0。deno和k0。inte0。val,我这样写:
K0.operator.b <- function(tvar, gam, deno,inte.val){res<- beta.G(tvar,gam) - (1-deno)*unit.l.func(tvar,gam)*inte.val
return(res)}
Basically, I want to have 2 function b1.til and b2.til like this:
基本上,我想要有两个函数b1。直到和b2。直到这样的:
b1.til <- K0.operator.b(tvar,gam,deno=K0.deno[1],inte.val=K0.inte.val[2])
b2.til <- K0.operator.b(tvar,gam,deno=K0.deno[2],inte.val=K0.inte.val[2])
Then, again I need to integrate b1.til and b2.til with gam=lambda.es. How can I do without writing everything twice? Thank you very much for looking. Any advice/help is much appreciated. I am grateful with any criticism for the code as well. Thanks a lot.
然后再对b1积分。直到和b2。直到与gam = lambda.es。我怎么能不把每件事都写两遍呢?非常感谢您的查找。非常感谢您的建议/帮助。我也很感激对代码的任何批评。非常感谢。
1 个解决方案
#1
0
I think this should succeed but you should actually provide real test cases for testing. At the moment we have no unit.l.func
or values of deno or val available for testing or any test case where you know what the answer should be.
我认为这应该会成功,但是您应该为测试提供真正的测试用例。目前我们没有单位。func或deno或val的值可用于测试或任何测试用例,您知道答案应该是什么。
Try returning a function that is defined at a point where those two values are available. The function should then carry them forward as part of its environment. That's called a "closure":
尝试返回在这两个值可用的点上定义的函数。然后,函数应该将它们作为环境的一部分进行转发。这就叫做“关闭”:
function1 <- function(tvar, gam)
{ function(tvar, gam){ beta.G(tvar,gam) - 1/(1-deno[1])*(unit.l.func( tvar, gam)-1)*val[1]}
}
function2 <- function(tvar, gam)
{ function(tvar, gam){ beta.G(tvar,gam) - 1/(1-deno[2])*(unit.l.func( tvar, gam)-1)*val[2]}
}
#1
0
I think this should succeed but you should actually provide real test cases for testing. At the moment we have no unit.l.func
or values of deno or val available for testing or any test case where you know what the answer should be.
我认为这应该会成功,但是您应该为测试提供真正的测试用例。目前我们没有单位。func或deno或val的值可用于测试或任何测试用例,您知道答案应该是什么。
Try returning a function that is defined at a point where those two values are available. The function should then carry them forward as part of its environment. That's called a "closure":
尝试返回在这两个值可用的点上定义的函数。然后,函数应该将它们作为环境的一部分进行转发。这就叫做“关闭”:
function1 <- function(tvar, gam)
{ function(tvar, gam){ beta.G(tvar,gam) - 1/(1-deno[1])*(unit.l.func( tvar, gam)-1)*val[1]}
}
function2 <- function(tvar, gam)
{ function(tvar, gam){ beta.G(tvar,gam) - 1/(1-deno[2])*(unit.l.func( tvar, gam)-1)*val[2]}
}