Skip to content

Commit

Permalink
v. 0.6.2
Browse files Browse the repository at this point in the history
  • Loading branch information
svkucheryavski committed Apr 22, 2015
1 parent 21942db commit 33c904b
Show file tree
Hide file tree
Showing 32 changed files with 183 additions and 171 deletions.
4 changes: 2 additions & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Package: mdatools
Title: Multivariate Data Analysis for Chemometrics
Version: 0.6.1
Date: 2014-01-22
Version: 0.6.2
Date: 2014-04-22
Author: Sergey Kucheryavskiy
Maintainer: Sergey Kucheryavskiy <[email protected]>
Description: Package implements projection based methods for preprocessing, exploring and analysis of multivariate data used in chemometrics.
Expand Down
5 changes: 5 additions & 0 deletions NEWS
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
v.0.6.2
========
* Q2 residuals renamed to Q (Squared residual distance)
* All plots have parameters `lab.col` and `lab.cex` for changing color and font size for data point labels

v.0.6.1
========
* fixed a bug led to incorrect calculation of specificity
Expand Down
68 changes: 34 additions & 34 deletions R/ldecomp.R
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@
#' number of selected components
#' @param T2
#' matrix with calculated T2 values (e.g. for CV)
#' @param Q2
#' matrix with calculated Q2 values (e.g. for CV)
#' @param Q
#' matrix with calculated Q statistic (e.g. for CV)
#' @param cal
#' logical, true if data is for calibration of a LDECOMP based model
#'
Expand All @@ -29,7 +29,7 @@
#' \item{scores }{matrix with score values (nobj x ncomp).}
#' \item{residuals }{matrix with data residuals (nobj x nvar).}
#' \item{T2 }{matrix with T2 distances (nobj x ncomp).}
#' \item{Q2 }{matrix with Q2 distances (nobj x ncomp).}
#' \item{Q }{matrix with Q statistic (nobj x ncomp).}
#' \item{tnorm }{vector with singular values used for scores normalization.}
#' \item{ncomp.selected }{selected number of components.}
#' \item{expvar }{explained variance for each component.}
Expand All @@ -48,7 +48,7 @@
#'
ldecomp = function(scores = NULL, loadings = NULL, residuals = NULL,
totvar, tnorm = NULL, ncomp.selected = NULL,
T2 = NULL, Q2 = NULL, cal = TRUE)
T2 = NULL, Q = NULL, cal = TRUE)
{
if (!is.null(scores))
{
Expand All @@ -69,12 +69,12 @@ ldecomp = function(scores = NULL, loadings = NULL, residuals = NULL,
obj$ncomp.selected = ncomp.selected

# calculate residual distances and explained variance
if (is.null(Q2) && is.null(T2) && !is.null(scores) && !is.null(loadings) && !is.null(residuals))
if (is.null(Q) && is.null(T2) && !is.null(scores) && !is.null(loadings) && !is.null(residuals))
{
res = ldecomp.getDistances(scores, loadings, residuals, tnorm, cal)

if (is.null(Q2))
obj$Q2 = res$Q2
if (is.null(Q))
obj$Q = res$Q

if (is.null(T2))
obj$T2 = res$T2
Expand All @@ -86,12 +86,12 @@ ldecomp = function(scores = NULL, loadings = NULL, residuals = NULL,
}
else
{
obj$Q2 = Q2
obj$Q = Q
obj$T2 = T2
obj$tnorm = tnorm
}

var = ldecomp.getVariances(obj$Q2, totvar)
var = ldecomp.getVariances(obj$Q, totvar)
obj$expvar = var$expvar
obj$cumexpvar = var$cumexpvar

Expand All @@ -105,7 +105,7 @@ ldecomp = function(scores = NULL, loadings = NULL, residuals = NULL,
#' Residuals distances for linear decomposition
#'
#' @description
#' Computes residual distances (Q2 and T2) and modelling power for a data decomposition X = TP' + E.
#' Computes residual distances (Q and T2) and modelling power for a data decomposition X = TP' + E.
#'
#' @param scores
#' matrix with scores (T).
Expand All @@ -123,7 +123,7 @@ ldecomp = function(scores = NULL, loadings = NULL, residuals = NULL,
#' (number of columns in scores and loadings).
#'
#' @return
#' Returns a list with Q2, Q2var, T2 and modelling power values for each component.
#' Returns a list with Q, Qvar, T2 and modelling power values for each component.
#'
ldecomp.getDistances = function(scores, loadings, residuals, tnorm = NULL, cal = TRUE)
{
Expand All @@ -132,7 +132,7 @@ ldecomp.getDistances = function(scores, loadings, residuals, tnorm = NULL, cal =
nvar = nrow(loadings)

T2 = matrix(0, nrow = nobj, ncol = ncomp)
Q2 = matrix(0, nrow = nobj, ncol = ncomp)
Q = matrix(0, nrow = nobj, ncol = ncomp)
modpower = matrix(0, nrow = nvar, ncol = ncomp)

# calculate normalized scores
Expand All @@ -153,20 +153,20 @@ ldecomp.getDistances = function(scores, loadings, residuals, tnorm = NULL, cal =
exp = scores[, 1:i, drop = F] %*% t(loadings[, 1:i, drop = F]);
res = data - exp;

Q2[, i] = rowSums(res^2)
Q[, i] = rowSums(res^2)
T2[, i] = rowSums(scoresn[, 1:i, drop = F]^2)

if (nobj > i && cal == TRUE)
modpower[, i] = 1 - sqrt(colSums(res^2)/(nobj - i - 1))/datasd
}

# set dimnames and return results
colnames(Q2) = colnames(T2) = colnames(modpower) = colnames(scores)
rownames(Q2) = rownames(T2) = rownames(scores)
colnames(Q) = colnames(T2) = colnames(modpower) = colnames(scores)
rownames(Q) = rownames(T2) = rownames(scores)
rownames(modpower) = rownames(loadings)

res = list(
Q2 = Q2,
Q = Q,
T2 = T2,
modpower = modpower,
tnorm = tnorm
Expand All @@ -179,17 +179,17 @@ ldecomp.getDistances = function(scores, loadings, residuals, tnorm = NULL, cal =
#' @description
#' Computes explained variance and cumulative explained variance for a data decomposition X = TP' + E.
#'
#' @param Q2
#' Q2 values (squared residuals distance from object to component space).
#' @param Q
#' Q values (squared residuals distance from object to component space).
#' @param totvar
#' Total variance of the original data (after preprocessing).
#'
#' @return
#' Returns a list with two vectors.
#'
ldecomp.getVariances = function(Q2, totvar)
ldecomp.getVariances = function(Q, totvar)
{
cumresvar = colSums(Q2) / totvar * 100
cumresvar = colSums(Q) / totvar * 100
cumexpvar = 100 - cumresvar
expvar = c(cumexpvar[1], diff(cumexpvar))

Expand All @@ -199,10 +199,10 @@ ldecomp.getVariances = function(Q2, totvar)
)
}

#' Statistical limits for Q2 and T2 residuals
#' Statistical limits for Q and T2 residuals
#'
#' @description
#' Computes statisticsl limits for Q2 and T2 residuals
#' Computes statisticsl limits for Q and T2 residuals
#'
#' @param eigenvals
#' vector with eigenvalues
Expand All @@ -217,7 +217,7 @@ ldecomp.getVariances = function(Q2, totvar)
#' T2 limits are calculated using Hotelling statistics.
#'
#' @return
#' Returns a list with two vectors: \code{T2lim} and \code{Q2lim}.
#' Returns a list with two vectors: \code{T2lim} and \code{Qlim}.
#'
ldecomp.getResLimits = function(eigenvals, nobj, ncomp, alpha = 0.05)
{
Expand All @@ -230,8 +230,8 @@ ldecomp.getResLimits = function(eigenvals, nobj, ncomp, alpha = 0.05)
T2lim[1, i] = (i * (nobj - 1) / (nobj - i)) * qf(1 - alpha, i, nobj - i);
}

# calculate Q2 limit using F statistics
Q2lim = matrix(0, nrow = 1, ncol = ncomp)
# calculate Q limit using F statistics
Qlim = matrix(0, nrow = 1, ncol = ncomp)
conflim = 100 - alpha * 100;
nvar = length(eigenvals)

Expand All @@ -253,16 +253,16 @@ ldecomp.getResLimits = function(eigenvals, nobj, ncomp, alpha = 0.05)
ca = sqrt(2) * erfinv(cl/100)
h1 = ca * sqrt(2 * t2 * h0^2)/t1
h2 = t2 * h0 * (h0 - 1)/(t1^2)
Q2lim[1, i] = t1 * (1 + h1 + h2)^(1/h0)
Qlim[1, i] = t1 * (1 + h1 + h2)^(1/h0)
}
else
Q2lim[1, i] = 0
Qlim[1, i] = 0
}

colnames(T2lim) = colnames(Q2lim) = paste('Comp', 1:ncomp)
colnames(T2lim) = colnames(Qlim) = paste('Comp', 1:ncomp)
res = list(
T2lim = T2lim,
Q2lim = Q2lim
Qlim = Qlim
)
}

Expand Down Expand Up @@ -394,7 +394,7 @@ plotScores.ldecomp = function(obj, comp = c(1, 2), main = 'Scores',
#' Residuals plot for linear decomposition
#'
#' @description
#' Shows a plot with T2 vs Q2 values for data objects.
#' Shows a plot with T2 vs Q values for data objects.
#'
#' @param obj
#' object of \code{ldecomp} class.
Expand All @@ -413,7 +413,7 @@ plotScores.ldecomp = function(obj, comp = c(1, 2), main = 'Scores',
#' @param ...
#' most of graphical parameters from \code{\link{mdaplot}} function can be used.
#'
plotResiduals.ldecomp = function(obj, ncomp = NULL, main = NULL, xlab = 'T2', ylab = 'Q2',
plotResiduals.ldecomp = function(obj, ncomp = NULL, main = NULL, xlab = 'T2', ylab = 'Squared residual distance (Q)',
show.labels = F, show.limits = T, ...)
{
if (is.null(main))
Expand All @@ -428,11 +428,11 @@ plotResiduals.ldecomp = function(obj, ncomp = NULL, main = NULL, xlab = 'T2', yl
ncomp = obj$ncomp.selected

if (show.limits == T)
show.lines = c(obj$T2lim[1, ncomp], obj$Q2lim[1, ncomp])
show.lines = c(obj$T2lim[1, ncomp], obj$Qlim[1, ncomp])
else
show.lines = F

data = cbind(obj$T2[, ncomp], obj$Q2[, ncomp])
data = cbind(obj$T2[, ncomp], obj$Q[, ncomp])
colnames(data) = c(xlab, ylab)
mdaplot(data, main = main, xlab = xlab, ylab = ylab, show.labels = show.labels,
show.lines = show.lines, ...)
Expand Down Expand Up @@ -465,7 +465,7 @@ print.ldecomp = function(x, str = NULL, ...)
cat('\nMajor fields:\n')
cat('$scores - matrix with score values\n')
cat('$T2 - matrix with T2 distances\n')
cat('$Q2 - matrix with Q2 residuals\n')
cat('$Q - matrix with Q residuals\n')
cat('$ncomp.selected - selected number of components\n')
cat('$expvar - explained variance for each component\n')
cat('$cumexpvar - cumulative explained variance\n')
Expand Down
18 changes: 11 additions & 7 deletions R/mdaplots.R
Original file line number Diff line number Diff line change
Expand Up @@ -684,7 +684,8 @@ mdaplot = function(data, type = 'p', pch = 16, col = NULL, lty = 1, lwd = 1, bwd
cgroup = NULL, xlim = NULL, ylim = NULL, colmap = 'default', labels = NULL,
main = NULL, xlab = NULL, ylab = NULL, single.x = T, show.labels = F,
show.colorbar = T, show.lines = F, show.grid = T, show.axes = T,
xticks = NULL, xticklabels = NULL, yticks = NULL, yticklabels = NULL, ...)
xticks = NULL, xticklabels = NULL, yticks = NULL, yticklabels = NULL,
lab.col = 'darkgray', lab.cex = 0.65, ...)
{
# Makes a plot for one series of data (scatter, line, scatterline, or bar).
#
Expand Down Expand Up @@ -713,7 +714,7 @@ mdaplot = function(data, type = 'p', pch = 16, col = NULL, lty = 1, lwd = 1, bwd
# xticklabels: labels for x axis corresponding to x tick values
# yticks: tick values for y axis
# yticklabels: labels for y axis corresponding to y tick values

# lab.col: color for data point labels
data = as.matrix(data)

if (is.null(dim(data)) || nrow(data) < 1)
Expand Down Expand Up @@ -837,7 +838,7 @@ mdaplot = function(data, type = 'p', pch = 16, col = NULL, lty = 1, lwd = 1, bwd

# show labels if needed
if ((show.labels == T || !is.null(labels)) && type != 'e')
mdaplot.showLabels(data, type = type)
mdaplot.showLabels(data, type = type, col = lab.col, cex = lab.cex)

# show lines if needed
if (is.numeric(show.lines) && length(show.lines) == 2 )
Expand All @@ -852,7 +853,8 @@ mdaplotg = function(data, type = 'p', pch = 16, lty = 1, lwd = 1, bwd = 0.8,
legend = NULL, xlab = NULL, ylab = NULL, main = NULL, labels = NULL,
ylim = NULL, xlim = NULL, colmap = 'default', legend.position = 'topright', single.x = T,
show.legend = T, show.labels = F, show.lines = F, show.grid = T,
xticks = NULL, xticklabels = NULL, yticks = NULL, yticklabels = NULL, ...)
xticks = NULL, xticklabels = NULL, yticks = NULL, yticklabels = NULL,
lab.col = 'darkgray', lab.cex = 0.65,...)
{
# Makes a group of plots for several data sets
#
Expand Down Expand Up @@ -987,7 +989,8 @@ mdaplotg = function(data, type = 'p', pch = 16, lty = 1, lwd = 1, bwd = 0.8,
}

mdaplot(data[[i]], type = type[i], col = col[i], pch = pch[i], lty = lty[i],
labels = slabels, show.grid = F, show.axes = F, show.labels = show.labels)
labels = slabels, show.grid = F, show.axes = F, show.labels = show.labels,
lab.col = lab.col, lab.cex = lab.cex)
}
}
else
Expand All @@ -1010,7 +1013,7 @@ mdaplotg = function(data, type = 'p', pch = 16, lty = 1, lwd = 1, bwd = 0.8,

mdaplot(cbind(x, y), type = type[i], col = col[i], pch = pch[i], lty = lty[i],
bwd = 0.9 * gbwd, labels = labels[, i], show.labels = show.labels,
show.grid = F, show.axes = F)
show.grid = F, show.axes = F, lab.col = lab.col, lab.cex = lab.cex)
}
}
else
Expand All @@ -1020,7 +1023,8 @@ mdaplotg = function(data, type = 'p', pch = 16, lty = 1, lwd = 1, bwd = 0.8,
x = data[, 2 * i - 1, drop = F]
y = data[, 2 * i, drop = F]
mdaplot(cbind(x, y), type = type[i], col = col[i], pch = pch[i], lty = lty[i],
labels = labels[, i], show.grid = F, show.axes = F, show.labels = show.labels)
labels = labels[, i], show.grid = F, show.axes = F, show.labels = show.labels,
lab.col = lab.col, lab.cex = lab.cex)
}
}
}
Expand Down
Loading

0 comments on commit 33c904b

Please sign in to comment.