# [R] Generating Random Numbers after Momentfitting

From: Jole, M. van <M.v.Jole_at_student.tue.nl>
Date: Mon 19 Jun 2006 - 17:59:08 EST

L.S.

After moment fitting, which I programmed as follows: #Function which, given the first two moment, computates the density, #according to momentfitting
momentfit<-function(ES,c){

res<-(1:5)
if(c<=1){

```        k<-floor(1/c^2)
p<-((1)/(1+c^2))*((k+1)*c^(2)-sqrt((k+1)*(1+c^(2))-(k+1)^(2)*c^(2)))
mu<-(k+1-p)/ES
res<-0
res<-k
res<-p
res<-mu
res<-0

```

}

if(c>1){

```        p1<-((1)/(2))*(1+sqrt((c^2-1)/(c^2+1)))
p2<-1-p1
mu1<-2*p1/ES
mu2<-2*p2/ES
res<-1
res<-p1
res<-p2
res<-mu1
res<-mu2
```

}
return(res)
}

I have the either one of the following distributionfunctions:

ES<-a
c<-b
para<-momenfit(a,c)
if(para==0){

fun1<-function(x){(para*dgamma(x,para,para)+(1-para)*dgamma(x,para+1,para))}
}

if(para==1){

fun2<-function(x){(para*dgamma(x,1,para)+para*dgamma(x,1,para))}
}

I now want to generate a random number according to the above distrubutions. Can somebody help me? Thanks.

Martin

R-help@stat.math.ethz.ch mailing list