BCWF-Wetlands / wespr

Conversion of WESP excel Calculator to R functions.
Apache License 2.0
3 stars 0 forks source link

Return score/subscores as an S3 class #84

Closed ateucher closed 9 months ago

ateucher commented 9 months ago

Closes #72

@gcperk I think this is ready for review. What I did was make a new class called indicator_score, which could have a with_subscores subclass. Each ecosystem function now wraps it output in as.indicator_score(), passing the score, and the subscores if it has them.

So the subscores are now also stored in the site object after calc_indicators() has been run. I don't include them in the table created by get_indicator_scores(), but you can see them if you print the indicators:

library(wespr)

wesp_file <- system.file("input_data/wetFlat.csv", package = "wespr")
wesp_data <- load_wesp_data(wesp_file)

site <- as.wesp_site(wesp_data)
#> Questions F25, F41, F43, F44, F59, OF28 do not appear to have been filled out.
#>  Please ensure this is valid.

site <- calc_indicators(site)

# Print the site, doesn't show the subscores
site
#> A wesp_site object
#> 
#> Site:  site_1 
#> 
#> Incomplete Questions:  F25, F41, F43, F44, F59, OF28 
#>   * Please ensure that it is valid to leave these questions unanswered.
#> 
#> Derived values:
#>   *  AllWater = 0
#>   *  NeverWater = 0
#>   *  NoSeasonal = 0
#>   *  NoPersis = 0
#>   *  TempWet = 0
#>   *  AllPermW = 0
#>   *  HiFlucW = 0
#>   *  TooShallow = 0
#>   *  NoPond = 0
#>   *  NoDeepPonded = 0
#>   *  NoOW = 0
#>   *  NoOutletX = 0
#>   *  NoOutlet = 1
#>   *  Inflow = 0
#>   *  Disturb = 1
#>   *  FishFound = 0
#>   *  Moose = 0
#>   *  Beaver = 1
#>   *  Muskrat = 0
#>   *  Bear = 0
#>   *  Caribou = 0
#>   *  NoCA = 0
#>   *  Fishless = 0
#>   *  GDeco = 0
#>   *  CMeco = 1
#>   *  SIMeco = 0
#>   *  BPeco = 0
#>   *  TPeco = 0
#>   *  OutMap = 0
#>   *  S1_sum = 2
#>   *  S1_subscore = 0.17
#>   *  S2_sum = 0
#>   *  S2_subscore = 0
#>   *  S3_sum = 0
#>   *  S3_subscore = 0
#>   *  S4_sum = 5
#>   *  S4_subscore = 0.42
#>   *  S5_sum = 0
#>   *  S5_subscore = 0
#>   *  S6_sum = 0
#>   *  S6_subscore = 0
#> 
#> Indicators:
#>   * WS:  
#>     - fun: 10 
#>     - ben: 5.89 
#>   * SR:  
#>     - fun: 4.12 
#>     - ben: 4.14 
#>   * PR:  
#>     - fun: 5.51 
#>     - ben: 3.75 
#>   * CP:  
#>     - fun: 8.9 
#>   * FR:  
#>     - fun: 2.23 
#>     - ben: 8.06 
#>   * SENS:  
#>     - fun: 7.3 
#>   * STR:  
#>     - fun: 7.96 
#>   * NR:  
#>     - fun: 6.01 
#>     - ben: 5 
#>   * APP:  
#>     - fun: 2.61 
#>     - ben: 4.99 
#>   * PD:  
#>     - fun: 6.12 
#>     - ben: NA 
#>   * KMH:  
#>     - fun: 5.78 
#>     - ben: 7.8 
#>   * WB:  
#>     - fun: 6.43 
#>     - ben: 7.19 
#>   * POL:  
#>     - fun: 5.07 
#>     - ben: 3.5 
#>   * RSB:  
#>     - fun: NA 
#>     - ben: 4.03 
#>   * OE:  
#>     - fun: 0 
#>   * AM:  
#>     - fun: 5.62 
#>     - ben: 10 
#>   * FH:  
#>     - fun: 2.92 
#>     - ben: 2.14 
#>   * SFTS:  
#>     - fun: 0 
#>     - ben: 7.83 
#>   * CRI:  
#>     - ben: 5.4 
#> 
#> * Retrieve indicator scores with `get_indicator_scores()`

# get_indicator_scores() doesn't show the subscores
get_indicator_scores(site)
#> # A tibble: 19 × 4
#>    site   indicator   fun   ben
#>    <chr>  <chr>     <dbl> <dbl>
#>  1 site_1 WS        10     5.89
#>  2 site_1 SR         4.12  4.14
#>  3 site_1 PR         5.51  3.75
#>  4 site_1 CP         8.90 NA   
#>  5 site_1 FR         2.23  8.06
#>  6 site_1 SENS       7.30 NA   
#>  7 site_1 STR        7.96 NA   
#>  8 site_1 NR         6.01  5   
#>  9 site_1 APP        2.61  4.99
#> 10 site_1 PD         6.12 NA   
#> 11 site_1 KMH        5.78  7.80
#> 12 site_1 WB         6.43  7.19
#> 13 site_1 POL        5.07  3.5 
#> 14 site_1 RSB       NA     4.03
#> 15 site_1 OE         0    NA   
#> 16 site_1 AM         5.62 10   
#> 17 site_1 FH         2.92  2.14
#> 18 site_1 SFTS       0     7.83
#> 19 site_1 CRI       NA     5.40

# But printing the `indicators` element of the `site` object does:
site$indicators
#> $ws
#> $ws$fun
#> Score: 10 
#>   Subscores: 
#>     - subsurf: 0.53
#>     - livestore: 0.2
#>     - friction: 0.75
#> 
#> $ws$ben
#> Score: 5.89
#> 
#> $sr
#> $sr$fun
#> Score: 4.11715 
#>   Subscores: 
#>     - livestore: 0.12
#>     - dryintercept: 0.47
#>     - wetintercept: 0.64
#>     - connectiv: 1
#> 
#> $sr$ben
#> Score: 4.14
#> 
#> $pr
#> $pr$fun
#> Score: 5.513188 
#>   Subscores: 
#>     - interceptdry: 0.31
#>     - interceptwet: 0.66
#>     - connec: 0.5
#>     - adsorb: 0.5
#>     - desorb: 0.22
#> 
#> $pr$ben
#> Score: 3.75
#> 
#> $cp
#> $cp$fun
#> Score: 8.9
#> 
#> $fr
#> $fr$fun
#> Score: 2.23
#> $fr$ben
#> Score: 8.06
#> 
#> $sens
#> $sens$fun
#> Score: 7.300439 
#>   Subscores: 
#>     - abiosens: 1
#>     - biosens: 0.75
#>     - colonizer: 0.62
#>     - growrate: 0.56
#> 
#> 
#> $str
#> $str$fun
#> Score: 7.959184 
#>   Subscores: 
#>     - hydrostress: 0.08
#>     - wqstress: 0.11
#>     - connecstress: 0.8
#> 
#> 
#> $nr
#> $nr$fun
#> Score: 6.009203 
#>   Subscores: 
#>     - warmth: 0.41
#>     - intercept: 0.64
#>     - connecc: 1
#>     - organic: 1
#>     - redox: 0.25
#> 
#> $nr$ben
#> Score: 5
#> 
#> $app
#> $app$fun
#> Score: 2.606893 
#>   Subscores: 
#>     - npinput: 0.12
#>     - npcycling: 0.62
#>     - templight: 0.67
#>     - stressors: 0.67
#> 
#> $app$ben
#> Score: 4.99
#> 
#> $pd
#> $pd$fun
#> Score: 6.118413 
#>   Subscores: 
#>     - spparea: 0.56
#>     - vrichness: 0.78
#>     - aqfertilpd: 0.36
#>     - vscape: 0.58
#>     - stresspd: 0.79
#> 
#> $pd$ben
#> Score: NA
#> 
#> $kmh
#> $kmh$fun
#> Score: 5.783674 
#>   Subscores: 
#>     - beaverhab: 1
#>     - muskrathab: 0.46
#>     - moosehab: 0.45
#>     - caribouhab: 0.33
#>     - bearhab: 0.65
#> 
#> $kmh$ben
#> Score: 7.8
#> 
#> $wb
#> $wb$fun
#> Score: 6.429227 
#>   Subscores: 
#>     - lscape: 0.56
#>     - hydro: 0.87
#>     - produc: 0.26
#>     - struc: 0.52
#> 
#> $wb$ben
#> Score: 7.19
#> 
#> $pol
#> $pol$fun
#> Score: 5.073095 
#>   Subscores: 
#>     - pollen: 0.61
#>     - nestsites: 0.33
#>     - stress: 0.54
#> 
#> $pol$ben
#> Score: 3.5
#> 
#> $rsb
#> $rsb$fun
#> Score: NA 
#>   Subscores: 
#>     - hydrosize: 0.59
#>     - struc: 0.81
#>     - foods: 2.37
#>     - habs: 0.25
#>     - lscape: 0.28
#>     - nopred: 0.33
#> 
#> $rsb$ben
#> Score: 4.03
#> 
#> $oe
#> $oe$fun
#> Score: 0 
#>   Subscores: 
#>     - histaccum: 0.89
#>     - productiv: 0.26
#>     - exportpot: 0
#> 
#> 
#> $am
#> $am$fun
#> Score: 5.620043 
#>   Subscores: 
#>     - waterscape: 0.11
#>     - hydro: 0.57
#>     - aqstruc: 0.93
#>     - terrstruc: 0.56
#>     - biostress: 0.24
#> 
#> $am$ben
#> Score: 10
#> 
#> $fh
#> $fh$fun
#> Score: 2.916639 
#>   Subscores: 
#>     - hydro: 0.4
#>     - struc: 0
#>     - nooxyrisk: 0
#>     - nostress: 0.56
#> 
#> $fh$ben
#> Score: 2.14
#> 
#> $sfts
#> $sfts$fun
#> Score: 0 
#>   Subscores: 
#>     - shadedsurf: 0.76
#>     - surfacestorage: 0.35
#>     - groundwater: 0.67
#> 
#> $sfts$ben
#> Score: 7.83
#> 
#> $cri
#> $cri$ben
#> Score: 5.4

I'm not sure how much we want to expose the subscores, or if they're only useful if we want to go digging for them. Any thoughts on how else to incorporate them in outputs?

ateucher commented 9 months ago

Thanks Gen!