简体   繁体   中英

How to integrate only a variable in multivariate function in R?

I have the next function:

f <- function(x,y,z,w) {dpois(x, lambda = w*cos(y-z) ) }

I want to construct a function g(y,z,w) defined by the integral of f with respect to x. I can do it when I have two variables, the code is for example, something like this

margin <- function(y) { sapply(y, function(y) { integrate(function(x) f(x,y), llimx, ulimx)$value }) }

But I don't know how to do that for more than two variables.

Maybe you can try the code below

f <- Vectorize(function(x,y,z,w) dpois(x, w*cos(y-z)),"x") 
g <- function(y,z,w) integrate(f, lower = 0, upper =Inf, y, z, w)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM