thibautjombart / adegenet

adegenet: a R package for the multivariate analysis of genetic markers
165 stars 64 forks source link

Geographic Distance (IBD) #359

Closed isapetitta closed 3 months ago

isapetitta commented 3 months ago

Hi! I am using the Isolation by Distance function for my data. When I use dist() to calculate geographic distance of my coordinates in the other column of my genepop object, what distance is being calculated and how? What units do I report?

Thanks! My code is below.

library(adegenet) lop <- genind2genpop(lup.genind) lop@other$xy<-lop[,3:4] lop

Dgen <- dist.genpop(lop,method=1) #genetic distance Dgeo <- dist(lop) #geographic distance

zkamvar commented 3 months ago

The units for euclidean distance are valueless unless you know how they are projected. If they are in degrees (lat/lon), then it might not be possible because you need to account for the curvature of the earth (see https://gis.stackexchange.com/a/380269).

I also notice that there is something that could be improved with the code to get you to a point where you are calculating geographic distance. I've commented below what your code is actually doing:

library(adegenet)                # 1. load adegenet
lop <- genind2genpop(lup.genind) # 2. transform lup.genind to a genpop object
lop@other$xy<-lop[,3:4]          # 3. subset the genpop object to the 
                                 #    3rd and 4th alleles and assign that object 
                                 #    to the $xy object in the @other slot
lop

Dgen <- dist.genpop(lop,method=1) # 4. calculate Nei's 1972 genetic distance
Dgeo <- dist(lop)                 # 5. calculate Nei's 1972 genetic distance
  1. The assignment of the XY coordinates are actually assigning a new genpop object:
    
    library(adegenet)
    data(nancycats)
    nanpop <- genind2genpop(nancycats)

The XY object from nancycats is already converted to the mean coordinates

there is no need to re-assign

dim(other(nanpop)$xy)

> [1] 17 2

class(other(nanpop)$xy)

> [1] "matrix" "array"

This replaces the coordinate matrix with the last two loci of the genpop object

other(nanpop)$xy <- nanpop[,8:9] dim(other(nanpop)$xy)

> NULL

class(other(nanpop)$xy)

> [1] "genpop"

> attr(,"package")

> [1] "adegenet"


<sup>Created on 2024-04-03 with [reprex v2.0.2](https://reprex.tidyverse.org)</sup>

The second point: these two calls are exactly the same.

```r
dist.genpop(lop, method = 1)
dist(lop)

If you want to get geographic distance, take a look at section 7 of the basics tutorial.

dist.genpop(lop, method = 1) # genetic distance
dist(other(lop)$xy) # geographic distance
isapetitta commented 3 months ago

Thank you!!