library(lmtest)
library(tseries)
library(urca)
library(ggplot2)
library(ggfortify) 


data <- read.csv("M3b_stationary/module3b_data_PE_ratios.csv")

# Convert date column to Date type
data$date <- as.Date(data$date)

# Filter the sample from 2000Q2 to 2015Q2
start_date <- as.Date("2000-04-01")  # 2000Q2
end_date <- as.Date("2015-06-30")    # 2015Q2

sample_data <- subset(data, date >= start_date & date <= end_date)
pe_vector <- sample_data$pe_ind

# Extract PE_IND and convert to time series (quarterly)
pe_ind_ts <- ts(pe_vector, start = c(2000, 2), end = c(2015, 2), frequency = 12)



Plot Time Series

plot(pe_ind_ts, main = "Price-Earnings Ratio, India", xlab = "Year", ylab = "PE Ratio")


Augmented Dickey-Fuller (ADF) test


The Augmented Dickey-Fuller (ADF) test checks whether a time series is non-stationary due to the presence of a unit root.
Null Hypothesis (H₀): The series has a unit root → it is non-stationary.
p < 0.05 -> Reject H₀. Series is stationary at 5%.
p ≥ 0.05 -> Fail to reject H₀. Series is non-stationary at 5%.

adf_test <- adf.test(pe_ind_ts)
print(adf_test)
## 
##  Augmented Dickey-Fuller Test
## 
## data:  pe_ind_ts
## Dickey-Fuller = -3.3104, Lag order = 5, p-value = 0.07149
## alternative hypothesis: stationary



Phillips-Perron (PP) test


Null Hypothesis (H₀): The series has a unit root (i.e., is non-stationary).
p < 0.05 -> Reject H₀. Series is stationary at 5%.
p ≥ 0.05 -> Fail to reject H₀. Series is non-stationary at 5%.

pp_test <- pp.test(pe_ind_ts)
print(pp_test)
## 
##  Phillips-Perron Unit Root Test
## 
## data:  pe_ind_ts
## Dickey-Fuller Z(alpha) = -21.358, Truncation lag parameter = 4, p-value
## = 0.04719
## alternative hypothesis: stationary



Kwiatkowski-Phillips-Schmidt-Shin (KPSS) test


Null Hypothesis (H₀): The series is stationary.
p < 0.05 -> Reject H₀. Series is non-stationary at 5%.
p ≥ 0.05 -> Fail to reject H₀. Series is Series is stationary at 5%.

kpss_intercept <- kpss.test(pe_ind_ts)
print(kpss_intercept)
## 
##  KPSS Test for Level Stationarity
## 
## data:  pe_ind_ts
## KPSS Level = 0.4055, Truncation lag parameter = 4, p-value = 0.07478
kpss_trend <- kpss.test(pe_ind_ts, null = "Trend")
print(kpss_trend)
## 
##  KPSS Test for Trend Stationarity
## 
## data:  pe_ind_ts
## KPSS Trend = 0.16798, Truncation lag parameter = 4, p-value = 0.03169