-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
356 additions
and
17 deletions.
There are no files selected for viewing
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
module Hydra | ||
|
||
using Random, LinearAlgebra, Distances, NamedArrays, StatsModels,Statistics,TexTables,LoopVectorization | ||
include("hydra2.jl") | ||
include("output.jl") | ||
|
||
export hydra2, | ||
permute, | ||
Euclidean, | ||
SqEuclidean, | ||
PeriodicEuclidean, | ||
Cityblock, | ||
TotalVariation, | ||
Chebyshev, | ||
Minkowski, | ||
Jaccard, | ||
BrayCurtis, | ||
RogersTanimoto, | ||
@formula | ||
|
||
|
||
end |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,22 @@ | ||
module PERMANOVA | ||
|
||
# Write your package code here. | ||
using Random, LinearAlgebra, Distances, StatsModels,Statistics,TexTables,LoopVectorization | ||
include("perm2.jl") | ||
include("output.jl") | ||
|
||
export permanova, | ||
permute, | ||
Euclidean, | ||
SqEuclidean, | ||
PeriodicEuclidean, | ||
Cityblock, | ||
TotalVariation, | ||
Chebyshev, | ||
Minkowski, | ||
Jaccard, | ||
BrayCurtis, | ||
RogersTanimoto, | ||
@formula | ||
|
||
|
||
end |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
|
||
function get_output(coefs,Df,sq,r2,f_terms,Gres,Tot,p,n) | ||
n_term = length(coefs) | ||
DF = RegCol("Df") | ||
R2 = RegCol("R²") | ||
sumsq = RegCol("SumOfSqs") | ||
F = RegCol("F") | ||
P = RegCol("P") | ||
|
||
for i in 1:n_term | ||
setcoef!(R2, coefs[i]=>r2[i]) | ||
setcoef!(DF, coefs[i]=>Df[i]) | ||
setcoef!(sumsq, coefs[i]=>sq[i]) | ||
setcoef!(F, coefs[i]=>f_terms[i]) | ||
setcoef!(P, coefs[i]=>p[i]) | ||
end | ||
setcoef!(R2, "Residual"=>1-sum(r2)) | ||
setcoef!(DF, "Residual"=>n-1-sum(Df)) | ||
setcoef!(sumsq, "Residual"=>Gres) | ||
setcoef!(R2, "Total"=>1) | ||
setcoef!(DF, "Total"=>n-1) | ||
setcoef!(sumsq, "Total"=>Tot) | ||
return hcat(DF,sumsq,R2,F,P) | ||
end |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
#The key to the non-parametric method describedhere | ||
#is that the sum of squared distances between points and | ||
#their centroid is equal to (and can be calculated directly from) | ||
#the sum of squared interpoint distancesdivided by the number of points | ||
# https://onlinelibrary.wiley.com/doi/epdf/10.1111/j.1442-9993.2001.01070.pp.x | ||
|
||
SS(D,N) = sum(D .^2)/N | ||
|
||
function F(D,W,N,n,a) | ||
SST = SS(D,N) | ||
SSW = SS(W,n) | ||
SSA = SST - SSW | ||
return (SSA/(a-1))/(SSW/(N-a)) | ||
end | ||
|
||
function F(SST ::Real,W,N,n,a) | ||
|
||
SSW = SS(W,n) | ||
SSA = SST - SSW | ||
return (SSA/(a-1))/(SSW/(N-a)) | ||
end | ||
|
||
function getinds(group,N) | ||
W =Vector{CartesianIndex}(undef,0) | ||
notW = Vector{CartesianIndex}(undef,0) | ||
for i in 1:(N-1) | ||
for j in i+1:N | ||
if i !==1j | ||
if group[i] == group[j] | ||
push!(W,(CartesianIndex(i,j))) | ||
else push!(notW,(CartesianIndex(i,j))) | ||
end | ||
end | ||
end | ||
end | ||
return W, notW | ||
end | ||
|
||
function perm1(D,group, n_perm = 1000) | ||
N = length(group) | ||
a = length(unique(group)) | ||
n = N/a | ||
Winds ,notWinds =getinds(group,N) | ||
|
||
@inbounds W = D[Winds] | ||
@inbounds notW= D[notWinds] | ||
|
||
Dvec = vcat(W,notW) | ||
|
||
SST = SS(Dvec,N) | ||
|
||
f = F(SST,W,N,n,a) | ||
Fs = Vector{Float64}(undef,n_perm) | ||
inds = 1:length(W) | ||
|
||
@inbounds for i in 1:n_perm | ||
shuffle!(Dvec) | ||
Fs[i] = F(SST,view(Dvec,inds),N,n,a) | ||
end | ||
P = sum(Fs .>= f)/n_perm | ||
|
||
g = unique(group) | ||
if a>2 | ||
ppairs = NamedArray(zeros(a,a),( g,g ), ("group","group")) | ||
tpairs = NamedArray(zeros(a,a),( g,g ), ("group","group")) | ||
Threads.@threads for i in 1:a-1 | ||
for j in i+1:a | ||
boolmask = (group .== g[i]) .| (group .== g[j]) | ||
d = D[boolmask,boolmask] | ||
fstat,pstat =permutest(d,group[boolmask], n_perm) | ||
ppairs[j,i] = pstat | ||
tpairs[j,i] = sqrt(fstat) | ||
end | ||
end | ||
return (F = f,P = P, fpairs = tpairs,ppairs = ppairs) | ||
else | ||
return (F = f,P = P) | ||
end | ||
|
||
end |
Oops, something went wrong.