John Majimboni
John Majimboni

Reputation: 133

Solving an integral using uniroot

Suppose I have the following data

n<-100
ki <- runif(n)
yi<-rbinom(n,1,0.5)
u<-runif(n)

I would like to solve the equation

This is the same as solving

#Attempt: Not sure about this
library(rootSolve)
library(pracma)
Intz<-function(s){exp( ki* log( s))}
fz <- function(z){
       exp(yi)  * quadinf( Intz , xa=0, xb= z)$Q 
            +  log(u)
    } 
z <- uniroot(fz, interval=c(0, 1e3) )$root
z 

I get the following error

Error in uniroot(h, interval = c(1, 1000), tol = .Machine$double.eps^0.5) : 
  f() values at end points not of opposite sign

I get the same error message when I try to solve

I should end up with n values of z.

Upvotes: 0

Views: 139

Answers (1)

jblood94
jblood94

Reputation: 17011

The integral of exp(ki*log(s)) with respect to s from 0 to z is just z^(ki + 1)/(ki + 1), so you can solve for z, no need for numeric root finding or integrals.

z <- (-(ki + 1)*log(u)*exp(-yi))^(1/(ki + 1))

Upvotes: 1

Related Questions