以随机顺序 RStudio 创建 2 个块的模拟数据

问题描述 投票:0回答:1

对于我的硕士论文的功效分析,我需要根据我的研究设计创建一个模拟数据集。 我的代码反映了我的研究计划设计,但现在我的主管告诉我改变一些方面。

set.seed(1234)

n_participants <- 50
n_stories <- 8
  simulated_data <- data.frame(
    subj = rep(1:n_participants, each = n_stories),
    story = rep(1:n_stories, times = n_participants),
    group = rep(sample(c(-1, 1), size = n_participants, replace = TRUE), each = n_stories),
    target = rep(sample(c(-1, 1), size = n_participants * n_stories, replace = TRUE), each = 1),
    action = rep(sample(c(-1, 1), size = n_participants * n_stories, replace = TRUE), each = 1),
    interc = rep(b0,n_participants * n_stories)
  )

首先,对于目标,我将向参与者展示 8 个故事,但分成 2 个块,每块 4 个。块的顺序应该是随机的,并且在每个块中,目标值应该只有 -1 或 1。 此外,我需要确保每个受试者看到每个行动值正好 4 次。因此,每个受试者都会看到 4x -1 和 4 乘以 1,但顺序是随机的。 我不是 R 专业人士,因此我似乎无法弄清楚。你能帮我解决这个问题吗?

对于目标部分,我尝试了这段代码,但是每个参与者只能看到一种类型的目标,这是我想避免的。

set.seed(123)

n_participants <- 50
n_stories <- 8

target_pattern <- rep(c(rep(c(1, 1, 1, 1, -1, -1, -1, -1), each = 1)), length.out = n_participants)

target_pattern <- sample(target_pattern, replace = TRUE)

target_order <- rep(target_pattern, each = n_stories)

simulated_data <- data.frame(
  subj = rep(1:n_participants, each = n_stories),
  story = rep(1:n_stories, times = n_participants),
  group = rep(sample(c(-1, 1), size = n_participants, replace = TRUE), each = n_stories),
  action = rep(sample(c(-1, 1), size = n_participants * n_stories, replace = TRUE), each = 1),
  target = target_order,
  interc = rep(b0, n_participants * n_stories)
)
r dataset simulation
1个回答
0
投票

您可以调整

target
,使其看起来像这样:

c(replicate(n = n_participants, rep(sample(c(-1,1)),each=4)))

你可以调整

action
,使它看起来像这样

c(replicate(n = n_participants, sample(c(rep(-1,4),rep(1,4)))))
© www.soinside.com 2019 - 2024. All rights reserved.