A stock market is where buyers and sellers trade shares of a company, and is one of the most popular ways for individuals and companies to invest money. The size of the world stock market is now estimated to be in the trillions. The largest stock market in the world is the New York Stock Exchange (NYSE), located in New York City. About 2,800 companies are listed on the NYSE. In this problem, we’ll look at the monthly stock prices of five of these companies: IBM, General Electric (GE), Procter and Gamble, Coca Cola, and Boeing. The data used in this problem comes from Infochimps.
There are two main types of crimes: violent crimes, and property crimes. In this problem, we’ll focus on one specific type of property crime, called “motor vehicle theft” (sometimes referred to as grand theft auto). This is the act of stealing, or attempting to steal, a car. In this problem, we’ll use some basic data analysis in R to understand the motor vehicle thefts in Chicago.
Download and read the following files into R, using the read.csv function: IBMStock.csv, GEStock.csv, ProcterGambleStock.csv, CocaCola.csv, and BoeingStock.csv
ID: the date of the stock price, always given as the first of the month.
StockPrice: the average stock price of the company in the given month.
In this problem, we’ll take a look at how the stock dynamics of these companies have changed over time.
Right now, the date variable is stored as a factor. We can convert this to a “Date” object in R by using the following five commands (one for each data set):
IBM = read.csv("IBMStock.csv")
GE = read.csv("GEStock.csv")
ProcterGamble = read.csv("ProcterGambleStock.csv")
CocaCola = read.csv("CocaColaStock.csv")
Boeing = read.csv("BoeingStock.csv")
IBM$Date = as.Date(IBM$Date, "%m/%d/%y")
GE$Date = as.Date(GE$Date, "%m/%d/%y")
CocaCola$Date = as.Date(CocaCola$Date, "%m/%d/%y")
ProcterGamble$Date = as.Date(ProcterGamble$Date, "%m/%d/%y")
Boeing$Date = as.Date(Boeing$Date, "%m/%d/%y")str(IBM)
## 'data.frame': 480 obs. of 2 variables:
## $ Date : Date, format: "1970-01-01" "1970-02-01" "1970-03-01" "1970-04-01" ...
## $ StockPrice: num 360 347 327 320 270 ...Explanation: Using the str function, we can see that each data set has 480 observations. We have monthly data for 40 years, so there are 12*40 = 480 observations.
summary(IBM)
## Date StockPrice
## Min. :1970-01-01 Min. : 43.40
## 1st Qu.:1979-12-24 1st Qu.: 88.34
## Median :1989-12-16 Median :112.11
## Mean :1989-12-15 Mean :144.38
## 3rd Qu.:1999-12-08 3rd Qu.:165.41
## Max. :2009-12-01 Max. :438.90Explanation: Using the summary function, the minimum value of the Date variable is January 1, 1970 for any dataset.
summary(IBM)
## Date StockPrice
## Min. :1970-01-01 Min. : 43.40
## 1st Qu.:1979-12-24 1st Qu.: 88.34
## Median :1989-12-16 Median :112.11
## Mean :1989-12-15 Mean :144.38
## 3rd Qu.:1999-12-08 3rd Qu.:165.41
## Max. :2009-12-01 Max. :438.90Explanation: Using the summary function, the maximum value of the Date variable is December 1, 2009 for any dataset.
summary(IBM)
## Date StockPrice
## Min. :1970-01-01 Min. : 43.40
## 1st Qu.:1979-12-24 1st Qu.: 88.34
## Median :1989-12-16 Median :112.11
## Mean :1989-12-15 Mean :144.38
## 3rd Qu.:1999-12-08 3rd Qu.:165.41
## Max. :2009-12-01 Max. :438.90Explanation: By typing summary(IBM), we can see that the mean value of the IBM StockPrice is 144.38.
summary(GE)
## Date StockPrice
## Min. :1970-01-01 Min. : 9.294
## 1st Qu.:1979-12-24 1st Qu.: 44.214
## Median :1989-12-16 Median : 55.812
## Mean :1989-12-15 Mean : 59.303
## 3rd Qu.:1999-12-08 3rd Qu.: 72.226
## Max. :2009-12-01 Max. :156.844Explanation: By typing summary(GE), we can see that the minimum value of the GE StockPrice is 9.294.
summary(CocaCola)
## Date StockPrice
## Min. :1970-01-01 Min. : 30.06
## 1st Qu.:1979-12-24 1st Qu.: 42.76
## Median :1989-12-16 Median : 51.44
## Mean :1989-12-15 Mean : 60.03
## 3rd Qu.:1999-12-08 3rd Qu.: 69.62
## Max. :2009-12-01 Max. :146.58Explanation: By typing summary(CocaCola), we can see that the maximum value of the Coca-Cola StockPrice is 146.58.
summary(Boeing)
## Date StockPrice
## Min. :1970-01-01 Min. : 12.74
## 1st Qu.:1979-12-24 1st Qu.: 34.64
## Median :1989-12-16 Median : 44.88
## Mean :1989-12-15 Mean : 46.59
## 3rd Qu.:1999-12-08 3rd Qu.: 57.21
## Max. :2009-12-01 Max. :107.28Explanation: By typing summary(Boeing), we can see that the median value of the Boeing StockPrice is 44.88.
sd(ProcterGamble$StockPrice)
## [1] 18.19414Explanation: By typing sd(ProcterGamble$StockPrice), we can see that the standard deviation of the Procter & Gamble StockPrice is 18.19414.
plot(CocaCola$Date, CocaCola$StockPrice, type="l")plot(CocaCola$Date, CocaCola$StockPrice, type="l", col="red")
lines(ProcterGamble$Date, ProcterGamble$StockPrice, col="blue")
abline(v=as.Date(c("2000-03-01")), lwd=2)
legend("bottomleft",
legend=c("Coca Cola", "ProcterGamble"),
col=c("red", "blue"), lty=1:2, cex=0.8)Procter and Gamble.
Coca-Cola.
Coca-Cola.
Let’s take a look at how the stock prices changed from 1995-2005 for all five companies. In your R console, start by typing the following plot command:
plot(CocaCola$Date[301:432], CocaCola$StockPrice[301:432], type="l", col="red", ylim=c(0,210), xlab = "Date", ylab = "Stock Price")
lines(ProcterGamble$Date[301:432], ProcterGamble$StockPrice[301:432], col="blue")
lines(IBM$Date[301:432], IBM$StockPrice[301:432], col="green")
lines(GE$Date[301:432], GE$StockPrice[301:432], col="purple")
lines(Boeing$Date[301:432], Boeing$StockPrice[301:432], col="orange")
abline(v=as.Date(c("2000-03-01")), lwd=2)
legend( "topleft",
legend=c("Coca Cola", "ProcterGamble", "IBM", "GE", "Boeing"),
col=c("red", "blue", "green", "purple", "orange"), lty=1:2, cex=0.8)General Electric (GE).
IBM.
plot(CocaCola$Date[301:432], CocaCola$StockPrice[301:432], type="l", col="red", ylim=c(0,210), xlab = "Date", ylab = "Stock Price")
lines(ProcterGamble$Date[301:432], ProcterGamble$StockPrice[301:432], col="blue")
lines(IBM$Date[301:432], IBM$StockPrice[301:432], col="green")
lines(GE$Date[301:432], GE$StockPrice[301:432], col="purple")
lines(Boeing$Date[301:432], Boeing$StockPrice[301:432], col="orange")
abline(v=as.Date(c("1997-09-01")), lwd=2)
abline(v=as.Date(c("1997-11-01")), lwd=2)
legend( "topleft",
legend=c("Coca Cola", "ProcterGamble", "IBM", "GE", "Boeing"),
col=c("red", "blue", "green", "purple", "orange"), lty=1:2, cex=0.8)Two companies had a decreasing trend in stock prices from September 1997 to November 1997: Boeing and Procter & Gamble.
plot(CocaCola$Date[301:432], CocaCola$StockPrice[301:432], type="l", col="red", ylim=c(0,210), xlab = "Date", ylab = "Stock Price")
lines(ProcterGamble$Date[301:432], ProcterGamble$StockPrice[301:432], col="blue")
lines(IBM$Date[301:432], IBM$StockPrice[301:432], col="green")
lines(GE$Date[301:432], GE$StockPrice[301:432], col="purple")
lines(Boeing$Date[301:432], Boeing$StockPrice[301:432], col="orange")
abline(v=as.Date(c("2004-1-01")), lwd=2)
abline(v=as.Date(c("2006-1-01")), lwd=2)
legend( "topleft",
legend=c("Coca Cola", "ProcterGamble", "IBM", "GE", "Boeing"),
col=c("red", "blue", "green", "purple", "orange"), lty=1:2, cex=0.8)Boeing is steadily increasing from 2004 to the beginning of 2006.
Lastly, let’s see if stocks tend to be higher or lower during certain months.
tapply(IBM$StockPrice, months(IBM$Date), mean)
## April August December February January July June March May November October September
## 152.1168 140.1455 140.7593 152.6940 150.2384 139.0670 139.0907 152.4327 151.5022 138.0187 137.3466 139.0885The overall average stock price for IBM is 144.375, which can be computed using the command mean(IBM$StockPrice). Comparing the monthly averages to this, using the command tapply(IBM$StockPrice, months(IBM$Date), mean), we can see that the price has historically been higher than average January - May, and lower than average during the remaining months.
tapply(GE$StockPrice, months(GE$Date), mean)
## April August December February January July June March May November October September
## 64.48009 56.50315 59.10217 62.52080 62.04511 56.73349 56.46844 63.15055 60.87135 57.28879 56.23897 56.23913
tapply(CocaCola$StockPrice, months(CocaCola$Date), mean)
## April August December February January July June March May November October September
## 62.68888 58.88014 59.73223 60.73475 60.36849 58.98346 60.81208 62.07135 61.44358 59.10268 57.93887 57.60024
tapply(IBM$StockPrice, months(IBM$Date), mean)
## April August December February January July June March May November October September
## 152.1168 140.1455 140.7593 152.6940 150.2384 139.0670 139.0907 152.4327 151.5022 138.0187 137.3466 139.0885
tapply(ProcterGamble$StockPrice, months(ProcterGamble$Date), mean)
## April August December February January July June March May November October September
## 77.68671 76.82266 78.29661 79.02575 79.61798 76.64556 77.39275 77.34761 77.85958 78.45610 76.67903 76.62385
tapply(Boeing$StockPrice, months(Boeing$Date), mean)
## April August December February January July June March May November October September
## 47.04686 46.86311 46.17315 46.89223 46.51097 46.55360 47.38525 46.88208 48.13716 45.14990 45.21603 46.30485General Electric has an average stock price of 64.48 in April, which is higher than any other month. Coca-Cola has an average stock price of 62.69 in April, which is higher than any other month.
Having lower stock prices in December is a trend that holds for all five companies.