You are on page 1of 6

HoltWinters

Aliya

10/4/2021
library(lubridate)

##
## Attaching package: 'lubridate'

## The following objects are masked from 'package:base':


##
## date, intersect, setdiff, union

library(TSA)

## Warning: package 'TSA' was built under R version 4.1.1

##
## Attaching package: 'TSA'

## The following objects are masked from 'package:stats':


##
## acf, arima

## The following object is masked from 'package:utils':


##
## tar

library(forecast)

## Registered S3 method overwritten by 'quantmod':


## method from
## as.zoo.data.frame zoo

## Registered S3 methods overwritten by 'forecast':


## method from
## fitted.Arima TSA
## plot.Arima TSA

#Part 1: Descriptive Statistics


#load the data into R environment. I'm on windows and the file is on ,y
downloads folder
pop1<-read.csv("population1.csv")
str(pop1)

## 'data.frame': 61 obs. of 2 variables:


## $ Year : int 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 ...
## $ Population: int 92417 100801 112112 125130 138049 149855 159979 169768
182620 203103 ...

pop1$Year <- as.Date(as.character(pop1$Year), format = "%Y")

periodogram(pop1$Population)#

pop1$pop<-ts(pop1$Population,start=c(1960))

fit <- HoltWinters(pop1$Pop, gamma = FALSE)


plot(fit)
fitted(fit)

## Time Series:
## Start = 3
## End = 61
## Frequency = 1
## xhat level trend
## 3 109185.0 100801.0 8384.000
## 4 122368.3 111584.7 10783.651
## 5 137680.2 124632.4 13047.782
## 6 151332.7 137982.6 13350.124
## 7 162259.9 150121.2 12138.673
## 8 170658.7 160389.9 10268.714
## 9 179467.0 169928.5 9538.524
## 10 194175.4 182051.9 12123.464
## 11 220937.1 201494.5 19442.607
## 12 262638.0 232066.3 30571.718
## 13 317517.8 274792.0 42725.760
## 14 382297.4 328544.7 53752.694
## 15 456263.2 392404.0 63859.261
## 16 538476.5 465440.3 73036.278
## 17 643339.5 554389.9 88949.620
## 18 723412.8 638901.3 84511.463
## 19 827492.4 733196.8 94295.515
## 20 934914.4 834055.6 100858.799
## 21 1030587.9 932321.8 98266.122
## 22 1147728.4 1040025.1 107703.307
## 23 1171601.5 1105813.3 65788.221
## 24 1226263.8 1166038.5 60225.231
## 25 1290085.2 1228061.8 62023.322
## 26 1358478.3 1293270.1 65208.232
## 27 1457832.1 1375551.1 82280.993
## 28 1521345.3 1448448.2 72897.131
## 29 1614214.7 1531331.4 82883.238
## 30 1718173.1 1624752.3 93420.810
## 31 1823896.2 1724324.2 99571.963
## 32 1930913.6 1827618.9 103294.676
## 33 2044448.7 1936033.8 108414.885
## 34 2166707.8 2051370.8 115337.009
## 35 2292583.3 2171977.0 120606.241
## 36 2416130.6 2294053.8 122076.800
## 37 2529862.1 2411958.0 117904.155
## 38 2662947.7 2537452.9 125494.873
## 39 2802237.6 2669845.2 132392.348
## 40 2952627.6 2811236.4 141391.199
## 41 3115992.6 2963614.5 152378.104
## 42 3298006.7 3130810.6 167196.080
## 43 3472934.3 3301872.5 171061.864
## 44 3653563.1 3477717.8 175845.329
## 45 3925112.3 3701415.0 223697.240
## 46 4384043.7 4042729.4 341314.340
## 47 4270159.5 4156444.4 113715.058
## 48 6072750.7 5114597.5 958153.123
## 49 7188468.0 6151532.8 1036935.231
## 50 8063105.8 7107319.3 955786.535
## 51 8779931.0 7943625.2 836305.868
## 52 9239223.6 8591424.4 647799.212
## 53 9407509.8 8999467.1 408042.711
## 54 9379546.1 9189506.6 190039.482
## 55 9271759.9 9230633.2 41126.636
## 56 9218478.1 9224555.6 -6077.579
## 57 9285231.0 9254893.3 30337.705
## 58 9439763.5 9347328.4 92435.081
## 59 9609988.4 9478658.4 131329.994
## 60 9775714.6 9627186.5 148528.108
## 61 9915735.1 9771460.8 144274.295

plot.ts(pop1$pop,main = "Smoothed Timeseries of Population", col = "blue")


#lines(fitted(fit),col = "red")

forecast(fit,9)

## Point Forecast Lo 80 Hi 80 Lo 95 Hi 95
## 62 10018468 9814806 10222130 9706994 10329942
## 63 10141972 9750830 10533114 9543771 10740173
## 64 10265476 9629945 10901006 9293516 11237436
## 65 10388979 9467049 11310910 8979008 11798951
## 66 10512483 9268731 11756235 8610328 12414638
## 67 10635987 9038944 12233030 8193520 13078454
## 68 10759491 8780452 12738530 7732812 13786170
## 69 10882994 8495355 13270634 7231415 14534574
## 70 11006498 8185331 13827665 6691895 15321101

plot(forecast(fit,9))

You might also like