R Markdown
Competitive Intelligence: Spirit Airlines Financial Dossier (R)
install required packages
bring packages into the workspace
library(RCurl) # functions for gathering data from the web
library(XML) # XML and HTML parsing
library(quantmod) # use for gathering and charting economic data
library(Quandl) # extensive financial data online
library(lubridate) # date functions
library(zoo) # utilities for working with time series
library(xts) # utilities for working with time series
library(ggplot2) # data visualization
———————————-
Text data acquisition and storage
———————————-
get current working directory (commands for Mac OS or Linux)
cwd <- getwd()
create directory for storing competitive intelligence data
ciwd <- paste(cwd, “/ci_data/”, sep =““)
dir.create(ciwd)
gather Wikipedia data using RCurl package
wikipedia_web_page <-
getURLContent('http://en.wikipedia.org/wiki/Spirit_Airlines')
store data in directory ciwd for future processing
sink(paste(ciwd, “/”, “wikipedia_web_page”, sep = ““))
wikipedia_web_page
sink()
similar procedures may be used for all acquired data
for the Spirit Airlines competitive intelligence study
use distinct file names to identify the data sources
—————————————————————
Yahoo! Finance for Spirit Airlines (NASDAQ stock symbol: SAVE)
—————————————————————
stock symbols for companies can be obtained from Yahoo! Finance
get Spirit Airlines stock price data
getSymbols(“SAVE”, return.class = “xts”, src = “yahoo”)
print(str(SAVE)) # show the structure of this xts time series
object
plot the series stock price
chartSeries(SAVE,theme=“white”)
examine the structure of the R data object
print(str(SAVE))
print(SAVE)
convert character string row names to decimal date for the year
Year <- decimal_date(ymd(row.names(as.data.frame(SAVE))))
Obtain the closing price of Spirit Airlines stock
Price <- as.numeric(SAVE$SAVE.Close)
create data frame for Spirit Airlines Year and Price for future
plots
SPIRIT_data_frame <- data.frame(Year, Price)
similar procedures may be used for all airline competitors
—————————————————————
Google! Finance for Spirit Airlines (NASDAQ stock symbol: SAVE)
(basically the same data as from Yahoo! Finance)
—————————————————————
get Spirit Airlines stock price data
getSymbols(“SAVE”, return.class = “xts”, src = “google”)
print(str(SAVE)) # show the structure of this xts time series
object
plot the series stock price
chartSeries(SAVE,theme=“white”)
examine the structure of the R data object
print(str(SAVE))
print(SAVE)
—————————————–
ggplot2 time series plotting, closing
price of Spirit Airlines common stock
—————————————–
with a data frame object in hand… we can go on to use ggplot2
and methods described in Chang (2013) R Graphics Cookbook
use data frame defined from Yahoo! Finance
the Spirit Airlines closing price per share SPIRIT_data_frame
now we use that data structure to prepare a time series plot
again, let’s highlight the Great Recession on our plot for this time
series
plotting_object <- ggplot(SPIRIT_data_frame, aes(x = Year, y =
Price)) +
geom_line() +
ylab("Stock Price (dollars/share)") +
ggtitle("Spirit Airlines Stock Price")
print(plotting_object)
send the plot to an external file (sans title, larger axis
labels)
pdf(“fig_competitive_intelligence_spirit.pdf”, width = 11, height =
8.5)
plotting_object <- ggplot(SPIRIT_data_frame, aes(x = Year, y =
Price)) +
geom_line() + ylab("Stock Price (dollars/share)") +
theme(axis.title.y = element_text(size = 20, colour = "black")) +
theme(axis.title.x = element_text(size = 20, colour = "black"))
print(plotting_object)
dev.off()