我想在多级模型中计算调节分析。我有 20 个任务块(1 级),供 33 名参与者(2 级)使用。对于我的相关 IV 和 DV,我已经编写了一个有效的循环:
for(i in 1:length(participants)) {
participant.sub <- df[df$participant == participants[i],]
participant.sub <- participant.sub[!is.na(participant.sub$participant),]
for (j in 1:20) {
block.sub <- participant.sub[participant.sub$block == j,]
data.row <- c(unique(block.sub$participant),
j,
block.sub$slider_mentaleffort.response[!is.na(block.sub$slider_mentaleffort.response)],
block.sub$goal[!is.na(block.sub$goal)],
block.sub$sum_correct[!is.na(block.sub$sum_correct)])
data.row <- data.row[2:6]
dummy.df <- rbind(dummy.df, data.row)
}
}
现在我想把主持人也包括进来。调节变量是在实验开始前得到回答的李克特量表。每个人在每个尺度上都有一个确切的值。如何将这些值添加到当前循环/数据帧,以便我可以继续 lmer?
我试过这个
for(i in 1:length(participants)) {
participant.sub <- df[df$participant == participants[i],]
participant.sub <- participant.sub[!is.na(participant.sub$participant),]
for (j in 1:20) {
block.sub <- participant.sub[participant.sub$block == j,]
data.row <- c(unique(block.sub$participant),
j,
block.sub$slider_mentaleffort.response[!is.na(block.sub$slider_mentaleffort.response)],
block.sub$goal[!is.na(block.sub$goal)],
block.sub$sum_correct[!is.na(block.sub$sum_correct)],
block.sub$slider_effort.response[!is.na(block.sub$slider_effort.response)],
block.sub$slider_talent.response[!is.na(block.sub$slider_talent.response)],
block.sub$slider_luck.response[!is.na(block.sub$slider_luck.response)],
block.sub$slider_taskdiff.response[!is.na(block.sub$slider_taskdiff.response)])
data.row <- data.row[2:9]
dummy.df <- rbind(dummy.df, data.row)
}
}
和
for(i in 1:length(participants)) {
participant.sub <- df[df$participant == participants[i],]
participant.sub <- participant.sub[!is.na(participant.sub$participant),]
for (j in 1:20) {
block.sub <- participant.sub[participant.sub$block == j,]
data.row <- c(unique(block.sub$participant),
j,
block.sub$slider_mentaleffort.response[!is.na(block.sub$slider_mentaleffort.response)],
block.sub$goal[!is.na(block.sub$goal)],
block.sub$sum_correct[!is.na(block.sub$sum_correct)],
block.sub$slider_effort.response,
block.sub$slider_talent.response,
block.sub$slider_luck.response,
block.sub$slider_taskdiff.response)
data.row <- data.row[2:9]
dummy.df <- rbind(dummy.df, data.row)
}
}
但是没有效果。
最好使用 dpylr 并将数据切片到我想要的行中,然后将它们合并到一帧中,这样就可以排除 na 的
df_attitude <- df %>%
filter(!is.na(slider_talent.response)) %>%
select(participant, slider_talent.response, slider_luck.response, slider_effort.response, slider_taskdiff.response)
data <- data %>%
merge(., df_attitude, by = "participant")
df_effort <- df %>%
filter(!is.na(slider_mentaleffort.response)) %>%
select(participant, block, slider_mentaleffort.response, sum_correct)
df_goal <- df %>%
filter(!is.na(goal)) %>%
select(participant, block, goal)
这是比循环更优雅、更灵活、更短的方式。