---
title: "forestDomo"
author: "Seong D. Yun"
output: rmarkdown::html_vignette
vignette: >
  %\VignetteIndexEntry{forestDemo}
  %\VignetteEngine{knitr::rmarkdown}
  %\VignetteEncoding{UTF-8}
---

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

# `vaprox.split()` Demonstration

This vignette illustrates the use of `vaprox.split()` to approximate the
value function in a forest management application.

The example replicates the Douglas-fir forest application for an
industrial site class 3 in western Oregon from Hashida and Fenichel
(2022).

The demonstration data are provided in the `forest` dataset included
in the `capn` package.

```{r example}
## Example (industrial classite 3) from Hashida and Fenichel (2022)
data("forest")

param   <- forest$param
simData <- forest$simData

Aspace <- aproxdef(param$order,param$lowerK,param$upperK,param$delta)

vCsplit <- vaprox.split(Aspace, simData$vol, simData$growth, 
                        simData$profit, param$crit.vol, 
                        param$margp, param$splittime)

vsimsplit <- vsim(vCsplit, simData[,1])

## harvest node and its time
hnode <- min(which(simData$vol > param$crit.vol)) 
m <- hnode -1

## plot data generation
plotdata <- data.frame(stock = vsimsplit$stock[1:m],
            price = exp(-(simData$times[1:m]-simData$times[1])*param$delta)*vsimsplit$shadowp[1:m],
            vfun = exp(-(simData$times[1:m]-simData$times[1])*param$delta)*vsimsplit$vfun[1:m])

## plot shadow (accounting) prices
## 3p of Figure 4 in Hashida and Fenichel (2022)
plot(plotdata$stock,plotdata$price,type='l',col='blue',
     xlab = 'Forest Volume (MBF)',
     ylab = 'Shadow Price ($/MBF)')

## plot value function
plot(plotdata$stock,plotdata$vfun,type='l',
     xlab = 'Forest Volume (MBF)',
     ylab = 'Value Function ($)')
```
