---
title: "NRMSampling: Comprehensive Framework for Sampling Design and Estimation in Natural Resource Management"
author: "Sadikul Islam"
date: "`r Sys.Date()`"
output: rmarkdown::html_vignette
vignette: >
  %\VignetteIndexEntry{NRMSampling: Comprehensive Framework for Sampling Design and Estimation in Natural Resource Management}
  %\VignetteEngine{knitr::rmarkdown}
  %\VignetteEncoding{UTF-8}
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>",
  fig.width = 6.5,
  fig.height = 4,
  fig.align = "center",
  warning = FALSE,
  message = FALSE
)
library(NRMSampling)
set.seed(123)
```

# 1. Introduction

Natural Resource Management (NRM) research requires **robust sampling frameworks** to ensure reliable estimation of environmental variables such as biomass, soil loss, and carbon stocks. Challenges include spatial heterogeneity, accessibility constraints, and integration with geospatial data.

The **NRMSampling** package provides a unified and extensible framework for:

* Probability sampling (SRS, stratified, cluster, PPS, systematic)
* Non-probability sampling (purposive, quota, convenience)
* Design-based estimation (mean, total, ratio, regression, Horvitz–Thompson)
* NRM-specific estimators (biomass, carbon, soil loss)
* Spatial sampling using `sf` and `terra`

---

# 2. Data Generation

```{r}
sample_nrm <- data.frame(
  plot_id   = 1:100,
  biomass   = round(runif(100, 15, 65), 1),
  soil_loss = round(runif(100, 0, 12), 2),
  rainfall  = round(runif(100, 800, 1500)),
  slope     = round(runif(100, 1, 35), 1),
  strata    = sample(c("forest", "grassland", "agriculture"), 100, replace = TRUE),
  cluster   = sample(1:10, 100, replace = TRUE),
  size      = round(runif(100, 0.5, 5.0), 2)
)
head(sample_nrm)
```

---

# 3. Probability Sampling Designs

## 3.1 Simple Random Sampling

```{r}
srs <- srs_sample(sample_nrm, n = 25)
head(srs)
```

## 3.2 Stratified Sampling

```{r}
st <- stratified_sample(sample_nrm, strata_var = "strata", n_per_stratum = 8)
table(st$strata)
```

## 3.3 Cluster Sampling

```{r}
cl <- cluster_sample(sample_nrm, cluster_var = "cluster", n_clusters = 4)
length(unique(cl$cluster))
```

## 3.4 Probability Proportional to Size (PPS)

```{r}
pps <- pps_sample(sample_nrm, size_var = "size", n = 20)
summary(pps$.inclusion_prob)
```

---

# 4. Non-Probability Sampling

```{r}
conv <- convenience_sample(sample_nrm, n = 10)

purp <- purposive_sample(sample_nrm, "biomass > 45 & strata == 'forest'")

quot <- quota_sample(sample_nrm, strata_var = "strata", quota = 6)
table(quot$strata)
```

---

# 5. Estimation Methods

## 5.1 Mean and Total Estimation

```{r}
N <- nrow(sample_nrm)
srs_est <- srs_sample(sample_nrm, n = 30)

estimate_mean(srs_est$biomass)
estimate_total(srs_est$biomass, N = N)
estimate_se(srs_est$biomass, N = N)
```

## 5.2 Ratio and Regression Estimators

```{r}
X_total <- sum(sample_nrm$size)
X_mean  <- mean(sample_nrm$size)

ratio_estimator(srs_est$biomass, srs_est$size, X_total)
regression_estimator(srs_est$biomass, srs_est$size, X_mean)
```

## 5.3 Horvitz–Thompson Estimator

```{r}
ht_estimator(pps$biomass, pps$.inclusion_prob)
ht_variance(pps$biomass, pps$.inclusion_prob)
```

## 5.4 Stratified Estimator

```{r}
N_h <- table(sample_nrm$strata)
stratified_estimator(st$biomass, st$strata, N_h)
```

---

# 6. NRM-Specific Applications

## 6.1 Biomass and Carbon Estimation

```{r}
bio <- biomass_estimate(srs_est, biomass_var = "biomass", area = 1500)
bio$total_biomass

carbon_stock_estimate(srs_est, biomass_var = "biomass", area = 1500)
```

## 6.2 Soil Loss Estimation

```{r}
sl <- soil_loss_estimate(srs_est, loss_var = "soil_loss", area = 1500)
sl$total_loss
```

---

# 7. Sampling Efficiency

```{r}
srs1 <- srs_sample(sample_nrm, n = 20)
srs2 <- srs_sample(sample_nrm, n = 40)

sampling_efficiency(srs1$biomass, srs2$biomass, N = 100)
```

---

# 8. Spatial Sampling (Optional)

```{r, eval=FALSE}
library(sf)

sample_spatial <- data.frame(
  lon = runif(50, 77.8, 78.2),
  lat = runif(50, 30.1, 30.4)
)

pts_sf <- to_sf_points(sample_spatial, lon = "lon", lat = "lat")
head(pts_sf)
```

---

# 9. Recommended Workflow

1. Define sampling objective
2. Choose appropriate sampling design
3. Collect and validate field data
4. Apply design-based estimators
5. Integrate spatial data if required
6. Report uncertainty (SE, CI)

---

# 10. Conclusion

The **NRMSampling** package provides a comprehensive and scalable framework for sampling and estimation in NRM research. It enables researchers to adopt statistically sound methodologies while ensuring reproducibility and integration with spatial data systems.

---

# References

Cochran, W.G. (1977). *Sampling Techniques*.
Lohr, S.L. (2022). *Sampling: Design and Analysis*.
Horvitz, D.G. and Thompson, D.J. (1952).
IPCC (2006). *Guidelines for Greenhouse Gas Inventories*.
