2. Importing the data

livestock <- read.csv("Livestock.csv")



3. Converting data into time series

#Converting data for Cattle as time Series
cattle_ts <- ts(livestock$Cattle,
                start = c(2000,1),
                end = c(2022,4),
                frequency = 4)

#Converting data for Hog as time Series
hog_ts <- ts(livestock$Hog,
                start = c(2000,1),
                end = c(2022,4),
                frequency = 4)

#Converting data for Goat as time Series
goat_ts <- ts(livestock$Goat,
                start = c(2000,1),
                end = c(2022,4),
                frequency = 4)



4. Creating Time series plot

#plot for Cattle time series
plot(cattle_ts,
     type="o",
     xlab="Year",
     ylab="Quarterly Production",
     main="Quarterly Production of Cattles: 2000-2022")


The time series plot of the quarterly production of cattle displays seasonality wherein it peaks at the 3rd or 4th quarter from the year 2000 until 2010. However for the latter years, the peak has shifted to the 1st or 2nd quarter.

#plot for Hogs time series
plot(hog_ts,
     type="o",
     xlab="Year",
     ylab="Quarterly Production",
     main="Quarterly Production of Hogs: 2000-2022")


The time series plot of the quarterly production of hogs displays seasonality with it peaking during the 4th quarter of the year except for the years after 2020 wherein there is a much longer increase and decrease in the production.

#plot for Goat time series
plot(goat_ts,
     type="o",
     xlab="Year",
     ylab="Quarterly Production",
     main="Quarterly Production of Goats: 2000-2022")


The quarterly production of goats exhibits a somewhat downward linear trend which is prominent after 2005.

5. Computing lag-1 and lag-2 of each series and plotting

#cattle
c.lag1 <-stats::lag(cattle_ts, k=-1)
c.lag2 <- stats::lag(cattle_ts,k=-2)
plot.ts(cbind(cattle_ts,c.lag1,c.lag2),
        plot.type = "single",
        col= c("grey","blue","green"),
        xlab="Year",
        ylab="Quarterly Production",
        main="Quarterly Production of Cattles: 2000-2022")


#hog
h.lag1 <-stats::lag(hog_ts, k=-1)
h.lag2 <- stats::lag(hog_ts,k=-2)
plot.ts(cbind(hog_ts,h.lag1,h.lag2),
        plot.type = "single",
        col= c("grey","blue","green"),
        xlab="Year",
        ylab="Quarterly Production",
        main="Quarterly Production of Hogs: 2000-2022")

#goat
g.lag1 <-stats::lag(goat_ts, k=-1)
g.lag2 <- stats::lag(goat_ts,k=-2)
plot.ts(cbind(goat_ts,g.lag1,g.lag2),
        plot.type = "single",
        col= c("grey","blue","green"),
        xlab="Year",
        ylab="Quarterly Production",
        main="Quarterly Production of Goats: 2000-2022")



6. Computing first- and second- differences

#cattle
c.dif1 <- diff(cattle_ts, lag=1)
c.dif2 <- diff(c.dif1, lag=1)

plot.ts(cbind(cattle_ts, c.dif1, c.dif2),
        plot.type = "single",
        col=c("grey","red","purple"),
        xlab="Year",
        ylab="Quarterly Production",
        main="Quarterly Production of Cattles: 2000-2022")


After applying differencing, the plot seems relatively stationary.

#hog
h.dif1 <- diff(hog_ts, lag=1)
h.dif2 <- diff(h.dif1, lag=1)

plot.ts(cbind(hog_ts, h.dif1, h.dif2),
        plot.type = "single",
        col=c("grey","red","purple"),
        xlab="Year",
        ylab="Quarterly Production",
        main="Quarterly Production of Hogs: 2000-2022")


After applying differencing, the plot seems relatively stationary.

#goat
g.dif1 <- diff(goat_ts, lag=1)
g.dif2 <- diff(g.dif1, lag=1)

plot.ts(cbind(goat_ts, g.dif1, g.dif2),
        plot.type = "single",
        col=c("grey","red","purple"),
        xlab="Year",
        ylab="Quarterly Production",
        main="Quarterly Production of Goats: 2000-2022")


After applying differencing, the plot seems relatively stationary.



7. Computing 3-quarter and 5-quarter moving averages

#cattle
c.ma3 <- ma(cattle_ts, order = 3)
c.ma5 <- ma(cattle_ts, order = 5)

plot.ts(cbind(cattle_ts,c.ma3, c.ma5),
        plot.type = "single",
        col=c("grey","magenta","orange"),
        xlab="Year",
        ylab="Quarterly Production",
        main="Quarterly Production of Cattles: 2000-2022")


The moving averages has somewhat smoothed the time series.

#hog
h.ma3 <- ma(hog_ts, order = 3)
h.ma5 <- ma(hog_ts, order = 5)

plot.ts(cbind(hog_ts,h.ma3, h.ma5),
        plot.type = "single",
        col=c("grey","magenta","orange"),
        xlab="Year",
        ylab="Quarterly Production",
        main="Quarterly Production of Hogs: 2000-2022")


The moving averages has somewhat smoothed the time series.

#goat
g.ma3 <- ma(goat_ts, order = 3)
g.ma5 <- ma(goat_ts, order = 5)

plot.ts(cbind(goat_ts,g.ma3, g.ma5),
        plot.type = "single",
        col=c("grey","magenta","orange"),
        xlab="Year",
        ylab="Quarterly Production",
        main="Quarterly Production of Goats: 2000-2022")


The moving averages has somewhat smoothed the time series.