near.obs {meteo}R 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, where 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 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),
         zcol = 3,
         n.obs = 10,
         rm.dupl = TRUE,
         avg = FALSE,
         increment,
         range,
         direct = FALSE,
         idw=FALSE,
         idw.p=2)

Arguments

locations

data.frame with x and y coordinates columns, or SpatialPoints-class, SpatialPointsDataFrame-class or SpatialPixelsDataFrame-class object. Locations from which distances to observations 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 SpatialPoints-class or SpatialPointsDataFrame-class object with observation column. Observations to which distances to locations are calculated.

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).

zcol

numeric or character; Column name or number showing the position of observations in 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; Will duplicates, i.e. nearest observations where Euclidean distance is 0, be removed from the result. Default is TRUE.

avg

boolean; 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 of coordinates.

range

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

direct

boolean; Will nearest observation in quadrants be calculated. Default is FALSE.

idw

boolean; 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 given 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

# prepare data
# load observation - data.frame of mean temperatures
data(dtempc) 
str(dtempc)
data(stations)
#
str(stations)
lonmin=18 ;lonmax=22.5 ; latmin=40 ;latmax=46
library(sp)
library(spacetime)
serbia = point.in.polygon(stations$lon, stations$lat, c(lonmin,lonmax,lonmax,lonmin), 
                          c(latmin,latmin,latmax,latmax))
stations= stations[ serbia!=0, ] # stations in Serbia approx.
# create STFDF
temp <- meteo2STFDF(dtempc,stations, crs= CRS('+proj=longlat +datum=WGS84'))
str(temp)
# get one day (SpatialPointsDataFrame)
temp <- temp[, 1]
# find 5 nearest observations and distances to them (remove duplicates)
nearest_obs <- near.obs(locations = temp,
                        observations = temp,
                        zcol = "tempc",
                        n.obs = 5,
                        rm.dupl = TRUE)
str(nearest_obs)

[Package meteo version 1.0-1 Index]