\HeaderA{vsn2}{Fit the vsn model}{vsn2}
\aliasA{coerce,RGList,NChannelSet-method}{vsn2}{coerce,RGList,NChannelSet.Rdash.method}
\aliasA{vsn2,AffyBatch-method}{vsn2}{vsn2,AffyBatch.Rdash.method}
\aliasA{vsn2,ExpressionSet-method}{vsn2}{vsn2,ExpressionSet.Rdash.method}
\aliasA{vsn2,matrix-method}{vsn2}{vsn2,matrix.Rdash.method}
\aliasA{vsn2,NChannelSet-method}{vsn2}{vsn2,NChannelSet.Rdash.method}
\aliasA{vsn2,numeric-method}{vsn2}{vsn2,numeric.Rdash.method}
\aliasA{vsn2,RGList-method}{vsn2}{vsn2,RGList.Rdash.method}
\aliasA{vsn2-methods}{vsn2}{vsn2.Rdash.methods}
\aliasA{vsnMatrix}{vsn2}{vsnMatrix}
\begin{Description}\relax
\code{vsn2} fits the vsn model to the data 
in \code{x} and returns a \code{\LinkA{vsn}{vsn.Rdash.class}} object with
the fit parameters and the transformed data matrix.
The data are, typically, feature intensity readings from a
microarray.
To obtain an object of the same class as \code{x}, containing
the normalised data and the same metdata as \code{x}, use
\begin{alltt}
    fit = vsn2(x, ...)
    nx = predict(fit, newdata=x)
\end{alltt}
or the wrapper \code{\LinkA{justvsn}{justvsn}}.
Please see the vignette \emph{Introduction to vsn} for a description
on how to use \code{vsn2} for different use cases.
\end{Description}
\begin{Usage}
\begin{verbatim}
vsnMatrix(x,
  reference,
  strata,
  lts.quantile = 0.9,
  subsample    = 0L,
  verbose      = interactive(),
  returnData   = TRUE,
  pstart,
  optimpar     = list(),
  defaultpar   = list(factr=5e7, pgtol=2e-4, maxit=60000L, trace=0L, cvg.niter=7L, cvg.eps=0))

## S4 method for signature 'ExpressionSet':
vsn2(x, reference, strata, ...)

## S4 method for signature 'AffyBatch':
vsn2(x, reference, strata, ...)

## S4 method for signature 'matrix':
vsn2(x, reference, strata, ...)

## S4 method for signature 'NChannelSet':
vsn2(x, reference, strata, backgroundsubtract=FALSE,
       foreground=c("R","G"), background=c("Rb", "Gb"), ...)

## S4 method for signature 'RGList':
vsn2(x, reference, strata, backgroundsubtract=FALSE, ...)
\end{verbatim}
\end{Usage}
\begin{Arguments}
\begin{ldescription}
\item[\code{x}] An object containing the data to which the model is to be
fitted.
\item[\code{reference}] Optional, a \code{\LinkA{vsn}{vsn.Rdash.class}} object from
a previous fit. If this argument is specified, the data in \code{x}
are normalized "towards" an existing set of reference arrays whose
parameters are stored in the object \code{reference}. If this
argument is not specified, then the data in \code{x} are normalized
"among themselves". See Details for a more precise explanation.
\item[\code{strata}] Optional, a \code{factor} or \code{integer}
whose length is \code{nrow(x)}. Can
be used for stratified normalization (i.e. separate offsets \code{a} and
factors \code{b} for each level of \code{strata}). If missing, all
rows of \code{x} are assumed to come from one stratum.
If \code{strata} is an integer, its values must cover the range
1...\emph{n}, where \emph{n} is the number of strata.
\item[\code{lts.quantile}] Numeric of length 1. The quantile that is used for
the resistant least trimmed sum of squares regression. Allowed
values are between 0.5 and 1. A value of 1 corresponds to ordinary
least sum of squares regression.
\item[\code{subsample}] Integer of length 1. If specified, the model parameters are
estimated from a subsample of the data of size \code{subsample}
only, yet the fitted transformation is
then applied to all data. For large datasets, this can substantially
reduce the CPU time and memory consumption at a negligible loss of precision.
\item[\code{backgroundsubtract}] Logical of length 1: should
local background estimates be subtracted before fitting vsn?
\item[\code{foreground, background}] Aligned character vectors of the same length,
naming the channels of \code{x} that should be used
as foreground and background values.
\item[\code{verbose}] Logical. If TRUE, some messages are printed.
\item[\code{returnData}] Logical. If TRUE, the transformed data are returned
in a slot of the resulting \code{\LinkA{vsn}{vsn.Rdash.class}} object.
Setting this option to \code{FALSE} allows saving memory
if the data are not needed.
\item[\code{pstart}] Optional, a three-dimensional numeric array that
specifies start values for the iterative parameter
estimation algorithm.
If not specified, the function tries to guess useful start values.
The first dimension corresponds to the levels of \code{strata},
the second dimension to the columns of \code{x} and the third dimension
must be 2, corresponding to offsets and factors.
\item[\code{optimpar}] Optional, a list with parameters for the likelihood
optimisation algorithm. Default parameters are taken from
\code{defaultpar}. See details.
\item[\code{defaultpar}] The default parameters  for the likelihood
optimisation algorithm. Values in \code{optimpar} take precedence
over those in \code{defaultpar}. The purpose of this argument is to
expose the default values in this manual page - it is not
intended to be changed, please use \code{optimpar} for that.
\item[\code{...}] Arguments that get passed on to \code{vsnMatrix}.
\end{ldescription}
\end{Arguments}
\begin{Value}
An object of class \code{\LinkA{vsn}{vsn.Rdash.class}}.
\end{Value}
\begin{Section}{Note on overall scale and location of the glog
  transformation}
The data are returned on a glog scale to base 2. More precisely,
the transformed data are subject to the transformation
glog2(f(b)*x+a) + c, where
glog2(u) = log2(u+sqrt(u*u+1)) = asinh(u)/log(2) is called the
generalised logarithm, a and b are the fitted model parameters
(see references), f is a parameter transformation [4],
and the overall constant offset c is computed from b such that for
large x the transformation approximately corresponds to the
log2 function. The offset c is inconsequential for all differential
expression calculations, but many users like to see the data in a
range that they are familiar with.
\end{Section}
\begin{Section}{Specific behaviour of the different methods}
\code{vsn2} methods exist for
\code{\LinkA{ExpressionSet}{ExpressionSet}},
\code{\LinkA{NChannelSet}{NChannelSet}},
\code{\LinkA{AffyBatch}{AffyBatch}} (from the \code{affy} package),
\code{\LinkA{RGList}{RGList}} (from the \code{limma} package),
\code{matrix} and \code{numeric}.
If \code{x} is an \code{\LinkA{NChannelSet}{NChannelSet}}, then
\code{vsn2} is applied to the matrix that is obtained
by horizontally concatenating the color channels.
Optionally, available background estimates can be subtracted before.
If \code{x} is an \code{\LinkA{RGList}{RGList}}, it is
converted into an \code{NChannelSet}
using a copy of Martin Morgan's code for \code{RGList} to
\code{NChannelSet} coercion, then the \code{NChannelSet} method is called.
\end{Section}
\begin{Section}{Standalone versus reference normalisation}
If the \code{reference} argument is \emph{not} specified, then the model
parameters $\mu_k$ and $\sigma$ are fit from the data in \code{x}.
This is the mode of operation described in [1]
and that was the only option in versions 1.X of this package.
If \code{reference} is specified, the model parameters
$\mu_k$ and $\sigma$ are taken from it.
This allows for 'incremental' normalization [4].
\end{Section}
\begin{Section}{Convergence of the iterative likelihood optimisation}
\code{L-BFGS-B} uses three termination criteria:
\Enumerate{
\item \code{(f\_k - f\_\{k+1\}) / max(|f\_k|, |f\_\{k+1\}|, 1) <= factr * epsmch}
where \code{epsmch} is the machine precision.
\item \code{|gradient| < pgtol}
\item \code{iterations > maxit}
}
These are set by the elements \code{factr}, \code{pgtol} and
\code{maxit} of \code{optimpar}. The remaining elements are
\describe{
\item[\code{trace}] An integer between 0 and 6, indicating the
verbosity level of \code{L-BFGS-B}, higher values
create more output.
\item[\code{cvg.niter}] The number of iterations to be used in the least
trimmed sum of squares regression.
\item[\code{cvg.eps}] Numeric. A convergence threshold for the least
trimmed sum of squares regression.
}
\end{Section}
\begin{Author}\relax
Wolfgang Huber \url{http://www.ebi.ac.uk/huber}
\end{Author}
\begin{References}\relax
[1] Variance stabilization applied to microarray data
calibration and to the quantification of differential expression,
Wolfgang Huber, Anja von Heydebreck, Holger Sueltmann, Annemarie
Poustka, Martin Vingron; Bioinformatics (2002) 18 Suppl.1 S96-S104.

[2] Parameter estimation for the calibration and variance stabilization 
of microarray data, 
Wolfgang Huber, Anja von Heydebreck, Holger Sueltmann, 
Annemarie Poustka, and Martin Vingron;  
Statistical Applications in Genetics and Molecular Biology (2003)
Vol. 2 No. 1, Article 3.
http://www.bepress.com/sagmb/vol2/iss1/art3.

[3] L-BFGS-B: Fortran Subroutines for Large-Scale Bound Constrained
Optimization, C. Zhu, R.H. Byrd, P. Lu and J. Nocedal,
Technical Report, Northwestern University (1996).

[4] Package vignette: Likelihood Calculations for vsn
\end{References}
\begin{SeeAlso}\relax
\code{\LinkA{justvsn}{justvsn}}, \code{\LinkA{predict}{predict}}
\end{SeeAlso}
\begin{Examples}
\begin{ExampleCode}
data("kidney")

fit = vsn2(kidney)                   ## fit
nkid = predict(fit, newdata=kidney)  ## apply fit

plot(exprs(nkid), pch=".")
abline(a=0, b=1, col="red")
\end{ExampleCode}
\end{Examples}

