我需要为以下数据集拟合具有ID和GROUP (COND)变量的混合模型:
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
其中信号变量不是一个预测变量,而只是一个名义变量。由于有12个信号王,并且每个信号王对应一个特定的值范围到旁边的列(值),我想知道是否通过使用这个数据长设置可以通过使用COND和ID作为固定和随机效应来运行lmer()函数,通过迭代函数(例如for loop, map(), apply()函数等)。如果不这样做,这将应该如何编写迭代分析的数据集作为一个广泛的格式?
如果可能的话,我想通过每个拟合模型提取迭代诊断图(如果包括)。谢谢那些愿意回答的人。
这里的数据集
> 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"))
>
请让我知道你是否需要宽格式数据集。
下面可能会为每个信号运行模型。与发布的数据,它给出了一个错误(见你的这篇文章),说明有太少的观察。
注意公式
ID + COND + COND:ID
等价于较短的
ID*COND
现在是拟合代码。
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)
tidyverse
解可能为
library(dplyr)
library(broom)
out_long %>%
group_by(signals) %>%
do(fit = lmer(value ~ COND + (1|ID), data = .))