Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions R/confusion.R
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@
#' frequencies of actual vs. predicted class with attributes `accuracy`
#' and `error` giving the overall rates of correct and incorrect prediction.
#' @seealso [MASS::lda()], [MASS::qda()]
#'
#' @importFrom insight get_response
#'
#' @export
#'
#' @examples
Expand Down
4 changes: 2 additions & 2 deletions R/plot_discrim.R
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
# DONE: ✔️ Added `rev.axes` parameter for reversing discriminant axes
# DONE: ✔️ Added `xlim`, `ylim` arguments to control axis limits; useful for plots in discrim space.
#
# TOTO: ❌ Fix mapping for stat_ellipse() when specifying `geom = "polygon"`
# TODO: ❌ Fix mapping for stat_ellipse() when specifying `geom = "polygon"`
# TODO: Create vignette detailing how to use more generally with ggplot

#' Discriminant Analysis Decision Plot using ggplot.
Expand All @@ -26,7 +26,7 @@
#'
#' In the case of discriminant analysis, the predicted values are class membership,
#' so this can be visualized by mapping the categorical predicted class to discrete colors used as the background for the plot, or
#' plotting the **contours** of predicted class membership as lines (for `[MASS::lda()]`) or qauadratic curves (for `[MASS::qda()]`) in the plot.
#' plotting the **contours** of predicted class membership as lines (for `[MASS::lda()]`) or quadratic curves (for `[MASS::qda()]`) in the plot.
#' The predicted class of any observation in the space of the variables displayed can also be rendered as colored **tiles** or **points**
#' in the background of the plot.
#'
Expand Down
220 changes: 110 additions & 110 deletions R/redundancy.R
Original file line number Diff line number Diff line change
@@ -1,110 +1,110 @@
#' Canonical Redundancy Analysis
#'
#' @description
#' Calculates indices of redundancy (Stewart & Love, 1968) from a canonical
#' correlation analysis. These give the proportion of variances of the
#' variables in each set (X and Y) which are accounted for by the variables in
#' the other set through the canonical variates.
#'
#' @details
#'
#' The term "redundancy analysis" has a different interpretation and implementation in the
#' environmental ecology literature, such as the \pkg{vegan}.
#' In that context, each \eqn{Y_i} variable is regressed separately on the predictors in \eqn{X},
#' to give fitted values \eqn{\widehat{Y} = [\widehat{Y}_1, \widehat{Y}_2, \dots}.
#' Then a PCA of \eqn{\widehat{Y}} is carried out to determine a reduced-rank structure of
#' the predictions.
#'
#'
#' @aliases redundancy print.cancor.redundancy
#' @param object A `"cancor"` object
#' @param x A `"cancor.redundancy"` for the `print` method.
#' @param digits Number of digits to print
#' @param \dots Other arguments
#' @return An object of class `"cancor.redundancy"`, a list with the
#' following 5 components:
#' \item{Xcan.redun}{Canonical redundancies for the X variables, i.e., the
#' total fraction of X variance accounted for by the Y variables through each
#' canonical variate.}
#' \item{Ycan.redun}{Canonical redundancies for the Y variables}
#' \item{X.redun}{Total canonical redundancy for the X variables,
#' i.e., the sum of `Xcan.redun`.}
#' \item{Y.redun}{Total canonical redundancy for the Y variables}
#' \item{set.names}{names for the X and Y sets of variables}
#' @author Michael Friendly
#' @seealso \ [cancor()]
#' @references
#' Muller K. E. (1981).
#'Relationships between redundancy analysis, canonical correlation, and multivariate regression.
#' *Psychometrika*, **46**(2), 139-42.
#'
#' Stewart, D. and Love, W. (1968). A general canonical correlation
#' index. *Psychological Bulletin*, 70, 160-163.
#'
#' Brainder, "Redundancy in canonical correlation analysis",
#' <https://brainder.org/2019/12/27/redundancy-in-canonical-correlation-analysis/>
#'
#' @keywords multivariate
#' @examples
#'
#' data(Rohwer, package="heplots")
#' X <- as.matrix(Rohwer[,6:10]) # the PA tests
#' Y <- as.matrix(Rohwer[,3:5]) # the aptitude/ability variables
#'
#' cc <- cancor(X, Y, set.names=c("PA", "Ability"))
#'
#' redundancy(cc)
#' ##
#' ## Redundancies for the PA variables & total X canonical redundancy
#' ##
#' ## Xcan1 Xcan2 Xcan3 total X|Y
#' ## 0.17342 0.04211 0.00797 0.22350
#' ##
#' ## Redundancies for the Ability variables & total Y canonical redundancy
#' ##
#' ## Ycan1 Ycan2 Ycan3 total Y|X
#' ## 0.2249 0.0369 0.0156 0.2774
#'
#'
#' @export redundancy
redundancy <- function(object, ...) {
if (!inherits(object, "cancor"))
stop("Not a cancor object")
cancor <- object$cancor
Xstruc <- object$structure$X.xscores
Ystruc <- object$structure$Y.yscores
# for each canonical variate, fraction of total X, Y variance associated
Xcan.vad <- apply(Xstruc^2, 2, mean, na.rm = TRUE)
Ycan.vad <- apply(Ystruc^2, 2, mean, na.rm = TRUE)
# canonical redundancies for X, Y variables (total fraction of X variance accounted for by Y variables through canonical
# variables, and vice-versa)
Xcan.redun <- Xcan.vad * cancor^2
Ycan.redun <- Ycan.vad * cancor^2
result <- list(Xcan.redun=Xcan.redun,
Ycan.redun=Ycan.redun,
X.redun=sum(Xcan.redun),
Y.redun=sum(Ycan.redun),
set.names=object$names$set.names)
class(result) <- "cancor.redundancy"
# invisible(result)
result
}
#' @describeIn redundancy `print()` method for `"cancor.redundancy"` objects.
#' @export
print.cancor.redundancy <- function(x, digits=max(getOption("digits") - 3, 3), ...) {
Xname <- x$set.names[1]
Yname <- x$set.names[2]
cat(paste("\nRedundancies for the", Xname, "variables & total X canonical redundancy\n\n"))
Xredun <- c(x$Xcan.redun, "total X|Y"=x$X.redun)
print(Xredun, digits=digits)
cat(paste("\nRedundancies for the", Yname, "variables & total Y canonical redundancy\n\n"))
Yredun <- c(x$Ycan.redun, "total Y|X"=x$Y.redun)
print(Yredun, digits=digits)
}

#' Canonical Redundancy Analysis
#'
#' @description
#' Calculates indices of redundancy (Stewart & Love, 1968) from a canonical
#' correlation analysis. These give the proportion of variances of the
#' variables in each set (X and Y) which are accounted for by the variables in
#' the other set through the canonical variates.
#'
#' @details
#'
#' The term "redundancy analysis" has a different interpretation and implementation in the
#' environmental ecology literature, such as the \pkg{vegan}.
#' In that context, each \eqn{Y_i} variable is regressed separately on the predictors in \eqn{X},
#' to give fitted values \eqn{\widehat{Y} = [\widehat{Y}_1, \widehat{Y}_2, \dots}.
#' Then a PCA of \eqn{\widehat{Y}} is carried out to determine a reduced-rank structure of
#' the predictions.
#'
#'
#' @aliases redundancy print.cancor.redundancy
#' @param object A `"cancor"` object
#' @param x A `"cancor.redundancy"` for the `print` method.
#' @param digits Number of digits to print
#' @param \dots Other arguments
#' @return An object of class `"cancor.redundancy"`, a list with the
#' following 5 components:
#' \item{Xcan.redun}{Canonical redundancies for the X variables, i.e., the
#' total fraction of X variance accounted for by the Y variables through each
#' canonical variate.}
#' \item{Ycan.redun}{Canonical redundancies for the Y variables}
#' \item{X.redun}{Total canonical redundancy for the X variables,
#' i.e., the sum of `Xcan.redun`.}
#' \item{Y.redun}{Total canonical redundancy for the Y variables}
#' \item{set.names}{names for the X and Y sets of variables}
#' @author Michael Friendly
#' @seealso [cancor()]
#' @references
#' Muller K. E. (1981).
#'Relationships between redundancy analysis, canonical correlation, and multivariate regression.
#' *Psychometrika*, **46**(2), 139-42.
#'
#' Stewart, D. and Love, W. (1968). A general canonical correlation
#' index. *Psychological Bulletin*, 70, 160-163.
#'
#' Brainder, "Redundancy in canonical correlation analysis",
#' <https://brainder.org/2019/12/27/redundancy-in-canonical-correlation-analysis/>
#'
#' @keywords multivariate
#' @examples
#'
#' data(Rohwer, package="heplots")
#' X <- as.matrix(Rohwer[,6:10]) # the PA tests
#' Y <- as.matrix(Rohwer[,3:5]) # the aptitude/ability variables
#'
#' cc <- cancor(X, Y, set.names=c("PA", "Ability"))
#'
#' redundancy(cc)
#' ##
#' ## Redundancies for the PA variables & total X canonical redundancy
#' ##
#' ## Xcan1 Xcan2 Xcan3 total X|Y
#' ## 0.17342 0.04211 0.00797 0.22350
#' ##
#' ## Redundancies for the Ability variables & total Y canonical redundancy
#' ##
#' ## Ycan1 Ycan2 Ycan3 total Y|X
#' ## 0.2249 0.0369 0.0156 0.2774
#'
#'
#' @export redundancy
redundancy <- function(object, ...) {
if (!inherits(object, "cancor"))
stop("Not a cancor object")
cancor <- object$cancor
Xstruc <- object$structure$X.xscores
Ystruc <- object$structure$Y.yscores

# for each canonical variate, fraction of total X, Y variance associated
Xcan.vad <- apply(Xstruc^2, 2, mean, na.rm = TRUE)
Ycan.vad <- apply(Ystruc^2, 2, mean, na.rm = TRUE)

# canonical redundancies for X, Y variables (total fraction of X variance accounted for by Y variables through canonical
# variables, and vice-versa)
Xcan.redun <- Xcan.vad * cancor^2
Ycan.redun <- Ycan.vad * cancor^2

result <- list(Xcan.redun=Xcan.redun,
Ycan.redun=Ycan.redun,
X.redun=sum(Xcan.redun),
Y.redun=sum(Ycan.redun),
set.names=object$names$set.names)
class(result) <- "cancor.redundancy"
# invisible(result)
result
}

#' @describeIn redundancy `print()` method for `"cancor.redundancy"` objects.
#' @export
print.cancor.redundancy <- function(x, digits=max(getOption("digits") - 3, 3), ...) {
Xname <- x$set.names[1]
Yname <- x$set.names[2]
cat(paste("\nRedundancies for the", Xname, "variables & total X canonical redundancy\n\n"))
Xredun <- c(x$Xcan.redun, "total X|Y"=x$X.redun)
print(Xredun, digits=digits)

cat(paste("\nRedundancies for the", Yname, "variables & total Y canonical redundancy\n\n"))
Yredun <- c(x$Ycan.redun, "total Y|X"=x$Y.redun)
print(Yredun, digits=digits)

}
2 changes: 1 addition & 1 deletion R/reflect.R
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
#' X <- as.matrix(Rohwer[,6:10]) # the PA tests
#' Y <- as.matrix(Rohwer[,3:5]) # the aptitude/ability variables
#' Rohwer.can <- cancor(X, Y, set.names=c("PA", "Ability"))
#' coef(Rohwer)
#' coef(Rohwer.can)
#' Rohwer.can |> reflect() |> coef()
#'
#'
Expand Down
2 changes: 1 addition & 1 deletion inst/CITATION
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ c(
family = "Fox", role="aut")),
year = year,
note = note,
url = "https://CRAN.R-project.org/package=heplots"
url = "https://CRAN.R-project.org/package=candisc"
),

bibentry(
Expand Down