evaluate.grf: Interpolate the value of a GRF function in specified...

Description Usage Arguments Value Note Author(s) Examples

View source: R/evaluate-grf.R

Description

Interpolate the value of a GRF function in specified locations.

Usage

1
2
3
4
evaluate.grf(locations, grf.object, function.number = 1,
  function.name = NULL, periodic = TRUE,
  interpolation.method = "bicubic", rescale.method = "minmax",
  return.df = FALSE)

Arguments

locations

An N x 2 matrix containing the coordinates of the N locations of interest.

grf.object

The GRF object containing the function, generated by generate.grf.object.

function.number

The index of the function of interest.

function.name

The name of the function of interest.

periodic

If true, locations outside the rectangular domain of grf.object are mapped to the corresponding point inside the domain. Otherwise, the locations are mapped to the nearest point on the boundary. It is not recommended to use periodic = FALSE.

interpolation.method

Specifies the method used for interpolation. Either "nearest" (which uses the value of the nearest grid cell), "bilinear" (which performs bilinear interpolation based on the nearest four grid cells), or "bicubic" (which performs bicubic interpolation based on the nearest 16 neighbors).

rescale.method

Specifies how the interpolated values are rescaled. Either "none" (no rescaling), "minmax" (min-max scaling to the interval [0, 1]), or "uniform" (a scaling to [0, 1] that attempts to make all of the values equally represented).

return.df

If true, the return value is a data frame containing 3 columns: The x- and y-coordinates from locations, and the interpolated values. If false, only the interpolated values are returned.

Value

A numeric of length N containing the interpolated values of the GRF in the specified locations.

Note

Using interpolation.method = "bicubic" can lead to interpolated values that are outside the range of the original grid values. If this is combined with rescale.method = "minmax", then the resulting values are not guaranteed to be inside the interval [0, 1]. This can be fixed by mapping values greater than 1 to 1, and less than 0 to 0.

Author(s)

Mathias Isaksen mathiasleanderi@gmail.com

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# Comparison of different interpolation and rescaling methods
library(GRFics)
library(ggplot2)

# Create GRF object on a 20 x 20 grid
grf.object = generate.grf.object(-1, 1, -1, 1, 20, 20, num.functions = 1,
                                 range.parameter = 1,
                                 strength.parameter = 1, direction.parameter = pi/4,
                                 initial.seed = 123)

# Create 500 x 500 grid for interpolation
interp.grid = generate.grid.centers(-1, 1, -1, 1, 500, 500)
rescale.method = c("minmax", "uniform")
comparison.df = data.frame()
for (i in 1:2) {
  nearest.values = evaluate.grf(interp.grid, grf.object, interpolation.method = "nearest", rescale.method = rescale.method[i])
  bilinear.values = evaluate.grf(interp.grid, grf.object, interpolation.method = "bilinear", rescale.method = rescale.method[i])
  bicubic.values = evaluate.grf(interp.grid, grf.object, interpolation.method = "bicubic", rescale.method = rescale.method[i])
  comparison.df = rbind(comparison.df,
                        data.frame(
                          x = interp.grid$x, y = interp.grid$y,
                          z = c(nearest.values, bilinear.values, bicubic.values),
                          interpolation.method = rep(c("1. nearest", "2. bilinear", "3. bicubic"), each = nrow(interp.grid)),
                          rescale.method = rescale.method[i]))
}

# Show comparison plot using facet_grid
ggplot()+
  theme_bw()+
  geom_raster(data = comparison.df, aes(x = x, y = y, fill = z))+
  facet_grid(cols = vars(interpolation.method), rows = vars(rescale.method))+
  scale_fill_gradientn(colours = c("black", "white"))+
  coord_fixed()

mathiasisaksen/GRFics documentation built on May 20, 2021, 5:55 a.m.