Generate production-ready R scripts for time series analysis with ARIMA modeling
If the p-value is > 0.05, your data is Non-Stationary. You MUST increase your d parameter!
chartSeries is what professional traders use in R to see trends, volume, and technical indicators all at once.
ACF helps find the $$q$$ (Moving Average order)PACF helps find the $$p$$ (AutoRegressive order)
1# ═══════════════════════════════════════════════════════════════
2# Time Series Analysis Script
3# Generated for: my_data.csv
4# ═══════════════════════════════════════════════════════════════
5
6# Install required packages (run once)
7# install.packages(c("forecast", "tseries", "ggplot2", "quantmod"))
8
9# Load libraries
10library(forecast)
11library(tseries)
12library(ggplot2)
13library(quantmod)
14
15# ─────────────────────────────────────────────────────────────────
16# 1. DATA LOADING
17# ─────────────────────────────────────────────────────────────────
18
19data <- read.csv("my_data.csv")
20ts_data <- ts(data$Close, frequency = 252) # Daily financial data
21
22# Quick summary
23cat("Data Summary:\n")
24summary(ts_data)
25
26# ─────────────────────────────────────────────────────────────────
27# 2. STATIONARITY TEST (ADF)
28# ─────────────────────────────────────────────────────────────────
29# If p-value > 0.05, data is NON-STATIONARY. Increase 'd' parameter!
30
31adf_result <- adf.test(ts_data)
32cat("\n═══ ADF Test Results ═══\n")
33cat(paste("Test Statistic:", round(adf_result$statistic, 4), "\n"))
34cat(paste("P-Value:", round(adf_result$p.value, 4), "\n"))
35
36if (adf_result$p.value > 0.05) {
37 cat("⚠️ WARNING: Data is NON-STATIONARY! Consider increasing 'd'.\n")
38} else {
39 cat("✓ Data is STATIONARY. Good to proceed!\n")
40}
41
42# ─────────────────────────────────────────────────────────────────
43# 3. ACF & PACF ANALYSIS
44# ─────────────────────────────────────────────────────────────────
45# ACF → helps find 'q' (Moving Average order)
46# PACF → helps find 'p' (AutoRegressive order)
47
48par(mfrow = c(1, 2))
49acf(ts_data, main = "ACF - Find q (MA Order)", lag.max = 40)
50pacf(ts_data, main = "PACF - Find p (AR Order)", lag.max = 40)
51par(mfrow = c(1, 1))
52
53# ─────────────────────────────────────────────────────────────────
54# 4. ARIMA MODEL
55# ─────────────────────────────────────────────────────────────────
56
57# Manual ARIMA(1, 1, 1) configuration
58model <- Arima(ts_data, order = c(1, 1, 1))
59
60# Model summary
61cat("\n═══ ARIMA Model Summary ═══\n")
62summary(model)
63
64# ─────────────────────────────────────────────────────────────────
65# 5. FORECASTING
66# ─────────────────────────────────────────────────────────────────
67
68forecast_horizon <- 30 # Forecast 30 periods ahead
69fc <- forecast(model, h = forecast_horizon)
70
71# Plot forecast with confidence intervals
72plot(fc, main = "ARIMA Forecast",
73 xlab = "Time", ylab = "Value",
74 col = "#5B8FF9", fcol = "#9254DE",
75 shadecols = c("#9254DE20", "#9254DE40"))
76
77# ─────────────────────────────────────────────────────────────────
78# 6. FINANCIAL VISUALIZATION (quantmod)
79# ─────────────────────────────────────────────────────────────────
80# chartSeries is what professional traders use to see trends,
81# volume, and technical indicators all at once.
82
83# Convert to xts for quantmod compatibility
84if ("Date" %in% colnames(data)) {
85 data$Date <- as.Date(data$Date)
86 xts_data <- xts::xts(data[, -1], order.by = data$Date)
87
88 # Professional trading chart
89 chartSeries(xts_data,
90 name = "Financial Time Series",
91 theme = chartTheme("black"),
92 TA = c(addVo(), addBBands(), addMACD()))
93}
94
95# ─────────────────────────────────────────────────────────────────
96# 7. MODEL DIAGNOSTICS
97# ─────────────────────────────────────────────────────────────────
98
99checkresiduals(model)
100
101# Ljung-Box test for autocorrelation in residuals
102lb_test <- Box.test(residuals(model), type = "Ljung-Box", lag = 20)
103cat("\n═══ Ljung-Box Test ═══\n")
104cat(paste("P-Value:", round(lb_test$p.value, 4), "\n"))
105if (lb_test$p.value > 0.05) {
106 cat("✓ No significant autocorrelation in residuals. Model is adequate.\n")
107} else {
108 cat("⚠️ Significant autocorrelation detected. Consider adjusting parameters.\n")
109}
110
111# ═══════════════════════════════════════════════════════════════
112# END OF SCRIPT
113# ═══════════════════════════════════════════════════════════════