1 Daily electricity demand for Victoria, Australia, during 2014 is contained in elecdaily.

  daily20 <- head(elecdaily,20)
  daily20
## Time Series:
## Start = c(1, 1) 
## End = c(3, 6) 
## Frequency = 7 
##            Demand WorkDay Temperature
## 1.000000 174.8963       0        26.0
## 1.142857 188.5909       1        23.0
## 1.285714 188.9169       1        22.2
## 1.428571 173.8142       0        20.3
## 1.571429 169.5152       0        26.1
## 1.714286 195.7288       1        19.6
## 1.857143 199.9029       1        20.0
## 2.000000 205.3375       1        27.4
## 2.142857 228.0782       1        32.4
## 2.285714 258.5984       1        34.0
## 2.428571 201.7970       0        22.4
## 2.571429 187.6298       0        22.5
## 2.714286 254.6636       1        30.0
## 2.857143 322.2323       1        42.4
## 3.000000 343.9934       1        41.5
## 3.142857 347.6376       1        43.2
## 3.285714 332.9455       1        43.1
## 3.428571 219.7517       0        23.7
## 3.571429 186.9816       0        22.3
## 3.714286 228.4876       1        24.0

1a. Plot the data and find the regression model for Demand with temperature as an explanatory variable. Why is there a positive relationship?

autoplot(daily20)

model1 <- tslm(Demand ~ Temperature, data = daily20)
summary(model1) 
## 
## Call:
## tslm(formula = Demand ~ Temperature, data = daily20)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -46.060  -7.117  -1.437  17.484  27.102 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept)  39.2117    17.9915   2.179   0.0428 *  
## Temperature   6.7572     0.6114  11.052 1.88e-09 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 22 on 18 degrees of freedom
## Multiple R-squared:  0.8716, Adjusted R-squared:  0.8644 
## F-statistic: 122.1 on 1 and 18 DF,  p-value: 1.876e-09
  The temperature is positive related to the demand. we can assume such relationship by observing the plot. The trend seems similar between this two.

1b. Produce a residual plot. Is the model adequate? Are there any outliers or influential observations?

  The model is adequate. There is outliers but there is no obvious correlation because the distribution of residuals is not normal.

1c. Use the model to forecast the electricity demand that you would expect for the next day if the maximum temperature was 15 and compare it with the forecast if the maximum temperature was 35.Do you believe these forecasts?

fc_model1 <- forecast(model1, newdata=data.frame(Temperature=c(15,35)))
fc_model1
##          Point Forecast    Lo 80    Hi 80     Lo 95    Hi 95
## 3.857143       140.5701 108.6810 172.4591  90.21166 190.9285
## 4.000000       275.7146 245.2278 306.2014 227.57056 323.8586

1d. Give prediction intervals for your forecasts.

fc_model1$lower[,1] 
## Time Series:
## Start = c(3, 7) 
## End = c(4, 1) 
## Frequency = 7 
## [1] 108.6810 245.2278
fc_model1$upper[,1]
## Time Series:
## Start = c(3, 7) 
## End = c(4, 1) 
## Frequency = 7 
## [1] 172.4591 306.2014
 80% interval: lower(108.6810 245.2278) upper(172.4591 306.2014)
fc_model1$lower[,2] 
## Time Series:
## Start = c(3, 7) 
## End = c(4, 1) 
## Frequency = 7 
## [1]  90.21166 227.57056
fc_model1$upper[,2]
## Time Series:
## Start = c(3, 7) 
## End = c(4, 1) 
## Frequency = 7 
## [1] 190.9285 323.8586
 95% interval: lower(90.21166 227.57056) upper(190.9285 323.8586)

1e. Plot Demand vs Temperature for all of the available data in elecdaily. What does this say about your model?

elecdaily %>%
  as.data.frame() %>%
  ggplot(aes(x=Temperature, y=Demand)) +
  ylab("Electricity Demand") +
  xlab("Temperature") +
  geom_point() +
  geom_smooth(method="lm", se=FALSE)

  the result plot shows that the model is based on a small part of the total data. It is not convincing enough.

2 Data set mens400 contains the winning times (in seconds) for the men’s 400 meters final in each Olympic Games from 1896 to 2016.

2a. Plot the winning time against the year. Describe the main features of the plot.

autoplot(mens400)## there are missing values and the trend in general is decreasing.

2b. Fit a regression line to the data. Obviously the winning times have been decreasing, but at what average rate per year?

time_mens400 <- time(mens400)# extract the time 
lm_mens400 <- tslm(mens400 ~ time_mens400, data = mens400)
lm_mens400$coefficients
##  (Intercept) time_mens400 
## 172.48147736  -0.06457385
  The winning times have been decreasing at average rate of 0.06457385 second per year.

2c. Plot the residuals against the year. What does this indicate about the suitability of the fitted line?

cbind(Time = time_mens400, Residuals = lm_mens400$residuals) %>%
  as.data.frame() %>%
  ggplot(aes(x = Time, y = Residuals)) +
  geom_point() +
  ylab("Residuals of Regression Line(Unit:s)")
## Warning: Removed 3 rows containing missing values (geom_point).

  The residual plot shows that the regression model generally fitted the data well.

2d. Predict the winning time for the men’s 400 meters final in the 2020 Olympics.Give a prediction interval for your forecasts. What assumptions have you made in these calculations?

lm2_mens400 <- lm(mens400 ~ time_mens400,data = mens400,na.action = na.exclude)
fc_mens400 <- forecast(lm2_mens400,newdata = data.frame(time_mens400 = 2020))
autoplot(mens400) +
  autolayer(fc_mens400, PI = TRUE)

fc_mens400$upper
##          [,1]     [,2]
## [1,] 43.63487 44.53176
fc_mens400$lower
##          [,1]     [,2]
## [1,] 40.44975 39.55286
 80% interval is from 40.45 to 43.63
 95% interval is from 39.55 to 44.53
 the assumption that the model's residuals were normally distributed.
  1. Type easter(ausbeer) and interpret what you see.
easter(ausbeer)
##      Qtr1 Qtr2 Qtr3 Qtr4
## 1956 0.67 0.33 0.00 0.00
## 1957 0.00 1.00 0.00 0.00
## 1958 0.00 1.00 0.00 0.00
## 1959 1.00 0.00 0.00 0.00
## 1960 0.00 1.00 0.00 0.00
## 1961 0.33 0.67 0.00 0.00
## 1962 0.00 1.00 0.00 0.00
## 1963 0.00 1.00 0.00 0.00
## 1964 1.00 0.00 0.00 0.00
## 1965 0.00 1.00 0.00 0.00
## 1966 0.00 1.00 0.00 0.00
## 1967 1.00 0.00 0.00 0.00
## 1968 0.00 1.00 0.00 0.00
## 1969 0.00 1.00 0.00 0.00
## 1970 1.00 0.00 0.00 0.00
## 1971 0.00 1.00 0.00 0.00
## 1972 0.33 0.67 0.00 0.00
## 1973 0.00 1.00 0.00 0.00
## 1974 0.00 1.00 0.00 0.00
## 1975 1.00 0.00 0.00 0.00
## 1976 0.00 1.00 0.00 0.00
## 1977 0.00 1.00 0.00 0.00
## 1978 1.00 0.00 0.00 0.00
## 1979 0.00 1.00 0.00 0.00
## 1980 0.00 1.00 0.00 0.00
## 1981 0.00 1.00 0.00 0.00
## 1982 0.00 1.00 0.00 0.00
## 1983 0.00 1.00 0.00 0.00
## 1984 0.00 1.00 0.00 0.00
## 1985 0.00 1.00 0.00 0.00
## 1986 1.00 0.00 0.00 0.00
## 1987 0.00 1.00 0.00 0.00
## 1988 0.00 1.00 0.00 0.00
## 1989 1.00 0.00 0.00 0.00
## 1990 0.00 1.00 0.00 0.00
## 1991 1.00 0.00 0.00 0.00
## 1992 0.00 1.00 0.00 0.00
## 1993 0.00 1.00 0.00 0.00
## 1994 0.00 1.00 0.00 0.00
## 1995 0.00 1.00 0.00 0.00
## 1996 0.00 1.00 0.00 0.00
## 1997 1.00 0.00 0.00 0.00
## 1998 0.00 1.00 0.00 0.00
## 1999 0.00 1.00 0.00 0.00
## 2000 0.00 1.00 0.00 0.00
## 2001 0.00 1.00 0.00 0.00
## 2002 1.00 0.00 0.00 0.00
## 2003 0.00 1.00 0.00 0.00
## 2004 0.00 1.00 0.00 0.00
## 2005 1.00 0.00 0.00 0.00
## 2006 0.00 1.00 0.00 0.00
## 2007 0.00 1.00 0.00 0.00
## 2008 1.00 0.00 0.00 0.00
## 2009 0.00 1.00 0.00 0.00
## 2010 0.00 1.00
str(ausbeer)
##  Time-Series [1:218] from 1956 to 2010: 284 213 227 308 262 228 236 320 272 233 ...
 I checked the help. "easter" means easter holidays in each season 
                     "ausbeer" means quarterly Australian Beer production
 start of ausbeer is 1st quarter of 1956 and the last is the 2nd quarter of 2010.
 There are 218 data points.
 easter function returns a vector of 0's and 1's or fractional results if Easter spans March and April in the observed time period.
 Easter is defined as the days from Good Friday to Easter Sunday inclusively, plus optionally Easter Monday if easter.mon=TRUE.
  1. The data set fancy concerns the monthly sales figures of a shop which opened in January 1987 and sells gifts, souvenirs, and novelties. 5a.Produce a time plot of the data and describe the patterns in the graph. Identify any unusual or unexpected fluctuations in the time series.
fancy
##            Jan       Feb       Mar       Apr       May       Jun       Jul
## 1987   1664.81   2397.53   2840.71   3547.29   3752.96   3714.74   4349.61
## 1988   2499.81   5198.24   7225.14   4806.03   5900.88   4951.34   6179.12
## 1989   4717.02   5702.63   9957.58   5304.78   6492.43   6630.80   7349.62
## 1990   5921.10   5814.58  12421.25   6369.77   7609.12   7224.75   8121.22
## 1991   4826.64   6470.23   9638.77   8821.17   8722.37  10209.48  11276.55
## 1992   7615.03   9849.69  14558.40  11587.33   9332.56  13082.09  16732.78
## 1993  10243.24  11266.88  21826.84  17357.33  15997.79  18601.53  26155.15
##            Aug       Sep       Oct       Nov       Dec
## 1987   3566.34   5021.82   6423.48   7600.60  19756.21
## 1988   4752.15   5496.43   5835.10  12600.08  28541.72
## 1989   8176.62   8573.17   9690.50  15151.84  34061.01
## 1990   7979.25   8093.06   8476.70  17914.66  30114.41
## 1991  12552.22  11637.39  13606.89  21822.11  45060.69
## 1992  19888.61  23933.38  25391.35  36024.80  80721.71
## 1993  28586.52  30505.41  30821.33  46634.38 104660.67
autoplot(fancy)

  Sales generally increased from January to December and increased sharply in Decembers. 
  Sales in Decembers increased every year except in 1990. There is a decrease in that year.
  From 1988 to 1993, there was also a smaller increase every March compared to the December increase.

5b.Explain why it is necessary to take logarithms of these data before fitting a model.

 Because the seaonal pattern varies increase in a proportion, it is necessary to transfer to logarithms to keep the same seasonal pattern size.

5c.Use R to fit a regression model to the logarithms of these sales data with a linear trend, seasonal dummies and a “surfing festival” dummy variable.

time_fancy <- time(fancy)
surfing_festival <- c()
for(i in 1:length(time_fancy)){
  month <- round(12*(time_fancy[i] - floor(time_fancy[i]))) + 1
  year <- floor(time_fancy[i])
  if(year >= 1988 & month == 3){
    surfing_festival[i] <- 1
  } else {
    surfing_festival[i] <- 0
  }
}

tslm_log_fancy <- tslm(BoxCox(fancy, 0) ~ trend + season + surfing_festival)

5d.Plot the residuals against time and against the fitted values. Do these plots reveal any problems with the model?

autoplot(tslm_log_fancy$residuals)

 The result plot shows that residuals have pattern as time goes by. It means that there is correlation between residuals and time.
cbind(Residuals = tslm_log_fancy$residuals,
      Fitted_values = tslm_log_fancy$fitted.values) %>%
  as.data.frame() %>%
  ggplot(aes(x = Fitted_values,
             y = Residuals)) +
  geom_point()

 The scatter plot shows that the distribution of residuals is heteroscedacity. 
 That is to say taking logarithms does not make the seasonal patter homoscadecity as we expected

5e.Do boxplots of the residuals for each month. Does this reveal any problems with the model?

cbind.data.frame(
  Month = factor(
    month.abb[round(12*(time_fancy - floor(time_fancy)) + 1)],
    labels = month.abb,
    ordered = TRUE
  ),
  Residuals = tslm_log_fancy$residuals
) %>%
  ggplot(aes(x = Month,
             y = Residuals)) +
  geom_boxplot()
## Don't know how to automatically pick scale for object of type ts. Defaulting to continuous.

  The warning says "Don't know how to automatically pick scale for object of type ts. Defaulting to continuous". 
   the box plot is hard to describe the residuals meaningfully. 

5f.What do the values of the coefficients tell you about each variable?

summary(tslm_log_fancy)
## 
## Call:
## tslm(formula = BoxCox(fancy, 0) ~ trend + season + surfing_festival)
## 
## Residuals:
##      Min       1Q   Median       3Q      Max 
## -0.33673 -0.12757  0.00257  0.10911  0.37671 
## 
## Coefficients:
##                   Estimate Std. Error t value Pr(>|t|)    
## (Intercept)      7.6196670  0.0742471 102.626  < 2e-16 ***
## trend            0.0220198  0.0008268  26.634  < 2e-16 ***
## season2          0.2514168  0.0956790   2.628 0.010555 *  
## season3          0.2660828  0.1934044   1.376 0.173275    
## season4          0.3840535  0.0957075   4.013 0.000148 ***
## season5          0.4094870  0.0957325   4.277 5.88e-05 ***
## season6          0.4488283  0.0957647   4.687 1.33e-05 ***
## season7          0.6104545  0.0958039   6.372 1.71e-08 ***
## season8          0.5879644  0.0958503   6.134 4.53e-08 ***
## season9          0.6693299  0.0959037   6.979 1.36e-09 ***
## season10         0.7473919  0.0959643   7.788 4.48e-11 ***
## season11         1.2067479  0.0960319  12.566  < 2e-16 ***
## season12         1.9622412  0.0961066  20.417  < 2e-16 ***
## surfing_festival 0.5015151  0.1964273   2.553 0.012856 *  
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 0.179 on 70 degrees of freedom
## Multiple R-squared:  0.9567, Adjusted R-squared:  0.9487 
## F-statistic:   119 on 13 and 70 DF,  p-value: < 2.2e-16
 the positive trend means the sale increase with time.
 the positive coefficients of seasons mean the every January has more sale than previous January.
 the positive coefficient of surfing_festivel having significant level of 1 star means surfing festivle posiively influecing the sale.

5g.What does the Breusch-Godfrey test tell you about your model?

checkresiduals(tslm_log_fancy)

## 
##  Breusch-Godfrey test for serial correlation of order up to 17
## 
## data:  Residuals from Linear regression model
## LM test = 37.954, df = 17, p-value = 0.002494
  the pvalue is 0.002494 and much samller than 0.05. 
  That means the model suffers from significent autocorrelation in residuals. 

5h.Regardless of your answers to the above questions, use your regression model to predict the monthly sales for 1994, 1995, and 1996. Produce prediction intervals for each of your forecasts.

future_fancy <- rep(0, 36)
for(i in 1:36){
  if(i %% 12 == 3){
    future_fancy[i] <- 1
  }
}
future_fancy <- ts(data = future_fancy, start = 1994, end = c(1996, 12),frequency = 12)
fc_tslm_log_fancy <- forecast(tslm_log_fancy,newdata = data.frame(Time = time(future_fancy),surfing_festival = future_fancy))
autoplot(fc_tslm_log_fancy)

fc_tslm_log_fancy$lower
##               [,1]      [,2]
## Jan 1994  9.238522  9.101594
## Feb 1994  9.511959  9.375031
## Mar 1994 10.048860  9.911228
## Apr 1994  9.688635  9.551707
## May 1994  9.736088  9.599161
## Jun 1994  9.797449  9.660522
## Jul 1994  9.981095  9.844168
## Aug 1994  9.980625  9.843698
## Sep 1994 10.084010  9.947083
## Oct 1994 10.184092 10.047165
## Nov 1994 10.665468 10.528541
## Dec 1994 11.442981 11.306054
## Jan 1995  9.499844  9.361338
## Feb 1995  9.773281  9.634775
## Mar 1995 10.310518 10.171489
## Apr 1995  9.949957  9.811451
## May 1995  9.997411  9.858904
## Jun 1995 10.058772  9.920265
## Jul 1995 10.242418 10.103911
## Aug 1995 10.241948 10.103441
## Sep 1995 10.345333 10.206826
## Oct 1995 10.445415 10.306908
## Nov 1995 10.926791 10.788284
## Dec 1995 11.704304 11.565797
## Jan 1996  9.760564  9.620151
## Feb 1996 10.034000  9.893588
## Mar 1996 10.571566 10.430810
## Apr 1996 10.210677 10.070264
## May 1996 10.258130 10.117718
## Jun 1996 10.319491 10.179079
## Jul 1996 10.503137 10.362725
## Aug 1996 10.502667 10.362254
## Sep 1996 10.606052 10.465640
## Oct 1996 10.706134 10.565722
## Nov 1996 11.187510 11.047097
## Dec 1996 11.965023 11.824611
fc_tslm_log_fancy$upper
##               [,1]     [,2]
## Jan 1994  9.744183  9.88111
## Feb 1994 10.017620 10.15455
## Mar 1994 10.557120 10.69475
## Apr 1994 10.194296 10.33122
## May 1994 10.241749 10.37868
## Jun 1994 10.303110 10.44004
## Jul 1994 10.486756 10.62368
## Aug 1994 10.486286 10.62321
## Sep 1994 10.589671 10.72660
## Oct 1994 10.689753 10.82668
## Nov 1994 11.171129 11.30806
## Dec 1994 11.948642 12.08557
## Jan 1995 10.011336 10.14984
## Feb 1995 10.284773 10.42328
## Mar 1995 10.823938 10.96297
## Apr 1995 10.461449 10.59996
## May 1995 10.508903 10.64741
## Jun 1995 10.570264 10.70877
## Jul 1995 10.753910 10.89242
## Aug 1995 10.753440 10.89195
## Sep 1995 10.856825 10.99533
## Oct 1995 10.956907 11.09541
## Nov 1995 11.438282 11.57679
## Dec 1995 12.215796 12.35430
## Jan 1996 10.279093 10.41951
## Feb 1996 10.552530 10.69294
## Mar 1996 11.091365 11.23212
## Apr 1996 10.729206 10.86962
## May 1996 10.776659 10.91707
## Jun 1996 10.838021 10.97843
## Jul 1996 11.021667 11.16208
## Aug 1996 11.021196 11.16161
## Sep 1996 11.124582 11.26499
## Oct 1996 11.224664 11.36508
## Nov 1996 11.706039 11.84645
## Dec 1996 12.483552 12.62396

5i.Transform your predictions and intervals to obtain predictions and intervals for the raw data.

5j.How could you improve these predictions by modifying the model?

  1. The gasoline series consists of weekly data for supplies of US finished motor gasoline product, from 2 February 1991 to 20 January 2017. The units are in “million barrels per day”. Consider only the data to the end of 2004. gasoline 6a.Fit a harmonic regression with trend to the data. Experiment with changing the number Fourier terms. Plot the observed gasoline and fitted values and comment on what you see.
str(gasoline)
##  Time-Series [1:1355] from 1991 to 2017: 6.62 6.43 6.58 7.22 6.88 ...
head(gasoline)  
## Time Series:
## Start = 1991.1 
## End = 1991.19582477755 
## Frequency = 52.1785714285714 
## [1] 6.621 6.433 6.582 7.224 6.875 6.947
gasoline_2004 <- window(gasoline, end = 2005)
autoplot(gasoline_2004, xlab = "Year")

for(num in c(1, 2, 3, 5, 10, 20)){
  var_name <- paste("fit",
                    as.character(num),
                    "_gasoline_2004",
                    sep = "")
  
  assign(var_name,
         tslm(gasoline_2004 ~ trend + fourier(
           gasoline_2004,
           K = num
         ))
  )
  
  print(
    autoplot(gasoline_2004) +
      autolayer(get(var_name)$fitted.values,
                series = as.character(num)) +
      ggtitle(var_name) +
      ylab("gasoline") +
      guides(colour = guide_legend(title = "Number of Fourier Transform pairs"))
  )
}

autoplot(gasoline_2004) +
  autolayer(fit1_gasoline_2004$fitted.values, series = "1") +
  autolayer(fit5_gasoline_2004$fitted.values, series = "2") +
  autolayer(fit10_gasoline_2004$fitted.values, series = "3") +
  autolayer(fit10_gasoline_2004$fitted.values, series = "5") +
  autolayer(fit20_gasoline_2004$fitted.values, series = "10") +
  autolayer(fit20_gasoline_2004$fitted.values, series = "20") +
  guides(colour = guide_legend(title = "Fourier Transform pairs")) +
  scale_color_discrete(breaks = c(1, 2, 3, 5, 10, 20))

  more number of Fourier pairs used, the fitted line looks more like the original data

6b.Select the appropriate number of Fourier terms to include by minimising the AICc or CV value.

for(i in c(1, 2, 3, 5, 10, 20)){
  fit_gasoline_2004.name <- paste(
    "fit", as.character(i), "_gasoline_2004",
    sep = ""
  )
  writeLines(
    paste(
      "\n", fit_gasoline_2004.name, "\n"
    )
  )
  print(CV(get(fit_gasoline_2004.name)))
}
## 
##  fit1_gasoline_2004 
## 
##            CV           AIC          AICc           BIC         AdjR2 
##  8.201921e-02 -1.813292e+03 -1.813208e+03 -1.790354e+03  8.250858e-01 
## 
##  fit2_gasoline_2004 
## 
##            CV           AIC          AICc           BIC         AdjR2 
##  8.136854e-02 -1.819113e+03 -1.818957e+03 -1.787001e+03  8.269569e-01 
## 
##  fit3_gasoline_2004 
## 
##            CV           AIC          AICc           BIC         AdjR2 
##  7.658846e-02 -1.863085e+03 -1.862834e+03 -1.821797e+03  8.375702e-01 
## 
##  fit5_gasoline_2004 
## 
##            CV           AIC          AICc           BIC         AdjR2 
##  7.553646e-02 -1.873234e+03 -1.872723e+03 -1.813596e+03  8.406928e-01 
## 
##  fit10_gasoline_2004 
## 
##            CV           AIC          AICc           BIC         AdjR2 
##  7.135754e-02 -1.915014e+03 -1.913441e+03 -1.809500e+03  8.516102e-01 
## 
##  fit20_gasoline_2004 
## 
##            CV           AIC          AICc           BIC         AdjR2 
##  7.223834e-02 -1.908017e+03 -1.902469e+03 -1.710753e+03  8.540588e-01
  fourior term of 10 minimize CV and AICc

6c.Check the residuals of the final model using the checkresiduals() function. Even though the residuals fail the correlation tests, the results are probably not severe enough to make much difference to the forecasts and prediction intervals. (Note that the correlations are relatively small, even though they are significant.)

checkresiduals(fit10_gasoline_2004)

## 
##  Breusch-Godfrey test for serial correlation of order up to 104
## 
## data:  Residuals from Linear regression model
## LM test = 155.45, df = 104, p-value = 0.0008135

6d.To forecast using harmonic regression, you will need to generate the future values of the Fourier terms. Forecast the next year of data.

fc_gasoline_2005 <- forecast(
  fit10_gasoline_2004,
  newdata=data.frame(fourier(
    gasoline_2004, K = 10, h = 52)
  )
)

6e.Plot the forecasts along with the actual data for 2005. What do you find?

autoplot(fc_gasoline_2005) +
  autolayer(window(
    gasoline,
    start = 2004,
    end = 2006
  )
  ) +
  scale_x_continuous(limits = c(2004, 2006))
## Scale for 'x' is already present. Adding another scale for 'x', which
## will replace the existing scale.
## Warning: Removed 674 rows containing missing values (geom_path).

   the model's prediction is able to be restrained in the 80% interval. However, it fails to predict the dramatic drop. 
  1. Data set huron gives the water level of Lake Huron in feet from 1875 to 1972. 7a.Plot the data and comment on its features.
autoplot(huron)

str(huron)
##  Time-Series [1:98] from 1875 to 1972: 10.38 11.86 10.97 10.8 9.79 ...
head(huron)
## Time Series:
## Start = 1875 
## End = 1880 
## Frequency = 1 
## [1] 10.38 11.86 10.97 10.80  9.79 10.39
   the seasonal pattern is not obvious. There is a downward trend between 1880 to 1920. 

7b.Fit a linear regression and compare this to a piecewise linear trend model with a knot at 1915. linear regression trend

tslm_huron <- tslm(huron ~ trend)
  piecewise linear trend 
t <- time(huron)
t.break <- 1915
t_piece <- ts(pmax(0,t-t.break), start=1875)
pw_huron <- tslm(huron ~ t + t_piece)
summary(pw_huron)
## 
## Call:
## tslm(formula = huron ~ t + t_piece)
## 
## Residuals:
##      Min       1Q   Median       3Q      Max 
## -2.49626 -0.66240 -0.07139  0.85163  2.39222 
## 
## Coefficients:
##              Estimate Std. Error t value Pr(>|t|)    
## (Intercept) 132.90870   19.97687   6.653 1.82e-09 ***
## t            -0.06498    0.01051  -6.181 1.58e-08 ***
## t_piece       0.06486    0.01563   4.150 7.26e-05 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 1.045 on 95 degrees of freedom
## Multiple R-squared:  0.3841, Adjusted R-squared:  0.3711 
## F-statistic: 29.62 on 2 and 95 DF,  p-value: 1.004e-10

7c.Generate forecasts from these two models for the period up to 1980 and comment on these.

h=8
fc_tslm_huron <- forecast(tslm_huron, h=h)

t.new <- t[length(t)] + seq(h)
t_piece_new <- t_piece[length(t_piece)]+seq(h)
newdata <- cbind(t=t.new,
                 t_piece=t_piece_new) %>%
  as.data.frame()
fc_pw_huron <- forecast(pw_huron,newdata = newdata)

autoplot(huron) +
  autolayer(fitted(tslm_huron), series = "Linear") +
  autolayer(fc_tslm_huron, series = "Linear") 

autoplot(huron) +
  autolayer(fitted(pw_huron), series = "Piecewise") +
  autolayer(fc_pw_huron, series="Piecewise") 

the tslm model follows the trend while the pw model highlights the trend change.