1
$\begingroup$

I would like to create a gamma probability distribution in R with a median of 3 and interquartile range of [2,5]. I am familiar with the methods of moments to estimate the scale and shape of a gamma distribution but this is based on the mean and standard deviation and both are not available. Is there a way to estimate a gamma distribution in R with a specific median and interquartile range?

$\endgroup$
4
  • $\begingroup$ The mean & sd formulas you refer to are for population quantities; if they were being estimated by sample quantities, unless the sample sizes were pretty large you should take the impact of this into account in constructing the estimators; you will have the same sort of issue with median and IQR. $\endgroup$
    – Glen_b
    Commented Nov 20, 2022 at 23:30
  • $\begingroup$ I expect that isn't the case for your question, but it comes up a lot and I figure it's worth at least mentioning the potential issue for other readers. $\endgroup$
    – Glen_b
    Commented Nov 20, 2022 at 23:43
  • $\begingroup$ The interquartile range is a single number (Q3-Q1). When you say "an interquartile range of [2,5]" do you instead mean "lower and upper quartiles of 2 and 5 respectively"? [alternatively, if you meant 2-and-a-half, what are the square brackets for?] $\endgroup$
    – Glen_b
    Commented Nov 21, 2022 at 0:05
  • $\begingroup$ I overlooked the word 'fit'. If this is about fitting then you might be better of using all of the data instead of just two statistics that describe the data. The use of statistics to summarize data for a fit is only done when these statistics work as a sufficient statistic or as an efficient estimator. I doubt that this is the case for a gamma distribution. $\endgroup$ Commented Nov 21, 2022 at 6:40

1 Answer 1

1
$\begingroup$

A gamma distribution with given median and IQR

It is not easy because there is no closed form expression for the median and quartiles. For the median you can find several approximate formula's. For the quartiles you could use R's approximation of the quantile function, qgamma, and then use an iterative algorithm that finds $k$ and $\theta$ that suit your target.

One way to make such algorithm can be by optimizing a cost function

f = function(par, targetmedian, targetIQR) {
  k = par[1]
  theta = par[2]

  median = theta*k*(1-1/9/k)^3
  ### alternatively compute the median like below
  #median = qgamma(0.5,shape = k,scale = theta)

  IQR = qgamma(0.75,shape = k,scale = theta) - qgamma(0.25,shape = k,scale = theta)

  error = (median-targetmedian)^2+(IQR-targetIQR)^2
  return(error)
}

### optim function below gives result k = 2.905083 theta = 1.160847
p2 = optim(par  = c(1,1), f, targetmedian = 3, targetIQR = 2.5)$par

### check median and IQR
qgamma(0.5, shape = p2[1], scale = p2[2]) ### 2.994302
qgamma(0.75,shape = p2[1], scale = p2[2])-qgamma(0.25,shape = p1[1],scale = p1[2]) # 2.494646

A gamma distribution with given quartiles

In this case you do not have two, but three values, whereas the gamma distribution only has two parameters. It might be possible that an exact fit is not possible. Now the cost function for the optimization becomes more important.

  • You could use the Kolmogorov statistic (the distance between the cdf and your quartiles that define a empirical distribution)

  • You can consider data bins $x\leq 2$, $2<x\leq 3$, $3 < x \leq 5$ and $5<x$ with each 25% probability and optimize the cross-entropy.

example code:

f = function(par, target_quartiles, fit_method = "Kolmogorov") {
  k = par[1]
  theta = par[2]

  if (fit_method %in% c("Kolmogorov", "cross-entropy") == FALSE) { 
    stop("use method Kolmogorov or cross-entropy")
  }

  p_quartiles = pgamma(target_quartiles, shape = k, scale = theta)
  
  if (fit_method == "Kolmogorov") {
     statistic = max(abs(p_quartiles - c(0.25,0.5,0.75)))
  }
  if (fit_method == "cross-entropy") {
     p1 = diff(c(0,p_quartiles,1))
     p2 = c(0.25,0.25,0.25,0.25)
     statistic = -sum(p2*log(p1))
  }     
  return(statistic)
}   

### optim function below gives result k = 2.399316 theta = 1.502534
par1 = optim(par  = c(1,1), f, target_quartiles = c(2,3,5))$par

### 0.2724194 0.4775805 0.7724194
pgamma(c(2,3,5), shape = par1[1], scale = par1[2])

### optim function below gives result k = 2.408474 theta = 1.530167
par2 = optim(par  = c(1,1), f, target_quartiles = c(2,3,5), fit_method = "cross-entropy")$par

### 0.2625420 0.4645755 0.7610788
pgamma(c(2,3,5), shape = par2[1], scale = par2[2])
$\endgroup$
2
  • $\begingroup$ This optimization with optim is very easy to program. But, originally (before starting to write a code) I was thinking of 1 starting with some theta 2 Given a theta compute the needed k for the required median 3 compute the IQR with the given theta and k 4 given k and theta adjust the theta to make a better matching IQR 5 go back to 2 and repeat. $\endgroup$ Commented Nov 20, 2022 at 22:56
  • $\begingroup$ With the second interpretation of the question it becomes like fitting a empirical distribution. I imagine that there might be other similar questions with better answers. A special detail that might make this question different is that the empirical distribution has only very few points. $\endgroup$ Commented Nov 21, 2022 at 7:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.