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.economic data Import the U.S. Industrial Production Index since 2010.## # A tibble: 38 x 2
## date price
## <date> <dbl>
## 1 2017-01-01 103.
## 2 2017-02-01 103.
## 3 2017-03-01 103.
## 4 2017-04-01 104.
## 5 2017-05-01 104.
## 6 2017-06-01 105.
## 7 2017-07-01 105.
## 8 2017-08-01 104.
## 9 2017-09-01 104.
## 10 2017-10-01 106.
## # … with 28 more rows
stock prices Import stock prices of NASDAQ and S&P500 since 2019.## Warning: `cols` is now required.
## Please use `cols = c(stock.prices)`
## # A tibble: 622 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 612 more rows
filter Select S&P500 and save it under stocks_filtered.Hint: See the code in 1.2.2 Selecting observations
## # A tibble: 311 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 301 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
## # A tibble: 311 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 301 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.
## Warning: Oanda only provides historical data for the past 180 days. Symbol: CNY/
## USD
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.
## # A tibble: 311 x 4
## symbol date close exchange.rate
## <chr> <date> <dbl> <dbl>
## 1 ^GSPC 2019-01-02 2510. NA
## 2 ^GSPC 2019-01-03 2448. NA
## 3 ^GSPC 2019-01-04 2532. NA
## 4 ^GSPC 2019-01-07 2550. NA
## 5 ^GSPC 2019-01-08 2574. NA
## 6 ^GSPC 2019-01-09 2585. NA
## 7 ^GSPC 2019-01-10 2597. NA
## 8 ^GSPC 2019-01-11 2596. NA
## 9 ^GSPC 2019-01-14 2583. NA
## 10 ^GSPC 2019-01-15 2610. NA
## # … with 301 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
## Warning: Removed 187 rows containing non-finite values (stat_smooth).
## Warning: Removed 187 rows containing missing values (geom_point).
Hint: See the scatterplot you created in the previous question.
There is no distinct correlation betwen the stock market and the exchange rate. The exchange rate also has lots of outliers and random points all over that do not show correlation with the stock market. The only similarity is that they both are rising.
Hint: Use message, echo and results in the chunk options. Refer to the RMarkdown Reference Guide.