# Tree Data Summaries

    library(FIESTA)
    options(scipen = 6)

### Example 1

Let’s first start by using the sample data from FIESTA. Here, in it’s most simple form, we get summed live basal area and net cubic-foot volume by plot.

Note: datSum_opts has several parameter options, such as TPA and rounding (See datSum_options() for other parameters).

What is TPA? TPA is Trees Per Acre (TPA_UNADJ). The TPA_UNADJ variable is an acre-level expansion factor for measured trees. For FIA’s annual inventory, fixed plot design: TPA equals the inverse of the plot area (TPA = 1 / (Number of plots * area of plot).

FIA’s fixed plot design includes 4 subplots that are 24ft (7.3m) radius (0.04154 acres) and 4 microplots that are 6.8ft (2.1m) radius (0.00333 acres). Trees 5.0in (61cm) and greater are measured on the subplots and trees less than 5.0in are measured on the microplots. TPA for trees measured on the subplots is 6.018 (1 / (4 * 0.04143) and TPA for trees measured on the microplots is 74.965 (1 / 4 * 0.00333)). So, if we set TPA = TRUE, each tree 5.0in and greater represents 6.018 trees and each tree less than 5.0in represents 74.965 trees.

In FIESTA, TPA is default to TRUE. This means anything in the tsumvarlst (except TPA_UNADJ) is multiplied by TPA_UNADJ. If TPA = FALSE, we simply get a sum of the count of trees measured on the FIA plots.

    sumdat1 <- datSumTree(tree = WYtree,
                          tsumvarlst = c("BA", "VOLCFNET"),
                          tfilter = "STATUSCD == 1")

    ## Warning: package 'RSQLite' was built under R version 4.3.3

    ## Returned list items
    names(sumdat1)

    ## [1] "treedat"      "sumvars"      "tsumuniqueid" "treeqry"      "tfilter"
    ## [6] "tround"

    ## The first six rows of the summarized data table.
    head(sumdat1$treedat)

    ## Key: <PLT_CN>
    ##            PLT_CN BA_TPA_live VOLCFNET_TPA_live
    ##            <char>       <num>             <num>
    ## 1: 40404729010690    14.27414          41.80061
    ## 2: 40404730010690   119.74279        1563.03478
    ## 3: 40404737010690     4.39131          25.08993
    ## 4: 40404738010690    17.71590         137.52138
    ## 5: 40404742010690    31.15636         448.67475
    ## 6: 40404743010690    70.82421         907.80021

    ## The summarized variable names
    sumdat1$sumvars

    ## [1] "BA_TPA_live"       "VOLCFNET_TPA_live"

    ## The query used to get data (use message to output in pretty format)
    message(sumdat1$treeqry)

    ## WITH tdat AS
    ## (SELECT 'TREE' src, t.PLT_CN, t.CONDID, t.SUBP, t.TREE, t.BA, t.VOLCFNET, t.TPA_UNADJ
    ##  FROM treex t
    ##  WHERE STATUSCD = 1)
    ## -------------------------------------------
    ## SELECT tdat.PLT_CN,
    ##   COALESCE(SUM(BA * TPA_UNADJ),0) AS BA_TPA_live,
    ##   COALESCE(SUM(VOLCFNET * TPA_UNADJ),0) AS VOLCFNET_TPA_live
    ## FROM tdat
    ## GROUP BY tdat.PLT_CN

### Example 2

So, let’s now get a little more familiar with this function by showing what else it can do.

This time we will do the following things: 1) Add custom names (`tsumvarnmlst`) 2) summarize by plot and species (`bydomainlst`) 3) Add a derived variable (`tderive`) 4) Exclude woodland species (`woodland`) 5) Include seedlings (`seedlings`) 6) include a per acre count (i.e., TPA_UNADJ)

Note: Derived variables are not multiplied by TPA_UNADJ when the default is set (`datSum_opts(TPA = TRUE)`). Therefore, you must include it in the derived statement if it is desired. Furthermore variables defined in `tderive` should not be included in `tsumvarlst`.

Notice that the definitions for the derived variables are written in SQL syntax. This is required so that the statement can be appropriately plugged into the query that is used to generate collect the data.

    sumdat2 <-
      datSumTree(tree = WYtree,
                 seed = WYseed,
                 tsumvarlst = c("BA", "VOLCFNET", "TPA_UNADJ"),
                 tsumvarnmlst = c("BA_LIVE", "VOLNET_LIVE", "COUNT"),
                 bydomainlst = "SPCD",
                 tderive = list(SDI = '(POWER(DIA / 10, 1.605)) * TPA_UNADJ'),
                 woodland = "N",
                 seedlings = "Y",
                 tfilter = "STATUSCD == 1")

    ## Returned list items
    names(sumdat2)

    ## [1] "treedat"      "sumvars"      "tsumuniqueid" "treeqry"      "tfilter"
    ## [6] "tdomainlst"   "pcdomainlst"  "tround"

    ## The first six rows of the summarized data table.
    head(sumdat2$treedat)

    ## Key: <PLT_CN>
    ##            PLT_CN  SPCD COUNT_TREE  BA_LIVE VOLNET_LIVE       SDI COUNT_SEED
    ##            <char> <num>      <num>    <num>       <num>     <num>      <num>
    ## 1: 40404729010690   113   18.05414  6.02915    41.80061  12.00007   74.96528
    ## 2: 40404730010690   108    6.01805  1.60830    21.73192   3.39498    0.00000
    ## 3: 40404730010690   113   36.10828 12.56114   124.99617  24.57426    0.00000
    ## 4: 40404730010690   122  135.14574 41.04085   612.11646  73.29117    0.00000
    ## 5: 40404730010690   202  138.41506 64.53249   804.19022 118.74775  224.89585
    ## 6: 40404737010690   113    6.01805  1.94604    10.21924   3.95615    0.00000
    ##        COUNT
    ##        <num>
    ## 1:  93.01942
    ## 2:   6.01805
    ## 3:  36.10828
    ## 4: 135.14574
    ## 5: 363.31090
    ## 6:   6.01805

    ## The summarized variable names
    sumdat2$sumvars

    ## [1] "COUNT_TREE"  "COUNT_SEED"  "COUNT"       "BA_LIVE"     "VOLNET_LIVE"
    ## [6] "SDI"

    ## The query used to get data (use message to output in pretty format)
    message(sumdat2$treeqry)

    ## WITH tdat AS
    ## (SELECT 'TREE' src, t.PLT_CN, t.CONDID, t.SUBP, t.TREE, t.SPCD, t.TPA_UNADJ, t.BA, t.VOLCFNET, t.DIA
    ##  FROM treex t
    ##  JOIN ref_species ref ON (ref.SPCD = t.SPCD)
    ##  WHERE STATUSCD = 1 AND WOODLAND = 'N'
    ##  UNION
    ##  SELECT 'SEED' src, s.PLT_CN, s.CONDID, s.SUBP, 0, s.SPCD, s.TPA_UNADJ, 'null', 'null', 'null'
    ##  FROM seedx s
    ##  JOIN ref_species ref ON (ref.SPCD = s.SPCD)
    ##  WHERE WOODLAND = 'N')
    ## -------------------------------------------
    ## SELECT tdat.PLT_CN, SPCD,
    ##   COALESCE(SUM(CASE WHEN src = 'TREE' THEN TPA_UNADJ ELSE 0 END),0) AS COUNT_TREE,
    ##   COALESCE(SUM(CASE WHEN src = 'TREE' THEN BA * TPA_UNADJ ELSE 0 END),0) AS BA_LIVE,
    ##   COALESCE(SUM(CASE WHEN src = 'TREE' THEN VOLCFNET * TPA_UNADJ ELSE 0 END),0) AS VOLNET_LIVE,
    ##   COALESCE(SUM(CASE WHEN src = 'TREE' THEN (POWER(DIA / 10, 1.605)) * TPA_UNADJ ELSE 0 END),0) AS SDI,
    ##   COALESCE(SUM(CASE WHEN src = 'SEED' THEN TPA_UNADJ ELSE 0 END),0) AS COUNT_SEED,
    ##   COALESCE(SUM(TPA_UNADJ),0) AS COUNT
    ## FROM tdat
    ## GROUP BY tdat.PLT_CN, SPCD

### Example 3:

Now, let’s go further and include classified domains to summarize by. 1) Classify species into 3 classes (C-Conifer;W-Woodland;H-Hardwood) 2) Specify diameter breaks 3) Add species look up table and diameter breaks to domclassify, while also adding variables classified to bydomainlst.

    ## First, find unique species in WYtree
    spcdlst <- sort(unique(WYtree$SPCD))
    ## specify new class values for each unique species in WYtree
    spcdlut <- data.frame(SPCD = spcdlst,
                          SPCDCL = c("C","W","W","C","C","C","W","C","C","C","C","H","H","W","H","H","H","H","H"))

    ## Next, find unique diameters in WYtree
    dialst <- sort(unique(WYtree$DIA))
    ## specify break values to define new diameter class
    diabrks <- c(0,20,40,80)

    sumdat3 <-
      datSumTree(tree = WYtree,
                 seed = WYseed,
                 tsumvarlst = c("BA", "VOLCFNET", "TPA_UNADJ"),
                 tsumvarnmlst = c("BA_LIVE", "VOLNET_LIVE", "COUNT"),
                 bydomainlst = c("SPCD", "DIA"),
                 tderive = list(SDI = '(POWER(DIA / 10, 1.605)) * TPA_UNADJ'),
                 domclassify = list(SPCD = spcdlut, DIA = diabrks),
                 woodland = "N",
                 seedlings = "Y",
                 tfilter = "STATUSCD == 1")

    ## Returned list items
    names(sumdat3)

    ## [1] "treedat"       "sumvars"       "tsumuniqueid"  "treeqry"
    ## [5] "tfilter"       "tdomainlst"    "pcdomainlst"   "classifynmlst"
    ## [9] "tround"

    ## The first six rows of the summarized data table.
    head(sumdat3$treedat)

    ## Key: <PLT_CN>
    ##            PLT_CN SPCDCL  DIACL COUNT_TREE BA_LIVE VOLNET_LIVE      SDI
    ##            <char> <char> <char>      <num>   <num>       <num>    <num>
    ## 1: 40404729010690      C   0-20    6.01805 0.88752     3.79245  2.10689
    ## 2: 40404729010690      C   0-20    6.01805 2.54177    14.99432  4.90174
    ## 3: 40404729010690      C   0-20    6.01805 2.59986    23.01384  4.99145
    ## 4: 40404729010690      C    80+    0.00000 0.00000     0.00000  0.00000
    ## 5: 40404730010690      C   0-20   74.96528 3.67975     0.00000 10.85525
    ## 6: 40404730010690      C   0-20    6.01805 0.85371     6.26437  2.04223
    ##    COUNT_SEED    COUNT
    ##         <num>    <num>
    ## 1:    0.00000  6.01805
    ## 2:    0.00000  6.01805
    ## 3:    0.00000  6.01805
    ## 4:   74.96528 74.96528
    ## 5:    0.00000 74.96528
    ## 6:    0.00000  6.01805

    ## The summarized variable names
    sumdat3$sumvars

    ## [1] "COUNT_TREE"  "COUNT_SEED"  "COUNT"       "BA_LIVE"     "VOLNET_LIVE"
    ## [6] "SDI"

    ## The query used to get data (use message to output in pretty format)
    message(sumdat3$treeqry)

    ## WITH tdat AS
    ## (SELECT 'TREE' src, t.PLT_CN, t.CONDID, t.SUBP, t.TREE, t.SPCD, t.TPA_UNADJ, t.DIA, t.BA, t.VOLCFNET
    ##  FROM treex t
    ##  JOIN ref_species ref ON (ref.SPCD = t.SPCD)
    ##  WHERE STATUSCD = 1 AND WOODLAND = 'N'
    ##  UNION
    ##  SELECT 'SEED' src, s.PLT_CN, s.CONDID, s.SUBP, 0, s.SPCD, s.TPA_UNADJ, 'null', 'null', 'null'
    ##  FROM seedx s
    ##  JOIN ref_species ref ON (ref.SPCD = s.SPCD)
    ##  WHERE WOODLAND = 'N')
    ## -------------------------------------------
    ## SELECT tdat.PLT_CN,
    ##   (CASE
    ##    WHEN tdat.SPCD = 19 THEN 'C'
    ##    WHEN tdat.SPCD = 65 THEN 'W'
    ##    WHEN tdat.SPCD = 66 THEN 'W'
    ##    WHEN tdat.SPCD = 93 THEN 'C'
    ##    WHEN tdat.SPCD = 96 THEN 'C'
    ##    WHEN tdat.SPCD = 101 THEN 'C'
    ##    WHEN tdat.SPCD = 106 THEN 'W'
    ##    WHEN tdat.SPCD = 108 THEN 'C'
    ##    WHEN tdat.SPCD = 113 THEN 'C'
    ##    WHEN tdat.SPCD = 122 THEN 'C'
    ##    WHEN tdat.SPCD = 202 THEN 'C'
    ##    WHEN tdat.SPCD = 313 THEN 'H'
    ##    WHEN tdat.SPCD = 375 THEN 'H'
    ##    WHEN tdat.SPCD = 475 THEN 'W'
    ##    WHEN tdat.SPCD = 544 THEN 'H'
    ##    WHEN tdat.SPCD = 745 THEN 'H'
    ##    WHEN tdat.SPCD = 746 THEN 'H'
    ##    WHEN tdat.SPCD = 749 THEN 'H'
    ##    WHEN tdat.SPCD = 823 THEN 'H' END) AS 'SPCDCL',
    ##   (CASE
    ##    WHEN tdat.DIA IS NULL THEN '<1'
    ##    WHEN tdat.DIA >= 0 AND tdat.DIA < 20 THEN '0-20'
    ##    WHEN tdat.DIA >= 20 AND tdat.DIA < 40 THEN '20-40'
    ##    WHEN tdat.DIA >= 40 AND tdat.DIA < 80 THEN '40-80'
    ##    WHEN tdat.DIA >= 80 THEN '80+' END) AS 'DIACL',
    ##   COALESCE(SUM(CASE WHEN src = 'TREE' THEN TPA_UNADJ ELSE 0 END),0) AS COUNT_TREE,
    ##   COALESCE(SUM(CASE WHEN src = 'TREE' THEN BA * TPA_UNADJ ELSE 0 END),0) AS BA_LIVE,
    ##   COALESCE(SUM(CASE WHEN src = 'TREE' THEN VOLCFNET * TPA_UNADJ ELSE 0 END),0) AS VOLNET_LIVE,
    ##   COALESCE(SUM(CASE WHEN src = 'TREE' THEN (POWER(DIA / 10, 1.605)) * TPA_UNADJ ELSE 0 END),0) AS SDI,
    ##   COALESCE(SUM(CASE WHEN src = 'SEED' THEN TPA_UNADJ ELSE 0 END),0) AS COUNT_SEED,
    ##   COALESCE(SUM(TPA_UNADJ),0) AS COUNT
    ## FROM tdat
    ## GROUP BY tdat.PLT_CN, DIA

### Example 4:

Lastly, let’s play around with some additional derived variables:

    sumdat4 <-
      datSumTree(tree = WYtree,
                 tderive = list(LIVE_BA = "SUM(power(DIA, 2) * 0.005454 * TPA_UNADJ * (CASE WHEN STATUSCD = 1 THEN 1 ELSE 0 END))",
                                DEAD_BA = "SUM(power(DIA, 2) * 0.005454 * TPA_UNADJ * (CASE WHEN STATUSCD = 2 THEN 1 ELSE 0 END))",
                                SDI = "SUM((POWER(DIA / 10, 1.605)) * TPA_UNADJ)",
                                QMD = "sqrt(SUM(power(DIA, 2) * 0.005454 * TPA_UNADJ) / (SUM(TPA_UNADJ) * 0.005454))",
                                MEAN_DIA = "AVG(DIA)",
                                MEDIAN_DIA = "MEDIAN(DIA)",
                                LIVELESS20 = "SUM(TPA_UNADJ * (CASE WHEN DIA < 10 THEN 1 ELSE 0 END))",
                                LIVE10to30 = "SUM(TPA_UNADJ * (CASE WHEN DIA > 10 AND DIA <= 30 THEN 1 ELSE 0 END))"))

    ## Returned list items
    names(sumdat4)

    ## [1] "treedat"      "sumvars"      "tsumuniqueid" "treeqry"      "tround"

    ## The first six rows of the summarized data table.
    head(sumdat4$treedat)

    ## Key: <PLT_CN>
    ##            PLT_CN   LIVE_BA  DEAD_BA       SDI      QMD MEAN_DIA MEDIAN_DIA
    ##            <char>     <num>    <num>     <num>    <num>    <num>      <num>
    ## 1: 40404729010690  14.27414  2.83356  32.50917  8.62902  8.31429       8.80
    ## 2: 40404730010690 119.74279  7.48450 234.13906  8.36041  8.65455       7.60
    ## 3: 40404737010690   4.39131  4.49306  17.14599  8.22618  7.90000       7.00
    ## 4: 40404738010690  17.71590 19.05506  67.95588  8.94547  8.45714       7.65
    ## 5: 40404742010690  31.15636  0.00000  53.66041 11.64499 11.60000      11.40
    ## 6: 40404743010690  70.82421  5.10750 146.05962  7.09833  8.69565       7.50
    ##    LIVELESS20 LIVE10to30
    ##         <num>      <num>
    ## 1:   30.09023   12.03609
    ## 2:  249.48862   78.23460
    ## 3:   18.05414    6.01805
    ## 4:   60.18046   18.05414
    ## 5:    6.01805   36.10828
    ## 6:  222.14712   54.16241

    ## The summarized variable names
    sumdat4$sumvars

    ## [1] "LIVE_BA"    "DEAD_BA"    "SDI"        "QMD"        "MEAN_DIA"
    ## [6] "MEDIAN_DIA" "LIVELESS20" "LIVE10to30"

    ## The query used to get data (use message to output in pretty format)
    message(sumdat4$treeqry)

    ## WITH tdat AS
    ## (SELECT 'TREE' src, t.PLT_CN, t.CONDID, t.SUBP, t.TREE, t.STATUSCD, t.DIA, t.TPA_UNADJ
    ##  FROM treex t)
    ## -------------------------------------------
    ## SELECT tdat.PLT_CN,
    ##   SUM(power(DIA, 2) * 0.005454 * TPA_UNADJ * (CASE WHEN STATUSCD = 1 THEN 1 ELSE 0 END)) AS LIVE_BA,
    ##   SUM(power(DIA, 2) * 0.005454 * TPA_UNADJ * (CASE WHEN STATUSCD = 2 THEN 1 ELSE 0 END)) AS DEAD_BA,
    ##   SUM((POWER(DIA / 10, 1.605)) * TPA_UNADJ) AS SDI,
    ##   sqrt(SUM(power(DIA, 2) * 0.005454 * TPA_UNADJ) / (SUM(TPA_UNADJ) * 0.005454)) AS QMD,
    ##   AVG(DIA) AS MEAN_DIA,
    ##   MEDIAN(DIA) AS MEDIAN_DIA,
    ##   SUM(TPA_UNADJ * (CASE WHEN DIA < 10 THEN 1 ELSE 0 END)) AS LIVELESS20,
    ##   SUM(TPA_UNADJ * (CASE WHEN DIA > 10 AND DIA <= 30 THEN 1 ELSE 0 END)) AS LIVE10to30
    ## FROM tdat
    ## GROUP BY tdat.PLT_CN