---
title: "Data Quality Assessment"
output:
html_document:
theme: flatly
highlight: tango
toc: TRUE
toc_depth: 4 # Define the depth of the TOC (increase as needed)
toc_float:
collapsed: TRUE # Whether the TOC starts collapsed
smooth_scroll: TRUE # Smooth scrolling when a TOC entry is clicked
fontsize: 12pt # Set font size
linkcolor: blue # Hyperlink color (for PDF output)
vignette: >
%\VignetteIndexEntry{Data Quality Assessment}
%\VignetteEngine{knitr::rmarkdown}
%\VignetteEncoding{UTF-8}
---
```{r setup_general, include = FALSE}
knitr::opts_chunk$set(
echo = TRUE,
warning = FALSE,
message = FALSE,
collapse = TRUE,
comment = "#>"
)
```
```{r setup_lib, include=FALSE}
if (!requireNamespace("htmltools", quietly = TRUE)) {
stop("Package 'htmltools' is required to run this function. Please install it.")
}
if (!requireNamespace("magick", quietly = TRUE)) {
stop("Package 'magick' is required to run this function. Please install it.")
}
library(growthTrendR)
library(data.table)
library(ggplot2)
library(sf)
```
Ensuring the quality of data measurements is critical to the success of any data collection effort. In this work, particular attention is given to the possibility of measurement errors due to incorrect data transformations and radius misalignment, as these can distort tree-ring width values and consequently impact downstream analyses.
```{r data_qa, echo= FALSE, eval = FALSE, eval=FALSE}
```
## Measurement accuracy
Ring-width measurements from certain sites may occasionally appear unusually high or low, often due to transformation errors, as tree-ring data is usually stored as integers with an associated scale factor. The CFS_scale() function addresses this by applying a k-nearest neighbors (k-NN) approach, using geodesic distances on the WGS84 ellipsoid (via the distGeo function from the geosphere package), to identify geographically close sites. It then compares the median tree-ring measurements of the target site to those of its nearest neighbors. This procedure is conducted within the same species.
```{r scale}
# ring measurement
# loading data
dt.samples <- fread(system.file("extdata", "dt.samples.csv", package = "growthTrendR"))
# formatting the users' data conformed to CFS-TRenD data structure
dt.samples_trt <- CFS_format(data = list(dt.samples, 39:68), usage = 1, out.csv = NULL)
# loading processed data
# otherwise need to run CFS_format() first as done in data report
# dt.samples_trt <- readRDS(system.file("extdata", "dt.samples_trt.rds", package = "growthTrendR"))
# , message = FALSE, warning = FALSE, results = 'hide'
all.sites <- dt.samples_trt$tr_all_wide[,.N, by = c("species", "uid_site", "site_id")][, N:=NULL]
if (nrow(all.sites[, .N, by = .(species, site_id)][N>1]) > 0) stop("species-site_id is not unique...")
# e.g. taking the target sites
target_site <- all.sites[c(1,2), -"uid_site"]
ref.sites <- merge(dt.samples_trt$tr_all_wide[,c("species", "uid_site", "site_id", "latitude","longitude", "uid_radius")], dt.samples_trt$tr_all_long$tr_7_ring_widths, by = c("uid_radius"))
dt.scale <- CFS_scale( target_site = target_site, ref_sites = ref.sites, scale.label_data_ref = "CFS-TRenD V1.2-proj69", scale.max_dist_km = 200, scale.N_nbs = 2)
```
#### **arguments of CFS_scale() function:**
**target_site**
The target site refers to a single site for a specific species to be evaluated and includes at least five columns: species, uid_site, site_id, latitude, and longitude.
**ref_sites**
The reference sites refer to a dataset of ring-width measurements that includes the target site. In addition to the columns present in the target site, the dataset also contains uid_radius, year, and rw_mm.
**scale.label_data_ref**
a short description of reference dataset.This text will appear in the report as the data source for the generated figures.
**scale.N_nbs**
This specifies the maximum number of neighbors to be considered in the procedure.
**scale.max_dist_km**
This specifies the maximum distance (in kilometers) for searching neighbors of the target site.
### **generate report:**
```{r scale_report_demo, eval= FALSE}
# default is NULL
generate_report(robj = dt.scale, output_file = NULL)
# user may also specify the directory and filename, for example
outfile_scale <- "scale_report.html"
generate_report(robj = dt.scale, output_file = outfile_scale)
```
#### **arguments of the generate_report() function:**
**robj **
The input for the scale report is the output of the CFS_scale() function, which assigns the class "cfs_scale" to the resulting object.
**output_file **
The output_file argument allows users to generate an HTML report at a specified path and file name. If set to NULL (default), the report opens automatically in the browser.
```{r scale_report, include = FALSE, message = FALSE, warning = FALSE, results = 'hide'}
outfile_scale <- tempfile(fileext = ".html")
generate_report(robj = dt.scale, output_file = outfile_scale)
```
```{r scale_inclu, echo = FALSE, results = 'asis'}
cat('