简体   繁体   中英

Parameters for Exponential function with maximum likelihood in R

I got a sample data and i'm trying to obtain the parameters for two-parameter exponential function calculed based on maximum likelihood.

My sample:

sample = c(136.5,150,94.1,127.6,77.2,136.1,83.4,75.6,92.7,106.5,95.9,112.1,80.7,90.4,143.7,152.7,113.3,143.9,87.9,85.2,117.2,193,153.7,84.7,97.3,140.3,80,103.6,72.6,90.7,52.6,52.8)

My main goal is to use the cdf or quantile of exponential for maximum likelihood, just like that:

Example with GEV:

library(nsRFA)

parameters <- ML_estimation(sample, dist = "GEV")
p = c(0.1,0.066667,0.05,0.04,0.033333,0.02,0.01,0.005,0.002,0.001,0.0002,0.0001)
q = invF.GEV(1-p, parameters[1], parameters[2], parameters[3]); q
> 149.4 158.8 165.2 170 173.9 184.3 197.6 210 225.4 236.2 258.9 267.7

The two-parameter exponential function is an exponential function with a lower endpoint at xi . Finding MLEs of distributions with such sharp boundary points is a bit of a special case: the MLE for the boundary is equal to the minimum value observed in the data set (see eg this CrossValidated question ). That makes the MLE of the two-parameter exponential equivalent to the MLE of the exponential distribution for x-xmin .

So the MLE of xi is

print(xi <- min(sample))
  1. With MASS::fitdistr :
(m0 <- MASS::fitdistr(sample-xi, "exponential"))
      rate    
  0.018382353 
 (0.003249572)
  1. with bbmle :
m1 <- bbmle::mle2(y-xi~dexp(lambda), 
      data=data.frame(y=sample), start=list(lambda=1),
      method="Brent", lower=0.001, upper=100)
broom::tidy(m1, conf.int=TRUE, conf.method="profile")
  term   estimate std.error statistic      p.value conf.low conf.high
  <chr>     <dbl>     <dbl>     <dbl>        <dbl>    <dbl>     <dbl>
1 lambda   0.0184   0.00325      5.66 0.0000000154   0.0127    0.0255
  1. Analytical solution:
1/mean(sample-xi)
## [1] 0.01838235

If you want a simple function that provides the shift and scale parameters (as apparently provided by your alternative software):

est_twoexp <- function(x) {
   xi <- min(x)
   c(xi = xi, scale = mean(sample-xi))
}
est_twoexp(sample)
##    xi scale 
##  52.6  54.4 
est_twoexp <- function(x) {
   xi <- min(x)
   c(xi = xi, scale = mean(sample-xi))
}
est_twoexp(sample)
##    xi scale 
##  52.6  54.4 

Plotting:

library(nsRFA)
ee <- ecdf(sample)
inv_ecdf <- approxfun(seq(0, 1, length=length(sample)),
                      sort(sample),
                      method="constant")
## homemade quantile function
q2exp <- function(p, xi, scale) {
  xi + qexp(p, rate=1/scale)
}
curve(inv_ecdf(x), from=0, to=1, type="s", ylim=c(40,250))
with(as.list(est_twoexp(sample)),
     curve( invF.exp (x, xi, scale), col=2, lwd=2, add=TRUE))
with(as.list(est_twoexp(sample)),
     curve( q2exp(x, xi, scale), col=4, lwd=2, lty= 2, add=TRUE))

glm with family=Gamma doesn't work because it doesn't allow zero values (within the general family of Gamma distributions, x==0 only has a positive, finite density for the exponential distribution)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM