glimpse(energy_co2_updated)
## Rows: 26,136
## Columns: 45
## $ country                                                                 <chr> …
## $ year                                                                    <dbl> …
## $ iso3_country                                                            <chr> …
## $ sector_detail                                                           <chr> …
## $ energy_price_flow_detail                                                <chr> …
## $ high_sulphur_fuel_oil_tonne                                             <dbl> …
## $ low_sulphur_fuel_oil_tonne                                              <dbl> …
## $ light_fuel_oil_1000_litres                                              <dbl> …
## $ automotive_diesel_litre                                                 <dbl> …
## $ premium_unleaded_98_ron_litre                                           <dbl> …
## $ premium_unleaded_95_ron_litre                                           <dbl> …
## $ regular_unleaded_gasoline_litre                                         <dbl> …
## $ premium_leaded_gasoline_litre                                           <dbl> …
## $ regular_leaded_gasoline_litre                                           <dbl> …
## $ liquefied_petroleum_gas_litre                                           <dbl> …
## $ natural_gas_m_wh                                                        <dbl> …
## $ steam_coal_tonne                                                        <dbl> …
## $ coking_coal_tonne                                                       <dbl> …
## $ electricity_m_wh                                                        <dbl> …
## $ consumer_price_index                                                    <dbl> …
## $ cpi_energy                                                              <dbl> …
## $ producer_price_index                                                    <dbl> …
## $ purchasing_power_parities                                               <dbl> …
## $ u_s_dollar_exchange_rate                                                <dbl> …
## $ group_type                                                              <chr> …
## $ advanced_economies                                                      <dbl> …
## $ advanced_g20                                                            <dbl> …
## $ emerging_g20                                                            <dbl> …
## $ emerging_market_and_middle_income_africa                                <dbl> …
## $ emerging_market_and_middle_income_asia                                  <dbl> …
## $ emerging_market_and_middle_income_europe                                <dbl> …
## $ emerging_market_and_middle_income_latin_america                         <dbl> …
## $ emerging_market_and_middle_income_middle_east_north_africa_and_pakistan <dbl> …
## $ emerging_market_economies                                               <dbl> …
## $ euro_area                                                               <dbl> …
## $ g20                                                                     <dbl> …
## $ g7                                                                      <dbl> …
## $ low_income_developing_asia                                              <dbl> …
## $ low_income_developing_countries                                         <dbl> …
## $ low_income_developing_latin_america                                     <dbl> …
## $ low_income_developing_others                                            <dbl> …
## $ low_income_developing_sub_saharan_africa                                <dbl> …
## $ low_income_oil_producers                                                <dbl> …
## $ oil_producers                                                           <dbl> …
## $ co2_per_capital                                                         <dbl> …
glimpse(energy_co2_2)
## Rows: 365,904
## Columns: 33
## $ country                                                                 <chr> …
## $ year                                                                    <dbl> …
## $ iso3_country                                                            <chr> …
## $ product_detail                                                          <chr> …
## $ sector_detail                                                           <chr> …
## $ energy_price_flow_detail                                                <chr> …
## $ energy_price_value                                                      <dbl> …
## $ consumer_price_index                                                    <dbl> …
## $ cpi_energy                                                              <dbl> …
## $ producer_price_index                                                    <dbl> …
## $ purchasing_power_parities                                               <dbl> …
## $ u_s_dollar_exchange_rate                                                <dbl> …
## $ group_type                                                              <chr> …
## $ advanced_economies                                                      <dbl> …
## $ advanced_g20                                                            <dbl> …
## $ emerging_g20                                                            <dbl> …
## $ emerging_market_and_middle_income_africa                                <dbl> …
## $ emerging_market_and_middle_income_asia                                  <dbl> …
## $ emerging_market_and_middle_income_europe                                <dbl> …
## $ emerging_market_and_middle_income_latin_america                         <dbl> …
## $ emerging_market_and_middle_income_middle_east_north_africa_and_pakistan <dbl> …
## $ emerging_market_economies                                               <dbl> …
## $ euro_area                                                               <dbl> …
## $ g20                                                                     <dbl> …
## $ g7                                                                      <dbl> …
## $ low_income_developing_asia                                              <dbl> …
## $ low_income_developing_countries                                         <dbl> …
## $ low_income_developing_latin_america                                     <dbl> …
## $ low_income_developing_others                                            <dbl> …
## $ low_income_developing_sub_saharan_africa                                <dbl> …
## $ low_income_oil_producers                                                <dbl> …
## $ oil_producers                                                           <dbl> …
## $ co2_per_capital                                                         <dbl> …
energy_co2_2 %>%
  count(year, sort = TRUE) %>%
  arrange(desc(year))
## # A tibble: 44 × 2
##     year     n
##    <dbl> <int>
##  1  2021  8316
##  2  2020  8316
##  3  2019  8316
##  4  2018  8316
##  5  2017  8316
##  6  2016  8316
##  7  2015  8316
##  8  2014  8316
##  9  2013  8316
## 10  2012  8316
## # … with 34 more rows
energy_co2_updated %>%
  count(year, sort = TRUE) %>%
  arrange(desc(year))
## # A tibble: 44 × 2
##     year     n
##    <dbl> <int>
##  1  2021   594
##  2  2020   594
##  3  2019   594
##  4  2018   594
##  5  2017   594
##  6  2016   594
##  7  2015   594
##  8  2014   594
##  9  2013   594
## 10  2012   594
## # … with 34 more rows
unique(energy_co2_2$product_detail)
##  [1] "Liquefied petroleum gas (litre)"   "Coking coal (tonne)"              
##  [3] "Regular unleaded gasoline (litre)" "Premium leaded gasoline (litre)"  
##  [5] "Steam coal (tonne)"                "Light fuel oil (1000 litres)"     
##  [7] "Natural gas (MWh)"                 "Regular leaded gasoline (litre)"  
##  [9] "High sulphur fuel oil (tonne)"     "Automotive diesel (litre)"        
## [11] "Premium unleaded 95 RON (litre)"   "Electricity (MWh)"                
## [13] "Premium unleaded 98 RON (litre)"   "Low sulphur fuel oil (tonne)"
unique(energy_co2_2$sector_detail)
## [1] "Households"             "Electricity generation" "Industry"
unique(energy_co2_2$energy_price_flow_detail)
## [1] "Total tax (USD/unit)"             "Total price (USD/unit)"          
## [3] "Total price (USD/unit using PPP)" "Total tax (USD/unit using PPP)"  
## [5] "Total tax (USD/toe NCV)"          "Total price (USD/toe NCV)"

#Descriptive ##Energy Price ###Adcanced Economies repeat this for all

energy_co2_advanced_2000 <- energy_co2_2 %>%
  filter(year >= 2000) %>%
  filter(advanced_g20 == 1) %>%
  #filter(sector_detail == "Industry") %>%
  filter(energy_price_flow_detail == "Total price (USD/unit)") %>%
  filter(product_detail == "Liquefied petroleum gas (litre)")  %>%
  group_by(country) 

ggplot(energy_co2_advanced_2000,
       aes(x = year, y = energy_price_value)
       ) + #why the color is not working here
  geom_point(aes(color = country)) + 
  geom_smooth(method = lm, level = 0.99) +
  #https://www.r-bloggers.com/2021/09/draw-a-trend-line-using-ggplot-quick-guide/
  facet_grid(sector_detail ~.)
## `geom_smooth()` using formula 'y ~ x'

energy_co2_advanced_economy_2000 <- energy_co2_2 %>%
  filter(year >= 2000) %>%
  filter(advanced_g20 == 1) %>%
  #filter(sector_detail == "Industry") %>%
  pivot_wider(names_from = energy_price_flow_detail, 
              values_from = energy_price_value) %>%
  #filter(energy_price_flow_detail == "Total price (USD/unit)") %>%
  filter(product_detail == "Liquefied petroleum gas (litre)")  %>%
  janitor::clean_names() %>%
  group_by(country) 

ggplot(energy_co2_advanced_economy_2000,
       aes(x = year)) +
  geom_point(aes(y = total_price_usd_unit, color = country)) + 
  geom_line(aes(y = total_tax_usd_unit, color = country)) + 
  scale_y_continuous(limits = c(0,1.25),name = "Total price (USD/unit)",
                     sec.axis = sec_axis(~.*0.1, 
                                         name = "Total tax (USD/unit)")) +
  facet_grid(sector_detail ~.)

#https://r-graph-gallery.com/line-chart-dual-Y-axis-ggplot2.html

##CO2 emission

ggplot(energy_co2_advanced_2000,
       aes(x = year, y = co2_per_capital)
       ) + #why the color is not working here
  geom_point(aes(color = country)) + 
  geom_smooth(method = lm, level = 0.99) 
## `geom_smooth()` using formula 'y ~ x'

  #https://www.r-bloggers.com/2021/09/draw-a-trend-line-using-ggplot-quick-guide/

#Interaction

energy_co2_g20_2000 <- energy_co2_2 %>%
  drop_na(energy_price_value) %>%
  drop_na(co2_per_capital)%>%
  filter(year == 2019) %>%
  filter(g20 == 1 ) %>%
  #filter(sector_detail == "Industry") %>%
  filter(energy_price_flow_detail == c("Total price (USD/unit)", "Total tax (USD/unit)")) %>%
  #filter(product_detail == "Liquefied petroleum gas (litre)")  %>%
  pivot_wider(names_from = energy_price_flow_detail, 
              values_from = energy_price_value) %>%
  janitor::clean_names() %>%
  group_by(country) 

#view(energy_co2_g20_2000)

energy_co2_g20_2000 %>%
  ggplot(aes(axis1 = product_detail, 
             axis2 = total_tax_usd_unit, 
             axis3 = total_price_usd_unit,
             y = co2_per_capital))+
  geom_alluvium(aes(fill = country)) +
  geom_stratum() 

#regression to be added
#test other variables

#Prediction time series - to be added