# NOT RUN {
# }
# NOT RUN {
data(eco.test)
#---------------------------------------------------------------------------#
#####################
# LOCAL MORAN'S I
#####################
DETAILED EXAMPLE
#-------------------------
# TESTING PHENOTYPIC DATA-
#-------------------------
set.seed(10)
# test for a single variable---------------------------------
#computing weights
con <- eco.weight(eco[["XY"]], method = "knearest", k = 4, row.sd = TRUE)
# row standardized weights = TRUE
# test for the first trait of the data frame P
localmoran <- eco.lsa(eco[["P"]][, 1], con, method = "I", nsim = 99)
# "rankplot" graph
eco.plotLocal(localmoran)
# test for several variables---------------------------------
# ordering the factor "pop" in increasing order and the object "eco"
# in relation to this ordered factor prior to the multivariate analysis.
# This step is important for "localplot" graphs.
eco <- eco[order(eco[["S"]][,1])]
#computing weights with the ordered object
con <- eco.weight(eco[["XY"]], method = "knearest", k = 4, row.sd = TRUE)
# row standardized weights = TRUE
all.traits <- eco.lsa(eco[["P"]], con, method = "I", nsim = 99)
# Plot of the phenotypic spatial patterns
# "rasterplot" graph
eco.plotLocal(all.traits)
# in grey: non significant results (P > 0.05)
# set significant = FALSE for showing significant and no significant results
eco.plotLocal(all.traits, significant = FALSE)
# single plots using "rankplot" graphs
all.single.traits <- eco.lsa(eco[["P"]],con, method = "I", nsim = 99, multi="list")
eco.plotLocal(all.single.traits)
# removing legends for a better visualization
eco.plotLocal(all.single.traits, legend = FALSE)
# - individual plots support ggplot2 sintax (plot equivalent to the previous):
eco.plotLocal(all.single.traits) + ggplot2::theme(legend.position="none")
#-------------------------
# TESTING GENOTYPIC DATA-
#-------------------------
# eco[["A"]] is a matrix with the genetic data of "eco"
# as frequencies for each allele in each individual.
head(eco[["A"]]) # head of the matrix - 40 alleles
# ordering the factor "pop" in increasing order and the object "eco"
# in relation to this ordered factor prior to the multivariate analysis.
# This step is important for "localplot" graphs
data(eco.test) # for security this resets the data (unordered)
eco <- eco[order(eco[["S"]][,1])] # ordering
# computing weights with the ordered object
con <- eco.weight(eco[["XY"]], method = "knearest", k = 4, row.sd = TRUE)
# row standardized weights = TRUE
# test for a single allele
localmoran.geno <- eco.lsa(eco[["A"]][, 32], con, method = "I", nsim = 99)
# test for several alleles - 40 alleles (it runs in less than 1 min
# for 99 simulations per allele; 999 simulations takes ~ 11 s per allele,
# less than 8 min in total.)
all.alleles <- eco.lsa(eco[["A"]], con, method = "I", nsim = 99)
# plot all alleles to get an overview of the spatial patterns
eco.plotLocal(all.alleles)
# in grey: non significant results (P > 0.05)
# set significant = FALSE for showing significant and no significant results
eco.plotLocal(all.alleles, significant = FALSE)
# counting individuals with P < 0.05 for each allele (5 * 225 /100 ~ 12 significant tests
# by random)
signif <- apply(ecoslot.PVAL(all.alleles), 2, function(x) sum (x < 0.05))
# filtering alleles, loci with > 12 significant individual tests
A.local <- eco[["A"]][, signif > 12] #filtered matrix
all.alleles.f <- eco.lsa(eco[["A"]][, signif > 12] , con, method = "I", nsim = 99)
# Plot of the genotypic spatial patterns using "localplot" graphs
eco.plotLocal(all.alleles.f)
## using "rankplot" graphs
all.sf <- eco.lsa(A.local, 2, eco.lsa, con, method = "I", nsim = 99, multi = "list")
eco.plotLocal(all.sf, legend = FALSE)
#####################
# GETIS-ORD'S G*
#####################
con<- eco.weight(eco[["XY"]], method = "knearest", k = 4, self = TRUE) # self = TRUE for G*
getis.ak <- eco.lsa(eco[["P"]][, 1], con, method = "G*", nsim = 99, adjust = "none")
getis.ak
### to plot the results, the function "eco.lsa" calls "eco.rankplot"
### (see ?eco.rankplot) when test = "permutation" and "eco.forestplot" (see ?eco.forestplot)
### when test = "bootstrap"
p <- eco.plotLocal(getis.ak) # rankplot graph
p # points with colors of the color-scale:
# points with P < 0.05. Yellow points : points with P > 0.05
p <- eco.plotLocal(getis.ak, significant = FALSE)
p # all points have a color of the color-scale
#-----------------------
# ACCESSORS USE EXAMPLE
#-----------------------
# the slots are accessed with the generic format
# (ecoslot. + name of the slot + name of the object).
# See help("EcoGenetics accessors")
ecoslot.OUT(getis.ak)
## bootstrap example
getis.akb <- eco.lsa(eco[["P"]][, 1], con, method = "G*", nsim = 99, test = "bootstrap")
p <- eco.plotLocal(getis.akb) # forestplot graph
p2 <- eco.plotLocal(getis.akb, interactivePlot = FALSE)
p2 + ggplot2::theme_bw() # the plot can be modified with ggplot2
# In this case, the background is modified (white color)
#---------------------------------------------------------------------------#
#####################
# GETIS-ORD'S G
#####################
con <- eco.weight(eco[["XY"]], method = "knearest", k = 4)
# self = FALSE for G
getis <- eco.lsa(eco[["P"]][, 1], con, method = "G", nsim = 99)
eco.plotLocal(getis)
#---------------------------------------------------------------------------#
#####################
# LOCAL GEARY'S C
#####################
con<- eco.weight(eco[["XY"]], method = "knearest", k = 4, row.sd = TRUE)
# row standardized weights = TRUE
localgeary <- eco.lsa(eco[["P"]][, 1], con, method = "C", nsim = 99, adjust = "none")
eco.plotLocal(localgeary)
# }
# NOT RUN {
# }
Run the code above in your browser using DataLab