# 시계열 분석 — 월별 항공 승객 자료의 일반 차분과 계절 차분
# 적용 범위: 공식 AirPassengers 자료와 diff()·ts의 정의를 조합한 학습 사례입니다. AirPassengers 문서처럼 log10 변환을 사용하되, 공식 ARIMA 적합 코드를 복제하지 않고 차분의 간격·길이·시간축을 비교합니다.
# 출처: https://stat.ethz.ch/R-manual/R-patched/library/datasets/html/AirPassengers.html
# 출처: https://stat.ethz.ch/R-manual/R-patched/library/base/html/diff.html
# 출처: https://stat.ethz.ch/R-manual/R-patched/library/stats/html/ts.html
# 실행 확인 환경: R version 4.6.2 (2026-10-29)
# 아래 코드를 실행하면 출력과 그림을 확인할 수 있습니다.

data(AirPassengers)
x <- log10(AirPassengers)
d1 <- diff(x)
d12 <- diff(x, lag = 12)
d12_1 <- diff(d12)
print(data.frame(변환 = c("원자료", "로그 후 전월", "로그 후 전년 동월", "로그 후 계절과 일반 차분"),
  관측수 = c(length(x), length(d1), length(d12), length(d12_1))))
cat("결합 차분의 시작·끝:\n")
print(start(d12_1)); print(end(d12_1))
cat("결합 차분의 처음 여섯 값:\n")
print(round(head(d12_1, 6), 6))
par(mfrow = c(2, 2), mar = c(4.2, 4.5, 3, 1))
plot(x, main = "원자료의 log10", xlab = "연도", ylab = "log10(천 명)", col = "#2563EB")
plot(d1, main = "일반 1차 차분: 전월과 비교", xlab = "연도", ylab = "로그 차이", col = "#D97706")
abline(h = 0, col = "#9CA3AF")
plot(d12, main = "계절 차분: 전년 같은 달과 비교", xlab = "연도", ylab = "로그 차이", col = "#059669")
abline(h = 0, col = "#9CA3AF")
plot(d12_1, main = "계절 차분 뒤 일반 1차 차분", xlab = "연도", ylab = "로그 차이", col = "#7C3AED")
abline(h = 0, col = "#9CA3AF")
