# Read the data
ozone = scan("https://raw.githubusercontent.com/FinYang/tsdl/refs/heads/master/data-raw/monthly/ozone.dat", skip=1)
labirths_ts = ts(ozone, frequency = 12, start = c(1955, 1))
# (Convert to a time series) This is monthly data,so the frequency value is 12. Also, data starts at January 1955.
labirths_ts
## Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec
## 1955 2.7 2.0 3.6 5.0 6.5 6.1 5.9 5.0 6.4 7.4 8.2 3.9
## 1956 4.1 4.5 5.5 3.8 4.8 5.6 6.3 5.9 8.7 5.3 5.7 5.7
## 1957 3.0 3.4 4.9 4.5 4.0 5.7 6.3 7.1 8.0 5.2 5.0 4.7
## 1958 3.7 3.1 2.5 4.0 4.1 4.6 4.4 4.2 5.1 4.6 4.4 4.0
## 1959 2.9 2.4 4.7 5.1 4.0 7.5 7.7 6.3 5.3 5.7 4.8 2.7
## 1960 1.7 2.0 3.4 4.0 4.3 5.0 5.5 5.0 5.4 3.8 2.4 2.0
## 1961 2.2 2.5 2.6 3.3 2.9 4.3 4.2 4.2 3.9 3.9 2.5 2.2
## 1962 2.4 1.9 2.1 4.5 3.3 3.4 4.1 5.7 4.8 5.0 2.8 2.9
## 1963 1.7 3.2 2.7 3.0 3.4 3.8 5.0 4.8 4.9 3.5 2.5 2.4
## 1964 1.6 2.3 2.5 3.1 3.5 4.5 5.7 5.0 4.6 4.8 2.1 1.4
## 1965 2.1 2.9 2.7 4.2 3.9 4.1 4.6 5.8 4.4 6.1 3.5 1.9
## 1966 1.8 1.9 3.7 4.4 3.8 5.6 5.7 5.1 5.6 4.8 2.5 1.5
## 1967 1.8 2.5 2.6 1.8 3.7 3.7 4.9 5.1 3.7 5.4 3.0 1.8
## 1968 2.1 2.6 2.8 3.2 3.5 3.5 4.9 4.2 4.7 3.7 3.2 1.8
## 1969 2.0 1.7 2.8 3.2 4.4 3.4 3.9 5.5 3.8 3.2 2.3 2.2
## 1970 1.3 2.3 2.7 3.3 3.7 3.0 3.8 4.7 4.6 2.9 1.7 1.3
## 1971 1.8 2.0 2.2 3.0 2.4 3.5 3.5 3.3 2.7 2.5 1.6 1.2
## 1972 1.5 2.0 3.1 3.0 3.5 3.4 4.0 3.8 3.1 2.1 1.6 1.3
plot(labirths_ts, ylab = "Birth Numbers", main = "Monthly birth numbers over downtown LA")
#(Plot the Tine Series) There is an overall decreasing trend.
labirths_components = stl(labirths_ts, s.window = 12)
labirths_components
## Call:
## stl(x = labirths_ts, s.window = 12)
##
## Components
## seasonal trend remainder
## Jan 1955 -1.678707361 4.511964 -0.133256348
## Feb 1955 -1.678272925 4.633099 -0.954825821
## Mar 1955 -0.772168067 4.754234 -0.382065717
## Apr 1955 -0.282112702 4.875369 0.406743881
## May 1955 -0.182726578 4.985758 1.696969029
## Jun 1955 0.848548804 5.096146 0.155304920
## Jul 1955 1.189853917 5.206535 -0.496388921
## Aug 1955 0.975857607 5.300126 -1.275984098
## Sep 1955 1.596364003 5.393718 -0.590081981
## Oct 1955 0.692842857 5.487309 1.219847678
## Nov 1955 0.132529847 5.499170 2.568299998
## Dec 1955 -0.877883042 5.511031 -0.733147804
## Jan 1956 -1.674268804 5.522892 0.251377268
## Feb 1956 -1.657441688 5.522322 0.635120120
## Mar 1956 -0.775685938 5.521752 0.753934339
## Apr 1956 -0.262055589 5.521182 -1.459126042
## May 1956 -0.182987528 5.479867 -0.496879363
## Jun 1956 0.847948624 5.438552 -0.686500774
## Jul 1956 1.201366363 5.397237 -0.298603773
## Aug 1956 1.001656468 5.366692 -0.468348123
## Sep 1956 1.575195893 5.336146 1.788658207
## Oct 1956 0.697850416 5.305600 -0.703450562
## Nov 1956 0.085030930 5.299443 0.315526347
## Dec 1956 -0.894605159 5.293285 1.301319859
## Jan 1957 -1.669629066 5.287128 -0.617498812
## Feb 1957 -1.636490173 5.279838 -0.243347890
## Mar 1957 -0.779164436 5.272548 0.406616188
## Apr 1957 -0.242079822 5.265258 -0.523178611
## May 1957 -0.183450544 5.237854 -1.054403323
## Jun 1957 0.846996017 5.210449 -0.357445318
## Jul 1957 1.212376021 5.183045 -0.095420756
## Aug 1957 1.026816134 5.128018 0.945165950
## Sep 1957 1.553252181 5.072991 1.373756723
## Oct 1957 0.702030136 5.017964 -0.519994413
## Nov 1957 0.036651936 4.913224 0.050124397
## Dec 1957 -0.912109833 4.808483 0.803626775
## Jan 1958 -1.663222661 4.703742 0.659480213
## Feb 1958 -1.596296875 4.558165 0.138131558
## Mar 1958 -0.785180428 4.412588 -1.127407759
## Apr 1958 -0.216075684 4.267011 -0.050935373
## May 1958 -0.200587337 4.195180 0.105407333
## Jun 1958 0.842590073 4.123349 -0.365939024
## Jul 1958 1.233545836 4.051518 -0.885063734
## Aug 1958 1.079965994 4.084260 -0.964226468
## Sep 1958 1.526720089 4.117003 -0.543723139
## Oct 1958 0.701847906 4.149746 -0.251593532
## Nov 1958 -0.056640225 4.287913 0.168727364
## Dec 1958 -0.930515976 4.426080 0.504435882
## Jan 1959 -1.654169041 4.564247 -0.010078288
## Feb 1959 -1.553859637 4.682119 -0.728259660
## Mar 1959 -0.789355754 4.799991 0.689364488
## Apr 1959 -0.189052946 4.917863 0.371189712
## May 1959 -0.217527595 4.908494 -0.690966559
## Jun 1959 0.837184149 4.899125 1.763690778
## Jul 1959 1.252519159 4.889756 1.557724848
## Aug 1959 1.129613676 4.810205 0.360180984
## Sep 1959 1.495380135 4.730655 -0.926034822
## Oct 1959 0.695867351 4.651104 0.353028615
## Nov 1959 -0.156721174 4.542743 0.413978325
## Dec 1959 -0.956035434 4.434382 -0.778346230
## Jan 1960 -1.648424294 4.326020 -0.977596185
## Feb 1960 -1.480326876 4.214766 -0.734438797
## Mar 1960 -0.824518525 4.103511 0.121007658
## Apr 1960 -0.083441727 3.992256 0.091185667
## May 1960 -0.219746723 3.925274 0.594472248
## Jun 1960 0.859616901 3.858293 0.282090209
## Jul 1960 1.325088760 3.791311 0.383599936
## Aug 1960 1.248260633 3.748518 0.003221586
## Sep 1960 1.353520939 3.705724 0.340754803
## Oct 1960 0.733288260 3.662931 -0.596218995
## Nov 1960 -0.386098095 3.584915 -0.798817325
## Dec 1960 -1.070594099 3.506900 -0.436306007
## Jan 1961 -1.631237331 3.428885 0.402352539
## Feb 1961 -1.395365935 3.367493 0.527873174
## Mar 1961 -0.848267149 3.306101 0.142166420
## Apr 1961 0.032910412 3.244709 0.022380890
## May 1961 -0.211898155 3.234583 -0.122684388
## Jun 1961 0.891117999 3.224456 0.184425613
## Jul 1961 1.405727357 3.214330 -0.420057590
## Aug 1961 1.373731050 3.216472 -0.390202823
## Sep 1961 1.217239664 3.218613 -0.535852977
## Oct 1961 0.774892267 3.220755 -0.095647119
## Nov 1961 -0.612686744 3.236972 -0.124285587
## Dec 1961 -1.183801551 3.253190 0.130611743
## Jan 1962 -1.636790249 3.269407 0.767382962
## Feb 1962 -1.300309121 3.318985 -0.118675594
## Mar 1962 -0.826524874 3.368562 -0.442037271
## Apr 1962 0.054695906 3.418140 1.027164521
## May 1962 -0.131351867 3.464427 -0.033075075
## Jun 1962 0.842563609 3.510714 -0.953277921
## Jul 1962 1.418964772 3.557002 -0.875966453
## Aug 1962 1.444620651 3.571915 0.683464674
## Sep 1962 1.165719174 3.586828 0.047453156
## Oct 1962 0.913471678 3.601741 0.484787657
## Nov 1962 -0.719066361 3.600731 -0.081664299
## Dec 1962 -1.332986360 3.599721 0.633265705
## Jan 1963 -1.655628374 3.598711 -0.243082276
## Feb 1963 -1.217435782 3.573231 0.844204548
## Mar 1963 -0.815864341 3.547752 -0.031887479
## Apr 1963 0.067103537 3.522272 -0.589375942
## May 1963 -0.058479563 3.480952 -0.022472682
## Jun 1963 0.788522796 3.439632 -0.428154881
## Jul 1963 1.428903324 3.398312 0.172784751
## Aug 1963 1.514580316 3.377822 -0.092401832
## Sep 1963 1.115637674 3.357331 0.427031219
## Oct 1963 1.055548044 3.336841 -0.892388741
## Nov 1963 -0.819891058 3.358764 -0.038873253
## Dec 1963 -1.475250655 3.380688 0.494562730
## Jan 1964 -1.636170714 3.402612 -0.166440825
## Feb 1964 -1.152966457 3.425496 0.027470726
## Mar 1964 -0.789932026 3.448380 -0.158447899
## Apr 1964 -0.010227775 3.471264 -0.361036343
## May 1964 0.020326744 3.479540 0.000133570
## Jun 1964 0.698230908 3.487815 0.313953838
## Jul 1964 1.419967002 3.496091 0.783942177
## Aug 1964 1.543885603 3.518666 -0.062552005
## Sep 1964 1.100560345 3.541242 -0.041802327
## Oct 1964 1.099465494 3.563818 0.136716944
## Nov 1964 -0.799591977 3.576457 -0.676864843
## Dec 1964 -1.522333232 3.589096 -0.666762845
## Jan 1965 -1.620191013 3.601735 0.118455678
## Feb 1965 -1.092866137 3.628501 0.364365475
## Mar 1965 -0.769259761 3.655266 -0.186006230
## Apr 1965 -0.093345523 3.682031 0.611314204
## May 1965 0.092820231 3.718315 0.088864314
## Jun 1965 0.601368710 3.754600 -0.255968302
## Jul 1965 1.404202879 3.790884 -0.595086607
## Aug 1965 1.566498979 3.806440 0.427061234
## Sep 1965 1.078926994 3.821996 -0.500922838
## Oct 1965 1.137315153 3.837552 1.125132945
## Nov 1965 -0.784872455 3.874052 0.410820936
## Dec 1965 -1.574283361 3.910551 -0.436267775
## Jan 1966 -1.600770608 3.947051 -0.546280145
## Feb 1966 -1.082581779 3.961574 -0.978991895
## Mar 1966 -0.694353847 3.976097 0.418257252
## Apr 1966 -0.089494573 3.990620 0.498875058
## May 1966 0.179620386 3.959991 -0.339610900
## Jun 1966 0.572300386 3.929362 1.098338102
## Jul 1966 1.360873696 3.898733 0.440393793
## Aug 1966 1.558788316 3.835872 -0.294660328
## Sep 1966 1.046481279 3.773012 0.780507206
## Oct 1966 0.998520017 3.710151 0.091328967
## Nov 1966 -0.782364587 3.618440 -0.336075194
## Dec 1966 -1.567629700 3.526729 -0.459098846
## Jan 1967 -1.573351405 3.435017 -0.061665906
## Feb 1967 -1.064869595 3.391212 0.173657749
## Mar 1967 -0.612591078 3.347406 -0.134815304
## Apr 1967 -0.079655140 3.303601 -1.423945778
## May 1967 0.271540657 3.317370 0.111089207
## Jun 1967 0.547316026 3.331139 -0.178455380
## Jul 1967 1.320592327 3.344909 0.234499100
## Aug 1967 1.553150504 3.372767 0.174082295
## Sep 1967 1.015133455 3.400626 -0.715759284
## Oct 1967 0.860118829 3.428484 1.111396714
## Nov 1967 -0.780166713 3.436878 0.343288655
## Dec 1967 -1.561690488 3.445272 -0.083581170
## Jan 1968 -1.537873696 3.453665 0.184208438
## Feb 1968 -1.039985020 3.434151 0.205834515
## Mar 1968 -0.550622841 3.414636 -0.064012911
## Apr 1968 -0.050541028 3.395121 -0.144579971
## May 1968 0.306522096 3.372321 -0.178843116
## Jun 1968 0.554689960 3.349521 -0.404211000
## Jul 1968 1.300428042 3.326721 0.272850898
## Aug 1968 1.523405885 3.316210 -0.639616321
## Sep 1968 0.977069115 3.305700 0.417231073
## Oct 1968 0.757344761 3.295189 -0.352533949
## Nov 1968 -0.787978133 3.296774 0.691203760
## Dec 1968 -1.537983664 3.298360 0.039624106
## Jan 1969 -1.505211825 3.299945 0.205267080
## Feb 1969 -1.017358958 3.284848 -0.567489096
## Mar 1969 -0.490355792 3.269751 0.020604427
## Apr 1969 -0.022295848 3.254655 -0.032358826
## May 1969 0.341466861 3.232994 0.825539101
## Jun 1969 0.562980446 3.211333 -0.374313848
## Jul 1969 1.282133533 3.189673 -0.571806299
## Aug 1969 1.496378639 3.180958 0.822663419
## Sep 1969 0.942569747 3.172243 -0.314812865
## Oct 1969 0.658673075 3.163528 -0.622201368
## Nov 1969 -0.791149764 3.142645 -0.051495572
## Dec 1969 -1.509548165 3.121762 0.587785786
## Jan 1970 -1.488548993 3.100879 -0.312330429
## Feb 1970 -1.007166193 3.082809 0.224357157
## Mar 1970 -0.465303153 3.064739 0.100564503
## Apr 1970 -0.012143143 3.046668 0.265474878
## May 1970 0.361153241 3.024464 0.314382862
## Jun 1970 0.561193203 3.002260 -0.563452733
## Jul 1970 1.272839414 2.980055 -0.452894578
## Aug 1970 1.489336385 2.950084 0.260579512
## Sep 1970 0.931158342 2.920113 0.748728616
## Oct 1970 0.616499897 2.890142 -0.606641878
## Nov 1970 -0.791104806 2.846536 -0.355430993
## Dec 1970 -1.496829401 2.802930 -0.006100215
## Jan 1971 -1.475568842 2.759323 0.516245409
## Feb 1971 -1.000465544 2.695750 0.304715174
## Mar 1971 -0.443552064 2.632177 0.011374758
## Apr 1971 -0.004845176 2.568604 0.436240934
## May 1971 0.378431692 2.537037 -0.515469188
## Jun 1971 0.557569329 2.505471 0.436959921
## Jul 1971 1.262279963 2.473904 -0.236183967
## Aug 1971 1.481535521 2.489802 -0.671337761
## Sep 1971 0.919495051 2.505700 -0.725195526
## Oct 1971 0.574389306 2.521599 -0.595988016
## Nov 1971 -0.790682790 2.560989 -0.170306457
## Dec 1971 -1.483622785 2.600380 0.083243003
## Jan 1972 -1.464583859 2.639770 0.324813540
## Feb 1972 -0.993791608 2.635028 0.358763692
## Mar 1972 -0.427604286 2.630286 0.897318774
## Apr 1972 0.003257856 2.625543 0.371199036
## May 1972 0.391308418 2.610885 0.497806489
## Jun 1972 0.553974854 2.596227 0.249798069
## Jul 1972 1.252261519 2.581569 0.166169420
## Aug 1972 1.475951557 2.554195 -0.230146580
## Sep 1972 0.911136135 2.526821 -0.337957119
## Oct 1972 0.539306811 2.499447 -0.938753756
## Nov 1972 -0.792292391 2.458984 -0.066691178
## Dec 1972 -1.472615763 2.418520 0.354095571
plot(labirths_components)
trend_component = labirths_components$time.series[, "trend"]
seasonal_component = labirths_components$time.series[, "seasonal"]
remainder_component = labirths_components$time.series[, "remainder"]
#(Decomposition) The overall trend is decreasing. In the seasonal component, it seems that the first 4 peaks are following a simiar pattern with each other.
library(tseries)
## Registered S3 method overwritten by 'quantmod':
## method from
## as.zoo.data.frame zoo
adf_test_result = adf.test(labirths_ts)
## Warning in adf.test(labirths_ts): p-value smaller than printed p-value
adf_test_result
##
## Augmented Dickey-Fuller Test
##
## data: labirths_ts
## Dickey-Fuller = -8.2519, Lag order = 5, p-value = 0.01
## alternative hypothesis: stationary
#(Stationarity Check) Since the p-value is less than 0.05, we reject the null hypothesis and conclude that the time series is stationary.
par(mfrow = c(1, 2))
acf(labirths_ts, lag.max = 48, main = "ACF plot", ylab = "Original birth data")
pacf(labirths_ts, lag.max = 48, main = "PACF plot", ylab = "Original birth data")
#(ACF and PACF plots) Here, you can observe that autocorrelation is initially high at larger lags, then decreases. However, when the same month repeats in the following year, autocorrelation increases again.
library("forecast")
automated_birthmodel = auto.arima(labirths_ts)
automated_birthmodel
## Series: labirths_ts
## ARIMA(3,0,0)(2,1,0)[12] with drift
##
## Coefficients:
## ar1 ar2 ar3 sar1 sar2 drift
## 0.3347 -0.0388 0.1506 -0.6648 -0.3376 -0.0137
## s.e. 0.0699 0.0741 0.0700 0.0734 0.0728 0.0045
##
## sigma^2 = 0.7038: log likelihood = -253.83
## AIC=521.66 AICc=522.23 BIC=544.89
#(Automated ARIMA model) Non-seasonal component - ARIMA(3,0,0).The middle coefficient of 0 indicates that the series has not been differenced before fitting the AR and MA components. Specifically, the model includes 0 AR terms and 3 MA terms.
#Seasonal component - (2,1,0)[12]. To model the seasonal component, the algorithm identified a seasonality of 12, which corresponds to monthly data. The series was differenced once before fitting the model. For the seasonal part, 2 seasonal AR terms and 0 seasonal MA terms were used.
residuals2 = residuals(automated_birthmodel)
plot(residuals2, main = "Residual plot over time")
#(Plot Residuals) When plotting the residuals, there should be no discernible patterns or structures, indicating that the model has adequately captured the underlying data
forecasted_series = forecast(automated_birthmodel, h = 60)
plot(forecasted_series)
#(Forecasting)The forecast model will predict values for the next 5 years, or 60 months.
Courtesy: \(\texttt{tsdl}\) package at https://github.com/FinYang/tsdl/↩︎