#
# OC_in_R.r
#
# Program to Run R Version of OC
#
#
# Remove all objects just to be safe
#
rm(list=ls(all=TRUE))
#
library(pscl)
library(oc)
library(gdata)
#
#
rcx.file <- "c:/oc/src/Annenberg_Panel_2.DAT"
#
# Standard fields and their widths -- OC output Legislators
#
rcx.fields <- c("id","rc 1","rc 2","rc 3","rc 4","rc 5","rc 6","rc 7","rc 8","rc 9","rc 10","rc 11","rc 12",
"rc 13","rc 14","rc 15","rc 16","rc 17","rc 18","rc 19","rc 20","rc 21","rc 22","rc 23","rc 24","rc 25","rc 26","rc 27",
"rc 28","rc 29","rc 30","rc 31","rc 32","rc 33","rc 34","rc 35","rc 36","rc 37","rc 38","rc 39","rc 40","rc 41","rc 42",
"rc 43","rc 44","rc 45","rc 46","rc 47","rc 48","rc 49","rc 50","rc 51","rc 52","rc 53","rc 54","rc 55","rc 56","rc 57",
"rc 58","rc 59","rc 60","rc 61","rc 62","rc 63","rc 64","rc 65","rc 66","rc 67","rc 68","rc 69","rc 70","rc 71","rc 72",
"rc 73","rc 74","rc 75","rc 76","rc 77","rc 78","rc 79","rc 80","rc 81","rc 82","rc 83","rc 84","rc 85","rc 86","rc 87",
"rc 88","rc 89","rc 90","rc 91","rc 92","rc 93","rc 94","rc 95","rc 96","rc 97","rc 98","rc 99","rc100","rc101","rc102",
"rc103","rc104","rc105","rc106","rc107","rc108","rc109","rc110","rc111","rc112","rc113","rc114","rc115","rc116")
rcx.fieldWidths <- c(36,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1)
#
# Read the vote data from fwf
#
T <- read.fwf(file=rcx.file,widths=rcx.fieldWidths,as.is=TRUE,col.names=rcx.fields)
T1 <- as.matrix(T)
mode(T1) <- "double"
#
TT <- T1[,2:117]
mode(TT) <- "double"
#
hr <- rollcall(TT,yea=1,nay=6,missing=0,notInLegis=9,
legis.names=NULL, vote.names=NULL, legis.data=NULL, vote.data=NULL,
desc=NULL, source=NULL)
#
# Call OC
#
# oc(rcObject, dims=2, minvotes=20, lop=0.025, polarity, verbose=FALSE)
#
###Arguments
#
#rcObject An object of class rollcall, from Simon Jackman's pscl package.
#dims integer, number of dimensions to estimate. Must be nonnegative and cannot exceed 10 dimensions.
#minvotes minimum number of votes a legislator must vote in for them to be analyzed.
#lop A proportion between 0 and 1, the cut-off used for excluding lopsided votes, expressed as the proportion of non-missing votes on the minority side. The default, lop=0.025, eliminates votes where the minority is smaller than 2.5 overwrites the lopsided attribute in the RC object inputted.
#polarity a vector specifying the legislator in the data set who is conservative on each dimension. For example, c(3,5) indicates legislator 3 is conservative on dimension 1, and legislator 5 is conservative on dimension 2. Alternatively, polarity can be specified as a string for legislator names found in legis.names (ie. c("Bush", "Gore")) if every legislative name in the data set is unique. Finally, polarity can be specified as a list (ie. list("cd",c(4,5))) where the first list item is a variable from the roll call object's legis.data, and the second list item is a conservative legislator on each dimension as specified by the first list item. list("cd",c(4,5)) thus specifies the legislators with congressional district numbers of 4 and 5.
#verbose logical, indicates whether bills and legislators to be deleted should be printed while data is being checked before ideal points are estimated.
#
result <- oc(hr, dims=2, polarity=c(1,2))
#result <- oc(hr, dims=1, minvotes=10,polarity=c(1))
#result <- oc(hr, dims=1, minvotes=5,polarity=c(1))
#
# length(result)
#[1] 5
#
# class(result)
#[1] "OCobject"
#
#names(result)
#[1] "legislators" "rollcalls" "dimensions" "eigenvalues" "fits"
#
# result$legislators
# result$rollcalls
# result$dimensions
# result$eigenvalues
# result$fits
# result$weights
# result$fits
#
#
# Legislators
#
write.fwf(x=format(as.data.frame(result$legislators),digits=5,width=10,
scientific=FALSE),"c:/pew_polls/Craig_Burnett_x_2008.txt")
#
# Roll Calls
#
write.fwf(x=format(as.data.frame(result$rollcalls),digits=5,width=10,
scientific=FALSE),"c:/pew_polls/Craig_Burnett_z_2008.txt")
#