--- title: "STAT 436 / 536 - Lecture 2: Time Series Intro" date: August 29, 2018 output: pdf_document --- ```{r setup, include=FALSE} knitr::opts_chunk$set(echo = TRUE) knitr::opts_chunk$set(warning = FALSE) library(datasets) library(ggplot2) library(dplyr) ``` ### Time Series Basics - So what is a time series? \vfill \vfill - *Time series models*, \vfill - What is the purpose of time series analyses? - \vfill - \vfill - \vfill - What are the defining features of time series data? - \vfill - \vfill - \vfill \newpage ### R Overview - R will be used frequently in this class. All assignments, labs, and take home exams should be completed using R Markdown to create reproducible reports. \vfill - We will also spend time creating dynamic graphics and use R Shiny. \vfill - For more details on some useful R packages see [https://www.rstudio.com/resources/cheatsheets/](https://www.rstudio.com/resources/cheatsheets/). - \vfill - \vfill - \vfill - \vfill ##### Time Series Objects - R allows objects to be defined as a time series class using the `ts()` command. The time series object contains - \vfill - \vfill - \vfill - \vfill ```{r} library(datasets) data(AirPassengers) str(AirPassengers) ``` \newpage ##### Plotting in R Many time series objects can be directly plotted using the `ggfortify` package. ```{r, message = F, fig.align='center', fig.width=6, fig.height=4} library(ggplot2) library(ggfortify) autoplot(AirPassengers) + ylim(0,650) + labs(title="Monthly Airline Passenger Count", y="Number of Passengers(thousands)", x= 'Year') ``` \newpage ##### Simulating discrete-time stochastic process \vfill ```{r, message = F, fig.align='center', fig.width=6, fig.height=4} set.seed(08182018) maxT <- 100 y = rep(0, T) evolution.var <- 1 # Simulate Data for (time.pt in 2:maxT){ y[time.pt] <- y[time.pt - 1] + rnorm(1, 0, evolution.var) } # Plot Data y.dat <- data.frame(response = y, time = 1:maxT) ggplot(data=y.dat, aes( x=time, y =response)) + geom_line() + geom_point() ``` \newpage ##### Advanced R: with Baltimore Tow For this example we will work through some basic commands to create a time series plot using a dataset containing information on vehicle towed in Baltimore, Maryland. 1. Download the dataset ```{r} library(readr) tow <- read_csv('http://math.montana.edu/ahoegh/teaching/stat408/datasets/BaltimoreTowing.csv') ``` 2. Modify the data variable to be usable format ```{r} library(dplyr) ``` 3. Create an figure to display time series aspect of the dataset ```{r} library(ggplot2) ```