shuffle: Shuffle Trial Order

Description Usage Arguments Details Value Examples

View source: R/simulation.R

Description

Randomly shuffle the order that trials appear for a set of participants.

Usage

1
shuffle(n_subj, n_obs)

Arguments

n_subj

Number of subjects.

n_obs

Number of observations per subject.

Details

This is the default function used to perform the randomization of within-subject factor labels when generating simulated data using sim_2x2. The original, unrandomized dataset is ordered by the variables B, subj_id, A, such that the first half of subjects are in B1 and the second half in B2, and the first half of the observations for subject i are in condition A1 and the last half are in condition A2. This function generates indices to use to re-order the trials randomly for each subject. It is simply a wrapper around the function replicate(n_subj, sample(seq_len(n_obs)), simplify=FALSE).

It is possible to write your own function to create pseudorandom series. Any user-defined function must have the same formal arguments as this function in the same order, and return a list of integer vectors, with each vector of length n_obs, and the list itself of length n_subj.

Value

An unnamed list of length n_subj, with each element of that list being a vector of length n_obs containing the position number for each trial.

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
shuffle(4, 8)

sim_2x2(4, 8, rand_fn="shuffle") ## the default for sim_2x2

## user-defined randomization function
my_shuffle <- function(n_subj, n_obs) {
  odds <- seq(1, n_obs, 2)
  evens <- seq(2, n_obs, 2)
  replicate(n_subj,
            if (sample(c(FALSE, TRUE), 1)) {
              c(odds, evens)
            } else {
              c(evens, odds)
            },
            simplify=FALSE)
}

my_shuffle(4, 8)
sim_2x2(4, 8, rand_fn="my_shuffle")

dalejbarr/autocorr documentation built on March 27, 2021, 3:03 a.m.