# Namita Kadam
# RUID: 174004689
library("fpp")
## Loading required package: forecast
## Loading required package: zoo
##
## Attaching package: 'zoo'
## The following objects are masked from 'package:base':
##
## as.Date, as.Date.numeric
## Loading required package: timeDate
## This is forecast 7.3
## Loading required package: fma
## Loading required package: tseries
## Loading required package: expsmooth
## Loading required package: lmtest
library(TTR)
require("fpp")
## The dataset is about the Unemployment Rate for United States, Percent, Monthly, Not Seasonally Adjusted.(1947-1966)
## It is taken from FRED Graph Observations - Federal Reserve Economic Data
## url - https://fred.stlouisfed.org
## It was officially provided by the Economic Research Division, Federal Reserve Bank of St. Louis
## It had the Frequency: Monthly_observation_date and the rate of unemployment.
data1 <- read.csv('D:/BF/Course Docs/rate.csv')
unemp <- data1$Rate
#Converting to a time-series Data
unempz <- ts(unemp, start = 1947, frequency = 12)
plot(unempz, xlab = "Monthly Data", ylab = "US Employment Rate", main = "United States Unemployment Time Series")

unempz_stl <- stl(unempz, s.window ="periodic", robust = TRUE)
unempz_stl
## Call:
## stl(x = unempz, s.window = "periodic", robust = TRUE)
##
## Components
## seasonal trend remainder
## Jan 1947 0.74680023 3.589366 -1.361664e-01
## Feb 1947 0.74821100 3.591153 -3.936424e-02
## Mar 1947 0.46503439 3.592940 -5.797472e-02
## Apr 1947 0.03251635 3.594275 4.732087e-01
## May 1947 -0.23688100 3.595610 -5.872854e-02
## Jun 1947 0.43136380 3.598213 -1.295769e-01
## Jul 1947 0.05977384 3.600817 3.394094e-01
## Aug 1947 -0.32266403 3.605927 1.167374e-01
## Sep 1947 -0.56112512 3.611037 5.008850e-02
## Oct 1947 -0.80336421 3.619126 -1.157617e-01
## Nov 1947 -0.36474782 3.627215 -5.624675e-01
## Dec 1947 -0.19491743 3.636771 -6.418532e-01
## Jan 1948 0.74680023 3.646326 -3.931263e-01
## Feb 1948 0.74821100 3.661532 2.902573e-01
## Mar 1948 0.46503439 3.676737 3.582283e-01
## Apr 1948 0.03251635 3.712741 2.547424e-01
## May 1948 -0.23688100 3.748745 -1.118641e-01
## Jun 1948 0.43136380 3.799361 -3.307249e-01
## Jul 1948 0.05977384 3.849977 -9.750885e-03
## Aug 1948 -0.32266403 3.952934 -3.026984e-02
## Sep 1948 -0.56112512 4.055891 -9.476558e-02
## Oct 1948 -0.80336421 4.204413 -5.010492e-01
## Nov 1948 -0.36474782 4.352936 -6.881883e-01
## Dec 1948 -0.19491743 4.537937 -7.430200e-01
## Jan 1949 0.74680023 4.722939 -4.697390e-01
## Feb 1949 0.74821100 4.936918 1.148711e-01
## Mar 1949 0.46503439 5.150897 -1.593144e-02
## Apr 1949 0.03251635 5.354677 1.280696e-02
## May 1949 -0.23688100 5.558456 3.784247e-01
## Jun 1949 0.43136380 5.718521 2.501154e-01
## Jul 1949 0.05977384 5.878585 1.061641e+00
## Aug 1949 -0.32266403 5.986117 6.365466e-01
## Sep 1949 -0.56112512 6.093650 3.674755e-01
## Oct 1949 -0.80336421 6.094640 8.087238e-01
## Nov 1949 -0.36474782 6.095631 -3.088339e-02
## Dec 1949 -0.19491743 6.035479 1.594383e-01
## Jan 1950 0.74680023 5.975327 8.778727e-01
## Feb 1950 0.74821100 5.840384 1.311405e+00
## Mar 1950 0.46503439 5.705440 9.295255e-01
## Apr 1950 0.03251635 5.542436 4.250480e-01
## May 1950 -0.23688100 5.379431 1.574498e-01
## Jun 1950 0.43136380 5.199687 -3.105125e-02
## Jul 1950 0.05977384 5.019944 2.202825e-01
## Aug 1950 -0.32266403 4.798319 -3.756548e-01
## Sep 1950 -0.56112512 4.576694 -1.556885e-02
## Oct 1950 -0.80336421 4.361994 -2.586297e-01
## Nov 1950 -0.36474782 4.147294 1.745399e-02
## Dec 1950 -0.19491743 3.968804 1.261138e-01
## Jan 1951 0.74680023 3.790313 -1.371137e-01
## Feb 1951 0.74821100 3.667219 -2.154298e-01
## Mar 1951 0.46503439 3.544124 -2.091584e-01
## Apr 1951 0.03251635 3.471817 -3.043330e-01
## May 1951 -0.23688100 3.399509 -2.626283e-01
## Jun 1951 0.43136380 3.350468 -3.818320e-01
## Jul 1951 0.05977384 3.301427 -6.120095e-02
## Aug 1951 -0.32266403 3.267227 -4.456255e-02
## Sep 1951 -0.56112512 3.233026 3.280991e-01
## Oct 1951 -0.80336421 3.211914 3.914498e-01
## Nov 1951 -0.36474782 3.190803 3.739450e-01
## Dec 1951 -0.19491743 3.172691 -7.777388e-02
## Jan 1952 0.74680023 3.154580 -2.013800e-01
## Feb 1952 0.74821100 3.135135 -8.334639e-02
## Mar 1952 0.46503439 3.115691 -2.807254e-01
## Apr 1952 0.03251635 3.095316 -1.278325e-01
## May 1952 -0.23688100 3.074941 6.193971e-02
## Jun 1952 0.43136380 3.050379 -2.817427e-01
## Jul 1952 0.05977384 3.025817 2.144096e-01
## Aug 1952 -0.32266403 2.994155 4.285089e-01
## Sep 1952 -0.56112512 2.962494 2.986313e-01
## Oct 1952 -0.80336421 2.925254 2.781100e-01
## Nov 1952 -0.36474782 2.888015 -2.326686e-02
## Dec 1952 -0.19491743 2.840468 -1.455504e-01
## Jan 1953 0.74680023 2.792921 -1.397213e-01
## Feb 1953 0.74821100 2.763206 -3.114170e-01
## Mar 1953 0.46503439 2.733491 -2.985253e-01
## Apr 1953 0.03251635 2.762302 5.181699e-03
## May 1953 -0.23688100 2.791113 -5.423202e-02
## Jun 1953 0.43136380 2.883135 -6.144985e-01
## Jul 1953 0.05977384 2.975156 -3.349303e-01
## Aug 1953 -0.32266403 3.125781 -4.031174e-01
## Sep 1953 -0.56112512 3.276407 -1.152814e-01
## Oct 1953 -0.80336421 3.499612 -1.962482e-01
## Nov 1953 -0.36474782 3.722818 -1.580705e-01
## Dec 1953 -0.19491743 3.953777 4.411406e-01
## Jan 1954 0.74680023 4.184735 7.684644e-01
## Feb 1954 0.74821100 4.404670 1.147119e+00
## Mar 1954 0.46503439 4.624605 1.310361e+00
## Apr 1954 0.03251635 4.820738 1.246746e+00
## May 1954 -0.23688100 5.016871 9.200105e-01
## Jun 1954 0.43136380 5.217246 5.139053e-02
## Jul 1954 0.05977384 5.417621 2.226053e-01
## Aug 1954 -0.32266403 5.418154 3.045104e-01
## Sep 1954 -0.56112512 5.418686 4.424387e-01
## Oct 1954 -0.80336421 5.328633 7.473117e-02
## Nov 1954 -0.36474782 5.238580 2.616811e-02
## Dec 1954 -0.19491743 5.122161 -1.272431e-01
## Jan 1955 0.74680023 5.005741 4.745845e-02
## Feb 1955 0.74821100 4.874889 7.690014e-02
## Mar 1955 0.46503439 4.744036 -9.070789e-03
## Apr 1955 0.03251635 4.631816 2.356675e-01
## May 1955 -0.23688100 4.519596 -8.271485e-02
## Jun 1955 0.43136380 4.430401 -4.617646e-01
## Jul 1955 0.05977384 4.341206 -4.009795e-01
## Aug 1955 -0.32266403 4.270945 -1.482810e-01
## Sep 1955 -0.56112512 4.200684 -1.395593e-01
## Oct 1955 -0.80336421 4.165157 3.820716e-02
## Nov 1955 -0.36474782 4.129630 3.511813e-02
## Dec 1955 -0.19491743 4.133494 -3.857688e-02
## Jan 1956 0.74680023 4.137359 -1.841592e-01
## Feb 1956 0.74821100 4.143411 -9.162188e-02
## Mar 1956 0.46503439 4.149463 8.550280e-02
## Apr 1956 0.03251635 4.147217 -7.973363e-02
## May 1956 -0.23688100 4.144972 2.919093e-01
## Jun 1956 0.43136380 4.145577 1.230588e-01
## Jul 1956 0.05977384 4.146183 1.940430e-01
## Aug 1956 -0.32266403 4.138402 -1.157379e-01
## Sep 1956 -0.56112512 4.130621 -1.694955e-01
## Oct 1956 -0.80336421 4.111808 -2.084435e-01
## Nov 1956 -0.36474782 4.092995 1.717531e-01
## Dec 1956 -0.19491743 4.079154 1.157631e-01
## Jan 1957 0.74680023 4.065314 8.788582e-02
## Feb 1957 0.74821100 4.068629 -1.168402e-01
## Mar 1957 0.46503439 4.071944 -2.369788e-01
## Apr 1957 0.03251635 4.108712 -1.412281e-01
## May 1957 -0.23688100 4.145479 -8.598038e-03
## Jun 1957 0.43136380 4.227488 -5.885195e-02
## Jul 1957 0.05977384 4.309497 -2.692711e-01
## Aug 1957 -0.32266403 4.441110 -4.184463e-01
## Sep 1957 -0.56112512 4.572723 -3.115982e-01
## Oct 1957 -0.80336421 4.762391 -3.590265e-01
## Nov 1957 -0.36474782 4.952058 1.268972e-02
## Dec 1957 -0.19491743 5.216491 -2.157374e-02
## Jan 1958 0.74680023 5.480924 5.722755e-01
## Feb 1958 0.74821100 5.577233 1.374556e+00
## Mar 1958 0.46503439 5.673543 1.561423e+00
## Apr 1958 0.03251635 5.785498 1.681985e+00
## May 1958 -0.23688100 5.897454 1.439427e+00
## Jun 1958 0.43136380 6.055143 1.113493e+00
## Jul 1958 0.05977384 6.212832 1.127394e+00
## Aug 1958 -0.32266403 6.357240 6.654242e-01
## Sep 1958 -0.56112512 6.501647 5.947782e-02
## Oct 1958 -0.80336421 6.401240 -9.787588e-02
## Nov 1958 -0.36474782 6.300833 -3.360851e-01
## Dec 1958 -0.19491743 6.152913 4.200454e-02
## Jan 1959 0.74680023 6.004993 2.482069e-01
## Feb 1959 0.74821100 5.876480 3.753088e-01
## Mar 1959 0.46503439 5.747968 1.869981e-01
## Apr 1959 0.03251635 5.660345 -4.928612e-01
## May 1959 -0.23688100 5.572722 -4.358411e-01
## Jun 1959 0.43136380 5.500396 -5.317598e-01
## Jul 1959 0.05977384 5.428070 -2.878437e-01
## Aug 1959 -0.32266403 5.372565 -2.499006e-01
## Sep 1959 -0.56112512 5.317059 -5.593431e-02
## Oct 1959 -0.80336421 5.305048 1.983162e-01
## Nov 1959 -0.36474782 5.293037 3.717112e-01
## Dec 1959 -0.19491743 5.305170 -1.025305e-02
## Jan 1960 0.74680023 5.317304 3.589542e-02
## Feb 1960 0.74821100 5.330433 -3.786444e-01
## Mar 1960 0.46503439 5.343562 2.914032e-01
## Apr 1960 0.03251635 5.373328 -2.058443e-01
## May 1960 -0.23688100 5.403093 -3.662124e-01
## Jun 1960 0.43136380 5.473974 -1.053380e-01
## Jul 1960 0.05977384 5.544855 -1.046288e-01
## Aug 1960 -0.32266403 5.672017 -1.493528e-01
## Sep 1960 -0.56112512 5.799179 -5.380537e-01
## Oct 1960 -0.80336421 5.947676 -1.443115e-01
## Nov 1960 -0.36474782 6.096173 -1.314248e-01
## Dec 1960 -0.19491743 6.217845 3.770729e-01
## Jan 1961 0.74680023 6.339517 6.136833e-01
## Feb 1961 0.74821100 6.431975 9.198136e-01
## Mar 1961 0.46503439 6.524434 7.105314e-01
## Apr 1961 0.03251635 6.582123 3.853604e-01
## May 1961 -0.23688100 6.639812 1.970687e-01
## Jun 1961 0.43136380 6.628545 2.400909e-01
## Jul 1961 0.05977384 6.617278 2.229479e-01
## Aug 1961 -0.32266403 6.511196 1.146797e-02
## Sep 1961 -0.56112512 6.405114 -4.398873e-02
## Oct 1961 -0.80336421 6.283483 1.988120e-02
## Nov 1961 -0.36474782 6.161852 -1.971043e-01
## Dec 1961 -0.19491743 6.051144 -5.622646e-02
## Jan 1962 0.74680023 5.940436 1.276416e-02
## Feb 1962 0.74821100 5.852337 -1.005478e-01
## Mar 1962 0.46503439 5.764238 -2.927247e-02
## Apr 1962 0.03251635 5.704614 -1.371305e-01
## May 1962 -0.23688100 5.644990 -2.081092e-01
## Jun 1962 0.43136380 5.616396 -4.775983e-02
## Jul 1962 0.05977384 5.587802 -1.475758e-01
## Aug 1962 -0.32266403 5.590694 3.196958e-02
## Sep 1962 -0.56112512 5.593587 -1.324619e-01
## Oct 1962 -0.80336421 5.614967 -2.116026e-01
## Nov 1962 -0.36474782 5.636347 2.840115e-02
## Dec 1962 -0.19491743 5.659649 -1.647318e-01
## Jan 1963 0.74680023 5.682952 1.702479e-01
## Feb 1963 0.74821100 5.693210 4.585785e-01
## Mar 1963 0.46503439 5.703469 1.314966e-01
## Apr 1963 0.03251635 5.702236 -1.347525e-01
## May 1963 -0.23688100 5.701003 1.358777e-01
## Jun 1963 0.43136380 5.686477 2.821596e-01
## Jul 1963 0.05977384 5.671950 -3.172389e-02
## Aug 1963 -0.32266403 5.642998 -1.203335e-01
## Sep 1963 -0.56112512 5.614045 -2.529200e-01
## Oct 1963 -0.80336421 5.577650 -7.428566e-02
## Nov 1963 -0.36474782 5.541255 2.234932e-01
## Dec 1963 -0.19491743 5.502027 -7.109638e-03
## Jan 1964 0.74680023 5.462800 1.904003e-01
## Feb 1964 0.74821100 5.423665 2.812405e-02
## Mar 1964 0.46503439 5.384530 5.043520e-02
## Apr 1964 0.03251635 5.338224 -7.074017e-02
## May 1964 -0.23688100 5.291917 -1.550362e-01
## Jun 1964 0.43136380 5.235286 4.333497e-01
## Jul 1964 0.05977384 5.178656 -2.384295e-01
## Aug 1964 -0.32266403 5.122741 -7.681007e-05
## Sep 1964 -0.56112512 5.066826 -5.700879e-03
## Oct 1964 -0.80336421 5.016547 1.868175e-01
## Nov 1964 -0.36474782 4.966267 -1.015197e-01
## Dec 1964 -0.19491743 4.920369 -2.545118e-02
## Jan 1965 0.74680023 4.874470 -1.212700e-01
## Feb 1965 0.74821100 4.826105 1.256837e-01
## Mar 1965 0.46503439 4.777741 -1.427753e-01
## Apr 1965 0.03251635 4.721434 4.605004e-02
## May 1965 -0.23688100 4.665126 -2.824533e-02
## Jun 1965 0.43136380 4.595009 4.736272e-01
## Jul 1965 0.05977384 4.524892 1.533443e-02
## Aug 1965 -0.32266403 4.433224 8.944022e-02
## Sep 1965 -0.56112512 4.341556 1.956923e-02
## Oct 1965 -0.80336421 4.252166 1.511982e-01
## Nov 1965 -0.36474782 4.162776 1.019717e-01
## Dec 1965 -0.19491743 4.101771 -1.068534e-01
## Jan 1966 0.74680023 4.040765 -3.875657e-01
## Feb 1966 0.74821100 4.001323 -5.495345e-01
## Mar 1966 0.46503439 3.961882 -4.269159e-01
## Apr 1966 0.03251635 3.947471 -2.799874e-01
## May 1966 -0.23688100 3.933061 1.038204e-01
## Jun 1966 0.43136380 3.918178 5.504581e-01
## Jul 1966 0.05977384 3.903296 3.693052e-02
## Aug 1966 -0.32266403 3.893169 1.294952e-01
## Sep 1966 -0.56112512 3.883042 -2.191698e-02
## Oct 1966 -0.80336421 3.875426 2.279384e-01
## Nov 1966 -0.36474782 3.867809 -1.030616e-01
## Dec 1966 -0.19491743 3.860421 -1.655031e-01
plot(unempz_stl)

# Interpretation:
# Decomposition methods are based on an analysis of the individual components of a time series.
# The strength of each component is estimated separately and then substituted into a model that explains the behavior of the time series.
# STL is a very versatile and robust method for decomposing time series. STL is an acronym for "Seasonal and Trend decomposition
# The plot above shows an example of STL applied to the unemployment data.
# The four graphs are the original data, seasonal component, trend component and the remainder and this shows the periodic seasonal pattern extracted out from the original data and the trend that moves around between 4 and 5.5 and hence is the driving componenet in this data series . There is a bar at the right hand side of each graph to allow a relative comparison of the magnitudes of each component.
# For this data the change in trend is less than the variation doing to the monthly variation.
# We can see the trend is more flexible, the seasonal component does not change over time, and the robust option has been used. There has been a down-turn at the end of the series, and that the rates in 1966 were unusually low (corresponding to some negative values in the remainder component).
# Often, the decomposition is used to remove the seasonal effect from a time series. It provides a cleaner way to understand the trend. We need to remove the seasonality from the time series.
# The seasonally adjusted time series provide a way to understand the underlying trend without the "noise" of the seasonality.
unempz_sa <- seasadj(unempz_stl)
plot(unempz)
lines(seasadj(unempz_stl), col = "red")

# There is more than one way to do things
unempz_decomp <- decompose(unempz)
plot(unempz)
lines(seasadj(unempz_decomp), col = "red")

# Default period forecast
fore_unempz_stl <- forecast(unempz_stl)
# you can pass the # of period
fore_unempz_stl <- forecast(unempz_stl,h=15)
#Print it out or graph it
fore_unempz_stl
## Point Forecast Lo 80 Hi 80 Lo 95 Hi 95
## Jan 1967 4.410771 4.0145970 4.806944 3.80487540 5.016666
## Feb 1967 4.385445 3.7666258 5.004264 3.43904273 5.331847
## Mar 1967 4.080879 3.2789267 4.882831 2.85439874 5.307359
## Apr 1967 3.631250 2.6656313 4.596868 2.15446385 5.108035
## May 1967 3.348163 2.2318743 4.464452 1.64094672 5.055380
## Jun 1967 4.005457 2.7484737 5.262440 2.08306710 5.927846
## Jul 1967 3.625106 2.2356651 5.014546 1.50013965 5.750072
## Aug 1967 3.235659 1.7208382 4.750480 0.91894044 5.552378
## Sep 1967 2.991591 1.3576129 4.625569 0.49263729 5.490544
## Oct 1967 2.744866 0.9972802 4.492452 0.07216417 5.417568
## Nov 1967 3.179894 1.3236930 5.036095 0.34107959 6.018708
## Dec 1967 3.346854 1.3865579 5.307149 0.34884011 6.344867
## Jan 1968 4.286275 2.2259959 6.346553 1.13535025 7.437199
## Feb 1968 4.285848 2.1293399 6.442356 0.98775345 7.583943
## Mar 1968 4.001202 1.7519014 6.250502 0.56119387 7.441209
plot(fore_unempz_stl)
