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 |
Checks attributes, geometries and projections of spatial data sets.
check( x = NULL, y = NULL, z = NULL, field = NULL, edge = 0, filter = 1, resolution = NULL )
check( x = NULL, y = NULL, z = NULL, field = NULL, edge = 0, filter = 1, resolution = NULL )
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. |
Intended for checking data in functions of mapsRinteractive.
A list with checked and corrected data sets together with a vector of logged feedback.
Calculates the Nash-Sutcliffe modelling efficiency (E) from observed and predicted values.
e(observed, predicted)
e(observed, predicted)
observed |
Numeric vector of observed values |
predicted |
Numeric vector of predicted values. The length shall be the same as for observed. |
E = 1 - sum(observed - predicted)/sum(observed - mean (observed))
The Nash-Sutcliffe modelling efficiency (E) calculated from observed and predicted values.
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.
o<-1:5 p<-c(2,2,4,3,5) e(observed=o, predicted=p)
o<-1:5 p<-c(2,2,4,3,5) e(observed=o, predicted=p)
Computes evaluation measures from observed and predicted data.
evaluate(df, observed, predicted)
evaluate(df, observed, predicted)
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. |
A data.frame with evaluation statistics. For details, see mri function.
df<-data.frame(obs=1:9, pred=c(2, 9, 10, 8, 3, 4, 6, 12, 1)) e<-evaluate(df, 'obs', 'pred') print(e)
df<-data.frame(obs=1:9, pred=c(2, 9, 10, 8, 3, 4, 6, 12, 1)) e<-evaluate(df, 'obs', 'pred') print(e)
Checks whether an integer is even.
even(x)
even(x)
x |
Integer. |
Logical value (TRUE or FALSE). TRUE means that the value is even.
even(3)
even(3)
Create a SpatVector of polygons from the extent of a spatial object.
extentpolygon(x)
extentpolygon(x)
x |
A spatial object. |
If x is projected, the SpatVector will also be projected
SpatVector of polygons.
Identification of the kth highest/lowest value(s).
kth( x = NULL, k = 2, highest = TRUE, index = FALSE, unique = FALSE, multiple = FALSE )
kth( x = NULL, k = 2, highest = TRUE, index = FALSE, unique = FALSE, multiple = FALSE )
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 |
NA values are removed.
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.
kth(x=1:20, k=3, highest=FALSE)
kth(x=1:20, k=3, highest=FALSE)
Calculates the mean absolute error (MAE) from observed and predicted values.
mae(observed, predicted)
mae(observed, predicted)
observed |
Numeric vector of observed values |
predicted |
Numeric vector of predicted values. The length shall be the same as for observed. |
mae = mean(abs(observed - predicted))
The mean absolute error (MAE) calculated from the observed and the predicted values.
o<-1:5 p<-c(2,2,4,3,5) mae(observed=o, predicted=p)
o<-1:5 p<-c(2,2,4,3,5) mae(observed=o, predicted=p)
Calculates the mean error (ME) from observed and predicted values.
me(observed, predicted)
me(observed, predicted)
observed |
Numeric vector of observed values |
predicted |
Numeric vector of predicted values. The length shall be the same as for observed. |
ME = bias = mean(observed - predicted)
The mean error (ME) calculated from the observed and the predicted values.
o<-1:5 p<-c(2,2,4,3,5) me(observed=o, predicted=p)
o<-1:5 p<-c(2,2,4,3,5) me(observed=o, predicted=p)
Local adaptation and evaluation of maps of continuous variables in raster format by use of point location data.
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 )
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 )
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. |
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.
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.
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.
#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)
#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)
Checks whether an integer is odd.
odd(x)
odd(x)
x |
Integer. |
Logical value (TRUE or FALSE). TRUE means that the value is odd.
odd(3)
odd(3)
Regression kriging using a standardized variogram.
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 )
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 )
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 |
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.
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.
Calculates the coefficient of determination (r2) for a linear regression model between predicted values and observed values.
r2(observed, predicted)
r2(observed, predicted)
observed |
Numeric vector of observed values |
predicted |
Numeric vector of predicted values. The length shall be the same as for observed. |
Coefficient of determination (r2) for a linear regression model between predicted values and observed values.
o<-1:5 p<-c(2,2,4,3,5) r2(observed=o, predicted=p)
o<-1:5 p<-c(2,2,4,3,5) r2(observed=o, predicted=p)
Regression kriging using a standardized variogram.
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 )
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 )
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 |
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.
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.
Regression kriging using a standardized variogram.
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 )
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 )
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 |
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.
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.
Calculates the root mean square error (RMSE) from observed and predicted values.
rmse(observed, predicted)
rmse(observed, predicted)
observed |
Numeric vector of observed values |
predicted |
Numeric vector of predicted values. The length shall be the same as for observed. |
rmse = sqrt(mean((observed - predicted)^2))
The root mean square err or (RMSE) calculated from the observed and the predicted values.
o<-1:5 p<-c(2,2,4,3,5) rmse(observed=o, predicted=p)
o<-1:5 p<-c(2,2,4,3,5) rmse(observed=o, predicted=p)