This is an optimisation problem to match a distribution to 3 parameters: 2 quantiles and the mode.
In fact, even the function you used earlier returns estimates from an optimisation. If you calculate the quantile values from those $\alpha, \beta$ parameters it gave you, you'll see they don't match up exactly:
> qbeta(p=c(0.025, 0.975), shape1=2.44, shape2=38.21)
[1] 0.01004775 0.14983899
We'll define an objective function that calculates the squared error between the known and optimised quantile values and the mode, for a given set of parameters $\alpha,\beta$ of the distribution (encoded in the params
vector):
objective.function <- function(params) {
alpha <- params[1]
beta <- params[2]
intended.quantiles <- c(0.01, 0.15)
calculated.quantiles <- qbeta(p=c(0.025, 0.975), shape1=alpha, shape2=beta)
squared.error.quantiles <- sum((intended.quantiles - calculated.quantiles)^2)
intended.mode <- 0.05
calculated.mode <- calculate.mode(alpha, beta)
squared.error.mode <- (intended.mode - calculated.mode)^2
return(squared.error.quantiles + squared.error.mode)
}
calculate.mode <- function(alpha, beta) {
return((alpha-1) / (alpha+beta-2))
}
You already have some good starting values for $\alpha, \beta$, so let's get those ready:
starting.params <- c(2.44, 38.21)
Incidentally, this is what the PDF of a Beta distribution parameterised with these starting estimates looks like. Red lines are actual quantiles & mode, and blue lines are what you are trying to match. As you suggest, the quantiles look fine but the mode is off:

Now we use the nlm
optimisation function to estimate optimal values $\alpha^*, \beta^*$, starting for those initial values. The algorithm converges:
nlm.result <- nlm(f = objective.function, p = starting.params)
optimal.alpha <- nlm.result$estimate[1]
optimal.beta <- nlm.result$estimate[2]
So the optimised estimates are:
$\alpha^* = 3.174725$
$\beta^* = 44.94454$
And the quantiles and mode corresponding to these optimal parameters are:
> qbeta(p=c(0.025, 0.975), shape1=optimal.alpha, shape2=optimal.beta)
[1] 0.01499578 0.15042877
> calculate.mode(optimal.alpha, optimal.beta)
[1] 0.04715437
Recreating the plot of the Beta PDF, this time with the new parameters, we can see that the mode is much better matched, at the expense of the lower $2.5\%$ quantile:

Possible enhancements:
- This is a quick implementation that may not deal well with extreme values and numerical issues. Have a look at this answer for better code in the case of 2 quantiles only.
- Investigate constrained optimisation packages, where the problem would be formulated as estimating 2 parameters ($\alpha, \beta$) subject to the constraint $\frac{\alpha - 1}{\alpha + \beta - 2} = 0.05$