Last active
December 18, 2017 17:46
-
-
Save zkamvar/e24b9cbb91bb03e6ddff851f3e5a6121 to your computer and use it in GitHub Desktop.
Get Phi statistics from amova object from either pegas or ade4
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
ade4phi <- function(sig){ | |
n <- length(sig) | |
f <- rep(0, n - 1) | |
if (length(sig) == 3) { | |
f <- rep(0, 1) | |
} | |
f[1] <- (sig[n] - sig[n - 1]) / sig[n] | |
if (length(f) > 1) { | |
s1 <- cumsum(sig[(n - 1):2])[-1] | |
s2 <- sig[(n - 2):2] | |
f[length(f)] <- sig[1] / sig[n] | |
f[2:(length(f) - 1)] <- s2 / s1 | |
} | |
return(f) | |
} | |
hfsphi <- function(sig){ | |
tot <- sig | |
nblevels <- length(tot) | |
n.names <- names(sig) | |
n.cols <- n.names[-nblevels] | |
n.rows <- c("Total", n.names[-(nblevels - 0:1)]) | |
f <- matrix(rep(0, (nblevels - 1)^2), | |
ncol = (nblevels - 1), | |
dimnames = list(n.rows, n.cols)) | |
for (i in 1:(nblevels - 1)) { | |
for (j in i:(nblevels - 1)) { | |
f[i, j] <- sum(tot[i:j])/sum(tot[i:nblevels]) | |
} | |
} | |
f | |
} | |
# adapted from the ade4 amova code | |
Statphi.default <- function(sig, method = "ade4") { | |
method <- match.arg(method, c("ade4", "hierfstat")) | |
if (method == "ade4") ade4phi(sig) else hfsphi(sig[-length(sig)]) | |
} | |
is_pegas_amova <- function(sig) identical(names(sig), c("tab", "varcoef", "varcomp", "call")) | |
Statphi.amova <- function(sig, ...){ | |
if (is_pegas_amova(sig)){ | |
if (is.data.frame(sig$varcomp)){ | |
siggies <- setNames(sig$varcomp$sigma2, rownames(sig$varcomp)) | |
} else { | |
siggies <- sig$varcomp | |
} | |
Statphi(c(siggies, sum(siggies)), ...) | |
} else { | |
Statphi(sig$componentsofcovariance$Sigma, ...) | |
} | |
} | |
Statphi <- function(sig, ...){ | |
UseMethod("Statphi") | |
} | |
library("poppr") | |
library("pegas") | |
library("hierfstat") | |
data(nancycats) | |
strata(nancycats) <- data.frame(pop = pop(nancycats)) | |
a <- poppr.amova(nancycats, ~pop, method = "pegas", nperm = 0) | |
Statphi(a) | |
Statphi(a, "h") | |
data(microbov) | |
strata(microbov) <- data.frame(other(microbov)) | |
b <- poppr.amova(microbov, ~coun/spe, method = "pegas", nperm = 0) | |
Statphi(b) | |
Statphi(b, "h") | |
# PhiST is always going to be in the top right corner of the matrix | |
# all other Phi values will be the diagonal. | |
bc <- poppr.amova(microbov, ~coun/spe/breed, method = "pegas", nperm = 0) | |
pha <- Statphi(bc) | |
phh <- Statphi(bc, "h") | |
phh <- c(phh[1, ncol(phh)], rev(diag(phh))) | |
phh | |
pha |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Created on 2017-12-14 by the reprex
package (v0.1.1.9000).