POLI 277 MEASUREMENT THEORY
Supplemental Movie Assignment
Due 11 June 2008
#
# Parametric_bootstrap_movie.r -- Does Two-Dim Plot of the bootstrap trials
# for any Member of Congress 1789 - 2004 (Congresses
# 1 - 108)
#
#
# Remove all objects just to be safe
#
rm(list=ls(all=TRUE))
#
#
library(MASS)
library(pcurve)
library(stats)
library(ellipse)
#
ncong <- 108 Select the Senate
ntrials <- 1001
#
# Select Senator here
#
# ICPSR/Poole-Rosenthal ID Number
#
idnoselect <- 99910 Use the ID Number of Senator You Want to Display
#
# Position in the file
#
iselect <- 1 The Senator's row in the Data
#
# Read bootstrap Means and Standard Deviations
#
rcx.file <- "c:/ucsd_homework_movie/s108_bs_1000_2.dat" This is the Summary File Showing the Bootstrapped Standard Errors
#
# Standard fields and their widths
#
rcx.fields <- c("counter","cong","idno","state","cd","statename","party","ecodes",
"membername","x1","x2","x1bmean","x2bmean","se1","se2","r12")
rcx.fieldWidths <- c(5,3,5,2,2,7,4,2,12,9,10,10,10,10,10,10)
#
# Read the vote data from fwf
#
T <- read.fwf(file=rcx.file,widths=rcx.fieldWidths,as.is=TRUE,col.names=rcx.fields)
#
#
nrowtarget <- length(T[,1])
ncoltarget <- length(T[1,])
#
# Read Bootstrap Trials
#
rcx.file <- "c:/ucsd_homework_movie/s108_nom31_1000.dat" This is the output matrix for the
# trials. It has 1001 trials all stacked up
# top of each other -- 1001 NOM31.DAT
# Standard fields and their widths files stacked up
#
rcx.fields <- c("counter","cong","idno","state","cd","statename","party","ecodes",
"membername","k11","k12","k21","k22","gmp","x1","x2","se1","se2")
rcx.fieldWidths <- c(5,3,5,2,2,7,4,2,12,4,4,4,4,7,7,7,7,7)
#
# Read the vote data from fwf
#
TTT <- read.fwf(file=rcx.file,widths=rcx.fieldWidths,as.is=TRUE,col.names=rcx.fields)
#
#
nrowstrials <- length(TTT[,1])
ncolstrials <- length(TTT[1,])
#
TBOOT <- TTT[TTT[,3]==idnoselect,] This pulls the 1001 sets of coordinates out for the
# selected Senator
#
# Loop Over Bootstrap trials
#
iiii <- 0
while (iiii < ntrials) {
#
iiii <- iiii + 1
fnum <- iiii
if(fnum<10)
fn <- paste("000",fnum,sep="")
else if(fnum<100) This block of statements guarantees the the Jpegs
fn <- paste("00",fnum,sep="") get written out from 0001.jpg to 9999.jpg
else if(fnum<1000)
fn <- paste("0",fnum,sep="")
else
fn <- fnum
#
#
# Pull Coordinate from Bootstrap Mean File
#
xtrial1 <- T[iselect,10] Selected Senator's Coordinate used to make Black Dot Below
xtrial2 <- T[iselect,11]
#
jpeg(file=paste("C:/ucsd_homework_movie/JPG_ANI/",fn,".jpg",sep=""), This is the Jpeg Write Statement
width=1165,height=651,quality=100,pointsize=12, bg = "white") Note the Path Statement
#
par(mfrow=c(1,2)) Creates Two Graphic Panels Side-by-Side
#
#
# mar
# A numerical vector of the form c(bottom, left, top, right) which
# gives the number of lines of margin to be specified on the four
# sides of the plot. The default is c(5, 4, 4, 2) + 0.1.
#
#
par(mar=c(4.1,4.1,4.1,2.1)) Nice Margins
#
plot(T[,10],T[,11],type="n",asp=1,main="",
xlab="",
ylab="",
xlim=c(-1.0,1.0),ylim=c(-1.0,1.0),
cex=1.2,font=2)
#
# Main title
# Note the Use of the Paste Command Here
mtext(paste("Senate ", ncong, "\n", 1787+2*ncong, "-", 1787+2*ncong+1, sep=""),side=3,line=1.25,cex=1.2,font=2)
#
# x-axis title
mtext("Liberal - Conservative",side=1,line=2.25,cex=1.2,font=2)
# y-axis title
mtext("Social Issues",side=2,line=2.25,cex=1.2,font=2)
#
# Southern Democrats
points(T[T[,7] == 100 & T[,4] >= 40 & T[,4] <= 51,10],T[T[,7] == 100 & T[,4] >= 40 & T[,4] <= 51,11],pch='S',col="red",font=2)
points(T[T[,7] == 100 & T[,4] == 53,10],T[T[,7] == 100 & T[,4] == 53,11],pch='S',col="red",font=2)
points(T[T[,7] == 100 & T[,4] == 54,10],T[T[,7] == 100 & T[,4] == 54,11],pch='S',col="red",font=2)
# Northern Democrats
points(T[T[,7] == 100 & (T[,4] < 40 | T[,4] > 54),10],T[T[,7] == 100 & (T[,4] < 40 | T[,4] > 54),11],pch='D',col="red",font=2)
points(T[T[,7] == 100 & T[,4] == 52,10],T[T[,7] == 100 & T[,4] == 52,11],pch='D',col="red",font=2)
# Republicans
points(T[T[,7] == 200,10],T[T[,7] == 200,11],pch='R',col="blue",font=2)
# Presidents
points(T[T[,4] == 99,10],T[T[,4] == 99,11],pch='P',col="black",font=2)
#
#
# This code does the cross-hairs for the standard errors. If the
# correlation is greater than .15 between the two dimensions,
# the 95% confidence ellipse is shown
#
for (i in 1:nrowtarget) { This Code Block is From Parametric Bootstrap
# Plotting Program Used in Homework 6
# These two statements do the cross-hairs The Left Panel Always Shows the Bootstrap Results --
# Cross-Hairs and Ellipses
lines(c(T[i,10],T[i,10]),c(T[i,11]-1.96*T[i,15],T[i,11]+1.96*T[i,15]),col="gray")
lines(c(T[i,10]-1.96*T[i,14],T[i,10]+1.96*T[i,14]),c(T[i,11],T[i,11]),col="gray")
#
# This if statement does the ellipse
#
if (abs(T[i,16]) > .30){
lines(ellipse(x=T[i,16],scale=c(T[i,14],T[i,15]),
centre=c(T[i,10],T[i,11])),
col="gray")
}
}
#
#
# mar
# A numerical vector of the form c(bottom, left, top, right) which
# gives the number of lines of margin to be specified on the four
# sides of the plot. The default is c(5, 4, 4, 2) + 0.1.
#
par(mar=c(4.1,4.1,4.1,2.1))
#
#
#
plot(T[,10],T[,11],type="n",asp=1,main="",
xlab="",
ylab="",
xlim=c(-1.0,1.0),ylim=c(-1.0,1.0),
cex=1.2,font=2)
#
# Main title Note that the Title has a Counter for the Trials
#
mtext(paste("Bootstrap Trial ", iiii, "\n", "President Bush"),side=3,line=1.25,cex=1.2,font=2)
#
# x-axis title
mtext("Liberal - Conservative",side=1,line=2.25,cex=1.2,font=2)
# y-axis title
#mtext("Social Issues",side=2,line=2.25,cex=1.2,font=2)
#
# Southern Democrats
points(T[T[,7] == 100 & T[,4] >= 40 & T[,4] <= 51,10],T[T[,7] == 100 & T[,4] >= 40 & T[,4] <= 51,11],pch='S',col="red",font=2)
points(T[T[,7] == 100 & T[,4] == 53,10],T[T[,7] == 100 & T[,4] == 53,11],pch='S',col="red",font=2)
points(T[T[,7] == 100 & T[,4] == 54,10],T[T[,7] == 100 & T[,4] == 54,11],pch='S',col="red",font=2)
# Northern Democrats
points(T[T[,7] == 100 & (T[,4] < 40 | T[,4] > 54),10],T[T[,7] == 100 & (T[,4] < 40 | T[,4] > 54),11],pch='D',col="red",font=2)
points(T[T[,7] == 100 & T[,4] == 52,10],T[T[,7] == 100 & T[,4] == 52,11],pch='D',col="red",font=2)
# Republicans
points(T[T[,7] == 200,10],T[T[,7] == 200,11],pch='R',col="blue",font=2)
# Presidents
points(T[T[,4] == 99,10],T[T[,4] == 99,11],pch='P',col="black",font=2)
#
# Plot Bootstrap Trial
#
points(TBOOT[1:iiii,15],TBOOT[1:iiii,16],pch=16,col="purple",font=2) With Each Jpeg You have to Write all the previous trials up through the current one
points(xtrial1,xtrial2,pch=16,col="black",cex=1.3,font=2) This Puts a Black Dot on the Position of the Senator
#
dev.off() # turn off the postscript device
}
After the program runs it creates 0001.jpg to 1001.jpg and stores them in
C:/ucsd_homework_movie/JPG_ANI/. To make the movie, open QuickTime
and in the file menu there is an option "Open Image Sequence:





