---
title: "Bayesian Unit Root Testing with Linear Spline and Polynomial Trends"
author: "Shikhar Tyagi, Arvind Pandey, Bhupendra Singh, Vrijesh Tripathi"
date: "`r Sys.Date()`"
output: rmarkdown::html_vignette
vignette: >
  %\VignetteIndexEntry{Bayesian Unit Root Testing with Linear Spline and Polynomial Trends}
  %\VignetteEngine{knitr::rmarkdown}
  %\VignetteEncoding{UTF-8}
---

```{r setup, include = FALSE}
knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>",
  fig.width = 6,
  fig.height = 4
)
library(BayesSplineUR)
```

## Introduction

The `BayesSplineUR` package provides a unified framework for Bayesian unit root hypothesis testing in autoregressive time series models featuring non-linear trends. The primary method implements the linear spline trend Bayesian unit root test derived by **Kumar et al. (2020)** (*Statistics, Optimization & Information Computing*, 8(2), 425-461, DOI: 10.19139/soic-2310-5070-786). Additionally, the package supports the maintained polynomial trend Bayesian unit root test proposed by **Chaturvedi and Kumar (2005)** (*Statistics & Probability Letters*, 74(1), 109-115, DOI: 10.1016/j.spl.2005.04.044).

## Model Specification

Consider an AR(1) process with non-linear trend approximated by a linear spline function with $r$ join points (knots) $1 < t_1 < t_2 < \dots < t_r < T$:

$$y_t = \delta_0 + \delta t + \sum_{i=1}^r \psi_i s_i(t) + u_t, \quad u_t = \rho u_{t-1} + \epsilon_t, \quad \epsilon_t \sim i.i.d. N(0, \tau^{-1})$$

where $s_i(t) = (t - t_i)_+ = \max(0, t - t_i)$. Under the unit root hypothesis $H_0: \rho = 1$, the model reduces to first differences:

$$\Delta y_t = \delta + \sum_{i=1}^r \psi_i [s_i(t) - s_i(t-1)] + \epsilon_t$$

The testing decision relies on the **Posterior Odds Ratio (POR)** $\beta_{01} = \frac{P(H_0|y)}{P(H_1|y)}$, Bayes factor $BF_{01}$, and posterior probabilities $P(H_0|y)$ vs $P(H_1|y)$.

## Example 1: Linear Spline Trend Test (Kumar et al., 2020)

We first demonstrate the linear spline unit root test on simulated time series data.

```{r spline_example}
set.seed(123)
t_vec <- 1:70
# Stationary AR(1) process with linear spline trend
u <- numeric(70)
for (t in 2:70) u[t] <- 0.45 * u[t - 1] + rnorm(1, 0, 1)
y_stat <- 10 + 0.15 * t_vec + pmax(0, t_vec - 35) * 0.4 + u

# Perform Bayesian unit root test with spline trend
res_spline <- bayes_ur_spline_test(y_stat, knots = c(35))
print(res_spline)
summary(res_spline)
```

## Example 2: Automatic Knot Selection (AIC/BIC)

When knot locations are unknown, `select_knots()` automatically identifies optimal join points based on AIC or BIC criteria:

```{r knot_selection}
# Automatic knot selection
res_auto <- bayes_ur_test(y_stat, trend_type = "spline", r = 1)
summary(res_auto)
```

## Example 3: Real Data Analysis (ARF Countries Imports)

The package includes the `arf_imports` dataset analyzed in Section 5 of Kumar et al. (2020):

```{r real_data}
data(arf_imports)
india_imports <- arf_imports$India

# Fit linear spline test with knots identified in paper (r=3 at t=14, 32, 52)
res_india <- bayes_ur_spline_test(india_imports, knots = c(14, 32, 52))
summary(res_india)
```

## Graphical Diagnostic Plot

The S3 `plot()` method displays the normalized posterior density of $\rho$ under $H_1$, the uniform prior density $U(a, 1)$, the posterior mean estimate, and the point mass assigned to the unit root hypothesis $H_0$:

```{r plot_diagnostic, fig.width = 6, fig.height = 4}
plot(res_india)
```

## References

- Kumar, J., Agiwal, V., Kumar, D., & Chaturvedi, A. (2020). Bayesian unit root test for AR(1) model with trend approximated by linear spline function. *Statistics, Optimization & Information Computing*, 8(2), 425--461. DOI: 10.19139/soic-2310-5070-786.
- Chaturvedi, A., & Kumar, J. (2005). Bayesian unit root test for model with maintained trend. *Statistics & Probability Letters*, 74(1), 109--115. DOI: 10.1016/j.spl.2005.04.044.
- Schotman, P., & van Dijk, H. K. (1991). A Bayesian analysis of the unit root in real exchange rates. *Journal of Econometrics*, 49(1-2), 195--238. DOI: 10.1016/0304-4076(91)90038-F.
