How to pass vector to integrate function - r

I want to integrate a function fun_integrate that has a vector vec as an input parameter:
fun_integrate <- function(x, vec) {
y <- sum(x > vec)
dnorm(x) + y
}
#Works like a charm
fun_integrate(0, rnorm(100))
integrate(fun_integrate, upper = 3, lower = -3, vec = rnorm(100))
300.9973 with absolute error < 9.3e-07
Warning message:
In x > vec :
longer object length is not a multiple of shorter object length
As far as I can see, the problem is the following: integrate calls fun_integrate for a vector of x that it computes based on upper and lower. This vectorized call seems not to work with another vector being passed as an additional argument. What I want is that integrate calls fun_integrate for each x that it computes internally and compares that single x to the vector vec and I'm pretty sure my above code doesn't do that.
I know that I could implement an integration routine myself, i.e. compute nodes between lower and upper and evaluate the function on each node separately. But that wouldn't be my preferred solution.
Also note that I checked Vectorize, but this seems to apply to a different problem, namely that the function doesn't accept a vector for x. My problem is that I want an additional vector as an argument.

integrate(Vectorize(fun_integrate,vectorize.args='x'), upper = 3, lower = -3, vec = rnorm(100),subdivisions=10000)
304.2768 with absolute error < 0.013
#testing with an easier function
test<-function(x,y) {
sum(x-y)
}
test(1,c(0,0))
[1] 2
test(1:5,c(0,0))
[1] 15
Warning message:
In x - y :
longer object length is not a multiple of shorter object length
Vectorize(test,vectorize.args='x')(1:5,c(0,0))
[1] 2 4 6 8 10
#with y=c(0,0) this is f(x)=2x and the integral easy to solve
integrate(Vectorize(test,vectorize.args='x'),1,2,y=c(0,0))
3 with absolute error < 3.3e-14 #which is correct

Roland's answer looks good. Just wanted to point out that it's sum , not integrate that is throwing the warning message.
Rgames> xf <- 1:10
Rgames> vf <- 4:20
Rgames> sum(xf>vf)
[1] 0
Warning message:
In xf > vf :
longer object length is not a multiple of shorter object length
The fact that the answer you got is not the correct value is what suggests that integrate is not sending the x-vector you expected to your function.

Related

Integrate function in R

From the code:
integrand <- function(x) {1/((x+1)*sqrt(x))}
a <- integrate(integrand, lower = 0, upper = Inf)
Then, it provides the result: 3.141593 with absolute error < 2.7e-05
How to keep only the value 3.141593?, because I need to calculate a+3 using R.
Thanks
Although it prints as a character, you can tell by
str(a)
that a is in fact a list with value as its first element. So you can get that value easily:
# by name
a$value
a[['value']]
# or index
a[[1]]
If you are interested in why it prints as a character: str(a) also tells you that a has a class attribute set to integrate. It turns out that print has a method for this class: you can find print.integrate in methods(print). This method determines the printing behavior. Normal printing can be forced by print.default(a).

an error in integrating a function in R

The following code chunk is for defining and integrating a function f1 involving matrix exponentials.
library(expm)
Lambdahat=rbind(c(-0.57,0.21,0.36,0,0),
c(0,-7.02,7.02,0,0),
c(1,0,-37.02,29,7.02),
c(0.03,0,0,-0.25,0.22),
c(0,0,0,0,0));
B=rbind(c(-1,1,0,0,0),c(0,0,0,0,0),c(0,0,0,0,0),c(0,0,0,0,0),c(0,0,0,0,0))
f1<-function(tau1)
{
A=(expm(Lambdahat*tau1)%*%B%*%expm(Lambdahat*(5-tau1)));
return(A[1,5]);
}
out=integrate(f1,lower=0,upper=5)#integration of f1
The integration in the above line gives the following error:
Error in integrate(f1, lower = 0, upper = 5) :
evaluation of function gave a result of wrong length
In addition: Warning messages:
1: In Lambdahat * tau1 :
longer object length is not a multiple of shorter object length
2: In Lambdahat * (t[i] - tau1) :
longer object length is not a multiple of shorter object length
To check for if the function outputs and inputs are of function f1 different length, 10 evenly spaced inputs and corresponding outputs of f1 are reported below. Input and output length for all the test cases were recorded as equal to 1.
sapply(X=seq(from=0,to=5,by=0.5),FUN=f1)
[1] 2.107718e-01 1.441219e-01 0.000000e+00 2.023337e+06 1.709569e+14
[6] 1.452972e+22 1.243012e+30 1.071096e+38 9.302178e+45 8.146598e+53
[11] 7.197606e+61
If anyone could share any hint or directions where the code may be going erroneous, it would be very helpful. Thanks very much!
The problem is that the function passed to integrate need to be vectorized, i.e. it should be able to receive a vector of input values and to return a vector of output values. I think f1 <- Vectorize(f1) could solve your problem.

Surprising warning in integrate (R), "longer object length not a multiple of shorter"

I reproduce a warning I get in integrate in R.
r <- list()
r$t <- 1:4
xsq <- function(x,r_obj){
r_obj$t == x
return(x^2)
}
integrate(f=xsq,0,1, r_obj = r)
The fourth line has no function in this toy example (that I constructed from a more complex function in which the line is needed). The problem is that the code above gives a warning related to this line:
0.3333333 with absolute error < 3.7e-15
Warning message:
In r_obj$t == x :
longer object length is not a multiple of shorter object length
Note that evaluating xsq at any value is not problematic, e.g. xsq(1, r_obj = r) does not give any warning. Also when r$t has less than 4 elements, e.g. r$t <- 1:3, the problem disappears. However, from ?integrate f is
An R function taking a numeric first argument and returning a numeric vector of the same length.
In my understanding xsq does just that. Where do I go wrong or why do I get a warning?

R summation within integration

I'm trying to figure out how to integrate the following function in R:
item.fill.rate <- function(x, lt, ib, S){
1-((((1/(factorial(S)))*((x*lt*ib)^S)))/
(sum(((1/(factorial(0:S)))*((x*lt*ib)^(0:S))))))}
Where x is a variable and lt, ib and S are input parameters
Based on a previous topic on here, I tried the following:
int.func <- function(lt, ib, S){
item.fill.rate <- function(x){
1-((((1/(factorial(S)))*((x*lt*ib)^S)))/(sum(((1/(factorial(0:S)))*((x*lt*ib)^(0:S))))))
}
return(item.fill.rate)
}
integrate(int.func(0.25, 1, 1), lower=0.25, upper=0.75)$value
When applying this, I get the following error:
> integrate(int.func(0.25, 1, 1), lower=0.25, upper=0.75)$value
[1] 0.4947184
Warning messages:
1: In (x * lt * ib)^(0:S) :
longer object length is not a multiple of shorter object length
2: In (1/(factorial(0:S))) * ((x * lt * ib)^(0:S)) :
longer object length is not a multiple of shorter object length
I evaluated the length of those objects, but that did not give me any indication where the error must be.
I tried to be as specific as possible, so hopefully someone is able to help me out with this!
The sum function is notorious for returning single items when a longer vector was expected, so integrand functions that have a call to sum generally need to be "vectorized" so they deliver the expected results (a vector of the the same length as a provided "x"-vector) for integrate to succeed. The Vectorize function is a wrapper for sapply and is quite handy for this process. You can set the parameters in the call to integrate. (At the moment I think you may be integrating a constant over a domain of length 1/2.)
item.fill.rate <- function(x,lt, ib, S){
1-((((1/(factorial(S)))*((x*lt*ib)^S)))/(sum(((1/(factorial(0:S)))*((x*lt*ib)^(0:S))))))
}
vint <- Vectorize(item.fill.rate)
integrate(vint, S=1, lt=0.25, ib= 1, lower=0.25, upper=0.75)$value
#[1] 0.4449025

Why are arguments to replacement functions not evaluated lazily?

Consider the following simple function:
f <- function(x, value){print(x);print(substitute(value))}
Argument x will eventually be evaluated by print, but value never will. So we can get results like this:
> f(a, a)
Error in print(x) : object 'a' not found
> f(3, a)
[1] 3
a
> f(1+1, 1+1)
[1] 2
1 + 1
> f(1+1, 1+"one")
[1] 2
1 + "one"
Everything as expected.
Now consider the same function body in a replacement function:
'g<-' <- function(x, value){print(x);print(substitute(value))}
(the single quotes should be fancy quotes)
Let's try it:
> x <- 3
> g(x) <- 4
[1] 3
[1] 4
Nothing unusual so far...
> g(x) <- a
Error: object 'a' not found
This is unexpected. Name a should be printed as a language object.
> g(x) <- 1+1
[1] 4
1 + 1
This is ok, as x's former value is 4. Notice the expression passed unevaluated.
The final test:
> g(x) <- 1+"one"
Error in 1 + "one" : non-numeric argument to binary operator
Wait a minute... Why did it try to evaluate this expression?
Well the question is: bug or feature? What is going on here? I hope some guru users will shed some light about promises and lazy evaluation on R. Or we may just conclude it's a bug.
We can reduce the problem to a slightly simpler example:
g <- function(x, value)
'g<-' <- function(x, value) x
x <- 3
# Works
g(x, a)
`g<-`(x, a)
# Fails
g(x) <- a
This suggests that R is doing something special when evaluating a replacement function: I suspect it evaluates all arguments. I'm not sure why, but the comments in the C code (https://github.com/wch/r-source/blob/trunk/src/main/eval.c#L1656 and https://github.com/wch/r-source/blob/trunk/src/main/eval.c#L1181) suggest it may be to make sure other intermediate variables are not accidentally modified.
Luke Tierney has a long comment about the drawbacks of the current approach, and illustrates some of the more complicated ways replacement functions can be used:
There are two issues with the approach here:
A complex assignment within a complex assignment, like
f(x, y[] <- 1) <- 3, can cause the value temporary
variable for the outer assignment to be overwritten and
then removed by the inner one. This could be addressed by
using multiple temporaries or using a promise for this
variable as is done for the RHS. Printing of the
replacement function call in error messages might then need
to be adjusted.
With assignments of the form f(g(x, z), y) <- w the value
of z will be computed twice, once for a call to g(x, z)
and once for the call to the replacement function g<-. It
might be possible to address this by using promises.
Using more temporaries would not work as it would mess up
replacement functions that use substitute and/or
nonstandard evaluation (and there are packages that do
that -- igraph is one).
I think the key may be found in this comment beginning at line 1682 of "eval.c" (and immediately followed by the evaluation of the assignment operation's RHS):
/* It's important that the rhs get evaluated first because
assignment is right associative i.e. a <- b <- c is parsed as
a <- (b <- c). */
PROTECT(saverhs = rhs = eval(CADR(args), rho));
We expect that if we do g(x) <- a <- b <- 4 + 5, both a and b will be assigned the value 9; this is in fact what happens.
Apparently, the way that R ensures this consistent behavior is to always evaluate the RHS of an assignment first, before carrying out the rest of the assignment. If that evaluation fails (as when you try something like g(x) <- 1 + "a"), an error is thrown and no assignment takes place.
I'm going to go out on a limb here, so please, folks with more knowledge feel free to comment/edit.
Note that when you run
'g<-' <- function(x, value){print(x);print(substitute(value))}
x <- 1
g(x) <- 5
a side effect is that 5 is assigned to x. Hence, both must be evaluated. But if you then run
'g<-'(x,10)
both the values of x and 10 are printed, but the value of x remains the same.
Speculation:
So the parser is distinguishing between whether you call g<- in the course of making an actual assignment, and when you simply call g<- directly.

Resources