Skip to content

Warning in ds.glmerSLMA: "number of items to replace is not a multiple of replacement length" #655

@villegar

Description

@villegar

 ⚠️ TL;DR

ds.glmerSLMA emits a harmless warning:

"number of items to replace is not a multiple of replacement length"

when different studies return different numbers of coefficients.

The function completes successfully; the warning comes from internal matrix assignment (betamatrix[,k] <- ...).
Padding shorter vectors with NA resolves it.


Description

While running the unit tests for dsBaseClient (e.g., test-smk-ds.glmerSLMA.R), the following warnings are observed:

Warning ('test-smk-ds.glmerSLMA.R:106:5'): check slope formulae - 1
number of items to replace is not a multiple of replacement length
Backtrace:
 1. dsBaseClient::ds.glmerSLMA(...) at test-smk-ds.glmerSLMA.R:106:5
Warning ('test-smk-ds.glmerSLMA.R:178:5'): check slope formulae - 2
number of items to replace is not a multiple of replacement length
Backtrace:
 1. dsBaseClient::ds.glmerSLMA(...) at test-smk-ds.glmerSLMA.R:178:5

These warnings occur even when the function completes successfully, and seems to originate from the internal assignment of coefficients into matrices:

  • Line ~220–224 in ds.glmerSLMA:
betamatrix <- matrix(NA, nrow=numcoefficients, ncol=numstudies)
sematrix  <- matrix(NA, nrow=numcoefficients, ncol=numstudies)

for(k in study.include.in.analysis){
  betamatrix[,k] <- study.summary[[k]]$coefficients[,1]
  sematrix[,k]  <- study.summary[[k]]$coefficients[,2]
}

The warning is triggered when different studies return different numbers of coefficients (e.g., due to aliasing or missing covariates).


Minimal Reproducible Example (Plain R)

The warning can be reproduced without a DataSHIELD server:

# Simulate study.summary as returned internally by ds.glmerSLMA
# study 1 has 3 coefficients
# study 2 has 2 coefficients → shorter than max
study.summary <- list(
  list(coefficients = matrix(c(1, 2, 3), ncol = 1)), 
  list(coefficients = matrix(c(4, 5), ncol = 1))
)

numstudies <- length(study.summary)
numcoefficients.max <- max(sapply(study.summary, function(s) nrow(s$coefficients)))

betamatrix <- matrix(NA, nrow = numcoefficients.max, ncol = numstudies)

for(k in 1:numstudies) {
  betamatrix[, k] <- study.summary[[k]]$coefficients[,1]
}

R Warning:

number of items to replace is not a multiple of replacement length

Suggested Minimal Fix

Replace the direct assignment with a length-safe assignment:

for(k in seq_len(numstudies)){
  ncoef_k <- length(study.summary[[k]]$coefficients[,1])
  betamatrix[1:ncoef_k, k] <- study.summary[[k]]$coefficients[,1]
  sematrix[1:ncoef_k, k]  <- study.summary[[k]]$coefficients[,2]
}

This ensures that shorter coefficient vectors are padded with NA rather than triggering a warning.


Additional Notes

  • The warning appears when different studies have different numbers of coefficients (e.g., random slopes, missing covariates, aliasing).
  • The warning does not indicate incorrect results — it’s only due to R’s column assignment behavior.
  • This affects both betamatrix and sematrix inside ds.glmerSLMA.
  • This also explains the test warnings seen in test-smk-ds.glmerSLMA.R on lines 106 and 178.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions