---
title: "Farm Partial-Budget Analysis with farmPartial"
author: "Chiranjit Mazumder, Utkarsh Tiwari, and Anbukkani Perumal"
output: rmarkdown::html_vignette
vignette: >
  %\VignetteIndexEntry{Farm Partial-Budget Analysis with farmPartial}
  %\VignetteEngine{knitr::rmarkdown}
  %\VignetteEncoding{UTF-8}
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(collapse = TRUE, comment = "#>", fig.width = 7, fig.height = 4.5)
```

## 1. What a partial budget measures

A partial budget estimates the economic effect of a limited change in the farm
plan. It includes only returns and costs that differ between the baseline and
the alternative. Let

- \(AR\) = added returns,
- \(RC\) = reduced costs,
- \(AC\) = added costs, and
- \(RR\) = reduced returns.

Then the expected change in net return is

\[
\Delta NR = (AR + RC) - (AC + RR).
\]

This framing is useful for questions such as whether to change tillage practice,
adopt a new input, alter irrigation management, or introduce a small equipment
investment when most of the farm business remains unchanged.

```{r load}
library(farmPartial)
```

## 2. Direct four-component analysis

The package contains an internally generated wheat example in INR per hectare.
It is intended only to demonstrate the method.

```{r direct}
changes <- wheat_example("changes")
changes

pb <- partial_budget(changes, currency = "INR", unit = "per ha")
pb
budget_summary(pb)
```

```{r direct-plot, fig.cap="Favorable and adverse changes in the illustrative wheat partial budget."}
plot(pb)
```

The example has INR 10,800/ha of favorable changes and INR 4,300/ha of adverse
changes, producing an expected net increase of INR 6,500/ha. That result is
conditional on the values entered; it is not a statement that the alternative
is universally preferable.

## 3. Start from baseline and alternative plans

When the two plans are already available as farm-budget tables,
`compare_budgets()` automatically classifies their differences.

```{r compare}
baseline <- wheat_example("baseline")
alternative <- wheat_example("alternative")
pb_compare <- compare_budgets(baseline, alternative)
pb_compare$comparison
budget_summary(pb_compare)
```

The direct and comparison workflows lead to the same INR 6,500/ha net change.

## 4. Break-even analysis

A break-even value asks how far one component could move before the economic
advantage disappears, with other entries held fixed.

```{r break-even}
break_even_component(pb, "Additional herbicide")
```

This can be reported as both a component amount and a multiple of the amount in
the central partial budget.

## 5. One-way and two-way sensitivity

```{r sensitivity}
s1 <- sensitivity_analysis(
  pb,
  "Higher grain return",
  multipliers = seq(0.6, 1.4, by = 0.1)
)
s1
plot(s1)
```

```{r 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)
```

Sensitivity analysis is especially important when partial-budget conclusions
depend on output prices, yields, hired labor costs, energy prices, or other
values that can vary substantially.

## 6. Named scenarios

Scenario analysis changes several components together, which is often more
realistic than moving one item at a time.

```{r 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)
```

## 7. Monte Carlo uncertainty analysis

For uncertain inputs, assign probability distributions to selected components.
Unspecified items stay fixed at their central values.

```{r 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)
```

```{r simulation-plot, fig.cap="Monte Carlo distribution of the partial-budget net change."}
plot(sim)
```

The probability of a positive net change is often more decision-relevant than a
single deterministic estimate. The interval returned here is an uncertainty
interval induced by the assumed component distributions, not a sampling
confidence interval.

## 8. Annualizing a capital change

When a new farm practice requires equipment that lasts several years, a one-year
partial budget should usually include an annualized capital charge rather than
the entire purchase price.

```{r capital}
annualize_investment(
  purchase = 120000,
  salvage = 20000,
  life = 8,
  rate = 0.08
)
```

## 9. Dominance and marginal analysis for trials

Treatment-level economic analysis can be paired with partial-budget reasoning.
The following example adjusts experimental yield downward by 10 percent before
calculating gross and net benefits.

```{r 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)
```

Dominance removes an alternative when another achieves at least as much net
benefit at no greater variable cost, with a strict improvement in at least one
dimension. Marginal analysis then asks how much additional net benefit is
generated by the extra cost of moving between remaining alternatives.

## 10. Interpretation limits

A favorable partial budget is evidence about incremental profitability under the
assumptions entered. It does not prove that the farmer has enough labor, credit,
water, machinery capacity, or risk tolerance to implement the change. If the
proposed change reorganizes many interacting enterprises or changes binding
resource constraints, a whole-farm model or investment appraisal may be more
appropriate.

## Reference

CIMMYT. (1988). *From agronomic data to farmer recommendations: An economics
training manual* (completely revised edition). CIMMYT. ISBN 968-6127-19-4.

