假设我想整合功能
integrand <- function(x, a) exp(a*x)
其中x
是我的协变量,a
只是用户指定的数字。如何将其整合到 R 中?我知道你可以这样做:
integrand <- function(x) {1/((x+1)*sqrt(x))}
integrate(integrand, lower = 0, upper = Inf)
但我怎样才能提供其他参数?我想我可能需要某种expression
或eval
函数,但我不确定如何实现它。
答案 0 :(得分:1)
尝试:
integrand <- function(x, a) exp(a*x)
integrate(integrand, lower = 0, upper = Inf, a=-1)
查看?integrate
,您会看到
integrate(f, lower, upper, ..., subdivisions = 100L,
rel.tol = .Machine$double.eps^0.25, abs.tol = rel.tol,
stop.on.error = TRUE, keep.xy = FALSE, aux = NULL)
Arguments
f an R function taking a numeric first argument and returning a numeric vector of the same length. Returning a non-finite element will generate an error.
lower, upper the limits of integration. Can be infinite.
... additional arguments to be passed to f.
因此,你可以传递与 integrand 需要的参数一样多的第四个参数,并在集成之前将它们传递给函数。