Hi Kirsten,
Could you please test whether the following function solves the problem.
Please let us know whether it worked or not.
Regards,
Luis
gl.ibd_fix
<- function(gl = NULL, Dgen = NULL, Dgeo = NULL, projected = FALSE,
permutations = 999, plot = TRUE)
{
if (!(requireNamespace("dismo", quietly = TRUE)))
{
stop("Package dismo needed for
this function to work. Please install it.")
}
else {
if (!is.null(Dgen) &
!is.null(Dgeo))
cat("Analysis performed on provided genetic and Euclidean distance
matrices.")
if (class(gl) ==
"genlight") {
cat("Standard analysis performed on the genlight object. Mantel test and
plot will be Fst/1-Fst versus log(distance)\n")
if
(nrow(gl@other$latlong) != nInd(gl))
stop("Cannot find coordinates for each individual in slot
@other$latlong")
if
(sum(match(names(gl@other$latlong), "long"), na.rm = T) == 1)
gl@other$latlong$lon <- gl@other$latlong$long
if
(!projected) {
xy <- dismo::Mercator(gl@other$latlong[, c("lon",
"lat")])
cat("Coordinates transformed to Mercator (google) projection to calculate
distances in meters.\n")
}
else {
xy = gl@other$latlong[, c("lon", "lat")]
cat("Coordinates not transformed. Distances calculated on the provided
coordinates.")
}
pop.xy <-
apply(xy, 2, function(a) tapply(a, pop(gl),
mean, na.rm = T))
Dgeo <-
dist(pop.xy)
Dgeo <-
log(Dgeo)
Dgen <-
as.dist(StAMPP::stamppFst(gl, nboots = 1))
Dgen <-
Dgen/(1 - Dgen)
ordering
<- levels(pop(gl))
Dgen <-
as.dist(as.matrix(Dgen)[ordering, ordering])
Dgeo <-
as.dist(as.matrix(Dgeo)[ordering, ordering])
}
miss = FALSE
if (sum(is.na(Dgen)) > 0 |
sum(is.infinite(Dgen)) > 0 ) {
miss = TRUE
cat("There are missing values in the genetic distance matrix. No kernel
distance plot is possible.\n")
}
if (sum(is.na(Dgeo)) > 0 |
sum(is.infinite(Dgeo)) > 0 ) {
miss = TRUE
cat("There are missing values in the geographic distance matrix. No kernel
distance plot is possible.\n")
}
manteltest <- vegan::mantel(Dgen,
Dgeo, na.rm = TRUE, permutations = 999)
print(manteltest)
if (plot) {
if (!miss) {
dens <- MASS::kde2d(Dgeo, Dgen, n = 300)
myPal <- colorRampPalette(c("white", "blue",
"gold", "orange", "red"))
plot(Dgeo, Dgen, pch = 20, cex = 0.8)
image(dens, col = transp(myPal(300), 0.7), add = TRUE)
points(Dgeo, Dgen, pch = 20, cex = 0.8)
abline(lm(Dgen ~ Dgeo))
title("Isolation by distance")
}
else {
plot(Dgeo, Dgen)
abline(lm(Dgen ~ Dgeo))
title("Isolation by distance")
}
}
out <- list(Dgen = Dgen, Dgeo =
Dgeo, mantel = manteltest)
return(out)
}
}


--
You received this message because you are subscribed to a topic in the Google Groups "dartR" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/dartr/lULh7pMe3vM/unsubscribe.
To unsubscribe from this group and all its topics, send an email to dartr+un...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/dartr/56f295a9-9815-4174-82bd-78551bfde904n%40googlegroups.com.
> gl.make.recode.pop(gl, outfile = "new_pop_assignments.csv",outpath = getwd())
This will generate a csv file with two columns, the first containing the existing population assignments, and the second also containing those assignments ready for editing to achieve the reassignments. This editing is best done in Excel. The population reassignments are then applied using:
> glnew <- gl.recode.pop(gl, pop.recode = "new_pop_assignments.csv")
You can check that the new assignments have been applied with:
> levels(pop(gl))
To view this discussion on the web visit https://groups.google.com/d/msgid/dartr/5dd61f43-7fa9-40c2-add7-2132f5195e9cn%40googlegroups.com.