Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

thin multiple species based on spec.col #19

Open
PhillRob opened this issue Apr 15, 2017 · 3 comments
Open

thin multiple species based on spec.col #19

PhillRob opened this issue Apr 15, 2017 · 3 comments

Comments

@PhillRob
Copy link

Is thin() looping through multiple species if unique(spec.col) >1? Can regions be used to separate out species instead?

@mlammens
Copy link
Owner

Hi @PhillRob. Both your questions point to good ideas for future advances. No, thin() doesn't loop through the different species right now. Though you could do this as a loop (or a subsetting) before running thin(). As for the region idea, that's something we meant to do, but haven't gotten to yet. Sorry.

@PhillRob
Copy link
Author

PhillRob commented Apr 19, 2017

Thanks @mlammens. I managed to loop through species using ddplyr on my 300k records gbif dataset, but it's too slow if you remove the timeout.

th<- function(x) {
    print(unique(x$species))
    results<-spThin(
        x,
        x.col = "decimalLatitude",
        y.col = "decimalLongitude",
        dist=1000,
        method="lpsolve",
        great.circle.distance=TRUE,
        timeout= 1000
    ) 
    return(as.data.frame(results[[1]]))
}
a<-ddply(.data = ISO.d.y.l.l.15,.(name),.fun = function(x) th(x))

To loop through the spatial dataframe using rarify and return usable results i put the data transformation steps inside a high level function.

rarefy.sp <- function(x) {
    print(unique(x$species))
    spDF <- SpatialPointsDataFrame(
        coords=as.matrix(x[,c("decimalLongitude", "decimalLatitude")]),
        data=x,
        proj4string=CRS('+proj=longlat +ellps=WGS84 +datum=WGS84 +no_def')
    )
    spDF <- spTransform(
        spDF,
        CRS('+proj=lcc +lat_1=9 +lat_2=3 +lat_0=6
            +lon_0=-66 +x_0=1000000 +y_0=1000000 
            +ellps=intl +towgs84=-288,175,-376,0,0,0,0 +units=m +no_defs')
        )
    # rarefy data
    rarefy <- spRarefy(
        spDF, 
        grid = 10000,
        cellsize = 1000, 
        nrep = 1000
    )
    rarefy.WSG <- spTransform(
        rarefy[[1]],
        CRS('+proj=longlat +ellps=WGS84 +datum=WGS84 +no_def')
    )
    
    return(as.data.frame(rarefy.WSG))
}

@Sandhyamutnali
Copy link

What is the difference between Spatially thin and Spatially Rerefy

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants