Skip to content

Commit a1a2ba2

Browse files
committed
Simplify bounds customization
1 parent f60cd63 commit a1a2ba2

File tree

3 files changed

+21
-21
lines changed

3 files changed

+21
-21
lines changed

R/drob.R

+11-8
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
#' The 4-parameter logistic model (4PL)
22
#'
3-
#' @description
43
#' `fpl` is a list containing three elements related to the 4-parameter logistic
54
#' model, as required by the `model` parameter of the `drob` function:
65
#' - `fun`: the 4PL-function itself. It takes as arguments a vector of values
@@ -33,12 +32,12 @@ fpl <- list(
3332
a / (1 + a)
3433
)
3534
},
36-
init = function(x, y, extend = 15, eps = 1e-6) {
35+
init = function(x, y, extend = 15, eps1 = 1e-6, eps2 = 1e-12) {
3736
ux <- unique(x)
3837
uy <- tapply(y, x, mean)
3938
r <- range(uy)
4039
p <- (uy - r[2]) / (r[1] - r[2])
41-
p <- (1 - 2 * eps) * p[ux != 0] + eps
40+
p <- (1 - 2 * eps1) * p[ux != 0] + eps1
4241
b <- coef(lm(log(p / (1 - p)) ~ log(ux[ux != 0])))
4342
t0 <- list(t1 = r[1], t2 = -b[2], t3 = exp(-b[1] / b[2]), t4 = r[2])
4443
w <- 1 / as.vector(tapply(y, x, var)[as.factor(x)])
@@ -51,14 +50,14 @@ fpl <- list(
5150
se <- unname(b[i, 2])
5251
lower <- t - extend * se
5352
upper <- t + extend * se
54-
lower[c(2, 3)] <- 0
53+
lower[2] <- 0
54+
lower[3] <- eps2 * min(ux[ux != 0])
5555
list(t = t, se = se, lower = lower, upper = upper)
5656
}
5757
)
5858

5959
#' Return bisquare and its derivatives
6060
#'
61-
#' @description
6261
#' This computes bisquare (aka Tukey's biweight) function for a given cutoff
6362
#' point. It also computes its first two derivatives. All three functions
6463
#' are returned as elements of a list with names `rho`, `psi` and `dpsi`,
@@ -92,7 +91,6 @@ bisquare <- function(k) {
9291

9392
#' Compute M-estimate of scale
9493
#'
95-
#' @description
9694
#' `m_scale` computes an M-estimate of scale for a given rho-function using
9795
#' a one-dimensional root finding routine.
9896
#'
@@ -252,6 +250,10 @@ m_scale <- function(r, rho, extend = 5) {
252250
#' be passed to `m_scale` in order to extend the root finding interval (for
253251
#' further details, refer to the documentation of `m_scale`). By default 5.
254252
#'
253+
#' @param bounds A function that takes the `init` list produced by the model
254+
#' and may update `init$lower` and/or `init$upper` based on domain-specific
255+
#' considerations. By default it returns `init` unchanged.
256+
#'
255257
#' @return A list containing the following elements:
256258
#' - `t`: a vector with the final location estimate, produced by step 3.
257259
#' - `t0`: a vector with the initial location estimate, produced by step 1.
@@ -296,7 +298,8 @@ drob <- function( # nolint
296298
de_args = list(),
297299
qn_args = list(),
298300
qn_gr = FALSE,
299-
ms_extend = 5
301+
ms_extend = 5,
302+
bounds = identity
300303
) {
301304
select <- function(arg, ...) if (is.character(arg)) switch(arg, ...) else arg
302305
mbi <- bisquare(mbi_k)
@@ -306,7 +309,7 @@ drob <- function( # nolint
306309
"fpl" = fpl,
307310
stop("Invalid model '", model, "'")
308311
)
309-
init <- model$init(x, y)
312+
init <- bounds(model$init(x, y))
310313
lower <- init$lower
311314
upper <- init$upper
312315

assets/drob.pdf

-41 Bytes
Binary file not shown.

man/drob.Rd

+10-13
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)