Repository navigation
Expand file tree
/
Copy pathREADME.Rmd
More file actions
266 lines (203 loc) · 8.48 KB
/
Copy pathREADME.Rmd
File metadata and controls
266 lines (203 loc) · 8.48 KB
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
---
output: github_document
---
# fiaplyr <picture><img src="inst/logo.png" alt="fiaplyr logo" height="210" align="right"></picture>
<!-- badges: start -->
[](https://github.com/brycefrank/fiaplyr/actions/workflows/R-CMD-check.yml) [](https://github.com/brycefrank/fiaplyr) [](https://brycefrank.com/fiaplyr)
<!-- badges: end -->
```{r setup, include=FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>",
message = FALSE, # Suppresses package startup messages
warning = FALSE # Suppresses warnings
)
```
`fiaplyr` provides a modern, `dplyr`-inspired interface for working with Forest
Inventory and Analysis (FIA) databases. Users interact with a `handler`, which
facilitates complex database operations while maintaining a concise syntax.
Estimation and other statistical objectives can be formed in a few lines of
code.
<br clear="all">
<img src="inst/readme_1.png" width="500" alt="fiaplyr workflow overview">
The verbs `subset`, `partition`, `transform`, `estimate`, and `aggregate` are
the main tools, allowing users to retain only the data they need, form domains,
create new variables, and produce estimates and plot-level values, respectively.
The interaction of these verbs creates a flexible self-documenting framework for
working with FIA data.
## Installation
You can install the development version of fiaplyr from GitHub:
``` r
# install.packages("devtools")
devtools::install_github("brycefrank/fiaplyr")
```
## Usage
### Connecting to an FIA Database
A connection to a database is required, and the database must be structured
like FIADB or similar. `duckdb` is used below, but other backends supported by
`DBI` should work, including `SQLite` and Oracle. We include a miniature version
of the FIA database for Vermont, which can be used for testing and learning.
```{r}
library(fiaplyr) # Load fiaplyr package from local source
library(dplyr)
library(DBI)
library(duckdb)
con <- dbConnect(duckdb(), fiadb_vt_mini_path())
```
### Specifying a Handler
A `handler` is the main way users create estimates and other inventory products
from `fiaplyr`. Currently, users must specify an `eval_handler`, which interacts
with an FIA-made data structure called an evaluation. Evaluations define an area
of interest, a time period, and an analysis context. Though evaluation-free
analysis is planned for implementation, we highly recommend them because they
take advantage of quality assurance processes used by the FIA. Users can explore
the available evaluations in the database by
```{r}
explore_evals(con) |>
head()
```
Because we are using the mini Vermont database, we see just two records, but in
a full FIA database there are many evaluations. For this tutorial, we will use
`500601`, which covers Vermont (`50`) for 2003-2006 (`06`), and can be used to
estimate status variables (`01`).
```{r}
# Initialize handler for EVALID 500601
handler <- eval_handler(con, 500601)
# Inspect the handler summary
handler
```
Here, we see the evaluation number, a description, and a summary of the sample
size and time range of data collection.
### Aggregation to the Plot Level
An essential step for nearly all FIA analyses is to aggregate inventory
components, such as trees or conditions, to the plot level. This can be done
explicitly with `aggregate`. Specify a slot (e.g., `tree` or `cond`) and one or
more variables to aggregate. Here, we produce plot-level values for `VOLCFNET`
and `VOLCFGRS`
```{r}
# Calculate Net Cubic Foot Volume per acre for each plot
plot_vol <- handler |>
aggregate(tree(net_vol = VOLCFNET, VOLCFGRS))
head(plot_vol)
```
Plot-level values are often used in statistical models and other applications.
However, some analyses do not explicitly need an `aggregate` step, such as the
estimation of state-wide means or totals, so it is not always necessary to call
`aggregate`. Note that columns can be dynamically named, otherwise the stated
value is used.
### Transforms
Often, custom variables are needed for analysis. These can be generated by
`transform`. A canonical example is the need for basal area, a variable that is
not explicitly stored in the FIA database, but is a simple function of diameter.
Specify the slot the new variable name, and its value.
```{r}
# Calculate Basal Area per acre
ba_handler <- handler |>
transform(tree(BA = 0.005454 * DIA^2))
plot_ba <- ba_handler |>
aggregate(tree(BA))
# Verify the output
head(plot_ba)
```
### Partitions
Many FIA estimates take advantage of the concept of domains, which are subsets
of trees, conditions, or other inventory components. Partitioning is the
process of assigning domains based on the values of one or more variables. As an
example, we can make species domains by using `partition(tree(SPCD))`.
```{r}
plot_ba_by_sp <- ba_handler |>
partition(tree(SPCD)) |>
aggregate(tree(BA)) |>
arrange(desc(BA))
head(plot_ba_by_sp)
```
### Subsets
Subsets are related to partitions, but are instead used to exlclude entire
portions of the data. This is useful when analysis only involves a
subpopulation, such as a specific species, and the remaining data can be
entirely ignored. For example, if we only want to estimate basal area for balsam
fir, we can subset the handler to only include trees with `SPCD == 12`.
```{r}
plot_ba_balsam <- ba_handler |>
subset(tree(SPCD == 12)) |>
aggregate(tree(BA)) |>
arrange(desc(BA))
head(plot_ba_balsam)
```
As a contrast, we can use partitions to achieve a similar purpose
```{r}
plot_ba_balsam <- ba_handler |>
partition(tree(SPCD)) |>
aggregate(tree(BA)) |>
arrange(desc(BA)) |>
filter(SPCD == 12) # use a standard dplyr filter to subset the aggregates
head(plot_ba_balsam)
```
Hence, the use of subsets or partitions is a matter of user preference, clarity,
and other analytical considerations.
## Estimation
Currently, `fiaplyr` supports post-stratified estimation used in tandem with
evaluations, much in the same way as `EVALIDator`. However, the flexibility of
domain specification and mutations makes a very expressive way to generate
custom estimates. Extensions to other types of estimators is possible, but not
currently implemented.
To produce post-stratified estimates use the `estimate` method on the handler.
The `estimate` method uses the same syntax as `aggregate`, but instead of
producing plot-level values, it produces estimates of the specified variable
for the area specified by the evaluation.
The default estimator is post-stratification with Taylor variance estimation.
It can be selected explicitly when a workflow needs to make the point- and
variance-estimator choices visible:
```{r eval=FALSE}
ba_est <- ba_handler |>
estimate(
tree(ba = BA),
estimator = pe_post_strat(var_est = ve_taylor())
)
```
Recall our desire to estimate basal area, this is now straightforward.
```{r}
# Create an estimator from the handler
ba_est <- ba_handler |>
estimate(tree(ba = BA))
ba_est
```
By default, outputs are means, typically representing areal densities. An
estimate of the total can be made instead
```{r}
ba_total_est <- ba_handler |>
estimate(tree(BA), output = "total")
ba_total_est
```
### Estimates of Partitions
Estimates respect partitions applied to the handler. To form basal area
estimates by species class simply do the following
```{r}
# Estimate Basal Area by Species
ba_by_sp_handler <- ba_handler |>
partition(tree(SPCD))
ba_by_sp_est <- ba_by_sp_handler |>
estimate(tree(BA)) |>
arrange(desc(estimate))
head(ba_by_sp_est)
```
For any given domain estimate, they are interpreted as the mean value for that
domain across the state. For tree attributes, these are per-acre densities.
Hence, the first row is interpreted as: the mean basal area per acre for
`SPCD = 318` is 18.6 ft²/acre, with a standard error of 0.96 ft²/acre across
the state of Vermont. This estimate is not normalized by forested area,
which requires the more sophisticated ratio estimator
```{r}
ba_by_sp_ratio_est <- ba_by_sp_handler |>
subset(cond(COND_STATUS_CD == 1)) |> # subset to only forested areas
estimate(
ratio(tree(BA), cond())
)
ba_by_sp_ratio_est |>
arrange(desc(estimate)) |>
head()
```
Here, estimates are divided by the forested proportion of the state, and the
first row is interpreted as: the mean basal area per acre on forested land for
`SPCD = 318` is 24.1 ft²/acre, with a standard error of 1.01 ft²/acre across the
state of Vermont.