---
title: "Bayesian Statistics and Computing"
author: "Yanfei Kang"
date: "`r Sys.Date()`"
output:
# pdf_document: default
html_document: default
subtitle: 'Lecture 6: Reproducible learning via `rmarkdown`'
bibliography: refs.bib
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(tidy = TRUE,
echo = TRUE,
warning = FALSE,
message = FALSE)
```
```{r xaringanExtra-clipboard, echo=FALSE}
htmltools::tagList(
xaringanExtra::use_clipboard(
button_text = "",
success_text = "",
),
rmarkdown::html_dependency_font_awesome()
)
```
## Why R markdown?
- Much **easier** *syntax* ~~syntax~~ than LaTex or Html
- Dynamic: easy to update and work with the R codes
- Multiple output formats
- Makes presentation easy
- Keep me (and my group) organized of weekly research progress reports, slides, papers etc.
## Header 1
### Header 2
## Creation of lists
### unordered
- Item 1
- Item 2
### ordered
1. Item 1
2. Item 2
+ item 2a
+ item 2b
## Inline codes
Write inline **R** code using backtick quotes: `forecast()`.
## Equations
- $x + y = z$ for inline equations
- $$ x^2 + \sqrt{(y)} = \epsilon $$
## Hyperlink
1. [R markdown website](http://rmarkdown.rstudio.com)
2. [R markdown guide](https://bookdown.org/yihui/rmarkdown/)
************************************
## R code with errors
```{r, error=TRUE}
x > 1
```
## R code chunks with plot
See @hong2014global.
```{r RcodeDemoPlot, fig.width=8, fig.height=4, echo=TRUE, eval=TRUE, cache=TRUE}
library(fpp3)
# Load the Victorian electricity demand data from fpp3
data <- vic_elec
# Aggregate data by daily demand
daily_elec <- data %>%
index_by(Date = as_date(Time)) %>%
summarise(DailyDemand = sum(Demand))
# View the first few rows of the aggregated data
head(daily_elec)
autoplot(daily_elec, DailyDemand) +
ggtitle("Daily Electricity Demand in Victoria") +
xlab("Date") +
ylab("Electricity Demand (MW)")
```
## Interactive plots
You can use interactive plots for if you output html documents.
```{r RcodeDemoPlotly, fig.width=8, fig.height=4, echo=FALSE, eval=TRUE}
# Create an interactive plot using Plotly
library(plotly)
p <- plot_ly(daily_elec, x = ~Date, y = ~DailyDemand, type = 'scatter', mode = 'lines') %>%
layout(title = "Daily Electricity Demand in Victoria",
xaxis = list(title = "Date"),
yaxis = list(title = "Electricity Demand (MW)"))
# Display the plot
p
```
## R code chunks with table
```{r RcodeDemoTable}
knitr::kable(head(iris))
```
## Plain code block
```
library(forecast)
library(ggplot2)
autoplot(AirPassengers)
```
## References