Showing posts with label code. Show all posts
Showing posts with label code. Show all posts

Tuesday, March 25, 2014

Kazakh Arms Imports Visualized (Part 1)

I thought it would be fun to put together a graph that shows the arms suppliers for Kazakhstan in 2013 in a network form.  The data come from SIPRI and I put them into a simple association matrix.  I used the R Statnet package to make the data a network, and then just used plot features in R to make the graph look a little nicer. I think that this is a fun little visualization of where these arms are coming from.  Here's the graph:



The code to do this is pretty straightforward.  If you want the data in the text format, please email me.  The code is pasted below using "Pretty R."


library(statnet)
supplier <- read.table("supplier.txt")
kasnet2013.m <- as.matrix(supplier)
kaznet2013 <- as.network(kasnet2013.m,matrix.type="adjacency", directed = T)
 
vertsize <- supplier$Kazakhstan*.05+1
png("Kazakh_imports2013.png", height = 8, width=11, units="in", bg="transparent",
    res=600)
plot(kaznet2013, displaylabels=TRUE, displayisolates = FALSE,
     vertex.col=c("gray","gray","red","gray","gray", "red","sienna4"),
     vertex.cex = vertsize,
     main=list("Kazakstan Arms Suppliers 2013", col="sienna4"),
     sub=list("Size of Vertex represents Value of Arms from Country",
              cex= .9, col="sienna3"))
 
mtext("Data from SIPRI TIV Database", side = 2, cex=.75, col="dimgray")
dev.off()
Created by Pretty R at inside-R.org

Wednesday, March 19, 2014

2013 Arms Imports into Former Soviet States

The Stockholm International Peace Research Institute (SIPRI) has updated its arms transfer data for 2013.  I haven't worked much with arms transfer data since I completed my dissertation a year ago.  However, I recently accepted a job as an Assistant Professor in the School of Humanities and Social Sciences at Nazarbayev University (Link) and am looking to begin some projects on arms and security in the area.  As a kind of preliminary step in this endeavor I thought I'd play around with some of the SIPRI data while I'm on spring break at UNLV.

I am also teaching a course on research methods in political science this semester.  I am teaching our political science students to do statistical analysis in R.  I am learning a lot myself and am forcing myself to use it rather than STATA - which is my go-to statistical and graphing package.

My first plot is a simple bar chart showing the value of arms imports (major weapons systems) into former Soviet states during 2013.  One interesting aspect is that there are only six states that received arms during this year.  Azerbaijan is also off the charts compared to the other states on the list.  I am also posting my R code to make this chart as well as a link to the csv file that I used as a data source.  I made the R code look good using "pretty R".



# Examine 2013 SIPR Export Data
#setwd() #uncomment and use this command to set your working directory to the folder
  #where you downloaded the csv file to.
imports_fsu_2013 <- read.csv("imports_fsu_2013.csv")
as.numeric(imports_fsu_2013$Imports) #make the Imports column numeric
 
mean_import <- mean(imports_fsu_2013$Imports) # generate mean value
 
png("fsu_imports.png", height = 8, width=11, units="in", bg="transparent",
    res=600)
 
barplot(imports_fsu_2013$Imports,
        names.arg = imports_fsu_2013$Country,
        main=list("Military Imports into Former Soviet Countries 2013 \n in Millions $US", 
                  cex=1.1),
        ylab="Millions $US",
        xlab=list("Importing Country", font=2),
        col = "sienna4")
 
abline(h=mean_import, lty=3, col="black")
text(4,192, "Mean Value of imports to Former Soviet States in 2013", cex=.75)
mtext("Data from SIPRI TIV Database", side = 1, cex=.75, col="dimgray")
 
dev.off()
Created by Pretty R at inside-R.org

Wednesday, February 19, 2014

Just an update on R coding

I'm teaching an introduction to political analysis course at UNLV this spring.  I have gone out on a limb and am teaching the students R instead of STATA or SPSS.  I am more familiar with STATA, but do my network analysis work in R.  I am trying to become more proficient.  I had a problem with a project I am working on - trying to count the number of rivalry ties that a state has in any given year. Normally I would do this in STATA, but I decided to give it a whirl in R.  After two hours and numerous "googlings" I have produced the code to give me the count for each year of my data.  The code looks like this:


for(i in 1816:2010){
  fp <- file.path("network", paste("net", i, ".txt", sep = ""))
  outcount <- file.path("count", paste("count", i, ".csv", sep=""))
 
  file <- read.table(fp)
  #sum row - this is the number of out ties by a state
  count.row <-rowSums(file, na.rm=FALSE, dims=1)
  count.col <- colSums(file, na.rm=FALSE, dims=1)
  comb.ties <- cbind(count.row, count.col)
  total.ties <- rowSums(comb.ties, na.rm=FALSE, dims=1)
  write.csv(total.ties, file= outcount, row.names = TRUE)  
}
Created by Pretty R at inside-R.org

The data come from text files with a network matrix (sociomatrix) for the states involved in the international system.  I use these to examine network characteristics in other contexts.  Here, I have just used them as matrices and have used the summing features to get both the outgoing ties, states that identify others as rivals, and in ties, those that identify the state as rivals.  These are combined and summed for a total.  The total is then exported to a CSV file that can be used in other contexts.  It's blunt force, and perhaps inelegant, but it's mine!

Monday, August 27, 2012

Putting Dollars in STATA Graphics Titles

This post is mostly so I remember how to do this.

There is a problem that I run into a lot when I am plotting in STATA.  The $ symbol is used by STATA to call up various macros.  When you try to add it as a currency symbol in a graph title it ends up disappearing. Take, for instance the following code and its associated graphical output:


twoway dot percapmil id, mlabel(description) mlabpos(12)
scheme(s1mono)
xlabel(0(1)5)
ylabel(, angle(0))
xtitle("")
ytitle("$1000 US Per Military Personnel")
title("Per Capita (Mil. Personell) Spending of US and Russian Customers", size(medium))
subtitle("1992-2000 and 2001-2002")


Notice the y-axis label is missing the dollar sign.  I really want to emphasize the fact that the y axis represents money spent by states for defense.  How can I add the dollar sign to the graph?  This requires using a STATA Markup Control Language (SMCL) workaround.  

The modified code looks like this (just taking the relevant line of code):

ytitle("{c $|}1000 US Per Military Personnel")

With this modification the graph looks the way that I want it to. 
It's a little bit inconvenient to have to add the extra code {c $|} just to get the symbol to show up like it should, but once you know what the workaround is you can get your work done instead of endlessly Googling.  Now back to work.