You are on page 1of 4

https://rpubs.

com/outandaway/616045

Introduction to Financial Economics with R


Lesson 1. Time Series Analysis
Seungho Shin
18 May, 2020
1. Download RStudio:
     https://rstudio.com/products/rstudio/download/
 

2. How to Import Apple Stock Data into R?


     (1) Import Data from URL to R:

library(quantmod)
Apple_Web <- getSymbols("AAPL",
from = "2015-01-01", to = "2020-01-01",
auto.assign = FALSE)

head(Apple_Web)
## AAPL.Open AAPL.High AAPL.Low AAPL.Close AAPL.Volume
AAPL.Adjusted
## 2015-01-02 111.39 111.44 107.35 109.33 53204600
99.94589
## 2015-01-05 108.29 108.65 105.41 106.25 64285500
97.13024
## 2015-01-06 106.54 107.43 104.63 106.26 65797100
97.13942
## 2015-01-07 107.20 108.20 106.70 107.75 40105900
98.50152
## 2015-01-08 109.23 112.15 108.70 111.89 59364500
102.28619
## 2015-01-09 112.67 113.25 110.21 112.01 53699500
102.39584

tail(Apple_Web)
## AAPL.Open AAPL.High AAPL.Low AAPL.Close AAPL.Volume
AAPL.Adjusted
## 2019-12-23 280.53 284.25 280.37 284.00 24643000
282.5627
https://rpubs.com/outandaway/616045

## 2019-12-24 284.69 284.89 282.92 284.27 12119700


282.8313
## 2019-12-26 284.82 289.98 284.70 289.91 23280300
288.4428
## 2019-12-27 291.12 293.97 288.12 289.80 36566500
288.3333
## 2019-12-30 289.46 292.69 285.22 291.52 36028600
290.0446
## 2019-12-31 289.93 293.68 289.52 293.65 25201400
292.1638

 
     (2) Import a CSV Data File in R:
          At Yahoo Finance, https://finance.yahoo.com/, you get free stock quotes, market
news, international market data, social interaction
          and other valuable resources. Find and download Apple Inc.(AAPL) historical data
from January 1, 2015 to December 31, 2019.

Apple_CSV <- read.csv("AAPL.csv", header = TRUE, sep = ",")

head(Apple_CSV)
## Date Open High Low Close Adj.Close Volume
## 1 1/2/2015 111.39 111.44 107.35 109.33 99.94589 53204600
## 2 1/5/2015 108.29 108.65 105.41 106.25 97.13024 64285500
## 3 1/6/2015 106.54 107.43 104.63 106.26 97.13942 65797100
## 4 1/7/2015 107.20 108.20 106.70 107.75 98.50152 40105900
## 5 1/8/2015 109.23 112.15 108.70 111.89 102.28619 59364500
## 6 1/9/2015 112.67 113.25 110.21 112.01 102.39584 53699500

tail(Apple_CSV)
## Date Open High Low Close Adj.Close Volume
## 1253 12/23/2019 280.53 284.25 280.37 284.00 282.5627 24643000
## 1254 12/24/2019 284.69 284.89 282.92 284.27 282.8313 12119700
## 1255 12/26/2019 284.82 289.98 284.70 289.91 288.4428 23280300
## 1256 12/27/2019 291.12 293.97 288.12 289.80 288.3333 36566500
## 1257 12/30/2019 289.46 292.69 285.22 291.52 290.0446 36028600
## 1258 12/31/2019 289.93 293.68 289.52 293.65 292.1638 25201400

※ head(Name of Your Data) returns the first n rows of your data; tail(Name of Your Data)
returns the last n rows of your data.
https://rpubs.com/outandaway/616045

3. How to Visualize Apple Stock Data?


chart_Series(Ad(Apple_Web))

library(ggplot2)
qplot(x = 1:length(Apple_CSV$Date), y = Apple_CSV$Adj.Close, geom = "line")
+
geom_line(color = "darkblue") +
labs(x = "" , y = "Adjusted Price", title = "Apple Inc.") +
geom_hline(yintercept = mean(Apple_CSV$Adj.Close), color = "red") +
theme_light()

4. How to Summarize Apple Stock Data (CSV File)?


     (1) Entire Data Set

summary(Apple_CSV)
## Date Open High Low
## Length:1258 Min. : 90.0 Min. : 91.67 Min. : 89.47
## Class :character 1st Qu.:115.8 1st Qu.:116.52 1st Qu.:114.51
## Mode :character Median :150.4 Median :152.05 Median :149.18
## Mean :154.4 Mean :155.76 Mean :153.08
## 3rd Qu.:186.0 3rd Qu.:187.38 3rd Qu.:184.67
## Max. :291.1 Max. :293.97 Max. :289.52
## Close Adj.Close Volume
## Min. : 90.34 Min. : 84.96 Min. : 11362000
## 1st Qu.:115.50 1st Qu.:107.79 1st Qu.: 23780725
## Median :150.56 Median :145.16 Median : 31576250
## Mean :154.48 Mean :148.69 Mean : 35899955
## 3rd Qu.:186.00 3rd Qu.:181.52 3rd Qu.: 42864550
## Max. :293.65 Max. :292.16 Max. :162206300

 
     (2) Specific Variable Only - 3 Examples:
           Example 1: Adjusted Close Price
https://rpubs.com/outandaway/616045

summary(Apple_CSV$Adj.Close)
## Min. 1st Qu. Median Mean 3rd Qu. Max.
## 84.96 107.79 145.16 148.69 181.52 292.16

 
           Example 2: Open Price

summary(Apple_CSV$Open)
## Min. 1st Qu. Median Mean 3rd Qu. Max.
## 90.0 115.8 150.4 154.4 186.0 291.1

 
           Example 3: Volume

summary(Apple_CSV$Volume)
## Min. 1st Qu. Median Mean 3rd Qu. Max.
## 11362000 23780725 31576250 35899955 42864550 162206300

You might also like