# 6. The data file winnebago contains monthly unit sales of recreational vehicles from
# Winnebago, Inc., from November 1966 through February 1972.
# a) Display and interpret the time series plot for these data.
# b) Use least squares to fit a line to these data. Interpret the regression output.
# Plot the standardized residuals from the fit as a time series. Interpret the plot.
# c) Now take natural logarithms of the monthly sales figures and display and inter-
# pret the time series plot of the transformed values.
# d) Use least squares to fit a line to the logged data. Display and interpret the time
# series plot of the standardized residuals from this fit.
# e) Perform a runs test and Shapiro-Wilk test on the standardized residuals and
# interpret the results.
# f) Calculate and interpret the sample autocorrelations for the standardized residu-
# als.
# h) Investigate the normality of the standardized residuals (error terms). Consider
# histograms and normal probability plots. Interpret the plots.
#6a
library(TSA)
##
## Attaching package: 'TSA'
## The following objects are masked from 'package:stats':
##
## acf, arima
## The following object is masked from 'package:utils':
##
## tar
data(winnebago)
plot(winnebago,ylab='Monthly Sales',type='l',col='blue',lwd=2)
points(y=winnebago,x=time(winnebago), pch=as.vector(season(winnebago)), col='red',lwd=2)

#6b
rmw1=lm(winnebago~time(winnebago))
summary(rmw1)
##
## Call:
## lm(formula = winnebago ~ time(winnebago))
##
## Residuals:
## Min 1Q Median 3Q Max
## -419.58 -93.13 -12.78 94.96 759.21
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) -394885.68 33539.77 -11.77 <2e-16 ***
## time(winnebago) 200.74 17.03 11.79 <2e-16 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 209.7 on 62 degrees of freedom
## Multiple R-squared: 0.6915, Adjusted R-squared: 0.6865
## F-statistic: 138.9 on 1 and 62 DF, p-value: < 2.2e-16
par(mfrow=c(1,2))
plot(winnebago,type="o",lwd=2,col="blue")
abline(rmw1,lwd="2",col="red")
plot(y=rstudent(rmw1),x=as.vector(time(winnebago)),main='Standardized Residuals of Winnebago',xlab='Time',ylab='Standardized Residuals',type='o',lwd=2,col="blue",cex.main=0.8)

#6c
lw=log(winnebago)
plot.ts(lw, main="Time Series Plot of log of sales",lwd=2,col="blue",cex.main=0.8)
#6d
rmw2=lm(lw~time(lw))
summary(rmw2)
##
## Call:
## lm(formula = lw ~ time(lw))
##
## Residuals:
## Min 1Q Median 3Q Max
## -1.03669 -0.20823 0.04995 0.25662 0.86223
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) -984.93878 62.99472 -15.63 <2e-16 ***
## time(lw) 0.50306 0.03199 15.73 <2e-16 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 0.3939 on 62 degrees of freedom
## Multiple R-squared: 0.7996, Adjusted R-squared: 0.7964
## F-statistic: 247.4 on 1 and 62 DF, p-value: < 2.2e-16
par(mfrow=c(1,2))

plot(lw,type='o',lwd=2,col='purple')
abline(rmw2,lwd=2,col='orange')
plot(y=rstudent(rmw2),x=as.vector(time(lw)),main='Standardized Residuals of log(Sales)',xlab='Time',ylab='Standardized Residuals',type='o',lwd=2,col="purple",cex.main=.8,)

#6e
runs(rstudent(rmw2))
## $pvalue
## [1] 2.97e-05
##
## $observed.runs
## [1] 16
##
## $expected.runs
## [1] 32.5
##
## $n1
## [1] 28
##
## $n2
## [1] 36
##
## $k
## [1] 0
shapiro.test(rstudent(rmw2))
##
## Shapiro-Wilk normality test
##
## data: rstudent(rmw2)
## W = 0.97912, p-value = 0.3498
#6f
acf(rstudent(rmw2),plot=FALSE)
##
## Autocorrelations of series 'rstudent(rmw2)', by lag
##
## 1 2 3 4 5 6 7 8 9 10 11
## 0.581 0.308 0.118 -0.099 -0.186 -0.210 -0.227 -0.102 0.076 0.210 0.250
## 12 13 14 15 16 17 18
## 0.293 0.106 0.005 -0.134 -0.273 -0.336 -0.295
acf(rstudent(rmw2),plot=T, col='blue',lwd=2)
#6g
par(mfrow=c(1,2))

hist(rstudent(rmw2),xlab='Standardized Residuals',lwd=2,cex.main=.8)
qqnorm(rstudent(rmw2) , lwd=2,col="dark turquoise" ,cex.main=.8)
qqline(rstudent(rmw2), lwd=2,col = "hot pink" )
