get_stats: get_stats

Description Usage Arguments Value Examples

View source: R/get_stats.R

Description

Calculate summary and spatial statistics across a single matrix or raster.

Usage

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
get_stats(
  img,
  id = NULL,
  calc_connectivity,
  conn_threshold = 1.5,
  patches = TRUE,
  style = "C",
  img_proj = NULL,
  img_extent = NULL,
  return_vals = c("df", "patches", "pstats"),
  sum_stats = c("mean", "min", "max")
)

Arguments

img

A numeric temperature matrix (such as that returned from Thermimage::raw2temp) or raster.

id

The image ID (optional). Useful when iterating over numerous images.

calc_connectivity

Whether or not to calculate cthermal connectivity across pixels (slow for large rasters). Defaults to TRUE.

conn_threshold

Climate threshold to use for calculation of thermal connectivity (i.e. the amount of change that organisms would be seeking to avoid). See ThermStats::connectivity.

patches

Whether to identify hot and cold spots. Defaults to TRUE.

style

Style to use when calculating neighbourhood weights using spdep::nb2listw. Defaults to 'C' (globally standardised).

img_proj

Spatial projection. Optional, but necessary for geographic data to plot correctly.

img_extent

Spatial extent. Optional, but necessary for geographic data to plot correctly.

return_vals

Which values to return? Any combination of the dataframe (df), SpatialPolygonsDataFrame of hot and cold patches (patches) and patch statistics dataframe (pstats). Note that pstats will always be returned – if this is not desired, use get_patches instead.

sum_stats

Summary statistics that should be calculated across all pixels. Several helper functions are included for use here: perc_5, perc_95, SHDI, SIDI, kurtosis and skewness.

Value

A list containing:

df

A dataframe with one row for each pixel, and variables denoting: the pixel value (val); the original spatial location of the pixel (x and y); its patch classification (G_bin) into a hot (1), cold (-1) or no patch (0) according to the Z value (see spdep::localG); the unique ID of the patch in which the pixel fell; and the matrix ID (if applicable).

patches

A SpatialPolygonsDataFrame of hot and cold patches. Hot patches have a value of 1, and cold patches a value of -1.

pstats

A dataframe with patch statistics for hot patches and cold patches, respectively. See patch_stats for details of all the statistics returned.

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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
## Not run: 
# FLIR temperature matrix ---------------------------------------------------

# Define individual matrix and raster
img <- flir11835$flir_matrix
val_raster <- 
    raster::raster(img,
                   xmn=0, xmx=ncol(img),
                   ymn=0, ymx=nrow(img))

# Define image ID (the photo number in this case)
id <- flir11835$photo_no

# Get stats!
get_stats(img = img,
          id = id,
          calc_connectivity = TRUE,
          conn_threshold = 1.5,
          patches = TRUE,
          style = "C",
          img_proj = NULL,
          img_extent = NULL,
          return_vals = "pstats",
          sum_stats = c("mean", "min","max"))

get_stats(img = val_raster,
          id = id,
          calc_connectivity = TRUE,
          conn_threshold = 1.5,
          patches = TRUE,
          style = "C",
          img_proj = NULL,
          img_extent = NULL,
          return_vals = "pstats",
          sum_stats = c("mean", "min","max"))

# Worldclim2 temperature raster ---------------------------------------------

# Dataset 'sulawesi_temp' represents mean January temperature for the
# island of Sulawesi

# Define projection and extent
img_proj <- raster::projection(sulawesi_temp)
img_extent <- raster::extent(sulawesi_temp)

# Find hot and cold patches
worldclim_results <-
 get_stats(img = sulawesi_temp,
           id = "sulawesi",
           calc_connectivity = FALSE,
           style = "C",
           img_proj = img_proj,
           img_extent = img_extent,
           return_vals = c("df", "patches", "pstats"),
           sum_stats = c("mean", "min","max"))

# Plot!
df <- worldclim_results$df
patches <- worldclim_results$patches
plot_patches(df, patches, print_plot = TRUE, save_plot = FALSE)

## End(Not run)

rasenior/ThermStats documentation built on Oct. 31, 2020, 3:48 p.m.