Showing posts with label "R". Show all posts
Showing posts with label "R". Show all posts

Wednesday, April 1, 2015

Don't call it a comeback!

Dear Spencer,

I have not been keeping up with this blog at all in the last few months.  I am involved in many projects and am still in the process of settling into my job at Nazarbayev University in Kazakhstan.  I would like to make this blog more of a part of my process of writing and thinking through issues that I am working on, but I am still thinking of ways that I can do that meaningfully.

My job is to write and to produce content. That content needs to be peer reviewed and validated by gatekeepers.  The kind of writing that I am doing here - or  not doing here- is unfiltered and unfettered. I think that there is value in the interaction of both types of writing.  If there are any regular readers of this blog (not even my wife or mother read this, so I don't have a lot of hope), which is unlikely since there is no regular writer on this blog, please be patient as I get back into the swing of things.

Topics that I will be posting on in the future:

  • Pedagogy - I am especially interested in incorporating undergraduates into the research process.
  • Technology - My personal computer is now running UBUNTU.  My academic writing is done in a text editor and LaTeX.  I am moving from Stata to R for my statistical analysis work.  There are a lot of changes for me on the technology front.
  • A new research area I am interested in: Covert Action by states.
  • More on military issues and arms sales.
  • Some work on role theory and network analysis - a new book project (from an ongoing paper project with my dissertation adviser and mentor, Cameron Thies).
  • Russia-Ukraine stuff.  Because I lived in Donetsk for two years, and this is a pretty big deal.
That's the plan for the future.  I am aiming to have one or two short posts a week with maybe a longer piece up once a month or so.  I may be ambitious and work to get my longer pieces linked to, or re-posted on some of the blogs of the cool kids.  Wouldn't that be something!

Best,

Spencer

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!

Tuesday, August 2, 2011

Russian and US Arms Exports - A Very general Overview

This particular post is just a bit of motivation for me.  I am doing a little exploratory work for my dissertation, but I am also working on learning how to use R for statistical analysis/graphing.  This is the result of my afternoon and evening.  The data come from the Stockholm International Peace Research Institute (SIPRI).

These graphs show the total arms exports from both the US and Russia (USSR before 1991).  There are two things that stand out from this simple visualization: 1) arms transfers are a cyclical process at the macro level, and 2) Russian arms exports dropped off much more quickly than the U.S., but are now rebounding along with the U.S.