简体   繁体   中英

How can I use the results from fitdist to create a probability distribution in R?

I used fitdist from the fitdistrplus package to fit a (gamma) distribution to my data: fitg <- fitdist(mdt, "gamma")

The result is a list of parameters that describe the fit. I wonder if there is a way to use the result to create cumulative distribution functions and random sample generators from this distribution.

For example, if the distribution fitted with fitdist corresponded to a normal distribution with mean 0 and sd 1, how can I recreate easily pnorm(..,0,1) and rnorm(..,0,1)?

I understand I can do it manually, but it would be much easier for me to have a function doing it "automatically", as I have to do it for many different datasets that will be fitted with different kinds of distributions.

Thanks a lot for your help!

Do you want something like the following?

library(fitdistrplus)
data <- rnorm(1000, 0.01, 1.01)  # sampled from original distribution N(0.01, 1.01^2)

fit_and_draw_sample <- function(data, nsamples, distr='norm') {
  if (distr == 'norm') {
    fitg <- fitdist(data, distr)   
    params <- fitg$estimate
    print(params) # fitted distribution N(0.0398281, 0.9876068^2) with estimated params
    #      mean        sd 
    # 0.0398281 0.9876068 
    mu <- params[1]
    sigma <- params[2]
    return (rnorm(nsamples, mu, sigma))
  }
  # handle other distributions here
  return (NULL)
}

samples <- fit_and_draw_sample(data, 1000)
hist(data, col=scales::alpha('blue',.2), border=FALSE, main='samples from original and fitted distribution')
hist(samples, col=scales::alpha('red',.2), add=TRUE, border=FALSE)
legend('topright', c("original", "fitted"), col = c(rgb(0,0,1,0.2), rgb(1,0,0,0.2)), lwd=c(2,2))

在此处输入图像描述

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