[英]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.我想构造一个由 f 对 x 的积分定义的函数 g(y,z,w)。 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)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.