Package

library(tidyverse)
library(forecast)
library(timetk)

Data

setwd("~/Jasmine")
live<- read.csv("Livestock.csv")

Time series

Cattle live stock

catts<-ts(live$Cattle,
              start = c(2000,1),
              end = c(2022,4),
              frequency = 4)

plot(catts,
     type = "o",
     xlab = "Year",
     ylab = "Cattle Livestock",
     main = "Quarterly Cattle Livestock Production:2000-2022")

Explanation: The time series of cattle seem to have a seasonality series component wherein the livestock increases at specified quarter. The highest increase of cattle livestock was on the 3rd and 4th quarter of the year 2004.

Hogs Livestock

hogts<-ts(live$Hog,
              start = c(2000,1),
              end = c(2022,4),
              frequency = 4)

plot(hogts,
     type = "o",
     xlab = "Year",
     ylab = "Hogs Production",
     main = "Quarterly Hog Livestock Production:2000-2022")

Explanation: The time series of hog seem to have a seasonality series component wherein the livestock increases at specified quarter. The highest increase of hog livestock was on the 3rd quarter of the year 2006.

Goat Livestock

gots<-ts(live$Goat,
              start = c(2000,1),
              end = c(2022,4),
              frequency = 4)

plot(gots,
     type = "o",
     xlab = "Year",
     ylab = "Goat Production",
     main = "Quarterly Goat Livestock Production:2000-2022")

Explanation: The time series of goat livestock seem to have a seasonality series with downward trend series component wherein the seasonality series of Hogs livestock production started to decrease on the year 2005 up to the year of 2022.

Lag operators

#Cattle Lag

cat.lag1 <- stats::lag(catts, k = -1) 
cat.lag2 <- stats::lag(catts, k = -2) 
catt<-cbind(catts,cat.lag1,cat.lag2)
head(catt)
##         catts cat.lag1 cat.lag2
## 2000 Q1   662       NA       NA
## 2000 Q2   766      662       NA
## 2000 Q3   980      766      662
## 2000 Q4   967      980      766
## 2001 Q1   608      967      980
## 2001 Q2   668      608      967
plot.ts(catt,
        plot.type = 'single', 
        col = c('black', 'blue',"orange"))

Explanation: The time series of lag-1 and lag-2 of cattle livestock to the original series have not much difference in terms of their presence of series component, however observations have move forward by one and two quarter every year.

Hog Lag

hog.lag1 <- stats::lag(hogts, k = -1) 
hog.lag2 <- stats::lag(hogts, k = -2) 
hog<-cbind(hogts,hog.lag1,hog.lag2)
head(hog)
##         hogts hog.lag1 hog.lag2
## 2000 Q1 22334       NA       NA
## 2000 Q2 25632    22334       NA
## 2000 Q3 23336    25632    22334
## 2000 Q4 28790    23336    25632
## 2001 Q1 21740    28790    23336
## 2001 Q2 25376    21740    28790
plot.ts(hog, 
        plot.type = 'single', 
        col = c('black', 'blue',"orange"))

Explanation: The time series of lag-1 and lag-2 of hog livestock to the original series have not much difference in terms of their presence series component, however observations have move forward by one and two quarter every year.

Goat lag

go.lag1 <- stats::lag(gots, k = -1) 
go.lag2 <- stats::lag(gots, k = -2) 
goat<-cbind(gots,go.lag1,go.lag2)
head(goat)
##         gots go.lag1 go.lag2
## 2000 Q1  352      NA      NA
## 2000 Q2  572     352      NA
## 2000 Q3  582     572     352
## 2000 Q4  494     582     572
## 2001 Q1  387     494     582
## 2001 Q2  556     387     494
plot.ts(goat, 
        plot.type = 'single', 
        col = c('black', 'blue',"orange"))

Explanation: The time series of lag-1 and lag-2 of Goat livestock to the original series have not much difference in terms of their present series component, however observations have move forward by one and two quarter every year.

Difference operator

cattle difference

catdif1<-diff(catts,lag = 1)#to compute the first difference
catdif2<-diff(catdif1,lag = 1)#to compute the 2nd difference
catdif<-cbind(catts,catdif1,catdif2)
head(catdif)
##         catts catdif1 catdif2
## 2000 Q1   662      NA      NA
## 2000 Q2   766     104      NA
## 2000 Q3   980     214     110
## 2000 Q4   967     -13    -227
## 2001 Q1   608    -359    -346
## 2001 Q2   668      60     419
plot.ts(catdif, 
        plot.type = 'single', 
        col = c('black', 'blue',"violet"))

Explanation: The time series plot of cattle with 1st and 2nd difference remove the series component present in the original time series.

Hog diff

hogdif1<-diff(hogts,lag = 1)#to compute the first difference
hogdif2<-diff(hogdif1,lag = 1)#to compute the 2nd difference
hogdif<-cbind(hogts,hogdif1,hogdif2)
head(hogdif)
##         hogts hogdif1 hogdif2
## 2000 Q1 22334      NA      NA
## 2000 Q2 25632    3298      NA
## 2000 Q3 23336   -2296   -5594
## 2000 Q4 28790    5454    7750
## 2001 Q1 21740   -7050  -12504
## 2001 Q2 25376    3636   10686
plot.ts(hogdif, 
        plot.type = 'single', 
        col = c('black', 'blue',"violet"))

Explanation: The time series plot of Hogs with 1st and 2nd difference remove the series component present in the original time series.

Goat diff

godif1<-diff(gots,lag = 1)#to compute the first difference
godif2<-diff(godif1,lag = 1)#to compute the 2nd difference
godif<-cbind(gots,godif1,godif2)
head(godif)
##         gots godif1 godif2
## 2000 Q1  352     NA     NA
## 2000 Q2  572    220     NA
## 2000 Q3  582     10   -210
## 2000 Q4  494    -88    -98
## 2001 Q1  387   -107    -19
## 2001 Q2  556    169    276
plot.ts(godif, 
        plot.type = 'single', 
        col = c('black', 'blue',"violet"))

Explanation: The time series plot of Goat with 1st and 2nd difference remove the series component present in the original time series.

Moving Average

Cattle MA

cat.ma3<-ma(catts, order=3) #computes 3-quarter MA
cat.ma5<-ma(catts, order=5) #computes 5-quarter MA
catma<- cbind(catts, cat.ma3,cat.ma5)
head(catma)
##         catts  cat.ma3 cat.ma5
## 2000 Q1   662       NA      NA
## 2000 Q2   766 802.6667      NA
## 2000 Q3   980 904.3333   796.6
## 2000 Q4   967 851.6667   797.8
## 2001 Q1   608 747.6667   824.8
## 2001 Q2   668 725.6667   812.4
plot.ts(cbind(catts, cat.ma3,cat.ma5), 
        plot.type = 'single', 
        col = c('black', 'red','blue'))

Explanation: The plot of cattle moving average have shorten or lessen the spike which smoothed the time series.

Hog MA

hog.ma3<-ma(hogts, order=3) #computes 3-quarter MA
hog.ma5<-ma(hogts, order=5) #computes 5-quarter MA
hogma<-cbind(hogts,hog.ma3,hog.ma5)
head(hogma)
##         hogts  hog.ma3 hog.ma5
## 2000 Q1 22334       NA      NA
## 2000 Q2 25632 23767.33      NA
## 2000 Q3 23336 25919.33 24366.4
## 2000 Q4 28790 24622.00 24974.8
## 2001 Q1 21740 25302.00 24855.6
## 2001 Q2 25376 24050.67 26424.4
plot.ts(cbind(hogts,hog.ma3,hog.ma5), 
        plot.type = 'single', 
        col = c('black', 'red','blue'))

Expalanation: The plot of hog moving average have shorten or lessen the spike which smoothed the time series.

Goat MA

go.ma3<-ma(gots, order=3) #computes 3-quarter MA
go.ma5<-ma(gots, order=5) #computes 5-quarter MA
goma<-cbind(gots,go.ma3,go.ma5)
plot.ts(cbind(gots,go.ma3,go.ma5), 
        plot.type = 'single', 
        col = c('black', 'red','blue'))

Explanation: The plot of goat moving average have shorten or lessen the spike which smoothed the time series