## ----setup, include=FALSE-----------------------------------------------------
knitr::opts_chunk$set(collapse = TRUE, comment = "#>", fig.width = 7, fig.height = 4.5)

## ----load---------------------------------------------------------------------
library(farmPartial)

## ----direct-------------------------------------------------------------------
changes <- wheat_example("changes")
changes

pb <- partial_budget(changes, currency = "INR", unit = "per ha")
pb
budget_summary(pb)

## ----direct-plot, fig.cap="Favorable and adverse changes in the illustrative wheat partial budget."----
plot(pb)

## ----compare------------------------------------------------------------------
baseline <- wheat_example("baseline")
alternative <- wheat_example("alternative")
pb_compare <- compare_budgets(baseline, alternative)
pb_compare$comparison
budget_summary(pb_compare)

## ----break-even---------------------------------------------------------------
break_even_component(pb, "Additional herbicide")

## ----sensitivity--------------------------------------------------------------
s1 <- sensitivity_analysis(
  pb,
  "Higher grain return",
  multipliers = seq(0.6, 1.4, by = 0.1)
)
s1
plot(s1)

## ----two-way, fig.cap="Two-way sensitivity of net change to an added return and an added cost."----
s2 <- two_way_sensitivity(
  pb,
  "Higher grain return",
  "Additional herbicide",
  multipliers_x = seq(0.6, 1.4, by = 0.1),
  multipliers_y = seq(0.6, 1.4, by = 0.1)
)
plot(s2)

## ----scenarios----------------------------------------------------------------
scenario_spec <- data.frame(
  scenario = c(
    "Output stress", "Input stress",
    "Combined stress", "Combined stress"
  ),
  item = c(
    "Higher grain return", "Additional herbicide",
    "Higher grain return", "Additional herbicide"
  ),
  multiplier = c(0.75, 1.30, 0.75, 1.30)
)

scenario_results <- scenario_analysis(pb, scenario_spec)
scenario_results
plot(scenario_results)

## ----simulation---------------------------------------------------------------
uncertainty <- data.frame(
  item = c("Higher grain return", "Additional herbicide"),
  distribution = c("normal", "triangular"),
  mean = c(6000, NA),
  sd = c(900, NA),
  min = c(NA, 900),
  mode = c(NA, 1200),
  max = c(NA, 1700)
)

sim <- simulate_partial_budget(pb, uncertainty, n = 3000, seed = 2026)
summary(sim)

## ----simulation-plot, fig.cap="Monte Carlo distribution of the partial-budget net change."----
plot(sim)

## ----capital------------------------------------------------------------------
annualize_investment(
  purchase = 120000,
  salvage = 20000,
  life = 8,
  rate = 0.08
)

## ----trial--------------------------------------------------------------------
trials <- trial_budget(
  treatment = c("Farmer practice", "Treatment A", "Treatment B", "Treatment C"),
  yield = c(3.0, 3.4, 3.8, 4.1),
  price = 22000,
  variable_cost = c(18000, 22000, 28000, 39000),
  yield_adjustment = 0.90
)
trials
dominance_analysis(trials)
marginal_analysis(trials, minimum_mrr = 50)

