0
votes

I want to try calculate a score or create an index that gives the degree of states "marginalization" within a world economy. In other words, an index that would indicate state's position within the world economy.

Basically, I try to replicate someone else's indicator. He defined "marginalization" within the World Economy as a function of that country's percentage of world trade.

It was computed by calculating: the total imports + total exports of a given nation divided by the total imports + exports of the world economy. The inverse of that divided by 100.

I am trying to figure out how to calculate this in R, using my data. Below I give an (real) example.

Country  Year     EXPORT(%GDP)  IMPORT(%GDP)   GDP(current$) 
A        2001        22,14        21,96            3,78(+11)                    
B        2001        35,43        31,80            5387293(..)
C        2001        27,22        30,84            1,90
WORLD    2001        24,43        24,20            3,30(+13)

To be clear, I want to calculate a score for each country and year in my data (about 150 countries, from 1990 to 2014). The equation is (to be clear, again): import + export (for a given country in the dataset) / import + export (of the world economy, please see the variable "WORLD" in the example.

EDIT: another example with current $ (if this is of any help)

Country  Year     EXPORT(current$)  IMPORT(c$)          GDP(c$) 
    A        2001        8,38177(..)   8,31506           3,78(+11)                    
    B        2001        1,90875(..)   1,71328           5,387293(..)
    C        2001        5,1872(..)    5,87710           1,90
    WORLD    2001        7,6811(..)    7,7101            3,30(+13)
2
There is a modelling issue: the figures in Export and Import makes no sense with your calculation. Exp. and Imp. are defined as percentage of GDP for each country: you must mention this GDP in a fifth column to calculate the raw value of import and export. From these raw values, we can apply your formula (but we can't apply it with percentage!)Colonel Beauvel
@ColonelBeauvel Thanks – make sense. I do also have raw values for EXP/IMP (Exp/Imp of goods and services current $). I have now added the GDP into inte data frame. How can I do the calculation in R?FKG
I'm still unable to make sense of the data for the purposes you propose. The totals of countries A,B, abd C are much more than the values for WORLD.IRTFM
@BondedDust I was thinking about it as well. The data is well used and comes from the World Bank (World Development Indicators). Not sure how they computed it. But it make sense that some of the countries are outliers with big/small export/import values. This is just a sample of my data. Not sure if the real values are important for the R-code? It is a pain to do this calculation in Excel, that is for sure.FKG
There is something you have not told us about that data.IRTFM

2 Answers

2
votes

Using the good old data.table:

library( data.table)

# Thanks "TheKevinNeville" for the sample data!
country <- c("A", "B", "C", "World")
year <- c(rep(2001, 4), rep(2002, 4))
export <- abs(rnorm(8) * 5)
import <- abs(rnorm(8) * 5)
dt <- data.table( country,year,export,import)

# Here you calculate the index (rank) per group
dt[,index := (import + export) / .SD[country=="World", import + export],by=.(year)]

The result looks like this:

   country year   export    import    index
1:       A 2001 4.641794 7.3788739 6.222089
2:       B 2001 4.286842 1.3656420 2.925816
3:       C 2001 1.919439 1.1210429 1.573802
4:   World 2001 1.164199 0.7677355 1.000000
5:       A 2002 1.303430 3.5848178 1.478056
6:       B 2002 4.231528 2.6427575 2.078573
7:       C 2002 8.655763 7.1272979 4.772314
8:   World 2002 2.134707 1.1725057 1.000000

And if you want to order the results per year and index (descending) you could add the following code:

# setorder reorders the rows of a data.table by reference,
# based on the columns provided. 
setorder(dt, year, -index)
-1
votes

Creating data.

country <- c("A", "B", "C", "World")
year <- c(rep(2001, 4), rep(2002, 4))
export <- abs(rnorm(8) * 5)
import <- abs(rnorm(8) * 5)

mydf <- data.frame(country=country,Year=year,EXPORT=export, IMPORT=import)

For loop.

mydf$Score <- NA
for(i in 2001:2002){
index <- mydf[,"Year"] == i
current_world <- mydf$country[index] == "World"
mydf$Score[index] <- (mydf$EXPORT[index] + mydf$IMPORT[index]) / (mydf$EXPORT[index][current_world] + mydf$IMPORT[index][current_world])
}