library(plotly)
library(DT)
library(WDI)
library(tidyverse)

# Hent data fra Verdensbanken
fx <- WDI(indicator='PA.NUS.FCRF', country=c('AU','NZ'), start=2000, end=2025)
cpi <- WDI(indicator='FP.CPI.TOTL', country=c('AU','NZ'), start=2000, end=2025)
adjust_factor <- WDI(indicator='PA.NUS.PRVT.PP', country=c('AU','NZ'), start=2000, end=2025)

# Farver
red <- "#E41A1C"
blue <- "#377EB8"
violet <- "#984EA3"
grey <- "#999999"
darkblue <- "#2E4053"

##### ABSOLUT PPP #####

# Konstruer sammenlignelige prisniveauer
prices <- cpi %>%
  left_join(adjust_factor %>% select("iso2c", "year", "PA.NUS.PRVT.PP"), by=c("iso2c", "year")) %>%
  mutate(
    price_level = FP.CPI.TOTL * PA.NUS.PRVT.PP
  ) %>%
  filter(!is.na(price_level))

# Beregn valutakursforholdet
compare_fx <- fx %>%
    filter(!is.na(PA.NUS.FCRF)) %>%
    select(iso2c, year, PA.NUS.FCRF) %>%
    pivot_wider(names_from = iso2c, values_from = PA.NUS.FCRF) %>%
    mutate(
      fx_AU_NZ = `AU` / `NZ`
    ) %>%
    select(year, fx_AU_NZ)

# Sammenlign prisniveauer og valutakurs
compare <- prices %>%
  filter(!is.na(price_level)) %>%
  select(iso2c, year, price_level) %>%
  pivot_wider(names_from = iso2c, values_from = price_level) %>%
  mutate(
    ratio_AU_NZ = `AU` / `NZ`
  ) %>%
  select(year, ratio_AU_NZ) %>%
  left_join(compare_fx, by="year")

# Plot både ratio_AU_NZ og fx_AU_NZ
p1 <- compare %>%
  pivot_longer(cols = c(ratio_AU_NZ, fx_AU_NZ), names_to = "variable", values_to = "value") %>%
  ggplot(aes(x = year, y = value, color = variable)) +
  geom_line(linewidth = 1) +
  labs(
    x = "År",
    y = "Ratio (AU/NZ)",
    color = "Variabel"
  ) +
  theme_minimal() +
  scale_color_manual(
    values = c(ratio_AU_NZ = blue, fx_AU_NZ = red),
    labels = c(ratio_AU_NZ = "Relativt prisniveau", fx_AU_NZ = "Valutakurs")
  )

# Beregn og vis afvigelsen fra absolut PPP
p2 <- compare %>%
  mutate(
    ppp_deviation = (fx_AU_NZ - ratio_AU_NZ) / ratio_AU_NZ * 100
  ) %>%
  ggplot(aes(x = year, y = ppp_deviation)) +
  geom_line(linewidth = 1, color = violet) +
  geom_hline(yintercept = 0, linetype = "dashed", color = grey) +
  labs(
    x = "År",
    y = "Afvigelse (%)"
  ) +
  theme_minimal()

##### RELATIV PPP #####

# Beregn inflationsrater og valutakursændringer (relativ PPP)
inflation_fx_compare <- cpi %>%
  filter(!is.na(FP.CPI.TOTL)) %>%
  select(iso2c, year, FP.CPI.TOTL) %>%
  pivot_wider(names_from = iso2c, values_from = FP.CPI.TOTL, names_prefix = "price_") %>%
  mutate(
    inflation_AU = (price_AU - lead(price_AU)) / lead(price_AU) * 100,
    inflation_NZ = (price_NZ - lead(price_NZ)) / lead(price_NZ) * 100,
    inflation_diff = inflation_AU - inflation_NZ
  ) %>%
  left_join(
    compare_fx %>%
      mutate(
        fx_change = (fx_AU_NZ - lead(fx_AU_NZ)) / lead(fx_AU_NZ) * 100
      ) %>%
      select(year, fx_change),
    by = "year"
  ) %>%
  select(year, inflation_diff, fx_change)

# Vis sammenhæng mellem inflationsdifferential og valutakursændring (relativ PPP)
p3 <- inflation_fx_compare %>%
  ggplot(aes(x = inflation_diff, y = fx_change)) +
  geom_point(size = 3, color = darkblue, alpha = 0.6) +
  geom_abline(slope = 1, intercept = 0, linetype = "dashed", color = grey) +
  labs(
    x = "Inflationsdifferential (AU − NZ, %)",
    y = "Valutakursændring (AU/NZ, %)"
  ) +
  theme_minimal() +
  geom_smooth(method = "lm", se = FALSE, color = red, linewidth = 1)

# Regression
rel_ppp_model <- lm(fx_change ~ inflation_diff, data = inflation_fx_compare)