knitr::opts_chunk$set(echo = TRUE) library(designr)
designr is an R package to create and simulate crossed factorial designs.
Install from CRAN within R using:
install.packages("designr")
Install the development version in R using devtools
:
devtools::install_github("mmrabe/designr", build_vignettes = TRUE)
designr supports factorial designs with an arbitrary number of fixed and random factors. Fixed factors are factors for which levels are known and typically defined by the experimenter, e.g. an experimental condition or a quasi-experimental variable such as a subject’s age group. Conversely, the instances of random factors are usually not known before data collection. Examples for random factors are subjects or items in a typical psychological experiment, with the individual tested subjects and used items being the instances of those random factors.
A fixed-effects design without repeated measurement is created as easily as this:
design1 <- fixed.factor("Age", levels=c("young", "old")) + fixed.factor("Material", levels=c("word", "image")) design1
As can be seen, this experimental design requires r nobs(design1)
observations.
Assume we want to test different groups of subjects. Each subject will only be old
or young
but be tested with stimuli of both categories word
and image
. In a typical behavioral experiment, Age
would now be a between-subject/within-item factor and Material
a within-subject/between-item factor. In other words, Material
is now nested within the instances of Subject
, whereas Subject
is grouped by Age
.
design2 <- fixed.factor("Age", levels=c("young", "old")) + fixed.factor("Material", levels=c("word", "image")) + random.factor("Subject", groups = "Age") design.codes(design2)
The minimal experimental design will still require r nobs(design2)
observations, assigning one subject to each level of the between-subject factor Age
.
Note that design1
is nested within design2
. This means that instead of defining design2
like we did above, we can also derive it from the existing design1
by adding the random factor Subject
like so:
design2 <- design1 + random.factor("Subject", groups = "Age")
Oftentimes, experiments will have more than one random factor, for example Subject
and Item
. This is because items in behavioral experiments are often prepared upfront and not randomly generated upon presentation. In that case we would like to make sure that each item is presented equally often across all subjects and within-item conditions. Suppose that we are extending our example from above by a second random factor Item
. Contrary to Subject
, Item
is grouped by Material
because each item can only be a word
or image
but it may be presented to both old
and young
subjects.
design3 <- fixed.factor("Age", levels=c("young", "old")) + fixed.factor("Material", levels=c("word", "image")) + random.factor("Subject", groups = "Age") + random.factor("Item", groups = "Material") design.codes(design3)
In this design, we plan to test 2 subjects, one young
and one old
, and each of them will be presented two items, an image
and a word
. The items will appear equally often in the levels of Age
and subjects will see an equal number of items in all levels of Material
.
Note that in the example above, each item really only appears once per subject. However, suppose we introduce a third fixed factor, which varies within subjects and within items, i.e. it is neither a subject nor item level fixed property. This could be something like the contrast on the screen or some other experimental manipulation that is pseudo-randomly varied for each subject and each item.
The resulting design may look something like this:
design4 <- fixed.factor("Age", levels=c("young", "old")) + fixed.factor("Material", levels=c("word", "image")) + fixed.factor("Contrast", levels=c("high", "low")) + random.factor("Subject", groups = "Age") + random.factor("Item", groups = "Material") design.codes(design4)
In a fully crossed and balanced experimental design, each item would now be presented twice per subject, once with high
and once with low
contrast. This can be absolutely legitimate, depending on the research question. In many behavioral experiments, however, the experimenter may wish to prevent the same item from being presented twice because that could introduce unwanted effects.
Essentially, what we want to do is to group each Subject
×Item
pairing by Contrast
, i.e. we want to ensure that each item assigned to a subject is only assigned in either high
or low
contrast. We can therefore add the interaction of Subject
and Item
as a random factor, grouped by Contrast
:
design5 <- fixed.factor("Age", levels=c("young", "old")) + fixed.factor("Material", levels=c("word", "image")) + fixed.factor("Contrast", levels=c("high", "low")) + random.factor("Subject", groups = "Age") + random.factor("Item", groups = "Material") + random.factor(c("Subject","Item"), groups = "Contrast") design.codes(design5)
The design now contains r nobs(design5)
planned observations for 4 subjects and 4 items. Each subject will be presented each item exactly once and an equal number of items (1) in each combination of Material
×Contrast
. Moreover, each item will be presented equally often in each combination of Age
×Contrast
.
For a more detailed example, see the design-to-dataframe
vignette (by executing vignette("design-to-dataframe")
) and the manual pages of the package.
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.