Skip to content

Commit

Permalink
- incorporated both pseudo R square AND goodness of fit indicator in …
Browse files Browse the repository at this point in the history
…the mds objects
  • Loading branch information
phauchamps committed Sep 28, 2023
1 parent e4fa004 commit 40a3057
Show file tree
Hide file tree
Showing 12 changed files with 68 additions and 37 deletions.
47 changes: 25 additions & 22 deletions R/ggplots.R
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ ggplotSamplesMDS <- function(
#browser()

RSq <- mdsObj$RSq[nDim]
GoF <- mdsObj$GoF[nDim]

# margRSq <- rep(0., nDim)
#
Expand All @@ -153,28 +154,28 @@ ggplotSamplesMDS <- function(
DF$stress <- mdsObj$spp

xlabel <- paste0("Coord. ", projectionAxes[1])
#if (projectionAxes[1] > 2) {
xlabel <- paste0(
xlabel,
#" (marg. R2 : ",
#round(100*margRSq[projectionAxes[1]], 2),
" (% var. : ",
round(100*explVar[projectionAxes[1]], 2),
"%)")
#}

xlabel <- paste0(
xlabel,
#" (marg. R2 : ",
#round(100*margRSq[projectionAxes[1]], 2),
" (% var. : ",
round(100*explVar[projectionAxes[1]], 2),
"%)")

ylabel <- paste0("Coord. ", projectionAxes[2])
#if (projectionAxes[2] > 2) {
ylabel <- paste0(
ylabel,
#" (marg. R2 : ",
#round(100*margRSq[projectionAxes[2]], 2),
" (% var. : ",
round(100*explVar[projectionAxes[2]], 2),
"%)")
#}

subtitle <- paste0("(Goodness of fit = ", round(RSq, 4),"; nDim = ", nDim, ")")

ylabel <- paste0(
ylabel,
#" (marg. R2 : ",
#round(100*margRSq[projectionAxes[2]], 2),
" (% var. : ",
round(100*explVar[projectionAxes[2]], 2),
"%)")

subtitle <- paste0("(Pseudo RSq = ", round(RSq, 4),
"; Goodness of Fit = ", round(GoF, 4),
"; nDim = ", nDim, ")")

mainAesMapping <- ggplot2::aes(
x = .data[["x"]],
Expand Down Expand Up @@ -400,8 +401,10 @@ ggplotSamplesMDSShepard <- function(
}

RSq <- mdsObj$RSq[nDim]
subtitle <- paste0("(Goodness of fit = ", round(RSq, 4),"; nDim = ", nDim, ")")
subtitle <- paste0(subtitle,")")
GoF <- mdsObj$GoF[nDim]
subtitle <- paste0("(Pseudo RSq = ", round(RSq, 4),
"; Goodness of Fit = ", round(GoF, 4),
"; nDim = ", nDim, ")")

xlabel <- "HD distances"
ylabel <- "Proj. distances"
Expand Down
31 changes: 26 additions & 5 deletions R/stats.R
Original file line number Diff line number Diff line change
Expand Up @@ -573,7 +573,14 @@ getChannelsSummaryStat <- function(
#' - `spp` the stress per point obtained from the SMACOF algorithm, i.e.
#' the contribution of each point to the stress loss function
#' - `$RSq` R squares, for each d, from 1 to `nDim`:
#' the R square when taking all dims from 1 to d.
#' the (pseudo) R square when taking all dims from 1 to d.
#' - `$GoF` Goodness of fit, for each d, from 1 to `nDim`:
#' the goodness of fit indicator (b/w 0 and 1) when taking all dims from 1 to d.
#' Note pseudo R square and goodness of fit indicators are essentially the
#' same indicator, only the definition of total sum of squares differ:
#' - for pseudo RSq: TSS is calculated using the mean pairwise distance
#' as minimum
#' - for goodness of fit: TSS is calculated using 0 as minimum
#'
#' @importFrom stats as.dist dist
#' @export
Expand Down Expand Up @@ -683,7 +690,10 @@ computeMetricMDS <- function(
scaleFactor <- sqrt(sum(delta^2, na.rm = TRUE)) / sqrt(N)
proj <- proj * scaleFactor

computeRSquares <- function(distances, projections) {
computeRSquares <- function(
distances,
projections,
asInLinearRegression = TRUE) {
delta <- as.dist(distances)
nDim <- ncol(projections)

Expand All @@ -697,8 +707,13 @@ computeMetricMDS <- function(
},
FUN.VALUE = 0.)

#TSS <- sum((delta - mean(delta, na.rm = TRUE))^2)
TSS <- sum(delta^2)
if (asInLinearRegression) {
TSS <- sum((delta - mean(delta, na.rm = TRUE))^2)
} else {
TSS <- sum(delta^2)
}



RSq <- 1-RSS/TSS

Expand All @@ -707,14 +722,20 @@ computeMetricMDS <- function(

RSq <- computeRSquares(
distances = pwDist,
projections = proj)
projections = proj,
asInLinearRegression = TRUE)
GoF <- computeRSquares(
distances = pwDist,
projections = proj,
asInLinearRegression = FALSE)

res$pwDist <- as.dist(pwDist)
res$proj <- proj
res$projDist <- dist(proj)
res$stress <- mdsRes$stress
res$spp <- mdsRes$spp
res$RSq <- RSq
res$GoF <- GoF
res$mdsObj <- mdsRes

class(res) <- "mdsRes"
Expand Down
9 changes: 8 additions & 1 deletion man/computeMetricMDS.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 40a3057

Please sign in to comment.