Giter Club home page Giter Club logo

hands-on-time-series-analysis-with-r's Introduction

Hands-On Time Series Analysis with R

This is the code repository for Hands-On Time Series Analysis with R, published by Packt.

Perform time series analysis and forecasting using R

What is this book about?

Time series analysis is the art of extracting meaningful insights from, and revealing patterns in, time series data using statistical and data visualization approaches. These insights and patterns can then be utilized to explore past events and forecast future values in the series.

This book covers the following exciting features:

  • Visualize time series data and derive better insights
  • Explore auto-correlation and master statistical techniques
  • Use time series analysis tools from the stats, TSstudio, and forecast packages
  • Explore and identify seasonal and correlation patterns
  • Work with different time series formats in R
  • Explore time series models such as ARIMA, Holt-Winters, and more
  • Evaluate high-performance forecasting solutions

If you feel this book is for you, get your copy today!

https://www.packtpub.com/

Instructions and Navigations

All of the code is organized into folders. For example, Chapter02.

The code will look like the following:

library(TSstudio)
data(USgas)

Following is what you need for this book:

This book was written under the assumption that its readers have the following knowledge and skills:

  • Basic knowledge of statistics or econometrics, which includes topics such as regression modeling, hypothesis testing, normal distribution, and so on
  • Experience with R, or another programming language

With the following software and hardware list you can run all code files present in the book (Chapter 1-11).

Software and Hardware List

Chapter Software required OS required
1-12 R (≥ 3.0.2), Recommended R(≥ 3.4.0) Windows, Mac OS X, and Linux (Any)

We also provide a PDF file that has color images of the screenshots/diagrams used in this book. Click here to download it.

Related products

Get to Know the Author

Rami Krispin Rami Krispin is a data scientist at a major Silicon Valley company, where he focuses on time series analysis and forecasting. In his free time, he also develops open source tools and is the author of several R packages, including the TSstudio package for time series analysis and forecasting applications. Rami holds an MA in applied economics and an MS in actuarial mathematics from the University of Michigan—Ann Arbor.

Suggestions and Feedback

Click here if you have any feedback or suggestions.

Download a free PDF

If you have already purchased a print or Kindle version of this book, you can get a DRM-free PDF version at no cost.
Simply click on the link to download a free PDF copy of this book.

https://packt.link/free-ebook/9781788629157

hands-on-time-series-analysis-with-r's People

Contributors

eishad avatar nileshpackt avatar packt-itservice avatar packtutkarshr avatar ramikrispin avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

hands-on-time-series-analysis-with-r's Issues

[Page130] lags function error

Fixing error (miscalculation) of the lags on page 130:

The start = start(ts.obj) + l argument add the number of lags to the cycle units of the start argument. For example:

> start(ts.obj) 
[1] 1976    1
> start(ts.obj) + l
[1] 1979    4

Solution - replacing start = start(ts.obj) + l with time(ts_merged)[l + 1]:

# The lags function return the series with its l lags
lags <- function(ts.obj, l){
  ts_merged <- NULL
  # Creating n lags
  for(i in 1:l){
    ts_merged <- ts.union(ts_merged, stats::lag(ts.obj, k = -i))
  }
  # Merge the lags with the original series
  ts_merged <- ts.union(ts.obj, ts_merged)
  # Set the columns names
  colnames(ts_merged) <- c("y", paste0("y_", 1:i))
  # Removing missing values as results of creating the lags
  ts_merged <- window(ts_merged,
                      start = time(ts_merged)[l + 1],
                      end = end(ts.obj))
  return(ts_merged)
}

Thanks for Anoushiravan Razmavar for pointing out this issue!

Chapter 2: US_long_format and CA_mix_format cannot be converted.

Hello. In Chapter 2, My RStudio cannot convert US_long_format and CA_mix_format.
Please advise. Thx!

My code and output is ..

str(dates_df)
'data.frame': 22 obs. of 7 variables:
$ Japanese_format : chr "2017/1/20" "2017/1/21" "2017/1/22" "2017/1/23" ...
$ US_format : chr "1/20/2017" "1/21/2017" "1/22/2017" "1/23/2017" ...
$ US_long_format : chr "Friday, January 20, 2017" "Saturday, January 21, 2017" "Sunday, January 22, 2017" "Monday, January 23, 2017" ...
$ CA_mix_format : chr "20-Jan-17" "21-Jan-17" "22-Jan-17" "23-Jan-17" ...
$ SA_mix_format : chr "20-Jan-17" "21-Jan-17" "22-Jan-17" "23-Jan-17" ...
$ NZ_format : chr "20/01/2017" "21/01/2017" "22/01/2017" "23/01/2017" ...
$ Excel_Numeric_Format: int 42755 42756 42757 42758 42759 42760 42761 42762 42763 42764 ...

dates_df$Japanese_format_new <-

  • as.Date(dates_df$Japanese_format)

head(dates_df[ , c("Japanese_format", "Japanese_format_new")])
Japanese_format Japanese_format_new
1 2017/1/20 2017-01-20
2 2017/1/21 2017-01-21
3 2017/1/22 2017-01-22
4 2017/1/23 2017-01-23
5 2017/1/24 2017-01-24
6 2017/1/25 2017-01-25

identical(dates_df$Japanese_format, dates_df$Japanese_format_new)
[1] FALSE

class(dates_df$Japanese_format)
[1] "character"
class(dates_df$Japanese_format_new)
[1] "Date"

as.Date("31-01-2008")
[1] "0031-01-20"

as.Date("31-01-2008", format = "%d-%m-%Y")
[1] "2008-01-31"

dates_df$US_format[1]
[1] "1/20/2017"

dates_df$US_format_new <- as.Date(dates_df$US_format, format = "%m/%d/%Y")

dates_df$US_long_format[1]
[1] "Friday, January 20, 2017"
dates_df$US_long_format_new <- as.Date(dates_df$US_long_format,

  •                                    format = "%A, %B %d, %Y")
    

dates_df$CA_mix_format[1]
[1] "20-Jan-17"
dates_df$CA_mix_format_new <- as.Date(dates_df$CA_mix_format,

  •                                   format = "%d-%b-%y")
    

dates_df$NZ_format[1]
[1] "20/01/2017"
dates_df$NZ_format_new <- as.Date(dates_df$NZ_format,

  •                               format = "%d/%m/%Y")
    

head(dates_df)
Japanese_format US_format US_long_format CA_mix_format
1 2017/1/20 1/20/2017 Friday, January 20, 2017 20-Jan-17
2 2017/1/21 1/21/2017 Saturday, January 21, 2017 21-Jan-17
3 2017/1/22 1/22/2017 Sunday, January 22, 2017 22-Jan-17
4 2017/1/23 1/23/2017 Monday, January 23, 2017 23-Jan-17
5 2017/1/24 1/24/2017 Tuesday, January 24, 2017 24-Jan-17
6 2017/1/25 1/25/2017 Wednesday, January 25, 2017 25-Jan-17
SA_mix_format NZ_format Excel_Numeric_Format Japanese_format_new
1 20-Jan-17 20/01/2017 42755 2017-01-20
2 21-Jan-17 21/01/2017 42756 2017-01-21
3 22-Jan-17 22/01/2017 42757 2017-01-22
4 23-Jan-17 23/01/2017 42758 2017-01-23
5 24-Jan-17 24/01/2017 42759 2017-01-24
6 25-Jan-17 25/01/2017 42760 2017-01-25
US_format_new US_long_format_new CA_mix_format_new NZ_format_new
1 2017-01-20 2017-01-20
2 2017-01-21 2017-01-21
3 2017-01-22 2017-01-22
4 2017-01-23 2017-01-23
5 2017-01-24 2017-01-24
6 2017-01-25 2017-01-25
Sys.info()
sysname release version nodename
"Windows" "10 x64" "build 22621" "LAPTOP-0CQKUS9G"
machine login user effective_user
"x86-64" "sato_" "sato_" "sato_"

my_err.txt

arima_search error - hessian setting?

Hi Rami,

Onto the right repo.

Here's the block from the script:
My suspicion is that the problem is derived from hessian = TRUE.

data(USgas)

ts_plot(USgas,
title = "US Monthly Natural Gas consumption",
Ytitle = "Billion Cubic Feet",
Xtitle = "Year")

USgas_split <- ts_split(USgas, sample.out = 12)

train <- USgas_split$train
test <- USgas_split$test

par(mfrow=c(1,2))
acf(train, lag.max = 60)
pacf(train, lag.max = 60)

USgas_d12 <- diff(train, 12)

ts_plot(USgas_d12,
title = "US Monthly Natural Gas consumption - First Seasonal Difference",
Ytitle = "Billion Cubic Feet (First Difference)",
Xtitle = "Year")

USgas_d12_1 <- diff(diff(USgas_d12, 1))

ts_plot(USgas_d12_1,
title = "US Monthly Natural Gas consumption - First Seasonal and Non-Seasonal Differencing",
Ytitle = "Billion Cubic Feet (Difference)",
Xtitle = "Year")

par(mfrow=c(1,2))
acf(USgas_d12_1, lag.max = 60)
pacf(USgas_d12_1, lag.max = 60)

p <- q <- P <- Q <- 0:2

arima_grid <- expand.grid(p,q,P,Q)
names(arima_grid) <- c("p", "q", "P", "Q")
arima_grid$d <- 1
arima_grid$D <- 1

arima_grid$k <- rowSums(arima_grid)

library(dplyr)

arima_grid <- arima_grid %>% filter(k <= 7)

arima_search <- lapply(1:nrow(arima_grid), function(i){
md <- NULL
md <- arima(train, order = c(arima_grid$p[i], 1, arima_grid$q[i]), seasonal = list(order = c(arima_grid$P[i], 1, arima_grid$Q[i])))

results <- data.frame(p = arima_grid$p[i], d = 1, q = arima_grid$q[i],
P = arima_grid$P[i], D = 1, Q = arima_grid$Q[i],
AIC = md$aic)
}) %>% bind_rows() %>% arrange(AIC)

Error in optim(init[mask], armafn, method = optim.method, hessian = TRUE, :
non-finite finite-difference value [2]
Called from: optim(init[mask], armafn, method = optim.method, hessian = TRUE,
control = optim.control, trans = as.logical(transform.pars))

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.