• RMD HTML code copy

    2021. 9. 15.

    by. 데이터와 미래

     

     

     

    R Markdown

    This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see http://rmarkdown.rstudio.com.

    When you click the Knit button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:

    #Part2-1: R vs Rstudio. Programming knowledge check.
    #Part2-2: Time Series Code Intro(Candy Production)
    #Part2-3: Supplement by Basic Curriculum
    #Part2-4: Time Series Theory
    #Part4: HW
    
    library(ggplot2)
    library(ggthemes)
    library(forecast)
    ## Registered S3 method overwritten by 'quantmod':
    ##   method            from
    ##   as.zoo.data.frame zoo
    library(tseries)
    library(readxl)
    
    US_candy_production <- read_excel("Candy Production/candy2.xlsx")
    
    # Take a look at the class of the dataset US_candy_production
    
    class(c("a","b","c"))
    ## [1] "character"
    class(c(1,2,3))
    ## [1] "numeric"
    str(US_candy_production)
    ## tibble [548 x 2] (S3: tbl_df/tbl/data.frame)
    ##  $ observation_date: POSIXct[1:548], format: "1972-01-01" "1972-02-01" ...
    ##  $ IPG3113N        : num [1:548] 85.7 71.8 66 64.6 65 ...
    head(US_candy_production)
    ## # A tibble: 6 x 2
    ##   observation_date    IPG3113N
    ##   <dttm>                 <dbl>
    ## 1 1972-01-01 00:00:00     85.7
    ## 2 1972-02-01 00:00:00     71.8
    ## 3 1972-03-01 00:00:00     66.0
    ## 4 1972-04-01 00:00:00     64.6
    ## 5 1972-05-01 00:00:00     65.0
    ## 6 1972-06-01 00:00:00     67.6
    dim(US_candy_production)
    ## [1] 548   2
    head(iris)
    ##   Sepal.Length Sepal.Width Petal.Length Petal.Width Species
    ## 1          5.1         3.5          1.4         0.2  setosa
    ## 2          4.9         3.0          1.4         0.2  setosa
    ## 3          4.7         3.2          1.3         0.2  setosa
    ## 4          4.6         3.1          1.5         0.2  setosa
    ## 5          5.0         3.6          1.4         0.2  setosa
    ## 6          5.4         3.9          1.7         0.4  setosa
    iris$Sepal.Width
    ##   [1] 3.5 3.0 3.2 3.1 3.6 3.9 3.4 3.4 2.9 3.1 3.7 3.4 3.0 3.0 4.0 4.4 3.9 3.5
    ##  [19] 3.8 3.8 3.4 3.7 3.6 3.3 3.4 3.0 3.4 3.5 3.4 3.2 3.1 3.4 4.1 4.2 3.1 3.2
    ##  [37] 3.5 3.6 3.0 3.4 3.5 2.3 3.2 3.5 3.8 3.0 3.8 3.2 3.7 3.3 3.2 3.2 3.1 2.3
    ##  [55] 2.8 2.8 3.3 2.4 2.9 2.7 2.0 3.0 2.2 2.9 2.9 3.1 3.0 2.7 2.2 2.5 3.2 2.8
    ##  [73] 2.5 2.8 2.9 3.0 2.8 3.0 2.9 2.6 2.4 2.4 2.7 2.7 3.0 3.4 3.1 2.3 3.0 2.5
    ##  [91] 2.6 3.0 2.6 2.3 2.7 3.0 2.9 2.9 2.5 2.8 3.3 2.7 3.0 2.9 3.0 3.0 2.5 2.9
    ## [109] 2.5 3.6 3.2 2.7 3.0 2.5 2.8 3.2 3.0 3.8 2.6 2.2 3.2 2.8 2.8 2.7 3.3 3.2
    ## [127] 2.8 3.0 2.8 3.0 2.8 3.8 2.8 2.8 2.6 3.0 3.4 3.1 3.0 3.1 3.1 3.1 2.7 3.2
    ## [145] 3.3 3.0 2.5 3.0 3.4 3.0
    iris[,"Sepal.Width"]
    ##   [1] 3.5 3.0 3.2 3.1 3.6 3.9 3.4 3.4 2.9 3.1 3.7 3.4 3.0 3.0 4.0 4.4 3.9 3.5
    ##  [19] 3.8 3.8 3.4 3.7 3.6 3.3 3.4 3.0 3.4 3.5 3.4 3.2 3.1 3.4 4.1 4.2 3.1 3.2
    ##  [37] 3.5 3.6 3.0 3.4 3.5 2.3 3.2 3.5 3.8 3.0 3.8 3.2 3.7 3.3 3.2 3.2 3.1 2.3
    ##  [55] 2.8 2.8 3.3 2.4 2.9 2.7 2.0 3.0 2.2 2.9 2.9 3.1 3.0 2.7 2.2 2.5 3.2 2.8
    ##  [73] 2.5 2.8 2.9 3.0 2.8 3.0 2.9 2.6 2.4 2.4 2.7 2.7 3.0 3.4 3.1 2.3 3.0 2.5
    ##  [91] 2.6 3.0 2.6 2.3 2.7 3.0 2.9 2.9 2.5 2.8 3.3 2.7 3.0 2.9 3.0 3.0 2.5 2.9
    ## [109] 2.5 3.6 3.2 2.7 3.0 2.5 2.8 3.2 3.0 3.8 2.6 2.2 3.2 2.8 2.8 2.7 3.3 3.2
    ## [127] 2.8 3.0 2.8 3.0 2.8 3.8 2.8 2.8 2.6 3.0 3.4 3.1 3.0 3.1 3.1 3.1 2.7 3.2
    ## [145] 3.3 3.0 2.5 3.0 3.4 3.0
    # Assign more meaningful variable names
    
    colnames(iris)
    ## [1] "Sepal.Length" "Sepal.Width"  "Petal.Length" "Petal.Width"  "Species"
    mtcars
    ##                      mpg cyl  disp  hp drat    wt  qsec vs am gear carb
    ## Mazda RX4           21.0   6 160.0 110 3.90 2.620 16.46  0  1    4    4
    ## Mazda RX4 Wag       21.0   6 160.0 110 3.90 2.875 17.02  0  1    4    4
    ## Datsun 710          22.8   4 108.0  93 3.85 2.320 18.61  1  1    4    1
    ## Hornet 4 Drive      21.4   6 258.0 110 3.08 3.215 19.44  1  0    3    1
    ## Hornet Sportabout   18.7   8 360.0 175 3.15 3.440 17.02  0  0    3    2
    ## Valiant             18.1   6 225.0 105 2.76 3.460 20.22  1  0    3    1
    ## Duster 360          14.3   8 360.0 245 3.21 3.570 15.84  0  0    3    4
    ## Merc 240D           24.4   4 146.7  62 3.69 3.190 20.00  1  0    4    2
    ## Merc 230            22.8   4 140.8  95 3.92 3.150 22.90  1  0    4    2
    ## Merc 280            19.2   6 167.6 123 3.92 3.440 18.30  1  0    4    4
    ## Merc 280C           17.8   6 167.6 123 3.92 3.440 18.90  1  0    4    4
    ## Merc 450SE          16.4   8 275.8 180 3.07 4.070 17.40  0  0    3    3
    ## Merc 450SL          17.3   8 275.8 180 3.07 3.730 17.60  0  0    3    3
    ## Merc 450SLC         15.2   8 275.8 180 3.07 3.780 18.00  0  0    3    3
    ## Cadillac Fleetwood  10.4   8 472.0 205 2.93 5.250 17.98  0  0    3    4
    ## Lincoln Continental 10.4   8 460.0 215 3.00 5.424 17.82  0  0    3    4
    ## Chrysler Imperial   14.7   8 440.0 230 3.23 5.345 17.42  0  0    3    4
    ## Fiat 128            32.4   4  78.7  66 4.08 2.200 19.47  1  1    4    1
    ## Honda Civic         30.4   4  75.7  52 4.93 1.615 18.52  1  1    4    2
    ## Toyota Corolla      33.9   4  71.1  65 4.22 1.835 19.90  1  1    4    1
    ## Toyota Corona       21.5   4 120.1  97 3.70 2.465 20.01  1  0    3    1
    ## Dodge Challenger    15.5   8 318.0 150 2.76 3.520 16.87  0  0    3    2
    ## AMC Javelin         15.2   8 304.0 150 3.15 3.435 17.30  0  0    3    2
    ## Camaro Z28          13.3   8 350.0 245 3.73 3.840 15.41  0  0    3    4
    ## Pontiac Firebird    19.2   8 400.0 175 3.08 3.845 17.05  0  0    3    2
    ## Fiat X1-9           27.3   4  79.0  66 4.08 1.935 18.90  1  1    4    1
    ## Porsche 914-2       26.0   4 120.3  91 4.43 2.140 16.70  0  1    5    2
    ## Lotus Europa        30.4   4  95.1 113 3.77 1.513 16.90  1  1    5    2
    ## Ford Pantera L      15.8   8 351.0 264 4.22 3.170 14.50  0  1    5    4
    ## Ferrari Dino        19.7   6 145.0 175 3.62 2.770 15.50  0  1    5    6
    ## Maserati Bora       15.0   8 301.0 335 3.54 3.570 14.60  0  1    5    8
    ## Volvo 142E          21.4   4 121.0 109 4.11 2.780 18.60  1  1    4    2
    colnames(US_candy_production)<-c("Period","candy_production")
    
    # Convert data into time series dataset
    attach(US_candy_production)
    
    candyts<-ts(candy_production,c(1972,1),c(2017,8),12)
    str(candyts)
    ##  Time-Series [1:548] from 1972 to 2018: 85.7 71.8 66 64.6 65 ...
    class(candyts)
    ## [1] "ts"
    sum(is.na(iris))
    ## [1] 0
    c(NA,1,2,3,4)
    ## [1] NA  1  2  3  4
    # Take a peek at the dataset
    candyts
    ##           Jan      Feb      Mar      Apr      May      Jun      Jul      Aug
    ## 1972  85.6945  71.8200  66.0229  64.5645  65.0100  67.6467  69.0429  70.8370
    ## 1973  91.2997  77.2700  69.6110  70.2986  71.6822  74.8635  72.0464  73.1748
    ## 1974  88.6985  83.6098  77.2300  67.3209  74.6196  79.5858  66.0568  71.1864
    ## 1975  67.0117  52.6964  50.6689  59.7613  60.8277  63.3629  62.3089  66.9021
    ## 1976  87.9578  75.1878  62.0101  64.4758  70.5454  68.2086  69.3122  71.5922
    ## 1977  97.3515  90.0083  77.2871  76.0459  77.9316  78.3077  75.8701  78.1822
    ## 1978  90.1141  80.4678  76.4640  77.4211  76.7081  78.1769  72.4653  75.9054
    ## 1979  98.6382  84.7727  81.0653  77.1607  78.3780  81.0958  74.7939  77.1113
    ## 1980  86.9268  84.4365  74.4834  65.5610  74.3631  76.9925  71.0376  77.2616
    ## 1981  96.3481  90.4918  78.0943  78.0284  83.3531  83.0404  79.2798  81.7679
    ## 1982  95.9863  92.9899  83.0765  73.5603  76.4383  78.5492  76.3145  77.7653
    ## 1983  95.1877  87.1973  77.9717  73.7339  75.5696  74.7701  76.3340  79.5580
    ## 1984  93.8437  86.3220  78.9029  75.6699  77.8830  77.6690  76.9080  81.2320
    ## 1985  97.6849  87.1184  79.1429  76.2069  77.3304  75.8357  75.1953  79.9166
    ## 1986  97.3994  93.6471  78.8262  73.6548  76.5236  76.7767  73.4034  79.5478
    ## 1987  97.1736  94.2793  83.6225  77.3408  78.0336  79.1708  76.1298  83.5260
    ## 1988  95.8055  95.3010  89.8740  80.8266  82.4593  86.7724  90.7579  98.0626
    ## 1989 102.9508 102.3499  93.4219  88.7382  87.9183  90.5658  89.7340  96.5697
    ## 1990  99.9894 101.2116  94.8477  88.4239  88.6775  92.7610  96.9885 102.3169
    ## 1991 107.4831 111.4080 104.8112  96.0485  94.9222 102.6901 100.1583 109.7879
    ## 1992 104.3101 102.7870  94.9205  92.0467  89.7304  92.8576  92.1938  96.2302
    ## 1993 105.0701 102.2842  94.8146  89.6044  88.4397  94.8144  95.7128 103.4214
    ## 1994 105.3330 100.7475  98.4825  89.3258  87.0124  93.7943  96.4548 102.9823
    ## 1995 107.7064  99.2227  96.1946  94.2656  93.5966  98.2886  98.2274 102.2729
    ## 1996 104.1852 105.5477 102.9177  94.8080  97.0557 100.4093  98.2829 108.0978
    ## 1997 110.6349 109.6545 106.5499  98.1605  97.3783 101.5750  98.3122 109.9673
    ## 1998 119.7766 117.1886 110.8164 106.1647 107.1149 110.8432 109.0117 117.6771
    ## 1999 117.3789 114.6903 107.1010 106.2725 107.8371 108.3356 107.8132 112.5035
    ## 2000 123.1325 119.7423 113.9508 115.9481 108.7202 114.2071 111.8737 117.9027
    ## 2001 122.5767 121.8879 118.5969 114.6967 112.9349 115.3333 113.4896 119.6772
    ## 2002 117.5658 114.5385 110.3068 104.6927 101.8499 112.2162 110.4021 117.5309
    ## 2003 113.0303 111.1786 111.2168 105.1536 107.6101 111.6628 102.8517 113.1477
    ## 2004 116.1890 117.6700 103.9096 102.2036 109.8036 106.2960 106.1117 116.2320
    ## 2005 124.5687 123.9260 107.2575 106.8015 111.4551 107.1940 110.2132 114.8196
    ## 2006 118.2816 117.8165 108.4194 107.5783 101.9894 101.9425 101.7114 112.0216
    ## 2007 121.7363 116.4986 112.6224  99.4400  98.0703  94.9320  91.3872 100.7496
    ## 2008 108.7465 101.7820  97.2060  91.8637  88.9254  89.0084  85.1186  88.5622
    ## 2009  89.9004  88.9836  85.5603  79.7102  80.2515  79.5651  82.3126  89.0494
    ## 2010 100.3797  99.0155  91.9654  89.4914  89.9713  89.5047  96.4638 106.7689
    ## 2011 103.0635 102.5548  98.9834  97.5274  91.3629  89.6899  89.6268  91.8899
    ## 2012  99.9662  99.0417  94.1484  87.6950  85.3510  86.5815  89.5217  98.2967
    ## 2013 107.0733 102.0263 102.6319  95.3206  91.7584  91.8125  92.4299 100.3593
    ## 2014 104.5665 103.9509 101.0708  93.0044  88.4073  89.3661  88.0949  98.0799
    ## 2015 109.9525 108.9073 106.5261 101.0631  96.7802 100.8339 102.8290 115.9030
    ## 2016 108.5041 108.1308 107.9417 103.6179 102.0816 102.4044 102.9512 104.6977
    ## 2017 109.4666 113.4661 105.2245 107.4288 101.9209 104.2022 102.5861 114.0613
    ##           Sep      Oct      Nov      Dec
    ## 1972  75.0462 106.9289 105.5962 105.9673
    ## 1973  80.5915 102.9200 109.2524 105.2210
    ## 1974  70.1750  99.2212 101.1201  86.8930
    ## 1975  66.3200  96.3411 105.6285 102.1819
    ## 1976  76.9073 107.9049 111.6584 113.9655
    ## 1977  84.2727 109.2254 106.1656 113.0575
    ## 1978  82.7320 105.0435 111.6915 114.0821
    ## 1979  80.8078 101.0970 106.7263 105.6220
    ## 1980  77.9510 100.8283 106.7109 107.0469
    ## 1981  83.2954 118.4981 116.9605 113.2558
    ## 1982  81.3017 114.1349 114.9389 115.1824
    ## 1983  82.8953 110.4480 106.5100 103.9983
    ## 1984  85.8844 112.1683 115.5118 112.8158
    ## 1985  89.5288 112.2728 113.6916 117.1114
    ## 1986  88.4485 115.9014 119.4066 115.4294
    ## 1987  90.7704 121.6259 124.8565 122.6595
    ## 1988 102.5171 125.7369 123.4990 122.4540
    ## 1989 101.0261 120.0367 123.3104 125.9960
    ## 1990 108.6388 124.4571 133.2020 134.4426
    ## 1991 111.1361 124.0982 129.3138 124.9696
    ## 1992 104.1677 118.9880 122.1755 121.4803
    ## 1993 108.6304 124.8315 124.8048 122.7720
    ## 1994 109.7563 123.0683 123.1853 124.9834
    ## 1995 107.2035 120.4112 123.8626 128.9061
    ## 1996 114.7798 126.2366 133.8463 136.1510
    ## 1997 115.0362 129.5071 135.1607 136.0268
    ## 1998 120.9282 132.6661 136.9855 135.9605
    ## 1999 116.6453 131.6485 131.7630 134.5654
    ## 2000 125.6499 136.8146 135.6331 138.7040
    ## 2001 123.5141 125.5298 128.7324 129.6597
    ## 2002 119.4877 124.0385 128.5738 124.1789
    ## 2003 116.8135 125.1860 132.7907 128.8211
    ## 2004 120.7290 132.3043 133.1452 129.9987
    ## 2005 119.7252 137.1695 136.3902 139.9153
    ## 2006 118.6654 129.6397 130.3710 132.0261
    ## 2007 110.1524 115.9774 118.4564 120.7117
    ## 2008 103.2736 114.0601 115.8743 101.7672
    ## 2009 101.1519 123.6728 117.0719 116.5435
    ## 2010 115.8542 126.2773 117.7195 118.7519
    ## 2011  93.9062 116.7634 116.8258 114.9563
    ## 2012 112.2694 114.9091 116.0791 116.1401
    ## 2013 105.5167 117.3458 121.6179 123.2412
    ## 2014 106.8675 119.7665 129.0619 128.5528
    ## 2015 115.8964 126.7440 124.5176 120.2374
    ## 2016 109.3191 119.0502 116.8431 116.4535
    ## 2017
    # Check for missing values
    sum(is.na(candyts))
    ## [1] 0
    # Check the frequency of the time series data
    frequency(candyts)
    ## [1] 12
    # Check the cycle of the time series
    cycle(candyts)
    ##      Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec
    ## 1972   1   2   3   4   5   6   7   8   9  10  11  12
    ## 1973   1   2   3   4   5   6   7   8   9  10  11  12
    ## 1974   1   2   3   4   5   6   7   8   9  10  11  12
    ## 1975   1   2   3   4   5   6   7   8   9  10  11  12
    ## 1976   1   2   3   4   5   6   7   8   9  10  11  12
    ## 1977   1   2   3   4   5   6   7   8   9  10  11  12
    ## 1978   1   2   3   4   5   6   7   8   9  10  11  12
    ## 1979   1   2   3   4   5   6   7   8   9  10  11  12
    ## 1980   1   2   3   4   5   6   7   8   9  10  11  12
    ## 1981   1   2   3   4   5   6   7   8   9  10  11  12
    ## 1982   1   2   3   4   5   6   7   8   9  10  11  12
    ## 1983   1   2   3   4   5   6   7   8   9  10  11  12
    ## 1984   1   2   3   4   5   6   7   8   9  10  11  12
    ## 1985   1   2   3   4   5   6   7   8   9  10  11  12
    ## 1986   1   2   3   4   5   6   7   8   9  10  11  12
    ## 1987   1   2   3   4   5   6   7   8   9  10  11  12
    ## 1988   1   2   3   4   5   6   7   8   9  10  11  12
    ## 1989   1   2   3   4   5   6   7   8   9  10  11  12
    ## 1990   1   2   3   4   5   6   7   8   9  10  11  12
    ## 1991   1   2   3   4   5   6   7   8   9  10  11  12
    ## 1992   1   2   3   4   5   6   7   8   9  10  11  12
    ## 1993   1   2   3   4   5   6   7   8   9  10  11  12
    ## 1994   1   2   3   4   5   6   7   8   9  10  11  12
    ## 1995   1   2   3   4   5   6   7   8   9  10  11  12
    ## 1996   1   2   3   4   5   6   7   8   9  10  11  12
    ## 1997   1   2   3   4   5   6   7   8   9  10  11  12
    ## 1998   1   2   3   4   5   6   7   8   9  10  11  12
    ## 1999   1   2   3   4   5   6   7   8   9  10  11  12
    ## 2000   1   2   3   4   5   6   7   8   9  10  11  12
    ## 2001   1   2   3   4   5   6   7   8   9  10  11  12
    ## 2002   1   2   3   4   5   6   7   8   9  10  11  12
    ## 2003   1   2   3   4   5   6   7   8   9  10  11  12
    ## 2004   1   2   3   4   5   6   7   8   9  10  11  12
    ## 2005   1   2   3   4   5   6   7   8   9  10  11  12
    ## 2006   1   2   3   4   5   6   7   8   9  10  11  12
    ## 2007   1   2   3   4   5   6   7   8   9  10  11  12
    ## 2008   1   2   3   4   5   6   7   8   9  10  11  12
    ## 2009   1   2   3   4   5   6   7   8   9  10  11  12
    ## 2010   1   2   3   4   5   6   7   8   9  10  11  12
    ## 2011   1   2   3   4   5   6   7   8   9  10  11  12
    ## 2012   1   2   3   4   5   6   7   8   9  10  11  12
    ## 2013   1   2   3   4   5   6   7   8   9  10  11  12
    ## 2014   1   2   3   4   5   6   7   8   9  10  11  12
    ## 2015   1   2   3   4   5   6   7   8   9  10  11  12
    ## 2016   1   2   3   4   5   6   7   8   9  10  11  12
    ## 2017   1   2   3   4   5   6   7   8
    # Review the summary statistics
    summary(candyts)
    ##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
    ##   50.67   87.86  102.28  100.66  114.69  139.92
    # Plot the raw data using the base plot function
    plot(candyts,xlab="Year", ylab = "Candy Production as (%) of 2012 Production",main="Monthly US Candy Production from 1972 to 2017")
    autoplot(candyts) + labs(x ="Year", y = "Candy Production as (%) of 2012 Production", title="Monthly US Candy Production from 1972 to 2017") + theme_classic()
    boxplot(candyts~cycle(candyts),xlab="Month", ylab = "Candy Production as (%) of 2012 Production" ,main ="Monthly US Candy Production from 1972 to 2017")
    decompose_candyts <- decompose(candyts,"multiplicative")
    autoplot(decompose_candyts) + theme_classic() 
    adf.test(candyts)
    ## 
    ##  Augmented Dickey-Fuller Test
    ## 
    ## data:  candyts
    ## Dickey-Fuller = -3.8511, Lag order = 8, p-value = 0.01644
    ## alternative hypothesis: stationary
    autoplot(acf(candyts,plot=FALSE))+ labs(title="Correlogram of Monthly US Candy Production from 1972 to 2017") + theme_classic()
    # Review random time series for any missing values
    decompose_candyts$random 
    ##            Jan       Feb       Mar       Apr       May       Jun       Jul
    ## 1972        NA        NA        NA        NA        NA        NA 0.9787572
    ## 1973 1.0761795 0.9473530 0.9132216 0.9656140 0.9793905 1.0007720 0.9804063
    ## 1974 1.0234654 1.0101506 1.0085930 0.9277814 1.0288494 1.0897889 0.9384914
    ## 1975 0.9230786 0.7606563 0.7895305 0.9796870 0.9907331 0.9993895 0.9778035
    ## 1976 1.1017564 0.9761060 0.8578393 0.9237903 0.9960446 0.9350020 0.9551832
    ## 1977 1.0802857 1.0352078 0.9486717 0.9741162 0.9947351 0.9823271 0.9709299
    ## 1978 1.0067991 0.9401793 0.9615251 1.0229469 1.0073131 1.0027358 0.9402659
    ## 1979 1.0815598 0.9678241 0.9945811 0.9947011 1.0090818 1.0295511 0.9743203
    ## 1980 1.0072378 1.0222154 0.9700077 0.8960477 1.0109169 1.0248370 0.9557686
    ## 1981 1.0663504 1.0382545 0.9581211 0.9923900 1.0408064 1.0083888 0.9757711
    ## 1982 1.0255183 1.0394728 1.0003954 0.9309010 0.9648606 0.9715567 0.9587531
    ## 1983 1.0438402 0.9964194 0.9556628 0.9478825 0.9716754 0.9508104 0.9923925
    ## 1984 1.0425804 0.9991186 0.9789151 0.9815199 0.9996099 0.9686329 0.9689979
    ## 1985 1.0583884 0.9858120 0.9610471 0.9679764 0.9776360 0.9383305 0.9437711
    ## 1986 1.0488043 1.0527404 0.9525917 0.9315782 0.9584188 0.9404565 0.9145469
    ## 1987 1.0279020 1.0369495 0.9852540 0.9513826 0.9498732 0.9389653 0.9151874
    ## 1988 0.9654684 0.9891577 0.9910146 0.9276815 0.9401442 0.9700056 1.0280570
    ## 1989 0.9779015 1.0149707 0.9964639 0.9947011 0.9824710 0.9902880 0.9968993
    ## 1990 0.9515141 0.9991962 1.0005037 0.9726390 0.9645352 0.9814228 1.0362785
    ## 1991 0.9508761 1.0237819 1.0308271 0.9890375 0.9736316 1.0372871 1.0330955
    ## 1992 0.9567976 0.9916921 0.9918939 1.0127637 0.9867154 1.0048243 1.0149749
    ## 1993 0.9951466 1.0059780 0.9970875 0.9832959 0.9619261 1.0088886 1.0343564
    ## 1994 0.9791843 0.9766466 1.0253259 0.9747276 0.9455486 0.9984593 1.0415612
    ## 1995 0.9891501 0.9499608 0.9906527 1.0193171 1.0073178 1.0346058 1.0505964
    ## 1996 0.9454690 0.9966754 1.0386312 0.9973990 1.0093170 1.0164543 1.0058724
    ## 1997 0.9624377 0.9941577 1.0369470 0.9996864 0.9846017 1.0058823 0.9860929
    ## 1998 0.9974208 1.0111229 1.0222381 1.0229087 1.0246063 1.0382498 1.0385843
    ## 1999 0.9684827 0.9891533 0.9956498 1.0371616 1.0489892 1.0350348 1.0451511
    ## 2000 1.0028894 1.0137996 1.0312639 1.0941691 1.0171673 1.0440861 1.0380995
    ## 2001 0.9707562 1.0055856 1.0512438 1.0701746 1.0544859 1.0609635 1.0661304
    ## 2002 0.9797347 0.9973486 1.0341330 1.0304900 0.9975998 1.0791611 1.0828742
    ## 2003 0.9555456 0.9844979 1.0607157 1.0514483 1.0680173 1.0824013 1.0103299
    ## 2004 0.9843716 1.0373285 0.9815785 1.0076299 1.0737324 1.0179147 1.0291336
    ## 2005 1.0223622 1.0597498 0.9861794 1.0275656 1.0634276 0.9975512 1.0409970
    ## 2006 0.9788623 1.0209626 1.0107090 1.0540690 0.9986648 0.9829915 0.9982987
    ## 2007 1.0425262 1.0488142 1.0972638 1.0236323 1.0137360 0.9700581 0.9579204
    ## 2008 1.0301257 1.0131347 1.0476286 1.0411874 1.0042244 0.9937603 0.9812811
    ## 2009 0.9440316 0.9755372 1.0084270 0.9811047 0.9776007 0.9429620 0.9803844
    ## 2010 0.9783712 0.9932854 0.9780225 0.9902678 0.9888176 0.9627460 1.0523867
    ## 2011 0.9532572 0.9978626 1.0501150 1.0981810 1.0275185 0.9902809 1.0085391
    ## 2012 0.9913205 1.0215820 1.0324084 1.0007144 0.9696736 0.9636440 1.0090708
    ## 2013 1.0081698 0.9998851 1.0825794 1.0553930 1.0071326 0.9824054 1.0032351
    ## 2014 0.9850306 1.0240050 1.0699937 1.0301243 0.9699663 0.9558069 0.9534352
    ## 2015 0.9855292 1.0054955 1.0459152 1.0335318 0.9834094 1.0087785 1.0492934
    ## 2016 0.9456591 0.9869491 1.0655132 1.0775657 1.0619599 1.0484242 1.0723096
    ## 2017 0.9744466 1.0498084        NA        NA        NA        NA        NA
    ##            Aug       Sep       Oct       Nov       Dec
    ## 1972 0.9365460 0.9344087 1.1140425 1.0742231 1.0793987
    ## 1973 0.9323009 0.9647906 1.0335452 1.0781336 1.0431200
    ## 1974 0.9755594 0.9389995 1.1378558 1.1534733 1.0170223
    ## 1975 0.9606815 0.8841904 1.0704250 1.1442028 1.1073961
    ## 1976 0.9142150 0.9154073 1.0657359 1.0737560 1.0958971
    ## 1977 0.9459972 0.9695885 1.0563222 1.0088540 1.0840816
    ## 1978 0.9182295 0.9429551 1.0045743 1.0489317 1.0780576
    ## 1979 0.9477358 0.9428646 1.0006080 1.0459912 1.0480651
    ## 1980 0.9676588 0.9193752 0.9920504 1.0210765 1.0254892
    ## 1981 0.9431090 0.9059491 1.0833782 1.0562073 1.0367204
    ## 1982 0.9192367 0.9139413 1.0812466 1.0703303 1.0839762
    ## 1983 0.9712381 0.9574885 1.0711528 1.0129875 0.9949386
    ## 1984 0.9579845 0.9578789 1.0514702 1.0640439 1.0491080
    ## 1985 0.9381556 0.9915886 1.0468959 1.0433569 1.0837119
    ## 1986 0.9295041 0.9754523 1.0705506 1.0812393 1.0521460
    ## 1987 0.9420154 0.9655095 1.0830817 1.0887821 1.0729630
    ## 1988 1.0358205 1.0201054 1.0469999 1.0050068 1.0010947
    ## 1989 1.0080431 0.9976865 0.9962387 1.0054791 1.0347648
    ## 1990 1.0183640 1.0150878 0.9711166 1.0158991 1.0276128
    ## 1991 1.0670233 1.0292693 0.9715222 0.9983479 0.9786283
    ## 1992 0.9936512 1.0179733 0.9786842 0.9889763 0.9913499
    ## 1993 1.0489271 1.0415762 1.0049974 0.9880296 0.9810659
    ## 1994 1.0427765 1.0531515 0.9918265 0.9711002 0.9892188
    ## 1995 1.0249400 1.0113341 0.9523684 0.9611766 1.0065362
    ## 1996 1.0336421 1.0354553 0.9549993 0.9936453 1.0186895
    ## 1997 1.0282986 1.0134091 0.9549606 0.9730800 0.9808064
    ## 1998 1.0534700 1.0265534 0.9480982 0.9617097 0.9631477
    ## 1999 1.0190936 0.9955268 0.9391874 0.9202806 0.9455156
    ## 2000 1.0256826 1.0318530 0.9435663 0.9182707 0.9452409
    ## 2001 1.0591137 1.0398947 0.8943203 0.9079712 0.9268690
    ## 2002 1.0844472 1.0440924 0.9108420 0.9256854 0.8998670
    ## 2003 1.0389558 1.0151862 0.9181335 0.9572822 0.9375534
    ## 2004 1.0519192 1.0302939 0.9466492 0.9340836 0.9188531
    ## 2005 1.0217025 1.0097629 0.9720382 0.9526534 0.9906123
    ## 2006 1.0305555 1.0318451 0.9491454 0.9420558 0.9658726
    ## 2007 1.0013257 1.0481904 0.9364191 0.9461593 0.9781993
    ## 2008 0.9707572 1.0824837 1.0157221 1.0233592 0.9136768
    ## 2009 0.9860127 1.0522149 1.0741510 0.9908983 0.9864714
    ## 2010 1.0898951 1.1142692 1.0150490 0.9263837 0.9417798
    ## 2011 0.9725887 0.9437122 0.9926709 0.9825157 0.9786966
    ## 2012 1.0349744 1.1131917 0.9516530 0.9392814 0.9432138
    ## 2013 1.0220293 1.0165699 0.9520167 0.9717735 0.9953433
    ## 2014 0.9916234 1.0181068 0.9542323 1.0039704 1.0006576
    ## 2015 1.1103347 1.0502633 0.9642597 0.9281548 0.9014666
    ## 2016 1.0204496 1.0071374 0.9217813 0.8877654 0.8916889
    ## 2017        NA
    # Autoplot the random time series which exclude the NA values
    autoplot(acf(na.remove(decompose_candyts$random),plot=FALSE))+ labs(title="Correlogram of Monthly US Candy Production from 1972 to 2016")  + theme_classic()
    #FIT A TIME SERIES MODEL
    #ARIMA Model
    #Use the auto.arima() function from the forecast R package to fit the best model and coefficients, given the default parameters including seasonality as TRUE. Note we have used the ARIMA modeling procedure as referenced
    
    #arima_candyts <- auto.arima(candyts)
    arima_candyts <- auto.arima(candyts) # or arima_candyts <- auto.arima(candyts)
    arima_candyts
    ## Series: candyts 
    ## ARIMA(2,0,2)(0,1,2)[12] with drift 
    ## 
    ## Coefficients:
    ##          ar1     ar2     ma1      ma2     sma1     sma2   drift
    ##       0.0089  0.8273  0.6804  -0.2666  -0.6063  -0.1153  0.0595
    ## s.e.  0.0519  0.0425  0.0676   0.0525   0.0456   0.0432  0.0337
    ## 
    ## sigma^2 estimated as 13.91:  log likelihood=-1467.25
    ## AIC=2950.5   AICc=2950.77   BIC=2984.77
    #auto.arima: Returns best ARIMA model according to either AIC, AICc or BIC value. The function conducts a search over possible model within the order constraints provided.
    
    #CALCULATE FORECASTS
    #Finally we can plot a forecast of the time series using the forecast function, again from the forecast R package, with a 95% confidence interval where h is the forecast horizon periods in months.
    
    forecast_candyts <- forecast(arima_candyts, level = c(95), h = 36)
    autoplot(forecast_candyts) + theme_classic()
    #BG knowledge: AR Model and MA Model: https://m.blog.naver.com/bluefish850/220749045909
    #BG Knowledge: ARIMA: https://www.youtube.com/watch?v=dXND1OEBABI
    #BG Knowledge: how to apply d in ARIMA(p,d,q): https://people.duke.edu/~rnau/411arim.htm
    #BG Knowledge: ACF VS PACF: https://leedakyeong.tistory.com/entry/ARIMA%EB%9E%80-ARIMA-%EB%B6%84%EC%84%9D%EA%B8%B0%EB%B2%95-AR-MA-ACF-PACF-%EC%A0%95%EC%83%81%EC%84%B1%EC%9D%B4%EB%9E%80#BG Knowledge: https://sodayeong.tistory.com/37
    #BG Knowledge: https://www.youtube.com/watch?v=zNLG8tsA_Go

     

    댓글