I have a function that runs a variable through a specific lme4::lmer() model syntax as such:
run_my_model <- function(i)
{
f <- formula(paste("y_variable ~", i, "+", i, "*interaction_variable + ", i, "*another_interaction_variable + (1|date) + (1|location)"))
result <- tidy_lmer(lmer(f, data=my_dataset))
print(result)
}
When I run it on one specific variable in my dataset it works fine:
run_my_model("one_of_my_variables")
returns:
term estimate std.error df statistic p.value
1 (Intercept) 1.977634e-04 0.001376695 24.21203 0.14365088 0.8869651
2 one_of_my_variables -2.805953e-03 0.001810723 69367.30600 -1.54963171 0.1212345
3 interaction_variableTRUE -8.648828e-04 0.001038360 20.06112 -0.83293141 0.4146903
4 another_interaction_variableTRUE 7.352286e-05 0.001539428 20.02418 0.04775987 0.9623808
5 one_of_my_variables:interaction_variableTRUE -1.383274e-04 0.010860708 67714.86149 -0.01273650 0.9898381
6 sd_(Intercept).location 2.358986e-02 NA NA NA NA
7 sd_(Intercept).date 2.098956e-03 NA NA NA NA
8 sd_Observation.Residual 4.812015e-02 NA NA NA NA
group
1 fixed
2 fixed
3 fixed
4 fixed
5 fixed
6 location
7 date
8 Residual
but when I try to use it with map() so it works on all variables in the dataset:
test <- map(names(my_dataset), run_my_model)
returns
Error in eval_f(x, ...) : Downdated VtV is not positive definite
it also returns the same error if I use a for loop e.g.
models <- list()
for (i in names(my_dataset))
{
f <- formula(paste("y_variable ~", i, "+", i, "*interaction_variable + ", i, "*another_interaction_variable + (1|date) + (1|location)"))
models[[i]] <- lmer(f, data=my_dataset)
}
I know that this model syntax works for some of the variables (as shown above) so clearly it is some specific ones that are generating this error. I tried looking at the traceback but it didn't provide any information of interest as to at which point the error first occurred. I have also tried nesting the lmer() call inside try() but the error still occurs.
How can I get the map() statement or loop to run successfully for the variables that do not generate this error message and just return NA or something similar for the ones that generate the error, without just writing out the syntax over and over again for each variable?