Use the given code below to answer the questions.
## Load package
library(tidyverse) # for cleaning, plotting, etc
## ── Attaching packages ────────────────────────────────────────── tidyverse 1.3.0 ──
## ✓ ggplot2 3.3.0 ✓ purrr 0.3.4
## ✓ tibble 3.0.1 ✓ dplyr 0.8.5
## ✓ tidyr 1.0.2 ✓ stringr 1.4.0
## ✓ readr 1.3.1 ✓ forcats 0.5.0
## ── Conflicts ───────────────────────────────────────────── tidyverse_conflicts() ──
## x dplyr::filter() masks stats::filter()
## x dplyr::lag() masks stats::lag()
library(tidyquant) # for financial analysis
## Loading required package: lubridate
##
## Attaching package: 'lubridate'
## The following objects are masked from 'package:dplyr':
##
## intersect, setdiff, union
## The following objects are masked from 'package:base':
##
## date, intersect, setdiff, union
## 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: 'xts'
## The following objects are masked from 'package:dplyr':
##
## first, last
##
## 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.
## ══ Need to Learn tidyquant? ═══════════════════════════════════════════════════════
## Business Science offers a 1-hour course - Learning Lab #9: Performance Analysis & Portfolio Optimization with tidyquant!
## </> Learn more at: https://university.business-science.io/p/learning-labs-pro </>
## Import data
stocks <- tq_get("AAPL", get = "stock.prices", from = "2016-01-01")
stocks
## # A tibble: 1,094 x 8
## symbol date open high low close volume adjusted
## <chr> <date> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 AAPL 2016-01-04 103. 105. 102 105. 67649400 98.2
## 2 AAPL 2016-01-05 106. 106. 102. 103. 55791000 95.8
## 3 AAPL 2016-01-06 101. 102. 99.9 101. 68457400 93.9
## 4 AAPL 2016-01-07 98.7 100. 96.4 96.4 81094400 89.9
## 5 AAPL 2016-01-08 98.6 99.1 96.8 97.0 70798000 90.4
## 6 AAPL 2016-01-11 99.0 99.1 97.3 98.5 49739400 91.9
## 7 AAPL 2016-01-12 101. 101. 98.8 100. 49154200 93.2
## 8 AAPL 2016-01-13 100. 101. 97.3 97.4 62439600 90.8
## 9 AAPL 2016-01-14 98.0 100. 95.7 99.5 63170100 92.8
## 10 AAPL 2016-01-15 96.2 97.7 95.4 97.1 79833900 90.6
## # … with 1,084 more rows
## Visualize
stocks %>%
ggplot(aes(x = date, y = close)) +
geom_line()
Hint: Insert a new code chunk below and type in the code, using the tq_get() function above. Replace the ticker symbol. Find ticker symbols from Yahoo Finance.
stocks <- tq_get("MSFT", get = "stock.prices", from = "2018-01-01")
stocks
## # A tibble: 591 x 8
## symbol date open high low close volume adjusted
## <chr> <date> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 MSFT 2018-01-02 86.1 86.3 85.5 85.9 22483800 83.0
## 2 MSFT 2018-01-03 86.1 86.5 86.0 86.3 26061400 83.4
## 3 MSFT 2018-01-04 86.6 87.7 86.6 87.1 21912000 84.2
## 4 MSFT 2018-01-05 87.7 88.4 87.4 88.2 23407100 85.2
## 5 MSFT 2018-01-08 88.2 88.6 87.6 88.3 22113000 85.3
## 6 MSFT 2018-01-09 88.7 88.7 87.9 88.2 19484300 85.2
## 7 MSFT 2018-01-10 87.9 88.2 87.4 87.8 18652200 84.8
## 8 MSFT 2018-01-11 88.1 88.1 87.2 88.1 17808900 85.1
## 9 MSFT 2018-01-12 88.7 89.8 88.4 89.6 24271500 86.6
## 10 MSFT 2018-01-16 90.1 90.8 88.0 88.3 36599700 85.3
## # … with 581 more rows
Hint: Watch the video, “Basic Data Types”, in DataCamp: Introduction to R for Finance: Ch1 The Basics.
Logical data is true or false data. Character data is data such as favorite colors, survery takers names, and addresses.
Hint: Insert a new code chunk below and type in the code, using the ggplot() function above. Revise the code so that it maps adjusted to the y-axis, instead of close.
knitr::opts_chunk$set(echo = TRUE, message = FALSE, results = 'markup')
stocks %>%
ggplot(aes(x = date, y = `adjusted`)) +
geom_line()
For more information on the ggplot() function, refer to Ch2 Introduction to ggplot2 in one of our e-textbooks, Data Visualization with R.
Since the beginning of 2019, Microsoft stock has increased reaching at a high point a few months into 2020.
Hint: Insert a new code chunk below and type in the code, using the tq_get() function above. You may refer to the manual of the tidyquant r package. Or, simply Google the tq_get function and see examples of the function’s usage. Do this by using the tq_get() function once, not twice.
mult_stocks <- tq_get(c("MSFT", "AMZN"),
get = "stock.prices",
from = "2016-01-01",
to = "2017-01-01")
mult_stocks
## # A tibble: 504 x 8
## symbol date open high low close volume adjusted
## <chr> <date> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 MSFT 2016-01-04 54.3 54.8 53.4 54.8 53778000 50.4
## 2 MSFT 2016-01-05 54.9 55.4 54.5 55.0 34079700 50.6
## 3 MSFT 2016-01-06 54.3 54.4 53.6 54.0 39518900 49.7
## 4 MSFT 2016-01-07 52.7 53.5 52.1 52.2 56564900 48.0
## 5 MSFT 2016-01-08 52.4 53.3 52.2 52.3 48754000 48.1
## 6 MSFT 2016-01-11 52.5 52.8 51.5 52.3 36943800 48.1
## 7 MSFT 2016-01-12 52.8 53.1 52.1 52.8 36095500 48.5
## 8 MSFT 2016-01-13 53.8 54.1 51.3 51.6 66883600 47.5
## 9 MSFT 2016-01-14 52 53.4 51.6 53.1 52381900 48.8
## 10 MSFT 2016-01-15 51.3 52.0 50.3 51.0 71820700 46.9
## # … with 494 more rows
Hint: Use message, echo and results in the chunk options. Refer to the RMarkdown Reference Guide.
Hint: Use echo and results in the chunk option. Note that this question only applies to the individual code chunk of Q6.