Skip to content

Commit

Permalink
all
Browse files Browse the repository at this point in the history
  • Loading branch information
HPCurtis committed Jun 21, 2024
1 parent d2b41a8 commit 40e76a2
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 29 deletions.
46 changes: 17 additions & 29 deletions R/data_anal_vis.r
Original file line number Diff line number Diff line change
@@ -1,37 +1,32 @@
# Import relevant
library(ggplot2)
library(fable)
library(feasts)

#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")

# Convert to date form.
df_og$Date <- as.Date(df_og$Date)
df_sa$Date <- as.Date(df_sa$Date)

df_sa <- df_sa %>% drop_na() %>%
as_tsibble( index = Date) %>%
mutate(Date = yearquarter(Date))

#Pre/Post Covid----
pre_covid <- df_sa %>%
drop_na() %>%
filter(Date < ymd("2020-03-01")) %>%
filter(Date < yearquarter("2020 Q2")) %>%
mutate(Date = yearquarter(Date)) %>%
as_tsibble(index = Date)

post_covid <- df_sa %>%
drop_na() %>%
filter(Date >= ymd("2020-03-01") )%>%
filter(Date >= yearquarter("2020 Q2"))%>%
mutate(Date = yearquarter(Date)) %>%
as_tsibble(index = Date)

# Visualisations for the project

#Time series Decomposition------
# Conduct STL decomposition.
dcmp <- drop_na(df_sa) |>
model(stl = STL(NumberSlaughteredCATTLEexclcalvesTotalState))

components(dcmp) |> autoplot()

# Fit linear model for seasonal adjusted data.
fitlinear <- pre_covid |>
model(trend_model = TSLM(NumberSlaughteredCATTLEexclcalvesTotalState ~ trend()))
Expand All @@ -42,27 +37,20 @@ fc <- fitlinear %>%
# Get forecast intervals
hilo()

# Convert Data column to approriate data type.
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)
plinear <- ggplot() +
geom_line(data = df_sa, aes(x = Date, y = NumberSlaughteredCATTLEexclcalvesTotalState), color = "blue", linetype = "solid") +
geom_line(aes(x = fc$Date, y = fc$.mean), color = "blue") +
geom_ribbon(aes(x = fc$Date, ymin = fc$`95%`$lower, ymax = fc$`95%`$upper),
fill = "blue", alpha = 0.3) +
geom_vline(xintercept = as.Date("2020-03-01"), color = "red", linetype = "dashed") +
labs(x = "Date", y = "Number of Cattle Slaughtered ('000)", title = "Total number of cattle (excl calves) across all Australian States") +

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

# Clauate mean and lower and upper bounds
# Calculate mean and lower and upper bounds
Totalslaughteredimpact <- post_covid$NumberSlaughteredCATTLEexclcalvesTotalState - yhat
Totalslaughteredimpactupper <- post_covid$NumberSlaughteredCATTLEexclcalvesTotalState - yhatupper
Totalslaughteredimpactlower <- post_covid$NumberSlaughteredCATTLEexclcalvesTotalState - yhatlower
Expand All @@ -78,7 +66,7 @@ causal_impact_plot <- ggplot() +
geom_ribbon(data = fc,
aes(x = Date, ymin = post_covid$NumberSlaughteredCATTLEexclcalvesTotalState),
ymax = yhat, fill = "blue", alpha = 0.2) +
labs(y = "Number of Cattle Slaughtered",
labs(y = "Number of Cattle Slaughtered ('000)",
title = "Total number of cattle (excl calves) across all Australian States") +
theme(
plot.title = element_text(size = 10), # Title font size
Expand Down
Empty file removed R/data_analyis.r
Empty file.
1 change: 1 addition & 0 deletions data/.~lock.cattle.xlsx#
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
,harrison,harrison-HP-Laptop-14-cf1xxx,21.06.2024 13:18,file:///home/harrison/.config/libreoffice/4;

0 comments on commit 40e76a2

Please sign in to comment.