Today in the computer lab we will go over some examples of time series data using R Tableau

Size: px
Start display at page:

Download "Today in the computer lab we will go over some examples of time series data using R Tableau"

Transcription

1 Visualizing Patterns over Time 2 Statistics 4868/6610 Data Visualization Prof. Eric A. Suess 2/12/2018 Introduction Today in the computer lab we will go over some examples of time series data using R Tableau Examples from the book Recall that the data files and relate R code can be downloaded from the book s website. Book We will look as some of the examples from Chapter 4 Hot Dog Eating, bargraphs with R The Hot Dog Eating example gives a good idea how to work with R to make bargraphs. Try the author s program bars.r or see Activity One: bars.rmd Note the use of the for loop and the if-then-else statement to color specific bars in the graph Note the use of the time labeling. Hot Dog Eating, bargraphs with Tableau Reproduce the graph from bars.r that is on page 96 using Tableau. First note that the hot-dogs-contest-winners.csv file is considered a Text File by Tableau. The Columns shelf will be Year and the Rows shelf will be SUM(Dogs eaten). To color code the years with a new recoded, drag New record to the Marks Color. Hot Dog Eating, map movie with Tableau Now produce a map movie of the Hot Dog Eating data showing the changing country of origin by time. Longitude is the Column Latitute if the Row And Year is the Pages 1

2 Hot Dog Eating, stacked bargraphs with R The Hot Dog Eating example gives a good idea how to work with R to make bargraphs. Try the author s program stackedbars.r FlowingData subscribers, time plot with R The FlowingData Subscribers example give a good idea of how to work with R to make time plots. Try the author s program scatter.r or see Activity Two: scatter.rmd What does type= h do in a plot() command? What does the points() command do? FlowingData subscribers, time plot with Tableau Now reproduce the graph from scatter.r that is on page 116 using Tableau. First note that the flowingdata_subscribers.csv file is considered a Text File by Tableau. The Columns shelf will be DAY(Date) and the Rows shelf will be SUM(Subscribers). Change the Marks to Bar and change the Size. World Population, time plot with Tableau Reproduce the graph from timeseries.r that is on page 120 using tableau. As produce the graph as part of Activity Three: timeseries.rmd US Postage, step chart with R The US Postage example gives a good idea how to work with R to make step charts. A step chart is for data in time that changes at a spcific time. US Postage rates change on specific dates. Try the author s program step.r or try Activity Four: step.rmd US Postage, step chart with R Reproduce the plots on page 126. What does type= s do in a plot() command? What does the points() command do? 2

3 Smoothing Data with R When looking at time series data it is common to examine time series plots for an underlying trend. The trend may linear or nonliear or may be periodic. The use of linear regression is common to see linear and nonlinear (quadratic, cubic, etc.) trends. The use of LOESS is commonly used when the data is not periodic. LOESS is locally weighted scatterplot smoothing. LOESS gives an easy way to smooth the data. Small slices are fitted with a low-degree polynomial, then the small curves a put together. Unemployment Data, step chart with R The Unemployment example gives a good idea how to work with R to smooth time series data using LOESS. Try the author s program loess.r or try Activity Five: loess.rmd Unemployment Data, step chart with R What does the function lines() do? What does the function scatter.smooth() do? Recall Time Series Models Basic models Additive model Y t = T t + S t + I t Multiplicative model Y t = T t S t I t What would a log transformation to to the multiplicative model? Recall Time Series Models In R Or decompose( ) stl() 3

4 Johnson & Johnson stock price with R library(astsa) plot(jj, type="o", ylab="quarterly Earnings per Share") Johnson & Johnson stock price with R library(astsa) plot(log(jj), type="o", ylab="quarterly Earnings per Share") Time Series random error - white noise x <- ts(rnorm(50)) plot(x) Johnson & Johnson stock price, decompose with R Using the Additive Model. Not so good! plot(decompose(jj, "additive")) Johnson & Johnson stock price, decompose with R Using the Multiplicative Model. This is better! plot(decompose(jj, "multiplicative")) Johnson & Johnson stock price, decompose Note: If we take the log() of the time series the heteroskedasticity is removed. plot(decompose(log(jj),"additive")) Johnson & Johnson stock price, ACF The time series is not stationary. So there is a slowly decaying ACF. acf(jj) 4

5 Figure 1: plot of chunk unnamed-chunk-1 5

6 Figure 2: plot of chunk unnamed-chunk-2 6

7 Figure 3: plot of chunk unnamed-chunk-3 7

8 Figure 4: plot of chunk unnamed-chunk-4 8

9 Figure 5: plot of chunk unnamed-chunk-5 9

10 Figure 6: plot of chunk unnamed-chunk-6 10

11 Figure 7: plot of chunk unnamed-chunk-7 11

12 Johnson & Johnson stock price, PCF pacf(jj) Johnson & Johnson stock price Differenced to remove trend. y t = x t x t 1 jj.d <- diff(log(jj)) Johnson & Johnson stock price Trend removed. plot(jj.d) Johnson & Johnson stock price Now we can see the seasonal part of the time series. acf(jj.d) Johnson & Johnson stock price Seasonally differenced to remove the seasonality. y t = x t x t d Where d is 4 because this is quarterly data. jj.d <- diff(log(jj), 4) Johnson & Johnson stock price This looks more random. plot(jj.d) Johnson & Johnson stock price No clear pattern. acf(jj.d) 12

13 Figure 8: plot of chunk unnamed-chunk-8 13

14 Figure 9: plot of chunk unnamed-chunk-10 14

15 Figure 10: plot of chunk unnamed-chunk-11 15

16 Figure 11: plot of chunk unnamed-chunk-13 16

17 Figure 12: plot of chunk unnamed-chunk-14 17