EDIT: I've re-read the thread and realize this sound a general question on mixed effects models than specifically for the coxme function. In addition, for the simulation, I increased the animals per genotype per experiment, to avoid any sample-size related issues.

I have survival data from two different animal strains (WT vs KO) over 24 days that was produced in 5 different, independent experiments. Therefore, I wanted to analyze my data using Mixed effects Cox Model, for which I want to use the coxme function from the coxme package. In order to simulate a similar dataset, I wrote this code:


               Mortality=rep(c(rbinom(100,size = 1,prob=0.2),

tb$TimePoint[tb$Mortality==1]<-sample(1:24,replace = T,
                          size = length(tb$Mortality[tb$Mortality==1]))

My question is, in order to account for different mortality rates in the experiments, as well as different genotype effect in the different experiments, how should I structure the random effects? Normally for a general lmer, I would add the intercept and a random slope for the Genotype varying with the Experiment, like this: (1 + Genotype|Experiment). However, this doesn't seem to work with coxme, producing the following output:

Error in gchol(kfun(theta, varlist, vparm, ntheta, ncoef)) : 
  NA/NaN/Inf in foreign function call (arg 5)
In addition: Warning messages:
1: In sqrt(xvar * zvar) : NaNs produced
2: In sqrt(xvar * zvar) : NaNs produced

Does anyone knows how I should include the random slope for the coxme function?


  • $\begingroup$ The warning messages suggest that xvar * zvar within the coxme code is returning negative values, perhaps due to a problem in simulating the data. Unless estimating the variance of Genotype effects among Experiments is of major importance, you could account for within-Experiment correlations simply by using a cluster() term in a standard coxph() model. That gives robust standard errors of coefficient estimates similar to what generalized estimating equations provide. If your experiment is as well balanced as your simulation, that should work fine. $\endgroup$
    – EdM
    Aug 18, 2021 at 14:40

1 Answer 1


Note: this answer was in response to an earlier version of the question.

The (1|site/block) random-effect specification means an "intercept varying among sites and among blocks within sites (nested random effects)." That's not what you have in terms of Genotype. That type of specification might be used if, for example, there were multiple litters of animals within each Experiment and you wanted to allow for litter-within-experiment random intercepts.

Remember that the main predictor of interest here is the two-level Genotype, whose Cox regression coefficient can be considered a "slope" in the model. If you want to allow for different Genotype effects among Experiments, you need to include a random slope for Genotype among Experiments. As you also want random intercepts, you need to decide what type of correlations you want to impose between random slopes and intercepts. The lmer cheat sheet and its links shows how to proceed, depending on that choice.

That said, with only 10 animals of each type in each experiment, you might not have enough events to handle both random intercepts and slopes well. Think carefully, based on your understanding of the subject matter, what you hope to gain from allowing for random slopes.

  • $\begingroup$ Thanks for your imput! After reading your answer and re-reading the post, I realized the question had a more general range than being focused on the coxme package. Sorry about that! However, regarding the random slope, I normally consider it to be important for lmer's since, besides the baseline being different (intercept), the effect of my variable of interest might be different across experiments; therefore, using only the intercept sounds I am not accounting for all the sources of variation in the experiments. That being said, I am also (painfully) aware of the sample size issue Thnks! $\endgroup$ Aug 18, 2021 at 10:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.