\documentclass[a4paper]{report}

\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{RJournal_edited}
\usepackage{amsmath,amssymb,array}
\usepackage{booktabs}
\usepackage{bm,subcaption,amsfonts}

\newcommand{\argmin}{\text{argmin}}
\newcommand{\argmax}{\text{argmax}}
\newcommand{\tr}{\text{tr}}
\newcommand{\Log}{\text{Log}}
\newcommand{\Rbb}{\mathbb{R}}
\newcommand{\Edist}{d_E}
\newcommand{\Rdist}{d_R}
\newcommand{\ProjMean}{\widehat{\bm S}_E}
\newcommand{\ProjMedian}{\widetilde{\bm S}_E}
\newcommand{\GeomMean}{\widehat{\bm S}_R}
\newcommand{\GeomMedian}{\widetilde{\bm S}_R}

%\VignetteIndexEntry{Introduction to rotations}
%\VignetteDepends{knitr,onion,orientlib}
%\VignetteEngine{knitr::knitr}


\begin{document}

<<include=FALSE>>=
library(knitr)
opts_chunk$set(
concordance=TRUE
)
@


<<setup,include=FALSE>>=
orig_options <- options()
options(digits=3)
library(knitr)
opts_chunk$set(
  concordance = TRUE, 
  fig.path = 'figure/', 
  fig.width = 5, 
  fig.height = 5, 
  fig.show = 'hold', 
  fig.align = 'center'
)
knit_hooks$set(document = function(x) {
  sub('\\usepackage[]{color}', '\\usepackage{xcolor}', x, fixed = TRUE)
})
library(rotations)
@

%% do not edit, for illustration only
\sectionhead{}
\volume{}
\volnumber{}
\year{}
\month{}
\begin{article}
%% replace RJtemplate with your article
\title{\pkg{rotations}: An R Package for $SO(3)$ Data}
\author{by Bryan Stanfill, Heike Hofmann, Ulrike Genschel}

\maketitle

\abstract{
In this article we introduce the \pkg{rotations} package which provides users with the ability to simulate, analyze and visualize three-dimensional rotation data.  More specifically it includes four commonly used distributions from which to simulate data, four estimators of the central orientation, six confidence region estimation procedures and two approaches to visualizing rotation data.  All of these features are available for two different parametrizations of rotations: three-by-three matrices and quaternions.  In addition, two datasets are included that illustrate the use of rotation data in practice.
}

\section{Introduction}

Data in the form of three-dimensional rotations have applications in many scientific areas, such as bio-medical engineering, computer vision, and geological and materials sciences where such data represent the positions of objects within a three-dimensional reference frame.  For example, \cite{humbert1996}, \cite{bingham2009} and \cite{bachmann2010} apply rotation data to study the orientation of cubic crystals on the surfaces of metal.  \cite{rancourt2000} use rotations to represent  variations in human movement while performing a task.

A common goal shared in the analysis of rotation data across all fields is to estimate the main or central orientation for a sample of rotations.
More formally, let  $SO(3)$ denote the  rotation group, which consists of all real-valued $3\times 3$  matrices $\bm R$ with determinant equal to +1.
Then observations $\bm{R}_1,\ldots,\bm{R}_n \in SO(3)$ can be conceptualized as a random sample from a \dfn{location model}
\begin{equation}
\label{eqn:loc_model}
\mathbf{R}_i = \bm{S} \bm{E}_i, \quad i=1,\ldots,n,
\end{equation}
where $\bm S \in SO(3)$ is the {\it fixed} parameter of interest indicating the central orientation, and $\bm{E}_1,\ldots,\bm{E}_n \in SO(3)$ denote i.i.d.\ {\it random} rotations which symmetrically perturb $\bm{S}$.  Model \eqref{eqn:loc_model} is a rotation-matrix analog of a location model for scalar data $Y_i = \mu + e_i$, where $\mu \in \mathbb{R}$ denotes a mean and $e_i \in \mathbb{R}$ denotes an additive error symmetrically distributed around zero.

Assuming the perturbations $\bm E_i$ symmetrically perturb $\bm S$ implies that the observations $\bm R_i$ have no preferred direction relative to $\bm S$ and that $E\left(\bm R_i\right)=c\bm S$ for some $c\in\mathbb{R}^+$ for all $i$.  Also note that under the symmetry assumption, \eqref{eqn:loc_model} could be equivalently specified as $\bm R_i=\bm E_i\bm S$, though the form given in \eqref{eqn:loc_model} is the most common form in the literature (see \citealt{bingham2009} for details).

While there is a multitude of packages and functions available in R to estimate the mean in a location model, the toolbox for rotational data is limited.  The \CRANpkg{orientlib} \citep{murdoch2003} package includes the definition of an orientation class along with a few methods to summarize and visualize rotation data.  A strength of the \pkg{orientlib} package is its thorough exploration of rotation representations, but the estimation and visualization techniques are lacking and no methods for inference are available.  The \CRANpkg{onion} \citep{hankin2011} package includes functions for rotation algebra but only the quaternion form is available and data analysis is not possible.  The \pkg{uarsbayes}  \citep{qu2013} package includes functions for data generation and Bayes inference but this package is currently not publicly available.  Packages for circular and spherical data, e.g.\ \CRANpkg{circular} \citep{agostinelli2011} and \CRANpkg{SpherWave} \citep{oh2013}, can possibly be used but their extension to rotation data is not straightforward.

The \CRANpkg{rotations} \citep{stanfill2014} package fills this void by providing users with the tools necessary to simulate rotations from \eqref{eqn:loc_model} with four distribution choices for the perturbation matrices $\bm E_i$.  Estimation and inference for $\bm{S}$ in \eqref{eqn:loc_model} is available along with two visualization techniques.  The remainder of this manuscript introduces rotation data more fully and discusses the ways they are handled by the \pkg{rotations} package.  For the latest on this package as well as a full list of available functions, see \code{help(package = "rotations")}.

\section{Rotation parametrizations}

Several parametrizations of rotations exist. We consider two of the most commonly used: orthogonal $3\times 3$ matrices with determinant one and four-dimensional unit vectors called \dfn{quaternions}.  The \pkg{rotations} package allows for both parametrizations as input as well as transforming one into the other. We will briefly discuss each:

\subsection{Matrix form}\label{subsec:matrix}

Rotations in three-dimensions can be represented by $3\times3$ orthogonal matrices with determinant one.  Matrices with these characteristics form a group called the \dfn{special orthogonal group}, or \dfn{rotation group}, denoted $SO(3)$.  Every element in $SO(3)$ is associated with a skew-symmetric matrix $\bm\Phi\left(\bm W\right)$ where
\[
\bm{\Phi}\left(\bm{W}\right) = \left(\begin{array}{ccc}0 & -w_3 & w_2 \\ w_3 & 0 & -w_1 \\-w_2 & w_1 & 0\end{array}\right)
\]
and $\bm W\in\mathbb{R}^3$.  Applying the exponential operator to the matrix $\bm\Phi\left(\bm W\right)$ results in the rotation $\bm R$
\begin{equation}\label{eqn:expW}
  \bm R=\exp\left[\bm{\Phi}\left(\bm{W}\right)\right] = \sum\limits_{k=0}^\infty \frac{\left[\bm{\Phi}\left(\bm{W}\right)\right]^k}{k!}.
  \end{equation}
Since $\bm\Phi\left(\bm W\right)$ is skew-symmetric, it can be shown that \eqref{eqn:expW} reduces to
\begin{equation}\label{eqn:angleAxis}
\bm R =\cos(r)\bm{I}_{3\times3} + \sin(r) \bm{\Phi}\left(\bm{U}\right) + [1-\cos (r)] \bm{U} \bm{U}^\top,
\end{equation}
where $r=\|\bm{W}\|$, $\bm{U} =\bm{W}/\|\bm{W}\|$.  In the material sciences literature $r$ and  $\bm U\in\Rbb^3$ are termed the \dfn{misorientation angle} and \dfn{misorientation axis}, respectively.

Given a rotation matrix $\bm R$ one can find the associated skew-symmetric matrix $\bm{\Phi}\left(\bm W\right)$ by applying the logarithm operator defined by
\begin{equation}\label{eqn:matLog}
\Log\left(\bm R\right)=\begin{cases}
\bm 0 &\text{if } \theta=0\\
\frac{r}{2\sin r}\left(\bm R-\bm R^\top\right)&\text{otherwise,}
\end{cases}
\end{equation}
where $r\in[-\pi,\pi)$ satisfies $\tr\left(\bm R\right)=1+2\cos r$ and $\mathbf{tr}(\cdot)$ denotes the trace of a matrix.  For more on the correspondence between $SO(3)$ and skew-symmetric matrices see \cite{stanfill2013}.

The \pkg{rotations} package defines the \code{S3} class \code{"SO3"}, which internally stores a sample of $n$ rotations as a $n\times9$ matrix.  If $n=1$ then an object of class \code{"SO3"} is printed as a $3\times3$ matrix but for $n>1$ the $n\times9$ matrix is printed.  Objects can be coerced into, or tested for the class \code{"SO3"} with the \code{as.SO3} and \code{is.SO3} functions, respectively.  Any object passed to \code{is.SO3} is tested for three characteristics: dimensionality, orthogonality and determinant one.

The \code{as.SO3} function coerces the input into the class \code{"SO3"}.  There are three types of input supported by the \code{as.SO3} function.  Given a singe angle $r$ and axis $\bm U$, \code{as.SO3} will form a rotation matrix according to \eqref{eqn:angleAxis}. Equivalently one could supply a three-dimensional vector $\bm W$, then the length of that vector will be taken to be the angle of rotation $r=\|\bm W\|$ and the axis is taken to be the unit-vector in the direction of $\bm W$, i.e.\ $\bm U=\bm W/\|\bm W\|$.  One can also supply a rotation $\bm Q$ in the quaternion representation. The \code{as.SO3} function will return the matrix equivalent of $\bm Q$.  For all input types the function \code{as.SO3} returns an $n\times9$ matrix of class \code{"SO3"} where each row corresponds to a rotation matrix.    Below we illustrate the use of the \code{as.SO3} function by constructing the $3\times3$ matrix associated with a $90^\circ$ rotation about the $y$-axis, i.e.\ $r=\pi/2$ and $\bm U=(0,1,0)$.  In this example and all that follow, we have rounded the output  to three digits for compactness.

<<ex2>>=
r <- pi/2
U <- c(0, 1 ,0)
W <- U*r
R <- as.SO3(W)
R
identical(R, as.SO3(U, r))
@

Given a rotation matrix $\bm R$, the functions \code{mis.angle} and \code{mis.axis} will determine the misorientation angle and axis of an object with class \code{"SO3"} as illustrated in the next example.
<<ex3>>=
mis.angle(R)*2/pi
mis.axis(R)
@


\subsection{Quaternion form}

Quaternions are unit vectors in $\mathbb{R}^4$ that are commonly written as
\begin{equation}\label{eqn:quat}
\bm Q = x_1 + x_2 i + x_3 j + x_4 k,
\end{equation}
where $x_l\in[-1,1]$ for $l=1,2,3,4$ and $i^2 = j^2= k^2 =ijk= -1$.  We can write $\bm Q=\left(s,\bm V\right)$ as tuple of the scalar $s$ for coefficient $\bm 1$ and vector $\bm V$ for the remaining coefficients, i.e.\ $s=x_1$ and $\bm V= (x_2, x_3, x_4)$.

A rotation around axis $\bm U$ by angle $r$ translates to $\bm Q=\left(s,\bm V\right)$ with
\[
s = \cos{(r/2)},  \ \ \bm V = \bm U \sin {(r/2)}.
\]
Note that rotations in quaternion form are over-parametrized: $\bm Q$ and $-\bm Q$ represent equivalent rotations.  This ambiguity has no impact on the distributional models, parameter estimation or inference methods to follow.  Hence, for consistency, the \pkg{rotations} package only generates quaternions satisfying $x_1\geq0$.  Data provided by the user does not need to satisfy this condition however.

The \code{S3} class \code{"Q4"} is defined for the quaternion representation of rotations. All the functionality of the \code{"SO3"} class also exists for the \code{"Q4"} class, e.g.\ \code{is.Q4} and \code{as.Q4} will test for and coerce to class \code{"Q4"}, respectively.  Internally, a sample of $n$ quaternions is stored in the form of a $n\times4$ matrix with each row a unit vector. Single quaternions are printed according to the representation in \eqref{eqn:quat} (see example below) while a sample of size $n$ is printed as a $n\times 4$ matrix with column names \code{Real}, \code{i}, \code{j} and \code{k} to distinguish between the four components.

The following code creates the same rotation from the previous section in the form of a quaternion with the \code{as.Q4} function.  This function works much the same way as the \code{as.SO3} function in terms of possible inputs but returns a vector of length four of the class \code{"Q4"}.

<<ex4>>=
as.Q4(U, r)
as.Q4(as.SO3(U, r))
@

\section{Data generation\label{section:generation}}

If the rotation $\bm{E}_i\in SO(3)$ from \eqref{eqn:loc_model} has an axis $\bm U$ that is uniformly distributed on the unit sphere and an angle $r$ that is independently distributed about zero according to some symmetric distribution function then $\bm E_i$ is said to belong to the \dfn{uniform-axis random spin}, or \dfn{UARS}, class of distributions.  From \cite{bingham2009} the density for $\bm E_i$ is given by
\begin{equation}\label{eq:uarsden}
f\left(\bm E_i|\kappa\right)=\frac{4\pi}{3-\tr\left(\bm E_i\right)}C\left(\left.\text{acos}\left\{\frac{\tr(\bm E_i)-1}{2}\right\}\right|\kappa\right),
\end{equation}
where $C(\cdot|\kappa)$ is the distribution function associated with the angle of rotation $r$ with concentration parameter $\kappa$.  Members of the UARS family of distributions are differentiated based on the angular distribution $C(\cdot|\kappa)$.

The \pkg{rotations} package gives the user access to four members of the UARS class.  Each member is differentiated by the distribution function for $r$: the uniform, the matrix Fisher \citep{langevin2005, downs1972, khatri1977, jupp1979}, the Cayley  \citep{Schaeben1997, leon2006} and the circular-von Mises distribution \citep{bingham2009}.  Note: probability distribution functions on $SO(3)$ such as \eqref{eq:uarsden} are defined with respect to the Haar measure, which we denote by $\lambda$.   That is, the expectation of a random rotation $\bm R\in SO(3)$ with corresponding misorientation angle $r$ is given by $E\left(\bm R\right)=\int_\Omega\bm Rf\left(\bm R|\kappa\right)\mathrm{d}\lambda$ where $\Omega=SO(3)$, $\mathrm{d} \lambda=[1-\cos(r)]\mathrm{d} r/(2\pi)$ and $\mathrm{d} r$ is the derivative of $r$ with respect to the Lebesgue measure.  Because the Haar measure acts as the uniform measure on $SO(3)$ and $\lambda\left(\Omega\right)=1$, then the angular distribution $C(r)=[1-\cos(r)]/(2\pi)$ is referred to as the uniform distribution for misorientation angles $r$ and has been included in the \pkg{rotations} package under the name \code{.haar} (see Table \ref{tab:Crforms}).

The spread of  the Cayley, matrix Fisher and circular-von Mises distributions is controlled by the concentration parameter $\kappa$.  Concentration is a distribution specific quantity and is not comparable across different distributions.  To make comparisons across distributions possible we also allow for specification of the circular variance, which is defined as $\nu=1-E[\cos(r)]$ where $E[\cos(r)]$ is often referred to as the \dfn{mean resultant length} \citep{fisher1996book}.    The form of each angular distribution along with the circular variance as a function of the concentration parameter is given in Table \ref{tab:Crforms}.


\begin{table}[h!]
\centering
\small{
\begin{tabular}{ lcccccl}\toprule
\textbf{Name}  & & \textbf{Density} $C(r |\kappa)$ & & \textbf{Circular variance $\nu$}& & \textbf{Function}\\ \midrule
Uniform  & & $\frac{1-\cos(r)}{2\pi}$ & & $\frac{3}{2}$& & \code{.haar} \\

\rule[2mm]{0mm}{6mm} Cayley  & & $\frac{\Gamma(\kappa+2)(1+\cos r)^\kappa(1-\cos r)}{2^{(\kappa+1)}\sqrt{\pi}\Gamma(\kappa+1/2)}$ & & $\frac{3}
{\kappa+2}$ & & \code{.cayley}\\

\rule[2mm]{0mm}{6mm} matrix Fisher  & & $\frac{[1-\cos(r)]\exp[2\kappa
\cos(r)]}{2\pi[\mathrm{I_0}(2\kappa)-\mathrm{I_1}(2\kappa)]}$ & &
$\frac{3\mathrm{I}_0(2\kappa)-4\mathrm{I}_1(2\kappa)+\mathrm{I}_2(2\kappa)}
{2[\mathrm{I}_0(2\kappa)-\mathrm{I}_1(2\kappa)]}$& & \code{.fisher} \\

\rule[2mm]{0mm}{6mm} circular-von Mises  & & $\frac{\exp[\kappa\cos(r)]}{2\pi \mathrm{I_0}(\kappa)}$&  &
$\frac{\mathrm{I_0}(\kappa)-\mathrm{I_1}(\kappa)}{\mathrm{I_0}(\kappa)}$& & \code{.vmises} \\[-7mm]
\rule[2mm]{0mm}{6mm} & & & & & & \\\bottomrule
\end{tabular}}
\caption{Circular densities and circular variance $\nu$; $I_i(\cdot)$ represents the modified Bessel function of order $i$ and $\Gamma(\cdot)$ is the gamma function.}  \label{tab:Crforms}
\end{table}


For a given concentration \code{d}, \code{p} and \code{r} take the same meaning as for the more familiar distributions such as \code{dnorm}.  To simulate a sample of $SO(3)$ data, the \code{ruars} function takes arguments \code{n}, \code{rangle}, and \code{kappa} to specify the sample size, angular distribution and concentration as shown below.  Alternatively, one can specify the circular variance $\nu$.  Circular variance is used in the event that both circular variance and concentration are provided.  The \code{space} argument determines the parametrization to form.   When a sample of rotations is printed then a $n\times 9$ matrix is printed with column titles that specify which element of the matrix each column corresponds to.  For example, the $\bm R_{\{1,1\}}$ element of a rotation matrix is printed under the column heading \code{R11} as illustrated below.


<<ex5>>=
Rs <- ruars(n = 20, rangle = rcayley, kappa = 1, space = 'SO3')
Qs <- ruars(n = 20, rangle = rcayley, kappa = 1, space = 'Q4')
Rs <- ruars(n = 20, rangle = rcayley, nu = 1, space = 'SO3')
Qs <- ruars(n = 20, rangle = rcayley, nu = 1, space = 'Q4')
head(Rs,3)
@

\section{Data analysis\label{section:analysis}}

In this section we present functions in the \pkg{rotations} package to compute point estimates and confidence regions for the central orientation $\bm S$.

\subsection{Estimation of central orientation}

Given a sample of $n$ observations $\bm R_1,\dots,\bm R_{n}$ generated according to \eqref{eqn:loc_model}, the \pkg{rotations} package offers four  built-in ways to estimate the central orientation $\bm S$.  These estimators are either Riemannian- or Euclidean-based in geometry and use either the $L_1$- or $L_2$- norm, i.e.\ they are median- or mean-type.  We briefly discuss how the choice of geometry affects estimation of $\bm S$.

The choice of geometry results in two different metrics to measure the distance between rotation matrices $\bm{R}_1$ and $\bm{R}_2 \in SO(3)$. The Euclidean distance, $\Edist$, between two rotations is defined by
\begin{equation*}
\Edist\left(\bm{R}_1,\bm{R}_2\right)=\left\|\bm{R}_1-\bm{R}_2\right\|_F,
\end{equation*}
where $\|\bm{A}\|_F = \sqrt{\mathbf{tr}({\bm A^\top \bm A})}$ denotes the Frobenius norm.  The Euclidean distance between two rotation matrices corresponds to the length of the shortest path in $\Rbb^{3\times3}$ that connects them and is therefore an \dfn{extrinsic} distance metric.

Estimators based on the Euclidean distance form the class of \dfn{projected} estimators.  The name is derived from the method used to compute these estimators.  That is, each estimator in this class is the projection of the the generic $3\times 3$ matrix that minimizes the loss function into $SO(3)$.  For an object with class \code{"SO3"} the \code{median} or \code{mean} function with argument \code{type = "projected"} will return a $3\times 3$ matrix in $SO(3)$ that minimizes the first- or second-order loss function, respectively.

By staying in the Riemannian space $SO(3)$ the natural distance metric becomes the Riemannian (or geodesic) distance, $\Rdist$, which for two rotations $\bm{R}_1,\bm{R}_2\in SO(3)$  is  defined as
\begin{equation*}
\Rdist\left(\bm{R}_1,\bm{R}_2\right)=  \frac{1}{\sqrt{2}}\left\|\text{Log}\left(\bm{R}_1^\top\bm{R}_2\right)\right\|_F = |r|,
\end{equation*}
where $\text{Log}(\bm{R})$ denotes the logarithm of $\bm{R}$ defined in \eqref{eqn:matLog} and $r\in[-\pi,\pi)$ is the misorientation angle of $\bm{R}_1^\top \bm{R}_2$.  The Riemannian distance corresponds to the length of the shortest path that connects $\bm{R}_1$ and $\bm{R}_2$ {\it within} the space $SO(3)$ and is therefore an \dfn{intrinsic} distance metric. For this reason, the Riemannian distance is often considered the more natural metric on $SO(3)$.  As demonstrated in \cite{stanfill2013}, the Euclidean and Riemannian distances are related by $\Edist(\bm R_1,\bm R_2)=2\sqrt{2}\sin\left[\Rdist(\bm R_1,\bm R_2)/2\right]$.

Estimators based on the Riemannian distance metric are called \dfn{geometric} estimators because they preserve the geometry of $SO(3)$.  These can be computed using the \code{mean} and \code{median} functions with the argument \code{type = "geometric"}.  Table~\ref{tab:ests.sum} summarizes the four estimators including their formal definition and how they can be computed.

The estimators in Table~\ref{tab:ests.sum} find estimates  based on minimization of $L_1$- and $L_2$-norms in the chosen geometry. The  function \code{gradient.search} provides the option to optimize for any other arbitrary minimization criterion. As the name suggests, the minimization is done along the gradient of the minimization function in the rotation space. Starting from an initial, user-specified rotation, the algorithm finds a (local) minimum by stepping iteratively in the direction of the steepest descent. Step size is regulated internally by adjusting for curvature of the minimization function.

We highlight this process  in the example below. The function \code{L1.error} is defined to minimize the intrinsic $L_1$-norm, the result from the optimization should therefore agree with the geometric median of the sample. In fact, the difference between the two results is at the same level as the minimal difference (\code{minerr}) used for convergence of the gradient search. What is gained in flexibility of the optimization is, of course, paid for in terms of speed: the built-in median function is faster by far than the gradient search.

Also illustrated in the example below is the \code{rot.dist} function, which computes the distance between two objects of class \code{"SO3"}, e.g.\ \code{R1} and \code{R2}.  The argument \code{method} specifies which type of distance to compute: the \code{"extrinsic"} option will return the Euclidean distance and the \code{"intrinsic"} option will return the Riemannian distance.  If \code{R1} is an $n\times9$ matrix representing a sample of rotations, then \code{rot.dist} will return a vector of length $n$ where the $i$th element represents the specified distance between \code{R2} and the $i$th row of \code{R1}.


<<gridsearch>>=
# error function definition
L1.error <- function(sample, Shat) {
    sum(rot.dist(sample, Shat, method = "intrinsic", p = 1))
}
cayley.sample <- ruars(n = 50, rangle = rcayley, nu = 1, space = "SO3")
# gradient based optimization
system.time(SL1 <- gradient.search(cayley.sample, L1.error))
# in-built function
system.time(S <- median(cayley.sample, type = "geometric"))
rot.dist(S, SL1$Shat)
@

\begin{table}[h]
\centering
\small
\begin{tabular}{lclclcl}
\toprule
\rule[2mm]{0mm}{1mm} \textbf{Estimator name} & & \textbf{Definition} & & \textbf{Code}\\
\midrule
Projected Mean & & $\ProjMean=\underset{\bm S\in SO(3)}{\argmin}\sum\limits_{i=1}^n d_E^2(\bm S,\bm R_i)$ & & \code{mean(Rs, type = "projected")} \\
\rule[2mm]{0mm}{6mm} Projected Median & & $\ProjMedian=\underset{\bm S\in SO(3)}{\argmin}\sum\limits_{i=1}^nd_E(\bm S,\bm R_i)$ & & \code{median(Rs, type = "projected")} \\
\rule[2mm]{0mm}{6mm} Geometric Mean & & $\GeomMean=\underset{\bm S\in SO(3)}{\argmin}\sum\limits_{i=1}^n d_R^2(\bm S,\bm R_i)$&  & \code{mean(Rs, type = "geometric")} \\
\rule[2mm]{0mm}{6mm} Geometric Median & & $\GeomMedian=\underset{\bm S\in SO(3)}{\argmin}\sum\limits_{i=1}^n\Rdist(\bm S,\bm R_i)$&  & \code{median(Rs, type = "geometric")} \\[-7mm]
\rule[2mm]{0mm}{6mm} & & & & \\ \bottomrule
\end{tabular}
\caption{A summary of the estimators included in the \pkg{rotations} package.  \code{Rs} is a sample of $n$ rotations with class \code{"SO3"} or \code{"Q4"}.}  \label{tab:ests.sum}
\end{table}

\subsection{Confidence regions\label{subsec:regions}}

Asymptotic results for the distribution of the projected mean $\ProjMean$ and median $\ProjMedian$ can be used to construct confidence regions for the central orientation $\bm S$. In the literature two approaches are available to justify the limiting distribution of the vector in $\mathbb{R}^3$ associated with the centered estimator through \eqref{eqn:expW}.  More specifically, the vector $\sqrt{n}\widehat{\bm h}$ has been shown to have a trivariate normal distribution where $\widehat{\bm h}\in\mathbb{R}^3$ satisfies
\[
\exp\left[\bm{\Phi}\left(\widehat{\bm h}\right)\right]=\bm S^\top\ProjMean.
\]
The first approach transforms a result from directional statistics while the second uses $M$-estimation theory in $SO(3)$ directly.  A summary of these methods is given next.

In the context of directional statistics, \cite{prentice1984} used results found in \cite{tyler1981} and the fact that $\ProjMean$ is a function of the spectral decomposition of $\overline{\bm R}=\sum_{i=1}^n\bm{R}_i/n$ in order to justify a multivariate normal limiting distribution for the scaled vector $\sqrt{n}\,\widehat{\bm h}$.  Unsatisfied with the coverage rate achieved by \cite{prentice1986}, \cite{fisher1996Boot} proposed a pivotal bootstrap procedure that results in coverage rates closer to the nominal level for small samples.  A transformation from unit vectors in $\Rbb^d$ to rotation matrices is required in order to apply the results of \cite{prentice1984} and \cite{fisher1996Boot} to $SO(3)$, therefore they are called \dfn{transformation-based}.  The projected median $\ProjMedian$ cannot be expressed as a function of the sample spectral decomposition, therefore this approach cannot be used to create confidence regions based on $\ProjMedian$.

It has also been shown that both estimators $\ProjMean$ and $\ProjMedian$ are $M$-estimators, which motivates a direct approach to confidence region estimation in $SO(3)$ \citep{chang2001}.  In \cite{stanfill2014b}, a pivotal bootstrap method based on the direct approach was implemented to improve coverage rates in small samples.  Because the results in \cite{chang2001} and \cite{stanfill2014b} deal with $SO(3)$ data directly, this approach is called \dfn{direct}.

The six possible confidence regions that result from these two methods are available through the wrapper function \code{region}.  They are differentiated based on the \code{method}, \code{type} and \code{estimator} arguments.  Set \code{estimator = "mean"} or  \code{estimator = "median"} to estimate a region based on $\ProjMean$ or $\ProjMedian$, respectively.  For $\ProjMean$ one can choose \code{method = "transformation"} for the transformation-based methods or \code{method = "direct"} for the direct method.  Since the transformation-based methods cannot be applied to $\ProjMedian$ an error is returned if \code{estimator = "median"} and \code{method = "transformation"} are combined.  A bootstrap version of the specified method is implemented if \code{type = "bootstrap"} or the normal limiting distribution can be chosen with\\ \code{type = "asymptotic"}.  If a bootstrap type region is specified one can additionally specify the bootstrap sample size with the \code{m} argument, which is set to 300 by default.  Regardless of the method and type chosen a single value is returned on the interval $(0,\pi]$.  This value corresponds to the radius of the confidence region centered at each of the axes of the specified estimator.

In the example code below a sample of $n=50$ rotations are drawn from the Cayley-UARS($\bm I_{3\times 3}, \kappa=10$) distribution then the four types of confidence regions based on the direct approach are constructed.  For a graphical representation of this dataset along with an interpretation of the confidence regions see Figure \ref{figure:eye1ex7}.

<<ex6>>=
Rs <- ruars(50, rcayley, kappa = 10)
region(
  x = Rs, 
  method = "direct", 
  type = "asymptotic", 
  estimator = "mean", 
  alp = 0.05
)
region(
  x = Rs, 
  method = "direct", 
  type = "bootstrap", 
  estimator = "mean", 
  alp = 0.05, 
  m = 300
)
region(
  x = Rs, 
  method = "direct", 
  type = "asymptotic", 
  estimator = "median", 
  alp = 0.05
)
region(
  x = Rs, 
  method = "direct", 
  type = "bootstrap", 
  estimator = "median", 
  alp = 0.05, 
  m = 300
)
@

\section{Visualizations}

The \pkg{rotations} package offers two methods to visualize rotation data in three-dimensions. Because rotation matrices are orthogonal, each column of a rotation matrix has length one and is perpendicular to the other axes.  Therefore each column of a rotation matrix can be illustrated as a point on the surface of a unit sphere, which represents the position of the $x$-, $y$- or $z$-axis for that rotation matrix.  Since each sphere represents one of the three axes, three spheres are required to fully visualize a sample of rotations.  Though the use of separate spheres to represent each axis can be seen as a disadvantage, the proposed visualization method makes the idea of a central orientation and a confidence region interpretable.

An existing function that can be used to illustrate rotation data is the \code{boat3d} function included in the \pkg{orientlib} package.  Given a sample of rotations, the \code{boat3d} function produces either a static or interactive three-dimensional boat to represent the provided data.  If only one rotation is of interest, the \code{boat3d} function is superior to the proposed method because it conveniently illustrates rotational data in a single image.  If multiple rotations are provided, however, the \code{boat3d} function will produce separate side-by-side boats, which can be hard to interpret.  In addition, the illustration of a estimated central orientation or a confidence region in $SO(3)$ with the \code{boat3d} function is not presently possible.

The \pkg{rotations} package can be used to produce high-quality static plots within the framework of the \CRANpkg{ggplot2} package \citep{wickham2009}.  Static plots are specifically designed for datasets that are highly concentrated and for use in presentations or publications. Alternatively, the \pkg{rotations} package can produce interactive plots using functions included in the \CRANpkg{sphereplot} package \citep{robotham2013}.  Interactive plots are designed so that the user can explore a dataset and visualize a diffuse sample.

Calling the \code{plot} function with a \code{"SO3"} or \code{"Q4"} object will result in an interactive or static sphere, differentiated by setting the argument \code{interactive} to \code{TRUE} or \code{FALSE}, respectively. The \code{center} argument defines the center of the plot and is usually set to the identity rotation \code{id.SO3} or an estimate of the central orientation, e.g.\ \code{mean(Rs)}.  The user can specify which columns to visualize with the \code{col} argument with options $1$, $2$ and $3$ representing the $x$-, $y$- and $z$- axes, respectively.  For static plots, multiple axes can be displayed simultaneously by supplying a vector to \code{col}; only one column will be displayed at a time for interactive plots.  Also available to static plots is the argument \code{to$\_$range}, which when set to \code{TRUE} will display the portion of the sphere where the observations are present.

All four estimates of the central orientation can be plotted along with a sample of rotations. Setting the argument \code{estimates$\_$show = "all"} will display all four simultaneously.  If only a few estimates are of interest then any combination of \code{"proj.mean"}, \code{"proj.median"}, \code{"geom.mean"} or \code{"geom.median"} are valid inputs.  The estimators are indicated by color and a legend is provided, see Figure \ref{figure:eye1ex7}. Finally, the \code{mean$\_$regions} and \code{median$\_$regions} options allow the user to draw a circle on the surface of the sphere representing the confidence region for that axis, centered at $\ProjMean$ and $\ProjMedian$ respectively.  If estimators are plotted along with the different regions in static plots then shapes represent the estimators and colors represent the region methods, see Figure \ref{figure:eye1ex7}, while regions and estimators are always distinguished by colors for the interactive plots.  Given the sample of rotations generated in a previous example, the example below illustrates how to produce static plots using the \code{plot} function for objects of class \code{"SO3"} and Figure \ref{figure:eye1ex7} illustrates the results of these commands.% and Figure \ref{figure:eye2} illustrates the same data with the additional argument \code{interactive=TRUE}.

<<ex7,fig.cap="The $x$-axis of a random sample from the Cayley-UARS distribution with $\\\\kappa=1$ and $n=50$. All four point estimates are displayed on the left and all three region methods along with the projected mean are on the right.",fig.lp="figure:eye1",out.width=".4\textwidth",fig.pos="h",dev='png'>>=
plot(
  x = Rs, 
  center = mean(Rs), 
  show_estimates = "all"
)
plot(
  x = Rs,
  center = mean(Rs),
  show_estimates = "proj.mean",
  mean_regions = "all",
  alp = .05
)
@

\section{Datasets}

Datasets \code{drill} and \code{nickel} are included in the \pkg{rotations} package to illustrate how the two representations of orientation data discussed here are used in practice.  The \code{drill} dataset was collected to assess variation in human movement while performing a task \citep{rancourt1995}.  Eight subjects drilled into a metal plate while being monitored by infrared cameras. Quaternions are used to represent the orientation of each subjects' wrist, elbow and shoulder in one of six positions.  For some subjects several replicates are available.  See \cite{rancourt2000} for one approach to analyzing these data.  In the example below we load the \code{drill} dataset, coerce the observations for subject one's wrist into a form usable by the \pkg{rotations} package via \code{as.Q4}, then estimate the central orientation with the projected mean.

<<ex1>>=
data(drill)
head(drill)
Subj1Wrist<-subset(drill, Subject == '1' & Joint == 'Wrist')
Subj1Wdata <- as.Q4(Subj1Wrist[,5:8])
mean(Subj1Wdata)
@

In the \code{nickel} dataset, rotation matrices are used to represent the orientation of cubic crystals on the surface of a nickel sample measured with Electron Backscatter Diffraction.  Each \code{location} on the surface of the nickel is identified by the \code{xpos} and \code{ypos} columns while the \code{rep} column identifies which of the fourteen replicate scans that measurement corresponds to.  The last nine columns, denoted \code{v1}-\code{v9}, represent the elements of the rotation matrix at that location in vector form.  See \cite{bingham2009, bingham2010b} and \cite{stanfill2013} for more details. In the example below we estimate the central orientation at location one.


<<ex21>>=
data(nickel)
head(nickel[,1:6])
Location1<-subset(nickel, location==1)
Loc1data<-as.SO3(Location1[,5:13])
mean(Loc1data)
@


\section{Summary}

In this manuscript we introduced the \pkg{rotations} package and demonstrated how it can be used to generate, analyze and visualize rotation data.  The \pkg{rotations} package is compatible with the quaternion specific \pkg{onion} package by applying its \code{as.quaternion} function to a transposed \code{"Q4"} object.  Connecting to the \pkg{onion} package gives the user access to a wide range of algebraic functions unique to quaternions.  Also compatible with the \pkg{rotations} package is the \pkg{orientlib} package, which includes additional parametrizations of rotations.  To translate rotation matrices generated by the \pkg{rotations} package into a form usable by the \pkg{orientlib} package, first coerce a \code{"SO3"} object into a matrix of the same dimension, i.e.\ $n\times9$, then apply the \code{rotvector} function provided by the \pkg{orientlib} package.  Quaternions are defined in the \pkg{orientlib} package by $\bm Q = x_1 i + x_2 j + x_3 k + x_4$, cf.\ \eqref{eqn:quat}, which may lead to confusion when translating quaternions between the \pkg{orientlib} package and either of the \pkg{onion} or \pkg{rotations} packages.  Below is a demonstration of how quaternions and rotation matrices generated by the \pkg{rotations} package can be translated into a form usable by the \pkg{onion} and \pkg{orientlib} packages, respectively.  See \code{help(package = "onion")} and \code{help(package = "orientlib")} for more on these packages.

<<summary>>=
 Qs <- ruars(20, rcayley, space = 'Q4')
 Rs <- as.SO3(Qs)
 suppressMessages(require(onion))
 onionQs <- as.quaternion(matrix(Qs,nrow=4, byrow = TRUE))
 suppressMessages(require(orientlib))
 orientRs <- rotvector(matrix(Rs, ncol = 9))
@

Computational speed of the \pkg{rotations} package has been enhanced through use of the \CRANpkg{Rcpp} and \CRANpkg{RcppArmadillo} packages \citep*{eddelbuettel2013,eddelbuettel2013b}. In future versions of the package we plan to extend the parametrization and estimator sections to include robust estimators currently being developed by the authors.

\section{Acknowledgements}

We would like to thank the reviewers for their comments and suggestions.  The \pkg{rotations} package and this article have benefited greatly from their time and effort.

<<resetoption, include=FALSE>>=
options(orig_options)
@

\bibliography{stanfill-hofmann-genschel}

\address{Bryan Stanfill\\
  Department of Statistics\\
  Iowa State University\\
  Ames, IA 50011}\\
\email{stanfill@iastate.edu}

\address{Heike Hofmann\\
  Department of Statistics\\
  Iowa State University\\
  Ames, IA 50011}\\
\email{hofmann@mail.iastate.edu}

\address{Ulrike Genschel\\
  Department of Statistics\\
  Iowa State University\\
  Ames, IA 50011}\\
\email{ulrike@mail.iastate.edu}

\end{article}

\end{document}
