# File Name: time_series_analysis.R
# Author: Gerard King - www.gerardking.dev
# Title: Time Series Analysis and Decomposition
# Description: This R program generates synthetic time series data, decomposes it into
# trend, seasonal, and residual components, and visualizes the decomposition. It demonstrates
# how to analyze time series data for understanding underlying patterns.
# Use Cases:
# - Analyzing trends in time-dependent data
# - Forecasting future values based on past patterns
# - Detecting seasonality and trends in business, finance, and operations
# Audience:
# - Data analysts working with time series data
# - Economists and business analysts forecasting future trends
# - Students learning about time series analysis and decomposition
# Blue Team Uses:
# - Monitoring and forecasting system performance trends over time
# - Detecting seasonal anomalies in system behavior or network traffic
# - Analyzing time-dependent data to predict potential security threats
# Red Team Uses:
# - Analyzing attack patterns over time to forecast future intrusion attempts
# - Simulating time series data for breach simulations and testing
# - Detecting and modeling seasonal patterns in security breaches or network anomalies
# Current Date: 2025-03-06
# Set the seed for reproducibility
set.seed(789)
# Generate synthetic time series data with trend and seasonality
time <- 1:120 # 120 time points (e.g., 10 years of monthly data)
trend <- time * 0.5 # Linear trend component
seasonality <- sin(2 * pi * time / 12) * 10 # Seasonal component (12 months cycle)
noise <- rnorm(120, mean=0, sd=3) # Random noise
# Combine components to create the time series
data <- trend + seasonality + noise
# Create a time series object
ts_data <- ts(data, start=c(2015, 1), frequency=12)
# Decompose the time series into trend, seasonal, and residual components
decomposed_ts <- decompose(ts_data)
# Plot the original time series
plot(ts_data, main="Original Time Series", xlab="Time", ylab="Value", col="blue")
# Plot the decomposed components
plot(decomposed_ts)
# Display the decomposition components
cat("Trend component:\n")
print(decomposed_ts$trend)
cat("\nSeasonal component:\n")
print(decomposed_ts$seasonal)
cat("\nResidual component:\n")
print(decomposed_ts$random)
# Print the current date for reference
cat("Date of execution:", Sys.Date(), "\n")