Package 'mapsRinteractive'

Title: Local Adaptation and Evaluation of Raster Maps
Description: Local adaptation and evaluation of maps of continuous attributes in raster format by use of point location data.
Authors: Kristin Persson [aut, cre, cph], Mats Soderstrom [ctb, cph], John Mutua [ctb]
Maintainer: Kristin Persson <[email protected]>
License: MIT + file LICENSE
Version: 2.0.1
Built: 2025-02-03 03:39:33 UTC
Source: https://github.com/ymutua/mapsrinteractive

Help Index


check

Description

Checks attributes, geometries and projections of spatial data sets.

Usage

check(
  x = NULL,
  y = NULL,
  z = NULL,
  field = NULL,
  edge = 0,
  filter = 1,
  resolution = NULL
)

Arguments

x

SpatRaster. Required. Must be have a defined Cartesian coordinate system. Data must be continuous. If more than one layer, the first layer will be used.

y

SpatVector of polygons. Optional. Delineates the area within which the raster layer shall be locally adapted and evaluated. If not provided, the analyses will be performed within the intersect of the raster and the sampled area. Must be have a defined Cartesian coordinate system (same as x).

z

SpatVector of points Required. Must have at least one column with numerical data and these data must be of the same entity and unit as x (specify this column by argument: field). Must be have a defined Cartesian coordinate system (same as x).

field

Character value. Required. Name of the column in y with the data that shall be used to locally adapt and evaluate the raster.

edge

Numeric value. Optional. Specifies the width (unit of the coordinate reference system) of a buffer zone inside the edge of the polygon that is excluded from the analyses. Allowed values are within the closed range of 0-10000.

filter

Positive integer. Optional. No of cells in the side of a square window for mean filtering of x. Filtering is done before any resampling (see argument: resolution). Allowed values are within the closed range of 1-20.

resolution

Positive numeric value. Optional. The resolution (m) to which the imported raster shall be resampled before the adaptation. Allowed values are within the closed range of 0.1-10000. In addition, a resolution that means more than 1E+8 raster cells is not allowed.

Details

Intended for checking data in functions of mapsRinteractive.

Value

A list with checked and corrected data sets together with a vector of logged feedback.


e

Description

Calculates the Nash-Sutcliffe modelling efficiency (E) from observed and predicted values.

Usage

e(observed, predicted)

Arguments

observed

Numeric vector of observed values

predicted

Numeric vector of predicted values. The length shall be the same as for observed.

Details

E = 1 - sum(observed - predicted)/sum(observed - mean (observed))

Value

The Nash-Sutcliffe modelling efficiency (E) calculated from observed and predicted values.

References

Nash, J. E., & Sutcliffe, J. V. (1970). River flow forecasting through conceptual models part I—A discussion of principles. Journal of hydrology, 10(3), 282-290.

Examples

o<-1:5
p<-c(2,2,4,3,5)
e(observed=o, predicted=p)

evaluate

Description

Computes evaluation measures from observed and predicted data.

Usage

evaluate(df, observed, predicted)

Arguments

df

Data.frame. Required. A data.frame with observed and predicted data.

observed

Character value. Required. The name of the column in df with predicted data.The data must be of class numeric.

predicted

Character value or vector. Required. The names of the column(s) in df with predicted data. The data must be of class numeric.

Value

A data.frame with evaluation statistics. For details, see mri function.

Examples

df<-data.frame(obs=1:9, pred=c(2, 9, 10, 8, 3, 4, 6, 12, 1))
e<-evaluate(df, 'obs', 'pred')
print(e)

even

Description

Checks whether an integer is even.

Usage

even(x)

Arguments

x

Integer.

Value

Logical value (TRUE or FALSE). TRUE means that the value is even.

Examples

even(3)

extentpolygon

Description

Create a SpatVector of polygons from the extent of a spatial object.

Usage

extentpolygon(x)

Arguments

x

A spatial object.

Details

If x is projected, the SpatVector will also be projected

Value

SpatVector of polygons.


kth

Description

Identification of the kth highest/lowest value(s).

Usage

kth(
  x = NULL,
  k = 2,
  highest = TRUE,
  index = FALSE,
  unique = FALSE,
  multiple = FALSE
)

Arguments

x

Numeric vector.

k

Positive integer. The order of the value to find. Default = 2, which means that the next highest/lowest values is identified.

highest

Logical. TRUE means that the kth highest value(s) is/are identified. FALSE means that the kth lowest value(s) is/are identified. Default = TRUE.

index

Logical. TRUE means that the index/indices of the kth highest/lowest value(s) is/are returned. FALSE means that the kth highest/lowest value itself is returned. If ties exist and argument multiple = TRUE, the returned value is a vector, else it is a value. Default = FALSE.

unique

Logical. TRUE means that duplicates are removed before the identification of the kth highest/lowest value(s). Default=FALSE

multiple

Logical. TRUE means that, If ties exist a vector of all values in x that are equal to the kth highest/lowest values is returned. FALSE means that one random value from the vector of index values is returned. Default=FALSE

Details

NA values are removed.

Value

If index = FALSE: the kth highest/lowest value is returned.

If index = TRUE: the index of the kth highest/lowest value (s) is/are returned.

Examples

kth(x=1:20, k=3, highest=FALSE)

mae

Description

Calculates the mean absolute error (MAE) from observed and predicted values.

Usage

mae(observed, predicted)

Arguments

observed

Numeric vector of observed values

predicted

Numeric vector of predicted values. The length shall be the same as for observed.

Details

mae = mean(abs(observed - predicted))

Value

The mean absolute error (MAE) calculated from the observed and the predicted values.

Examples

o<-1:5
p<-c(2,2,4,3,5)
mae(observed=o, predicted=p)

me

Description

Calculates the mean error (ME) from observed and predicted values.

Usage

me(observed, predicted)

Arguments

observed

Numeric vector of observed values

predicted

Numeric vector of predicted values. The length shall be the same as for observed.

Details

ME = bias = mean(observed - predicted)

Value

The mean error (ME) calculated from the observed and the predicted values.

Examples

o<-1:5
p<-c(2,2,4,3,5)
me(observed=o, predicted=p)

mri

Description

Local adaptation and evaluation of maps of continuous variables in raster format by use of point location data.

Usage

mri(
  x = NULL,
  y = NULL,
  z = NULL,
  field = NULL,
  edge = 0,
  filter = 1,
  resolution = NULL,
  md = "Sph",
  rg = NULL,
  ng = 0.1,
  check.data = TRUE
)

Arguments

x

SpatRaster. Required. Must be have a defined Cartesian coordinate system. Data must be continuous. If more than one layer, the first layer will be used.

y

SpatVector of polygons. Optional. Delineates the area within which the raster layer shall be locally adapted and evaluated. If not provided, the analyses will be performed within the intersect of the raster and the sampled area. Must be have a defined Cartesian coordinate system (same as x).

z

SpatVector of points Required. Must have at least one column with numerical data and these data must be of the same entity and unit as x (specify this column by argument: field). Must be have a defined Cartesian coordinate system (same as x).

field

Character value. Required. Name of the column in y with the data that shall be used to locally adapt and evaluate the raster.

edge

Numeric value. Optional. Specifies the width (unit of the coordinate reference system) of a buffer zone inside the edge of the polygon that is excluded from the analyses. Allowed values are within the closed range of 0-10000.

filter

Positive integer. Optional. No of cells in the side of a square window for mean filtering of x. Filtering is done before any resampling (see argument: resolution). Allowed values are within the closed range of 1-20.

resolution

Positive numeric value. Optional. The resolution (m) to which the imported raster shall be resampled before the adaptation. Allowed values are within the closed range of 0.1-10000. In addition, a resolution that means more than 1E+8 raster cells is not allowed.

md

Character value. Optional. Variogram model type for the standardized variograms used for ordinary kriging interpolation of observed data or residuals. Variograms are generated by gstat::vgm. Default is "Sph" (spherical model).

rg

Numeric value. Optional. Range of the standardized variograms used for ordinary kriging interpolation of observed data or residuals. Variograms are generated by gstat::vgm. If no rg is specified it will be set to half of the square root of the mapping area: y (possibly shrinked by edge).

ng

Numeric value. Optional. Nugget of the standardized variograms used for ordinary kriging interpolation of observed data or residuals. Variograms are generated by gstat::vgm. The nugget is expressed as a fraction of the sill. A ng = 0.1 means that the nugget is 10 percent of the sill. The sill is by default equal to the variance of the data to be kriged (i.e the point observations or the residuals). Allowed values of ng are within the closed range of 0-1.

check.data

Logical value. Default is TRUE. Shall attributes, geometries and projections of the input data (arguments x, y and z) be checked.

Details

The mri function is intended for local adaptation and evaluation of raster maps with continuous variables. A SpatRaster and a SpatVector of point data (same variable and unit as the raster) are required. A SpatVector of polygons can optionally be used to delineate the area for local adaptation and evaluation.

It is a requirement that all spatial objects (x, y and z) have the same projection. The analyses require a Cartesian coordinate reference system.

Four maps are (created and) evaluated: the original raster map, a map created solely based on the soil samples data (ordinary kriging using a standardized variogram), two maps based on a combination of the raster data and the point observations (regression kriging and residual kriging, both using standardized variograms).

The maps are evaluated by leave-one-out cross validation and a number of evaluation measures are computed: the Nash-Sutcliffe modelling efficiency (E), the mean absolute error (MAE; Janssen & Heuberger, 1995), the coefficient of determination of a linear regression between predicted and measured values (r2).

The mapped area is the intersection between the original raster map (argument: x), any provided SpatVector of polygons (argument: y) and the buffered point locations. The buffer width is 1.5*(next largest distance) between one point and its nearest neighbor).

The mapsRInteractive algorithmns have been described ad by Piikki et al.(2017) and Nijbroek et al. (2018), where more details can be found .

On error: check that required data are provided (arguments x, y, z and field), check that all spatal datasets (arguments x, y, z) are projected, check that they do overlap and check that the arguments edge, filter and resolution have appropriate values.

Value

A list with:

1) 'maps'. A raster stack of the original raster map ('map'), the map, created by ordinary kriging of observed data ('ordkrig'), by residual kriging ('reskrig') and by regression kriging ('regkrig').

2) 'area'. SpatVector of the polygon delineating the mapped area.

3) 'pts'. SpatVector of point locations used for mapping, i.e points falling within the mapped area, excluding points with NA values in the observed values or the values extacted from the original map. The column names mean: obs = observed values. map = original map values. ordkrig_cv = values from the leave-one-out cross validation of the ordinary kriging. res = residuals (map - obs) reskrig_cv = values from the leave-one-out cross validation of the residual kriging. regpred = predicted values from the linear regression (obs = a*map + b) regres = residuals (regpred - obs) regkrig_cv = values from the leave-one-out cross validation of the regression kriging.

4) 'evaluation'. a data.frame with evaluation statistics for the original map and the leave-one-out cross-validation of the other mapping methods.

5) 'feedback' a character vector with logged feedback on inputted and used data.

References

Nijbroek, R., Piikki, K., Söderström, M., Kempen, B., Turner, K. G., Hengari, S., & Mutua, J. (2018). Soil Organic Carbon Baselines for Land Degradation Neutrality: Map Accuracy and Cost Tradeoffs with Respect to Complexity in Otjozondjupa, Namibia. Sustainability, 10(5), 1610. doi:10.3390/su10051610

Piikki, K.,Söderström, M., Stadig, H. 2017. Local adaptation of a national digital soil map for use in precision agriculture. Adv. Anim. Biosci. 8, 430–432.

Janssen, P.H.M.; Heuberger, P.S.C.1995. Calibration of process-oriented models. Ecol. Model., 831, 55–66.

Nash, J.E.; Sutcliffe, J.V. River flow forecasting through conceptual models part I—A discussion of principles. J. Hydrol. 1970, 103, 282–290.

Examples

#load package
require(terra)

#create a synthetic example raster dataset
rr1<-rast(nrow=10, ncol=10, 
  vals= sample(1:4, 100, replace=TRUE), 
  crs=crs("EPSG:3857")
  )
rr2<-disagg(rr1, 4, 'bilinear')

#create an example SpatVector of points
p<-spatSample(x=rr1, size=30, values=TRUE, as.points=TRUE)

#do local evaluation and adaptation of the raster data based on the point data
m<-mri(x = rr2, z = p, field ="lyr.1")

##check evaluation measures
print(m$evaluation)
plot(m$maps)

even

Description

Checks whether an integer is odd.

Usage

odd(x)

Arguments

x

Integer.

Value

Logical value (TRUE or FALSE). TRUE means that the value is odd.

Examples

odd(3)

ordkrige

Description

Regression kriging using a standardized variogram.

Usage

ordkrige(
  x = NULL,
  y = NULL,
  z = NULL,
  field = NULL,
  edge = 0,
  filter = 1,
  resolution = NULL,
  md = "Sph",
  rg = NULL,
  ng = 0.1,
  check.data = TRUE,
  cross.validate = TRUE
)

Arguments

x

SpatRaster. Required. Must be have a defined Cartesian coordinate system. Data must be continuous. If more than one layer, the first layer will be used.

y

SpatVector of polygons. Optional. Delineates the area within which the raster layer shall be locally adapted and evaluated. If not provided, the analyses will be performed within the intersect of the raster and the sampled area. Must be have a defined Cartesian coordinate system (same as x).

z

SpatVector of points Required. Must have at least one column with numerical data and these data must be of the same entity and unit as x (specify this column by argument: field). Must be have a defined Cartesian coordinate system (same as x).

field

Character value. Required. Name of the column in y with the data that shall be used to locally adapt and evaluate the raster.

edge

Numeric value. Optional. Specifies the width (unit of the coordinate reference system) of a buffer zone inside the edge of the polygon that is excluded from the analyses. Allowed values are within the closed range of 0-10000.

filter

Positive integer. Optional. No of cells in the side of a square window for mean filtering of x. Filtering is done before any resampling (see argument: resolution). Allowed values are within the closed range of 1-20.

resolution

Positive numeric value. Optional. The resolution (m) to which the imported raster shall be resampled before the adaptation. Allowed values are within the closed range of 0.1-10000. In addition, a resolution that means more than 1E+8 raster cells is not allowed.

md

Character value. Optional. Variogram model type for the standardized variograms used for ordinary kriging interpolation of observed data or residuals. Variograms are generated by gstat::vgm. Default is "Sph" (spherical model).

rg

Numeric value. Optional. Range of the standardized variograms used for ordinary kriging interpolation of observed data or residuals. Variograms are generated by gstat::vgm. If no rg is specified it will be set to half of the square root of the mapping area: y (possibly shrinked by edge).

ng

Numeric value. Optional. Nugget of the standardized variograms used for ordinary kriging interpolation of observed data or residuals. Variograms are generated by gstat::vgm. The nugget is expressed as a fraction of the sill. A ng = 0.1 means that the nugget is 10 percent of the sill. The sill is by default equal to the variance of the data to be kriged (i.e the point observations or the residuals). Allowed values of ng are within the closed range of 0-1.

check.data

Logical value. Default is TRUE. Shall attributes, geometries and projections of the input data (arguments x, y and z) be checked.

cross.validate

Logical value. If TRUE, a leave-one-out cross-validation is performed

Details

This is the ordinary kriging function called by the mri function. It uses a standardized semivariogram model and requires a raster template for which predictions are made. For details, see documentation of the mri function.

Value

A list with 1) a raster layer with predicted values and 2) a SpatVector of points with predictions from a leave-one-out cross- validation For details, see mri function.


r2

Description

Calculates the coefficient of determination (r2) for a linear regression model between predicted values and observed values.

Usage

r2(observed, predicted)

Arguments

observed

Numeric vector of observed values

predicted

Numeric vector of predicted values. The length shall be the same as for observed.

Value

Coefficient of determination (r2) for a linear regression model between predicted values and observed values.

Examples

o<-1:5
p<-c(2,2,4,3,5)
r2(observed=o, predicted=p)

regkrige

Description

Regression kriging using a standardized variogram.

Usage

regkrige(
  x = NULL,
  y = NULL,
  z = NULL,
  field = NULL,
  edge = 0,
  filter = 1,
  resolution = NULL,
  md = "Sph",
  rg = NULL,
  ng = 0.1,
  check.data = TRUE,
  cross.validate = TRUE
)

Arguments

x

SpatRaster. Required. Must be have a defined Cartesian coordinate system. Data must be continuous. If more than one layer, the first layer will be used.

y

SpatVector of polygons. Optional. Delineates the area within which the raster layer shall be locally adapted and evaluated. If not provided, the analyses will be performed within the intersect of the raster and the sampled area. Must be have a defined Cartesian coordinate system (same as x).

z

SpatVector of points Required. Must have at least one column with numerical data and these data must be of the same entity and unit as x (specify this column by argument: field). Must be have a defined Cartesian coordinate system (same as x).

field

Character value. Required. Name of the column in y with the data that shall be used to locally adapt and evaluate the raster.

edge

Numeric value. Optional. Specifies the width (unit of the coordinate reference system) of a buffer zone inside the edge of the polygon that is excluded from the analyses. Allowed values are within the closed range of 0-10000.

filter

Positive integer. Optional. No of cells in the side of a square window for mean filtering of x. Filtering is done before any resampling (see argument: resolution). Allowed values are within the closed range of 1-20.

resolution

Positive numeric value. Optional. The resolution (m) to which the imported raster shall be resampled before the adaptation. Allowed values are within the closed range of 0.1-10000. In addition, a resolution that means more than 1E+8 raster cells is not allowed.

md

Character value. Optional. Variogram model type for the standardized variograms used for ordinary kriging interpolation of observed data or residuals. Variograms are generated by gstat::vgm. Default is "Sph" (spherical model).

rg

Numeric value. Optional. Range of the standardized variograms used for ordinary kriging interpolation of observed data or residuals. Variograms are generated by gstat::vgm. If no rg is specified it will be set to half of the square root of the mapping area: y (possibly shrinked by edge).

ng

Numeric value. Optional. Nugget of the standardized variograms used for ordinary kriging interpolation of observed data or residuals. Variograms are generated by gstat::vgm. The nugget is expressed as a fraction of the sill. A ng = 0.1 means that the nugget is 10 percent of the sill. The sill is by default equal to the variance of the data to be kriged (i.e the point observations or the residuals). Allowed values of ng are within the closed range of 0-1.

check.data

Logical value. Default is TRUE. Shall attributes, geometries and projections of the input data (arguments x, y and z) be checked.

cross.validate

Logical value. If TRUE, a leave-one-out cross-validation is performed

Details

This is the ordinary kriging function called by the mri function. It uses a standardized semivariogram model and requires a raster template for which predictions are made. For details, see documentation of the mri function.

Value

A list with 1) a raster layer with predicted values and 2) if cross.validate=T, a SpatVector of points with predictions from a leave-one-out cross-validation. For details, see mri function.


reskrige

Description

Regression kriging using a standardized variogram.

Usage

reskrige(
  x = NULL,
  y = NULL,
  z = NULL,
  field = NULL,
  edge = 0,
  filter = 1,
  resolution = NULL,
  md = "Sph",
  rg = NULL,
  ng = 0.1,
  check.data = TRUE,
  cross.validate = TRUE
)

Arguments

x

SpatRaster. Required. Must be have a defined Cartesian coordinate system. Data must be continuous. If more than one layer, the first layer will be used.

y

SpatVector of polygons. Optional. Delineates the area within which the raster layer shall be locally adapted and evaluated. If not provided, the analyses will be performed within the intersect of the raster and the sampled area. Must be have a defined Cartesian coordinate system (same as x).

z

SpatVector of points Required. Must have at least one column with numerical data and these data must be of the same entity and unit as x (specify this column by argument: field). Must be have a defined Cartesian coordinate system (same as x).

field

Character value. Required. Name of the column in y with the data that shall be used to locally adapt and evaluate the raster.

edge

Numeric value. Optional. Specifies the width (unit of the coordinate reference system) of a buffer zone inside the edge of the polygon that is excluded from the analyses. Allowed values are within the closed range of 0-10000.

filter

Positive integer. Optional. No of cells in the side of a square window for mean filtering of x. Filtering is done before any resampling (see argument: resolution). Allowed values are within the closed range of 1-20.

resolution

Positive numeric value. Optional. The resolution (m) to which the imported raster shall be resampled before the adaptation. Allowed values are within the closed range of 0.1-10000. In addition, a resolution that means more than 1E+8 raster cells is not allowed.

md

Character value. Optional. Variogram model type for the standardized variograms used for ordinary kriging interpolation of observed data or residuals. Variograms are generated by gstat::vgm. Default is "Sph" (spherical model).

rg

Numeric value. Optional. Range of the standardized variograms used for ordinary kriging interpolation of observed data or residuals. Variograms are generated by gstat::vgm. If no rg is specified it will be set to half of the square root of the mapping area: y (possibly shrinked by edge).

ng

Numeric value. Optional. Nugget of the standardized variograms used for ordinary kriging interpolation of observed data or residuals. Variograms are generated by gstat::vgm. The nugget is expressed as a fraction of the sill. A ng = 0.1 means that the nugget is 10 percent of the sill. The sill is by default equal to the variance of the data to be kriged (i.e the point observations or the residuals). Allowed values of ng are within the closed range of 0-1.

check.data

Logical value. Default is TRUE. Shall attributes, geometries and projections of the input data (arguments x, y and z) be checked.

cross.validate

Logical value. If TRUE, a leave-one-out cross-validation is performed

Details

This is the ordinary kriging function called by the mri function. It uses a standardized semivariogram model and requires a raster template for which predictions are made. For details, see documentation of the mri function.

Value

A list with 1) a raster layer with predicted values and 2) if cross.validate=T, a SpatVector of points with predictions from a leave-one-out cross-validation. For details, see mri function.


rmse

Description

Calculates the root mean square error (RMSE) from observed and predicted values.

Usage

rmse(observed, predicted)

Arguments

observed

Numeric vector of observed values

predicted

Numeric vector of predicted values. The length shall be the same as for observed.

Details

rmse = sqrt(mean((observed - predicted)^2))

Value

The root mean square err or (RMSE) calculated from the observed and the predicted values.

Examples

o<-1:5
p<-c(2,2,4,3,5)
rmse(observed=o, predicted=p)