I measured the percentage of individuals that crossed an averisve barrier at six different time points for four different genotypes.

The barrier is made out of an aversive substance that the control individuals do not like. With increased time the control individuals get hungry and start searching for food so the response to this aversive substance reduces and they cross the barrier.

The time points represent hours, so I measured the number of individuals that crossed this barrier at 0.5, 1, 2, 3 and 4 hours but for the same individuals.

Individuals did not go back to the other side so this movement is only in one direction.

For example for one genotype 1 individual out of 20 crossed the barrier at 0.5 h, 5% crossed the border. One time point later I measured the same worms again. So now the individual that crossed the barrier at 0.5 h and 3 more individuals are on the other side, so 4 individuals were counted (20%) etc.

I want to investigate other individuals with different genotypes than that of the control and see if their behaviour to this aversive barrier differs, so perhaps they can't sense it at all so it would be expected that more individuals cross the barrier or start to cross it earlier than control or the opposite effect is seen that fewer individuals cross the barrier or they cross it later showing increased sensitivity to the barrier.

I want to analyze statistically if there is a difference between the genotypes in general (so over all six time points together) meaning if one genotype is more or less likely to cross the barrier than the other in the span of the 4 hours and perhaps also for individual times.

New contributor
MarsC is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.
  • $\begingroup$ A linear mixed model. $\endgroup$ Commented Jul 11 at 10:03
  • $\begingroup$ Thank you for your suggestion. I am an absolute statistic noob. Could you further explain how this would look like and which software you woud use? How has the data be formated for that? I have multiple repeats for every genotype of course. $\endgroup$
    – MarsC
    Commented Jul 11 at 10:47
  • $\begingroup$ Instead of the proportion of individuals that crossed, can you obtain the data labeled per individual? Each row would then be of a given individual, at a given time point, and your columns would be individual (ID), time, genotype and crossed (yes or no). If so, I can write an answer using R. $\endgroup$ Commented Jul 11 at 14:28
  • $\begingroup$ Theoretically yes, but individuals are not completly independend from each other as they are all from the same population for one replicate. If I would give every individual an ID I would treat the individuals from different repeats on different days like they were all measured at the same day right? I don't know if this does matter or not. $\endgroup$
    – MarsC
    Commented Jul 11 at 17:50
  • $\begingroup$ If you have enough of those replicates then you could incorporate that as well. Do you mean the entire story as you described it has been replicated several times? By the way I count 5 timepoints, are we missing one? $\endgroup$ Commented Jul 11 at 18:40

1 Answer 1


Generalized Linear Mixed Model with Nesting

You could model this with a generalized linear mixed model, which would look something like this:

GLMM <- glmer(crossing ~ genotype * log(time) + (1 | replicate / ID), 
              family = "binomial", data = DF)

Quick explanation:

  • Crossing the border is a binary event ($0 = \textrm{not crossed}$, $1 = \textrm{crossed}$), which can be assumed to follow a binomial distribution with a single trial.

  • There is a nested random effect of individuals within replicates, (1 | replicate | ID).

    • Individuals within a replicate are assumed to come from some larger possible population of individuals they could have been picked from;
    • These replicates themselves are assumed to be a random draw from all the possible replicates that could have been performed.
  • There is some presumably positive relationship between time and the probability of crossing. As an example I used a logarithmic effect here, log(time).

  • This relationship with time depends on (i.e., has a different slope based on) the genotype, which can be modelled as an interaction between time and genotype, genotype * log(time).

Estimated Fixed and Random Effects


# Random effects:
#  Groups       Name        Variance Std.Dev.
#  ID:replicate (Intercept) 0.06785  0.2605  
#  replicate    (Intercept) 0.83129  0.9118  
# Number of obs: 2100, groups:  ID:replicate, 420; replicate, 7
# Fixed effects:
#                     Estimate Std. Error z value Pr(>|z|)    
# (Intercept)          0.73274    0.35576   2.060   0.0394 *  
# genotypeB            0.09504    0.12260   0.775   0.4382    
# log(time)            0.03356    0.09367   0.358   0.7201    
# genotypeB:log(time)  0.75235    0.13844   5.434  5.5e-08 ***

(Suppressed some of the output.)

There are lots of great posts on CV on how to interpret this, but in short (all on the linear scale):

  • The estimated variance between replicates is 0.83129.
  • Within a replicate, indidivuals have an estimated variance of 0.06785.
  • Genotype A has a weakly positive, but non-significant slope for time (0.03356).
  • Genotype B has a much stronger slope for time (0.03356 + 0.75235) and if it matters, the difference is significant ($p = 5.5 \cdot 10^{-8}$).

Visualization of the Estimated Relationship

There's an amazing package that can help with this:

plot_model(GLMM, type = "pred", terms = c("time", "genotype"))

marginal effects plot

Here you can easily see how to interpret the positive, significant interaction term that said B has a steeper slope for time than A.

Simulated Example Data

Here is the simulation I used:

r <- 7  # replicates
n <- 30 # individuals within a single replicate
k <- 2  # genotypes
t <- 5  # time points

ID <- factor(rep(1:(n * r), each = t, times = k))
time <- rep(c(0.5, 1:4), times = k * n * r)
genotype <- factor(rep(LETTERS[1:k], each = t, times = n * r))
replicate <- factor(rep(1:r, each = n * k * t))

# Design matrix of the fixed effects
X <- model.matrix(~ ns(time, 2) * genotype)

# Design matrix of the random effects
Z <- model.matrix(~ 0 + replicate + ID)

# Random coefficients
beta <- rnorm(ncol(X))
upsilon <- numeric(ncol(Z))
upsilon[1:r] <- rnorm(r)
upsilon[(r + 1):ncol(Z)] <- rnorm(n * r - 1, 0, 0.5)

# The linear combination of fixed and random effects
eta <- X %*% beta + Z %*% upsilon

# Simulate the binomial process based on eta
y <- rbinom(nrow(X), size = 1, prob = binomial()$linkinv(eta))

# Combine all variables in a data frame and clean up
DF <- data.frame(replicate, ID, time, genotype, crossing = y)
rm(r, n, k, t, ID, time, genotype, replicate, X, Z, beta, upsilon, eta, y)


This is not the only possible way this can be analyzed, just one possible approach that I think can be reasonably justified.

Another approach might be to do some sort of survival analysis, where you model the time until each fish crosses.

  • $\begingroup$ Thank you so much for this detailed explanation. I will try to do this with my data. I also think that survial analysis could be possible and will also look into that. $\endgroup$
    – MarsC
    Commented Jul 12 at 10:46
  • 1
    $\begingroup$ I agree that you will want to look into survival analysis, a.k.a. time-to-event modeling or duration modeling. UCLA's statistics site has some good information and worked examples. $\endgroup$
    – rolando2
    Commented Jul 12 at 17:50

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.