---
title: "Local methods example"
output: 
  rmarkdown::html_vignette:
    fig_width: 5
    fig_height: 5
vignette: >
  %\VignetteIndexEntry{Local methods example}
  %\VignetteEngine{knitr::rmarkdown}
  %\VignetteEncoding{UTF-8}
---

```{r, include = FALSE}
knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>",
        echo=TRUE, results='hold', warning=FALSE, cache=FALSE, 
  message=FALSE, 
  tidy.opts=list(width.cutoff=75), tidy=TRUE,
  # --- The size optimization flags ---
  dev = 'png',       # Forces compressed PNG files instead of heavy vector shapes
  dpi = 72,          # Slashes the image resolution down to web-standard 72 DPI
  fig.retina = NULL  # Disables high-density Retina scaling which doubles image sizes
)
old <- options(scipen = 1, digits = 4)
```

```{r setup}
library(circularEV)
plotly_installed <- requireNamespace("plotly", quietly = TRUE)
if(!plotly_installed) {
  message("Skipping plotly-based figures because the 'plotly' package is not installed.")
}
```

### Reading Data

```{r}
data(HsSP)
data(drc)
timeRange <- 54.5

idx <- order(drc)
drc <- drc[idx]
Data <- HsSP[idx]
set.seed(1234)
Data <- Data + runif(length(Data), -1e-4, 1e-4)
```

```{r}
PlotData(Data=Data, drc=drc, thr=NULL, pointSize=1, cex.axis=15, cex.lab=2, thrWidth=2)
if(plotly_installed) {
  PolarPlotData(Data=Data, drc=drc, thr=NULL, pointSize=4, fontSize=14,
                thrWidth=4, ylim=c(0,max(Data)) )
} else {
  message("plotly not installed: skipping PolarPlotData output")
}
```


### Threshold selection

Grid values at which the estimation is performed:
```{r}
thetaVec <- 1:360
```

```{r, eval=F}
thrResultMom <- ThrSelection(Data=Data, drc=drc, h=60, b=0.35, thetaGrid=thetaVec,
                          EVIestimator="Mom", useKernel=TRUE, concent=10, bw=30, numCores=2)$thr
```

```{r, echo=F}
data(thresholdExampleMom)
thrResultMom <- thresholdExampleMom
```




```{r}
PlotData(Data=Data, drc=drc, thr=thrResultMom, pointSize=1, cex.axis=15, cex.lab=2, thrWidth=2)
if(plotly_installed) {
  PolarPlotData(Data=Data, drc=drc, thr=thrResultMom, pointSize=4, fontSize=12, 
                thrWidth=4, ylim=c(0,max(Data)))
} else {
  message("plotly not installed: skipping PolarPlotData output")
}
```



### Estimation of EVI, scale and T-year levels

```{r, results='hide'}
h <- 60
useKernel <- TRUE
concent <- 10
movThr <- TRUE
nBoot <- 30
set.seed(1234)
output <- LocalEstim(Data=Data, drc=drc, thr=thrResultMom,
                     thetaGrid=thetaVec, nBoot=nBoot, EVIestimator="Mom", h=h,
                     useKernel=useKernel, concent=concent, movThr=movThr,
                     TTs=c(100, 10000), timeRange=timeRange)

RLBoot <- output$RLBoot
```


```{r}
PlotParamEstim(bootEstimates=output$xiBoot, thetaGrid=thetaVec, ylab=bquote(hat(xi)),
               alpha=0.05, ylim=NULL, cex.axis=15, cex.lab=2, thrWidth=2)

PlotParamEstim(bootEstimates=output$sigBoot, thetaGrid=thetaVec, ylab=bquote(hat(sigma)),
               alpha=0.05, ylim=NULL, cex.axis=15, cex.lab=2, thrWidth=2)
```


```{r}
# 100-year level

PlotRL(RLBootList=RLBoot, thetaGrid=thetaVec, Data=Data, drc=drc,
       TTs=c(100, 10000), whichPlot=1, alpha=0.05, ylim=NULL,
       pointSize=1, cex.axis=15, cex.lab=2, thrWidth=2)
if(plotly_installed) {
  PolarPlotRL(RLBootList=RLBoot, thetaGrid=thetaVec, Data=Data, drc=drc,
              TTs=c(100, 10000), whichPlot=1, alpha=0.05, ylim=NULL,
              pointSize=4, fontSize=12, lineWidth=2)
} else {
  message("plotly not installed: skipping PolarPlotRL output")
}
```


```{r}
# 10000-year level

PlotRL(RLBootList=RLBoot, thetaGrid=thetaVec, Data=Data, drc=drc,
       TTs=c(100, 10000), whichPlot=2, alpha=0.05, ylim=NULL,
       pointSize=1, cex.axis=15, cex.lab=2, thrWidth=2)
if(plotly_installed) {
  PolarPlotRL(RLBootList=RLBoot, thetaGrid=thetaVec, Data=Data, drc=drc,
              TTs=c(100, 10000), whichPlot=2, alpha=0.05, ylim=NULL,
              pointSize=4, fontSize=12, lineWidth=2)
} else {
  message("plotly not installed: skipping PolarPlotRL output")
}
```

```{r, include = FALSE}
options(old)
```
