economic data Import the U.S. Industrial Production Index since 2010.stock prices Import stock prices of NASDAQ and S&P500 since 2019.filter Select S&P500 and save it under stocks_filtered.select Select two variables (date and closing price) from stocks_filtered, and save it under stocks_selected.exchange rates Import the exchange rate between the U.S. dollar and the Chinese Yuan Renminbi, and save it under FX.left_join Merge FX and stocks_selected.Scatterplot Plot the relationship between the stock market and the exchange rate. Add the best fit line.# Load packages
library(tidyquant)
## Loading required package: lubridate
##
## Attaching package: 'lubridate'
## The following object is masked from 'package:base':
##
## date
## Loading required package: PerformanceAnalytics
## Loading required package: xts
## Loading required package: zoo
##
## Attaching package: 'zoo'
## The following objects are masked from 'package:base':
##
## as.Date, as.Date.numeric
##
## Attaching package: 'PerformanceAnalytics'
## The following object is masked from 'package:graphics':
##
## legend
## Loading required package: quantmod
## Loading required package: TTR
## Registered S3 method overwritten by 'quantmod':
## method from
## as.zoo.data.frame zoo
## Version 0.4-0 included new data defaults. See ?getSymbols.
## Loading required package: tidyverse
## ── Attaching packages ───────────────────────────────────────────────────────── tidyverse 1.3.0 ──
## ✓ ggplot2 3.3.0 ✓ purrr 0.3.3
## ✓ tibble 2.1.3 ✓ dplyr 0.8.5
## ✓ tidyr 1.0.2 ✓ stringr 1.4.0
## ✓ readr 1.3.1 ✓ forcats 0.5.0
## ── Conflicts ──────────────────────────────────────────────────────────── tidyverse_conflicts() ──
## x lubridate::as.difftime() masks base::as.difftime()
## x lubridate::date() masks base::date()
## x dplyr::filter() masks stats::filter()
## x dplyr::first() masks xts::first()
## x lubridate::intersect() masks base::intersect()
## x dplyr::lag() masks stats::lag()
## x dplyr::last() masks xts::last()
## x lubridate::setdiff() masks base::setdiff()
## x lubridate::union() masks base::union()
library(tidyverse)
library(dplyr)
economic data Import the U.S. Industrial Production Index since 2010.Hint: Find the symbol in FRED.
library(tidyquant)
library(tidyverse)
Production <- tq_get("INDPRO", get = "economic.data", from = "2010-01-01")
Production
## # A tibble: 122 x 2
## date price
## <date> <dbl>
## 1 2010-01-01 91.7
## 2 2010-02-01 92.0
## 3 2010-03-01 92.6
## 4 2010-04-01 92.9
## 5 2010-05-01 94.3
## 6 2010-06-01 94.4
## 7 2010-07-01 94.9
## 8 2010-08-01 95.1
## 9 2010-09-01 95.4
## 10 2010-10-01 95.1
## # … with 112 more rows
stock prices Import stock prices of NASDAQ and S&P500 since 2019.library(tidyquant)
library(tidyverse)
stocks <- tq_get(c("^GSPC", "^IXIC"), get = "stock.prices", from = " 1999-01-01")
## Warning: `cols` is now required.
## Please use `cols = c(stock.prices)`
stocks
## # A tibble: 10,682 x 8
## symbol date open high low close volume adjusted
## <chr> <date> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 ^GSPC 1999-01-04 1229. 1249. 1219. 1228. 877000000 1228.
## 2 ^GSPC 1999-01-05 1228. 1246. 1228. 1245. 775000000 1245.
## 3 ^GSPC 1999-01-06 1245. 1272. 1245. 1272. 986900000 1272.
## 4 ^GSPC 1999-01-07 1272. 1272. 1258. 1270. 863000000 1270.
## 5 ^GSPC 1999-01-08 1270. 1278. 1262. 1275. 937800000 1275.
## 6 ^GSPC 1999-01-11 1275. 1276. 1253. 1264. 818000000 1264.
## 7 ^GSPC 1999-01-12 1264. 1264. 1238. 1240. 800200000 1240.
## 8 ^GSPC 1999-01-13 1240. 1248. 1205. 1234. 931500000 1234.
## 9 ^GSPC 1999-01-14 1234. 1237. 1210. 1212. 797200000 1212.
## 10 ^GSPC 1999-01-15 1212. 1243. 1212. 1243. 798100000 1243.
## # … with 10,672 more rows
filter Select S&P500 and save it under stocks_filtered.Hint: See the code in 1.2.2 Selecting observations
stocks_filtered <- filter(stocks,
symbol == "^GSPC")
stocks_filtered
## # A tibble: 5,341 x 8
## symbol date open high low close volume adjusted
## <chr> <date> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 ^GSPC 1999-01-04 1229. 1249. 1219. 1228. 877000000 1228.
## 2 ^GSPC 1999-01-05 1228. 1246. 1228. 1245. 775000000 1245.
## 3 ^GSPC 1999-01-06 1245. 1272. 1245. 1272. 986900000 1272.
## 4 ^GSPC 1999-01-07 1272. 1272. 1258. 1270. 863000000 1270.
## 5 ^GSPC 1999-01-08 1270. 1278. 1262. 1275. 937800000 1275.
## 6 ^GSPC 1999-01-11 1275. 1276. 1253. 1264. 818000000 1264.
## 7 ^GSPC 1999-01-12 1264. 1264. 1238. 1240. 800200000 1240.
## 8 ^GSPC 1999-01-13 1240. 1248. 1205. 1234. 931500000 1234.
## 9 ^GSPC 1999-01-14 1234. 1237. 1210. 1212. 797200000 1212.
## 10 ^GSPC 1999-01-15 1212. 1243. 1212. 1243. 798100000 1243.
## # … with 5,331 more rows
select Select two variables (date and closing price) from stocks_filtered, and save it under stocks_selected.Hint: See the code in 1.2.2 Selecting observations
stocks_selected <- select(stocks_filtered, symbol, close)
stocks_selected
## # A tibble: 5,341 x 2
## symbol close
## <chr> <dbl>
## 1 ^GSPC 1228.
## 2 ^GSPC 1245.
## 3 ^GSPC 1272.
## 4 ^GSPC 1270.
## 5 ^GSPC 1275.
## 6 ^GSPC 1264.
## 7 ^GSPC 1240.
## 8 ^GSPC 1234.
## 9 ^GSPC 1212.
## 10 ^GSPC 1243.
## # … with 5,331 more rows
exchange rates Import the exchange rate between the U.S. dollar and the Chinese Yuan Renminbi, and save it under FX.Hint: Find the symbol in oanda.com.
FX <- tq_get("CNY/USD",
get = "exchange.rates")
## Warning: Oanda only provides historical data for the past 180 days. Symbol: CNY/
## USD
FX
## # A tibble: 180 x 2
## date exchange.rate
## <date> <dbl>
## 1 2019-09-28 0.140
## 2 2019-09-29 0.140
## 3 2019-09-30 0.140
## 4 2019-10-01 0.140
## 5 2019-10-02 0.140
## 6 2019-10-03 0.140
## 7 2019-10-04 0.140
## 8 2019-10-05 0.140
## 9 2019-10-06 0.140
## 10 2019-10-07 0.140
## # … with 170 more rows
left_join Merge FX and stocks_selected.Hint: For left_join, refer to our other textbook, R for Data Scince, Ch13.4 Mutating joins. Or Google dplyr::left_join() to find example codes.
Scatterplot Plot the relationship between the stock market and the exchange rate. Add the best fit line.Hint: See the code in 4.2.1 Scatterplot
Hint: See the scatterplot you created in the previous question.
Hint: Use message, echo and results in the chunk options. Refer to the RMarkdown Reference Guide.