library(fpp)
## Loading required package: forecast
## Warning: package 'forecast' was built under R version 3.4.4
## Loading required package: fma
## Loading required package: expsmooth
## Loading required package: lmtest
## Loading required package: zoo
##
## Attaching package: 'zoo'
## The following objects are masked from 'package:base':
##
## as.Date, as.Date.numeric
## Loading required package: tseries
## Warning: package 'tseries' was built under R version 3.4.4
setwd("/Users/brendanobrien/Downloads")
Barley.data=read.csv("/Users/brendanobrien/Downloads/annual-barley-yields-per-acre-in.csv", header = T)
Barley.data$Year = NULL
barley.ts<- ts(data = Barley.data, start = 1884, end = 1939, frequency = 4, ts.eps = getOption("ts.eps"), class ="ts")
as.ts(barley.ts)
## Qtr1 Qtr2 Qtr3 Qtr4
## 1884 15.2 16.9 15.3 14.9
## 1885 15.7 15.1 16.7 16.3
## 1886 16.5 13.3 16.5 15.0
## 1887 15.9 15.5 16.9 16.4
## 1888 14.9 14.5 16.6 15.1
## 1889 14.6 16.0 16.8 16.8
## 1890 15.5 17.3 15.5 15.5
## 1891 14.2 15.8 15.7 14.1
## 1892 14.8 14.4 15.6 13.9
## 1893 14.7 14.3 14.0 14.5
## 1894 15.4 15.3 16.0 16.4
## 1895 17.2 17.8 14.4 15.0
## 1896 16.0 16.8 16.9 16.6
## 1897 16.2 14.0 18.1 17.5
## 1898 NA 15.2 16.9 15.3
## 1899 14.9 15.7 15.1 16.7
## 1900 16.3 16.5 13.3 16.5
## 1901 15.0 15.9 15.5 16.9
## 1902 16.4 14.9 14.5 16.6
## 1903 15.1 14.6 16.0 16.8
## 1904 16.8 15.5 17.3 15.5
## 1905 15.5 14.2 15.8 15.7
## 1906 14.1 14.8 14.4 15.6
## 1907 13.9 14.7 14.3 14.0
## 1908 14.5 15.4 15.3 16.0
## 1909 16.4 17.2 17.8 14.4
## 1910 15.0 16.0 16.8 16.9
## 1911 16.6 16.2 14.0 18.1
## 1912 17.5 NA 15.2 16.9
## 1913 15.3 14.9 15.7 15.1
## 1914 16.7 16.3 16.5 13.3
## 1915 16.5 15.0 15.9 15.5
## 1916 16.9 16.4 14.9 14.5
## 1917 16.6 15.1 14.6 16.0
## 1918 16.8 16.8 15.5 17.3
## 1919 15.5 15.5 14.2 15.8
## 1920 15.7 14.1 14.8 14.4
## 1921 15.6 13.9 14.7 14.3
## 1922 14.0 14.5 15.4 15.3
## 1923 16.0 16.4 17.2 17.8
## 1924 14.4 15.0 16.0 16.8
## 1925 16.9 16.6 16.2 14.0
## 1926 18.1 17.5 NA 15.2
## 1927 16.9 15.3 14.9 15.7
## 1928 15.1 16.7 16.3 16.5
## 1929 13.3 16.5 15.0 15.9
## 1930 15.5 16.9 16.4 14.9
## 1931 14.5 16.6 15.1 14.6
## 1932 16.0 16.8 16.8 15.5
## 1933 17.3 15.5 15.5 14.2
## 1934 15.8 15.7 14.1 14.8
## 1935 14.4 15.6 13.9 14.7
## 1936 14.3 14.0 14.5 15.4
## 1937 15.3 16.0 16.4 17.2
## 1938 17.8 14.4 15.0 16.0
## 1939 16.8
#found NA's
require("imputeTS")
## Loading required package: imputeTS
## Warning: package 'imputeTS' was built under R version 3.4.4
##
## Attaching package: 'imputeTS'
## The following object is masked from 'package:tseries':
##
## na.remove
## The following object is masked from 'package:zoo':
##
## na.locf
#impute NA's using "imputeTS" package
library(imputeTS)
newbarley<-na.interpolation(barley.ts)
#data is time series
str(newbarley)
## Time-Series [1:221] from 1884 to 1939: 15.2 16.9 15.3 14.9 15.7 15.1 16.7 16.3 16.5 13.3 ...
#variation seems to be constant over time
plot(newbarley)

#seasonal
mydec=decompose(newbarley, type = "additive")
plot(mydec)

seasonplot(barley.ts, col = seq(1:221), main = "Season Plot", year.labels = TRUE)

mydec2=decompose(newbarley, type = "multiplicative")
plot(mydec2)
