Timothy Alston
Timothy Alston

Reputation: 1549

How can I find a distribution for a set of data, and then further propagate this distribution?

My problem is that I have a set of data which I want to fit a distribution to, and then once I have found the distribution, run monte carlo simulation on it to propagate the found distribution.

My first bit of code is:

require(fitdistrplus)
example1<-c(29,23,29,25,26,29,29,27,25,25,25,26,28,25,29,28,28,26,28,25,29,26,30) 
f1<-fitdist(example1,rgamma,method="mle")

If I then use the command

print(f1)

it tells me that the shape is 204.00 and the rate is 7.568 for the gamma distribution

(please note the numbers I am fitting the distribution to are arbitrary at the moment, I would normally have hundreds of observations to fit the distribution to).

Where I now need help is when I use the code from package mc2d to propagate this distribution as follows:

require(mc2d)
ndunc(1000)
fitted<-mcstoc(rgamma, type="U", shape=204.00, rate=7.569)

Currently I am having to manually type in the shape and rate into this above function from the previous "print" of the "fitdist" command.

My question is, is there a way to get the mcstoc command to automatically pick up the shape and rate from the fitdist command so that I do not have to interrupt the code to do so manually? Or if it is not possible with the fitdistrplus package and mc2d package, then is there another package out there which might do this for me?

Many thanks in advance!

Upvotes: 2

Views: 1778

Answers (3)

Vincent Zoonekynd
Vincent Zoonekynd

Reputation: 32351

If you do not want to type the name of the parameters, you can use do.call:

fitted <- do.call( 
  function(...) mcstoc(rgamma, type="U", ...), 
  as.list(f1$estimate) 
)

Upvotes: 1

Alan
Alan

Reputation: 3203

myFunction <- function (data){
    f1<-fitdist(data,rgamma,method="mle")
    fitted<-mcstoc(rgamma, type="U", shape=f1$estimate[1], rate=f1$estimate[2])
    return(fitted)
}

example1<-c(29,23,29,25,26,29,29,27,25,25,25,26,28,25,29,28,28,26,28,25,29,26,30)

fitted.example1 <- myFunction(exemple1)

This function isn't tested.

Upvotes: 2

Roland
Roland

Reputation: 132706

f1$estimate[1]
#   shape 
#204.0008 

f1$estimate[2]
#    rate 
#7.567762

fitted<-mcstoc(rgamma, type="U", shape=f1$estimate[1], rate=f1$estimate[2])

Upvotes: 4

Related Questions