1
1
# ' The 4-parameter logistic model (4PL)
2
2
# '
3
- # ' @description
4
3
# ' `fpl` is a list containing three elements related to the 4-parameter logistic
5
4
# ' model, as required by the `model` parameter of the `drob` function:
6
5
# ' - `fun`: the 4PL-function itself. It takes as arguments a vector of values
@@ -33,12 +32,12 @@ fpl <- list(
33
32
a / (1 + a )
34
33
)
35
34
},
36
- init = function (x , y , extend = 15 , eps = 1e-6 ) {
35
+ init = function (x , y , extend = 15 , eps1 = 1e-6 , eps2 = 1e-12 ) {
37
36
ux <- unique(x )
38
37
uy <- tapply(y , x , mean )
39
38
r <- range(uy )
40
39
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
42
41
b <- coef(lm(log(p / (1 - p )) ~ log(ux [ux != 0 ])))
43
42
t0 <- list (t1 = r [1 ], t2 = - b [2 ], t3 = exp(- b [1 ] / b [2 ]), t4 = r [2 ])
44
43
w <- 1 / as.vector(tapply(y , x , var )[as.factor(x )])
@@ -51,14 +50,14 @@ fpl <- list(
51
50
se <- unname(b [i , 2 ])
52
51
lower <- t - extend * se
53
52
upper <- t + extend * se
54
- lower [c(2 , 3 )] <- 0
53
+ lower [2 ] <- 0
54
+ lower [3 ] <- eps2 * min(ux [ux != 0 ])
55
55
list (t = t , se = se , lower = lower , upper = upper )
56
56
}
57
57
)
58
58
59
59
# ' Return bisquare and its derivatives
60
60
# '
61
- # ' @description
62
61
# ' This computes bisquare (aka Tukey's biweight) function for a given cutoff
63
62
# ' point. It also computes its first two derivatives. All three functions
64
63
# ' are returned as elements of a list with names `rho`, `psi` and `dpsi`,
@@ -92,7 +91,6 @@ bisquare <- function(k) {
92
91
93
92
# ' Compute M-estimate of scale
94
93
# '
95
- # ' @description
96
94
# ' `m_scale` computes an M-estimate of scale for a given rho-function using
97
95
# ' a one-dimensional root finding routine.
98
96
# '
@@ -252,6 +250,10 @@ m_scale <- function(r, rho, extend = 5) {
252
250
# ' be passed to `m_scale` in order to extend the root finding interval (for
253
251
# ' further details, refer to the documentation of `m_scale`). By default 5.
254
252
# '
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
+ # '
255
257
# ' @return A list containing the following elements:
256
258
# ' - `t`: a vector with the final location estimate, produced by step 3.
257
259
# ' - `t0`: a vector with the initial location estimate, produced by step 1.
@@ -296,7 +298,8 @@ drob <- function( # nolint
296
298
de_args = list (),
297
299
qn_args = list (),
298
300
qn_gr = FALSE ,
299
- ms_extend = 5
301
+ ms_extend = 5 ,
302
+ bounds = identity
300
303
) {
301
304
select <- function (arg , ... ) if (is.character(arg )) switch (arg , ... ) else arg
302
305
mbi <- bisquare(mbi_k )
@@ -306,7 +309,7 @@ drob <- function( # nolint
306
309
" fpl" = fpl ,
307
310
stop(" Invalid model '" , model , " '" )
308
311
)
309
- init <- model $ init(x , y )
312
+ init <- bounds( model $ init(x , y ) )
310
313
lower <- init $ lower
311
314
upper <- init $ upper
312
315
0 commit comments