股票動態
股票市場是買賣雙方交易公司股票的地方,也是個人和公司投資的最受歡迎的方式之一。現在估計世界股票市場規模達到數兆。紐約證券交易所位於紐約市,是世界上最大的股票市場。 紐約證券交易所約有2,800家上市公司。在這個問題上,我們將看看其中五家公司的每月股價:IB、通用電氣(GE)、寶潔、可口可樂和波音。此問題中使用的數據來自Infochimps。
使用read.csv函數下載並讀取以下文件:IBMStock.csv,GEStock.csv,ProcterGambleStock.csv,CocaColaStock.csv和BoeingStock.cv。 (在完成此問題之前,請勿在任何電子表格軟件中打開這些文件,因為它可能會更改「日期」字段的格式。) 分別調用數據框“IBM”、“GE”、“ProcterGamble”、“CocaCola”和“Boeing”。每個數據框都有兩個變量,描述如下:
日期
:股票價格的日期,以每月的第一天給出。StockPrice
:給定月份公司的平均股價。 在這個問題上,我們將看看這些公司的股票動態如何隨著時間的推移而發生變化。§ 1.1 Our five datasets all have the same number of observations. How many observations are there in each data set?
IBM = read.csv("../data/IBMStock.csv")
GE = read.csv("../data/GEStock.csv")
PG = read.csv("../data/ProcterGambleStock.csv")
CK = read.csv("../data/CocaColaStock.csv")
BE = read.csv("../data/BoeingStock.csv")
IBM$Date = as.Date(IBM$Date, "%m/%d/%y")
GE$Date = as.Date(GE$Date, "%m/%d/%y")
CK$Date = as.Date(CK$Date, "%m/%d/%y")
PG$Date = as.Date(PG$Date, "%m/%d/%y")
BE$Date = as.Date(BE$Date, "%m/%d/%y")
str(IBM)
'data.frame': 480 obs. of 2 variables:
$ Date : Date, format: "1970-01-01" "1970-02-01" ...
$ StockPrice: num 360 347 327 320 270 ...
nrow(IBM)
[1] 480
§ 1.2 What is the earliest year in our datasets?
min(IBM$Date)
[1] "1970-01-01"
§ 1.3 What is the latest year in our datasets?
max(IBM$Date)
[1] "2009-12-01"
§ 1.4 What is the mean stock price of IBM over this time period?
mean(IBM$StockPrice)
[1] 144.4
§ 1.5 What is the minimum stock price of General Electric (GE) over this time period?
min(GE$StockPrice)
[1] 9.294
§ 1.6 What is the maximum stock price of Coca-Cola over this time period?
max(CK$StockPrice)
[1] 146.6
§ 1.7 What is the median stock price of Boeing over this time period?
median(BE$StockPrice)
[1] 44.88
§ 1.8 What is the standard deviation of the stock price of Procter & Gamble over this time period?
sd(PG$StockPrice)
[1] 18.19
§ 2.1 Around what year did Coca-Cola has its highest stock price in this time period? Around what year did Coca-Cola has its lowest stock price in this time period?
plot(CK$Date, CK$StockPrice, type='l') # 1973 1980
abline(v=CK$Date[which.max(CK$StockPrice)], col='green')
abline(v=CK$Date[which.min(CK$StockPrice)], col='red')
§ 2.2 In March of 2000, the technology bubble burst, and a stock market crash occurred. According to this plot, which company’s stock dropped more?
plot(CK$Date, CK$StockPrice, type='l', col='blue', lwd=2)
lines(PG$Date, PG$StockPrice, col="green", lwd=2)
abline(v = as.Date("2000-03-01"), lty=3, col='orange')
abline(v = as.Date("1983-07-01"), lty=3, col='orange')
legend("topright",legend=c("Coke","P&G"),col=c('blue','green'),lwd=2)
§ 2.3 (a) Around 1983, the stock for one of these companies (Coca-Cola or Procter and Gamble) was going up, while the other was going down. Which one was going up?
# Coca-Cola
# Coca-Cola
§ 3.1 Which stock fell the most right after the technology bubble burst in March 2000?
plot(CK$Date[301:432], CK$StockPrice[301:432],
type="l", col="red", ylim=c(0,210))
lines(PG$Date[301:432], PG$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(BE$Date[301:432], BE$StockPrice[301:432], col="orange")
abline(v = as.Date("2000-03-01"), lty=3, col='gray')
abline(v = as.Date("1997-09-01"), lty=3, col='gray')
abline(v = as.Date("1997-11-01"), lty=3, col='gray')
§ 3.2 Which stock reaches the highest value in the time period 1995-2005?
# IBM
§ 3.3 In October of 1997, there was a global stock market crash that was caused by an economic crisis in Asia. Comparing September 1997 to November 1997, which companies saw a decreasing trend in their stock price? (Select all that apply.)
# P&G and Boeing
§ 3.4 In the last two years of this time period (2004 and 2005) which stock seems to be performing the best, in terms of increasing stock price?
# Boeing
§ 4.1 For IBM, compare the monthly averages to the overall average stock price. In which months has IBM historically had a higher stock price (on average)? Select all that apply.
tapply(IBM$Stock, format(IBM$Date,"%m"), mean) - mean(IBM$Stock)
01 02 03 04 05 06 07 08 09 10 11 12
5.863 8.319 8.058 7.742 7.127 -5.284 -5.308 -4.230 -5.287 -7.028 -6.356 -3.616
§ 4.2 General Electric and Coca-Cola both have their highest average stock price in the same month. Which month is this?
sapply(list(BE=BE,CK=CK,GE=GE,IBM=IBM,PG=PG), function(df) {
tapply(df$Stock, format(df$Date,"%m"), mean) - mean(df$Stock)
}) # April
BE CK GE IBM PG
01 -0.08196 0.3388 2.7416 5.863 1.91347
02 0.29930 0.7050 3.2173 8.319 1.32124
03 0.28914 2.0416 3.8470 8.058 -0.35691
04 0.45393 2.6592 5.1766 7.742 -0.01781
05 1.54423 1.4139 1.5678 7.127 0.15506
06 0.79232 0.7824 -2.8351 -5.284 -0.31177
07 -0.03933 -1.0463 -2.5700 -5.308 -1.05896
08 0.27017 -1.1496 -2.8004 -4.230 -0.88185
09 -0.28808 -2.4295 -3.0644 -5.287 -1.08067
10 -1.37690 -2.0909 -3.0645 -7.028 -1.02548
11 -1.44303 -0.9270 -2.0147 -6.356 0.75159
12 -0.41979 -0.2975 -0.2013 -3.616 0.59209
§ 4.3 For the months of December and January, every company’s average stock is higher in one month and lower in the other. In which month are the stock prices lower?
# December