constraints_dt: Establishes per pixel probability of fields being converted...

Description Usage Arguments Details Value Examples

View source: R/constraints_dt.R

Description

This function determines the per pixel probability of a field being converted for a crop as a function of that crop's yield, as well as any constraints related to carbon, biodiversity, and travel costs, factoring in any prior yield modifications made (e.g. due to climate change or added irrigation). This version uses data.tables rather than raster and reframes the constraints as per yield potential.

Usage

1
constraints_dt(inlist, cbetas, code, cropnames, ctype = "X", silent = TRUE)

Arguments

inlist

A list of data.tables for the four constraints

cbetas

4 element vector (values 0-1) containing land use weights

code

Unique simulation code resulting from run_code function

cropnames

Vector of crop names in analysis

ctype

Specific multiplicative ("X") or additive ("+") constraints

silent

TRUE, otherwise FALSE gives verbose mode

Details

For inlist, the input should be a named list, with the first element named "y_std, then "C", "bd", and "cost".

Value

data.table of conversion probabilities for each crop

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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
rc <- run_code(input_key = "ZA")
il <- fetch_inputs(input_key = "ZA")  # fetch all necessary inputs
il_dt <- raster_list_to_dt(inlist = il[c("p_yield", "pp_curr", "carbon_p",
                                         "cons_p", "cost")], base = TRUE)
ybetas <- list(1, 1)
ybeta <- yield_mod_dt(inlist = il_dt[[2]][c("p_yield", "pp_curr")],
                      ybetas = ybetas, code = rc, cropnames = il$cropnames)
clist <- list("y_std" = ybeta$y_std, "C" = il_dt[[2]]$carbon_p,
              "bd" = il_dt[[2]]$cons_p, "cost" = il_dt[[2]]$cost)
prj <- projection(il$pp_curr)
base <- il_dt[[1]][, c("x", "y"), with = FALSE]

# Checking case where yields are excluded
cbetas <- c("y_std" = 0, "C" = 1, "bd" = 1, "cost" = 1)
con1 <- constraints_dt(inlist = clist, cbetas = cbetas, code = rc,
                       cropnames = il$cropnames)
#con1[, lapply(.SD, max, na.rm = TRUE)]

# all four constraints
cbetas <- c("y_std" = 1, "C" = 1, "bd" = 1, "cost" = 1)
con2 <- constraints_dt(inlist = clist, cbetas = cbetas, code = rc,
                       cropnames = il$cropnames)
con2r <- dt_list_to_raster(base = base, inlist = list(con2),
                           CRSobj = prj)[[1]]
plot(con2r - ybetar$y_std * il$carbon_p * il$cons_p * il$cost))
round(cellStats(con2r - (ybetar$y_std * il$carbon_p * il$cons_p * il$cost),
                range), 7)  #' R inferno
con2b <- constraints_r(inlist = clistr, cbetas = cbetas, code = rc,
                       cropnames = il$cropnames)
plot(con2r - con2b)  # equivalent to constraints_r

# yield and carbon
cbetas <- c("y_std" = 1, "C" = 1, "bd" = 0, "cost" = 0)
con3 <- constraints_dt(inlist = clist, cbetas = cbetas, code = rc,
                       cropnames = il$cropnames)
con3r <- dt_list_to_raster(base = base, inlist = list(con3),
                           CRSobj = prj)[[1]]
plot(con3r - ybetar$y_std * il$carbon_p)  #' stacks up against basic raster math
con3b <- constraints_r(inlist = clistr, cbetas = cbetas, code = rc,
                       cropnames = il$cropnames)
plot(con3r - con3b)  # equivalent to constraints_r

# carbon and bd
cbetas <- c("y_std" = 0, "C" = 1, "bd" = 1, "cost" = 0)
con4 <- constraints_dt(inlist = clist, cbetas = cbetas, code = rc,
                       cropnames = il$cropnames)
con4r <- dt_list_to_raster(base = base, inlist = list(con4),
                           CRSobj = prj)[[1]]
plot(con4r - il$carbon_p * il$cons_p)  #' stacks up against basic raster math
con4b <- constraints_r(inlist = clistr, cbetas = cbetas, code = rc,
                       cropnames = il$cropnames)
plot(con4r - con4b)  # equivalent to constraints_r

# all four, but cost partial
cbetas <- c("y_std" = 1, "C" = 1, "bd" = 1, "cost" = 0.5)
con5 <- constraints_dt(inlist = clist, cbetas = cbetas, code = rc,
                       cropnames = il$cropnames)
con5r <- dt_list_to_raster(base = base, inlist = list(con5),
                           CRSobj = prj)[[1]]
plot(con5r - ybetar$y_std * il$carbon_p * il$cons_p * il$cost * 0.5)
round(cellStats(con5r - ybetar$y_std * il$carbon_p * il$cons_p * il$cost*0.5,
                range), 10)  #' R inferno
con5b <- constraints_r(inlist = clistr, cbetas = cbetas, code = rc,
                       cropnames = il$cropnames)
plot(con5r - con5b)  # equivalent to constraints_r

# no yield, all 3 constrains, but cost partial
cbetas <- c("y_std" = 0, "C" = 1, "bd" = 0.5, "cost" = 1)
con6 <- constraints_dt(inlist = clist, cbetas = cbetas, code = rc,
                       cropnames = il$cropnames)
con6r <- dt_list_to_raster(base = base, inlist = list(con6),
                           CRSobj = prj)[[1]]
plot(con6r - il$carbon_p * il$cons_p * 0.5 * il$cost)
con6b <- constraints_r(inlist = clistr, cbetas = cbetas, code = rc,
                       cropnames = il$cropnames)
plot(con6r - con6b)  # equivalent to constraints_r

marcusspiegel/agroEcoTradeoff documentation built on May 21, 2019, 11:44 a.m.