#loading the dataset
#loading the “cbe.dat” file 
global <- scan("global.dat")
#see the first few rows of the dataset
head(global)
## [1] -0.384 -0.457 -0.673 -0.344 -0.311 -0.071
plot.ts(global)

#1.Produce a time plot of the data.  Plot the aggregated annual series and a boxplot 
#that summarizes the observed values for each season.Comment on the plots.
#convert the dataset class to timeseries class
global.month.ts <- ts(global,start = c(1856,1),end = c(2005,12),frequency = 12)
global.annual.ts <- aggregate(global.month.ts, FUN=mean)
plot(t(global.month.ts), col = 1:5)
legend("topleft", legend = 1:5, col = 1:5, lty = 1)

boxplot(global.month.ts~cycle(global.month.ts))

#Peak months are May because of the summers, people are more outside 
#and least month is January because of the winter might won't go out.
#From January the level is increasing whereas if you see the level is going down from august
#onwards.

#Decompose the series into the components trend, seasonal effect, and residuals, and plot 
#the decomposed series.  Produce a plot of the trend with a superimposed seasonal effect.
global.decom <- decompose(global.month.ts, type = "mult")
trend <- global.decom$trend
seasonal <- global.decom$seasonal
plot(cbind(trend,trend*seasonal),lty=1:2)

#Plot the correlogram of the residuals from Question 2.  
#Comment on the plot, explaining any ‘significant’ correlations at significant lags.
global.decom <- decompose(global.month.ts, type = "mult")
plot(global.decom$seasonal)

plot(global.decom)

plot(ts(global.decom$random[7:138]))

acf(global.decom$random[7:138])

#from the decomposition we can see that global temprature was higher
#in the month of april and lowest in the month of february.
#The plot above shows the original time series (top), the estimated
#trend component (second from top), the estimated seasonal component 
#(third from top), and the estimated irregular component (bottom). 
#We see that the estimated trend component shows a small global
#temprature before 1900 which increasing after 1950

#Fit an appropriate Holt-Winters model to the monthly data.  Explain why 
#you chose that particular Holt-Winters model, and give the parameter estimates.
#Holt-Winters method generalizes the simple exponential smoothing
#equation and takes into account seasonal effects and trends.
#here I have taken beta = false and gamma = false in order to have
#simple exponential smoothing.
global.hw <- HoltWinters(global.month.ts, seasonal = 'multiplicative')
global.hw
## Holt-Winters exponential smoothing with trend and multiplicative seasonal component.
## 
## Call:
## HoltWinters(x = global.month.ts, seasonal = "multiplicative")
## 
## Smoothing parameters:
##  alpha: 0.7616377
##  beta : 0.5693481
##  gamma: 0.01225672
## 
## Coefficients:
##           [,1]
## a    0.1878636
## b   -0.1255702
## s1   1.9764446
## s2   0.4762221
## s3   0.6218828
## s4   0.9229188
## s5   0.8111828
## s6   0.4177652
## s7   0.5730167
## s8   0.6151843
## s9   0.8981872
## s10  1.2871224
## s11  1.6120281
## s12  1.4695998
plot(global.hw)

plot(fitted(global.hw))
#By fitting the data to the Holt-Winters model, we obtained parameter estimates 
#as above. Since the data exhibits some systematic and seasonal trend, 
#it’s appropriate fit the data using Holt-Winters model.

#Using the fitted model, forecast values for the years 2006-2010.  Add these 
#forecasts to a time plot of the original series.  Under what circumstances would 
#these forecasts be valid?  What comments of caution would you make to an economist 
#or politician who wanted to use these forecasts to make statements about the potential
#impact of global warming on the world economy?
library(forecast)
## Warning: package 'forecast' was built under R version 3.6.2
## Registered S3 method overwritten by 'xts':
##   method     from
##   as.zoo.xts zoo
## Registered S3 method overwritten by 'quantmod':
##   method            from
##   as.zoo.data.frame zoo

global.predict <- predict(global.hw,n.head=4*12,prediction.interval = TRUE)
global.predict
##                fit       upr        lwr
## Jan 2006 0.1231193 0.5924454 -0.3462067
ts.plot(global.month.ts,global.predict,lty=1:2)

#The forecast is based on the trending effect during which the model was fitted. 
#It’s valid if the trend and seasonal effect from 2006 to 2010 are the same as in 
#previous years. This should be keep in mind that it should be used when using the 
#plot to measure the impact of global warming because the two event might exhibit 
#causual relationship with each other.