#R Script: Cut and paste this file into R and you will be on your way! #Note: You need to change the paths for your directories!!! #Note: the “#” symbol comments out text so R ignores it. #The first three statements read in various R libraries. These are #packages that perform various functions. If you try and run these, #you may get an error because some packages must be installed. To #execute this file, you do not need the package UsingR! The other #two libaries will be automatically installed with R. library(UsingR) library(foreign) library(graphics) #Below I'm creating an object that specifies the location of the data #used in this script. The object is called pol51 #Office pol51 <- "D:\\classes\\pol51\\fall2008\\data" #The setwd command stands for "set working directory." It's useful # because R will automatically save output in this directory. setwd(pol51) #Using the read.dta function to read in the Stata formatted data set. #If I were reading in a comma delimited Excel sheet, I’d use the second #option (commented out) vs<-read.dta('om_wta.dta') #vs<- read.csv("congress_price_data.csv") #Attaching and summarizing the data: attach(vs) summary(vs) #Below is a technical command to convert character data into a factor #for purposes of plotting. I recommend just using this code for your assignment #since newdate is a variable in the data set. newdate <- as.Date(date, "%m/%d/%y") #Making the string variable a factor newdate<-sort(newdate) #Some Summary Statisics summary(obama_price) summary(mccain_price) #Illustrating the "column bind" command and saving it in an object called "table" table <- cbind(obama_price, mccain_price, newdate) table #This code fragment will put up to 4 plots in a single plot. par(mfcol=c(2,2) ) #Plot example 1: Plotting both time-series plot(newdate, obama_price, col="blue", ylim=c(.0, .99), type="l", lty="solid", lwd="3", ylab="Closing Price", xlab="Date", main="Obama vs. McCain: Winner-take-all Market") lines(newdate, mccain_price, col="red", type="l", lty="solid", lwd="3") #Plot example 2: Plotting just Obama plot(newdate, obama_price, col="blue", ylim=c(.0, .99), type="l", lty="solid", lwd="3", ylab="Closing Price", xlab="Date", main="Obama: Winner-take-all Market") #Plot example 3: Plotting just Obama plot(newdate, mccain_price, col="red", ylim=c(.0, .99), type="l", lty="solid", lwd="3", ylab="Closing Price", xlab="Date", main="McCain: Winner-take-all Market")