user3440244
user3440244

Reputation: 371

error of ploting pdf, cdf and quantile functions of truncated gamma distribution in R

After searching in the forum, I did not find similar questions. If I missed it, please let me know. I would really appreciate.

I need to plot pdf , cdf, and quantile function of a truncated gamma for any given shape and scale values in R . But, I got error for some values of shape and scale.

My code:

 library(distr) 
 library(distrEx) 
 library(truncdist)
 scale8 <- 750000
 shape8 <- 0.0016     
 G0 <- Gammad(scale = scale8, shape = shape8) 
 plot(G0)
 TG <- Truncate(G0, lower=0, upper=1000000) #  Error in if (.isEqual(gaps[jj, 2], gaps[j + 1,   
                                            #  1])) gaps[jj, 2] <- gaps[j +  : 
                                            #    missing value where TRUE/FALSE needed

 plot(TG) 

My understanding is that for some pdf of gamma, its trucnated gamma distribution do not exist ?

Any help would be appreciated!

Upvotes: 0

Views: 516

Answers (1)

jlhoward
jlhoward

Reputation: 59355

Like this??

scale8 <- 750000
shape8 <- 0.0016 
library(truncdist)
par(mfrow=c(1,3))
q <- seq(0,100,1)
p <- seq(0,1,.01)
plot(q,dtrunc(q,"gamma",a=20,b=50,scale=scale8,shape=shape8),type="l",main="PDF")
plot(q,ptrunc(q,"gamma",a=20,b=50,scale=scale8,shape=shape8),type="l",main="CDF")
plot(p,qtrunc(p,"gamma",a=20,b=50,scale=scale8,shape=shape8),type="l",main="Quantile")

Setting the lower and upper limits to [0,1000000] is hardly truncating at all, so I changed it for the sake of the example.

EDIT (Response to OP's comment)

The lower and upper limit of the truncated range is set with a=... and b=... in the call to dtrunc(...), ptrunc(...), or qtrunc(...). In this example it's set to [20,50].

Upvotes: 1

Related Questions