## ----setup, include=FALSE-----------------------------------------------------
knitr::opts_chunk$set(echo = TRUE)

## ----eval=FALSE---------------------------------------------------------------
# # Install from local directory
# install.packages("path/to/MAARTS", repos = NULL, type = "source")

## -----------------------------------------------------------------------------
library(MAARTS)

## -----------------------------------------------------------------------------
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")

## -----------------------------------------------------------------------------
desc_stats <- ma_descriptive_stats(ma_sample_data)
print(desc_stats)

## -----------------------------------------------------------------------------
stationarity <- ma_stationarity_tests(ma_sample_data)
print(stationarity)

## -----------------------------------------------------------------------------
acf_pacf <- ma_acf_pacf(ma_sample_data, plot = TRUE)
print(acf_pacf)

## -----------------------------------------------------------------------------
ar_model <- ma_ar_fit(ma_sample_data, order = 2)
print(ar_model)

## -----------------------------------------------------------------------------
forecast <- ma_forecast(ar_model, h = 12, confidence = c(0.80, 0.90, 0.95, 0.99))
print(forecast)
plot(forecast)

## -----------------------------------------------------------------------------
diagnostics <- ma_diagnostic_tests(ar_model)
print(diagnostics)

## -----------------------------------------------------------------------------
resid_diag <- ma_residual_diagnostics(ar_model, plot = FALSE)
print(resid_diag)

## -----------------------------------------------------------------------------
stability <- ma_stability_analysis(ar_model)
print(stability)

## -----------------------------------------------------------------------------
irf <- ma_impulse_response(ar_model, n_periods = 20, plot = FALSE)
print(irf)

## -----------------------------------------------------------------------------
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)

## -----------------------------------------------------------------------------
breaks <- ma_structural_break(ma_sample_data)
print(breaks)

## -----------------------------------------------------------------------------
spectral <- ma_spectral_analysis(ma_sample_data, plot = FALSE)
print(spectral)

## -----------------------------------------------------------------------------
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)

## -----------------------------------------------------------------------------
# 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)

## -----------------------------------------------------------------------------
# 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")

