apply Integrate function to a vector - r

I'm a newbie to R programming and I have an assignment to submit so my prob is the following:
I wrote the following function :
and I should be using integrate () function to calculate phi(x) for all x
of the vector quantiles and store them in a vector named probs.
I started the following and I got blocked
Rep <- function (x) {
vector <-integrate (phi, lower = Inf , upper = x)$Value }
Not sure if this is the right way to apply the function integrate() to a vector.
Your help is much appreciated

You could do the following:
phi <- function(x)exp(-x^2/2)/sqrt(2 * pi)
quantiles <- seq(from = 0, to = 5.5, by = 0.01)
fun<-Vectorize(function(x)integrate(phi,-Inf, x)[[1]])
fun(quantiles)

Related

Optimization of an integrated function in R

I would like to find the optimum value of a for the following integration which is a function of x
integrand <- function(x,a) {
D=a/((x+1)*sqrt(x+a))
D
}
I can do the integration for fixed value of a. For example, if a=5
> integrate(integrand,0,5,a=5)$value
[1] 3.490687
But I want to find the optimum value using the optim() function in R or any available built-in optimization function. I have tried the following code, unfortunately it is not working,
optim(5,integrate(integrand, lower = 0, upper = 5))
Any help is appreciated.
You can use the 'optimize' function, which is ideal for one-parameter optimization:
optimize(f = integrand, interval = c(0, 5), a = 5)

non-numeric argument to binary operator in integration in R

f1 = function(t){exp(-t^2)}
phi1 = function(theta){
0.5-1/sqrt(pi)*integrate(f1, lower = 0, upper = (10-theta)/(2*sqrt(2)))}
mu1 = (450*barx)/(454)
sigs1 = 36/454
pi_thetapost = function(theta){
(1/sqrt(2*pi*sigs1))*exp(-((theta-mu1)^2)/(2*sigs1))}
E_phipost = integrate(phi1*pi_thetapost, lower = -Inf, upper =Inf)
I was trying to do a integration, and I got the error says:
non-numeric argument to binary operator
I think that * is a binary operator, but I am not sure how to figure this out.
Thanks~
A few problems here. First barx isn't defined. But the main problem is that you can't just multiple functions in R. You can multiple the values returns by function, but not the functions themselves. You need to pass in a proper function to integrate.
But then after that problem, you need to make sure all the functions you pass in to integrate() are vectorized and your phi function is not. you need to be able to pass in a vector and get a vector out. The easiest way to fix this is with Vectorize(). And finally, integrate() returns an object, not just a number. So if you want to return the calculated value, you need to extract it from the object before you can multiply it by another object. Try
f1 <- function(t){
exp(-t^2)
}
phi1 <- function(theta) {
0.5-1/sqrt(pi)*integrate(f1, lower = 0, upper = (10-theta)/(2*sqrt(2)))$value
}
barx <- 2 # or whatever
mu1 <- (450*barx)/(454)
sigs1 <- 36/454
pi_thetapost <- function(theta){
(1/sqrt(2*pi*sigs1))*exp(-((theta-mu1)^2)/(2*sigs1))
}
myfun <- function(x) {
Vectorize(phi1)(x)*pi_thetapost(x)
}
E_phipost <- integrate(myfun, lower = -Inf, upper =Inf)
E_phipost
# 3.690467e-05 with absolute error < 5.6e-05
E_phipost$value
# [1] 3.690467e-05

writing a loop and the integrate function

I would like to calculate integral from two columns in my data frame
the integral is based on a function; 1/x, I quess I need to write a loop . Can you help with writing a loop and the integrate function?
This is the sample data frame;
upper_concentration<-c(1:200, 1)
lower_concentration<-upper_concentration*0.9
df = data.frame(upper_concentration,lower_concentration)
for (i in 1:(length(df))){
integral <- function (x) {1/df$upper_concentration}
result <- integrate(integral, lower = df$upper_concentration, upper =upper_concentration*0.9)
}
The built-in integrate function will perform the integral for the full range.
lower <- 1
upper <- 200
integral <- function (x) {1/x}
result <- integrate(integral, lower, upper)
Now, if you want the integral for each portion of the dataframe then a loop is necessary.
result<-vector(list, length=nrow(df)) #initialize an empty vector
integral <- function (x) {1/x}
for (i in 1:(nrow(df))){
result[[i]] <- integrate(integral, df$lower_concentration[i], df$upper_concentration[i])
}

Integrate a function in R through time

I would like to integrate the following function with respect to t, for lower bound = 0 and upper bound = t. I can do that with the following code, but my ultimate goal is to have a value of the integral for each t. Even if I make t a sequence instead of a value, or if I try and use sapply, I still cannot get a value for the integral at each step of t.
#initialize constants
kap=-0.1527778
alph0<-6
b<-0
po<-0.01
t<-100
gp_st<-integrate(function(t) (1-alph0/(alph0+b*t)*(1-po^kap))^(1/kap),lower=0,upper=t)$value
#try alternate where t is now a sequence
t<-seq(1:100)
gp_st2<-function(h) sapply(h,gp_st) #still gives length of 1
Thanks!
Try making gp_st a function of your upper bound, like so:
gp_st <- function(h) {
integrate(function(t) (1-alph0/(alph0+b*t)*(1-po^kap))^(1/kap),lower=0,upper=h)$value
}
Then you can use sapply much as you intended:
t<-seq(1:100)
gp_st2 <- sapply(t, gp_st)
and now gp_st2 is a numeric vector of length 100.
The problem is that you are evaluating the integral in gp_st, and you don't want to do that. You want the following:
ff = function(t) {
(1-alph0/(alph0+b*t)*(1-po^kap))^(1/kap)
}
sapply(1:100, function(ul) {
integrate(ff, lower = 0, upper = ul)$value
})
There are naturally more efficient ways to do this.

Compute multiple Integral and plot them (with R)

I'm having trouble to compute and then plot multiple integral. It would be great if you could help me.
So I have this function
> f = function(x, mu = 30, s = 12){dnorm(x, mu, s)}
which i want to integrate multiple time between z(1:100) to +Inf to plot that with x=z and y = auc :
> auc = Integrate(f, z, Inf)
R return :
Warning message:
In if (is.finite(lower)) { :
the condition has length > 1 and only the first element will be used
I have tested to do a loop :
while(z < 100){
z = 1
auc = integrate(f,z,Inf)
z = z+1}
Doesn't work either ... don't know what to do
(I'm new to R , so I'm already sorry if it is really easy .. )
Thanks for your help :) !
There is no need to do the integrating by hand. pnorm gives the integral from negative infinity to the input for the normal density. You can get the upper tail instead by modifying the lower.tail parameter
z <- 1:100
y <- pnorm(z, mean = 30, sd = 12, lower.tail = FALSE)
plot(z, y)
If you're looking to integrate more complex functions then using integrate will be necessary - but if you're just looking to find probabilities for distributions then there will most likely be a function built in that does the integration for you directly.
Your problem is actually somewhat subtle, and in a certain sense gets to the core of how R works, so here is a slightly longer explanation.
R is a "vectorized" language, which means that just about everything works on vectors. If I have 2 vectors A and B, then A+B is the element-by-element sum of A and B. Nearly all R functions work this way also. If X is a vector, then Y <- exp(X) is also a vector, where each element of Y is the exponential of the corresponding element of X.
The function integrate(...) is one of the few functions in R that is not vectorized. So when you write:
f <- function(x, mu = 30, s = 12){dnorm(x, mu, s)}
auc <- integrate(f, z, Inf)
the integrate(...) function does not know what to do with z when it is a vector. So it takes the first element and complains. Hence the warning message.
There is a special function in R, Vectorize(...) that turns scalar functions into vectorized functions. You would use it this way:
f <- function(x, mu = 30, s = 12){dnorm(x, mu, s)}
auc <- Vectorize(function(z) integrate(f,z,Inf)$value)
z <- 1:100
plot(z,auc(z), type="l") # plot lines

Resources