Skip to content

BUG - ED() Fails for Exponential Decay Models with Two Fixed Parameters #50

Description

@hreinwald

The Problem

Calling ED() on exponential decay models (EXD.2, EXD.3, AR.2, AR.3, W1.x, W2.x) fitted with two fixed parameters — leaving only a single parameter to be estimated — throws an "incorrect number of dimensions" error. It is currently impossible to retrieve ED values from these models in this configuration. See the code example below:

Code Example

# Reproducible example for testing

x <- data.frame(
  conc = c(1e+01, 1e+00, 1e-01, 1e+04, 1e+03, 1e+02, 1e+01, 1e+00,
           1e-01, 1e+04, 1e+03, 1e+02, 1e+01, 1e+00, 1e-01, 1e+04,
           1e+03, 1e+02, 0e+00, 0e+00, 0e+00, 0e+00, 0e+00, 0e+00,
           0e+00, 0e+00, 0e+00, 0e+00, 0e+00, 0e+00, 0e+00, 0e+00,
           0e+00, 0e+00, 0e+00, 0e+00),
  yield = c(15083.677, 142275.764, 197718.468, 0.000, 0.000, 0.000,
            67265.046, 197718.468, 266206.515, 28129.019, 5299.695,
            47697.033, 0.000, 139014.428, 178150.455, 0.000, 0.000,
            28129.019, 181411.790, 142275.764, 329394.891, 156544.107,
            230331.770, 256422.508, 112923.744, 187934.462, 321649.219,
            158582.442, 189157.384, 109662.408, 155321.106, 158582.442,
            311865.213, 259683.844, 152059.728, 178150.455)
)

##  Parameters ##
drm.formula = as.formula("yield ~ conc")
lowerl = 0 
# Compute the mean of the control group (conc == 0) to use as the upper limit in the model fitting
x %>%
  filter(conc == 0) %>%
  pull(yield) %>%
  mean(na.rm = TRUE) -> upperl


## Fitting with one fixed parameter (lower limit) and one free parameter (upper limit)
res = drm(
  formula = drm.formula, data = x,
  fct = EXD.3(fixed = c(lowerl, NA, NA)),      # <- this works
  na.action = na.omit 
)

plot(res, type = "all")
ED(res, c(10, 20,50), interval = "tfls")


## Fitting with one fixed parameter (lower limit) and one free parameter (upper limit)
res2 = drm(
  formula = drm.formula, data = x,
  fct = EXD.3(fixed = c(lowerl, upperl, NA)),  # <- this leads to the BUG in the ED() call
  na.action = na.omit 
)

plot(res2, type = "all")
try( 
  ED(res2, c(10, 20,50), interval = "tfls")  # <<< ERROR HERE
)

Two issues inside ED.drc:

  1. Variance-covariance matrix not coerced to a proper matrix. When only one parameter is estimated, vcov() returns a 1×1 object that can collapse to a scalar or unnamed numeric of length 1.
    ED.drc performs matrix operations (%*%, t(), indexing) that assume vcov always has two dimensions. A scalar lacks these dimensions, causing the error.

  2. Named gradient vectors cause dimension mismatches. Model-specific derivative functions (e.g., for EXD or W1) can return named numeric vectors. When passed into matrix multiplication, R can interpret the names as an implicit dimension attribute, producing conformability or dimension errors in the delta-method calculation.

Suggested Fix

Two small defensive changes in ED.drc:

# 1. Always coerce vcov to a proper matrix
vcMat <- as.matrix(vcov(object))

# 2. Strip names from gradient vectors before matrix algebra
g <- unname(g)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions