---
title: "ExportGenius Trade Analysis: Kazakhstan"
author: Gagan Atreya
date: today
format:
html:
toc: true
toc-location: left
toc-depth: 4
theme: lumen
fontsize: large
code-fold: true
code-tools: true
code-summary: "Display code"
code-overflow: wrap
editor:
markdown:
wrap: 72
---
## **Tobacco exports**
```{r, error = F, message = F, warning = F}
rm(list = ls())
pacman::p_load(readxl, vtable, tidyverse,
lubridate, data.table, zoo,
readODS, patchwork, scales)
rm(list = ls())
df <- fread("/home/gagan/Desktop/soc_ace_2024/KAZ-Detailed-Export-Tobacco.csv")
df$date <- dmy(df$Date)
df$date <- df$date
df$description <- df$`Product Description`
df$destination_country <- df$`Destination Country`
#table(df$destination_country)
df$destination_country <- ifelse(df$destination_country == "Europiean Union", "European Union",
ifelse(df$destination_country == "Unknwon", "Unknown",
ifelse(df$destination_country == "EUROPIEAN UNION", "European Union",
df$destination_country)))
df$supplier <- df$Exporter
df$importer <- df$Buyer
df$price_invoice <- df$`Total Value USD`/df$`Net Weight Kg`
df$total_value <- df$`Total Value USD`
df <- df[, c("date", "description", "destination_country",
"supplier", "importer", "price_invoice", "total_value")]
df02 <- fread("~/Desktop/soc_ace_2024/scripts/exportgenius/gold_analyses_latest/goldprice.csv")
df02$date <- ymd(df02$date)
df <- merge(df, df02, by = "date")
df$price_benchmark <- df$price_gram
#df[sapply(df, is.infinite)] <- NA
#df <- df[!(df$price_invoice > 1000),]
plot01 <- ggplot(df,
aes(x = date,
y = total_value)) +
geom_line() +
geom_point() +
# ylim(15, 120) +
geom_vline(xintercept = as.Date("2022-02-24"),
linetype = "dashed",
color = "black",
size = 0.45) +
labs(title = "Kazakhstan Tobacco Exports",
x = "Date",
y = "Total Value") +
theme_bw()
plot01
plot02 <- df %>%
ggplot( aes(x = date,
y = total_value,
color = destination_country))+
geom_line()+
geom_vline(xintercept = as.Date("2022-02-24"),
linetype = "dashed",
color = "black",
size = 0.45) +
geom_point()+
labs(title = "Kazakhstan Tobacco Exports",
x = "Date",
y = "Total value")+
theme_bw()
plot02
plot03 <- df %>%
ggplot( aes(x = date,
y = total_value)) +
geom_line()+
geom_point()+
geom_vline(xintercept = as.Date("2022-02-24"),
linetype = "dashed",
color = "black",
size = 0.45) +
facet_wrap(~destination_country, ncol = 3)+
labs(title = "Kazakhstan Tobacco Exports by Destination",
x = "Date",
y = "Total Value") +
theme_bw()
plot03
max_price_index <- which.max(df$price_invoice)
# Remove the row with the highest value in the 'price' column
df <- df[-max_price_index, ]
df$filter01 <- ifelse(df$price_invoice > 150, 1, 0)
# table(df$filter01)
df <- df[df$price_invoice <=120, ]
df$price_difference <- df$price_invoice - df$price_benchmark
#df <- df[df$destination_country == "China", ]
df03 <- df[, c("date", "price_invoice", "price_benchmark")]
df_long <- reshape2::melt(df03,
id.vars = "date",
variable.name = "price_type",
value.name = "price")
df$price_difference <- df$price_invoice - df$price_benchmark
df$price_difference2 <- scale(df$price_difference)
df$price_benchmark2 <- scale(df$price_benchmark)
plot04 <- ggplot(df, aes(x = date,
y = price_difference2)) +
geom_segment(aes(x = date,
xend = date,
y = 0,
yend = price_difference2,
color = price_difference2 > 0)) +
geom_point(color = "black", size = 2) +
geom_vline(xintercept = as.Date("2022-02-24"),
linetype = "dashed",
color = "black",
size = 0.45) +
theme_bw() +
scale_color_manual(values = c("red", "blue")) +
labs(title = "Kazakhstan Tobacco Exports: Price Differentials Over Time",
x = "Date",
y = "Price differential (standardized)") +
guides(color = FALSE)
plot04
```
## Kazakhstan Tobacco Exports: Notable Destination Countries
```{r, error = F, message = F, warning = F}
df11 <- df[df$destination_country %in% c("Afghanistan", "China", "Georgia",
"Germany", "Indonesia", "Moldova", "Mongolia",
"Poland", "Romania", "Switzerland",
"Tajikistan", "Turkey", "Ukraine", "Uzbekistan") ]
plot11 <- df11 %>%
ggplot( aes(x = date,
y = total_value)) +
geom_line()+
geom_point()+
geom_vline(xintercept = as.Date("2022-02-24"),
linetype = "dashed",
color = "black",
size = 0.45) +
facet_wrap(~destination_country, ncol = 3)+
labs(title = "Kazakhstan Tobacco Exports",
x = "Date",
y = "Total Value") +
theme_bw()
plot11
```
```{r, error = F, message = F, warning = F}
plot12 <- ggplot(df11, aes(x = date,
y = price_difference2)) +
geom_segment(aes(x = date,
xend = date,
y = 0,
yend = price_difference2,
color = price_difference2 > 0)) +
geom_point(color = "black", size = 2) +
geom_vline(xintercept = as.Date("2022-02-24"),
linetype = "dashed",
color = "black",
size = 0.45) +
theme_bw() +
scale_color_manual(values = c("red", "blue")) +
labs(title = "Kazakhstan Tobacco Exports: Price Differentials Over Time",
x = "Date",
y = "Price differential (standardized)") +
facet_wrap(~destination_country, ncol = 3)+
guides(color = FALSE)
plot12
```
## Kazakhstan - Afghanistan
```{r, error = F, message = F, warning = F}
df11 <- df[df$destination_country == "Afghanistan", ]
plot11 <- df11 %>%
ggplot( aes(x = date,
y = total_value)) +
geom_line()+
geom_point()+
geom_vline(xintercept = as.Date("2022-02-24"),
linetype = "dashed",
color = "black",
size = 0.45) +
facet_wrap(~destination_country, ncol = 2)+
labs(title = "Kazakhstan Tobacco Exports: Afghanistan",
x = "Date",
y = "Total Value") +
theme_bw()
plot11
```
```{r, error = F, message = F, warning = F}
plot12 <- ggplot(df11, aes(x = date,
y = price_difference2)) +
geom_segment(aes(x = date,
xend = date,
y = 0,
yend = price_difference2,
color = price_difference2 > 0)) +
geom_point(color = "black", size = 2) +
geom_vline(xintercept = as.Date("2022-02-24"),
linetype = "dashed",
color = "black",
size = 0.45) +
theme_bw() +
scale_color_manual(values = c("red", "blue")) +
labs(title = "Kazakhstan Tobacco Exports to Afghanistan: Price Differentials Over Time",
x = "Date",
y = "Price differential (standardized)") +
guides(color = FALSE)
plot12
```
## Kazakhstan - China
```{r, error = F, message = F, warning = F}
df11 <- df[df$destination_country == "China", ]
plot11 <- df11 %>%
ggplot( aes(x = date,
y = total_value)) +
geom_line()+
geom_point()+
geom_vline(xintercept = as.Date("2022-02-24"),
linetype = "dashed",
color = "black",
size = 0.45) +
facet_wrap(~destination_country, ncol = 2)+
labs(title = "Kazakhstan Tobacco Exports: China",
x = "Date",
y = "Total Value") +
theme_bw()
plot11
```
```{r, error = F, message = F, warning = F}
plot12 <- ggplot(df11, aes(x = date,
y = price_difference2)) +
geom_segment(aes(x = date,
xend = date,
y = 0,
yend = price_difference2,
color = price_difference2 > 0)) +
geom_point(color = "black", size = 2) +
geom_vline(xintercept = as.Date("2022-02-24"),
linetype = "dashed",
color = "black",
size = 0.45) +
theme_bw() +
scale_color_manual(values = c("red", "blue")) +
labs(title = "Kazakhstan Tobacco Exports to China: Price Differentials Over Time",
x = "Date",
y = "Price differential (standardized)") +
guides(color = FALSE)
plot12
```
## Kazakhstan - Georgia
```{r, error = F, message = F, warning = F}
df11 <- df[df$destination_country == "Georgia", ]
plot11 <- df11 %>%
ggplot( aes(x = date,
y = total_value)) +
geom_line()+
geom_point()+
geom_vline(xintercept = as.Date("2022-02-24"),
linetype = "dashed",
color = "black",
size = 0.45) +
facet_wrap(~destination_country, ncol = 2)+
labs(title = "Kazakhstan Tobacco Exports: Georgia",
x = "Date",
y = "Total Value") +
theme_bw()
plot11
```
```{r, error = F, message = F, warning = F}
plot12 <- ggplot(df11, aes(x = date,
y = price_difference2)) +
geom_segment(aes(x = date,
xend = date,
y = 0,
yend = price_difference2,
color = price_difference2 > 0)) +
geom_point(color = "black", size = 2) +
geom_vline(xintercept = as.Date("2022-02-24"),
linetype = "dashed",
color = "black",
size = 0.45) +
theme_bw() +
scale_color_manual(values = c("red", "blue")) +
labs(title = "Kazakhstan Tobacco Exports to Georgia: Price Differentials Over Time",
x = "Date",
y = "Price differential (standardized)") +
guides(color = FALSE)
plot12
```
## Kazakhstan - Germany
```{r, error = F, message = F, warning = F}
df11 <- df[df$destination_country == "Germany", ]
plot11 <- df11 %>%
ggplot( aes(x = date,
y = total_value)) +
geom_line()+
geom_point()+
geom_vline(xintercept = as.Date("2022-02-24"),
linetype = "dashed",
color = "black",
size = 0.45) +
facet_wrap(~destination_country, ncol = 2)+
labs(title = "Kazakhstan Tobacco Exports: Germany",
x = "Date",
y = "Total Value") +
theme_bw()
plot11
```
```{r, error = F, message = F, warning = F}
plot12 <- ggplot(df11, aes(x = date,
y = price_difference2)) +
geom_segment(aes(x = date,
xend = date,
y = 0,
yend = price_difference2,
color = price_difference2 > 0)) +
geom_point(color = "black", size = 2) +
geom_vline(xintercept = as.Date("2022-02-24"),
linetype = "dashed",
color = "black",
size = 0.45) +
theme_bw() +
scale_color_manual(values = c("red", "blue")) +
labs(title = "Kazakhstan Tobacco Exports to Germany: Price Differentials Over Time",
x = "Date",
y = "Price differential (standardized)") +
guides(color = FALSE)
plot12
```
## Kazakhstan - Indonesia
```{r, error = F, message = F, warning = F}
df11 <- df[df$destination_country == "Indonesia", ]
plot11 <- df11 %>%
ggplot( aes(x = date,
y = total_value)) +
geom_line()+
geom_point()+
geom_vline(xintercept = as.Date("2022-02-24"),
linetype = "dashed",
color = "black",
size = 0.45) +
facet_wrap(~destination_country, ncol = 2)+
labs(title = "Kazakhstan Tobacco Exports: Indonesia",
x = "Date",
y = "Total Value") +
theme_bw()
plot11
```
```{r, error = F, message = F, warning = F}
plot12 <- ggplot(df11, aes(x = date,
y = price_difference2)) +
geom_segment(aes(x = date,
xend = date,
y = 0,
yend = price_difference2,
color = price_difference2 > 0)) +
geom_point(color = "black", size = 2) +
geom_vline(xintercept = as.Date("2022-02-24"),
linetype = "dashed",
color = "black",
size = 0.45) +
theme_bw() +
scale_color_manual(values = c("red", "blue")) +
labs(title = "Kazakhstan Tobacco Exports to Indonesia: Price Differentials Over Time",
x = "Date",
y = "Price differential (standardized)") +
guides(color = FALSE)
plot12
```
## Kazakhstan - Moldova
```{r, error = F, message = F, warning = F}
df11 <- df[df$destination_country == "Moldova", ]
plot11 <- df11 %>%
ggplot( aes(x = date,
y = total_value)) +
geom_line()+
geom_point()+
geom_vline(xintercept = as.Date("2022-02-24"),
linetype = "dashed",
color = "black",
size = 0.45) +
facet_wrap(~destination_country, ncol = 2)+
labs(title = "Kazakhstan Tobacco Exports: Moldova",
x = "Date",
y = "Total Value") +
theme_bw()
plot11
```
```{r, error = F, message = F, warning = F}
plot12 <- ggplot(df11, aes(x = date,
y = price_difference2)) +
geom_segment(aes(x = date,
xend = date,
y = 0,
yend = price_difference2,
color = price_difference2 > 0)) +
geom_point(color = "black", size = 2) +
geom_vline(xintercept = as.Date("2022-02-24"),
linetype = "dashed",
color = "black",
size = 0.45) +
theme_bw() +
scale_color_manual(values = c("red", "blue")) +
labs(title = "Kazakhstan Tobacco Exports to Moldova: Price Differentials Over Time",
x = "Date",
y = "Price differential (standardized)") +
guides(color = FALSE)
plot12
```
## Kazakhstan - Mongolia
```{r, error = F, message = F, warning = F}
df11 <- df[df$destination_country == "Mongolia", ]
plot11 <- df11 %>%
ggplot( aes(x = date,
y = total_value)) +
geom_line()+
geom_point()+
geom_vline(xintercept = as.Date("2022-02-24"),
linetype = "dashed",
color = "black",
size = 0.45) +
facet_wrap(~destination_country, ncol = 2)+
labs(title = "Kazakhstan Tobacco Exports: Mongolia",
x = "Date",
y = "Total Value") +
theme_bw()
plot11
```
```{r, error = F, message = F, warning = F}
plot12 <- ggplot(df11, aes(x = date,
y = price_difference2)) +
geom_segment(aes(x = date,
xend = date,
y = 0,
yend = price_difference2,
color = price_difference2 > 0)) +
geom_point(color = "black", size = 2) +
geom_vline(xintercept = as.Date("2022-02-24"),
linetype = "dashed",
color = "black",
size = 0.45) +
theme_bw() +
scale_color_manual(values = c("red", "blue")) +
labs(title = "Kazakhstan Tobacco Exports to Mongolia: Price Differentials Over Time",
x = "Date",
y = "Price differential (standardized)") +
guides(color = FALSE)
plot12
```
## Kazakhstan - Poland
```{r, error = F, message = F, warning = F}
df11 <- df[df$destination_country == "Poland", ]
plot11 <- df11 %>%
ggplot( aes(x = date,
y = total_value)) +
geom_line()+
geom_point()+
geom_vline(xintercept = as.Date("2022-02-24"),
linetype = "dashed",
color = "black",
size = 0.45) +
facet_wrap(~destination_country, ncol = 2)+
labs(title = "Kazakhstan Tobacco Exports: Poland",
x = "Date",
y = "Total Value") +
theme_bw()
plot11
```
```{r, error = F, message = F, warning = F}
plot12 <- ggplot(df11, aes(x = date,
y = price_difference2)) +
geom_segment(aes(x = date,
xend = date,
y = 0,
yend = price_difference2,
color = price_difference2 > 0)) +
geom_point(color = "black", size = 2) +
geom_vline(xintercept = as.Date("2022-02-24"),
linetype = "dashed",
color = "black",
size = 0.45) +
theme_bw() +
scale_color_manual(values = c("red", "blue")) +
labs(title = "Kazakhstan Tobacco Exports to Poland: Price Differentials Over Time",
x = "Date",
y = "Price differential (standardized)") +
guides(color = FALSE)
plot12
```
## Kazakhstan - Romania
```{r, error = F, message = F, warning = F}
df11 <- df[df$destination_country == "Romania", ]
plot11 <- df11 %>%
ggplot( aes(x = date,
y = total_value)) +
geom_line()+
geom_point()+
geom_vline(xintercept = as.Date("2022-02-24"),
linetype = "dashed",
color = "black",
size = 0.45) +
facet_wrap(~destination_country, ncol = 2)+
labs(title = "Kazakhstan Tobacco Exports: Romania",
x = "Date",
y = "Total Value") +
theme_bw()
plot11
```
```{r, error = F, message = F, warning = F}
plot12 <- ggplot(df11, aes(x = date,
y = price_difference2)) +
geom_segment(aes(x = date,
xend = date,
y = 0,
yend = price_difference2,
color = price_difference2 > 0)) +
geom_point(color = "black", size = 2) +
geom_vline(xintercept = as.Date("2022-02-24"),
linetype = "dashed",
color = "black",
size = 0.45) +
theme_bw() +
scale_color_manual(values = c("red", "blue")) +
labs(title = "Kazakhstan Tobacco Exports to Romania: Price Differentials Over Time",
x = "Date",
y = "Price differential (standardized)") +
guides(color = FALSE)
plot12
```
## Kazakhstan - Switzerland
```{r, error = F, message = F, warning = F}
df11 <- df[df$destination_country == "Switzerland", ]
plot11 <- df11 %>%
ggplot( aes(x = date,
y = total_value)) +
geom_line()+
geom_point()+
geom_vline(xintercept = as.Date("2022-02-24"),
linetype = "dashed",
color = "black",
size = 0.45) +
facet_wrap(~destination_country, ncol = 2)+
labs(title = "Kazakhstan Tobacco Exports: Switzerland",
x = "Date",
y = "Total Value") +
theme_bw()
plot11
```
```{r, error = F, message = F, warning = F}
plot12 <- ggplot(df11, aes(x = date,
y = price_difference2)) +
geom_segment(aes(x = date,
xend = date,
y = 0,
yend = price_difference2,
color = price_difference2 > 0)) +
geom_point(color = "black", size = 2) +
geom_vline(xintercept = as.Date("2022-02-24"),
linetype = "dashed",
color = "black",
size = 0.45) +
theme_bw() +
scale_color_manual(values = c("red", "blue")) +
labs(title = "Kazakhstan Tobacco Exports to Switzerland: Price Differentials Over Time",
x = "Date",
y = "Price differential (standardized)") +
guides(color = FALSE)
plot12
```
## Kazakhstan - Tajikistan
```{r, error = F, message = F, warning = F}
df11 <- df[df$destination_country == "Tajikistan", ]
plot11 <- df11 %>%
ggplot( aes(x = date,
y = total_value)) +
geom_line()+
geom_point()+
geom_vline(xintercept = as.Date("2022-02-24"),
linetype = "dashed",
color = "black",
size = 0.45) +
facet_wrap(~destination_country, ncol = 2)+
labs(title = "Kazakhstan Tobacco Exports: Tajikistan",
x = "Date",
y = "Total Value") +
theme_bw()
plot11
```
```{r, error = F, message = F, warning = F}
plot12 <- ggplot(df11, aes(x = date,
y = price_difference2)) +
geom_segment(aes(x = date,
xend = date,
y = 0,
yend = price_difference2,
color = price_difference2 > 0)) +
geom_point(color = "black", size = 2) +
geom_vline(xintercept = as.Date("2022-02-24"),
linetype = "dashed",
color = "black",
size = 0.45) +
theme_bw() +
scale_color_manual(values = c("red", "blue")) +
labs(title = "Kazakhstan Tobacco Exports to Tajikistan: Price Differentials Over Time",
x = "Date",
y = "Price differential (standardized)") +
guides(color = FALSE)
plot12
```
## Kazakhstan - Turkey
```{r, error = F, message = F, warning = F}
df11 <- df[df$destination_country == "Turkey", ]
plot11 <- df11 %>%
ggplot( aes(x = date,
y = total_value)) +
geom_line()+
geom_point()+
geom_vline(xintercept = as.Date("2022-02-24"),
linetype = "dashed",
color = "black",
size = 0.45) +
facet_wrap(~destination_country, ncol = 2)+
labs(title = "Kazakhstan Tobacco Exports: Turkey",
x = "Date",
y = "Total Value") +
theme_bw()
plot11
```
```{r, error = F, message = F, warning = F}
plot12 <- ggplot(df11, aes(x = date,
y = price_difference2)) +
geom_segment(aes(x = date,
xend = date,
y = 0,
yend = price_difference2,
color = price_difference2 > 0)) +
geom_point(color = "black", size = 2) +
geom_vline(xintercept = as.Date("2022-02-24"),
linetype = "dashed",
color = "black",
size = 0.45) +
theme_bw() +
scale_color_manual(values = c("red", "blue")) +
labs(title = "Kazakhstan Tobacco Exports to Turkey: Price Differentials Over Time",
x = "Date",
y = "Price differential (standardized)") +
guides(color = FALSE)
plot12
```
## Kazakhstan - Ukraine
```{r, error = F, message = F, warning = F}
df11 <- df[df$destination_country == "Ukraine", ]
plot11 <- df11 %>%
ggplot( aes(x = date,
y = total_value)) +
geom_line()+
geom_point()+
geom_vline(xintercept = as.Date("2022-02-24"),
linetype = "dashed",
color = "black",
size = 0.45) +
facet_wrap(~destination_country, ncol = 2)+
labs(title = "Kazakhstan Tobacco Exports: Ukraine",
x = "Date",
y = "Total Value") +
theme_bw()
plot11
```
```{r, error = F, message = F, warning = F}
plot12 <- ggplot(df11, aes(x = date,
y = price_difference2)) +
geom_segment(aes(x = date,
xend = date,
y = 0,
yend = price_difference2,
color = price_difference2 > 0)) +
geom_point(color = "black", size = 2) +
geom_vline(xintercept = as.Date("2022-02-24"),
linetype = "dashed",
color = "black",
size = 0.45) +
theme_bw() +
scale_color_manual(values = c("red", "blue")) +
labs(title = "Kazakhstan Tobacco Exports to Ukraine: Price Differentials Over Time",
x = "Date",
y = "Price differential (standardized)") +
guides(color = FALSE)
plot12
```
## Kazakhstan - Uzbekistan
```{r, error = F, message = F, warning = F}
df11 <- df[df$destination_country == "Uzbekistan", ]
plot11 <- df11 %>%
ggplot( aes(x = date,
y = total_value)) +
geom_line()+
geom_point()+
geom_vline(xintercept = as.Date("2022-02-24"),
linetype = "dashed",
color = "black",
size = 0.45) +
facet_wrap(~destination_country, ncol = 2)+
labs(title = "Kazakhstan Tobacco Exports: Uzbekistan",
x = "Date",
y = "Total Value") +
theme_bw()
plot11
```
```{r, error = F, message = F, warning = F}
plot12 <- ggplot(df11, aes(x = date,
y = price_difference2)) +
geom_segment(aes(x = date,
xend = date,
y = 0,
yend = price_difference2,
color = price_difference2 > 0)) +
geom_point(color = "black", size = 2) +
geom_vline(xintercept = as.Date("2022-02-24"),
linetype = "dashed",
color = "black",
size = 0.45) +
theme_bw() +
scale_color_manual(values = c("red", "blue")) +
labs(title = "Kazakhstan Tobacco Exports to Uzbekistan: Price Differentials Over Time",
x = "Date",
y = "Price differential (standardized)") +
guides(color = FALSE)
plot12
```