Dear Ivan, Thank you very much for your suggestions. I understand what you are saying. I am afraid I didn't explain well enough what was needed, however. Let us again restrict ourselves to the case of two dimensions. Then, what I have is a bivariate density function f(x,y) and my intention is to obtain f(x) by integrating out y. However, I don't only need to know the value of the marginal density f(x) at one point - I need to integrate it again to obtain the marginal cumulative density function F(x) as a next step. Thus, I also need to pass x as an additional parameter before integration, and this is what makes the problem more difficult.
I tried to do the following (here H and j are additional arguments needed to obtain the estimated bivariate density function): f.int1 <- function(x,y) {Vectorize (function(y) f(c(x,y),H=H,j=j))} mrg.d1 <- function(x){ integrate(f.int1,lower=-3,upper=3)$value } mrg.cdf1 <- function(x){ integrate(mrg.d1,lower=-Inf,upper=x)$value } I received the following error message: Error in integrate(f.int1, lower = -3, upper = 3) : evaluation of function gave a result of wrong length How would you go about performing this task? Yours sincerely, Michael Michael Levine Associate Professor, Statistics Department of Statistics Purdue University 250 North University Street West Lafayette, IN 47907 USA email: mlev...@purdue.edu Phone: +1-765-496-7571 Fax: +1-765-494-0558 URL: www.stat.purdue.edu/~mlevins ________________________________ From: Ivan Krylov <ikry...@disroot.org> Sent: Wednesday, June 12, 2024 6:41 AM To: Levine, Michael <mlev...@purdue.edu> Cc: r-help@r-project.org <r-help@r-project.org> Subject: Re: [R] Integration of functions with a vector argument [You don't often get email from ikry...@disroot.org. Learn why this is important at https://aka.ms/LearnAboutSenderIdentification ] ---- External Email: Use caution with attachments, links, or sharing data ---- � Tue, 11 Jun 2024 18:44:08 +0000 "Levine, Michael" <mlev...@purdue.edu> �����: > Let us say we have a function > > F <- function(x){ body of the function} > > Where x is, in general, a d by 1 vector with d>1. Now I want to > integrate out some of the coordinates of x, e.g. x[1] or x[2] or both > of them etc. I'm well aware of how to integrate out e.g. y if a > function is defined as f <- function (x,y) {body of the function} > where y is a scalar. The reason integrate() wants a separate function argument for the integration coordinate is so that it could give the function a vector of different values of the variable and receive a vector of the same length containing the corresponding values of the function. If the problem is small enough to make performance considerations irrelevant, you can use Vectorize to make a function compatible with integrate() from your function F: x <- x0 z <- z0 Fiy <- Vectorize(function(y) F(c(x, y, z))) integrate(Fiy, ymin, ymax) The resulting function Fiy will accept a vector of values for y and translate it into multiple calls to F with a three-element vector argument as it expects. Achieving better performance will require rewriting the function F to be "vectorised", i.e. to accept vectors for arguments and return a vector of the same length. -- Best regards, Ivan [[alternative HTML version deleted]]
______________________________________________ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.