R Markdown

This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see http://rmarkdown.rstudio.com.

When you click the Knit button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:

# 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
## Loading required package: xts
## Loading required package: zoo
## 
## Attaching package: 'zoo'
## The following objects are masked from 'package:base':
## 
##     as.Date, as.Date.numeric
## Loading required package: TTR
## Registered S3 method overwritten by 'quantmod':
##   method            from
##   as.zoo.data.frame zoo
# online documentation for quantmod at <http://www.quantmod.com/> 
library(Quandl)  # extensive financial data online
# online documentation for Quandl at <http://www.quandl.com/> 
library(lubridate) # date functions
## 
## Attaching package: 'lubridate'
## The following objects are masked from 'package:base':
## 
##     date, intersect, setdiff, union
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
# ----------------------------------

# 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: TSLA)
# ---------------------------------------------------------------
# stock symbols for companies can be obtained from Yahoo! Finance
# <http://finance.yahoo.com/lookup>

# get Spirit Airlines stock price data
getSymbols("TSLA", return.class = "xts", src = "yahoo")
## [1] "TSLA"
print(str(TSLA)) # show the structure of this xtx time series object
## An xts object on 2010-06-29 / 2026-02-09 containing: 
##   Data:    double [3928, 6]
##   Columns: TSLA.Open, TSLA.High, TSLA.Low, TSLA.Close, TSLA.Volume ... with 1 more column
##   Index:   Date [3928] (TZ: "UTC")
##   xts Attributes:
##     $ src    : chr "yahoo"
##     $ updated: POSIXct[1:1], format: "2026-02-10 02:30:55"
## NULL
# plot the series stock price
chartSeries(TSLA,theme="white")

# examine the structure of the R data object
print(str(TSLA))
## An xts object on 2010-06-29 / 2026-02-09 containing: 
##   Data:    double [3928, 6]
##   Columns: TSLA.Open, TSLA.High, TSLA.Low, TSLA.Close, TSLA.Volume ... with 1 more column
##   Index:   Date [3928] (TZ: "UTC")
##   xts Attributes:
##     $ src    : chr "yahoo"
##     $ updated: POSIXct[1:1], format: "2026-02-10 02:30:55"
## NULL
print(TSLA)
##             TSLA.Open  TSLA.High   TSLA.Low TSLA.Close TSLA.Volume
## 2010-06-29   1.266667   1.666667   1.169333   1.592667   281494500
## 2010-06-30   1.719333   2.028000   1.553333   1.588667   257806500
## 2010-07-01   1.666667   1.728000   1.351333   1.464000   123282000
## 2010-07-02   1.533333   1.540000   1.247333   1.280000    77097000
## 2010-07-06   1.333333   1.333333   1.055333   1.074000   103003500
## 2010-07-07   1.093333   1.108667   0.998667   1.053333   103825500
## 2010-07-08   1.076000   1.168000   1.038000   1.164000   115671000
## 2010-07-09   1.172000   1.193333   1.103333   1.160000    60759000
## 2010-07-12   1.196667   1.204667   1.133333   1.136667    33037500
## 2010-07-13   1.159333   1.242667   1.126667   1.209333    40201500
##        ...                                                        
## 2026-01-27 437.410004 437.519989 430.690002 430.899994    37733100
## 2026-01-28 431.910004 438.260010 430.100006 431.459991    54857400
## 2026-01-29 437.799988 440.230011 414.619995 416.559998    81686100
## 2026-01-30 425.350006 439.880005 422.700012 430.410004    82626100
## 2026-02-02 421.290009 427.149994 414.500000 421.809998    58739500
## 2026-02-03 424.269989 428.559998 413.690002 421.959991    56886500
## 2026-02-04 420.459991 423.899994 399.179993 406.010010    74606900
## 2026-02-05 397.019989 402.100006 387.529999 397.209991    72819800
## 2026-02-06 400.869995 414.549988 397.750000 411.109985    62677100
## 2026-02-09 409.910004 421.250000 407.290009 417.320007    54351900
##            TSLA.Adjusted
## 2010-06-29      1.592667
## 2010-06-30      1.588667
## 2010-07-01      1.464000
## 2010-07-02      1.280000
## 2010-07-06      1.074000
## 2010-07-07      1.053333
## 2010-07-08      1.164000
## 2010-07-09      1.160000
## 2010-07-12      1.136667
## 2010-07-13      1.209333
##        ...              
## 2026-01-27    430.899994
## 2026-01-28    431.459991
## 2026-01-29    416.559998
## 2026-01-30    430.410004
## 2026-02-02    421.809998
## 2026-02-03    421.959991
## 2026-02-04    406.010010
## 2026-02-05    397.209991
## 2026-02-06    411.109985
## 2026-02-09    417.320007
# convert character string row names to decimal date for the year
Year <- decimal_date(ymd(row.names(as.data.frame(TSLA))))
# Obtain the closing price of Spirit Airlines stock
Price <- as.numeric(TSLA$TSLA.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: TSLA)
#   (basically the same data as from Yahoo! Finance)
# ---------------------------------------------------------------

# get Spirit Airlines stock price data
getSymbols("TSLA", return.class = "xts", src = "yahoo")
## [1] "TSLA"
print(str(TSLA)) # show the structure of this xtx time series object
## An xts object on 2010-06-29 / 2026-02-09 containing: 
##   Data:    double [3928, 6]
##   Columns: TSLA.Open, TSLA.High, TSLA.Low, TSLA.Close, TSLA.Volume ... with 1 more column
##   Index:   Date [3928] (TZ: "UTC")
##   xts Attributes:
##     $ src    : chr "yahoo"
##     $ updated: POSIXct[1:1], format: "2026-02-10 02:30:55"
## NULL
# plot the series stock price
chartSeries(TSLA,theme="white")
# examine the structure of the R data object
print(str(TSLA))
## An xts object on 2010-06-29 / 2026-02-09 containing: 
##   Data:    double [3928, 6]
##   Columns: TSLA.Open, TSLA.High, TSLA.Low, TSLA.Close, TSLA.Volume ... with 1 more column
##   Index:   Date [3928] (TZ: "UTC")
##   xts Attributes:
##     $ src    : chr "yahoo"
##     $ updated: POSIXct[1:1], format: "2026-02-10 02:30:55"
## NULL
print(TSLA)
##             TSLA.Open  TSLA.High   TSLA.Low TSLA.Close TSLA.Volume
## 2010-06-29   1.266667   1.666667   1.169333   1.592667   281494500
## 2010-06-30   1.719333   2.028000   1.553333   1.588667   257806500
## 2010-07-01   1.666667   1.728000   1.351333   1.464000   123282000
## 2010-07-02   1.533333   1.540000   1.247333   1.280000    77097000
## 2010-07-06   1.333333   1.333333   1.055333   1.074000   103003500
## 2010-07-07   1.093333   1.108667   0.998667   1.053333   103825500
## 2010-07-08   1.076000   1.168000   1.038000   1.164000   115671000
## 2010-07-09   1.172000   1.193333   1.103333   1.160000    60759000
## 2010-07-12   1.196667   1.204667   1.133333   1.136667    33037500
## 2010-07-13   1.159333   1.242667   1.126667   1.209333    40201500
##        ...                                                        
## 2026-01-27 437.410004 437.519989 430.690002 430.899994    37733100
## 2026-01-28 431.910004 438.260010 430.100006 431.459991    54857400
## 2026-01-29 437.799988 440.230011 414.619995 416.559998    81686100
## 2026-01-30 425.350006 439.880005 422.700012 430.410004    82626100
## 2026-02-02 421.290009 427.149994 414.500000 421.809998    58739500
## 2026-02-03 424.269989 428.559998 413.690002 421.959991    56886500
## 2026-02-04 420.459991 423.899994 399.179993 406.010010    74606900
## 2026-02-05 397.019989 402.100006 387.529999 397.209991    72819800
## 2026-02-06 400.869995 414.549988 397.750000 411.109985    62677100
## 2026-02-09 409.910004 421.250000 407.290009 417.320007    54351900
##            TSLA.Adjusted
## 2010-06-29      1.592667
## 2010-06-30      1.588667
## 2010-07-01      1.464000
## 2010-07-02      1.280000
## 2010-07-06      1.074000
## 2010-07-07      1.053333
## 2010-07-08      1.164000
## 2010-07-09      1.160000
## 2010-07-12      1.136667
## 2010-07-13      1.209333
##        ...              
## 2026-01-27    430.899994
## 2026-01-28    431.459991
## 2026-01-29    416.559998
## 2026-01-30    430.410004
## 2026-02-02    421.809998
## 2026-02-03    421.959991
## 2026-02-04    406.010010
## 2026-02-05    397.209991
## 2026-02-06    411.109985
## 2026-02-09    417.320007
# -----------------------------------------
# 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()
## png 
##   2