6. Problem
Want to make it very easy to make these
types of display - should be able to create
as easy as doing as simple numerical test.
With the right tools, it turns out to be really
easy!
Two basic problems: how to do
resampling under the null, and how to
deﬁne the graphic.
7. Resampling
Many resampling methods have
particularly elegant expression in R code.
For example, if null hypothesis is that of
independence:
df <- transform(df, resp = sample(resp))
However, better to generalise the pattern.
9. Advantages
Separate description from
implementation.
Call describes action.
Can later replace implementation if better
approach discovered.
Unimportant details concealed.
10. n resamples
Each method gives us a single resample,
but we need n.
Trivial to repeat n times with rdply() from
the plyr package.
(rdply() is a generalisation of
replicate() that returns a data frame
with a column that labels each replicate.)
11. Overall
resamp <- function(true, method, n = 19, pos = sample(n + 1, 1)) {
samples <- rdply(n, method(true))
if (missing(pos)) {
message(quot;True data in position quot;, pos)
}
add_true(samples, true, pos)
}
add_true <- function(samples, true, n) {
samples$.n <- with(samples, ifelse(.n >= n, .n + 1, .n))
true$.n <- n
all <- rbind(samples, true)
all[order(all$.n), ]
}
Will see application shortly
12. Other nulls
Need functions for other common null
hypotheses.
Experimental null_model(), which
computes rotation residuals given a
speciﬁed linear model. (Demo a little
later)
13. Display
How is the plot of the simulated data with
true data different from a single plot?
Just need to repeat the same display n
times and label appropriately.
This is easy if we have a description of
the plot, independent of the data.
14. Grammar of Graphics
This is one principle of the grammar of
graphics: should describe the graphic we
want, not how to create it.
Implemented with the ggplot2 package in
R.
Easy to modify a graphic after it has been
created. For graphical inference, we need
to change the data and facetting.
Be the first to comment