\HeaderA{plw}{Probe level Locally moderated Weighted median-t.}{plw}
\keyword{htest}{plw}
\keyword{models}{plw}
\keyword{design}{plw}
\begin{Description}\relax
Computes locally moderated weighted median t-test for microarray data.
\end{Description}
\begin{Usage}
\begin{verbatim}
plw(x,design=rep(1,ncol(x)),contrast=matrix(1),
    probenames = unlist(ifelse(class(x) == "AffyBatch", 
                                list(p = probeNames(x)),
                                list(p = NULL))),
    maxIter = 200, epsilon = 1e-06, verbose = TRUE,
    nknots = 10, nOut = 2000, nIn = 4000, knots = NULL,
    checkRegulation = TRUE)
\end{verbatim}
\end{Usage}
\begin{Arguments}
\begin{ldescription}
\item[\code{x}] Data, log2(PM) intensities or an AffyBatch object, see details
\item[\code{design}] design matrix
\item[\code{contrast}] contrast matrix
\item[\code{probenames}] If not null, it is used to group PM probes into probe sets, see details.
\item[\code{maxIter}] maximum number of iterations
\item[\code{epsilon}] convergence criteria
\item[\code{verbose}] print computation info or not
\item[\code{nknots}] Number of knots of spline for \eqn{\nu}{}
\item[\code{nOut}] Parameter for calculating knots, see getKnots
\item[\code{nIn}] Parameter for calculating knots, see getKnots
\item[\code{knots}] Knots, if not NULL it overrides nknots, nOut and nIn
\item[\code{checkRegulation}] If TRUE, data is checked for a correct specified contrast (see details)
\end{ldescription}
\end{Arguments}
\begin{Details}\relax
This function computes the Probe level Locally moderated Weighted median-t statistic (PLW) described in 
\eqn{\mbox{\AA}}{}strand (2007b),
specially design for Affymetrix type data, or other microarray data with multiple probes.

The data object x should be either a matrix of perfect match (PM) intensities, or an object of class AffyBatch.
When x is a matrix of PM intensities, the intensities should be background corrected, normalized, and logged (with base 2).
If x is an AffyBatch object, the default background correction and normalization of RMA is applied to x.

When probenames is not null, it should be a vector of length equal to the number rows in the matrix x, giving the probe-set identity for each PM probe.
Use the function probeNames in the affy package to get probenames when x is a matrix of log2(PM) intensities.

Inference is done for each PM probe, thus moderated t-statistic, p-value and log2(FC) is calculated for each probe.
The median t-statistics for each probe-set is also computed.

Each PM probe g (row of x) is modeled as:
\deqn{y_g|c_g \sim N(\mu_g,c_g\Sigma)}{}
\deqn{c_g \sim \mbox{InvGamma}(m/2,m\nu/2)}{}
where \eqn{\nu}{} is function of the mean intensity: \eqn{\nu(\bar\mu_g)}{},
\eqn{N}{} denotes a multivariate normal distribution, 
\eqn{\Sigma}{} is a covariance matrix and 
\eqn{\mbox{InvGamma}(a,b)}{} is the inverse-gamma distribution with density function
\deqn{f(x)=(b)^{a} \exp\{-b/x\} x^{-a-1}/\Gamma(a)}{}

Given the design matrix D, \eqn{\mu_g}{} equals \eqn{D\gamma_g}{}, 
and given the contrast matrix C the hypothesis \eqn{C\gamma_g=0}{} is tested.
C should be a one row matrix of same length as the column vector \eqn{\gamma_g}{}.

See examples on how to specify the design and contrast matrices.

A cubic spline is used to parameterize the smooth function \eqn{\nu(x)}{} 
\deqn{\nu(x) = \exp\{ H(x)^T \beta \}}{}
where 
\eqn{H:R \to R^{2p-1}}{} 
is a set B-spline basis functions for a given set of p interior spline-knots, see chapter 5 of Hastie et al. (2001).

The parameter estimation procedure is based on the assumption that the specified contrast is close to zero
for most genes, or at least that the median contrast over all genes is close to zero. A check is run on data
to validate this assumptions. If the checking fails, with the error message "warning: most genes appears to be regulated..."
and if YOU ARE SURE that the design and contrast is correct, use checkRegulation=FALSE.
\end{Details}
\begin{Value}
\begin{ldescription}
\item[\code{Sigma}] Estimated covariance matrix for \eqn{y=P^Tx}{}
\item[\code{m}] Estimated shape parameter for inverse-gamma prior for probe variances
\item[\code{v}] Estimated scale parameter curve for inverse-gamma prior for probe variances
\item[\code{converged}] T if the EM algorithms converged
\item[\code{iter}] Number of iterations
\item[\code{modS2}] Moderated estimator of probe-specific variances
\item[\code{histLogS2}] Histogram of log(s2) where s2 is the ordinary variance estimator
\item[\code{fittedDensityLogS2}] The fitted density for log(s2)
\item[\code{logs2}] Variance estimators, logged with base 2.
\item[\code{medianT}] Median moderated t-statistic for each probe-set
\item[\code{t}] Moderated t-statistic for each PM probe
\item[\code{coefficients}] Estimated contrast for each PM probe
\item[\code{p.value}] P-value from the moderated t-statistic for each PM probe
\item[\code{dfT}] Degrees of freedom of the moderated t-statistic
\item[\code{weights}] Weights for estimating the contrast
\item[\code{P}] Transformation matrix
\item[\code{beta}] Estimated parameter vector \eqn{\beta}{} of spline for \eqn{\nu(x)}{} 
\item[\code{knots}] The knots used in spline for \eqn{\nu(x)}{}
\item[\code{x}] The input vector covariate vector x
\end{ldescription}
\end{Value}
\begin{Author}\relax
Magnus \eqn{\mbox{\AA}}{}strand
\end{Author}
\begin{References}\relax
Hastie, T., Tibshirani, R., and Friedman, J. (2001). The Elements of Statistical Learning, volume 1. Springer, first edition.

Kristiansson, E., Sj\eqn{\mbox{\"o}}{}gren, A., Rudemo, M., Nerman, O. (2005). Weighted Analysis of Paired Microarray Experiments. Statistical Applications in Genetics and Molecular Biology 4(1)

\eqn{\mbox{\AA}}{}strand, M. et al. (2007a). Improved covariance matrix estimators for weighted analysis of microarray data. Journal of Computational Biology, Accepted.

\eqn{\mbox{\AA}}{}strand, M. et al. (2007b). Empirical Bayes models for multiple-probe type arrays at the probe level. Bioinformatics, Submitted 1 October 2007.
\end{References}
\begin{SeeAlso}\relax
estimateSigmaMVbeta, lmw
\end{SeeAlso}
\begin{Examples}
\begin{ExampleCode}

# ------------------------------------------
# Example analyzing the 6 arrays in the 
# AffySpikeU95Subset data set

# Loading the data
data(AffySpikeU95Subset)

# Defining design and contrast matrix
group<-factor(rep(1:2,each=3))
design<-model.matrix(~group-1)
contrast<-matrix(c(1,-1),1,2)

# Analyzing with an AffyBatch object as input
model1<-plw(AffySpikeU95Subset,design=design,contrast=contrast,
            epsilon=0.01)

## Look at fitted vs observed density for log(s2)
varHistPlot(model1)

## Look at fitted curve for scale parameter
scaleParameterPlot(model1)

## Selecting top genes
topRankSummary(model1,nGenes=10)

## Plotting t-statistics and log2FC for top genes
par(mfrow=c(1,2))
plotSummaryT(model1,nGenes=20)
plotSummaryLog2FC(model1,nGenes=20)

###---------------------------------------
# Analyzing with BG-adjusted and normalized PM data
pm1<-pm(bg.correct.rma(AffySpikeU95Subset, bgtype = 2))
pm2<-matrix(.C("qnorm_c", as.double(as.vector(pm1)), 
               as.integer(nrow(pm1)), 
               as.integer(ncol(pm1)))[[1]],
               nrow(pm1),ncol(pm1))
data<-log2(pm2)

probenames<-probeNames(AffySpikeU95Subset)
model2<-plw(data,design=design,contrast=contrast,
            probenames=probenames,epsilon=0.01)

###---------------------------------------
# Model1 and model2 should give identical result
# For example identical top ranking:
range(topRankSummary(model1)$t-
      topRankSummary(model2)$t,na.rm=TRUE)

\end{ExampleCode}
\end{Examples}

