[1] "AMZN" "TSLA" "NFLX" "AAPL"
package 'knitr' successfully unpacked and MD5 sums checked
The downloaded binary packages are in
C:\Users\madha\AppData\Local\Temp\Rtmpqe3PCK\downloaded_packages
| symbol | volatility |
|---|---|
| NFLX_Close | 0.0333936 |
| TSLA_Close | 0.0286121 |
| AMZN_Close | 0.0187507 |
| AAPL_Close | 0.0150303 |
##Column
###Decision
Based on the data shown above, we recommend investing in Apple. This company has the highest expected return and relatively low volatility compared to the other companies we analyzed. We encourage you to conduct your own research and make an informed decision before investing.
---
title: "Lab 1"
author: "Madhav Shukla"
date: "April 1, 2023"
output:
flexdashboard::flex_dashboard:
orientation: columns
vertical_layout: fill
social: menu
source: embed
---
```{r}
# library(flexdashboard)
# flex_dashboard()
```
```{r setup, include=FALSE}
# Load necessary libraries
library(flexdashboard)
library(tidyverse)
library(lubridate)
library(quantmod)
library(plotly)
# Define color scheme
colors <- c("#619CFF", "#FFA500", "#FF5050", "#228B22")
```
## Column
## Summary
### Company stock prices
```{r}
# Define the companies to compare
companies <- c("AMZN", "TSLA", "NFLX", "AAPL")
# Get stock data from Yahoo Finance
start_date <- as.Date("2011-01-01")
end_date <- as.Date("2011-03-27")
options(repos = "https://cloud.r-project.org")
getSymbols(companies, src = "yahoo", from = start_date, to = end_date)
# Extract closing prices
stock_data <- data.frame(Date = index(AMZN), AMZN_Close = as.numeric(AMZN$AMZN.Close),
TSLA_Close = as.numeric(TSLA$TSLA.Close),
NFLX_Close = as.numeric(NFLX$NFLX.Close),
AAPL_Close = as.numeric(AAPL$AAPL.Close))
# Combine data for all companies and gather the Close prices
stock_data <- stock_data %>%
gather(key = "symbol", value = "Close", -Date)
# Format data for plotting
stock_plot <- stock_data %>%
mutate(date = ymd(Date)) %>%
ggplot(aes(x = date, y = Close, color = symbol)) +
geom_line(linewidth = 1) + # use linewidth instead of size
labs(title = "Stock Prices",
x = "Date",
y = "Closing Price",
color = "Company") +
theme_bw() +
theme(legend.position = "bottom",
legend.key.size = unit(0.5, "cm"),
axis.title = element_text(size = 14),
axis.text = element_text(size = 12))
# Add interactive tooltip
stock_plot <- ggplotly(stock_plot, tooltip = c("x", "y"))
stock_plot
```
## Column
### Returns
```{r}
# Calculate returns
returns <- stock_data %>%
group_by(symbol) %>%
mutate(return = Close / lag(Close, 1) - 1) %>%
ungroup()
# filter(!is.na(return)) # exclude the first day of data
# Plot returns
returns_plot <- ggplot(returns, aes(x = return, fill = symbol)) +
geom_density(alpha = 0.5) +
labs(title = "Distribution of Returns",
x = "Return",
y = "Density",
fill = "Company") +
scale_fill_manual(values = colors) +
theme_bw() +
theme(legend.position = "none",
axis.title = element_text(size = 14),
axis.text = element_text(size = 12))
returns_plot
```
## Column
### Volatility
```{r}
# Calculate volatility
volatility <- returns %>%
filter(!is.na(return) & is.finite(return)) %>%
group_by(symbol) %>%
summarize(volatility = sd(return, na.rm = TRUE)) %>%
arrange(desc(volatility))
# Plot volatility
ggplot(volatility, aes(x = symbol, y = volatility)) +
geom_bar(stat = "identity") +
coord_flip() +
labs(title = "Volatility",
x = "Company",
y = "Volatility") +
theme_bw()
# Show top 4 most volatile
install.packages("knitr") # install the package if not already installed
library(knitr) # load the package
kable(head(volatility, 4))
```
##Column
###Decision
Based on the data shown above, we recommend investing in Apple. This company has the highest expected return and relatively low volatility compared to the other companies we analyzed. We encourage you to conduct your own research and make an informed decision before investing.