By setting the 3-dimensional Weibull distribution in R

I am doing some data analysis in R, and I'm trying to figure out how to fit my data to 3 Weibull parameters. I found how to do this with 2 Weibull parameters, but briefly found how to do this with 3 Weibull parameters.

Here's how I put the data using the fitdistr () function from the MASS package:

y <- fitdistr(x[[6]], 'weibull') 

x[[6]] is a subset of my data, and y is where I save the fitting result.

+8
r distribution model-fitting weibull
source share
1 answer

You can look at the FAdist package first . However, it is not so difficult to switch from rweibull3 to rweibull :

 > rweibull3 function (n, shape, scale = 1, thres = 0) thres + rweibull(n, shape, scale) <environment: namespace:FAdist> 

and similarly from dweibull3 to dweibull

 > dweibull3 function (x, shape, scale = 1, thres = 0, log = FALSE) dweibull(x - thres, shape, scale, log) <environment: namespace:FAdist> 

so we have this

 > x <- rweibull3(200, shape = 3, scale = 1, thres = 100) > fitdistr(x, function(x, shape, scale, thres) dweibull(x-thres, shape, scale), list(shape = 0.1, scale = 1, thres = 0)) shape scale thres 2.42498383 0.85074556 100.12372297 ( 0.26380861) ( 0.07235804) ( 0.06020083) 

Edit: As mentioned in the comment, various warnings appear when trying to install the distribution in this way

 Error in optim(x = c(60.7075705026659, 60.6300379017397, 60.7669410153573, : non-finite finite-difference value [3] There were 20 warnings (use warnings() to see them) Error in optim(x = c(60.7075705026659, 60.6300379017397, 60.7669410153573, : L-BFGS-B needs finite values of 'fn' In dweibull(x, shape, scale, log) : NaNs produced 

For me, at first there was only NaNs produced , and this is not the first time I see it, so I thought it was not so significant, because the ratings were good. After some searching, this seemed like a pretty popular problem, and I could not find a reason or solution. One alternative would be to use the stats4 and mle() packages, but it also had problems. But I can suggest you use a modified version of code by danielmedic, which I checked several times:

 thres <- 60 x <- rweibull(200, 3, 1) + thres EPS = sqrt(.Machine$double.eps) # "epsilon" for very small numbers llik.weibull <- function(shape, scale, thres, x) { sum(dweibull(x - thres, shape, scale, log=T)) } thetahat.weibull <- function(x) { if(any(x <= 0)) stop("x values must be positive") toptim <- function(theta) -llik.weibull(theta[1], theta[2], theta[3], x) mu = mean(log(x)) sigma2 = var(log(x)) shape.guess = 1.2 / sqrt(sigma2) scale.guess = exp(mu + (0.572 / shape.guess)) thres.guess = 1 res = nlminb(c(shape.guess, scale.guess, thres.guess), toptim, lower=EPS) c(shape=res$par[1], scale=res$par[2], thres=res$par[3]) } thetahat.weibull(x) shape scale thres 3.325556 1.021171 59.975470 
+8
source share

All Articles