0

Following my previous question on loops (click here to see it), i now have another doubt: I am trying to run lmer models of several responses in a loop. The dataset is the same used in my previous example (and again the actual one contains many more variables). So here is what I produced:

library(dplyr)
library(purrr)
library(lme4)


lmer_test_list <- test2[c("IA_DWELL_TIME", "IA_MEAN_FIXATION_DURATION", "IA_FIRST_FIXATION_DURATION")] %>% colnames() %>% set_names(.,.)

Lmer_test_FN <- function(test2, na.rm = TRUE, ...){

  for (i in lmer_test_list) {
    # create plot for each text in paired dataset 

    model <- lmer(log(get(i)) ~ Modified +
           Position_line +
           Page +
           Trial +
           (1|Text) +
           (1|Participant),
         data = test2,
         REML=FALSE) %>%
      summary() %>%
      print()

    assign(paste(lmer_test_list[i], "_lmer", sep = ""), model) %>%
      summary() %>%
      print()

  }
}

Lmer_test_FN(test2)

I managed to have the loop working perfectly, with the only problem that my variable name does appear in the summary and the print as "get(i)", meaning I do not know which one I am looking at.

    Linear mixed model fit by maximum likelihood . t-tests use Satterthwaite's method ['lmerModLmerTest']
Formula: log(get(i)) ~ Modified + Position_line + Page + Trial + (1 |      Text) + (1 | Participant)
   Data: test2

any ideas how to preserve the variable name? also, as you might have noticed, i tried to save my model in the environment with the assign function, which did not work either.

As requested I add here a small reproducible example of my dataset:

test2 <- structure(list(Modified = structure(c(2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L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2, 3, 4, 5, 6, 5, 4, 3, 2, 1, 1, 2, 3, 4, 5, 6, 7, 6, 5, 
    4, 3, 2, 1, 1, 2, 3, 4, 5, 6, 5, 4, 3, 2, 1, 1, 3, 4, 5, 
    6, 6), Text = structure(c(6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 
    6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 
    6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 
    6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 
    6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 
    6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 
    6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 
    6L, 6L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 6L, 6L, 6L, 6L, 6L, 6L, 
    6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 
    6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 
    6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 
    6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 
    6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 
    6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L), .Label = c("1", 
    "2", "3", "4", "5", "6", "7", "8"), class = "factor"), Participant = structure(c(1L, 
    1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
    1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
    1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
    1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
    1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
    1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
    1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 58L, 58L, 58L, 58L, 58L, 
    58L, 58L, 58L, 58L, 58L, 58L, 58L, 58L, 58L, 58L, 58L, 58L, 
    58L, 58L, 58L, 58L, 58L, 58L, 58L, 58L, 58L, 58L, 58L, 58L, 
    58L, 58L, 58L, 58L, 58L, 58L, 58L, 58L, 58L, 58L, 58L, 58L, 
    58L, 58L, 58L, 58L, 58L, 58L, 58L, 58L, 58L, 58L, 58L, 58L, 
    58L, 58L, 58L, 58L, 58L, 58L, 58L, 58L, 58L, 58L, 58L, 58L, 
    58L, 58L, 58L, 58L, 58L, 58L, 58L, 58L, 58L, 58L, 58L, 58L, 
    58L, 58L, 58L, 58L, 58L, 58L, 58L, 58L, 58L, 58L, 58L, 58L, 
    58L, 58L, 58L, 58L, 58L, 58L, 58L, 58L, 58L, 58L, 58L, 58L
    ), .Label = c("101", "102", "103", "104", "105", "106", "107", 
    "108", "109", "110", "111", "113", "114", "115", "116", "117", 
    "118", "119", "120", "121", "122", "123", "124", "125", "126", 
    "127", "128", "129", "130", "201", "202", "203", "204", "205", 
    "206", "207", "208", "209", "210", "211", "212", "213", "214", 
    "215", "216", "217", "218", "219", "220", "221", "222", "224", 
    "225", "226", "227", "228", "229", "230"), class = "factor"), 
    IA_LABEL = c("Dear", "Clarissa", "exclaimed", "Mrs", "Hilbery", 
    "you", "look", "to-night", "so", "like", "your", "mother", 
    "as", "I", "first", "saw", "her", "walking", "in", "a", "garden", 
    "in", "a", "grey", "hat", "she", "said", "And", "Clarissas", 
    "eyes", "filled", "with", "tears", "My", "mother", "walking", 
    "in", "a", "garden", "But", "alas", "I", "must", "go", "There", 
    "was", "Professor", "Brierly", "who", "lectured", "on", "Milton", 
    "talking", "to", "little", "Jim", "Hutton", "He", "is", "unable", 
    "even", "for", "a", "party", "like", "this", "to", "compass", 
    "both", "tie", "and", "waistcoat", "or", "make", "his", "hair", 
    "lie", "flat", "and", "even", "at", "this", "distance", "they", 
    "are", "quarrelling", "I", "can", "see", "Professor", "Brierly", 
    "is", "a", "very", "queer", "fish", "with", "all", "those", 
    "degrees", "People", "seemed", "to", "come", "in", "a", "rush", 
    "Dear", "Clarissa", "exclaimed", "Mrs", "Hilbery", "She", 
    "looked", "to-night", "she", "said", "so", "like", "her", 
    "mother", "as", "she", "first", "saw", "her", "walking", 
    "in", "a", "garden", "in", "a", "grey", "hat", "And", "Clarissas", 
    "eyes", "filled", "with", "tears", "Her", "mother", "walking", 
    "in", "a", "garden", "But", "alas", "she", "must", "go", 
    "there", "was", "Professor", "Brierly", "who", "lectured", 
    "on", "Milton", "talking", "to", "little", "Jim", "Hutton", 
    "who", "was", "unable", "even", "for", "a", "party", "like", 
    "this", "to", "compass", "both", "tie", "and", "waistcoat", 
    "or", "make", "his", "hair", "lie", "flat", "and", "even", 
    "at", "this", "distance", "they", "were", "quarrelling", 
    "she", "could", "see", "Professor", "Brierly", "was", "a", 
    "very"), Line_on_page = c(1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 
    2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 4, 4, 4, 
    4, 4, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 5, 5, 6, 6, 6, 6, 6, 
    6, 6, 6, 6, 6, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 1, 1, 1, 1, 
    1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 
    2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 7, 1, 1, 1, 1, 1, 
    1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 
    2, 2, 2, 2, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4, 4, 5, 
    5, 5, 5, 5, 5, 5, 5, 6, 6, 6, 6, 6, 6, 6, 6, 6, 7, 7, 7, 
    7, 7, 7, 7, 7, 7, 7, 7, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 
    1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3), 
    IA_MEAN_FIXATION_DURATION = c(NaN, 241, 254.5, NaN, 281, 
    NaN, 463, 487, NaN, 315, 248, NaN, NaN, 293, 270.666666666667, 
    NaN, 271, NaN, NaN, NaN, NaN, NaN, NaN, NaN, 504, 264, NaN, 
    NaN, 236, 342, 209, NaN, 196, NaN, 148, 294, NaN, NaN, NaN, 
    150, 387, NaN, 213, NaN, 479, 204, 280.666666666667, 295.5, 
    225, 291, NaN, 321, 341, NaN, 269.5, NaN, 348, NaN, NaN, 
    NaN, 598, 298, NaN, 105, 250, 190, NaN, 459, 333, NaN, NaN, 
    233, NaN, 285, NaN, 319, NaN, 224, 165, 272.5, NaN, NaN, 
    NaN, 179, NaN, NaN, NaN, NaN, NaN, 320, 332, NaN, 304, 305, 
    338, NaN, 359, 335, NaN, 220, 246.5, 205, NaN, 264, 203, 
    NaN, 337.666666666667, 144, 232.5, 209.5, 179, 191, NaN, 
    216, 311.666666666667, 162, 299.666666666667, 179, 498.5, 
    235.666666666667, 246, 199, 228.5, 261, 249, 284.5, 150.333333333333, 
    188, 143, 181.666666666667, 171.666666666667, NaN, 204, NaN, 
    NaN, 177.5, NaN, 192, 156, NaN, 229, 223, NaN, NaN, NaN, 
    348, NaN, 219, NaN, 150, 210, 273, 175, 230.5, 565, NaN, 
    181, NaN, 201, 199, NaN, 291.666666666667, NaN, 201, 205.5, 
    156, 253, 178, NaN, 494, 215, 533, 229, 228, 188.333333333333, 
    238.666666666667, 181.5, NaN, 237.666666666667, NaN, 245, 
    184, 252, NaN, NaN, 153, 221, NaN, 287.5, 156, NaN, 186, 
    264, 268, 470.5, NaN, 228.5, 166, 231, NaN, 284), IA_DWELL_TIME = c(NA, 
    241, 509, NA, 281, NA, 463, 487, NA, 315, 248, NA, NA, 293, 
    812, NA, 271, NA, NA, NA, NA, NA, NA, NA, 504, 264, NA, NA, 
    236, 342, 209, NA, 196, NA, 148, 294, NA, NA, NA, 150, 387, 
    NA, 213, NA, 479, 204, 842, 591, 225, 291, NA, 321, 341, 
    NA, 539, NA, 696, NA, NA, NA, 598, 298, NA, 105, 250, 190, 
    NA, 459, 333, NA, NA, 233, NA, 285, NA, 319, NA, 224, 165, 
    545, NA, NA, NA, 179, NA, NA, NA, NA, NA, 320, 332, NA, 304, 
    305, 338, NA, 359, 335, NA, 220, 493, 205, NA, 264, 203, 
    NA, 1013, 144, 465, 419, 179, 715, NA, 1277, 1323, 162, 1539, 
    179, 997, 707, 492, 199, 457, 951, 249, 569, 843, 188, 143, 
    952, 515, NA, 408, NA, NA, 355, NA, 384, 156, NA, 229, 223, 
    NA, NA, NA, 348, NA, 219, NA, 150, 210, 819, 175, 461, 565, 
    NA, 362, NA, 402, 398, NA, 875, NA, 201, 411, 156, 253, 534, 
    NA, 988, 215, 533, 458, 456, 565, 716, 363, NA, 713, NA, 
    245, 184, 252, NA, NA, 153, 221, NA, 575, 156, NA, 186, 264, 
    268, 941, NA, 457, 166, 231, NA, 852), IA_FIRST_FIXATION_DURATION = c(NA, 
    241, 258, NA, 281, NA, 463, 487, NA, 315, 248, NA, NA, 293, 
    175, NA, 271, NA, NA, NA, NA, NA, NA, NA, 504, 264, NA, NA, 
    236, 342, 209, NA, 196, NA, 148, 294, NA, NA, NA, 150, 387, 
    NA, 213, NA, 479, 204, 191, 345, 225, 291, NA, 321, 341, 
    NA, 274, NA, 425, NA, NA, NA, 598, 298, NA, 105, 250, 190, 
    NA, 459, 333, NA, NA, 233, NA, 285, NA, 319, NA, 224, 165, 
    241, NA, NA, NA, 179, NA, NA, NA, NA, NA, 320, 332, NA, 304, 
    305, 338, NA, 359, 335, NA, 220, 293, 205, NA, 264, 203, 
    NA, 527, 144, 225, 224, 179, 163, NA, 181, 183, 162, 292, 
    179, 600, 253, 194, 199, 209, 449, 249, 346, 143, 188, 143, 
    171, 164, NA, 236, NA, NA, 194, NA, 184, 156, NA, 229, 223, 
    NA, NA, NA, 348, NA, 219, NA, 150, 210, 145, 175, 204, 565, 
    NA, 203, NA, 225, 196, NA, 435, NA, 201, 226, 156, 253, 166, 
    NA, 493, 215, 533, 263, 215, 183, 332, 185, NA, 178, NA, 
    245, 184, 252, NA, NA, 153, 221, NA, 418, 156, NA, 186, 264, 
    268, 373, NA, 273, 166, 231, NA, 216)), row.names = c(NA, 
-201L), groups = structure(list(Text = structure(c(5L, 6L, 6L
), .Label = c("1", "2", "3", "4", "5", "6", "7", "8"), class = "factor"), 
    Modified = structure(c(1L, 1L, 2L), .Label = c("Original", 
    "Modified"), class = "factor"), .rows = list(101:107, 108:201, 
        1:100)), row.names = c(NA, -3L), class = c("tbl_df", 
"tbl", "data.frame"), .drop = TRUE), class = c("grouped_df", 
"tbl_df", "tbl", "data.frame"))
4
  • Better to build a formula in as a string than using get, as.formula(paste(i, "~ Modified + Word_length + ...")). Commented Mar 11, 2019 at 20:21
  • @Gregor, not sure what u mean, tried this but still got "i" and not the variable name: lmer(as.formula(paste(i, "~ Modified + Word_length + Page + Trial + (1|Text) + (1|Participant)")), data = dataset_byTrial, REML=FALSE) Commented Mar 11, 2019 at 23:03
  • also, I don’t know how to save the model with a customised name. For instance, I tried to add this just before the ‘lmer’ formula: paste(lmer_durations_list [i], "_lmer") <- lmer(log(get(i) #...... etc or to add it after the formula with the assign() function, but neither of them works. Commented Mar 12, 2019 at 11:16
  • new reproducible dataset edited, please help if you can! thanks :) Commented Mar 12, 2019 at 16:30

1 Answer 1

0

In order to get the right names in the formula, you should avoid get() and actually build a proper formula object. You can use bquote() to insert symbol names (created from character) into expressions. Here we change the function to use lapply in order to return a list of models

Lmer_test_FN <- function(test2, na.rm = TRUE, ...){
  lapply(lmer_test_list, function(i) {
    model_formula <- bquote(log(.(as.name(i))) ~ Modified +
                  Position_line +
                  Page +
                  Trial +
                  (1|Text) +
                  (1|Participant))
    lmer(model_formula, data = test2, REML=FALSE)
  })
}
models <- Lmer_test_FN(test2)

Then if you want to view the summary of the models, you can just apply summary to that list

lapply(models, summary)

It's generally not a good idea to create a bunch of variables in R in your global names space that contain data in their names. Everything is easier if you work with named lists. You can extract individuals models using indexing

models[[1]]
models$IA_DWELL_TIME
Sign up to request clarification or add additional context in comments.

2 Comments

That works nicely, thanks. If I had several variable lists with measures that need slightly different lmer models, could I nest functions so that all the results of my 4 different sub-functions (and model-types) are grouped together in one final list (your "models")?
also if I do not need to log my value, and i tried this `bquote(as.name(i) ~ Modified ... etc) it now gives me an error, any idea why?

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.