Hello all,
I have a question concerning integration of a function of a multivariate
argument with respect to one or more variables in r. 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.
However, it seems to be quite difficult to do the same if the function is
defined with a vector argument x. At the very least, I haven't seen any good
examples of this being done.
Any suggestions?
Yours sincerely,
Michael
Michael Levine
Associate Professor, Statistics
Department of Statistics
Purdue University
250 North University Street
West Lafayette, IN 47907 USA
email: mlevins at purdue.edu
Phone: +1-765-496-7571
Fax: +1-765-494-0558
URL: www.stat.purdue.edu/~mlevins
[[alternative HTML version deleted]]
How do you integrate perpendicular to a plane if you don't have a concept of 3d? In that direction the plane is just a point. Vectors in R do not have any concept of "n by 1"... they are like a string of beads. When you take a column out of a matrix `a` using indexing b <- a[,j] the resulting vector can only be indexed along its length as b[i]. Such a vector is indistinguishable in structure from a[i,] which originated from a row. So I would say you are tilting at windmills. If you want you can write a[, j, drop=FALSE] to end up with an n by 1 matrix... but that is not a vector, it is a matrix. I have reservations about the value of "integrating out" along a row in an n by 1 matrix... seems like a degenerate case and you would be better off not creating the n by 1 matrix in the first place. But then I left Matlab behind a long time ago. On June 11, 2024 11:44:08 AM PDT, "Levine, Michael" <mlevins at purdue.edu> wrote:>Hello all, > >I have a question concerning integration of a function of a multivariate argument with respect to one or more variables in r. 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. >However, it seems to be quite difficult to do the same if the function is defined with a vector argument x. At the very least, I haven't seen any good examples of this being done. >Any suggestions? > >Yours sincerely, >Michael > >Michael Levine >Associate Professor, Statistics > >Department of Statistics >Purdue University >250 North University Street >West Lafayette, IN 47907 USA > >email: mlevins at purdue.edu >Phone: +1-765-496-7571 >Fax: +1-765-494-0558 >URL: www.stat.purdue.edu/~mlevins > > [[alternative HTML version deleted]] > >______________________________________________ >R-help at 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.-- Sent from my phone. Please excuse my brevity.
? Tue, 11 Jun 2024 18:44:08 +0000 "Levine, Michael" <mlevins at 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