You will not be able to complete HW 5 without having Quarto installed on your computer.
Quarto Dashboard is a new feature of Quarto that is extremely flexible and straightforward to use.
The Quarto Dashboard Gallery includes example dashboards made with R, Python, and other langaugages.
In this course I will provide a simple template for HW 5 that can be used to build your dashboard.
Once you understand how to add pages, rows, column, tabsets, and modify as needed you are welcome to tailor the template to your project.
A Quarto dashboard is a flexible blank canvas that you can tailor to your project and future endeavors.
Types of Time Series Data in R
In recent weeks, we have worked with Box Office Mojo and Bureau of Labor Statistics Data
These datasets are time series data.
They all include a date variable and another quantitative variable that changes at each time period.
So far we have worked with data in an R format called a tibble.
Two common data formats in R, tibble and data.frame are needed for creating ggplots of time series.
tibble is the more modern format and is more compatible with tidyverse commands to manage data.
Today, we’ll discuss a third data format, xts that can be used specifically for time series data.
Importing Stock Data as xts using tidyquant Package
Yahoo Finance, the Federal Reserve Bank, the Wall Street Journal, and others are excellent data sources that can be directly imported into R.
The default for getsymbols in the tidyquant package is Yahoo Finance.
Data format is xts which we will cover today
#|label: importing data from yahoo finance#|output: false# download data from Netflix, Amazon, Disney# time series starts day after from date specified# time series ends day before to date specifiedgetSymbols("NFLX", from ="2015-01-01", to ="2024-09-30")
[1] "NFLX"
getSymbols("AMZN", from ="2015-01-01", to ="2024-09-30")
[1] "AMZN"
getSymbols("DIS", from ="2015-01-01", to ="2024-09-30")
[1] "DIS"
Example of hchart for One Stock
hchart in the highcharter package is one way to plot xts data
Converting xts to a tibble or dataframe (R data formats) is required if you want to create a ggplot or use other methods covered previously
A good first step is to create a merged xts dataset of the desired variables.
#|label: merge xts stock data # data are merged by matching datesnflx_amzn_dis <-merge(NFLX$NFLX.Adjusted, AMZN$AMZN.Adjusted, DIS$DIS.Adjusted) head(nflx_amzn_dis)
There are a few ways to convert an xts to a tibble.
In the code below I show the conversion and then I rename the the new date variable as date
# converting data to a tibble requires a couple lines of code # I prefer to rename the index as date nflx_amzn_dis_tibble <- nflx_amzn_dis |>fortify.zoo() |>as_tibble(.name_repair ="minimal") |>rename("date"="Index") head(nflx_amzn_dis_tibble)
Any dataset with a date formatted variable can be converted to an xts dataset
This means that we can create a hchart or dygraph (next topic) for any dataset with a date variable.
#|label: convert tibble to xtsexp_imp <-read_csv("data/export_import_tidy.csv", show_col_types=F)exp_imp_xts <-xts(x=exp_imp[,2:3], order.by=exp_imp$date) # order.by must be a date variable
Straightforward to modify, add reference lines and shaded regions
Both dygraph and hchart allow viewer to interactively select date range
Here is the dataset we will use:
#|label: dataset for dygraphs examplethree_stocks <-merge(AMZN$AMZN.Adjusted, DIS$DIS.Adjusted, NFLX$NFLX.Adjusted) names(three_stocks) <-c("AMZN.adj", "DIS.adj", "NFLX.adj")head(three_stocks, 3) # print first three rows only
Create an unformatted hchart OR a dygraph with two variables
Plot lfM and empM and save it as labor_hc or labor_dy
#|label: create and display labor hchart# (labor_hc <- hchart()) or (labor_dy <- dygraph())
Basic hchart
Basic dygraph
In-class Exercise - Final Steps
Submit screenshots of plot from Viewer pane.
Save R code as an R Script. In the R project folder I have saved an R Script for your work (Updated October 2024).
Copy and paste code into provided R Script and use save as to save the file with your name., e.g. Week_7_In_Class_Penelope_Pooler.R
R Script should include:
code I provided to import and modify data
tibble to xts conversion of labor dataset
hchart OR dygraph plot code with comments
Submit final script on Blackboard (counts towards class participation for Week 7)
Due by Friday 2/28. No late submission accepted for In-class Exercises.
Quarto, R Markdown files and R Scripts
Quarto and Markdown files are ‘smart’, i.e. aware of where they are located.
R Scripts (older common file type) are useful BUT not aware of file location.
User must specify working directory
The script I provided is saved to your working directory
To check working directory: getwd()
To set working directory to code_data_output folder: (for working in an R Script)
Click Session > Set Working Directory > To Source File Location
NOTES:
R users and developers do not recommend setting working directories within code which would have to be changed for each laptop.
Whenever possible, use R Projects and ‘smart’ files such as .qmd and .Rmd files.
Key Points from This Week
Time Series Data
Importing stock data from Yahoo Finance as xts
Converting between xts and tibble
Plotting options include area plots, hcharts and dygraphs
dygraphs and hcharts are useful tools for understanding, managing, and curating time series data.
HW 4 due Wednesday, 2/26.
Grace period in effect.
TAs and I are available to assist if you have questions.
You may submit an ‘Engagement Question’ about each lecture until midnight on the day of the lecture. A minimum of four submissions are required during the semester.