knitr::opts_chunk$set( collapse = TRUE, comment = "#>", fig.path = "README-" )
Analysis of Two-Way Tables a la Tukey
Version: 0.6.3
The twoway
package provides analysis and graphical methods for two-way tables with one observation per cell,
most typically used in an Analysis of Variance (ANOVA) context.
The methods follow Tukey (1949), "One Degree of Freedom for Non-additivity", explained more concretely in Tukey (1972), Exploratory Data Analysis,
but the graphical ideas are more interesting and general:
The goal of the package is to introduce these ideas in R, and allow further development. This R implementation is based on my SAS macro, twoway.sas.
This currently released package can be installed directly from CRAN:
install.packages("twoway")
You can install the development version twoway
from github with:
# install.packages("devtools") devtools::install_github("friendly/twoway")
A trivial example shows the analysis of a 3 x 3 table, containing mean reaction times for three subjects presented with three types of sentences and asked to judge whether the sentence was TRUE or FALSE. The questions are:
library(twoway) data("sentRT") sentRT
The twoway()
function gives the basic analysis: a decomposition of the two-way table, giving the:
sent.2way <- twoway(sentRT) print(sent.2way)
twoway()
also allows for a robust fitting by row and column medians, using Tukey's idea of median polish, as implemented
in stats::medpolish()
. This uses method="median"
in the call to twoway()
.
print(twoway(sentRT, method="median"), border=2)
The result is much simpler, in that most residuals are closer to 0, and one large one stands out.
The plot method for twoway
objects currently provides two types of plots:
which="fit"
)which="diagnose"
). plot(sent.2way)
The diagnostic plot shows the regression of residuals under the additive model against the comparison values under the additive model. If the points in this plot are reasonably linear and have a non-zero slope, b, a suggested power transformation of the response to $x^{1-b}$ will often remove non-additivity.
plot(sent.2way, which="diagnose")
There is an opposite-corner pattern to the residuals in the analysis by means. In the diagnostic plot, the positive slope, $b=1.6$ suggests a power transformation $x^{1-b} = x^{-0.6}$, which can be taken as close to $1 / \sqrt{x}$. Alternatively, reaction time data is often more easily analyzed by classical methods and the results more easily understood in terms of response speed, using the transformation $1/x$.
In the present version, the anova()
method for a "twoway"
object gives the results of two analyses: one for the additive
model, and one for the model allowing 1 df for non-additivity. Both of these assume
that row and column effects are fit using means (method="mean"
).
anova(sent.2way)
There is a variety of other tests for additivity in twoway tables with $n=1$ per cell. A number of these are implemented in the additivityTests package.
as.data.frame()
method for a "twoway"
object gives a tidy data.frame result, containing the components of the fitted
values and other quantities.as.data.frame(sent.2way)
to_long()
and to_wide()
facilitate conversion between matrix format and a long format
with variables such as Row
, Col
and Value
.This package is at a middle stage of development. There are some small TODOs scattered throughout the code. In addition:
Row
and Col
.Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.