You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
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.
The text was updated successfully, but these errors were encountered:
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.
The text was updated successfully, but these errors were encountered: