runiroot

f() values at end points not of opposite sign


I am trying to get the inverse of cdf and use it for the QQ plot as follows.

cdf<-function(x,b){
  1 - (1+(b*x*(b*x+2)/(b^2+2)))*exp(-b*x)
}

qf<-function(p,b){
  uniroot(function(x) cdf(x,b) - p, lower=0, upper=1000)[1]
}

DATA1<-c(14, 13, 19, 22, 11,17)

dakash <- function(x,b) (b^3/(b^2+2))*(1+x^2)*exp(-b*x)
pakash <- function(q,b) (1 - (1+(b*q*((b*q)+2)/(b^{2}+2)))*exp(-b*q))
qakash <- function(p,b) qf(p,b)



fitgumbel <- fitdist(DATA1, "akash", start=list(b=5))

qqcomp(list(fitgumbel),legendtext=c("akash"))

But I get the following error.

Error in uniroot(function(x) cdf(x, b) - p, lower = 0, upper = 1, extendInt = "yes") : f() values at end points not of opposite sign

Please anyone can help me? Thank you in advance.

I need to plot Q-Q plot Empirical quantiles vs Theoretical quantiles.


Solution

  • You need to ensure that the parameter b is strictly positive. An easy way of doing that is the substitution b = exp(beta):

    cdf<-function(x,beta){
      1 - (1+(exp(beta)*x*(exp(beta)*x+2)/(exp(beta)^2+2)))*exp(-exp(beta)*x)
    }
    
    curve(cdf(x, log(5)), to = 1)
    
    qf<-function(p,beta){
      uniroot(function(x) cdf(x, beta) - p, lower=0, upper=1000)$root
    }
    
    abline(v = qf(.5, log(5)), lty = 2)
    abline(h = 0.5, lty = 2)
    #works correctly
    
    
    DATA1<-c(14, 13, 19, 22, 11,17)
    
    dakash <- function(x,beta) (exp(beta)^3/(exp(beta)^2+2))*(1+x^2)*exp(-exp(beta)*x)
    pakash <- function(q, beta) cdf(q, beta)
    qakash <- function(p,beta) Vectorize(qf)(p, beta)
    
    library(fitdistrplus)
    
    fitgumbel <- fitdist(DATA1, "akash", start=list(beta=log(5)))
    
    setNames(exp(fitgumbel$estimate), "b")
    #        b 
    #0.1853882 
    
    qqcomp(list(fitgumbel),legendtext=c("akash"))
    

    resulting qq-plot