near.obs: Finds n nearest observations from given locations.

View source: R/near.obs.R

near.obsR Documentation

Finds n nearest observations from given locations.

Description

The function finds n nearest observations from given locations and creates an object of data.frame class. First n columns are Euclidean distances to n nearest locations and next n columns are observations at n nearest stations, and rows are given locations. Further more it can calculate averages in circles with different radiuses, can find nearest observation in quadrants (directions) and calculate IDW predictions from nearest observations. It is based on knn function of package nabor.

Usage

near.obs(locations,
         locations.x.y = c(1,2),
         observations,
         observations.x.y = c(1,2),
         obs.col = 3,
         n.obs = 10,
         rm.dupl = TRUE,
         avg = FALSE,
         increment,
         range,
         quadrant = FALSE,
         idw=FALSE,
         idw.p=2)

Arguments

locations

data.frame with x and y coordinates columns, or sf-class, SpatVector-class or SpatRaster-class object; Locations (FROM) for which n nearest observations are found and distances are calculated.

locations.x.y

numeric or character vector; Positions or names of the x and y columns in locations if data.frame. Default is c(1,2).

observations

data.frame with x, y and observation columns, or sf-class or SpatVector-class object with an observation column; Observations (TO).

observations.x.y

numeric or character vector; Positions or names of the x and y columns in observations if data.frame. Default is c(1,2).

obs.col

numeric or character; Column name or number showing position of the observation column in the observations. Default is 3.

n.obs

numeric; Number of nearest observations to be found. Note that it cannot be larger than number of obsevrations. Default is 10.

rm.dupl

boolean; Remove spatial duplicates - will the spatial duplicates (nearest observations where Euclidean distance is 0) be removed from the result. Default is TRUE.

avg

boolean; Averages in circles - will averages in circles with different radiuses be calculated. Default is FALSE.

increment

numeric; Increment of radiuses for calculation of averages in circles with different radiuses. Units depends on CRS.

range

numeric; Maximum radius for calculation of averages in circles with different radiuses. Units depends on CRS.

quadrant

boolean; Nearest observations in quadrants - will nearest observation in quadrants be calculated. Default is FALSE.

idw

boolean; IDW prediction as predictor - will IDW predictions from n.obs nearest observations be calculated. Default is FALSE.

idw.p

numeric; Exponent parameter for IDW weights. Default is 2.

Value

data.frame object. Rows represents specific locations. First n.obs columns are Euclidean distances to n.obs nearest observations. Next n.obs columns are observations at n.obs nearest stations. The following columns are averages in circles with different radiuses if avg is set to TRUE. The following columns are nearest observation in quadrants if direct is set to TRUE. The following columns are IDW prediction from nearest observation if idw is set to TRUE.

Note

The function can be used in any case if it is needed to find n nearest observations from given locations and distances to them.

Author(s)

Aleksandar Sekulic asekulic@grf.bg.ac.rs

References

Sekulić, A., Kilibarda, M., Heuvelink, G. B., Nikolić, M. & Bajat, B. Random Forest Spatial Interpolation. Remote. Sens. 12, 1687, https://doi.org/10.3390/rs12101687 (2020).

See Also

knn rfsi pred.rfsi tune.rfsi cv.rfsi

Examples

library(sp)
library(sf)
library(terra)
library(meteo)
# prepare data
# load observation - data.frame of mean temperatures
demo(meuse, echo=FALSE)
meuse <- meuse[complete.cases(meuse@data),]
locations = terra::rast(meuse.grid)
observations = st_as_sf(meuse, coords = c("x", "y"), crs = 28992, agr = "constant")
# find 5 nearest observations and distances to them (remove duplicates)
nearest_obs <- near.obs(locations = locations, # from
                        observations = observations, # to
                        obs.col = "zinc",
                        n.obs = 5, # number of nearest observations
                        rm.dupl = TRUE) 
str(nearest_obs)
summary(nearest_obs)

meteo documentation built on Oct. 14, 2023, 5:07 p.m.