Skip to content

Commit

Permalink
Major push of all files
Browse files Browse the repository at this point in the history
  • Loading branch information
HPCurtis committed Jun 21, 2024
1 parent 20e7bfe commit 5aee824
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 28 deletions.
101 changes: 75 additions & 26 deletions R/data_vis.r
Original file line number Diff line number Diff line change
@@ -1,45 +1,94 @@
library(ggplot2)
library(fable)

#Import data----
# Import data from github repo.
df_og <- read.csv("https://raw.githubusercontent.com/HPCurtis/causalcovidcattle/main/data/cattle_og.csv")
df_sa <- read.csv("https://raw.githubusercontent.com/HPCurtis/causalcovidcattle/main/data/cattle_sa.csv")

df_og$Date <- as.Date(df_og$Date)
df_sa$Date <- as.Date(df_sa$Date)
df_og$Date <- as.Date(df_og$Date)
df_sa$Date <- as.Date(df_sa$Date)

df_og <- df_og[,1:2] |> mutate(Date = yearmonth(Date)) |>
#Pre/Post Covid----
pre_covid <- df_sa %>%
drop_na() %>%
filter(Date < ymd("2020-03-01")) %>%
mutate(Date = yearquarter(Date)) %>%
as_tsibble( index = Date)

df_sa <- df_sa[,1:2] |> mutate(Date = yearmonth(Date)) |>
as_tsibble(index = Date)
post_covid <- df_sa %>%
drop_na() %>%
filter(Date >= ymd("2020-03-01") )%>%
mutate(Date = yearquarter(Date)) %>%
as_tsibble( index = Date)

# Visualisations for the project

# Visualise timeseries oforiginal total number of cattle slaughtered.
ggplot(df_og, aes(x = Date, y = NumberSlaughteredCATTLEexclcalvesTotalState)) +
geom_line(color = "blue") +
labs(title = "Total number of slaughtered cattle (excl calves) across all Australian states",
x = "Date",
y = "Number of Slaughtered Cattle") +
theme_minimal()
#Time series Decomposition------
# Conduct STL decomposition.
dcmp <- drop_na(df_sa) |>
model(stl = STL(NumberSlaughteredCATTLEexclcalvesTotalState))

components(dcmp) |> autoplot()

# Visualise timeseries of seasonally adjsuted total number of cattle slaughtered.
ggplot(df_sa, aes(x = Date, y = NumberSlaughteredCATTLEexclcalvesTotalState)) +
geom_line(color = "blue") +
labs(title = "Total number of slaughtered cattle (excl calves) across all Australian states (seasonally adjusted",
x = "Date",
y = "Number of Slaughtered Cattle") +
theme_minimal()

# Fit forecast model for seasonaly adjusted model.
fit <- df_og |>
# Fit forecast model for seasonal adjusted model.
fitlinear <- pre_covid |>
model(trend_model = TSLM(NumberSlaughteredCATTLEexclcalvesTotalState ~ trend()))

fit |> forecast(h = 10) |> autoplot(df_og)
fc <- fitlinear %>%
forecast(h = nrow(post_covid)) %>%
# Get forecast intervals
hilo()

# Fit forecast model for seasonaly adjusted model.
fitlinear <- df_sa |>
model(trend_model = TSLM(NumberSlaughteredCATTLEexclcalvesTotalState ~ trend()))
fc$Date <- as_date(fc$Date)

plinear <- fc %>% autoplot(drop_na(df_sa)) +
labs(y = "Number of Cattle Slaughtered",
title = "Total number of cattle (excl calves) across all Australian States") +
theme(
plot.title = element_text(size = 10), # Title font size
axis.title = element_text(size = 8), # Axis titles font size
axis.text = element_text(size = 7) # Axis text font size
) + geom_vline(xintercept = as.Date("2020-03-01"))

# Save plot out
ggsave(filename = "/home/harrison/Desktop/gitHubRepos/cattlecovidcausal/img/linearforecast.png", plot = plinear, width = 6, height = 4, units = "in", dpi = 300)

#Calculate Causal impact----
yhat <- fc$.mean
yhatupper <- fc$`95%`$upper
yhatlower <- fc$`95%`$lower

# Clauate mean and lower and upper bounds
Totalslaughteredimpact <- post_covid$NumberSlaughteredCATTLEexclcalvesTotalState - yhat
Totalslaughteredimpactupper <- post_covid$NumberSlaughteredCATTLEexclcalvesTotalState - yhatupper
Totalslaughteredimpactlower <- post_covid$NumberSlaughteredCATTLEexclcalvesTotalState - yhatlower

# Estimate impact of Covid over the forecast-able period.
totalmean <- sum(Totalslaughteredimpact)
totalupper <- sum(Totalslaughteredimpactupper)
totallower <- sum(Totalslaughteredimpactlower)

# PLot of causal impact

causal_impact_plot <- ggplot() +
geom_line(data = drop_na(df_sa), aes(x = Date, y = NumberSlaughteredCATTLEexclcalvesTotalState), color = "black") +
geom_ribbon(data = fc,
aes(x = Date, ymin = post_covid$NumberSlaughteredCATTLEexclcalvesTotalState),
ymax = yhat, fill = "blue", alpha = 0.2) +
labs(y = "Number of Cattle Slaughtered",
title = "Total number of cattle (excl calves) across all Australian States") +
theme(
plot.title = element_text(size = 10), # Title font size
axis.title = element_text(size = 8), # Axis titles font size
axis.text = element_text(size = 7) # Axis text font size
) +
geom_vline(xintercept = as.Date("2020-03-01"), color = "red", linetype = "dashed") +
theme_minimal()

plinear <-fitlinear |> forecast(h = 10) |> autoplot(df_sa)
# Save plots out-----
ggsave(filename = "/home/harrison/Desktop/gitHubRepos/cattlecovidcausal/img/linearforecast.png",
plot = plinear, width = 6, height = 4, units = "in", dpi = 300)
ggsave(filename = "/home/harrison/Desktop/gitHubRepos/cattlecovidcausal/img/causal_impact.png",
plot = causal_impact_plot, width = 6, height = 4, units = "in", dpi = 300)
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
# Causal impact of Covid on Australian Cattle livestock slaughter numbers

The following analysis is investigation into whether Covid had any potential causal impact of Covid on the Australian livestock market. Specifically, the analysis here uses an interupted time-series quasi-experimental methodology to whether the number of cattle slaughtered was impacted by the Covid-19 pandemic. The following work is highly inspired by the work of [Rami Kasparin](https://ramikrispin.github.io/2021/01/covid19-effect/), [Matheus Facure](https://matheusfacure.github.io/python-causality-handbook/landing-page.html) and the work of [CausalPy and all its developers](https://causalpy.readthedocs.io/en/stable/examples.html#interrupted-time-series) and generally [Hyndman & Athanasopoulos](https://otexts.com/fpp3/) incredible book and associated timeseries analysis packages that they developed thsat are simple brilliant.
The following analysis is investigation into whether Covid had any potential causal impact of Covid on the Australian livestock market. Specifically, truethe analysis here uses an interupted time-series quasi-experimental methodology to whether the number of cattle slaughtered was impacted by the Covid-19 pandemic. The following work is highly inspired by the work of [Rami Kasparin](https://ramikrispin.github.io/2021/01/covid19-effect/), [Matheus Facure](https://matheusfacure.github.io/python-causality-handbook/landing-page.html) and the work of [CausalPy and all its developers](https://causalpy.readthedocs.io/en/stable/examples.html#interrupted-time-series) and generally [Hyndman & Athanasopoulos](https://otexts.com/fpp3/) incredible book and associated timeseries analysis packages that they developed thsat are simple brilliant.

![Alt text](https://github.com/HPCurtis/causalcovidcattle/blob/main/img/linearforecast.png?raw=true)

![Alt text](https://github.com/HPCurtis/causalcovidcattle/blob/main/img/causal_impact.png?raw=true)



Expand Down

0 comments on commit 5aee824

Please sign in to comment.