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)
library(tidyverse)
economic data Import the U.S. Industrial Production Index since 2010.USINDPRO <- tq_get("INDPRO",
get = "economic.data",
from = "2010-01-01")
USINDPRO
## # 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.stocks <- tq_get(c("^IXIC", "^GSPC"), get = "stock.prices", from = "2019-01-01")
stocks
## # A tibble: 620 x 8
## symbol date open high low close volume adjusted
## <chr> <date> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 ^IXIC 2019-01-02 6507. 6694. 6507. 6666. 2261800000 6666.
## 2 ^IXIC 2019-01-03 6585. 6600. 6457. 6464. 2607290000 6464.
## 3 ^IXIC 2019-01-04 6567. 6761. 6554. 6739. 2579550000 6739.
## 4 ^IXIC 2019-01-07 6758. 6856. 6741. 6823. 2507550000 6823.
## 5 ^IXIC 2019-01-08 6893. 6910. 6796. 6897 2380290000 6897
## 6 ^IXIC 2019-01-09 6923. 6985. 6900. 6957. 2422590000 6957.
## 7 ^IXIC 2019-01-10 6909. 6991. 6877. 6986. 2179080000 6986.
## 8 ^IXIC 2019-01-11 6947. 6976. 6934. 6971. 2066500000 6971.
## 9 ^IXIC 2019-01-14 6908. 6936. 6887. 6906. 1942210000 6906.
## 10 ^IXIC 2019-01-15 6931. 7026. 6928. 7024. 2038090000 7024.
## # … with 610 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: 310 x 8
## symbol date open high low close volume adjusted
## <chr> <date> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 ^GSPC 2019-01-02 2477. 2519. 2467. 2510. 3733160000 2510.
## 2 ^GSPC 2019-01-03 2492. 2493. 2444. 2448. 3822860000 2448.
## 3 ^GSPC 2019-01-04 2474. 2538. 2474. 2532. 4213410000 2532.
## 4 ^GSPC 2019-01-07 2536. 2566. 2525. 2550. 4104710000 2550.
## 5 ^GSPC 2019-01-08 2568. 2580. 2548. 2574. 4083030000 2574.
## 6 ^GSPC 2019-01-09 2580 2595. 2569. 2585. 4052480000 2585.
## 7 ^GSPC 2019-01-10 2574. 2598. 2562. 2597. 3704500000 2597.
## 8 ^GSPC 2019-01-11 2588. 2596. 2577. 2596. 3434490000 2596.
## 9 ^GSPC 2019-01-14 2580. 2589. 2570. 2583. 3664450000 2583.
## 10 ^GSPC 2019-01-15 2585. 2613. 2585. 2610. 3572330000 2610.
## # … with 300 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, c("symbol", "date", "close"))
stocks_selected
## # A tibble: 310 x 3
## symbol date close
## <chr> <date> <dbl>
## 1 ^GSPC 2019-01-02 2510.
## 2 ^GSPC 2019-01-03 2448.
## 3 ^GSPC 2019-01-04 2532.
## 4 ^GSPC 2019-01-07 2550.
## 5 ^GSPC 2019-01-08 2574.
## 6 ^GSPC 2019-01-09 2585.
## 7 ^GSPC 2019-01-10 2597.
## 8 ^GSPC 2019-01-11 2596.
## 9 ^GSPC 2019-01-14 2583.
## 10 ^GSPC 2019-01-15 2610.
## # … with 300 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("CNH/USD",
get = "exchange.rates")
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.141
## 9 2019-10-06 0.141
## 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.
Merged <- stocks_selected %>%
select(-symbol) %>%
left_join(FX, by = "date")
Merged
## # A tibble: 310 x 3
## date close exchange.rate
## <date> <dbl> <dbl>
## 1 2019-01-02 2510. NA
## 2 2019-01-03 2448. NA
## 3 2019-01-04 2532. NA
## 4 2019-01-07 2550. NA
## 5 2019-01-08 2574. NA
## 6 2019-01-09 2585. NA
## 7 2019-01-10 2597. NA
## 8 2019-01-11 2596. NA
## 9 2019-01-14 2583. NA
## 10 2019-01-15 2610. NA
## # … with 300 more rows
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
library(ggplot2)
ggplot(Merged,
aes(close, exchange.rate)) +
geom_point() +
geom_smooth(method = "lm")
There is a weak, positive correlation between the stock market and the exchange rate. As the stock market climbs, the exchange rate should climb with it.