Skip to content

Commit 08a0372

Browse files
committed
permutations
Updates so number of permutations flows through table generation properly
1 parent 363be15 commit 08a0372

File tree

2 files changed

+26
-15
lines changed

2 files changed

+26
-15
lines changed

AICc_PERMANOVA.R

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,15 @@ AICc.PERMANOVA2 <- function(adonis2.model) {
5353

5454
# Ok, now extract appropriate terms from the adonis model Calculating AICc
5555
# using residual sum of squares (RSS or SSE) since I don't think that adonis
56-
# returns something I can use as a liklihood function... o wait maximum likelihood may be MSE.
56+
# returns something I can use as a likelihood function... maximum likelihood
57+
# and MSE estimates are the same when distribution is gaussian See e.g.
58+
# https://www.jessicayung.com/mse-as-maximum-likelihood/;
59+
# https://towardsdatascience.com/probability-concepts-explained-maximum-likelihood-estimation-c7b4342fdbb1
60+
# So using RSS or MSE estimates is fine as long as the residuals are
61+
# Gaussian https://robjhyndman.com/hyndsight/aic/ If models have different
62+
# conditional likelihoods then AIC is not valid. However, comparing models
63+
# with different error distributions is ok (above link).
64+
5765

5866
RSS <- adonis2.model$SumOfSqs[ length(adonis2.model$SumOfSqs) - 1 ]
5967
MSE <- RSS / adonis2.model$Df[ length(adonis2.model$Df) - 1 ]
@@ -63,27 +71,29 @@ AICc.PERMANOVA2 <- function(adonis2.model) {
6371
k <- nn - adonis2.model$Df[ length(adonis2.model$Df) - 1 ]
6472

6573

66-
# AIC : 2*k + n*ln(RSS)
74+
# AIC : 2*k + n*ln(RSS/n)
6775
# AICc: AIC + [2k(k+1)]/(n-k-1)
6876

6977
# based on https://en.wikipedia.org/wiki/Akaike_information_criterion;
78+
# https://www.statisticshowto.datasciencecentral.com/akaikes-information-criterion/ ;
7079
# https://www.researchgate.net/post/What_is_the_AIC_formula;
71-
# http://avesbiodiv.mncn.csic.es/estadistica/ejemploaic.pdf
80+
# http://avesbiodiv.mncn.csic.es/estadistica/ejemploaic.pdf;
81+
# https://medium.com/better-programming/data-science-modeling-how-to-use-linear-regression-with-python-fdf6ca5481be
7282

7383
# AIC.g is generalized version of AIC = 2k + n [Ln( 2(pi) RSS/n ) + 1]
7484
# AIC.pi = k + n [Ln( 2(pi) RSS/(n-k) ) +1],
7585

76-
AIC <- 2*k + nn*log(RSS)
86+
AIC <- 2*k + nn*log(RSS/nn)
7787
AIC.g <- 2*k + nn * (1 + log( 2 * pi * RSS / nn))
7888
AIC.MSE <- 2*k + nn * log(MSE)
7989
AIC.pi <- k + nn*(1 + log( 2*pi*RSS/(nn-k) ) )
8090
AICc <- AIC + (2*k*(k + 1))/(nn - k - 1)
8191
AICc.MSE <- AIC.MSE + (2*k*(k + 1))/(nn - k - 1)
8292
AICc.pi <- AIC.pi + (2*k*(k + 1))/(nn - k - 1)
8393

84-
output <- list("AIC" = AIC, "AIC.g" = AIC.g, "AICc" = AICc,
94+
output <- list("AIC" = AIC, "AICc" = AICc, "AIC.g" = AIC.g,
8595
"AIC.MSE" = AIC.MSE, "AICc.MSE" = AICc.MSE,
86-
"AIC.pi" = AIC.pi, "AICc.pi" = AICc.pi, "k" = k)
96+
"AIC.pi" = AIC.pi, "AICc.pi" = AICc.pi, "k" = k, "N" = nn)
8797

8898
return(output)
8999

AICc_table_generation.R

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ require(tibble)
44

55
## -- For two variables: ------------------------------------
66

7-
AICc.table.2var <- function(sig.vars, control.var.char = NULL, c.var = 0, matrix.char, perm = 999, type = "AICc", method = "bray") {
7+
AICc.table.2var <- function(sig.vars, control.var.char = NULL, c.var = 0, matrix.char, perm, type = "AICc", method = "bray") {
88

99
varcomb.2.AICc <- tibble(variables = rep("var.name", choose(length(sig.vars),2)),
1010
AICc.values = rep(0),
@@ -13,7 +13,7 @@ AICc.table.2var <- function(sig.vars, control.var.char = NULL, c.var = 0, matrix
1313
`Var Explnd` = rep(0),
1414
Model = rep("model"))
1515

16-
if (is.character(control.var.char) == TRUE & c.var == 0) {c.var = 1}
16+
if (is.character(control.var.char) == TRUE & c.var == 0) {c.var = length(control.var.char)}
1717

1818
combo.list <- combn(x = sig.vars, m = 2, simplify = FALSE)
1919

@@ -52,11 +52,12 @@ AICc.table.2var <- function(sig.vars, control.var.char = NULL, c.var = 0, matrix
5252

5353
varcomb.2.AICc$`Delta AICc` <- varcomb.2.AICc$AICc.values -
5454
min(varcomb.2.AICc$AICc.values)
55-
varcomb.2.AICc$`Relative Likelihood` <- exp((min(varcomb.2.AICc$AICc.values) -
56-
varcomb.2.AICc$AICc.values)/2)
55+
varcomb.2.AICc$`Relative Likelihood` <-
56+
exp(-.5 * (varcomb.2.AICc$AICc.values - min(varcomb.2.AICc$AICc.values)))
5757

5858
# Relative likelihood compared with best model; see
5959
# https://en.wikipedia.org/wiki/Likelihood_function
60+
# https://www.rdocumentation.org/packages/qpcR/versions/1.4-1/topics/akaike.weights
6061

6162

6263
return(varcomb.2.AICc)
@@ -68,7 +69,7 @@ return(varcomb.2.AICc)
6869
## -- For N variables: ---------------------------------------------------
6970

7071

71-
AICc.table.Nvar <- function(sig.vars, control.var.char = NULL, c.var = 0, matrix.char, perm = 999, n.var = 1, composite = FALSE, type = "AICc", method = "bray") {
72+
AICc.table.Nvar <- function(sig.vars, control.var.char = NULL, c.var = 0, matrix.char, perm, n.var = 1, composite = FALSE, type = "AICc", method = "bray") {
7273

7374
if (n.var > length(sig.vars)) { stop("n.var greater than number of significant variables")}
7475

@@ -123,8 +124,8 @@ AICc.table.Nvar <- function(sig.vars, control.var.char = NULL, c.var = 0, matrix
123124
if (composite == FALSE) {
124125
varcomb.N.AICc$`Delta AICc` <- varcomb.N.AICc$AICc.values -
125126
min(varcomb.N.AICc$AICc.values)
126-
varcomb.N.AICc$`Relative Likelihood` <- exp((min(varcomb.N.AICc$AICc.values) -
127-
varcomb.N.AICc$AICc.values)/2)
127+
varcomb.2.AICc$`Relative Likelihood` <-
128+
exp(-.5 * (varcomb.2.AICc$AICc.values - min(varcomb.2.AICc$AICc.values)))
128129
}
129130

130131

@@ -149,7 +150,7 @@ AICc.table.all <- function(sig.vars, control.var.char = NULL, matrix.char, perm
149150

150151
temp <- AICc.table.Nvar(sig.vars = control.var.char, control.var.char = NULL,
151152
matrix.char = matrix.char, n.var = 1, composite = TRUE,
152-
type = type, method = method)
153+
type = type, method = method, perm = perm)
153154

154155
varcomb.all <- rbind(varcomb.all, temp)
155156

@@ -161,7 +162,7 @@ AICc.table.all <- function(sig.vars, control.var.char = NULL, matrix.char, perm
161162

162163
temp <- AICc.table.Nvar(sig.vars = sig.vars, control.var.char = control.var.char,
163164
matrix.char = matrix.char, n.var = i, composite = TRUE,
164-
type = type, method = method)
165+
type = type, method = method, perm = perm)
165166

166167
varcomb.all <- rbind(varcomb.all, temp)
167168

0 commit comments

Comments
 (0)