Intro to comtradr (baseret på vignetten: https://cran.r-project.org/web/packages/comtradr/vignettes/comtradr-vignette.html)

Package information

API wrapper for the UN Comtrade Database, which features inter-country trade data dating back to the early 1990’s. Full API documentation can be found here. This package allows users to interact with the API directly from R, and features functions for making queries and importing data.

Install and load comtradr

Install from CRAN and load the package in R:

##install.packages("comtradr")
library(comtradr)
ct_commodity_lookup("aluminium")
## $aluminium
##  [1] "2510 - Natural calcium phosphates; natural aluminium calcium phosphates and phosphatic chalk"                                                                                                                    
##  [2] "251010 - Natural calcium phosphates, natural aluminium calcium phosphates and phosphatic chalk; unground"                                                                                                        
##  [3] "251020 - Natural calcium phosphates, natural aluminium calcium phosphates and phosphatic chalk; ground"                                                                                                          
##  [4] "2606 - Aluminium ores and concentrates"                                                                                                                                                                          
##  [5] "260600 - Aluminium ores and concentrates"                                                                                                                                                                        
##  [6] "262040 - Slag, ash and residues; (not from the manufacture of iron or steel), containing mainly aluminium"                                                                                                       
##  [7] "2818 - Aluminium oxide (including artificial corundum); aluminium hydroxide"                                                                                                                                     
##  [8] "281810 - Aluminium oxide; artificial corundum"                                                                                                                                                                   
##  [9] "281820 - Aluminium oxide; other than artificial corundum"                                                                                                                                                        
## [10] "281830 - Aluminium hydroxide"                                                                                                                                                                                    
## [11] "282612 - Fluorides; of aluminium"                                                                                                                                                                                
## [12] "282619 - Fluorides; other than of ammonium or sodium or aluminium"                                                                                                                                               
## [13] "282732 - Chlorides; of aluminium"                                                                                                                                                                                
## [14] "282739 - Chlorides; other than of ammonium, calcium, magnesium, aluminium, iron, cobalt, nickel and zinc"                                                                                                        
## [15] "283322 - Sulphates; of aluminium"                                                                                                                                                                                
## [16] "721060 - Iron or non-alloy steel; flat-rolled, width 600mm or more, plated or coated with aluminium"                                                                                                             
## [17] "721061 - Iron or non-alloy steel; flat-rolled, width 600mm or more, plated or coated with aluminium zinc-alloys"                                                                                                 
## [18] "721069 - Iron or non-alloy steel; flat-rolled, width 600mm or more, plated or coated with aluminium, other than plated or coated with aluminium zinc-alloys"                                                     
## [19] "76 - Aluminium and articles thereof"                                                                                                                                                                             
## [20] "7601 - Aluminium; unwrought"                                                                                                                                                                                     
## [21] "760110 - Aluminium; unwrought, (not alloyed)"                                                                                                                                                                    
## [22] "760120 - Aluminium; unwrought, alloys"                                                                                                                                                                           
## [23] "7602 - Aluminium; waste and scrap"                                                                                                                                                                               
## [24] "760200 - Aluminium; waste and scrap"                                                                                                                                                                             
## [25] "7603 - Aluminium; powders and flakes"                                                                                                                                                                            
## [26] "760310 - Aluminium; powders of non-lamellar structure"                                                                                                                                                           
## [27] "760320 - Aluminium; powders of lamellar structure, flakes"                                                                                                                                                       
## [28] "7604 - Aluminium; bars, rods and profiles"                                                                                                                                                                       
## [29] "760410 - Aluminium; (not alloyed), bars, rods and profiles"                                                                                                                                                      
## [30] "760421 - Aluminium; alloys, hollow profiles"                                                                                                                                                                     
## [31] "760429 - Aluminium; alloys, bars, rods and profiles, other than hollow"                                                                                                                                          
## [32] "7605 - Aluminium wire"                                                                                                                                                                                           
## [33] "760511 - Aluminium; (not alloyed), wire, maximum cross-sectional dimension exceeds 7mm"                                                                                                                          
## [34] "760519 - Aluminium; (not alloyed), wire, maximum cross-sectional dimension is 7mm or less"                                                                                                                       
## [35] "760521 - Aluminium; alloys, wire, maximum cross-sectional dimension exceeding 7mm"                                                                                                                               
## [36] "760529 - Aluminium; alloys, wire, maximum cross-sectional dimension is 7mm or less"                                                                                                                              
## [37] "7606 - Aluminium; plates, sheets and strip, thickness exceeding 0.2mm"                                                                                                                                           
## [38] "760611 - Aluminium; plates, sheets and strip, thickness exceeding 0.2mm, (not alloyed), rectangular (including square)"                                                                                          
## [39] "760612 - Aluminium; plates, sheets and strip, thickness exceeding 0.2mm, alloys, rectangular (including square)"                                                                                                 
## [40] "760691 - Aluminium; plates, sheets and strip, thickness exceeding 0.2mm, not alloyed, (not rectangular or square)"                                                                                               
## [41] "760692 - Aluminium; plates, sheets and strip, thickness exceeding 0.2mm, alloys, (not rectangular or square)"                                                                                                    
## [42] "7607 - Aluminium foil (whether or not printed or backed with paper, paperboard, plastics or similar backing materials) of a thickness (excluding any backing) not exceeding 0.2mm"                               
## [43] "760711 - Aluminium; foil, (not backed), rolled (but not further worked), of a thickness not exceeding 0.2mm"                                                                                                     
## [44] "760719 - Aluminium; foil, (not backed), of a thickness not exceeding 0.2mm, not rolled"                                                                                                                          
## [45] "760720 - Aluminium; foil, backed with paper, paperboard, plastics or similar backing materials, of a thickness (excluding any backing) not exceeding 0.2mm"                                                      
## [46] "7608 - Aluminium; tubes and pipes"                                                                                                                                                                               
## [47] "760810 - Aluminium; tubes and pipes, not alloyed"                                                                                                                                                                
## [48] "760820 - Aluminium; tubes and pipes, alloys"                                                                                                                                                                     
## [49] "7609 - Aluminium; tube or pipe fittings (eg couplings, elbows, sleeves)"                                                                                                                                         
## [50] "760900 - Aluminium; tube or pipe fittings (eg couplings, elbows, sleeves)"                                                                                                                                       
## [51] "7610 - Aluminium; structures (excluding prefabricated buildings of heading no. 9406) and parts (eg bridges and sections, towers, lattice masts, etc) plates, rods, profiles and tubes for structures"            
## [52] "761010 - Aluminium; structures (excluding prefabricated buildings of heading no. 9406) and parts of structures, doors, windows and their frames and thresholds for doors"                                        
## [53] "761090 - Aluminium; structures (excluding prefabricated buildings of heading no. 9406) and parts of structures, n.e.s. in heading no. 7610, plates, rods, profiles, tubes and the like"                          
## [54] "7611 - Aluminium; reservoirs, tanks, vats and the like for material (not compressed or liquefied gas) of capacity over 300l, whether or not lined, heat-insulated, not fitted with mechanical, thermal equipment"
## [55] "761100 - Aluminium; reservoirs, tanks, vats and similar containers, for material (not compressed or liquefied gas), of a capacity over 300l, whether or not lined, not fitted with mechanical/thermal equipment" 
## [56] "7612 - Aluminium casks, drums, cans, boxes etc (including rigid, collapsible tubular containers), for materials other than compressed, liquefied gas, 300l capacity or less, lined, heat-insulated or not"       
## [57] "761210 - Aluminium; collapsible tubular containers, for any material, (not compressed or liquefied gas), 300l capacity or less, whether or not lined, not fitted with mechanical/thermal equipment"              
## [58] "761290 - Aluminium; casks, drums, cans, boxes and the like for any material (not compressed or liquefied gas), 300l capacity or less, whether or not lined or heat-insulated, no mechanical or thermal equipment"
## [59] "7613 - Aluminium; containers for compressed or liquefied gas"                                                                                                                                                    
## [60] "761300 - Aluminium; containers for compressed or liquefied gas"                                                                                                                                                  
## [61] "7614 - Aluminium; stranded wire, cables, plaited bands and the like, (not electrically insulated)"                                                                                                               
## [62] "761410 - Aluminium; stranded wire, cables, plaited bands and the like, (not electrically insulated), with steel core"                                                                                            
## [63] "761490 - Aluminium; stranded wire, cables, plaited bands and the like, (not electrically insulated), other than steel core"                                                                                      
## [64] "7615 - Aluminium; table, kitchen or other household articles and parts thereof, pot scourers and scouring or polishing pads, gloves and the like, sanitary ware and parts thereof"                               
## [65] "761510 - Aluminium; table, kitchen or other household articles and parts thereof; pot scourers and scouring or polishing pads, gloves and the like"                                                              
## [66] "761511 - Aluminium; pot scourers and scouring or polishing pads, gloves and the like"                                                                                                                            
## [67] "761519 - Aluminium; table, kitchen or other household articles and parts thereof, n.e.s. in item no. 7615.11"                                                                                                    
## [68] "761520 - Aluminium; sanitary ware and parts thereof"                                                                                                                                                             
## [69] "7616 - Aluminium; articles n.e.s. in chapter 76"                                                                                                                                                                 
## [70] "761610 - Aluminium; nails, tacks, staples (other than those of heading no. 8305), screws, bolts, nuts, screw hooks, rivets, cotters, cotter-pins, washers and similar articles"                                  
## [71] "761690 - Aluminium; articles n.e.s. in heading no. 7616"                                                                                                                                                         
## [72] "761691 - Aluminium; cloth, grill, netting and fencing, of aluminium wire"                                                                                                                                        
## [73] "761699 - Aluminium; n.e.s. in heading no. 7616"                                                                                                                                                                  
## [74] "853222 - Electrical capacitors; fixed, aluminium electrolytic"

Making API calls

Lets say we want to get data on all imports into EU-28 (now 27, but I have included the UK since it is historical) from Germany and Russia (equivalent of exports from Germany and Russia) for all Years. We can specify it as exports or imports, we just have to switch around reporters and partners, so the direction is always seen from the perspective of the reporter:

 Alu <- ct_search(reporters = c("Germany", "Russian Federation"),  
               partners = c( "France", "Germany", "Italy", "Spain", "Sweden"), 
               trade_direction = "exports",
               start_date = 2010, 
               end_date = 2014,
               commod_codes = c("7601"))

str(Alu)
## 'data.frame':    44 obs. of  35 variables:
##  $ classification        : chr  "H4" "H4" "H4" "H4" ...
##  $ year                  : int  2012 2012 2012 2012 2012 2012 2012 2012 2013 2013 ...
##  $ period                : int  2012 2012 2012 2012 2012 2012 2012 2012 2013 2013 ...
##  $ period_desc           : chr  "2012" "2012" "2012" "2012" ...
##  $ aggregate_level       : int  4 4 4 4 4 4 4 4 4 4 ...
##  $ is_leaf_code          : int  0 0 0 0 0 0 0 0 0 0 ...
##  $ trade_flow_code       : int  2 2 2 2 2 2 2 2 2 2 ...
##  $ trade_flow            : chr  "Export" "Export" "Export" "Export" ...
##  $ reporter_code         : int  276 276 276 276 643 643 643 643 276 276 ...
##  $ reporter              : chr  "Germany" "Germany" "Germany" "Germany" ...
##  $ reporter_iso          : chr  "DEU" "DEU" "DEU" "DEU" ...
##  $ partner_code          : int  251 381 724 752 251 276 381 752 251 381 ...
##  $ partner               : chr  "France" "Italy" "Spain" "Sweden" ...
##  $ partner_iso           : chr  "FRA" "ITA" "ESP" "SWE" ...
##  $ second_partner_code   : logi  NA NA NA NA NA NA ...
##  $ second_partner        : chr  NA NA NA NA ...
##  $ second_partner_iso    : chr  NA NA NA NA ...
##  $ customs_proc_code     : chr  NA NA NA NA ...
##  $ customs               : chr  NA NA NA NA ...
##  $ mode_of_transport_code: chr  NA NA NA NA ...
##  $ mode_of_transport     : chr  NA NA NA NA ...
##  $ commodity_code        : chr  "7601" "7601" "7601" "7601" ...
##  $ commodity             : chr  "Aluminium; unwrought" "Aluminium; unwrought" "Aluminium; unwrought" "Aluminium; unwrought" ...
##  $ qty_unit_code         : int  8 8 8 8 8 8 8 8 8 8 ...
##  $ qty_unit              : chr  "Weight in kilograms" "Weight in kilograms" "Weight in kilograms" "Weight in kilograms" ...
##  $ alt_qty_unit_code     : logi  NA NA NA NA NA NA ...
##  $ alt_qty_unit          : chr  NA NA NA NA ...
##  $ qty                   : int  71603401 17893606 2398757 2730606 3042818 2667164 31004283 22538283 69171973 26395660 ...
##  $ alt_qty               : logi  NA NA NA NA NA NA ...
##  $ netweight_kg          : int  71603401 17893606 2398757 2730606 3042818 2667164 31004283 22538283 69171973 26395660 ...
##  $ gross_weight_kg       : logi  NA NA NA NA NA NA ...
##  $ trade_value_usd       : int  167562433 47970367 5776587 7373351 6137763 5372433 61498134 42173418 159774262 72057838 ...
##  $ cif_trade_value_usd   : logi  NA NA NA NA NA NA ...
##  $ fob_trade_value_usd   : logi  NA NA NA NA NA NA ...
##  $ flag                  : int  0 0 6 0 0 0 0 0 0 0 ...
##  - attr(*, "url")= chr "https://comtrade.un.org/api/get?max=50000&type=C&freq=A&px=HS&ps=2010%2C2011%2C2012%2C2013%2C2014&r=276%2C643&p"| __truncated__
##  - attr(*, "time_stamp")= POSIXct[1:1], format: "2021-04-27 12:11:08"
##  - attr(*, "req_duration")= num 4.48

API search metadata

In addition to the trade data, each API return object contains metadata as attributes (kan angives som dokumentation for de data man har nedhentet…).

# The url of the API call.
attributes(Alu)$url
## [1] "https://comtrade.un.org/api/get?max=50000&type=C&freq=A&px=HS&ps=2010%2C2011%2C2012%2C2013%2C2014&r=276%2C643&p=251%2C276%2C381%2C724%2C752&rg=2&cc=7601&fmt=json&head=H"
# The date-time of the API call.
attributes(Alu)$time_stamp
## [1] "2021-04-27 12:11:08 CEST"
# The total duration of the API call, in seconds.
attributes(Alu)$req_duration
## [1] 4.48121

“Polished” Column Headers

ct_pretty_cols is a named vector of column header values that provide the option of using column headers that are more polished and human-friendly than those returned by the API function ct_search. The polished column headers may be useful when plotting the Comtrade data, or for use in publication tables. The data can be accessed directly by using data(“ct_pretty_cols”), but there is also a function for applying the polished headers to comtradr data frames, ct_use_pretty_cols. Below is a quick demonstration.

# Column headers returned from function ct_search
colnames(Alu)
##  [1] "classification"         "year"                   "period"                
##  [4] "period_desc"            "aggregate_level"        "is_leaf_code"          
##  [7] "trade_flow_code"        "trade_flow"             "reporter_code"         
## [10] "reporter"               "reporter_iso"           "partner_code"          
## [13] "partner"                "partner_iso"            "second_partner_code"   
## [16] "second_partner"         "second_partner_iso"     "customs_proc_code"     
## [19] "customs"                "mode_of_transport_code" "mode_of_transport"     
## [22] "commodity_code"         "commodity"              "qty_unit_code"         
## [25] "qty_unit"               "alt_qty_unit_code"      "alt_qty_unit"          
## [28] "qty"                    "alt_qty"                "netweight_kg"          
## [31] "gross_weight_kg"        "trade_value_usd"        "cif_trade_value_usd"   
## [34] "fob_trade_value_usd"    "flag"
## Tell R to use the pretty column headers
Alu<-ct_use_pretty_cols(Alu)
## See the difference...
colnames(Alu)
##  [1] "Classification"               "Year"                        
##  [3] "Period"                       "Period Description"          
##  [5] "Aggregate Level"              "Is Leaf Code"                
##  [7] "Trade Flow Code"              "Trade Flow"                  
##  [9] "Reporter Code"                "Reporter Country"            
## [11] "Reporter ISO"                 "Partner Code"                
## [13] "Partner Country"              "Partner ISO"                 
## [15] "Second Partner Code"          "Second Partner Country"      
## [17] "Second Partner ISO"           "Customs Procurement Code"    
## [19] "Customs"                      "Mode of Transportation Code" 
## [21] "Mode of Transportation"       "Commodity Code"              
## [23] "Commodity"                    "Quantity Unit Code"          
## [25] "Quantity Unit"                "Alternate Quantity Unit Code"
## [27] "Alternate Quantity Unit"      "Quantity"                    
## [29] "Alternate Quantity"           "Net Weight kg"               
## [31] "Gross Weight kg"              "Trade Value usd"             
## [33] "CIF Trade Value usd"          "FOB Trade Value usd"         
## [35] "Flag"

Visualize downloaded data

Once the data is collected, we can use it to create some basic visualizations.

Plot 1: Plot total value (USD) of German and Russian exports of Aluminium, unwrought (7601) to five partner countries for all years that are available in the Comtrade database

Alu_Germany <- ct_search(reporters="Germany", 
                              partners = c( "France", "Germany", "Italy", "Spain", "Sweden"), 
                             trade_direction = "exports", 
                             commod_codes = c("7601"))

library(ggplot2)
Alu_1 <- ct_use_pretty_cols(Alu_Germany)

ggplot(Alu_1, aes(Year, `Trade Value usd`, color = factor(`Partner Country`), 
               shape = factor(`Partner Country`))) +
  geom_point(size = 2) +
  geom_line(size = 1) +
  scale_x_continuous(limits = c(min(Alu_1$Year), max(Alu_1$Year)), 
                     breaks = seq.int(min(Alu_1$Year), max(Alu_1$Year), 2)) +
  scale_color_manual(values = c("blue", "green", "orange", "yellow"), 
                     name = "Destination\nCountry") +
  scale_shape_discrete(name = "Destination\nCountry") +
  labs(title = "Total Value (USD) of German Alu (7601) Exports, by Year") +
  theme(axis.text.x = element_text(angle = 45, vjust = 1, hjust = 1))

Alu_Russia<- ct_search(reporters="Russian Federation", 
                              partners = c( "France", "Germany", "Italy", "Spain", "Sweden"), 
                             trade_direction = "exports", 
                             commod_codes = c("7601"))

library(ggplot2)
Alu_2 <- ct_use_pretty_cols(Alu_Russia)

ggplot(Alu_2, aes(Year, `Trade Value usd`, color = factor(`Partner Country`), 
               shape = factor(`Partner Country`))) +
  geom_point(size = 2) +
  geom_line(size = 1) +
  scale_x_continuous(limits = c(min(Alu_1$Year), max(Alu_1$Year)), 
                     breaks = seq.int(min(Alu_1$Year), max(Alu_1$Year), 2)) +
  scale_color_manual(values = c("blue", "black", "green", "orange", "yellow"), 
                     name = "Destination\nCountry") +
  scale_shape_discrete(name = "Destination\nCountry") +
  labs(title = "Total Value (USD) of Russian Alu (7601) Exports, by Year") +
  theme(axis.text.x = element_text(angle = 45, vjust = 1, hjust = 1))

Plot 2: Plot the top eight destination countries/areas of Russian exports, by weight (KG), for 2015-2019

Alu_Russia_2 <- ct_search(reporters = "Russian Federation", 
                partners = "All", 
                trade_direction = "exports", 
                start_date = 2015, 
                end_date = 2019, 
                commod_codes = c("7601"))
library(ggplot2)
library(dplyr)
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
Alu_3 <- ct_use_pretty_cols(Alu_Russia_2)

plotAlu<- Alu_3 %>% 
  group_by_(.dots = c("`Partner Country`", "Year")) %>% 
  summarise(kg = as.numeric(sum(`Net Weight kg`, na.rm = TRUE))) %>% 
  as_data_frame()
## Warning: `as_data_frame()` was deprecated in tibble 2.0.0.
## Please use `as_tibble()` instead.
## The signature and semantics have changed, see `?as_tibble`.
## Warning: `group_by_()` was deprecated in dplyr 0.7.0.
## Please use `group_by()` instead.
## See vignette('programming') for more help
## `summarise()` has grouped output by 'Partner Country'. You can override using the `.groups` argument.
top8 <- plotAlu %>% 
  group_by(`Partner Country`) %>% 
  summarise(kg = as.numeric(sum(kg, na.rm = TRUE))) %>% 
  top_n(8, kg) %>%
  arrange(desc(kg)) %>% 
  .[["Partner Country"]]
plotAlu <- plotAlu %>% filter(`Partner Country` %in% top8)

qplot(Year, kg, data = plotAlu) + 
  geom_line(data = plotAlu[plotAlu$`Partner Country` %in% names(which(table(plotAlu$`Partner Country`) > 1)), ]) + 
  xlim(min(plotAlu$Year), max(plotAlu$Year)) + 
  labs(title = "Weight (KG) of Russian Alu Exports, by Destination Area, 2015 - 2019") + 
  theme(axis.text.x = element_text(angle = 45, vjust = 1, hjust = 1), 
        axis.text = element_text(size = 7)) + 
  facet_wrap(~factor(`Partner Country`, levels = top8), scales = "free", nrow = 2, ncol = 4)

Building the full dataset

library(comtradr)

Germany_1 <- ct_search(reporters = c("Germany"),  
               partners = c( "All"), 
               trade_direction = "exports",
               start_date = 1996, 
               end_date = 2000,
               commod_codes = c("7601"))

Germany_2 <- ct_search(reporters = c("Germany"),  
               partners = c( "All"), 
               trade_direction = "exports",
               start_date = 2001, 
               end_date = 2005,
               commod_codes = c("7601"))

Germany_3 <- ct_search(reporters = c("Germany"),  
               partners = c( "All"), 
               trade_direction = "exports",
               start_date = 2006, 
               end_date = 2010,
               commod_codes = c("7601"))

Germany_4 <- ct_search(reporters = c("Germany"),  
               partners = c( "All"), 
               trade_direction = "exports",
               start_date = 2011, 
               end_date = 2015,
               commod_codes = c("7601"))

Germany_5 <- ct_search(reporters = c("Germany"),  
               partners = c( "All"), 
               trade_direction = "exports",
               start_date = 2016, 
               end_date = 2020,
               commod_codes = c("7601"))

Germany <- rbind(Germany_1, Germany_2, Germany_3, Germany_4, Germany_5)

Russia_1 <- ct_search(reporters = c("Russian Federation"),  
               partners = c( "All"), 
               trade_direction = "exports",
               start_date = 1996, 
               end_date = 2000,
               commod_codes = c("7601"))

Russia_2 <- ct_search(reporters = c("Russian Federation"),  
               partners = c( "All"), 
               trade_direction = "exports",
               start_date = 2001, 
               end_date = 2005,
               commod_codes = c("7601"))

Russia_3 <- ct_search(reporters = c("Russian Federation"),  
               partners = c( "All"), 
               trade_direction = "exports",
               start_date = 2006, 
               end_date = 2010,
               commod_codes = c("7601"))

Russia_4 <- ct_search(reporters = c("Russian Federation"),  
               partners = c( "All"), 
               trade_direction = "exports",
               start_date = 2011, 
               end_date = 2015,
               commod_codes = c("7601"))

Russia_5 <- ct_search(reporters = c("Russian Federation"),  
               partners = c( "All"), 
               trade_direction = "exports",
               start_date = 2016, 
               end_date = 2020,
               commod_codes = c("7601"))

Russia <- rbind(Russia_1, Russia_2, Russia_3, Russia_4, Russia_5)

ALUM <- rbind(Germany, Russia)
ALUM <- ct_use_pretty_cols(ALUM)

ALUM <- ALUM[ALUM$`Partner Country` != "World", ]

ALUM$LCI_rus <- NA
ALUM$LCI_ger <- NA

ALUM$LCI_rus[ALUM$Year == 2012] <- 4.73
ALUM$LCI_rus[ALUM$Year == 2013] <- 5.05
ALUM$LCI_rus[ALUM$Year == 2014] <- 4.565
ALUM$LCI_rus[ALUM$Year == 2015] <- 3.12
ALUM$LCI_rus[ALUM$Year == 2016] <- 3.075
ALUM$LCI_rus[ALUM$Year == 2017] <- 4.25
ALUM$LCI_rus[ALUM$Year == 2018] <- 4.38
ALUM$LCI_rus[ALUM$Year == 2019] <- 4.45

ALUM$LCI_ger[ALUM$Year == 2012] <- 25.2
ALUM$LCI_ger[ALUM$Year == 2013] <- 25.9
ALUM$LCI_ger[ALUM$Year == 2014] <- 27.35
ALUM$LCI_ger[ALUM$Year == 2015] <- 28.9
ALUM$LCI_ger[ALUM$Year == 2016] <- 29.6
ALUM$LCI_ger[ALUM$Year == 2017] <- 30.8
ALUM$LCI_ger[ALUM$Year == 2018] <- 31.25
ALUM$LCI_ger[ALUM$Year == 2019] <- 32.2

ALUM$LCI_rel <- ALUM$LCI_ger/ALUM$LCI_rus

ALUM$El_pris_rus <- NA
ALUM$El_pris_ger <- NA

ALUM$El_pris_rus[ALUM$Year == 2012] <- NA
ALUM$El_pris_rus[ALUM$Year == 2013] <- 0.0182
ALUM$El_pris_rus[ALUM$Year == 2014] <- 0.0132
ALUM$El_pris_rus[ALUM$Year == 2015] <- 0.0167
ALUM$El_pris_rus[ALUM$Year == 2016] <- 0.0156
ALUM$El_pris_rus[ALUM$Year == 2017] <- 0.0129
ALUM$El_pris_rus[ALUM$Year == 2018] <- 0.0116
ALUM$El_pris_rus[ALUM$Year == 2019] <- 0.0162

ALUM$El_pris_ger[ALUM$Year == 2012] <- NA
ALUM$El_pris_ger[ALUM$Year == 2013] <- 0.1425
ALUM$El_pris_ger[ALUM$Year == 2014] <- 0.1586
ALUM$El_pris_ger[ALUM$Year == 2015] <- 0.1509
ALUM$El_pris_ger[ALUM$Year == 2016] <- 0.1505
ALUM$El_pris_ger[ALUM$Year == 2017] <- 0.1519
ALUM$El_pris_ger[ALUM$Year == 2018] <- 0.1499
ALUM$El_pris_ger[ALUM$Year == 2019] <- 0.1557

ALUM$El_pris_rel <- ALUM$El_pris_ger/ALUM$El_pris_rus

ALUM_EU <- ALUM[which(ALUM$`Partner ISO`== "AUT" | 
                ALUM$`Partner ISO`== "BEL" |
                ALUM$`Partner ISO`== "BGR" |
                ALUM$`Partner ISO`== "HRV" |
                ALUM$`Partner ISO`== "CYP" |
                ALUM$`Partner ISO`== "DEU" |
                ALUM$`Partner ISO`== "CZE" |
                ALUM$`Partner ISO`== "DNK" |
                ALUM$`Partner ISO`== "AND" |
                ALUM$`Partner ISO`== "EST" |
                ALUM$`Partner ISO`== "FIN" |
                ALUM$`Partner ISO`== "FRA" |
                ALUM$`Partner ISO`== "GRC" |
                ALUM$`Partner ISO`== "HUN" |
                ALUM$`Partner ISO`== "IRL" |
                ALUM$`Partner ISO`== "ITA" |
                ALUM$`Partner ISO`== "LVA" |
                ALUM$`Partner ISO`== "LTU" |
                ALUM$`Partner ISO`== "NLD" |
                ALUM$`Partner ISO`== "POL" |
                ALUM$`Partner ISO`== "PRT" |
                ALUM$`Partner ISO`== "ROU" |
                ALUM$`Partner ISO`== "SVK" |
                ALUM$`Partner ISO`== "SVN" |
                ALUM$`Partner ISO`== "ESP" |
                ALUM$`Partner ISO`== "SWE" |
                ALUM$`Partner ISO`== "GBR"),]

ALUM_EU_GER <- ALUM_EU[ALUM_EU$`Reporter Country`=="Germany",]
ALUM_EU_RUS <- ALUM_EU[ALUM_EU$`Reporter Country`=="Russian Federation",]

Estimating models

## Først prøver vi nogle simple modeller med en dummy for partnerland, de to relativpriser og opdelt for Tykslands og Ruslands eksport til EU landene hver for sig...

Fit1 <- lm(log(`Trade Value usd`) ~ factor(`Partner Country`) + LCI_rel + El_pris_rel, data = ALUM_EU_GER)
summary(Fit1)
## 
## Call:
## lm(formula = log(`Trade Value usd`) ~ factor(`Partner Country`) + 
##     LCI_rel + El_pris_rel, data = ALUM_EU_GER)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -6.5994 -0.2953 -0.0136  0.3946  3.8751 
## 
## Coefficients:
##                                          Estimate Std. Error t value Pr(>|t|)
## (Intercept)                              19.25731    0.82576  23.321  < 2e-16
## factor(`Partner Country`)Belgium         -0.79535    0.59736  -1.331   0.1853
## factor(`Partner Country`)Bulgaria        -8.21567    0.59736 -13.753  < 2e-16
## factor(`Partner Country`)Croatia         -5.80206    0.59736  -9.713  < 2e-16
## factor(`Partner Country`)Cyprus          -8.37378    1.19989  -6.979 1.19e-10
## factor(`Partner Country`)Czechia         -1.30869    0.59736  -2.191   0.0302
## factor(`Partner Country`)Denmark         -4.71052    0.59736  -7.886 9.11e-13
## factor(`Partner Country`)Estonia         -8.29505    0.59736 -13.886  < 2e-16
## factor(`Partner Country`)Finland         -4.18223    0.59736  -7.001 1.06e-10
## factor(`Partner Country`)France          -0.96015    0.59736  -1.607   0.1103
## factor(`Partner Country`)Greece          -4.28337    0.59736  -7.171 4.33e-11
## factor(`Partner Country`)Hungary         -3.04950    0.59736  -5.105 1.09e-06
## factor(`Partner Country`)Ireland        -10.13856    0.70161 -14.450  < 2e-16
## factor(`Partner Country`)Italy           -1.52285    0.59736  -2.549   0.0119
## factor(`Partner Country`)Latvia          -8.40237    0.65462 -12.836  < 2e-16
## factor(`Partner Country`)Lithuania       -7.73712    0.62202 -12.439  < 2e-16
## factor(`Partner Country`)Netherlands     -1.42529    0.59736  -2.386   0.0184
## factor(`Partner Country`)Poland          -1.05542    0.59736  -1.767   0.0795
## factor(`Partner Country`)Portugal        -5.68379    0.59736  -9.515  < 2e-16
## factor(`Partner Country`)Romania         -3.51584    0.59736  -5.886 2.94e-08
## factor(`Partner Country`)Slovakia        -4.03331    0.59736  -6.752 3.87e-10
## factor(`Partner Country`)Slovenia        -3.13783    0.59736  -5.253 5.64e-07
## factor(`Partner Country`)Spain           -2.41771    0.59736  -4.047 8.65e-05
## factor(`Partner Country`)Sweden          -3.49122    0.59736  -5.844 3.58e-08
## factor(`Partner Country`)United Kingdom  -3.26390    0.59736  -5.464 2.15e-07
## LCI_rel                                   0.02673    0.05793   0.461   0.6453
## El_pris_rel                              -0.01822    0.05164  -0.353   0.7247
##                                            
## (Intercept)                             ***
## factor(`Partner Country`)Belgium           
## factor(`Partner Country`)Bulgaria       ***
## factor(`Partner Country`)Croatia        ***
## factor(`Partner Country`)Cyprus         ***
## factor(`Partner Country`)Czechia        *  
## factor(`Partner Country`)Denmark        ***
## factor(`Partner Country`)Estonia        ***
## factor(`Partner Country`)Finland        ***
## factor(`Partner Country`)France            
## factor(`Partner Country`)Greece         ***
## factor(`Partner Country`)Hungary        ***
## factor(`Partner Country`)Ireland        ***
## factor(`Partner Country`)Italy          *  
## factor(`Partner Country`)Latvia         ***
## factor(`Partner Country`)Lithuania      ***
## factor(`Partner Country`)Netherlands    *  
## factor(`Partner Country`)Poland         .  
## factor(`Partner Country`)Portugal       ***
## factor(`Partner Country`)Romania        ***
## factor(`Partner Country`)Slovakia       ***
## factor(`Partner Country`)Slovenia       ***
## factor(`Partner Country`)Spain          ***
## factor(`Partner Country`)Sweden         ***
## factor(`Partner Country`)United Kingdom ***
## LCI_rel                                    
## El_pris_rel                                
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 1.118 on 136 degrees of freedom
##   (424 observations deleted due to missingness)
## Multiple R-squared:  0.8698, Adjusted R-squared:  0.8449 
## F-statistic: 34.93 on 26 and 136 DF,  p-value: < 2.2e-16
Fit2 <- lm(log(`Trade Value usd`) ~ factor(`Partner Country`) + LCI_rel + El_pris_rel, data = ALUM_EU_RUS)
summary(Fit2)
## 
## Call:
## lm(formula = log(`Trade Value usd`) ~ factor(`Partner Country`) + 
##     LCI_rel + El_pris_rel, data = ALUM_EU_RUS)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -6.0907 -0.4474  0.0100  0.4454  2.3095 
## 
## Coefficients:
##                                         Estimate Std. Error t value Pr(>|t|)
## (Intercept)                             11.33104    0.93534  12.114  < 2e-16
## factor(`Partner Country`)Belgium         2.67400    0.74370   3.596 0.000472
## factor(`Partner Country`)Bulgaria        4.65963    0.69537   6.701 7.29e-10
## factor(`Partner Country`)Croatia         5.02873    0.69537   7.232 5.01e-11
## factor(`Partner Country`)Cyprus          4.79181    0.69537   6.891 2.82e-10
## factor(`Partner Country`)Czechia         2.47319    0.71621   3.453 0.000768
## factor(`Partner Country`)Estonia         3.32218    0.69537   4.778 5.11e-06
## factor(`Partner Country`)Finland         1.82420    0.74453   2.450 0.015735
## factor(`Partner Country`)France          3.05311    0.69537   4.391 2.46e-05
## factor(`Partner Country`)Germany         5.77003    0.69537   8.298 1.90e-13
## factor(`Partner Country`)Greece          6.84835    0.69537   9.848  < 2e-16
## factor(`Partner Country`)Hungary         0.78379    0.71549   1.095 0.275531
## factor(`Partner Country`)Ireland         1.76621    1.25365   1.409 0.161487
## factor(`Partner Country`)Italy           6.66252    0.69537   9.581  < 2e-16
## factor(`Partner Country`)Latvia          0.96827    0.74710   1.296 0.197467
## factor(`Partner Country`)Lithuania       2.67454    0.74710   3.580 0.000499
## factor(`Partner Country`)Netherlands     8.61371    0.69537  12.387  < 2e-16
## factor(`Partner Country`)Poland          6.18037    0.69537   8.888 7.97e-15
## factor(`Partner Country`)Romania         1.17726    0.78942   1.491 0.138528
## factor(`Partner Country`)Slovakia        3.13409    0.69537   4.507 1.55e-05
## factor(`Partner Country`)Slovenia        4.02585    0.71621   5.621 1.27e-07
## factor(`Partner Country`)Spain           4.74854    0.69537   6.829 3.85e-10
## factor(`Partner Country`)Sweden          5.00863    0.69537   7.203 5.81e-11
## factor(`Partner Country`)United Kingdom  6.11903    0.69537   8.800 1.28e-14
## LCI_rel                                  0.12478    0.06229   2.003 0.047442
## El_pris_rel                             -0.03662    0.05444  -0.673 0.502440
##                                            
## (Intercept)                             ***
## factor(`Partner Country`)Belgium        ***
## factor(`Partner Country`)Bulgaria       ***
## factor(`Partner Country`)Croatia        ***
## factor(`Partner Country`)Cyprus         ***
## factor(`Partner Country`)Czechia        ***
## factor(`Partner Country`)Estonia        ***
## factor(`Partner Country`)Finland        *  
## factor(`Partner Country`)France         ***
## factor(`Partner Country`)Germany        ***
## factor(`Partner Country`)Greece         ***
## factor(`Partner Country`)Hungary           
## factor(`Partner Country`)Ireland           
## factor(`Partner Country`)Italy          ***
## factor(`Partner Country`)Latvia            
## factor(`Partner Country`)Lithuania      ***
## factor(`Partner Country`)Netherlands    ***
## factor(`Partner Country`)Poland         ***
## factor(`Partner Country`)Romania           
## factor(`Partner Country`)Slovakia       ***
## factor(`Partner Country`)Slovenia       ***
## factor(`Partner Country`)Spain          ***
## factor(`Partner Country`)Sweden         ***
## factor(`Partner Country`)United Kingdom ***
## LCI_rel                                 *  
## El_pris_rel                                
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 1.108 on 119 degrees of freedom
##   (377 observations deleted due to missingness)
## Multiple R-squared:  0.816,  Adjusted R-squared:  0.7774 
## F-statistic: 21.11 on 25 and 119 DF,  p-value: < 2.2e-16
Fit3 <- lm(log(`Quantity`) ~ factor(`Partner Country`) + LCI_rel + El_pris_rel, data = ALUM_EU_GER)
summary(Fit3)
## 
## Call:
## lm(formula = log(Quantity) ~ factor(`Partner Country`) + LCI_rel + 
##     El_pris_rel, data = ALUM_EU_GER)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -6.8031 -0.3067  0.0000  0.4053  3.7123 
## 
## Coefficients:
##                                          Estimate Std. Error t value Pr(>|t|)
## (Intercept)                              18.17281    0.84384  21.536  < 2e-16
## factor(`Partner Country`)Belgium         -0.81429    0.61044  -1.334  0.18445
## factor(`Partner Country`)Bulgaria        -8.42889    0.61044 -13.808  < 2e-16
## factor(`Partner Country`)Croatia         -5.73722    0.61044  -9.399  < 2e-16
## factor(`Partner Country`)Cyprus          -8.24707    1.22617  -6.726 4.43e-10
## factor(`Partner Country`)Czechia         -1.39709    0.61044  -2.289  0.02364
## factor(`Partner Country`)Denmark         -4.75479    0.61044  -7.789 1.55e-12
## factor(`Partner Country`)Estonia         -8.54265    0.61044 -13.994  < 2e-16
## factor(`Partner Country`)Finland         -4.20702    0.61044  -6.892 1.88e-10
## factor(`Partner Country`)France          -0.95710    0.61044  -1.568  0.11923
## factor(`Partner Country`)Greece          -4.44202    0.61044  -7.277 2.46e-11
## factor(`Partner Country`)Hungary         -3.13060    0.61044  -5.128 9.85e-07
## factor(`Partner Country`)Ireland        -10.46018    0.71697 -14.589  < 2e-16
## factor(`Partner Country`)Italy           -1.61694    0.61044  -2.649  0.00903
## factor(`Partner Country`)Latvia          -8.43708    0.66895 -12.612  < 2e-16
## factor(`Partner Country`)Lithuania       -7.88392    0.63564 -12.403  < 2e-16
## factor(`Partner Country`)Netherlands     -1.51096    0.61044  -2.475  0.01454
## factor(`Partner Country`)Poland          -1.07217    0.61044  -1.756  0.08127
## factor(`Partner Country`)Portugal        -5.52117    0.61044  -9.045 1.36e-15
## factor(`Partner Country`)Romania         -3.62566    0.61044  -5.939 2.27e-08
## factor(`Partner Country`)Slovakia        -4.08883    0.61044  -6.698 5.11e-10
## factor(`Partner Country`)Slovenia        -3.09880    0.61044  -5.076 1.24e-06
## factor(`Partner Country`)Spain           -2.46122    0.61044  -4.032 9.17e-05
## factor(`Partner Country`)Sweden          -3.54468    0.61044  -5.807 4.29e-08
## factor(`Partner Country`)United Kingdom  -3.26142    0.61044  -5.343 3.75e-07
## LCI_rel                                   0.07884    0.05920   1.332  0.18517
## El_pris_rel                              -0.02527    0.05277  -0.479  0.63276
##                                            
## (Intercept)                             ***
## factor(`Partner Country`)Belgium           
## factor(`Partner Country`)Bulgaria       ***
## factor(`Partner Country`)Croatia        ***
## factor(`Partner Country`)Cyprus         ***
## factor(`Partner Country`)Czechia        *  
## factor(`Partner Country`)Denmark        ***
## factor(`Partner Country`)Estonia        ***
## factor(`Partner Country`)Finland        ***
## factor(`Partner Country`)France            
## factor(`Partner Country`)Greece         ***
## factor(`Partner Country`)Hungary        ***
## factor(`Partner Country`)Ireland        ***
## factor(`Partner Country`)Italy          ** 
## factor(`Partner Country`)Latvia         ***
## factor(`Partner Country`)Lithuania      ***
## factor(`Partner Country`)Netherlands    *  
## factor(`Partner Country`)Poland         .  
## factor(`Partner Country`)Portugal       ***
## factor(`Partner Country`)Romania        ***
## factor(`Partner Country`)Slovakia       ***
## factor(`Partner Country`)Slovenia       ***
## factor(`Partner Country`)Spain          ***
## factor(`Partner Country`)Sweden         ***
## factor(`Partner Country`)United Kingdom ***
## LCI_rel                                    
## El_pris_rel                                
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 1.142 on 136 degrees of freedom
##   (424 observations deleted due to missingness)
## Multiple R-squared:  0.8685, Adjusted R-squared:  0.8433 
## F-statistic: 34.54 on 26 and 136 DF,  p-value: < 2.2e-16
Fit4 <- lm(log(`Quantity`) ~ factor(`Partner Country`) + LCI_rel + El_pris_rel, data = ALUM_EU_RUS)
summary(Fit4)
## 
## Call:
## lm(formula = log(Quantity) ~ factor(`Partner Country`) + LCI_rel + 
##     El_pris_rel, data = ALUM_EU_RUS)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -6.2235 -0.4527  0.0021  0.4786  2.5237 
## 
## Coefficients:
##                                         Estimate Std. Error t value Pr(>|t|)
## (Intercept)                             10.44339    0.93607  11.157  < 2e-16
## factor(`Partner Country`)Belgium         2.65507    0.74429   3.567 0.000521
## factor(`Partner Country`)Bulgaria        4.61778    0.69592   6.636 1.01e-09
## factor(`Partner Country`)Croatia         4.99299    0.69592   7.175 6.71e-11
## factor(`Partner Country`)Cyprus          4.82303    0.69592   6.930 2.31e-10
## factor(`Partner Country`)Czechia         2.45644    0.71677   3.427 0.000838
## factor(`Partner Country`)Estonia         3.42427    0.69592   4.920 2.80e-06
## factor(`Partner Country`)Finland         1.98198    0.74512   2.660 0.008892
## factor(`Partner Country`)France          3.02398    0.69592   4.345 2.95e-05
## factor(`Partner Country`)Germany         5.70580    0.69592   8.199 3.21e-13
## factor(`Partner Country`)Greece          6.78489    0.69592   9.750  < 2e-16
## factor(`Partner Country`)Hungary         0.61077    0.71606   0.853 0.395396
## factor(`Partner Country`)Ireland         2.03351    1.25464   1.621 0.107711
## factor(`Partner Country`)Italy           6.63449    0.69592   9.533 2.38e-16
## factor(`Partner Country`)Latvia          0.87230    0.74769   1.167 0.245678
## factor(`Partner Country`)Lithuania       2.62548    0.74769   3.511 0.000630
## factor(`Partner Country`)Netherlands     8.55013    0.69592  12.286  < 2e-16
## factor(`Partner Country`)Poland          6.11193    0.69592   8.783 1.41e-14
## factor(`Partner Country`)Romania         1.10427    0.79004   1.398 0.164793
## factor(`Partner Country`)Slovakia        3.16425    0.69592   4.547 1.32e-05
## factor(`Partner Country`)Slovenia        3.95311    0.71677   5.515 2.06e-07
## factor(`Partner Country`)Spain           4.68976    0.69592   6.739 6.03e-10
## factor(`Partner Country`)Sweden          4.94530    0.69592   7.106 9.51e-11
## factor(`Partner Country`)United Kingdom  6.08563    0.69592   8.745 1.73e-14
## LCI_rel                                  0.17194    0.06234   2.758 0.006734
## El_pris_rel                             -0.03483    0.05448  -0.639 0.523813
##                                            
## (Intercept)                             ***
## factor(`Partner Country`)Belgium        ***
## factor(`Partner Country`)Bulgaria       ***
## factor(`Partner Country`)Croatia        ***
## factor(`Partner Country`)Cyprus         ***
## factor(`Partner Country`)Czechia        ***
## factor(`Partner Country`)Estonia        ***
## factor(`Partner Country`)Finland        ** 
## factor(`Partner Country`)France         ***
## factor(`Partner Country`)Germany        ***
## factor(`Partner Country`)Greece         ***
## factor(`Partner Country`)Hungary           
## factor(`Partner Country`)Ireland           
## factor(`Partner Country`)Italy          ***
## factor(`Partner Country`)Latvia            
## factor(`Partner Country`)Lithuania      ***
## factor(`Partner Country`)Netherlands    ***
## factor(`Partner Country`)Poland         ***
## factor(`Partner Country`)Romania           
## factor(`Partner Country`)Slovakia       ***
## factor(`Partner Country`)Slovenia       ***
## factor(`Partner Country`)Spain          ***
## factor(`Partner Country`)Sweden         ***
## factor(`Partner Country`)United Kingdom ***
## LCI_rel                                 ** 
## El_pris_rel                                
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 1.109 on 119 degrees of freedom
##   (377 observations deleted due to missingness)
## Multiple R-squared:  0.8156, Adjusted R-squared:  0.7769 
## F-statistic: 21.06 on 25 and 119 DF,  p-value: < 2.2e-16
## Nu deklarerer vi datasættene for panel i dimensionerne partnerland og år, så kan vi prøve en fixed effect model og se om det gør en forskel bl.a. ved at estimere med standardfejl, der tager højde for at dataene gentager sig år efter år...

## First we declare the dataset as panel with the plm package
library(plm)
## 
## Attaching package: 'plm'
## The following objects are masked from 'package:dplyr':
## 
##     between, lag, lead
ALUM_PANEL_EU_GER <- pdata.frame(ALUM_EU_GER, index = c("Partner.Country", "Year"))

library(lfe)
## Loading required package: Matrix
## 
## Attaching package: 'lfe'
## The following object is masked from 'package:plm':
## 
##     sargan
Model_pool <- plm( log(Trade.Value.usd) ~ LCI_rel + El_pris_rel, data=ALUM_PANEL_EU_GER, model="pooling")

Model_between <- plm(log(Trade.Value.usd) ~ LCI_rel + El_pris_rel, data=ALUM_PANEL_EU_GER, model="between")

Model_panel_fixed <- plm(log(Trade.Value.usd) ~ LCI_rel + El_pris_rel, data=ALUM_PANEL_EU_GER, model="within", effect="individual")

Model_panel_random <- plm(log(Trade.Value.usd) ~ LCI_rel + El_pris_rel, data=ALUM_PANEL_EU_GER, model="random", effect="individual")
  
Model_panel_fd <- plm(log(Trade.Value.usd) ~ LCI_rel + El_pris_rel, data=ALUM_PANEL_EU_GER, model="fd", effect="individual")

Model_panel_fddyn <- plm(log(Trade.Value.usd) ~ plm::lag(log(Trade.Value.usd)) + LCI_rel + El_pris_rel, data=ALUM_PANEL_EU_GER, model="fd", effect="individual")

Model_gmm <- pgmm(log(Trade.Value.usd) ~ plm::lag(log(Trade.Value.usd), 1) + LCI_rel + El_pris_rel
                  | plm::lag(log(Trade.Value.usd), 2:3),
                  data = ALUM_PANEL_EU_GER, model = "onestep", effect = "individual" )
## Warning in pgmm(log(Trade.Value.usd) ~ plm::lag(log(Trade.Value.usd), 1) + : the
## first-step matrix is singular, a general inverse is used
## Warning in pgmm(log(Trade.Value.usd) ~ plm::lag(log(Trade.Value.usd), 1) + : the
## second-step matrix is singular, a general inverse is used
library(stargazer)
## 
## Please cite as:
##  Hlavac, Marek (2018). stargazer: Well-Formatted Regression and Summary Statistics Tables.
##  R package version 5.2.2. https://CRAN.R-project.org/package=stargazer
stargazer::stargazer(Model_pool, Model_between, Model_panel_fixed, Model_panel_random, Model_panel_fd, Model_panel_fddyn, Model_gmm, title = "Panel models for German Aluminium exports to other EU countries", dep.var.labels=c("Aluminium export values in usd"),  omit.stat=c("f", "ser"), align=TRUE, no.space=TRUE, type="text", add.lines = list(c("Panel model", "Pooling", "Between", "Within", "Random", "First diff.", "FDwlag", "GMM"), c("Country effect", "No", "No", "Yes", "Yes", "No", "No", "Yes")))
## 
## Panel models for German Aluminium exports to other EU countries
## =======================================================================================================
##                                                            Dependent variable:                         
##                                   ---------------------------------------------------------------------
##                                                      Aluminium export values in usd                    
##                                                               panel                              panel 
##                                                              linear                               GMM  
##                                      (1)        (2)       (3)      (4)        (5)        (6)      (7)  
## -------------------------------------------------------------------------------------------------------
## plm::lag(log(Trade.Value.usd))                                                        -0.328***        
##                                                                                        (0.087)         
## plm::lag(log(Trade.Value.usd), 1)                                                                0.127 
##                                                                                                 (0.361)
## LCI_rel                             0.021    16.837***   0.027    0.028      0.056      0.021    0.015 
##                                    (0.148)    (5.006)   (0.058)  (0.060)    (0.080)    (0.076)  (0.051)
## El_pris_rel                         0.036    12.035***  -0.018   -0.018     -0.057     -0.081   -0.070 
##                                    (0.131)    (3.807)   (0.052)  (0.053)    (0.051)    (0.049)  (0.052)
## Constant                          14.840*** -233.844***         15.039***   -0.008      0.004          
##                                    (1.821)   (75.154)            (0.885)    (0.129)    (0.123)         
## -------------------------------------------------------------------------------------------------------
## Panel model                        Pooling    Between   Within   Random   First diff.  FDwlag     GMM  
## Country effect                       No         No        Yes      Yes        No         No       Yes  
## Observations                         163        25        163      163        138        136      26   
## R2                                  0.001      0.340     0.003    0.004      0.022      0.120          
## Adjusted R2                        -0.012      0.280    -0.188   -0.009      0.007      0.100          
## =======================================================================================================
## Note:                                                                       *p<0.1; **p<0.05; ***p<0.01
plmtest(Model_pool, type=c("bp"))
## 
##  Lagrange Multiplier Test - (Breusch-Pagan) for unbalanced panels
## 
## data:  log(Trade.Value.usd) ~ LCI_rel + El_pris_rel
## chisq = 273.47, df = 1, p-value < 2.2e-16
## alternative hypothesis: significant effects
phtest(Model_panel_fixed, Model_panel_random)
## 
##  Hausman Test
## 
## data:  log(Trade.Value.usd) ~ LCI_rel + El_pris_rel
## chisq = 0.017357, df = 2, p-value = 0.9914
## alternative hypothesis: one model is inconsistent
## Nu deklarerer vi datasættene for panel i dimensionerne partnerland og år, så kan vi prøve en fixed effect model og se om det gør en forskel bl.a. ved at estimere med standardfejl, der tager højde for at dataene gentager sig år efter år...

## First we declare the dataset as panel with the plm package
library(plm)
ALUM_PANEL_EU_GER <- pdata.frame(ALUM_EU_GER, index = c("Partner.Country", "Year"))

library(lfe)

Model_pool <- plm( log(Quantity) ~ LCI_rel + El_pris_rel, data=ALUM_PANEL_EU_GER, model="pooling")

Model_between <- plm(log(Quantity) ~ LCI_rel + El_pris_rel, data=ALUM_PANEL_EU_GER, model="between")

Model_panel_fixed <- plm(log(Quantity) ~ LCI_rel + El_pris_rel, data=ALUM_PANEL_EU_GER, model="within", effect="individual")

Model_panel_random <- plm(log(Quantity) ~ LCI_rel + El_pris_rel, data=ALUM_PANEL_EU_GER, model="random", effect="individual")
  
Model_panel_fd <- plm(log(Quantity) ~ LCI_rel + El_pris_rel, data=ALUM_PANEL_EU_GER, model="fd", effect="individual")

Model_panel_fddyn <- plm(log(Quantity) ~ plm::lag(log(Quantity)) + LCI_rel + El_pris_rel, data=ALUM_PANEL_EU_GER, model="fd", effect="individual")

Model_gmm <- pgmm(log(Quantity) ~ plm::lag(log(Quantity), 1) + LCI_rel + El_pris_rel
                  | plm::lag(log(Trade.Value.usd), 2:3),
                  data = ALUM_PANEL_EU_GER, model = "onestep", effect = "individual" )
## Warning in pgmm(log(Quantity) ~ plm::lag(log(Quantity), 1) + LCI_rel +
## El_pris_rel | : the first-step matrix is singular, a general inverse is used
## Warning in pgmm(log(Quantity) ~ plm::lag(log(Quantity), 1) + LCI_rel +
## El_pris_rel | : the second-step matrix is singular, a general inverse is used
library(stargazer)
stargazer::stargazer(Model_pool, Model_between, Model_panel_fixed, Model_panel_random, Model_panel_fd, Model_panel_fddyn, Model_gmm, title = "Panel models for German Aluminium exports to other EU countries", dep.var.labels=c("Aluminium export quantity in kgs"),  omit.stat=c("f", "ser"), align=TRUE, no.space=TRUE, type="text", add.lines = list(c("Panel model", "Pooling", "Between", "Within", "Random", "First diff.", "FDwlag", "GMM"), c("Country effect", "No", "No", "Yes", "Yes", "No", "No", "Yes")))
## 
## Panel models for German Aluminium exports to other EU countries
## =================================================================================================
##                                                     Dependent variable:                          
##                            ----------------------------------------------------------------------
##                                               Aluminium export quantity in kgs                   
##                                                        panel                              panel 
##                                                       linear                               GMM  
##                               (1)        (2)       (3)      (4)        (5)        (6)      (7)   
## -------------------------------------------------------------------------------------------------
## plm::lag(log(Quantity))                                                        -0.333***         
##                                                                                 (0.086)          
## plm::lag(log(Quantity), 1)                                                               -0.211**
##                                                                                          (0.107) 
## LCI_rel                      0.072    17.141***   0.079    0.081      0.090      0.049    0.039  
##                             (0.150)    (5.061)   (0.059)  (0.061)    (0.082)    (0.078)  (0.057) 
## El_pris_rel                  0.031    12.360***  -0.025   -0.024     -0.075    -0.101**   -0.091 
##                             (0.133)    (3.850)   (0.053)  (0.054)    (0.052)    (0.050)  (0.056) 
## Constant                   13.679*** -240.290***         13.895***    0.020      0.032           
##                             (1.851)   (75.991)            (0.902)    (0.132)    (0.125)          
## -------------------------------------------------------------------------------------------------
## Panel model                 Pooling    Between   Within   Random   First diff.  FDwlag     GMM   
## Country effect                No         No        Yes      Yes        No         No       Yes   
## Observations                  163        25        163      163        138        136       26   
## R2                           0.002      0.343     0.015    0.006      0.041      0.146           
## Adjusted R2                 -0.011      0.283    -0.173   -0.007      0.027      0.126           
## =================================================================================================
## Note:                                                                 *p<0.1; **p<0.05; ***p<0.01
plmtest(Model_pool, type=c("bp"))
## 
##  Lagrange Multiplier Test - (Breusch-Pagan) for unbalanced panels
## 
## data:  log(Quantity) ~ LCI_rel + El_pris_rel
## chisq = 271.94, df = 1, p-value < 2.2e-16
## alternative hypothesis: significant effects
phtest(Model_panel_fixed, Model_panel_random)
## 
##  Hausman Test
## 
## data:  log(Quantity) ~ LCI_rel + El_pris_rel
## chisq = 0.016416, df = 2, p-value = 0.9918
## alternative hypothesis: one model is inconsistent
## Nu deklarerer vi datasættene for panel i dimensionerne partnerland og år, så kan vi prøve en fixed effect model og se om det gør en forskel bl.a. ved at estimere med standardfejl, der tager højde for at dataene gentager sig år efter år...

## First we declare the dataset as panel with the plm package
library(plm)
ALUM_PANEL_EU_RUS <- pdata.frame(ALUM_EU_RUS, index = c("Partner.Country", "Year"))

library(lfe)

Model_pool <- plm( log(Trade.Value.usd) ~ LCI_rel + El_pris_rel, data=ALUM_PANEL_EU_RUS, model="pooling")

Model_between <- plm(log(Trade.Value.usd) ~ LCI_rel + El_pris_rel, data=ALUM_PANEL_EU_RUS, model="between")

Model_panel_fixed <- plm(log(Trade.Value.usd) ~ LCI_rel + El_pris_rel, data=ALUM_PANEL_EU_RUS, model="within", effect="individual")

Model_panel_random <- plm(log(Trade.Value.usd) ~ LCI_rel + El_pris_rel, data=ALUM_PANEL_EU_RUS, model="random", effect="individual")
  
Model_panel_fd <- plm(log(Trade.Value.usd) ~ LCI_rel + El_pris_rel, data=ALUM_PANEL_EU_RUS, model="fd", effect="individual")

Model_panel_fddyn <- plm(log(Trade.Value.usd) ~ plm::lag(log(Trade.Value.usd)) + LCI_rel + El_pris_rel, data=ALUM_PANEL_EU_RUS, model="fd", effect="individual")

Model_gmm <- pgmm(log(Trade.Value.usd) ~ plm::lag(log(Trade.Value.usd), 1) + LCI_rel + El_pris_rel
                  | plm::lag(log(Trade.Value.usd), 2:3),
                  data = ALUM_PANEL_EU_RUS, model = "onestep", effect = "individual" )
## Warning in pgmm(log(Trade.Value.usd) ~ plm::lag(log(Trade.Value.usd), 1) + : the
## first-step matrix is singular, a general inverse is used
## Warning in pgmm(log(Trade.Value.usd) ~ plm::lag(log(Trade.Value.usd), 1) + : the
## second-step matrix is singular, a general inverse is used
library(stargazer)
stargazer::stargazer(Model_pool, Model_between, Model_panel_fixed, Model_panel_random, Model_panel_fd, Model_panel_fddyn, Model_gmm, title = "Panel models for Russian Aluminium exports to the EU countries", dep.var.labels=c("Aluminium export values in usd"),  omit.stat=c("f", "ser"), align=TRUE, no.space=TRUE, type="text", add.lines = list(c("Panel model", "Pooling", "Between", "Within", "Random", "First diff.", "FDwlag", "GMM"), c("Country effect", "No", "No", "Yes", "Yes", "No", "No", "Yes")))
## 
## Panel models for Russian Aluminium exports to the EU countries
## =================================================================================================
##                                                         Dependent variable:                      
##                                   ---------------------------------------------------------------
##                                                   Aluminium export values in usd                 
##                                                            panel                           panel 
##                                                           linear                            GMM  
##                                      (1)      (2)     (3)      (4)        (5)       (6)     (7)  
## -------------------------------------------------------------------------------------------------
## plm::lag(log(Trade.Value.usd))                                                    -0.026         
##                                                                                   (0.079)        
## plm::lag(log(Trade.Value.usd), 1)                                                          0.150 
##                                                                                           (0.144)
## LCI_rel                             0.122    2.126  0.125**  0.127**    0.158*     0.021   0.015 
##                                    (0.129)  (2.258) (0.062)  (0.062)    (0.083)   (0.063) (0.049)
## El_pris_rel                        -0.069   -1.533  -0.037   -0.036     -0.062    -0.042  -0.005 
##                                    (0.114)  (2.092) (0.054)  (0.055)    (0.055)   (0.041) (0.040)
## Constant                          15.892*** 16.048*         15.182***    0.091     0.077         
##                                    (1.600)  (9.090)          (0.887)    (0.133)   (0.101)        
## -------------------------------------------------------------------------------------------------
## Panel model                        Pooling  Between Within   Random   First diff. FDwlag    GMM  
## Country effect                       No       No      Yes      Yes        No        No      Yes  
## Observations                         145      24      145      145        121       113     27   
## R2                                  0.010    0.046   0.039    0.041      0.075     0.018         
## Adjusted R2                        -0.004   -0.045  -0.162    0.028      0.059    -0.009         
## =================================================================================================
## Note:                                                                 *p<0.1; **p<0.05; ***p<0.01
plmtest(Model_pool, type=c("bp"))
## 
##  Lagrange Multiplier Test - (Breusch-Pagan) for unbalanced panels
## 
## data:  log(Trade.Value.usd) ~ LCI_rel + El_pris_rel
## chisq = 201.16, df = 1, p-value < 2.2e-16
## alternative hypothesis: significant effects
phtest(Model_panel_fixed, Model_panel_random)
## 
##  Hausman Test
## 
## data:  log(Trade.Value.usd) ~ LCI_rel + El_pris_rel
## chisq = 1.4385, df = 2, p-value = 0.4871
## alternative hypothesis: one model is inconsistent
## Nu deklarerer vi datasættene for panel i dimensionerne partnerland og år, så kan vi prøve en fixed effect model og se om det gør en forskel bl.a. ved at estimere med standardfejl, der tager højde for at dataene gentager sig år efter år...

## First we declare the dataset as panel with the plm package
library(plm)
ALUM_PANEL_EU_RUS <- pdata.frame(ALUM_EU_RUS, index = c("Partner.Country", "Year"))

library(lfe)

Model_pool <- plm( log(Quantity) ~ LCI_rel + El_pris_rel, data=ALUM_PANEL_EU_RUS, model="pooling")

Model_between <- plm(log(Quantity) ~ LCI_rel + El_pris_rel, data=ALUM_PANEL_EU_RUS, model="between")

Model_panel_fixed <- plm(log(Quantity) ~ LCI_rel + El_pris_rel, data=ALUM_PANEL_EU_RUS, model="within", effect="individual")

Model_panel_random <- plm(log(Quantity) ~ LCI_rel + El_pris_rel, data=ALUM_PANEL_EU_RUS, model="random", effect="individual")
  
Model_panel_fd <- plm(log(Quantity) ~ LCI_rel + El_pris_rel, data=ALUM_PANEL_EU_RUS, model="fd", effect="individual")

Model_panel_fddyn <- plm(log(Quantity) ~ plm::lag(log(Quantity)) + LCI_rel + El_pris_rel, data=ALUM_PANEL_EU_RUS, model="fd", effect="individual")

Model_gmm <- pgmm(log(Quantity) ~ plm::lag(log(Quantity), 1) + LCI_rel + El_pris_rel
                  | plm::lag(log(Trade.Value.usd), 2:3),
                  data = ALUM_PANEL_EU_RUS, model = "onestep", effect = "individual" )
## Warning in pgmm(log(Quantity) ~ plm::lag(log(Quantity), 1) + LCI_rel +
## El_pris_rel | : the first-step matrix is singular, a general inverse is used
## Warning in pgmm(log(Quantity) ~ plm::lag(log(Quantity), 1) + LCI_rel +
## El_pris_rel | : the second-step matrix is singular, a general inverse is used
library(stargazer)
stargazer::stargazer(Model_pool, Model_between, Model_panel_fixed, Model_panel_random, Model_panel_fd, Model_panel_fddyn, Model_gmm, title = "Panel models for Russian Aluminium exports to the EU countries", dep.var.labels=c("Aluminium export quantity in kgs"),  omit.stat=c("f", "ser"), align=TRUE, no.space=TRUE, type="text", add.lines = list(c("Panel model", "Pooling", "Between", "Within", "Random", "First diff.", "FDwlag", "GMM"), c("Country effect", "No", "No", "Yes", "Yes", "No", "No", "Yes")))
## 
## Panel models for Russian Aluminium exports to the EU countries
## ===========================================================================================
##                                                  Dependent variable:                       
##                            ----------------------------------------------------------------
##                                            Aluminium export quantity in kgs                
##                                                     panel                            panel  
##                                                     linear                            GMM   
##                               (1)      (2)     (3)       (4)        (5)       (6)     (7)  
## -------------------------------------------------------------------------------------------
## plm::lag(log(Quantity))                                                     -0.014         
##                                                                             (0.077)        
## plm::lag(log(Quantity), 1)                                                          -0.037 
##                                                                                     (0.081)
## LCI_rel                      0.166    2.273  0.172*** 0.174***    0.198**    0.056   0.075 
##                             (0.129)  (2.239) (0.062)   (0.063)    (0.083)   (0.062) (0.053)
## El_pris_rel                 -0.071   -1.732   -0.035   -0.035     -0.064    -0.045  0.0003 
##                             (0.113)  (2.075) (0.054)   (0.055)    (0.055)   (0.040) (0.039)
## Constant                   15.034*** 16.499*          14.284***    0.097     0.084         
##                             (1.595)  (9.013)           (0.886)    (0.134)   (0.099)        
## -------------------------------------------------------------------------------------------
## Panel model                 Pooling  Between  Within   Random   First diff. FDwlag    GMM  
## Country effect                No       No      Yes       Yes        No        No      Yes  
## Observations                  145      24      145       145        121       113     27   
## R2                           0.015    0.050   0.067     0.057      0.100     0.035         
## Adjusted R2                  0.002   -0.041   -0.129    0.044      0.085     0.008         
## ===========================================================================================
## Note:                                                           *p<0.1; **p<0.05; ***p<0.01
plmtest(Model_pool, type=c("bp"))
## 
##  Lagrange Multiplier Test - (Breusch-Pagan) for unbalanced panels
## 
## data:  log(Quantity) ~ LCI_rel + El_pris_rel
## chisq = 200.27, df = 1, p-value < 2.2e-16
## alternative hypothesis: significant effects
phtest(Model_panel_fixed, Model_panel_random)
## 
##  Hausman Test
## 
## data:  log(Quantity) ~ LCI_rel + El_pris_rel
## chisq = 0.31425, df = 2, p-value = 0.8546
## alternative hypothesis: one model is inconsistent

How correlated are the two relative prices e.g. the relative price of labour and electricity in Germany and Russia?

cor(ALUM_PANEL_EU_GER$LCI_rel, ALUM_PANEL_EU_GER$El_pris_rel, use = "complete.obs")
## [1] -0.0767007
cor(ALUM_PANEL_EU_RUS$LCI_rel, ALUM_PANEL_EU_RUS$El_pris_rel, use = "complete.obs")
## [1] -0.0919475