将对象传递给R中的integrate()评估的函数

时间:2014-06-05 22:40:43

标签: r statistics integration

我有以下代码......

integrand <- function(v){
  (v^(beta1-1))*exp(-((lambda1*v)^beta1)-(lambda0*(XX-v))^beta0)
  }

F_j <- function(XX) {
  (1-exp(-(lambda1*XX)^beta1))-beta1*(lambda1^beta1)*integrate(integrand,lower=0,upper=XX)$value
}

其中beta0 = beta1 = 1且lambda0 = lambda1 = 0.001。当我在R中运行时,我有以下错误..

#Error in f(x, ...) : object 'XX' not found

1 个答案:

答案 0 :(得分:1)

您需要将对象传递给integrand。这是一种方法:

integrand <- function(v, XX, beta0, beta1, lambda0, lambda1) {
  (v^(beta1-1)) * exp(-((lambda1*v)^beta1) - (lambda0*(XX-v))^beta0)
}

F_j <- function(XX) {
  (1-exp(-(lambda1*XX)^beta1)) - beta1*(lambda1^beta1) *
    integrate(integrand, lower=0, upper=XX, 
              XX=XX, beta0=beta0, beta1=beta1, 
              lambda0=lambda0, lambda1=lambda1)$value
}

beta0 <- beta1 <- lambda0 <- lambda1 <- 1
F_j(4)

## [1] 0.9084218