Skip to content

Commit

Permalink
use natural way of calculationg RsquareAdj for cca: conflicts varpart
Browse files Browse the repository at this point in the history
varpart adjusts R2 of basic components, and R2-adj for a testable
partial model is the difference of adjusted models. This version
of RsquareAdj.cca directly estimates the R2 of the partial model and
adjusts that, or in other words, adjusts a difference of R2 values
of taking difference of adjusted R2 values (varpart).
  • Loading branch information
Jari Oksanen committed Feb 22, 2018
1 parent 9c25e6f commit 6ac3365
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 25 deletions.
30 changes: 10 additions & 20 deletions R/RsquareAdj.R
Original file line number Diff line number Diff line change
Expand Up @@ -33,30 +33,20 @@
list(r.squared = R2, adj.r.squared = radj)
}

## cca result: RsquareAdj is calculated similarly as in
## varpart(). This is similar "semipartial" model as for rda() and
## found as a difference of R2-adj values of combined model with
## constraints + conditions and only conditions.
## cca result: R2_adj calculation for partial models differs from
## varpart(), where it is assessed as difference of R2_adj values for
## conditions and constraints. Here we use difference of unadjusted R2
## values, and adjust that difference.
`RsquareAdj.cca` <-
function (x, permutations = 1000, ...)
{
r2 <- x$CCA$tot.chi / x$tot.chi
if (is.null(x$pCCA)) {
p <- permutest(x, permutations, ...)
radj <- 1 - ((1 - r2) / (1 - mean(p$num / x$tot.chi)))
} else {
p <- getPermuteMatrix(permutations, nobs(x))
Y <- ordiYbar(x, "initial")
r2tot <- (x$pCCA$tot.chi + x$CCA$tot.chi) / x$tot.chi
r2null <- mean(sapply(seq_len(nrow(p)), function(i)
sum(qr.fitted(x$CCA$QR, Y[p[i,],])^2)))
r2tot <- 1 - ((1-r2tot)/(1-r2null/x$tot.chi))
r2p <- x$pCCA$tot.chi / x$tot.chi
r2null <- mean(sapply(seq_len(nrow(p)), function(i)
sum(qr.fitted(x$pCCA$QR, Y[p[i,],])^2)))
r2p <- 1 - ((1-r2p)/(1-r2null/x$tot.chi))
radj <- r2tot - r2p
}
## model='direct' makes a difference in pCCA: it guarantees that
## dependent data are permuted in the same way in the partial and
## constrained components so that we can take their difference for
## the correction term.
p <- permutest(x, permutations, model = "direct")
radj <- 1 - ((1 - r2) / (1 - mean(p$num) / x$tot.chi))
list(r.squared = r2, adj.r.squared = radj)
}

Expand Down
7 changes: 2 additions & 5 deletions man/RsquareAdj.Rd
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,7 @@ Adjusted R-square
R-squared for a cca. The permutations can be calculated in parallel by
specifying the number of cores which is passed to \code{\link{permutest}}}

\item{\dots}{ Other arguments (ignored) except in the case of cca in
which these arguments are passed to \code{\link{permutest}}.}
\item{\dots}{ Other arguments (ignored).}
}

\details{ The default method finds the adjusted
Expand All @@ -49,9 +48,7 @@ Adjusted R-square
models in \code{\link{glm}}.

The adjusted, \eqn{R^2}{R-squared} of \code{cca} is computed using a
permutation approach developed by Peres-Neto et al. (2006). By
default 1000 permutations are used. Adjusted \eqn{R^2}{R-squared} is
not calculated for partial CCA.
permutation approach developed by Peres-Neto et al. (2006).

The raw \eqn{R^2}{R-squared} of partial \code{rda} gives the
proportion explained after removing the variation due to conditioning
Expand Down

0 comments on commit 6ac3365

Please sign in to comment.