r - Scaling for estimating scale and shape parameter with fitdist function (fitdistrplus package) -
as specified in title have scaling problem fitdist
function in r (fitdistrplus
package).
please have @ following code:
# initialize arrays storing result fit_store_scale <- rep(na, 3) fit_store_shape <- rep(na, 3) # load data data1 <- c(7.616593e-05, 5.313253e-05, 1.604328e-04, 6.482365e-05, 4.217499e-05, 6.759114e-05, 3.531301e-05, 1.934228e-05, 6.263665e-05, 8.796205e-06) data2 <- c(7.616593e-06, 5.313253e-06, 1.604328e-05, 6.482365e-06, 4.217499e-06, 6.759114e-06, 3.531301e-06, 1.934228e-06, 6.263665e-06, 8.796205e-07) data3 <- c(7.616593e-07, 5.313253e-07, 1.604328e-06, 6.482365e-07, 4.217499e-07, 6.759114e-07, 3.531301e-07, 1.934228e-07, 6.263665e-07, 8.796205e-08) # form data frame data <- data.frame(data1, data2, data3) # set scaling factor scaling <- 1 #works without warnings , errors at: #10000 (data1), 100000 (data2) or #1000000 (data3) # store scale , shape parameter of data1, data2 , data3 in array for(i in 1:3) { fit.w1 <- fitdist(data[[i]]*scaling,"weibull", method = "mle") fit_store_scale[i] <- fit.w1$estimate[[2]]*1/scaling #1/scaling needed correcting scale parameter fit_store_shape[i] <- fit.w1$estimate[[1]] }
i have 3 vectors of data, stored in data frame. want use fitdist
function estimating scale , shape parameter separately each column of data (data1
, data2
, data3
) , storing them in fit_store_scale
, fit_store_shape
respectively.
the problem here fitdist
function doesn't work without appropriate scaling factor , data1
, data2
, data3
need different factors. looking solution determine optimal scaling factor automatically each column of data , getting fitdist
function work in end.
if you're not absolutely wedded fitdist
, use little bit more robust -- following fits weibull parameters on log scale, , uses nelder-mead rather gradient-based approach. doesn't seem have problems fitting these data.
dd <- data.frame(data1,data2,data3) library("bbmle") fx <- function(x) { m1 <- mle2(y~dweibull(shape=exp(logshape),scale=exp(logscale)), data=data.frame(y=x),start=list(logshape=0,logscale=0), method="nelder-mead") exp(coef(m1)) } t(sapply(dd,fx)) ## not quite output format asked for, ## easy enough convert. ## logshape logscale ## data1 1.565941 6.589057e-05 ## data2 1.565941 6.589054e-06 ## data3 1.565941 6.589055e-07
this approach should work reasonably distribution have standard distribution (d*()
) function.
Comments
Post a Comment