How to fix "Error: number of items to replace is not a multiple of replacement length" with NCT()?
11:02 09 Nov 2026

I'm trying to compare two networks using NetworkComparisonTest::NCT(). I used a custom estimator function to impute and transform data prior to network estimation. The networks work well (i.e., in plotting and bootstrapping) until I use NCT, where I get the following error:

> Error in diffedges.perm[i, ] <- abs(r1perm - r2perm)[upper.tri(abs(r1perm -  : 
  number of items to replace is not a multiple of replacement length

I assume this has to do with having changed the dimensions of the data during network estimation. I'm not sure how to proceed.

Below is a minimum, reproducible example:

libraries and dataset
library(tidyverse)
library(mice)
library(bootnet)
library(abind)
library(mgm)

data1 <- selfreport |> slice_sample(n=50, replace=TRUE) |>
 select(c(src,sex,hm,wm,hr,wr)) |> 
 mutate(
  src = case_when(
    src == "krul" ~ 0,
    src == "mgg" ~ 1
  ),
  sex = case_when(
    sex == "Female" ~ 0,
    sex == "Male" ~ 1
  ),
)

data2 <- selfreport |> slice_sample(n=50, replace=TRUE) |> 
select(c(src,sex,hm,wm,hr,wr)) |> 
mutate(
  src = case_when(
    src == "krul" ~ 0,
    src == "mgg" ~ 1
  ),
  sex = case_when(
    sex == "Female" ~ 0,
    sex == "Male" ~ 1
  ),
)
# Multiple imputation estimator function for mgm:
# CODE DERIVED FROM LIU ET AL 2021 https://osf.io/vufj4/files/watkp
impute_estimator <- function(x, nImpute = 10, type, level, ...){
  
  # Impute missing data:
  miceres <- mice(x, m = nImpute, seed = 500, use.matcher = TRUE) |>
 mice::complete(action = "long", include = TRUE) |> 
 mutate(
    hmhr = hm+hr,
    wmwr = wm+wr
  ) |> dplyr::select(c(
    src,
    sex,
    hmhr,
    wmwr,
    .id,
    .imp
  )) |> as.mids()
  
  # Results:
  graphs <- lapply(seq_len(nImpute), function(i){
    imp <- complete(miceres, i)
    
    # Estimate the network:
    net <- estimateNetwork(imp, default = "mgm", type = type, level = level, 
                           criterion  = "EBIC", rule="AND", tuning = 0.25,
                           binarySign = TRUE)
    
    # Return graph:
    return(net$graph)                
  })
  
  # Make array:
  array <- do.call(abind,c(graphs,list(along=3)))
  
  # Average and retain only edges included in all imputations:
  final <- apply(array,1:2,function(x){
    (mean(x!=0)>=0.9) * mean(x)
  })
  
  # Return:
  # Source - https://stackoverflow.com/a/80008025
  # Posted by Lakshay Sharma
  # Retrieved 2026-10-05, License - CC BY-SA 4.0
  dimnames(final) <- list(node_names, node_names)
  return(final)
} 
type_mgm=c(rep("c",2),rep("g",2))
level_mgm=c(rep("2",2),rep("1",2))
node_names <- c("src", "sex", "hmhr", "wmwr")
# compute network 1
# Source - https://stackoverflow.com/a/80008025
# Posted by Lakshay Sharma
# Retrieved 2026-10-05, License - CC BY-SA 4.0
network1 <- estimateNetwork(
  data = data1,
  fun = impute_estimator,
  type = type_mgm,
  level = level_mgm,
  labels = node_names
)
network1$labels <- node_names
network1$nNode <- length(node_names)
# compute network 2
network2 <- estimateNetwork(
  data = data2,
  fun = impute_estimator,
  type = type_mgm,
  level = level_mgm,
  labels = node_names
)
network2$labels <- node_names
network2$nNode <- length(node_names)
NCT
# nct
library(NetworkComparisonTest)
nct1 <- NCT(network1, network2, it=1,
            test.edges=T,
            edges="all",
            p.adjust.methods = "holm",
            test.centrality = T,
            centrality = c("closeness", "betweenness", "strength"),
            nodes = "all"
)
r