## ----setup, include=FALSE-----------------------------------------------------
knitr::opts_chunk$set(collapse = TRUE, comment = "#>")

## ----basic--------------------------------------------------------------------
library(IndFarmCost)

dat <- farm_cost_example()
fc <- farm_costs(dat)
fc[1:4, c("farm_id", "crop", "A1", "A2", "B1", "B2", "C1", "C2", "C3")]

## ----a2fl---------------------------------------------------------------------
a2_plus_fl(fc)[1:4]

## ----aggregate----------------------------------------------------------------
farm_costs_aggregate(fc, by = "crop")
farm_costs_aggregate(fc, by = c("state", "farm_size"), method = "median")

## ----summary------------------------------------------------------------------
summarize_costs(fc)

## ----returns------------------------------------------------------------------
ret <- farm_returns(
  fc,
  main_output = "main_output_q",
  main_price = "main_price_rs_q",
  byproduct_output = "byproduct_output_q",
  byproduct_price = "byproduct_price_rs_q"
)
head(ret[, c("gross_return", "net_C2", "net_C3", "bcr_C2", "bcr_C3")])

## ----cop----------------------------------------------------------------------
byproduct_value <- dat$byproduct_output_q * dat$byproduct_price_rs_q
cost_of_production(fc, "main_output_q", concept = "C2",
                   byproduct_value = byproduct_value)[1:4]
break_even_price(fc, "main_output_q", concept = "C3",
                 byproduct_value = byproduct_value)[1:4]

## ----shares-------------------------------------------------------------------
shares <- cost_shares(fc, "C3")
head(shares)

## ----sensitivity--------------------------------------------------------------
cost_sensitivity(dat, "fertilizer", changes = c(-0.20, -0.10, 0, 0.10, 0.20))

## ----plot, fig.width=6, fig.height=4------------------------------------------
plot(fc, row = 1)

## ----custom-------------------------------------------------------------------
my_a1 <- setdiff(standard_a1_components(), "insurance")
fc_custom <- farm_costs(dat, a1_cols = my_a1)
fc_custom[1:3, c("A1", "C2", "C3")]

