##### Example 1 : single repeated measure design #####
# Outcome measure is mean response time (MRT), measured in two conditions
# with 4 participants. All participants participate in both conditions.
MRT_cond1 <- c( 415,500,478,550 ) # response times in condition 1
MRT_cond2 <- c( 455,532,499,602 ) # response times in condition 2
id <- 1:4 # id variable
# Note that all repeated measures variable names are formatted in the
# "right" format: the name of the outcome variable (MRT), followed by a
# separator character (_) and then the value on the repeated measures
# factor (e.g. cond1). Similarly, note that the separator character
# does not appear anywhere else in the names; including the names of
# the between subject variable (i.e. id).
expt <- data.frame( id, MRT_cond1, MRT_cond2 ) # convert to data frame
# This is what the expt data frame looks like:
#
# id MRT_cond1 MRT_cond2
# 1 1 415 455
# 2 2 500 532
# 3 3 478 499
# 4 4 550 602
#
# This is a standard "wide" form data frame.
# --- Example 1.1 --- Extracting the repeated measures structure from
# the wide form data frame is straightforward in this case, because all
# the variable names match the defaults
wideRM( expt )
# This is what the output looks like:
#
# $within
# wide.name measure treatment.1
# 1 MRT_cond1 MRT cond1
# 2 MRT_cond2 MRT cond2
#
# $between
# [1] "id"
#
# The equivalent command with all input arguments specified manaully
# looks like this:
wideRM( data = expt, # the data frame
treatments = "treatment.1", # name of the within-subjects treatment
sep = "_" # the separator string used in the names
)
# --- Example 1.2 --- Converting the wide form data frame to the long form
# data frame is equally simple, again because the variable names have been
# specified in the "right" format:
wideToLong( expt )
# Here's the output:
#
# id MRT treatment.1
# 1 1 415 cond1
# 2 1 455 cond2
# 3 2 500 cond1
# 4 2 532 cond2
# 5 3 478 cond1
# 6 3 499 cond2
# 7 4 550 cond1
# 8 4 602 cond2
# And the equivalent command with all arguments specified manually:
wideToLong( data = expt, # the data frame
rms = wideRM( expt ) # the repeated measures structure
)
# --- Example 1.3 --- Conversion from long form to wide form is
# straightforward as long as we have the repeated measures structure
# available to us. However, constructing the repeated measures
# structure from a long form data frame is slightly more tedious.
# First, we need a long form data frame:
expt2 <- wideToLong( expt )
# This is the same long form data frame above. We can create the
# repeated measures structure as follows:
expt2.rms <- longRM( data = expt2, # data frame
treatments = "treatment.1", # within subjects treatments
measures = "MRT", # measured variables (outcomes)
between = "id", # between subjects variables
sep = "_" # separator character
)
# This produces the exact same repeated measures structure that we
# obtained from the wideRM(expt) command earlier.
# Now that we have a repeated measures structure specified, the
# reshaping is straightforward:
longToWide( data = expt2, rms = expt2.rms )
# The output here is identical to the wide form data frame that we
# started with.
# --- Example 1.4 --- Conversion from wide form to a "multivariate" form.
# This is useful for multivariate linear models. As before, it's easy in
# this case because the names in the wide form data are structured the
# way we need it:
wideToMV( expt )
wideToMV( data = expt, rm = wideRM(expt) ) # equivalent command
# Here's the output:
#
# $MRT
# MRT_cond1 MRT_cond2
# [1,] 415 455
# [2,] 500 532
# [3,] 478 499
# [4,] 550 602
#
# $id
# [1] 1 2 3 4
#
##### Example 2 : two treatments and two measures #####
# A more complex, but more realistic, version of the experiment. Again, we have only
# four participants, but now we have two different outcome measures, mean response
# time (MRT) and the proportion of correct responses (PC). Additionally, we have two
# different repeated measures variables. As before, we have the experimental condition
# (cond1, cond2), but this time each participant does both conditions on two different
# days (day1, day2). Finally, we have multiple between-subject variables too, namely
# id and gender.
# response times across both conditions and both days:
MRT_cond1_day1 <- c( 415,500,478,550 )
MRT_cond2_day1 <- c( 455,532,499,602 )
MRT_cond1_day2 <- c( 400,490,468,502 )
MRT_cond2_day2 <- c( 450,518,474,588 )
# proportion of correct reponses in both conditions and days:
PC_cond1_day1 <- c( 79,83,91,75 )
PC_cond2_day1 <- c( 82,86,90,78 )
PC_cond1_day2 <- c( 88,92,98,89 )
PC_cond2_day2 <- c( 93,97,100,95 )
# between subjects variables
id <- 1:4
gender <- factor( c("male","male","female","female") )
# create wide form data frame
expt3 <- data.frame( id, gender,
MRT_cond1_day1, MRT_cond1_day2, MRT_cond2_day1, MRT_cond2_day2,
PC_cond1_day1, PC_cond1_day2, PC_cond2_day1, PC_cond2_day2
)
# Here's the wide form data frame:
#
# id gender MRT_cond1_day1 MRT_cond1_day2 MRT_cond2_day1 MRT_cond2_day2 PC_cond1_day1 PC_cond1_day2 PC_cond2_day1 PC_cond2_day2
# 1 1 male 415 400 455 450 79 88 82 93
# 2 2 male 500 490 532 518 83 92 86 97
# 3 3 female 478 468 499 474 91 98 90 100
# 4 4 female 550 502 602 588 75 89 78 95
# Extracting the repeated measures structure from the variable names:
wideRM( expt3 )
# Output:
#
# $within
# wide.name measure treatment.1 treatment.2
# 1 MRT_cond1_day1 MRT cond1 day1
# 2 MRT_cond1_day2 MRT cond1 day2
# 3 MRT_cond2_day1 MRT cond2 day1
# 4 MRT_cond2_day2 MRT cond2 day2
# 5 PC_cond1_day1 PC cond1 day1
# 6 PC_cond1_day2 PC cond1 day2
# 7 PC_cond2_day1 PC cond2 day1
# 8 PC_cond2_day2 PC cond2 day2
#
# $between
# [1] "id" "gender"
#
# Conversion to long form:
wideToLong( expt3 )
# Output:
#
# id gender MRT PC treatment.1 treatment.2
# 1 1 male 415 79 cond1 day1
# 2 1 male 400 88 cond1 day2
# 3 1 male 455 82 cond2 day1
# 4 1 male 450 93 cond2 day2
# 5 2 male 500 83 cond1 day1
# 6 2 male 490 92 cond1 day2
# 7 2 male 532 86 cond2 day1
# 8 2 male 518 97 cond2 day2
# 9 3 female 478 91 cond1 day1
# 10 3 female 468 98 cond1 day2
# 11 3 female 499 90 cond2 day1
# 12 3 female 474 100 cond2 day2
# 13 4 female 550 75 cond1 day1
# 14 4 female 502 89 cond1 day2
# 15 4 female 602 78 cond2 day1
# 16 4 female 588 95 cond2 day2
#
# Conversion to multivariate form:
wideToMV( expt3 )
# Output:
#
# $MRT
# MRT_cond1_day1 MRT_cond1_day2 MRT_cond2_day1 MRT_cond2_day2
# [1,] 415 400 455 450
# [2,] 500 490 532 518
# [3,] 478 468 499 474
# [4,] 550 502 602 588
#
# $PC
# PC_cond1_day1 PC_cond1_day2 PC_cond2_day1 PC_cond2_day2
# [1,] 79 88 82 93
# [2,] 83 92 86 97
# [3,] 91 98 90 100
# [4,] 75 89 78 95
#
# $id
# [1] 1 2 3 4
#
# $gender
# [1] male male female female
# Levels: female male
#
Run the code above in your browser using DataLab