Time series data

gdp <- read.table("c:/data/GDPC96.txt", header = TRUE)



plot(gdp~date, data=gdp,pch=16,xlab="",ylab="GDP (2000 dollars)")


Conservatism of Congressional delegation and %Bush vote

Busy day today, so I'll just post this:

plot(bush04 ~ cons_hr, type = "n",
xlab="Mean ACU rating",
ylab="2004 Bush vote",
col.axis = "#777777",
col.lab = "#777777")
text(y=bush04,x=cons_hr, labels=stateid,cex=0.75)
abline(lm2, lty=2, col="red")
axis(side = 2, at = seq(0,100,by=5), cex=0.75)


Filtering cases

Something that's very important to be able to do in data analysis and visualization is to filter out cases. Let's say you want to do identical analyses of two different groups, or of one group and then a subset of it. R can do this a little differently; instead of merely filtering out cases you can create an object that is a subset, and then call it when necessary.

Let's look at some data on the U.S. Congress. Keith Poole has developed a two-dimensional procedure that places members of Congress at specific points based on roll call votes. What we'll do now is compare Democrats and Republicans in the 110th Congress.

First, we load the data into R.

voteview <- read.csv ("C:/Data/HouseSmall.csv", header = TRUE) attach (voteview)

The voteview data frame contains data on all Congresses beginning with the 101st. That's more than we want to deal with, and also, we need a way to look at Democrats and Republicans separately. We'll create an object just for Democrats in the 110th Congress. and then one for Republicans.

dems110 <- subset(voteview, party == 100 & cong == 110)

reps110 <- subset(voteview, party == 200 & cong == 110)

Now let's create a graph to compare them.

plot (c (-1.5, 1.5), c(-1.5, 1.5), type = 'n',
xlab = "1st dimension",

ylab = "2nd dimension",

col.axis = "#777777",

col.lab = "#777777",

cex.axis = 0.75,

cex.lab = 1.25,

main = "DW-nominate scores, 110th Congress",

col.main = "#444444")

abline (v = 0, col = "#cccccc")

points (dwnom2 ~ dwnom1, data = dems110, pch = "D", col = "blue", cex = 0.75)

points (dwnom2 ~ dwnom1, data = reps110, pch = "R", col = "red", cex = 0.75)

That's all a bit complicated, so next time I'll talk about what all those things do. But for now I'll just show what it looks like.

Figure 1. The polarization of Congress.


A bit about linear models

Before we delve into slightly more advanced plotting commands I want to talk a little about linear models, specifically, linear regression. In R this is very, very simple. For instance, in our 'states' data frame, we might want to look at median household income as a predictor of state education expenditures. The command lm calculates this for us. We'll call our first model, 'model1':

model1 <- lm (publicedexp~hincome)

OK, great, but where are our results? One of the things about R is that you can assign names to all sorts of things, even models. That way, you can continually refer to them when doing other things (as we'll see a bit later.) The way to look at our results is with this:

summary (model1)

lm(formula = publicedexp ~ hincome)

Min 1Q Median 3Q Max
-397.50 -127.43 -8.69 120.96 431.85

Estimate Std. Error t value Pr(>|t|)
(Intercept) 2.516e+02 1.735e+02 1.450 0.153
hincome 2.346e-02 3.869e-03 6.063 1.87e-07 ***
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

Residual standard error: 198.8 on 49 degrees of freedom
Multiple R-squared: 0.4287, Adjusted R-squared: 0.417
F-statistic: 36.76 on 1 and 49 DF, p-value: 1.87e-07

Which gives us a lot more information than if we'd just run the lm command without assigning a name to the model. Later we'll look at how we can integrate our linear model with our plots.


Reading data, and a graph

Using Microsoft Excel I'm collecting aggregate data, by state, of various social, political, and economic indicators. I export them into a tab-delimited file called 'states.txt' (pretty clever, I know.) I've got data on education expenditures, firearm deaths per capita, median household income, etc. I'd like to do some analysis and graphing of these data to see if there are any patterns of interest.

First, I import the data into R with the following command:

states <- read.table("C:/Data/states.txt", header=TRUE, sep = "\t")

This creates a data frame called 'states', and reads the text file into it. The command 'header = TRUE' tells R that the first row of data contains variable names, and 'sep = "\t"' tells the program that the file is tab-demimited.

Next I 'attach' the data frame with the following command:

attach (states)

OK, now that I've got my data into R, what can I do with it?

First, I'll run some correlations and see what's going on.

cor (read2children, publicedexp)
[1] 0.4211508

This tells me that the correlation between public expenditures on education and the percentage of children below the age of five who are read to daily is 0.42. It is unsurprising that there's a strong relationship between the two. It's also likely that a third variable, household income, might be related.

cor (hincome, publicedexp)
[1] 0.6547179
cor (read2children, hincome)
[1] 0.4094883

These relationships are even stronger.

Let's look at the data a different way, by using scatterplots to see the relationships.

plot is the R command for, well, plotting. It's very powerful and you can do lots of things with it. First I'll do a very simple scatterplot of education expenditures and children read to.

plot (publicedexp, read2children)

Figure 1. A very basic scatterplot

Next time we'll work on making it better looking.


A start

I've decided that this summer I will finally break down and force myself to learn a little bit about using R. I currently use Stata, a very good program, but the idea of R is appealing since it's free under the GNU license. It has a large and active user community and is constantly growing and expanding.

I'll be using this blog to keep track of my progress and to record what I'm doing along the way.