daily20 <- head(elecdaily, 20) > daily20 %>% + as.data.frame() %>% + ggplot(aes(x=Temperature, y=Demand))+ ylab(“Daily Electrcity Demand in Victoria, Australia”) > elecdaily %>% + as.data.frame() %>% + ggplot(aes(x=Temperature, y=Demand)) +ylab(“Daily Electricty Demand in Victoria, Australia”)+xlab(“Temperature in Victoria, Australia”)+geom_point()+geom_smooth(methods=“lm”, se=FALSE) Warning: Ignoring unknown parameters: methods geom_smooth() using method = ‘loess’ and formula ‘y ~ x’ > # Yes, there is a positive relationship. I am able to say this because of the positive slope that is seen. However, this is not a linear relationship. > checkresiduals(elecdaily) Error in ggtsdisplay(residuals, plot.type = “histogram”, main = main, : ggtsdisplay is only for univariate time series > checkresiduals(elecdaily) Error in ggtsdisplay(residuals, plot.type = “histogram”, main = main, : ggtsdisplay is only for univariate time series > checkresiduals(daily20) Error in ggtsdisplay(residuals, plot.type = “histogram”, main = main, : ggtsdisplay is only for univariate time series > fit <- tslm(Demand ~ Tempterature, data = elecdaily) Error in (function (…, flatten = TRUE, functions = TRUE) : object ‘Tempterature’ not found > fit<-tslm(Demand ~ Temperature, data = elecdaily) > checkresiduals(fit)
Breusch-Godfrey test for serial
correlation of order up to 14
data: Residuals from Linear regression model LM test = 271.66, df = 14, p-value < 2.2e-16
The model is not accurate and this is because you can see that there is a pattern. The books states that this pattern should not be there for it to be considered accurate.
forecast(fit, newdata= dataframe(Temperature= c(15,35))) Error in dataframe(Temperature = c(15, 35)) : could not find function “dataframe” forecast(fit, newdata = data.frame(Temperature= c(15,35))) Point Forecast Lo 80 Hi 80 Lo 95 53.14286 218.6592 184.4779 252.8406 166.3039 53.28571 227.0242 192.6585 261.3898 174.3866 Hi 95 53.14286 271.0146 53.28571 279.6617 # Here we predict that for the 15 degrees, for the 80% confidence interval, the prediction is in the interval [184.4779, 252.8406] and in the 95% confidence interval, the prediction is in [252.8406, 166.3039]. For the 35 degrees, the 80% confidence interval, the prediction is [192.6585, 261.3898] and for the 95% interval, the prediction is [261.3898, 174.3866]. The forcast for the 15 is 218, and for the 35 it is 227. I do believe the forecast as it is in their 80% confidence interval. autoplot(daily20,facets=TRUE) daily20 %>% + as.data.frame() %>% + ggplot(aes(x=Temperature, y=Demand))+ + geom_point()+ + geom_smooth(method=“lm”, se=FALSE) fit<-tslm(Demand~ Tempterature, data= daily20) Error in (function (…, flatten = TRUE, functions = TRUE) : object ‘Tempterature’ not found fit<-tslm(Demand~Temperature, data=daily20) checkresiduals(fit)
Breusch-Godfrey test for serial correlation
of order up to 5
data: Residuals from Linear regression model LM test = 3.8079, df = 5, p-value = 0.5774
forecast(fit, newdata=data.frame(Temperature=c(15,35))) Point Forecast Lo 80 Hi 80 Lo 95 3.857143 140.5701 108.6810 172.4591 90.21166 4.000000 275.7146 245.2278 306.2014 227.57056 Hi 95 3.857143 190.9285 4.000000 323.8586 # the forecast for the 15 degrees is 140,5701, and in the 80 percent CI is: [108.6810,172.4591] and in the 95% is [90.21166,190.9285]. The forecast for the 35 degrees is 275.7146, and the 80% interval is [245.2278,306.2014] and in the 95% is [227.57056,323.8586]. elecdaily %>% + as.data.frame() %>% + ggplot(aes(x=Temperature, y=Demand, color=as.factor(WorkDay))) + geom_point()+ + labs(title= Demand of Electricity in Victoria, x=“Demand”, y= “Temperature”, color= NULL)+ scale_color_manual(labels=c(“NoWork”, “WorkDay”)), values=c(“orange”, “purple”) Error: unexpected symbol in: “+ geom_point()+ labs(title= Demand of” elecdaily %>% + as.data.frame() %>% + ggplot(aes(x=Temperature, y=Demand, colour=as.factor(WorkDay)))+geom_point()+labs(title=“Demand of Electricity in Victoria”, x=“Demand”, y=“Temperature”, colour= NULL)+ scale_color_manuel(labels=c(“NoWork”, “WorkDay”), values=c(“orange”, “purple”)) Error in scale_color_manuel(labels = c(“NoWork”, “WorkDay”), values = c(“orange”, : could not find function “scale_color_manuel” elecdaily %>% + as.data.frame() %>% + ggplot(aes(x=Temperature,y=Demand, colour=as.factor(WorkDay)))+geom_print()+labs(title=“Demand of Electricity in Victoria”, x=“Demand”, y=“Temperature”, colour=NULL)+scale_color_manual(labels=c(“NoWork”, “WorkDay”), values=c(“orange”,“purple”)) Error in geom_print() : could not find function “geom_print” elecdaily 5>% Error: unexpected numeric constant in “elecdaily 5” elecdaily %>% + as.data.frame() %>% + ggplot(aes(x=Temperature,y=Demand, colour=as.factor(WorkDay)))+ + geom_point()+ + labs(title=“Demand of Electricity in Victoria”, x=“Demand”, y=“Temperature”, color=NULL)+scale_color_manual(labels=c(“NoWork”,“WorkDay”), values=c(“orange”,“purple”)) # The model tells me that the demand for work days is higher than the no work days. mens400 Time Series: Start = 1896 End = 2016 Frequency = 0.25 [1] 54.20 49.40 49.20 50.00 48.20 NA 49.60 47.60 47.80 46.20 46.50 NA [13] NA 46.20 45.90 46.70 44.90 45.10 43.80 44.66 44.26 44.60 44.27 43.87 [25] 43.50 43.49 43.84 44.00 43.75 43.94 43.03 mens400 %>% + as.data.frame() %>% + ggplot(aes(x=Seconds, y=Year))+ + ylab(“Olympic Year”)+xlab(“Time in Seconds”)+geom_point()+geom_smooth(methods=“lm”, se=FALSE) Warning: Ignoring unknown parameters: methods Error in FUN(X[[i]], …) : object ‘Seconds’ not found autoplot(mens400) # By looking at this model, there were less winners (as we see decreasing) as time went on. fit.mens400 Error: object ‘fit.mens400’ not found line <-na.interp(mens400, lambda = NULL) times <- time(df) Error in attr(x, “tsp”) <- c(1, NROW(x), 1) : object is not a matrix time <-time(df) Error in attr(x, “tsp”) <- c(1, NROW(x), 1) : object is not a matrix df <-na.interp(mens400, lambda =NULL) time <-time(df) line <-na.interc(mens400, lambda = NULL) Error in na.interc(mens400, lambda = NULL) : could not find function “na.interc” df <-na.inter(mens400, lambda = NULL) Error in na.inter(mens400, lambda = NULL) : could not find function “na.inter” line <- na.interp(mens400, lambda = NULL) time <-time(line) fit.mens400 <-tslm(line-time, data = line) Error in formula.default(object, env = baseenv()) : invalid formula fit.mens400 <-tslm (line-time, data = line) Error in formula.default(object, env = baseenv()) : invalid formula l <- na.interp(mens400, lambda = NULL) a <- time(l) fit.mens400 <- tslm (l-a, data = l) Error in formula.default(object, env = baseenv()) : invalid formula df <- na.interp(mens400, lambda = NULL) t <- time(df) fit.mens400 <- tslm (df-t, data = df) Error in formula.default(object, env = baseenv()) : invalid formula l <- na.interp(mens400, lambda = NULL) a <- time(l) mens400 %>% + as.data.frame() %>% + ggplot(aes(x,y))+ylab()+xlab()+geom_point()+ geom_smoth(method= “lm”, se=FALSE) Error in rlang::list2(…, title = title, subtitle = subtitle, caption = caption, : argument “label” is missing, with no default autoplot(mens400, series = “Fitted”) summary(mens400) Min. 1st Qu. Median Mean 3rd Qu. Max. NA’s 43.03 43.92 45.00 46.02 47.65 54.20 3 tslm(Time ~ Year, data = mens400) Error in (function (…, flatten = TRUE, functions = TRUE) : object ‘Time’ not found mens400 Time Series: Start = 1896 End = 2016 Frequency = 0.25 [1] 54.20 49.40 49.20 50.00 48.20 NA 49.60 47.60 47.80 46.20 46.50 [12] NA NA 46.20 45.90 46.70 44.90 45.10 43.80 44.66 44.26 44.60 [23] 44.27 43.87 43.50 43.49 43.84 44.00 43.75 43.94 43.03 tslm(seconds ~ year, data = mens400) Error in (function (…, flatten = TRUE, functions = TRUE) : object ‘seconds’ not found time <- time(mens400) timetime <- tslm(mens400 ~ time, data = mens400) summary(timetime)
Call: tslm(formula = mens400 ~ time, data = mens400)
Residuals: Error in quantile.default(resid) : missing values and NaN’s not allowed if ‘na.rm’ is FALSE > summary(timetime)
Call: tslm(formula = mens400 ~ time, data = mens400)
Residuals: Error in quantile.default(resid) : missing values and NaN’s not allowed if ‘na.rm’ is FALSE > summary(timetime)
Call: tslm(formula = mens400 ~ time, data = mens400)
Residuals: Error in quantile.default(resid) : missing values and NaN’s not allowed if ‘na.rm’ is FALSE > tslm(mens400 ~ time, data= mens400)
Call: tslm(formula = mens400 ~ time, data = mens400)
Coefficients: (Intercept) time
172.48148 -0.06457
I have that the average rate per year is 0.064 seconds.
checkresiduals(timetime)
Breusch-Godfrey test for serial correlation of order up to 6
data: Residuals from Linear regression model LM test = 3.6082, df = 6, p-value = 0.7295
the models show that there are several values that are less than zero. Also, we see no pattern here. This means (unlike in question one), that this model is an adequate representation of the data
So then the fitted line is a good representation of our data
win <-seq(2020,2020, frequency = 4) Warning message: In seq.default(2020, 2020, frequency = 4) : extra argument ‘frequency’ will be disregarded win <-seq(2020,2020) forecast(fit.mens400, newdata = data.frame(win)) Error in forecast(fit.mens400, newdata = data.frame(win)) : object ‘fit.mens400’ not found win <-seq(2020,2020) forecast(timetime, newdata = data.frame(win)) Error in model.frame.default(Terms, newdata, na.action = na.action, xlev = object\(xlevels) : object is not a matrix In addition: Warning message: In forecast.lm(timetime, newdata = data.frame(win)) : Could not find required variable time in newdata. Specify newdata as a named data.frame win <- seq(2020,2020,4) forecast(timetime newdata = data.frame(win)) Error: unexpected symbol in "forecast(timetime newdata" win<- seq(2020,2020,4) forecast(timetime, newdata = data.frame(win)) Error in model.frame.default(Terms, newdata, na.action = na.action, xlev = object\)xlevels) : object is not a matrix In addition: Warning message: In forecast.lm(timetime, newdata = data.frame(win)) : Could not find required variable time in newdata. Specify newdata as a named data.frame win <-seq(2020, 2020, 4) forecast(mens400 ~ time, newdata = data.frame(win)) Error in meanf(object, h = h, level = level, fan = fan, lambda = lambda, : unused argument (newdata = data.frame(win)) forecast(fit, newdata=data.frame(win)) Error in eval(predvars, data, env) : object ‘Temperature’ not found In addition: Warning message: In forecast.lm(fit, newdata = data.frame(win)) : Could not find required variable Temperature in newdata. Specify newdata as a named data.frame forecast(timetime, 2020) Point Forecast Lo 80 Hi 80 Lo 95 Hi 95 2020 42.04231 40.44975 43.63487 39.55286 44.53176 Warning message: In forecast.lm(timetime, 2020) : newdata column names not specified, defaulting to first variable required. # I had trouble with this one, but if it is right, then with a 80% confidence interval, the winning time will be between [40.44975,43.63487], and with a 95 percent confidence interval, the winning time will be 95% confidence interval [39.55286, 44.53176] # I can assume that the winning time will be around 42 seconds 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
# I see that there are dummy variables here. The book helps me to understand that the one means that Easter falls during that time period. The zero means that Easter does not fall during that time period (lambda <- BoxCox.lamba(elec)) Error in BoxCox.lamba(elec) : could not find function “BoxCox.lamba” autoplot(BoxCox(elec,lambda)) Error in BoxCox(elec, lambda) : object ‘lambda’ not found (lambda <- BoxCox.lambda(elec)) [1] 0.2654076 autoplot(BoxCox(elec, lambda)) # I believe that to do this you have to do the natural logarithm transformation which in chapter 3 tells us to use the Box-Cox transformation. The way to do this is to use the BoxCox.lambda() command timeplot <- autoplot(fancy) timeplotsecond <- ggsubseriesplot(fancy) timeplot <- autoplot(fancy, main =NULL) timeplot <- autoplot(fancy, main = NULL) autoplot(fancy[,“Sales”])+ggtitle(“Sales Volume”) Error in[.default(fancy, , “Sales”) : incorrect number of dimensions autoplot(fancy) # You can see that there is a significant increase (an upward trend) over the time. autoplot(fancy,month) Error in autoplot.ts(fancy, month) : object ‘month’ not found autoplot(fancy(month.abb)) Error in fancy(month.abb) : could not find function “fancy” autoplot(fancy) ggsubseriesplot(fancy) # this command then shows me the sales every month. Of course, it shows that in December there is a SIGNIFICANT increase in the sales. This is because of Christmas. The fall months, there are also much more sales than in the spring months. March has that weird peak, and that is probably because of surfing time. # We must transform this into a logarithm because we are dealing with data that gives us a nonlinear relationship. Also, this allows us to see the seasonality in the data, which in this case, is very important. regressionfit.fancy <-tslm(BoxCox(fancy, 0)~trend+season) autoplot(BoxCox(fancy,0))+ + xlab(“Month”)+ylab(“Sales”) # Not completely sure if this is right, but this is the code I would think is right to complete 5c plot <- autoplot(regressionfit.fancy(residuals())) Error in regressionfit.fancy(residuals()) : could not find function “regressionfit.fancy” plot <- autoplot(regressionfit.fancy\(residuals) plot <- autoplot(regressionfit.fancy\)residuals) plot<-autoplot(regressionfit.fancy\(redsiduals) Error: Objects of type NULL not supported by autoplot. plot <-autoplot(regressionfit.fancy\)residuals) autoplot(plot) Error: Objects of type gg/ggplot not supported by autoplot. autoplot(regressionfit.fancy$residuals) autoplot(regressionfit.fancy&fittedresiduals) Error in autoplot(regressionfit.fancy & fittedresiduals) : object ‘fittedresiduals’ not found # This model shows me that there is no set pattern of the data. Therefore, this isn’t good time series data. ggsubseriesplot(a10) # I do think that this model shows some problems. As you can see here, it now seems like January has the highest sales, when it should be December. Also, you see that there is a random decrease in sales in the month of December which was not seen in the other models. summary(fancy) Min. 1st Qu. Median Mean 3rd Qu. Max. 1665 5884 8772 14316 16889 104661 summary(regressionfit.fancy)
Call: tslm(formula = BoxCox(fancy, 0) ~ trend + season)
Residuals: Min 1Q Median 3Q Max -0.41644 -0.12619 0.00608 0.11389 0.38567
Coefficients: Estimate Std. Error t value Pr(>|t|)
(Intercept) 7.6058604 0.0768740 98.939 < 2e-16 trend 0.0223930 0.0008448 26.508 < 2e-16 season2 0.2510437 0.0993278 2.527 0.013718 *
season3 0.6952066 0.0993386 6.998 1.18e-09 season4 0.3829341 0.0993565 3.854 0.000252 season5 0.4079944 0.0993817 4.105 0.000106 season6 0.4469625 0.0994140 4.496 2.63e-05 season7 0.6082156 0.0994534 6.116 4.69e-08 season8 0.5853524 0.0995001 5.883 1.21e-07 season9 0.6663446 0.0995538 6.693 4.27e-09 season10 0.7440336 0.0996148 7.469 1.61e-10 season11 1.2030164 0.0996828 12.068 < 2e-16 season12 1.9581366 0.0997579 19.629 < 2e-16 — Signif. codes: 0 ‘’ 0.001 ‘’ 0.01 ‘’ 0.05 ‘.’ 0.1 ‘’ 1
Residual standard error: 0.1858 on 71 degrees of freedom Multiple R-squared: 0.9527, Adjusted R-squared: 0.9447 F-statistic: 119.1 on 12 and 71 DF, p-value: < 2.2e-16
All of the coefficients are positive. This means that there is an increase in the sales. And of course, season12 has the largest coefficient, meaning that December has the highest sales which makes sense. The spring months have somewhat higher coefficients, and this is because of the surfing season.
The Breusch-Godfrey test is able to tell me the autocorrelation that is in the residuals. This also tests for serial correlation. If this were to show us a small p-value then there is autocorrelation in the residuals.
checkresiduals(fancy) Warning message: In modeldf.default(object) : Could not find appropriate degrees of freedom for this model. checkresiduals(regressionfit.fancy)
Breusch-Godfrey test for serial correlation of order up to 16
data: Residuals from Linear regression model LM test = 37.152, df = 16, p-value = 0.001996
We do have a small p value, but this p value is positive, so there isn’t autocorrelation in the residuals.
prediction <- forecast(regressionfit.fancy, n = 12 3) Warning message: In forecast.lm(regressionfit.fancy, n = 12 3) : newdata column names not specified, defaulting to first variable required. prediction <- forecast(regressionfit.fancy, h = 12 * 3) autoplot(prediction) # The model of the prediction. forecast(prediction) Point Forecast Lo 80 Hi 80 Lo 95 Hi 95 Jan 1994 9.509264 9.246995 9.771532 9.105002 9.913525 Feb 1994 9.782700 9.520432 10.044969 9.378439 10.186962 Mar 1994 10.249256 9.986988 10.511525 9.844995 10.653518 Apr 1994 9.959377 9.697108 10.221645 9.555115 10.363638 May 1994 10.006830 9.744562 10.269098 9.602569 10.411091 Jun 1994 10.068191 9.805923 10.330459 9.663930 10.472452 Jul 1994 10.251837 9.989569 10.514105 9.847576 10.656099 Aug 1994 10.251367 9.989099 10.513635 9.847106 10.655628 Sep 1994 10.354752 10.092484 10.617020 9.950491 10.759014 Oct 1994 10.454834 10.192566 10.717102 10.050573 10.859095 Nov 1994 10.936210 10.673942 11.198478 10.531949 11.340471 Dec 1994 11.713723 11.451455 11.975991 11.309462 12.117984 Jan 1995 9.777979 9.512777 10.043182 9.369195 10.186763 Feb 1995 10.051416 9.786214 10.316618 9.642632 10.460200 Mar 1995 10.517972 10.252770 10.783174 10.109188 10.926756 Apr 1995 10.228092 9.962890 10.493295 9.819308 10.636876 May 1995 10.275546 10.010343 10.540748 9.866762 10.684330 Jun 1995 10.336907 10.071704 10.602109 9.928123 10.745691 Jul 1995 10.520553 10.255351 10.785755 10.111769 10.929337 Aug 1995 10.520083 10.254880 10.785285 10.111299 10.928867 Sep 1995 10.623468 10.358266 10.888670 10.214684 11.032252 Oct 1995 10.723550 10.458347 10.988752 10.314766 11.132334 Nov 1995 11.204926 10.939723 11.470128 10.796142 11.613710 Dec 1995 11.982439 11.717236 12.247641 11.573655 12.391223 Jan 1996 10.046695 9.777950 10.315440 9.632451 10.460940 Feb 1996 10.320132 10.051387 10.588877 9.905887 10.734376 Mar 1996 10.786688 10.517943 11.055433 10.372443 11.200932 Apr 1996 10.496808 10.228063 10.765553 10.082564 10.911053 May 1996 10.544261 10.275516 10.813007 10.130017 10.958506 Jun 1996 10.605623 10.336878 10.874368 10.191378 11.019867 Jul 1996 10.789269 10.520524 11.058014 10.375024 11.203513 Aug 1996 10.788798 10.520053 11.057543 10.374554 11.203043 Sep 1996 10.892184 10.623439 11.160929 10.477939 11.306428 Oct 1996 10.992266 10.723521 11.261011 10.578021 11.406510 Nov 1996 11.473641 11.204896 11.742386 11.059397 11.887886 Dec 1996 12.251155 11.982409 12.519900 11.836910 12.665399 # Intervals of my prediction print(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 # the predictions and intervals for the raw data # I coudl improve these predictions by using the classical decomposition that we practiced earlier this week. I say this because I learned earlier in the week that these should be used for seasonal data. 2004 <- tslm(gasoline, end = 2004) Error in 2004 <- tslm(gasoline, end = 2004) : invalid (do_set) left-hand side to assignment twothousand <- tslm(gasoline, end = 2004) Error in formula.default(object, env = baseenv()) : invalid formula gasoline Time Series: Start = 1991.1 End = 2017.04934976044 Frequency = 52.1785714285714 [1] 6.621 6.433 6.582 7.224 6.875 6.947 7.328 6.777 [9] 7.503 6.916 7.045 6.956 6.976 7.185 6.899 7.396 [17] 7.287 7.220 7.651 7.720 7.543 7.961 7.327 7.380 [25] 7.501 7.605 7.584 7.401 7.505 7.427 6.993 7.402 [33] 7.391 7.088 6.949 7.506 7.396 7.421 7.159 7.379 [41] 7.369 6.652 6.769 6.828 7.344 7.447 7.186 6.642 [49] 6.788 6.783 7.037 7.112 7.039 6.576 7.002 6.770 [57] 7.002 6.806 7.221 7.379 7.027 7.744 6.464 7.376 [65] 7.899 7.318 7.174 7.086 7.774 7.104 7.129 7.407 [73] 7.701 7.572 7.813 7.422 7.792 7.433 7.796 7.700 [81] 7.365 7.814 7.326 7.473 7.181 7.286 7.293 7.514 [89] 7.204 7.276 7.509 6.923 7.056 7.240 7.138 7.432 [97] 7.354 7.400 7.596 7.358 6.852 6.726 7.131 6.603 [105] 7.322 6.779 7.325 6.961 7.188 7.100 7.239 7.194 [113] 6.964 6.797 7.561 7.392 7.470 7.657 7.649 7.295 [121] 7.364 7.018 7.740 7.392 7.804 7.868 7.578 8.113 [129] 7.916 7.336 8.033 7.581 8.187 7.629 7.386 7.889 [137] 7.478 7.485 7.387 7.276 7.613 7.704 7.788 7.640 [145] 7.552 7.992 6.845 7.430 8.074 7.684 7.411 7.567 [153] 7.124 7.124 7.157 6.321 6.953 7.487 6.993 7.372 [161] 7.286 7.214 7.521 7.481 7.398 7.187 7.720 7.042 [169] 7.892 6.824 7.552 7.938 8.028 7.461 7.770 7.650 [177] 7.909 7.930 7.562 8.006 7.523 7.581 7.638 8.187 [185] 7.836 7.750 8.061 6.955 7.601 7.763 7.733 7.515 [193] 7.562 8.143 8.204 7.432 7.377 7.929 7.431 7.593 [201] 8.229 7.933 8.278 7.718 7.134 7.534 7.260 6.976 [209] 7.267 7.132 7.398 7.670 7.194 7.548 7.834 7.797 [217] 8.195 7.588 7.543 8.156 7.731 7.539 8.052 8.183 [225] 7.797 8.460 8.055 7.971 8.489 8.238 7.996 7.866 [233] 8.483 8.213 8.402 7.628 8.411 8.206 7.825 8.078 [241] 8.249 7.821 7.257 8.106 7.879 7.712 8.103 7.449 [249] 8.477 8.021 7.840 7.439 7.949 7.489 8.115 7.035 [257] 7.958 7.513 6.949 7.393 7.513 7.520 7.868 7.826 [265] 7.338 7.877 7.757 7.900 7.568 7.280 7.804 7.433 [273] 8.079 7.592 8.267 7.512 8.080 7.836 7.946 7.957 [281] 7.956 8.563 8.015 7.968 8.152 8.067 8.279 7.722 [289] 7.818 8.364 8.369 7.843 7.537 8.000 7.619 7.935 [297] 7.637 7.958 7.991 7.682 7.804 8.073 7.883 7.370 [305] 7.706 8.355 8.051 7.740 7.122 7.440 7.408 7.265 [313] 7.125 7.013 7.530 8.011 7.779 7.417 8.125 7.729 [321] 7.492 8.115 7.791 7.914 7.922 8.224 7.765 8.253 [329] 8.088 8.154 7.590 8.009 8.557 8.234 8.489 8.606 [337] 8.595 8.600 8.290 8.253 8.539 7.922 8.695 8.053 [345] 8.215 7.746 8.140 8.379 8.221 8.315 7.741 8.355 [353] 8.025 7.793 8.043 7.578 8.437 7.955 8.151 8.201 [361] 7.832 7.673 7.779 7.940 7.670 7.673 8.201 8.005 [369] 7.662 7.744 8.103 7.753 7.996 8.992 8.031 8.303 [377] 8.118 8.190 7.503 8.524 8.337 8.205 8.312 8.103 [385] 8.160 8.644 8.755 8.234 8.749 8.125 8.997 8.317 [393] 8.981 8.288 8.607 8.713 8.722 7.873 8.359 8.058 [401] 8.797 8.049 8.099 8.360 8.195 7.711 8.381 8.313 [409] 8.162 8.314 8.588 8.153 8.519 7.385 7.810 8.186 [417] 7.866 7.532 8.756 7.948 8.316 8.273 8.451 8.367 [425] 8.337 7.959 7.878 8.066 8.923 7.825 8.059 8.761 [433] 8.275 8.401 8.193 8.827 8.522 9.045 9.076 8.679 [441] 8.622 8.639 8.537 8.644 8.519 8.415 8.810 8.778 [449] 8.593 8.504 8.865 8.557 8.433 8.406 8.521 8.532 [457] 8.333 8.416 8.404 8.416 7.670 8.557 8.638 8.590 [465] 8.851 7.907 7.543 7.919 8.385 7.642 8.375 8.524 [473] 8.072 7.608 8.662 8.201 8.384 7.896 8.655 8.557 [481] 8.616 8.302 8.679 8.242 8.408 9.075 8.863 8.403 [489] 8.895 8.475 8.620 8.519 8.844 8.361 8.614 8.759 [497] 8.813 8.786 8.960 8.644 8.573 8.814 8.153 7.819 [505] 8.340 8.663 8.604 8.942 8.780 8.216 8.419 8.063 [513] 8.233 8.711 8.475 8.400 8.693 7.825 8.225 7.894 [521] 7.338 8.589 8.032 7.850 8.187 8.489 8.425 8.484 [529] 8.475 8.507 8.306 8.438 8.708 8.404 8.665 8.275 [537] 8.917 8.270 8.809 8.428 8.393 8.921 9.036 8.622 [545] 9.095 9.176 9.007 8.959 8.671 9.035 8.943 8.612 [553] 8.529 8.969 7.834 8.514 8.500 8.766 8.619 8.702 [561] 8.797 8.771 8.612 8.868 8.517 8.327 8.622 8.888 [569] 8.860 8.288 8.359 8.049 8.184 8.223 8.117 8.428 [577] 8.729 8.782 8.277 8.713 8.621 8.386 8.865 8.840 [585] 8.684 8.699 8.521 8.548 9.190 8.685 8.778 9.004 [593] 8.771 8.752 9.172 9.004 8.985 9.056 9.093 9.297 [601] 9.270 8.939 9.173 9.135 8.844 8.899 8.625 8.582 [609] 8.895 9.090 8.653 9.022 9.324 8.759 8.540 8.667 [617] 9.060 8.616 8.854 9.063 8.989 8.411 8.335 8.645 [625] 8.793 8.551 8.106 8.454 8.619 8.421 9.020 8.596 [633] 8.666 8.284 8.458 8.640 8.737 8.358 8.834 8.894 [641] 8.992 9.299 8.516 8.529 8.880 8.833 9.476 9.050 [649] 8.763 9.177 9.563 9.303 9.381 9.325 9.668 9.337 [657] 8.886 8.974 9.166 8.690 9.190 9.156 8.754 9.413 [665] 9.316 8.995 9.232 8.725 8.979 8.776 8.930 9.166 [673] 8.726 8.686 8.411 8.426 8.543 8.514 8.627 8.727 [681] 8.796 8.970 8.968 9.044 8.818 8.731 9.249 9.338 [689] 8.973 9.255 8.926 9.373 9.119 9.154 9.259 9.045 [697] 9.219 9.194 9.199 9.398 9.300 8.840 9.310 9.263 [705] 9.472 9.521 9.424 9.267 9.453 9.234 9.005 8.922 [713] 8.915 8.917 9.177 8.883 8.977 9.217 9.237 8.988 [721] 9.204 9.107 9.268 9.068 9.329 9.287 8.710 8.695 [729] 8.881 8.848 8.837 8.825 8.898 8.884 9.014 9.131 [737] 9.069 9.075 9.095 9.089 9.170 9.306 9.024 9.175 [745] 9.269 9.443 9.461 9.451 9.528 9.313 9.367 9.721 [753] 9.391 9.415 9.449 9.586 9.483 9.408 9.471 9.406 [761] 9.027 8.636 8.819 8.836 8.840 8.783 8.961 8.981 [769] 9.045 9.246 9.151 9.124 9.189 9.221 9.268 9.326 [777] 9.249 9.358 8.950 8.859 8.826 8.879 8.967 9.083 [785] 9.046 8.951 9.057 9.106 9.100 9.054 9.063 9.297 [793] 9.100 9.012 9.099 9.346 9.327 9.193 9.430 9.367 [801] 9.413 9.429 9.540 9.645 9.621 9.569 9.573 9.639 [809] 9.697 9.530 9.571 9.610 9.622 9.368 9.229 9.287 [817] 9.081 9.242 9.298 9.511 9.499 9.256 9.110 9.149 [825] 9.320 9.477 9.394 9.491 9.229 9.247 9.201 9.060 [833] 9.008 9.090 9.136 9.061 9.198 9.121 9.191 9.158 [841] 9.240 9.250 9.491 9.472 9.247 9.163 9.260 9.344 [849] 9.404 9.433 9.485 9.495 9.487 9.591 9.575 9.557 [857] 9.663 9.710 9.688 9.662 9.575 9.572 9.762 9.626 [865] 9.577 9.393 9.247 9.235 9.120 9.200 9.253 9.373 [873] 9.355 9.367 9.187 9.222 9.356 9.266 9.348 9.287 [881] 9.446 9.286 9.304 9.116 8.964 8.940 8.916 9.020 [889] 9.125 9.045 9.071 9.131 9.071 9.116 9.330 9.286 [897] 9.338 9.215 9.189 9.311 9.343 9.359 9.373 9.129 [905] 9.411 9.251 9.334 9.357 9.347 9.344 9.342 9.468 [913] 9.484 9.446 9.423 9.411 9.424 9.090 8.907 8.741 [921] 8.730 8.691 8.894 9.065 9.057 9.095 9.002 8.962 [929] 8.844 8.933 9.015 9.175 8.867 9.106 8.990 8.758 [937] 8.641 8.650 9.015 9.006 8.908 9.010 9.204 8.972 [945] 8.955 9.100 9.125 9.024 8.944 9.136 9.151 8.923 [953] 8.911 9.232 9.538 9.020 9.141 9.354 9.129 9.053 [961] 9.229 9.163 9.255 9.171 9.199 8.951 9.205 9.105 [969] 9.478 9.283 9.001 8.790 9.126 9.269 9.256 8.950 [977] 8.858 9.015 8.844 9.015 9.092 8.943 9.012 8.963 [985] 9.045 9.074 8.741 8.740 8.602 8.619 8.613 8.766 [993] 8.521 9.064 8.882 8.992 8.849 9.087 9.060 9.076 [ reached getOption(“max.print”) – omitted 355 entries ] autoplot(gasoline(start = 1991, end = 2004)) Error in gasoline(start = 1991, end = 2004) : could not find function “gasoline” harmonic.regression(gasoline) Error in harmonic.regression(gasoline) : could not find function “harmonic.regression” harmonic.regression Error: object ‘harmonic.regression’ not found import harmonic. regression Error: unexpected symbol in “import harmonic.” harmonic(gasoline,2004) Error in harmonic(gasoline, 2004) : could not find function “harmonic” autoplot(gasoline) autoplot(gasoline, end = 2004) Warning: Ignoring unknown parameters: end autoplot(gasoline, end = 2004) Warning: Ignoring unknown parameters: end harmonic.regression(gasoline, end = 2004) Error in harmonic.regression(gasoline, end = 2004) : could not find function “harmonic.regression” four <- tsml(data = gasoline, end = 2004) Error in tsml(data = gasoline, end = 2004) : could not find function “tsml” four <- tslm(data = gasoline, end = 2004) Error in “formula” %in% class(formula) : argument “formula” is missing, with no default autoplot(gasoline, end = 2004) Warning: Ignoring unknown parameters: end autoplot(gasoline, 2004) y <- ts(gasoline, end = 2004) autoplot(y) # I read on the R website that there should be a package for the harmonic regression, and that all that I need to do is to use the command harmonic.regression; however, that did not work, so I created a ts and has it end in 2004. I then graphed this. I believe that this may also be another correct way to answer this quesiton. I see that for a while, there was a patter in the data, as the data increased for a good amount of time. However, then the data began to decrease for a short time, and then increase when reaching the year 2004. aelec <_ window(gasoline, end = 2004) Error: unexpected input in “aelec <_" aelec <- window(gasoline, end = 2004) autoplot(aelec) # I now actually believe that this above is the correct code to answer the question. So here you see that there actually is a pattern, and you see that there is overall a increase over time. However, you do see some points of a decrease. e <- tsCV(gasoline, end = 2004) sqrt(mean(e^2, na.rm = TRUE)) [1] NaN # I had trouble with this question. In chapter 3, the book mentions to use the tsCV() function. However, when I use the example in the text, I get that the number of Fourier terms are not available, if done correctly. checkresiduals(aelec) Warning message: In modeldf.default(object) : Could not find appropriate degrees of freedom for this model. # I see that there is somewhat of a pattern in the residuals, therefore meaning that the model is not adequete to the data. fc <- forecast(fit, newdata = data.frame(fourier(x,K,h))) Error in fourier(x, K, h) : object ‘h’ not found fc <- forecast(fit, newdata=data.frame(fourier(x,K,h))) Error in fourier(x, K, h) : object ‘h’ not found fc <- forecast(aelec, newdata=data.frame(fourier(x,K,h))) Error in etsmodel(y, errortype[i], trendtype[j], seasontype[k], damped[l], : unused argument (newdata = data.frame(fourier(x, K, h))) fc <- forecast(aelec, newdata = data.frame(fourier(NULL))) Error in etsmodel(y, errortype[i], trendtype[j], seasontype[k], damped[l], : unused argument (newdata = data.frame(fourier(NULL))) fc <- forecast(aelec, newdata = data.frame(fourier())) Error in etsmodel(y, errortype[i], trendtype[j], seasontype[k], damped[l], : unused argument (newdata = data.frame(fourier())) fc <- forecast(aelec, newdata = data.frame(fourier(x,4,5))) Error in etsmodel(y, errortype[i], trendtype[j], seasontype[k], damped[l], : unused argument (newdata = data.frame(fourier(x, 4, 5))) fc <- forecast(alec, newdata= data.frame(fourier(aelec))) Error in forecast(alec, newdata = data.frame(fourier(aelec))) : object ‘alec’ not found fc <- forecast(alec, newdata = data.frame(fourier(x,K,h))) Error in forecast(alec, newdata = data.frame(fourier(x, K, h))) : object ‘alec’ not found fc<- forecast(aelec, newdata = data.frame(fourier(x,K,h))) Error in etsmodel(y, errortype[i], trendtype[j], seasontype[k], damped[l], : unused argument (newdata = data.frame(fourier(x, K, h))) fc <-forecast(aelec, newdata = data.frame(fourier())) Error in etsmodel(y, errortype[i], trendtype[j], seasontype[k], damped[l], : unused argument (newdata = data.frame(fourier())) fc <-forecast(aelec, newdata = data.frame(fourier(NULL))) Error in etsmodel(y, errortype[i], trendtype[j], seasontype[k], damped[l], : unused argument (newdata = data.frame(fourier(NULL))) fc <- forecast(aelec, newdata = data.frame(fourier(aelec, K = 4, h =5))) Error in etsmodel(y, errortype[i], trendtype[j], seasontype[k], damped[l], : unused argument (newdata = data.frame(fourier(aelec, K = 4, h = 5))) fc <- forecast(aelec, newdata = data.frame(fourier(aelec))) Error in etsmodel(y, errortype[i], trendtype[j], seasontype[k], damped[l], : unused argument (newdata = data.frame(fourier(aelec))) fc <- forecast(aelec, newdata = data.frame(fourier(aelec))) Error in etsmodel(y, errortype[i], trendtype[j], seasontype[k], damped[l], : unused argument (newdata = data.frame(fourier(aelec))) # After multiple tries, I only get errors whenever I try to use this code. I must have made a mistake somewhere earlier. However, as the book states, this should be the correct code to use to forecast the next year of data. 2005 <- window(gasoline, end = 2005) Error in 2005 <- window(gasoline, end = 2005) : invalid (do_set) left-hand side to assignment five <- window(gasoline, end = 2005) autoplot(five) # I used the above command to find the forecast for the year 2005. I see that this upward trend that I saw when I plotted 2004 continues in the year 2005. autoplot(huron) # Here I see that there is no pattern that the data follows. From the year 1880 to the year 1925, we see that there is an overall downward trend with some upward peaks during some of those years. After that, there is no overall trend- there is an increase and then decrease sporadically. huron Time Series: Start = 1875 End = 1972 Frequency = 1 [1] 10.38 11.86 10.97 10.80 9.79 10.39 10.42 10.82 11.40 11.32 [11] 11.44 11.68 11.17 10.53 10.01 9.91 9.14 9.16 9.55 9.67 [21] 8.44 8.24 9.10 9.09 9.35 8.82 9.32 9.01 9.00 9.80 [31] 9.83 9.72 9.89 10.01 9.37 8.69 8.19 8.67 9.55 8.92 [41] 8.09 9.37 10.13 10.14 9.51 9.24 8.66 8.86 8.05 7.79 [51] 6.75 6.75 7.82 8.64 10.58 9.48 7.38 6.90 6.94 6.24 [61] 6.84 6.85 6.90 7.79 8.18 7.51 7.23 8.42 9.61 9.05 [71] 9.26 9.22 9.38 9.10 7.95 8.12 9.75 10.85 10.41 9.96 [81] 9.61 8.76 8.18 7.21 7.13 9.10 8.25 7.91 6.89 5.96 [91] 6.80 7.68 8.38 8.52 9.74 9.31 9.89 9.96 huron %>% + as.data.frame() %>% + geom_point() + geom_smooth(method = “lm”, se = FALSE) Error:
mappingmust be created byaes()huron %>% + as.data.frame() %>% + geom_point()+geom_smooth(method= “lm”, se=FALSE) Error:mappingmust be created byaes()e <- tslm(huron) Error in formula.default(object, env = baseenv()) : invalid formula e <- tslm(huron, h) Error in formula.default(object, env = baseenv()) : invalid formula e <- tslm(huron ~ trend, data = huron) forecast(e) Point Forecast Lo 80 Hi 80 Lo 95 Hi 95 1973 7.806127 6.317648 9.294605 5.516501 10.095752 1974 7.781926 6.292536 9.271315 5.490899 10.072952 1975 7.757724 6.267406 9.248043 5.465269 10.050179 1976 7.733523 6.242259 9.224788 5.439613 10.027434 1977 7.709322 6.217094 9.201550 5.413929 10.004715 1978 7.685121 6.191912 9.178331 5.388219 9.982024 1979 7.660920 6.166712 9.155128 5.362481 9.959359 1980 7.636719 6.141494 9.131943 5.336717 9.936721 1981 7.612518 6.116259 9.108776 5.310925 9.914110 1982 7.588317 6.091007 9.085626 5.285107 9.891526 autoplot(e) Error: Objects of type tslm/lm not supported by autoplot. ey <- ts(huron) autoplot(ey) h <- 1915 fit.line <- tslm(huron ~ trend) fcasts.lin <- forecast(fit.lin, h = h) Error in forecast(fit.lin, h = h) : object ‘fit.lin’ not found facsts.lin <- forecast(fit.line, h = h) fit.exp <- tslm(huron ~ trend, lambda = 0) fcasts.exp <-forecast(fit.exp, h = h) autoplot(fcasts.exp) # This is the code I found in 5.8 of the book to forecast using a piecewise linear trend model. I see here that we have an overall downward trend happening. This was not seen in the linear regression.
eyy <- forecast(ey, newdata = data.frame(ey), end = 1980) Error in etsmodel(y, errortype[i], trendtype[j], seasontype[k], damped[l], : unused arguments (newdata = data.frame(ey), end = 1980) forecast(ey, numbers = data.frame(ey, end = 1980)) Error in etsmodel(y, errortype[i], trendtype[j], seasontype[k], damped[l], : unused argument (numbers = data.frame(ey, end = 1980)) w <-forecast(ey, end = 1980) Error in etsmodel(y, errortype[i], trendtype[j], seasontype[k], damped[l], : unused argument (end = 1980) forecast(ey,newdata = data.frame(1980)) Error in etsmodel(y, errortype[i], trendtype[j], seasontype[k], damped[l], : unused argument (newdata = data.frame(1980)) eyy <- ts(huron, end = 1980) plot(forecast(eyy)) h <- 1980 fit.line <- tslm(huron ~ trend) fcasts.lin <- forecast(fit.lin, h = h) Error in forecast(fit.lin, h = h) : object ‘fit.lin’ not found fcasts.lin <- forecast(fit.line, h = h) fit.exp <- tslm(huron ~ trend, lambda = 0) fcasts. exp <- forecast(fit.exp, h = h) Error: unexpected symbol in “fcasts. exp” fcasts.exp <- forecast(fit.exp, h = h) plot(fcasts.exp) # I believe that this is the correct code for forecasting to the year 1980. The model for the linear regression shows me that data remains constant in the year 1980. The model for the piecewise shows me that there is a continued downward trend. solve((1/6) * 6(T)) Error in solve((1/6) * 6(T)) : attempt to apply non-function solve((1/6)6((1/2))) Error in solve((1/6) 6((1/2))) : attempt to apply non-function ((1/6)(6)(1/2)) [1] 0.5 ((1/6)(3)(1/2)(1/2+1)) Error: attempt to apply non-function (1/6)(3)(1/2)(1/2)+1 [1] 1.125 (1/6)(3)(1/2)(1/2)+1 [1] 1.125 (1/6)(1/2)(1/2)+1(2)(1/2)+1 [1] 2.041667 (2)/(1/2)(1/2)^2-1 (1/2)+(1)(2)(1/2)+1 Error: attempt to apply non-function (2)/(1/2)(1/2)(1/2)-(1) Error: attempt to apply non-function (2)/(1/2) [1] 4 (4)* (1/4) [1] 1 1-1 [1] 0 # So all of the values in question b will be zero because it is multiplied by zero… (2)/(1/2) [1] 4 (1/2)-1 [1] -0.5 4-0.5 [1] -2 (-2)(1/2)+1 Error: attempt to apply non-function -2 1/2 +1 [1] 0 # all will be zero because they are multiplied by zero (6)/(1/2) [1] 12 (12)* (1/4) -1 [1] 2 2 -(1/2)+1 [1] 2.5 # answer to d is 2.5 … (1)+(2)/(1/2)* (1/2)-1 [1] 2 2 * 1-4(1/2)-6(1)+6(1/2)+(1)/ (1/2)+1 [1] 0 # 2.5 is the second answer to c… # answer to d is zero… # Chapter 6 (1/15)(Y1) Error: attempt to apply non-function (1/15)(1/5) [1] 0.01333333 (1/15)(14/5) Error: attempt to apply non-function (1/15)(14/5) [1] 0.1866667 (1/15)(20/5) Error: attempt to apply non-function (1/15) (20/5) [1] 0.2666667 (0.067 -1)/3 [1] -0.311 (0.133 - 1)/3 [1] -0.289 (0.200 - 1)/3 [1] -0.2666667 (0.22 - 1)/3 [1] -0.26 (0.200 - 1)/3 [1] -0.2666667 (0.200 - 1)/ 3 [1] -0.2666667 (0.133 - 1)/ 3 [1] -0.289 (0.067 - 1)/3 [1] -0.311 # For this question, I followed part 6.2 of the book where I was given the equation for k. However, I had a base of three here as I has a different matrix. plastics %>% + as.data.frame() %>% + geom_point()+ geom_smooth(method= “lm”, se= FALSE) Error:mappingmust be created byaes()autoplot(plastics) # By plotting plastics, I can very much see that there are seasonal fluctuations of the sales. I can see that at certain times, the sales are much higher and then at certain times, the sales are much lower. plastics %>% decompose(type=“multiplicative”) %>% + autoplot() # Code for the classical multiplicative decomposition. # Yes, the results support the results from part a. Here, you can also see that the sales are higher and also lower at different times. library(seasonal) Error in library(seasonal) : there is no package called ‘seasonal’ library(seasonal) Error in library(seasonal) : there is no package called ‘seasonal’ library(seasonal) Error in library(seasonal) : there is no package called ‘seasonal’ plastis %>% seas(x11=“”)-> fit Error in eval(lhs, parent, parent) : object ‘plastis’ not found plastics %>% seas(x11=“”) -> fit Error in seas(., x11 = “”) : could not find function “seas” install.packages(“seasonal”) also installing the dependency ‘x13binary’
trying URL ‘https://cran.rstudio.com/bin/macosx/el-capitan/contrib/3.5/x13binary_1.1.39-1.tgz’ Content type ‘application/x-gzip’ length 2060207 bytes (2.0 MB) ================================================== downloaded 2.0 MB
trying URL ‘https://cran.rstudio.com/bin/macosx/el-capitan/contrib/3.5/seasonal_1.7.0.tgz’ Content type ‘application/x-gzip’ length 667365 bytes (651 KB) ================================================== downloaded 651 KB
The downloaded binary packages are in /var/folders/2w/_hjtt119213fxzpcnqbzh2pw0000gn/T//Rtmp5El8I9/downloaded_packages > plastics %>% seas(x11=“”)-> fit Error in seas(., x11 = “”) : could not find function “seas” > library(seasonal) > plastics %>% seas(x11=“”)-> fit Error in seas(., x11 = “”) : start year of ‘x’ must be > 999. > plastics%>% seas(x1000=“”)->fit Error in seas(., x1000 = “”) : start year of ‘x’ must be > 999. > plastics %>% seas(1000x=“”)-> fit Error: unexpected symbol in “plastics %>% seas(1000x” > plastics %>% seas(x11=“” ) -> fit Error in seas(., x11 = “”) : start year of ‘x’ must be > 999. > plastics %>% seas(1000,x11=“”)->fit Error in seas(., 1000, x11 = “”) : start year of ‘x’ must be > 999. > plastics %>5 seas(1000)-> Error: unexpected input in “plastics %>5 seas(1000)->” > plastics %>% seas(1000,x11=“”)->fit Error in seas(., 1000, x11 = “”) : start year of ‘x’ must be > 999. > autoplot(fit) Error: Objects of type tslm/lm not supported by autoplot. > # For part 6.4 of the book. The code for this part of the question should be: library(seasonal) plastics %>% seas(x11=“”)-> fit autoplot(fit). I type this in, but I only get errors. A model should appear too. > library(seasonal) > plastics %>% seas() %>% + autoplot() Error in seas(.) : start year of ‘x’ must be > 999. > # I get an error with this code as well. > plot(seasonadj(plastics)) Error in seasonadj(plastics) : could not find function “seasonadj” > plot(seasadj(plastics)) Error in UseMethod(“seasadj”) : no applicable method for ‘seasadj’ applied to an object of class “ts” > pl[82] = pl[82]+500 Error: object ‘pl’ not found > plastics %>% seas(1000+500,x11=“”) -> + autoplot() Error in seas(., 1000 + 500, x11 = “”) : start year of ‘x’ must be > 999. > plastics %>% seas(x=1000+500, x11=“”)->fit Error in seas(., x = 1000 + 500, x11 = “”) : ‘x’ argument is not a time series. > plastis %>% seas(start=1000+500,x11=“”)->fit Error in eval(lhs, parent, parent) : object ‘plastis’ not found > plastics%>% seas(start=1000+500, x11=“”)->fit Error in seas(., start = 1000 + 500, x11 = “”) : start year of ‘x’ must be > 999. > season <- seasadj(plastics) Error in UseMethod(“seasadj”) : no applicable method for ‘seasadj’ applied to an object of class “ts” > season <-seasonadj(plastics) Error in seasonadj(plastics) : could not find function “seasonadj” > season <-seasadj(plastics) Error in UseMethod(“seasadj”) : no applicable method for ‘seasadj’ applied to an object of class “ts” > plastics %>% seas(x=1000+500,x11=“”)->fit Error in seas(., x = 1000 + 500, x11 = “”) : ‘x’ argument is not a time series. > # I believe that this is the code to add 500 to one observation; however, it does not work for me, even after multiple tries. Therefore, I can’t see what the effect of the outlier is. > plastics %>% seasonal() Error in seasonal(.) : Unknown object type > plastics %>% seasonal() %>% ggsubseriesplot() Error in seasonal(.) : Unknown object type > # I do not think that it matters if the outlier is near the end rather than the middle as this outlier does not have a large effect on the understanding of the data in the model. > library(seasonal) > retaildata %>% seas(x11=“”)->fit Error in eval(lhs, parent, parent) : object ‘retaildata’ not found > retaildata Error: object ‘retaildata’ not found > library(readxl) > retaildata <- read_excel(“retail.xlsx”,skip=1) Error: path does not exist: ‘retail.xlsx’ > retaildara <-read_excel(“/Downloads/retail.xlsx“) New names:
* `` -> …1 > retaildata <-read_excel(”/Downloads/retail.xlsx”,skip =1)
retaildata %>% seas(x11=“”)->fit Error in seas(., x11 = “”) : ‘x’ argument is not a time series. myts <- ts(retaildata[,“A3349873A”], frequency = 12, start=c(1982,4)) retaildata %>% seas(x11=“”)->fit Error in seas(., x11 = “”) : ‘x’ argument is not a time series. retaildata %>% seasonal() %>% ggsubseriesplot() Error in seasonal(.) : Unknown object type # The code solve this question is: retaildata %>% seas(x11=“”)-> fit autoplot(fit). However, I get an error when I do this, and I cann’t fix it after several tries. So therefore, I can’t see if there are any outliers or unusual features. # The X11 decomposition captures the increase in the data, and most importantly shows the unusual observation that is seen right after 1990. The second model shows us that there are several unsual changes that happen over the time. A majority of them show a decrease/downward trend. # Yes the recession is very much visible in the first model, as I pointed it out above.
autoplot(cangas) ggsubseriesplot(cangas) ggseasonaplot(cangas) Error in ggseasonaplot(cangas) : could not find function “ggseasonaplot” ggseasonplot(cangas) # I think that the season/weather is what causes it to change so much. When it is cold in Canada, many people do not travel, and therefore there is no need to produce more gas. cangas %>% + stl(t.window=13,s.window=“periodic”,robust=TRUE)%>% + autoplot() # Above is the code for the STL decomposition. # Comparing the results, I see that in the model that I just created, there is a massive increase in the mid 1980s. Also, there is a solid patter in the seasonal model. # I do not see this in the first model that I created. bricksq %>% + stl(t.window=13, s.window =“periodic”, robust+TRUE) %>% + autoplot() # Above is the code for the STL decomposition of bricksq plot(seasadj(cangas)) Error in UseMethod(“seasadj”) : no applicable method for ‘seasadj’ applied to an object of class “ts” plot(seasonplot(cangas)) # I believe that this might be the code to plot the seasonally adjusted data. cangas %>% forecast(method = “naive”) %>% + autoplot() Error in etsmodel(y, errortype[i], trendtype[j], seasontype[k], damped[l], : unused argument (method = “naive”) canagas %>% forecast(method = “naive”) %>% + autoplot() Error in eval(lhs, parent, parent) : object ‘canagas’ not found cangas %>% forecast(method = “naive”) %>% + autoplot() Error in etsmodel(y, errortype[i], trendtype[j], seasontype[k], damped[l], : unused argument (method = “naive”) # The code to use a naive method, that does not work. fcast <- stlf(cangas, method = ‘naive’) plot(fcast) # code of the stlf() method, and then I plotted it. # The residuals do not look uncorrelated to me. As they all seem to follow the same pattern over time. plot(seasonadj(bricksq)) Error in seasonadj(bricksq) : could not find function “seasonadj” # I just saw that I was using the data from the previous question by accident. bricksq %>% forecast(method = “naive”) %>% + autoplot() Error in etsmodel(y, errortype[i], trendtype[j], seasontype[k], damped[l], : unused argument (method = “naive”) # The code for the bricksq data, for the naive method, doesn’t work fcast <- stlf(bricksq, method= ‘naive’) plot(fcast) # At certain parts, I can saw that the residuals look uncorrelated as there are random periods of decrease in the model. bricksq %>% + + stl(t.window=13, s.window =“periodic”, robust+TRUE) %>% + + autoplot() Error in as.ts(x) : object ‘robust’ not found bricksq %>% + stl(t.window = 13, s.window = “periodic”, robust= TRUE) %>% autoplot() Error in as.ts(x) : argument “x” is missing, with no default # Code for the STL decomposition, that does not work. Because of this, I can’t tell if there is a difference. fit <- stl(bricksq, t.window=13, s.window = “bricks”, robust= TRUE) Error in stl(bricksq, t.window = 13, s.window = “bricks”, robust = TRUE) : unknown string value for s.window fit <- stl(bricksq, t.window=13, robust = TRUE) Error in stl(bricksq, t.window = 13, robust = TRUE) : argument “s.window” is missing, with no default fit<- stl(bricksq, t.window=13, s.window= “periodic”, robust = TRUE) fit %>% seasadj() %>% naive() %>% + autoplot() # It looks like I figured out the stl() code. fit %>% forecast(method = “naive”) %>% + autoplot() # Overall, I think the naive plot is more precise, I see the downward trends more clearly in this model. So the naive forecast is better to me. writing Jan Feb Mar Apr May Jun 1968 562.674 599.000 668.516 597.798 579.889 668.233 1969 634.712 639.283 712.182 621.557 621.000 675.989 1970 646.783 658.442 712.906 687.714 723.916 707.183 1971 676.155 748.183 810.681 729.363 701.108 790.079 1972 747.636 773.392 813.788 766.713 728.875 749.197 1973 795.337 788.421 889.968 797.393 751.000 821.255 1974 843.038 847.000 941.952 804.309 840.307 871.528 1975 778.139 856.075 938.833 813.023 783.417 828.110 1976 895.217 856.075 893.268 875.000 835.088 934.595 1977 875.024 992.968 976.804 968.697 871.675 1006.852 Jul Aug Sep Oct Nov Dec 1968 499.232 215.187 555.813 586.935 546.136 571.111 1969 501.322 220.286 560.727 602.530 626.379 605.508 1970 629.000 237.530 613.296 730.444 734.925 651.812 1971 594.621 230.716 617.189 691.389 701.067 705.777 1972 680.954 241.424 680.234 708.326 694.238 772.071 1973 691.605 290.655 727.147 868.355 812.390 799.556 1974 656.330 370.508 742.000 847.152 731.675 898.527 1975 657.311 310.032 780.000 860.000 780.000 807.993 1976 832.500 300.000 791.443 900.000 781.729 880.000 1977 832.037 345.587 849.528 913.871 868.746 993.733 fit <- stl(writing, t.window = 13, s.window = “periodic”, robust = TRUE) fit %>% seasadj() %>% naive () %>% + autoplot() fit %>% forecast(method = “naive”) %>% + autoplot() fit %>% seasadj() %>% rwdrift() %>% + autoplot() Error in rwdrift(.) : could not find function “rwdrift” fit %>% seasadj() %>% naive() %>% + autoplot() fit %>% forecast(method = “rwdrift”) %>% + autoplot() # I would say that the method = “rwdrift” is the most appropriate to use. The results here are very clear, and there is more results shown of the forecat and the random walk with a drift wow <- stl(fancy, t.window= 13, s.window=“periodic”, robust = TRUE) fit %>% seasj() %>% naive() %>% + autoplot() Error in seasj(.) : could not find function “seasj” wow <- stl(fancy, t.window =13, s.window = “periodic”, robust = TRUE) fit %>% seasadj() %>% naive() %>% + autoplot() wow %>% forecast(method = “naive”) %>% + autoplot() wow %>% forecast(method = “rwdrift”) %>% + autoplot() Error in paste(class(object), collapse = “/”) : argument “object” is missing, with no default wow %>% forecast(method=“rwdrift”) %>% + autoplot() Error in paste(class(object), collapse = “/”) : argument “object” is missing, with no default wow %>% forecast(method=“rwdrift”) %>% + autoplot() # Even though I think that they both look very similar, I still think that the random walk with a drift is the better one to use. Overall, the upward trends are more clear and the random walk with a drift is more clear as well.