yield_mod: Modifies input data.tables by provided multipliers

Description Usage Arguments Details Note Examples

Description

This is a data.table-based version of this function.

Usage

1
yield_mod(inlist, ybetas, code, cropnames, silent = TRUE)

Arguments

inlist

Named list of crop yield data.table

ybetas

list of either 2 data.tables or 2 vectors providing modifications for climate & irrigation

code

Unique simulation code resulting from run_code function

cropnames

Names of simulated crops

silent

Hide or show print statements (TRUE [default] or FALSE)

Details

For ybetas, if rasters are provided, they should be as a single brick that maps the yield impacts of climate change/irrigation, or both, with 1 layer for each crop. For ybetas, if data.tables are provided, each data.table should provide one column to represent the impacts to each crop, or a single column that will apply to all crops. Vectors can alternatively be provided that apply a uniform yield modification. Vectors can alternatively be provided that apply a uniform yield modification. A single value can be provided, in which case it will be recycled across all assessed crops, or a vector equal in length to the number of crops being analyzed can be used. The first element of ybeta can be treated as a climate change modifier, as a way to modify the potential yield of each crop to investigate, say, the spatial patterns that will result if yield achieve only half their potential. The second element should be reserved for testing irrigation effects, for the sake of good practice.

Note

ybetas could also be used in future to test fertilizer-related yield modifications, thereby becoming a three parameter term. The write_out logic will need to be fixed for theified pp_curr and p_yield also

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
rc <- run_code(input_key = "ZA")  
il <- fetch_inputs(input_key = "ZA")  # fetch all necessary inputs
inlist_dt <- raster_list_to_dt(inlist = il[c("p_yield", "pp_curr")])

# no yield modification
ybetas <- list(1, 1)
ybeta <- yield_mod(inlist = inlist_dt[[2]], ybetas = ybetas, 
                   code = rc, cropnames = il$cropnames)

# yield modification with vectors
ybetas <- list(rep(0.75, nlayers(il[[3]])), rep(0.75, nlayers(il[[3]])))
ybeta2 <- yield_mod(inlist = inlist_dt[[2]], ybetas = ybetas, code = rc,
                    cropnames = il$cropnames)
ybeta2r <- dt_list_to_raster(inlist_dt[[1]][, c("x", "y"), with = FALSE],
                             ybeta2, projection(il$currprod))

# yield modification with rasters
# ybetas as rasters
dfact <- c(0.9, 1.2)
ybetas_l <- lapply(1:2, function(x) {
 r <- il[[3]]  # recycling
 r[] <- rnorm(n = ncell(r), mean = dfact[x], sd = 0.05)
 mask(r, il[[3]])
})
ybetas_dt <- raster_list_to_dt(ybetas_l)

ybeta3 <- yield_mod(inlist = inlist_dt[[2]], ybetas = ybetas_dt[[2]],
                    code = rc, cropnames = il$cropnames)
ybeta3r <- dt_list_to_raster(inlist_dt[[1]][, c("x", "y"), with = FALSE],
                             ybeta3, projection(il$currprod))

# Raster modifier X vector
ybetas_dt[[2]][[2]] <- c(0.9, 0.9)
ybeta4 <- yield_mod(inlist = inlist_dt[[2]], ybetas = ybetas_dt[[2]],
                    code = rc, cropnames = il$cropnames)  # wrong length
ybetas_dt[[2]][[2]] <- c(0.9, 0.9) <- c(1)
ybeta4 <- yield_mod(inlist = inlist_dt[[2]], ybetas = ybetas_dt[[2]],
                    code = rc, cropnames = il$cropnames)  # wrong length
ybetas_dt[[2]][[2]] <- rep(1.25, nlayers(il$currprod)) 
ybeta5 <- yield_mod(inlist = inlist_dt[[2]], ybetas = ybetas_dt[[2]],
                    code = rc, cropnames = il$cropnames)
ybeta5r <- dt_list_to_raster(inlist_dt[[1]][, c("x", "y"), with = FALSE],
                             ybeta5, projection(il$currprod))
ybetas_l[[2]] <- rep(1.25, nlayers(il$currprod)) 

PrincetonUniversity/agroEcoTradeoff documentation built on May 8, 2019, 3:12 p.m.