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

View source: R/near.obs.soil.R

near.obs.soilR Documentation

Finds n nearest observations from given locations for soil mapping.

Description

The function finds n nearest observations from given locations and at specific depth range from location min depth 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. It is based on knn function of package nabor.

Usage

near.obs.soil(locations,
              locations.x.y.md = c(1,2,3),
              observations,
              observations.x.y.md = c(1,2,3),
              obs.col = 4,
              n.obs = 5,
              depth.range = 0.1,
              no.obs = 'increase',
              parallel.processing = TRUE,
              pp.type = "doParallel", # "snowfall"
              cpus = detectCores()-1)

Arguments

locations

data.frame with x and y coordinates and mid depth 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.md

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

observations

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

observations.x.y.md

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

obs.col

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

n.obs

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

depth.range

numeric; Depth range for location mid depth in which to search for nearest observations. It's in the mid depth units. Default is 0.1.

no.obs

character; Possible values are increase (default) and exactly. If set to increase, in case if there is no n.obs observations in depth.range for a specific location, the depth.range is increased (multiplied by 2, 3, ...) until the number of observations are larger or equal to n.obs. If set to exactly, the function will raise an error when it come to the first location with no n.obs observations in specified depth.range.

parallel.processing

logical; If parallel processing is performed. Default is FALSE.

pp.type

character; Type (R package) used for parallel processing, "doParallel" (default) or "snowfall".

cpus

numeric; Number of processing units. Default is detectCores()-1.

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.

Note

The function is intended for soil mapping applications.

Author(s)

Aleksandar Sekulic asekulic@grf.bg.ac.rs, Anatol Helfenstein anatol.helfenstein@wur.nl

See Also

knn near.obs rfsi pred.rfsi tune.rfsi cv.rfsi

Examples

library(sp)
library(sf)
library(meteo)
# prepare data
# load observation - data.frame of mean temperatures
demo(meuse, echo=FALSE)
meuse <- meuse[complete.cases(meuse@data),]
locations = st_as_sf(meuse, coords = c("x", "y"), crs = 28992, agr = "constant")
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.soil(locations = locations, # from
                             locations.x.y.md =  c("x","y","dist"),
                             observations = observations, # to
                             observations.x.y.md= c("x","y","dist"),
                             obs.col = "zinc",
                             n.obs = 5) # number of nearest observations
str(nearest_obs)
summary(nearest_obs)

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