Time Series in 5-Minutes, Part 1: Visualization with the Time Plot

June 7, 2020

[This article was first published onbusiness-science.io,and kindly contributed to188bet app]. (You can report issue about the content on this pagehere
Want to share your content on R-bloggers?188bet app if you have a blog, orhereif you don't.

有5分钟?那么让我们来学习的时间序列。In this short articles series, I highlight how you can get up to speed quickly on important aspects of time series analysis. Today we are focusing on the most fundamental tool,时间曲线。了解如何使interactiveplotly)and静态的GGPLOT2)可视化与轻松timetk


This article has been updated.View the updated Time Series in 5-Minutes article at Business Science.

Time Series in 5-Mintues

I just releasedtimetk2.0.0(阅读发行公告)。A ton of new functionality has been added. We’ll discuss some of the key pieces in this article series:

  • 第1部分,时间散点图
  • 第2部分,自相关
  • 第3部分,季节性
  • Part 4, Anomalies and Anomaly Detection
  • 第5部分,缺失时间序列数据处理中



This tutorial focuses on,plot_time_series(),一个主力的时间序列绘制函数:

  • Generates interactiveplotly地块(great for exploring & shiny apps)
  • 来合并20+线GGPLOT2plotlycode
  • Scales well to many time series
  • 可以从互动转换plotlyGGPLOT2地块

加载以下库。对于本教程的目的,我将所有的阴谋静GGPLOT2运用互动< - FALSE,但我鼓励你这个切换到TRUEto see how easy it is to make interactiveplotly地块。

librarytidyverselibrarylubridatelibrarytimetk# Setup for the plotly charts (# FALSE returns ggplots)interactive<-FALSE

Plotting a Single Time Series

让我们从一个流行的时间序列开始,taylor_30_min,which includes energy demand in megawatts at a sampling interval of 30-minutes. This is a single time series.

###A tibble:4032×2 ##的日期值##
           ## 1 2000年6月5日00:00:00 22262 ## 2 2000年6月5日00:30:00 21756 ## 3 2000年6月5日01:00:00 22247 ## 4 2000年6月5日01:30:00 22759 ## 5 2000年6月5日02:00:00 22549 ## 6 2000年6月5日二点30分00秒22313 ## 7 2000年6月5日3时00分00秒22128 ##8 2000年6月5日三点30分00秒21860 ## 9 2000年6月5日四点00分00秒## 21751 10 2000年6月5日4点三十分00秒21336 ###...与4022个更多的行

plot_time_series()function generates an interactiveplotly通过默认图表。

  • 简单地提供日期变量(基于时间的列,.date_var)and the numeric variable (。value)作为第2个参数随时间改变
  • When薛志东隋卫平李利军= TRUE中,.plotly_slider = TRUEadds a date slider to the bottom of the chart.
taylor_30_min%>%plot_time_seriesdatevalue。互动=interactive# <- Toggle this TRUE/FALSE.plotly_slider=TRUE

plot of chunk unnamed-chunk-3

Plotting Groups


Visualizing grouped data很简单,只要分组与所述数据集通过...分组()管道联接到前plot_time_series()功能。关键点:

  • Groups can be added in 2 ways: by通过...分组()或者使用...to add groups.
  • 然后基转化为刻面。
  • .facet_ncol = 2returns a 2-column faceted plot
  • .facet_scales = “自由”允许每个图的x和y轴以独立地缩放其他情节

plot of chunk unnamed-chunk-4


你可能会问,那是什么blue line这一直显示在我们所有的阴谋了。这就是所谓的一smoother,and it’s a really awesome way to visualize trend through the noise in a time series.

We can adjust the smoother using:

  • 开启/关闭:。光滑=TRUE/FALSE
  • 改变线的灵活性:Try.smooth_period =“52周”(数据的30天)或.smooth_span = 0.25(25% of data). By default,。光滑_span获得优先权。

Here I’m changing thesmooth_span = 0.25通过使用在更平滑的数据的25%,以增加的灵活性。这并不是说我使用的时间序列是一个每周系列,m4_weekly

m4_weekly%>%通过...分组id%>%plot_time_seriesdatevalue#平滑。光滑=TRUE。光滑_span=0.25# <- Uses % of data# .smooth_period = "52 weeks", # <- Uses windows of data.facet_ncol=2.facet_scales="free"。互动=interactive

plot of chunk unnamed-chunk-5

Visualizing Transformations & Sub-Groups

Let’s switch to an hourly dataset with multiple groups. We can showcase:

  1. 对数变换to the。value
  2. 用于.color_varto highlight子组。

这样做的目的是展示方位地块组,但要突出每周窗口(周子组,使用week())中的数据,同时做一个内log()transformation to the value. This is simple to do:

  1. 。value = log(value)Applies the Log Transformation
  2. 该data is ungrouped, so we can add facets internally using the...to supply one or more facet columns.
  3. .color_var =周(日期)日期列被变换为lubridate::小k()数。颜色应用于每个星期的数字。
m4_hourly%>%通过...分组id%>%plot_time_seriesdatelogvalue),# Apply a Log Transformation.color_var=weekdate),#颜色应用于周转型#小面格式.facet_ncol=2.facet_scales="free"。互动=interactive

plot of chunk unnamed-chunk-6


所有的可视化可以从互动转换plotly(great for exploring and shiny apps) to staticGGPLOT2visualizations (great for reports).

  • 切换对话/静态:薛志东隋卫平李利军= TRUE / FALSE
  • 添加标题,图例,X和Y轴标签:。标题。color_lab.x_laband.y_lab
taylor_30_min%>%plot_time_seriesdatevalue.color_var=date标签=TRUE),。互动=FALSE#< - 返回静态ggplot# Customization。标题="Taylor's MegaWatt Data".x_lab="Date (30-min intervals)".y_lab="Energy Demand (MW)"。color_lab=“月”+scale_y_continuous标签=scales::comma_format())

plot of chunk unnamed-chunk-7

时间系列课程(Coming Soon)

I teach Time Series (timetk,more) in my时间序列分析和预测课程。如果有兴趣学习临预测策略则join my waitlist。该课程即将到来。


  • 时间序列预处理,降噪,和异常检测
  • Feature engineering using lagged variables & external regressors
  • 超参数调整
  • 时间序列交叉验证
  • Ensembling Multiple Machine Learning & Univariate Modeling Techniques (Competition Winner)
  • NEW – Deep Learning with RNNs (Competition Winner)
  • 和更多。

Signup for the Time Series Course waitlist

Have questions on using Timetk for time series?

Make a comment in the chat below.

而且,如果你打算使用timetk为您的企业,这是一个没有脑子 -Join my Time Series Course Waitlist(It’s coming, it’s really insane).

leave a comment对于作者,请按照自己的博客上的链接和评论:business-science.io

188bet appR-bloggers.com报价daily e-mail updates金宝搏网址 [R新闻和教程金宝搏网址金宝搏网址 and many other topics.点击这里,如果你正在寻找张贴或找到一个R /数据科学工作
Want to share your content on R-bloggers?188bet app if you have a blog, orhereif you don't.

If you got this far, why not订阅更新从网站?选择你的味道:e-mailtwitter1188bet app, 要么facebook...

Comments are closed.

搜索R-博客188bet app


Subscribe to R-bloggersto receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)