~ubuntu-branches/ubuntu/saucy/ess/saucy-proposed

« back to all changes in this revision

Viewing changes to etc/useR-2006-ESS/Lab2/esApply.Rnw

  • Committer: Bazaar Package Importer
  • Author(s): Dirk Eddelbuettel
  • Date: 2011-02-03 16:10:05 UTC
  • mfrom: (1.2.19 upstream)
  • Revision ID: james.westby@ubuntu.com-20110203161005-g1bg3cd5mtu15uh3
Tags: 5.13-1
New upstream version released today

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
%
 
2
% NOTE -- ONLY EDIT THE .Rnw FILE!!!  The .tex file is
 
3
% likely to be overwritten.
 
4
%
 
5
% \VignetteIndexEntry{esApply Introduction}
 
6
%\VignetteDepends{Biobase}
 
7
%\VignetteKeywords{Expression Analysis}
 
8
%\VignettePackage{Biobase}
 
9
\documentclass[12pt]{article}
 
10
 
 
11
\usepackage{amsmath,pstricks}
 
12
\usepackage[authoryear,round]{natbib}
 
13
\usepackage{hyperref}
 
14
 
 
15
 
 
16
\textwidth=6.2in
 
17
\textheight=8.5in
 
18
%\parskip=.3cm
 
19
\oddsidemargin=.1in
 
20
\evensidemargin=.1in
 
21
\headheight=-.3in
 
22
 
 
23
\newcommand{\scscst}{\scriptscriptstyle}
 
24
\newcommand{\scst}{\scriptstyle}
 
25
 
 
26
\newcommand{\Rfunction}[1]{{\texttt{#1}}}
 
27
\newcommand{\Robject}[1]{{\texttt{#1}}}
 
28
\newcommand{\Rpackage}[1]{{\textit{#1}}}
 
29
\newcommand{\Rmethod}[1]{{\texttt{#1}}}
 
30
\newcommand{\Rfunarg}[1]{{\texttt{#1}}}
 
31
\newcommand{\Rclass}[1]{{\textit{#1}}}
 
32
 
 
33
 
 
34
 
 
35
\bibliographystyle{plainnat}
 
36
 
 
37
\begin{document}
 
38
 
 
39
\section*{A note on {\tt esApply}}
 
40
 
 
41
{\tt exprSet}s are complex objects.  We will think
 
42
of them as linked arrays: the \Robject{exprs} element
 
43
of an \Rclass{exprSet} is $G \times N$, where $G$ is
 
44
the number of genes on a chip and $N$ is the number
 
45
of tissues analyzed, and the \Robject{pData} element
 
46
of the associated \Robject{phenoData} element is $N \times p$,
 
47
where $p$ is the number of phenotypic or demographic, etc.,
 
48
variables collected.
 
49
 
 
50
Abstractly, we are often interested in evaluating
 
51
functions $f(y;x)$ where $y$ is an $N$-vector of
 
52
expression results for a specific gene and $x$ is
 
53
an $N$-dimensional structure, coordinated with $y$, that
 
54
distinguishes elements of $y$ for processing in the function $f$.
 
55
A basic problem is to guarantee that the $j$th element
 
56
of $y$ is correctly associated with the $j$th component of $x$.
 
57
 
 
58
<<R.hide, results=hide, echo=FALSE>>=
 
59
library(Biobase)
 
60
data(eset)
 
61
@
 
62
As an example, let's consider \Robject{eset} which is an %'
 
63
\Rclass{exprSet} supplied with Biobase.  We will print
 
64
a little report, then
 
65
the first $N$-vector of gene expressions and
 
66
some covariate data:
 
67
<<R>>=
 
68
print(eset)
 
69
print(exprs(eset)[1,])
 
70
print(pData(eset)[1:2,1:3])
 
71
 
 
72
@
 
73
 
 
74
Now let's see how expressions %'
 
75
and a covariate are related:
 
76
 
 
77
<<R>>=
 
78
print( rbind(exprs(eset[1,]),
 
79
cov1=t(pData(eset))[1,]))
 
80
@
 
81
A function that evaluates the difference in median expression across
 
82
strata defined using an abstract covariate \Robject{x} is
 
83
<<R>>=
 
84
medContr <- function( y, x ) {
 
85
 ys <- split(y,x)
 
86
 median(ys[[1]]) - median(ys[[2]])
 
87
 }
 
88
@
 
89
We can apply this to a small
 
90
\Rclass{exprSet} that gives back the data listed above:
 
91
<<R>>=
 
92
print(apply(exprs(eset[1,,drop=F]),1,
 
93
  medContr, pData(eset)[["cov1"]]))
 
94
@
 
95
That's a bit clumsy.  This is where %'
 
96
\Rfunction{esApply} comes in.  We pay for some
 
97
simplicity by following a strict protocol
 
98
for the definition of the statistical
 
99
function to be applied.
 
100
<<R>>=
 
101
medContr1 <- function(y) {
 
102
 ys <- split(y,cov1)
 
103
 median(ys[[1]]) - median(ys[[2]])
 
104
 }
 
105
 
 
106
print(esApply( eset, 1, medContr1 )[1])
 
107
@
 
108
The manual page on \Rfunction{esApply} has a number of
 
109
additional examples that show how applicable functions
 
110
can be constructed and used.  The important thing to
 
111
note is that the applicable functions {\em know} the names of
 
112
the covariates in the \Robject{pData} dataframe.
 
113
 
 
114
This is achieved by having an environment populated with all the
 
115
variables in the \Robject{phenoData} component of the \Rclass{exprSet} put
 
116
in as the environment of the function that will be applied. If that
 
117
function already has an environment we retain that but in the second
 
118
position. Thus, there is some potential for variable shadowing.
 
119
 
 
120
\end{document}