### R code from vignette source 'agriME-introduction.Rnw'

###################################################
### code chunk number 1: totals
###################################################
library(agriME)

marketing_metrics(
  producer_price = 1900,
  consumer_price = 3150,
  marketing_cost = 510,
  marketing_margin = 740,
  channel = "Producer-Wholesaler-Retailer"
)


###################################################
### code chunk number 2: channels
###################################################
data(tomato_channels)
fit <- analyse_channels(tomato_channels)
summary(fit)[, c(
  "channel", "producer_price", "consumer_price", "price_spread",
  "producer_share_percent", "acharya_efficiency"
)]


###################################################
### code chunk number 3: decomposition
###################################################
plot(fit, type = "decomposition", las = 2, cex.names = 0.75)


###################################################
### code chunk number 4: rupee-ranking
###################################################
consumer_rupee(fit)
rank_channels(fit)


###################################################
### code chunk number 5: bootstrap
###################################################
data(market_observations)
boot <- bootstrap_marketing_metrics(
  market_observations,
  marketing_margin = "marketing_margin",
  channel = "channel",
  weight = "volume_qtl",
  R = 199,
  seed = 2026
)
subset(boot$summary, metric == "acharya_efficiency")


###################################################
### code chunk number 6: bootstrap-figure
###################################################
plot(boot, metric = "acharya_efficiency")


###################################################
### code chunk number 7: sensitivity
###################################################
sens <- marketing_sensitivity(
  producer_price = 1900,
  consumer_price = 3150,
  marketing_cost = 510,
  marketing_margin = 740,
  producer_change = c(-0.05, 0, 0.05),
  cost_change = c(-0.10, 0, 0.10)
)
head(sens[, c(
  "producer_change", "cost_change", "price_spread",
  "producer_share_percent", "acharya_efficiency"
)])

efficiency_target(
  target = 2,
  method = "acharya",
  solve_for = "marketing_cost",
  producer_price = 1900,
  marketing_margin = 740
)


###################################################
### code chunk number 8: loss
###################################################
loss_adjusted_margin(
  purchase_price = 20,
  sale_price = 28,
  quantity_purchased = 100,
  quantity_sold = 92,
  marketing_cost = 180,
  cost_basis = "lot"
)
