---
title: "MAARTS: A Comprehensive Guide to M&A AR Time-Series Analysis"
author: "Shikhar Tyagi"
date: "`r Sys.Date()`"
output: rmarkdown::html_vignette
vignette: >
  %\VignetteIndexEntry{MAARTS User Guide}
  %\VignetteEngine{knitr::rmarkdown}
  %\VignetteEncoding{UTF-8}
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```

# Introduction

The MAARTS (Merger and Acquisition Autoregressive Time-Series Models) package provides a comprehensive framework for analyzing M&A time-series data using autoregressive models. This vignette demonstrates the key features and functionality of the package.

# Installation

```{r, eval=FALSE}
# Install from local directory
install.packages("path/to/MAARTS", repos = NULL, type = "source")
```

# Loading the Package

```{r}
library(MAARTS)
```

# Sample Data

The package includes a sample M&A time-series dataset for demonstration:

```{r}
data(ma_sample_data)
head(ma_sample_data)
plot(ma_sample_data, type = "l", main = "Sample M&A Time-Series",
     xlab = "Time", ylab = "M&A Activity")
```

# Descriptive Statistics

Calculate comprehensive descriptive statistics:

```{r}
desc_stats <- ma_descriptive_stats(ma_sample_data)
print(desc_stats)
```

# Stationarity Tests

Perform stationarity tests to ensure the time-series is suitable for AR modeling:

```{r}
stationarity <- ma_stationarity_tests(ma_sample_data)
print(stationarity)
```

# ACF and PACF Analysis

Examine autocorrelation structure to determine appropriate AR order:

```{r}
acf_pacf <- ma_acf_pacf(ma_sample_data, plot = TRUE)
print(acf_pacf)
```

# AR Model Estimation

Fit an AR model to the data:

```{r}
ar_model <- ma_ar_fit(ma_sample_data, order = 2)
print(ar_model)
```

# Forecasting

Generate forecasts with multiple confidence intervals:

```{r}
forecast <- ma_forecast(ar_model, h = 12, confidence = c(0.80, 0.90, 0.95, 0.99))
print(forecast)
plot(forecast)
```

# Diagnostic Tests

Check for residual autocorrelation:

```{r}
diagnostics <- ma_diagnostic_tests(ar_model)
print(diagnostics)
```

# Residual Diagnostics

Comprehensive residual analysis including normality and heteroscedasticity tests:

```{r}
resid_diag <- ma_residual_diagnostics(ar_model, plot = FALSE)
print(resid_diag)
```

# Stability Analysis

Analyze model stability and persistence:

```{r}
stability <- ma_stability_analysis(ar_model)
print(stability)
```

# Impulse Response Analysis

Analyze shock transmission and dynamic effects:

```{r}
irf <- ma_impulse_response(ar_model, n_periods = 20, plot = FALSE)
print(irf)
```

# Model Comparison

Compare multiple AR models using information criteria:

```{r}
ar1 <- ma_ar_fit(ma_sample_data, order = 1)
ar2 <- ma_ar_fit(ma_sample_data, order = 2)
ar3 <- ma_ar_fit(ma_sample_data, order = 3)
comparison <- ma_model_comparison(ar1, ar2, ar3)
print(comparison)
```

# Structural Break Analysis

Detect structural breaks in the time-series:

```{r}
breaks <- ma_structural_break(ma_sample_data)
print(breaks)
```

# Spectral Analysis

Identify cyclical components in the frequency domain:

```{r}
spectral <- ma_spectral_analysis(ma_sample_data, plot = FALSE)
print(spectral)
```

# Monte Carlo Simulation

Evaluate estimator performance using simulation:

```{r}
set.seed(123)
sim_results <- ma_monte_carlo_simulation(
  true_coefficients = c(0.6, -0.2),
  intercept = 10,
  n = 100,
  n_sim = 500,
  sigma = 2
)
print(sim_results)
```

# Accuracy Measures

Calculate forecast accuracy measures:

```{r}
# Create actual vs predicted for demonstration
actual <- ma_sample_data[1:150]
predicted <- ma_sample_data[2:151]
accuracy <- ma_accuracy(actual, predicted)
print(accuracy)
```

# Complete Workflow Example

A complete analysis workflow:

```{r}
# 1. Load and examine data
data(ma_sample_data)
plot(ma_sample_data, type = "l", main = "M&A Time-Series")

# 2. Descriptive statistics
desc_stats <- ma_descriptive_stats(ma_sample_data)

# 3. Check stationarity
stationarity <- ma_stationarity_tests(ma_sample_data)

# 4. Examine ACF/PACF
acf_pacf <- ma_acf_pacf(ma_sample_data, plot = FALSE)

# 5. Fit models of different orders
models <- list()
for (p in 1:4) {
  models[[p]] <- ma_ar_fit(ma_sample_data, order = p)
}

# 6. Compare models
comparison <- do.call(ma_model_comparison, models)
print(comparison)

# 7. Select best model
best_model <- models[[comparison$Order[1]]]

# 8. Forecast
forecast <- ma_forecast(best_model, h = 12)

# 9. Diagnostics
diagnostics <- ma_diagnostic_tests(best_model)
resid_diag <- ma_residual_diagnostics(best_model, plot = FALSE)

# 10. Stability analysis
stability <- ma_stability_analysis(best_model)

# 11. Impulse response
irf <- ma_impulse_response(best_model, n_periods = 20, plot = FALSE)

# Summary
cat("\n=== Analysis Summary ===\n")
cat("Best Model: AR", comparison$Order[1], "\n")
cat("AIC:", round(comparison$AIC[1], 4), "\n")
cat("BIC:", round(comparison$BIC[1], 4), "\n")
cat("Stable:", stability$is_stable, "\n")
cat("Persistence:", round(stability$persistence, 4), "\n")
```

# Conclusion

The MAARTS package provides a comprehensive toolkit for M&A time-series analysis. All major aspects of AR modeling are covered, from descriptive statistics and stationarity testing to forecasting and advanced diagnostic analysis.
