12666727b9
12666727b9

Reputation: 1127

How to fit quickly mixed models by using an iterative method on a long format dataset

I need to fit a mixed model with both ID and GROUP (COND) variables for the following dataset:

   ID    GR    SES   COND    signals   value
   <chr> <chr> <chr> <chr>   <fct>     <dbl>
 1 01    RP    V     NEG-CTR P3FCz   -11.6  
 2 01    RP    V     NEG-NOC P3FCz   -11.1  
 3 01    RP    V     NEU-NOC P3FCz    -4.00 
 4 04    RP    V     NEG-CTR P3FCz    -0.314
 5 04    RP    V     NEG-NOC P3FCz     0.239
 6 04    RP    V     NEU-NOC P3FCz     5.04 
 7 06    RP    V     NEG-CTR P3FCz    -0.214
 8 06    RP    V     NEG-NOC P3FCz    -2.96 
 9 06    RP    V     NEU-NOC P3FCz    -1.97 
10 07    RP    V     NEG-CTR P3FCz    -2.83 
# ... with 965 more rows

where signals variable is not a predictor variable, but it is just a nominal variable. Since there are 12 king of signals and to each of this corresponds a spefic range of value into the beside column (value), I would like to know whether by using this data long setting it is possible to run the lmer() function by using COND and ID as fixed and random effects, via an iterative function (such as for loop, map(), apply() functions and so on). If don't, how this will be supposed to be written for iterating analysis on data set as a wide format?

If possible I would like to extract iteratively diagnostic plot (if included) by each fitted model. Thanks for those ones who will answer.

Here the dataset

> dput(head(out_long, 50))
structure(list(ID = c("01", "01", "01", "04", "04", "04", "06", 
"06", "06", "07", "07", "07", "08", "08", "08", "09", "09", "09", 
"10", "10", "10", "11", "11", "11", "12", "12", "12", "13", "13", 
"13", "15", "15", "15", "16", "16", "16", "17", "17", "17", "18", 
"18", "18", "19", "19", "19", "21", "21", "21", "22", "22"), 
    GR = c("RP", "RP", "RP", "RP", "RP", "RP", "RP", "RP", "RP", 
    "RP", "RP", "RP", "RP", "RP", "RP", "RP", "RP", "RP", "RP", 
    "RP", "RP", "RP", "RP", "RP", "RP", "RP", "RP", "RP", "RP", 
    "RP", "RP", "RP", "RP", "RP", "RP", "RP", "RP", "RP", "RP", 
    "RP", "RP", "RP", "RP", "RP", "RP", "RP", "RP", "RP", "RP", 
    "RP"), SES = c("V", "V", "V", "V", "V", "V", "V", "V", "V", 
    "V", "V", "V", "V", "V", "V", "V", "V", "V", "V", "V", "V", 
    "V", "V", "V", "V", "V", "V", "V", "V", "V", "V", "V", "V", 
    "V", "V", "V", "V", "V", "V", "V", "V", "V", "V", "V", "V", 
    "V", "V", "V", "V", "V"), COND = c("NEG-CTR", "NEG-NOC", 
    "NEU-NOC", "NEG-CTR", "NEG-NOC", "NEU-NOC", "NEG-CTR", "NEG-NOC", 
    "NEU-NOC", "NEG-CTR", "NEG-NOC", "NEU-NOC", "NEG-CTR", "NEG-NOC", 
    "NEU-NOC", "NEG-CTR", "NEG-NOC", "NEU-NOC", "NEG-CTR", "NEG-NOC", 
    "NEU-NOC", "NEG-CTR", "NEG-NOC", "NEU-NOC", "NEG-CTR", "NEG-NOC", 
    "NEU-NOC", "NEG-CTR", "NEG-NOC", "NEU-NOC", "NEG-CTR", "NEG-NOC", 
    "NEU-NOC", "NEG-CTR", "NEG-NOC", "NEU-NOC", "NEG-CTR", "NEG-NOC", 
    "NEU-NOC", "NEG-CTR", "NEG-NOC", "NEU-NOC", "NEG-CTR", "NEG-NOC", 
    "NEU-NOC", "NEG-CTR", "NEG-NOC", "NEU-NOC", "NEG-CTR", "NEG-NOC"
    ), signals = 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), .Label = c("P3FCz", 
    "P3Cz", "P3Pz", "LPPearlyFCz", "LPPearlyCz", "LPPearlyPz", 
    "LPP1FCz", "LPP1Cz", "LPP1Pz", "LPP2FCz", "LPP2Cz", "LPP2Pz", 
    "LPP2POz"), class = "factor"), value = c(-11.6312151716924, 
    -11.1438413285935, -3.99591470944713, -0.314155675382471, 
    0.238885648959708, 5.03749946898385, -0.213621915029167, 
    -2.96032491743069, -1.97168681693488, -2.83109425298642, 
    1.09291198163802, -6.692991645215, 4.23849942428043, 2.9898889629932, 
    3.5510699900835, 9.57481668808606, 5.4167795618285, 1.7067607715475, 
    -6.13036076093477, -2.82955734597919, -2.50672211111696, 
    0.528517585832501, 8.16418133488309, 1.88777321897925, -7.73588468896919, 
    -9.83058052401056, -6.97442700196932, 1.27327945355082, 2.11962397764132, 
    0.524299677616254, -1.83310726842883, 0.658810483381172, 
    -0.261373488428192, 4.37524298634374, 0.625555654900511, 
    3.19617639836154, 0.0405517582137798, -3.29357103412113, 
    -0.381435057304614, -5.73445509910268, -6.1129152355645, 
    -2.45744234877604, 2.95352732001065, 0.527721249096473, 1.91803490989119, 
    -3.46703346467546, -2.40438419043702, -5.35374408162217, 
    -7.27028665849262, -7.1532211375959)), row.names = c(NA, 
-50L), class = c("tbl_df", "tbl", "data.frame"))
> 

PLease just let me know whether you will need the wide format dataset.

Upvotes: 0

Views: 262

Answers (1)

Rui Barradas
Rui Barradas

Reputation: 76460

The following could probably run the models for each signal. With the posted data it gives an error (see this post of yours) stating that there are too few observations.

Note that the formula

ID + COND + COND:ID

is equivalent to the shorter

ID*COND

Now the fitting code.

library(lme4)

out_long_list <- split(out_long, out_long$signals)

i <- sapply(out_long_list, nrow) != 0
models_list <- lapply(out_long_list[i], function(DF){
  tryCatch(lmer(value ~ COND + (1|ID), data = DF),
           error = function(e) e)
})

lapply(model_list, summary)

A tidyverse solution could be

library(dplyr)
library(broom)

out_long %>%
  group_by(signals) %>%
  do(fit = lmer(value ~ COND + (1|ID), data = .))

Upvotes: 2

Related Questions