I wish to write a function in R that fits a quantile function to two given tertiles, t1, t2
. If I know what the quantile function is, qgamma
, say, then I can write:
gapfn <- function(par, t1, t2) {
a <- t1 - qgamma(1 / 3, shape = par[1], rate = par[2])
b <- t2 - qgamma(2 / 3, shape = par[1], rate = par[2])
return(a ^ 2 + b ^ 2)
}
result <- optim(par = c(10,1), gapfn, t1, t2)
and then result
gives me what I want.
But what I really want to do is to write a function that looks like this:
tertile_fit <- function(t1, t2, qfun, par, par_start)
where qfun
can be any quantile function defined by parameters par
, par_start
are starting values for the optimisation, and where the output of tertile_fit
is a list as produced by optim
.
The reason I want such a function is that I wish to try fitting a number of different quantile functions and want to avoid writing multiple slightly varying pieces of code.
I have tried to write such a function using
gapfn <- function(par, t1, t2, qfun) {
a <- t1 - qfun(1 / 3, par)
b <- t2 - qfun(2 / 3, par)
return(a ^ 2 + b ^ 2)
}
to measure the closeness of fit, and then use
result <- optim(par = par_start,
gapfn,
T1 = T1,
T2 = T2,
qfun = qfun)
But that does not work:
tertile_fit <- function(t1, t2, qfun, par_start) {
gapfn <- function(par, t1, t2, qfun) {
a <- t1 - qfun(1 / 3, par)
b <- t2 - qfun(2 / 3, par)
return(a ^ 2 + b ^ 2)
}
result <- optim(
par = par_start,
gapfn,
t1,
t2,
qfun = qfun
)
return(result)
}
tertile_fit(t1 = 0.5, t2 = 2, qfun = qgamma, par_start = c(1, 10))
delivers:
Error in fn(par, ...) : argument "t2" is missing, with no default
What should I do?