Go to Session > Set Working Directory to select your working directory.
You'll want to install the following packages if you didn't in the previous R Session. This only has to be done once.
install.packages("TSA") install.packages("pdfetch")
I've got a few more for you:
install.packages("ggplot2") install.packages("stargazer") install.packages("zoo")
Once we've installed, we load the libraries. This needs to be done in every new session.
library(TSA) library(ggplot2) library(stargazer) library(zoo) library(pdfetch)
Just to review, we could get spreadsheets into R like so:
oil <- read.csv("monthly_dataset_baseline.csv")
As you may have noticed, you can have many objects loaded into your environment in R at the same time, and they come in a variety of flavors. Now that we have an oil object, let's see what it is.
str(oil)
Remember the Johnson & Johnson data you loaded like this?
data(JJ)
What kind of object is it?
We'll use the stargazer package to make tables of descriptive statistics.
stargazer(oil, type = "html", title="Descriptive statistics", digits=2, out="oil.htm", covariate.labels=c("Variable 1","Variable 2","Variable 3", "Variable 4","Variable 5", "Variable 6"))
You should have a document called oil.htm in your working directory. This can be opened in MS Word.
Remember making a chart like this?
plot(JJ, ylab="USD$", las=1, col="blue", xlab="Year", main="Earnings per Share (EPS) for Johnson & Johnson", lwd=2) minor.tick(nx=5,ny=5)
We can map the same data using different packages. Let's try with the autoplot.zoo command, which is a way to access a package of graphing tools called ggplot2.
autoplot.zoo(JJ)
Let's say we want to make a chart that looks something like this:
Let's get similar data from FRED using pdfetch.
treasury <- pdfetch_FRED(c("THREEFY10", "THREEFYTP10")) names(treasury) <- c("10 Year Treasury Yield","10 Year Term Premium")
You can find information on these series on the FRED website:
What kind of object is this?
Let's plot.
p <- autoplot.zoo(treasury, facets = NULL) p
The facets = NULL put both series in the same plot. Now let's add some labels.
p1 <- p + labs(title = "Ten Year Treasury Yield and Term Premium, 1990-2017", caption = "Sources: Federal Reserve Bank of New York, Federal Reserve", x = "Year") p1
We can control the font style and size to an extent.
p2 <- p1 + theme(plot.title = element_text(family = "serif", color="#666666", face="bold", size=16, hjust=0)) + theme(plot.caption = element_text(family = "serif", color="#666666", face="italic"))) p2
You can change the color scale of the lines and also the background theme, like so:
p2 + scale_colour_grey() + theme_bw() p2 + scale_colour_manual(values=c("blue", "red"))
We can change the legend.
p3 <- p2 + theme(legend.title = element_blank()) + theme(legend.justification=c(1,1), legend.position=c(.95,.95)) p3
We can add a horizontal line as well as an annotation of the line.
p4 <- p3 + geom_hline(yintercept = .0163, color= "blue") + annotate("text", x = as.Date("1995-01-01"), y = 0, label = "your equation here") + annotate("text", x = as.Date("2006-01-01"), y = .02, label = "Average = 1.63%")
Adding recession bars takes a couple of steps. First we need the beginning and end dates of recessions, which I am giving you here:
recessions.df = read.table(textConnection( "Peak, Trough 1857-06-01, 1858-12-01 1860-10-01, 1861-06-01 1865-04-01, 1867-12-01 1869-06-01, 1870-12-01 1873-10-01, 1879-03-01 1882-03-01, 1885-05-01 1887-03-01, 1888-04-01 1890-07-01, 1891-05-01 1893-01-01, 1894-06-01 1895-12-01, 1897-06-01 1899-06-01, 1900-12-01 1902-09-01, 1904-08-01 1907-05-01, 1908-06-01 1910-01-01, 1912-01-01 1913-01-01, 1914-12-01 1918-08-01, 1919-03-01 1920-01-01, 1921-07-01 1923-05-01, 1924-07-01 1926-10-01, 1927-11-01 1929-08-01, 1933-03-01 1937-05-01, 1938-06-01 1945-02-01, 1945-10-01 1948-11-01, 1949-10-01 1953-07-01, 1954-05-01 1957-08-01, 1958-04-01 1960-04-01, 1961-02-01 1969-12-01, 1970-11-01 1973-11-01, 1975-03-01 1980-01-01, 1980-07-01 1981-07-01, 1982-11-01 1990-07-01, 1991-03-01 2001-03-01, 2001-11-01 2007-12-01, 2009-06-01"), sep=',', colClasses=c('Date', 'Date'), header=TRUE)
We can use subset to pick out recessions within the date range of this chart.
recessions.trim = subset(recessions.df, Peak >= "1990-01-01" )
Now we can add the recession bars using this data.
p5 <- p4 + geom_rect(data=recessions.trim, inherit.aes = F, mapping = aes(xmin=Peak, xmax=Trough, ymin=-Inf, ymax=+Inf), fill='pink', alpha=0.2)
You may have noticed that we used geom_rect() to add rectangles and geom_hline() to add a horizontal line. We can use geom_point() to add a highlighted point.
treasury['2013-12-02'] p6 <- p5 + annotate("point", x = rep(as.Date("2013-12-02"),2), y = as.vector(treasury['2013-12-02']), colour = "red", size = 3) + annotate("text", x = as.Date("2013-12-02")+100, y = 2.9484+.5, label="December 2013") p6