rmixed-modelsr-inla

Error message when running mixed effect models using R-INLA


I am using R-INLA to run the following model (Treatment, Animal.1 and Animal.2 are factors and Encounter.Length is continuous):

formula <- Encounter.Length ~ Treatment +f(Animal.1, model = "iid", n = n.animal) + 
           f(Animal.2, copy = "Animal.1")

m.1 <- inla(formula, data = inla.dat)

However, after running this code I get the following error message:

Error in inla(formula, data = inla.dat) : In f(Animal.1): 'covariate' must match 'values', and both must either be 'numeric', or 'factor'/'character'.

I am new to using INLA and want to know what this error message means and how to fix it.


Solution

  • Answer (from r-inla.help): The levels of B are not a subset of A (which is used to define the model, for which B copies). So you must define the models on the union on the levels.

    For example:

    n <- 3 
    A <- as.factor(letters[1:n]) 
    B <- as.factor(letters[1+1:n]) 
    y <- 1:n 
    

    This does not work

    inla(y ~ -1 + f(A) + f(B, copy = "A"), data = data.frame(A, B)) 
    

    But this does

    values <- as.factor(unique(c(levels(A), levels(B)))) 
    inla(y ~ -1 + f(A, values = values) + f(B, copy = "A"), 
    data = list(A = A, B = B, values = values))