---
title: "Economic Analysis of SWC Measures using swcEcon"
author: "swcEcon package"
date: "`r Sys.Date()`"
output:
  rmarkdown::html_vignette:
    toc: true
    toc_depth: 3
    number_sections: true
vignette: >
  %\VignetteIndexEntry{Economic Analysis of SWC Measures using swcEcon}
  %\VignetteEngine{knitr::rmarkdown}
  %\VignetteEncoding{UTF-8}
---

```{r setup, include = FALSE}
knitr::opts_chunk$set(
  collapse = TRUE,
  comment  = "#>",
  warning  = FALSE,
  message  = FALSE
)
library(swcEcon)
```

# Introduction

Sound investment decisions in watershed development require rigorous
economic appraisal.  The **swcEcon** package provides a complete
toolkit for evaluating soil and water conservation (SWC) measures
from project inception through to report generation.

This vignette demonstrates all major functions using the bundled
benchmark datasets.  Methods follow Gittinger (1982), the CIMMYT
(1988) on-farm economics manual, Wischmeier and Smith (1978), and
NABARD (2019) appraisal guidelines.

# Financial appraisal

## Benefit-cost ratio

```{r bcr}
r <- calc_bcr(
  investment    = 20,     # INR 20 lakh capital cost
  annual_benefit = 6,    # INR 6 lakh per year
  annual_omc    = 0.8,   # INR 0.8 lakh O&M per year
  life          = 20,    # 20-year design life
  discount_rate = 0.12   # 12% discount rate (GoI 2008)
)
print(r)
```

The BCR of `r round(r$bcr, 2)` exceeds 1.5, meeting the NABARD
threshold for robust watershed investment (NABARD 2019).

## Net present value

```{r npv}
n <- calc_npv(
  investment    = 20,
  annual_benefit = 6,
  annual_omc    = 0.8,
  life          = 20,
  discount_rate = 0.12
)
print(n)
head(n$cashflows)
```

## Internal rate of return

```{r irr}
i <- calc_irr(
  investment    = 20,
  annual_benefit = 6,
  annual_omc    = 0.8,
  life          = 20
)
print(i)
```

The IRR of `r round(i$irr_pct, 1)` per cent exceeds the Planning
Commission benchmark of 12 per cent (GoI 2008) and the NABARD
threshold of 15 per cent (NABARD 2019).

## Payback period

```{r pbp}
p <- calc_pbp(
  investment    = 20,
  annual_benefit = 6,
  annual_omc    = 0.8
)
print(p)
```

A simple payback period of `r round(p$simple_pbp, 1)` years falls
within the 3--5 year range that strongly predicts voluntary SWC
adoption among smallholder farmers in rainfed India (Joshi et al.
2005).

## Marginal rate of return (CIMMYT method)

```{r mrr}
m <- calc_mrr(
  nb_with      = 18000,   # net benefit per ha with contour bund
  nb_without   = 11000,   # net benefit per ha current practice
  cost_with    = 16000,   # variable cost per ha with SWC
  cost_without = 11500    # variable cost per ha current practice
)
print(m)
```

An MRR of `r round(m$mrr, 0)` per cent far exceeds the CIMMYT
(1988) minimum acceptable threshold of 100 per cent, recommending
adoption.

## Modified BCR

```{r mbcr}
calc_mbcr(total_benefit = 80, operating_cost = 12, capital_cost = 20)
```

# Soil loss economic valuation

## USLE-based soil loss cost

```{r soil}
data(usle_india_soils)
K_vert <- usle_india_soils[
  usle_india_soils$soil_series == "Vertisols", "k_mean"]

s <- calc_soil_loss_cost(
  R      = 720,      # R-factor from rainfall_erosivity_india (Pune)
  K      = K_vert,   # K-factor from usle_india_soils
  LS     = 4.2,      # slope length-gradient factor
  C_pre  = 0.35,     # cover factor before contour bund
  C_post = 0.18,     # cover factor after contour bund
  P_pre  = 1.0,      # no support practice before
  P_post = 0.5,      # support practice P after bunding
  area_ha = 500      # watershed area
)
print(s)
```

The contour bund reduces soil loss by `r round(s$pct_reduction, 0)`
per cent, saving `r format(s$annual_benefit_inr, big.mark=",")` INR
per year in nutrient replacement costs alone.

## Nutrient replacement cost

```{r nutrient}
data(usle_india_soils)
soil <- usle_india_soils[usle_india_soils$soil_series == "Vertisols", ]

calc_nutrient_cost(
  soil_loss_t_ha = s$soil_loss_pre,
  area_ha        = 500,
  n_kg_per_t     = soil$n_kg_per_t,
  p_kg_per_t     = soil$p_kg_per_t,
  k_kg_per_t     = soil$k_kg_per_t
)
```

# Water resource valuation

```{r water}
data(rainfall_erosivity_india)
rf_pune <- rainfall_erosivity_india[
  rainfall_erosivity_india$district == "Pune", "annual_rf_mm"]

w <- calc_water_value(
  area_ha        = 500,
  rainfall_mm    = rf_pune,
  rc_pre         = 0.35,   # runoff coefficient before SWC
  rc_post        = 0.20,   # runoff coefficient after SWC
  harvest_pct    = 45,     # percentage of reduced runoff harvested
  gw_recharge_pct = 20,   # percentage percolating to groundwater
  water_value_m3 = 3.5    # INR per cubic metre (Joshi et al. 2005)
)
print(w)
```

```{r irrigation}
calc_irrigation_benefit(
  irrig_area_ha       = 80,
  yield_increase_t_ha = 1.6,
  crop_price_inr_t    = 18000,
  input_cost_inr_ha   = 8000
)
```

# Social indicators

```{r employment}
calc_employment(
  employment_days = 45000,   # total person-days
  investment_lakh = 50,      # INR 50 lakh project cost
  wages_per_day   = 250      # daily wage rate (INR)
)
```

# Risk analysis

## Sensitivity analysis

```{r sensitivity}
sa <- sensitivity_analysis(
  investment    = 20,
  annual_benefit = 6,
  annual_omc    = 0.8,
  life          = 20,
  discount_rate = 0.12,
  cost_range_pct    = 20,
  benefit_range_pct = 20,
  rate_range_pct    = 3
)
print(sa)
```

## Switching value

```{r switching}
sv <- calc_switching_value(
  investment    = 20,
  annual_benefit = 6,
  annual_omc    = 0.8,
  life          = 20,
  discount_rate = 0.12
)
print(sv)
```

## Monte Carlo simulation

```{r mc, eval = FALSE}
mc <- monte_carlo_swc(
  inv_mean = 20, inv_cv  = 0.10,
  ben_mean = 6,  ben_cv  = 0.15,
  omc_mean = 0.8, omc_cv = 0.20,
  life_min = 15, life_max = 25,
  r_min    = 0.10, r_max = 0.14,
  n_sim    = 5000, seed  = 42
)
print(mc)
```

# Benchmark datasets

## State-wise BCR benchmarks

```{r benchmarks}
data(swc_benchmarks)
swc_benchmarks[, c("state", "agro_zone", "bcr_typical",
                    "irr_pct", "pbp_years")]
```

## USLE parameters for Indian soils

```{r soils_table}
data(usle_india_soils)
usle_india_soils[, c("soil_series", "soil_order",
                      "k_mean", "t_value", "n_kg_per_t")]
```

## Rainfall erosivity

```{r erosivity}
data(rainfall_erosivity_india)
rainfall_erosivity_india[, c("district", "state",
                              "annual_rf_mm", "r_factor")]
```

## SWC unit cost norms (PMKSY-WDC 2015)

```{r costs}
data(swc_cost_norms)
swc_cost_norms[, c("measure", "norm_2024_inr",
                    "design_life_yr", "labour_pct")]
```

# Full pipeline and report

```{r pipeline, eval = FALSE}
pl <- run_swc_pipeline(
  investment     = 20,
  annual_benefit = 6,
  annual_omc     = 0.8,
  life           = 20,
  discount_rate  = 0.12,
  project_name   = "Hypothetical Check Dam, Semi-arid India",
  include_sensitivity = TRUE,
  include_monte_carlo = FALSE
)
print(pl)

# AFTER
generate_swc_report(
  pl,
  output_file  = "swcEcon_appraisal.html",
  title        = "Economic Appraisal: Hypothetical Watershed",
  author       = "Your Name",
  organisation = "Your Organisation"
)

```

# References

Brent, R.P. (1973). *Algorithms for Minimization Without Derivatives*.
Prentice-Hall, Englewood Cliffs, NJ. ISBN: 9780130223715.

CIMMYT (1988). *From Agronomic Data to Farmer Recommendations: An
Economics Training Manual*. Completely revised edition. CIMMYT,
Mexico DF. ISBN: 9686127127.

Gittinger, J.P. (1982). *Economic Analysis of Agricultural Projects*,
2nd ed. Johns Hopkins University Press, Baltimore. ISBN: 9780801825439.

GoI (2008). *Guidelines for Economic Analysis of Projects*. Planning
Commission of India, New Delhi.

GoI (2015). *Common Guidelines for Watershed Development Projects under
PMKSY-WDC*. Ministry of Rural Development, New Delhi.

Joshi, P.K., Jha, A.K., Wani, S.P., Joshi, L. and Shiyani, R.L.
(2005). *Meta-Analysis to Assess Impact of Watershed Program and
People's Participation*. IWMI Research Report 8. ISBN: 9290906677.

NABARD (2019). *Operational Guidelines: Watershed Development Fund*.
National Bank for Agriculture and Rural Development, Mumbai.

Pouliquen, L.Y. (1970). *Risk Analysis in Project Appraisal*. World
Bank Staff Occasional Papers No. 11. Johns Hopkins University Press.

Squire, L. and van der Tak, H.G. (1975). *Economic Analysis of
Projects*. Johns Hopkins University Press. ISBN: 9780801816697.

Wischmeier, W.H. and Smith, D.D. (1978). *Predicting Rainfall Erosion
Losses: A Guide to Conservation Planning*. USDA Agriculture Handbook
No. 537. ISBN: 0160016258.
