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

GetNearestCovariate.R issues (NAs, run time, projection) #6

Open
LeaDambly opened this issue Jul 16, 2021 · 1 comment
Open

GetNearestCovariate.R issues (NAs, run time, projection) #6

LeaDambly opened this issue Jul 16, 2021 · 1 comment

Comments

@LeaDambly
Copy link

I've got a few issues with GetNearestCovariate.R. If I understand correctly, then the function is meant to get the spatially closest covariate value for a data (or integration) point. I am experiencing an issue where I end up with NAs assigned to the points. Sometimes as high as~20% of the points. There's also the issue of long run-time and the function only working for Lat/Long data. I have made some changes which would improve on this. Using the original function on 717 points with 3 covariates leads me to to get 10 points with NAs and a run time of approx. 50 seconds.
If I use the code below instead, I get 0 NAs and a run time of approx, 0.1 secs. Note that the nn2 function technically doesn't return NAs but instead "If there are no neighbours then nn.idx will contain 0 and nn.dists will contain 1.340781e+154 for that point". Not getting any of those either though.

` require(RANN)
require(dplyr)
if(class(points)=="SpatialPointsDataFrame") {
points <- points@coords
}
covnames <- names(covariates@data)
pointsXY <- as.data.frame(points)
names(pointsXY) <- c("X", "Y")
covXY <- cbind(covariates@coords, covariates@data)
covXY$ID <- seq(nrow(covXY))
closest <- nn2(covXY[c("X", "Y")], pointsXY[c("X", "Y")], k = 1)
closest <- as.data.frame(closest) %>%
rename(ID = nn.idx)
joined <- inner_join(closest, covXY, by = "ID") %>%
dplyr::select(-c(nn.dists, X, Y, ID))
allpts <- bind_cols(pointsXY, joined)

points.df <- SpatialPointsDataFrame(coords = allpts[c("X", "Y")],
data = allpts[covnames],
proj4string = CRS(proj4string(covs)))
return(points.df)`

I haven't tested this substantially (it works for my data so that doesn't necessarily mean anything) but I would definitely recommend using nn2 instead of spDistsN1. If only to save a lot of time.

@oharar
Copy link
Owner

oharar commented Jul 28, 2021

Thanks. I'm not surprised there was something faster than my code, so I appreciate you taking the time to improve it.

I'll pass this on to Philip, who is working on the package (updating it to work with INLAbru).

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

2 participants