---
title: "Local influence diagnostics for the EVBS regression model"
author: "Raydonal Ospina"
output: rmarkdown::html_vignette
vignette: >
  %\VignetteIndexEntry{Local influence diagnostics for the EVBS regression model}
  %\VignetteEngine{knitr::rmarkdown}
  %\VignetteEncoding{UTF-8}
---

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

## Introduction

The **evbsreg** package implements local influence diagnostics for the
Extreme-Value Birnbaum--Saunders (EVBS) regression model. This vignette
walks through the complete workflow: fitting the model, diagnosing
influential observations, assessing model adequacy, and interpreting the
results, using the bundled Itajaí wind gust dataset.

```{r setup}
library(evbsreg)
```

## The data

The `itajai` dataset contains 124 monthly maximum wind gust speeds and the
corresponding daily mean atmospheric pressure, recorded at INMET station
A-868 in Itajaí, Brazil, from July 2010 to October 2020.

```{r data}
data(itajai)
str(itajai)
summary(itajai$wind)
```

Observation 82 is the catastrophic event of 26 April 2017 (33.9 m/s).

## Fitting the model

The design matrix must include an intercept column. We model the location
of the log-EVBS distribution as a linear function of atmospheric pressure.

```{r fit}
X <- cbind(1, itajai$pressure)
fit <- evbsreg.fit(X, itajai$wind)

data.frame(
  Parameter = c("beta0", "beta1", "alpha", "gamma"),
  Estimate  = round(fit$coeff, 4),
  SE        = round(c(fit$stderrors, fit$stderroralpha, fit$stderrorgama), 4)
)
```

Both regression coefficients are highly significant:

```{r pvalues}
round(fit$pvalues, 4)
```

## Local influence diagnostics

The `cnc_diagnostics()` function computes the conformal normal curvature
diagnostics from the fitted object.

```{r diag}
diag <- cnc_diagnostics(fit)

## Top four normalized eigenvalues
round(head(diag$eigenvalues_norm, 4), 5)

## Observations flagged at q = 7
which(diag$Bj[7, ] > diag$bq[7])
```

The two-panel diagnostic figure shows the normalized eigenvalues (left) and
the aggregate contributions (right). Observation 82 dominates.

```{r plot-cnc, fig.width = 10, fig.height = 4.5}
plot_cnc(diag, q = 7)
```

## Deletion analysis

We refit the model without the flagged observation and measure the
relative change in each parameter.

```{r deletion}
fit82 <- evbsreg.fit(X[-82, ], itajai$wind[-82])
rc <- 100 * (fit82$coeff - fit$coeff) / abs(fit$coeff)
names(rc) <- c("beta0", "beta1", "alpha", "gamma")
round(rc, 2)
```

The tail-shape parameter $\gamma$ changes by about $-73.67\%$, while the
regression coefficients and the scale parameter change by less than $4\%$.
The influence is therefore concentrated almost entirely on the tail.

## Model adequacy

Randomized quantile residuals should be approximately standard normal under
a correct specification.

```{r residuals}
r <- rqrandomized(X, itajai$wind)
shapiro.test(r)
```

```{r envelope, fig.width = 6, fig.height = 6}
envelope_qq(X, itajai$wind, nrep = 100)
```

## Density shapes

The package also provides the density-plotting functions used to produce
Figures 1 and 2 of the paper.

```{r densities, fig.width = 7, fig.height = 5}
plot_evbs_alpha()
```

## Reproducing the full study

Five standalone scripts reproduce every figure, table, and simulation in the
paper. After installation they are available via `system.file()`:

```{r scripts, eval = FALSE}
# Density figures (Figures 1-2)
source(system.file("scripts/script_01_density_figures.R", package = "evbsreg"))

# Itajai application (Tables 1-3, Figures 3-6)
source(system.file("scripts/script_02_itajai_application.R", package = "evbsreg"))

# Monte Carlo (Tables 4-9); set m <- 500 inside for a quick check
source(system.file("scripts/script_03_simulation_scenario1.R", package = "evbsreg"))
source(system.file("scripts/script_04_simulation_scenario2.R", package = "evbsreg"))
source(system.file("scripts/script_05_simulation_scenario3.R", package = "evbsreg"))
```

## References

Ospina, R., Lima, J. I. C., Barros, M., and Macêdo, A. M. S. (2026).
*Local influence diagnostics for the extreme-value Birnbaum--Saunders
regression model: methodology, validation, and application to anomalous
wind gusts.* Submitted.
