I am trying to simulate data based on two random effects and no fixed effects. The simulated data table contains nP projects and within each project nB Batches exists. Each Batch has nR replicates. Thus, I have a balanced design in the end. The random effect (i.e. standard deviation) over all Batches is sd_B. However, this standard deviation also varies over the different projects with sd_ranP. From my understanding, the random effects from Batch and Product are nested and thus should be estimated by a nested linear mixed model. I tried to model the simulated data by using the lme4 package in GNU R. However, I encounter singularity issues and question myself whether I have understood nested random effects correctly or if there is a mistake in my simulation.
What I tried so far is the following:
First, I define the number of Products, Batches and Replicates together with the corresponding standard deviations.
nP <- 25 #choose number of Products nB <- 25 #choose number of Batches nR <- 5 #choose number of Replicates sd_B <- 100 #Standard Deviation for Batch sd_ranP <- 10 #Random variability of SD Batch across the different Products sdRes <- 0.2 #Residual Standard Deviation Next, I sample the standard deviation for each batch over all products. Since the standard deviation of the batches change slightly from one product to the next, I consider this by with the nested random term sd_ranP (For simplification reason I assumed that the sampled sdB is also normally distributed, fully aware that standard deviations cannot be negative so maybe a log normal sampling would be more correct here. However, I have used values for sd_B and sd_ranP that no negative values for sdB occur.)
#sample the standard deviation of the batches by considering a nested random effect over the products sdB <- rnorm(n = nP, mean = sd_B, sd= sd_ranP) Afterwards, I sample from sdB, add some residual variance sdRes which has the same standard deviation across all products and batches and finally I create a data frame from this new y response vector.
#sample from sdB resB <- unlist(map(.x = sdB, ~rnorm(n = nB, mean = 0, sd = .x))) #create y and add sdRes y <- unlist(map2(.x = resB, .y = sdRes, ~rnorm(n = nR, mean = .x, sd = sdRes))) #create a data frame df <- data.frame(Product = as.factor(rep(1:nP, each = nB*nR)), Batch = as.factor(rep(1:(nB*nP), each = nR)), y = y) By using the following seed setseed(29072023) the simulate data visualized in ggplot2 show a different batch variability across the projects with a very small residual or within batch variability. This exactly what I would expect.

Finally, I tried to fit the following model: model <- lmer(y ~ 1 + (1|Product/Batch), data = df) expecting estimated random effects for Batch:Product of around 100, Product of around 10 and Residual of around 0.2.
The interesting point is that in cases where I do not use a seed, in some cases I get exactly these values but in around half of the cases I end up with a singularity issue and a random effect for Product very close or equal to 0. My first thought was that the two random effects might be confounded, however in that case I would expect a singularity issue in all cases and not only in around 50% of the time.
To investigate this observation closer, I repeated the sampling process and model fitting 1000 times. Each time I saved the estimated random effect value for the Product in a vector and created a histogram of the results. I turned out, that in around 58% of the cases the estimated random effect for Product was below 0.1. However, if these values are neglected, the other 42% show exactly the distribution I would expect (the mean of the distribution is approximately at 10).

I hope someone can shed a bit light into my observation ;-)
Here the code for 1000 sampling repetitions.
#load packages library(tidyverse) library(lme4) #outputs of for loop outm <- NULL nP <- 25 #choose number of Products nB <- 25 #choose number of Batches nR <- 5 #choose number of Replicates sd_B <- 100 #Standard Deviation for Batch sd_ranP <- 10 #Random variability of SD Batch across the different Products sdRes <- 0.2 #Residual Standard Deviation set.seed(29072023) for(i in 1:1000){ #sample the standard deviation of the batches by considering a nested random effect over the products sdB <- rnorm(n = nP, mean = sd_B, sd= sd_ranP) #sample from sdB resB <- unlist(map(.x = sdB, ~rnorm(n = nB, mean = 0, sd = .x))) #create y and add sdRes y <- unlist(map2(.x = resB, .y = sdRes, ~rnorm(n = nR, mean = .x, sd = sdRes))) #create a data frame df <- data.frame(Product = as.factor(rep(1:nP, each = nB*nR)), Batch = as.factor(rep(1:(nB*nP), each = nR)), y = y) model <- lmer(y ~ 1 + (1|Product/Batch), data = df) summary(model) outm <- rbind(outm, as.data.frame(summary(model)[["varcor"]])) } outm$sdcor <- as.numeric(outm$sdcor) test <- outm %>% dplyr::filter(grp == "Product") hist(test$sdcor)