Coder Social home page Coder Social logo

ggseas's Introduction

ggseas R package

seasonal adjustment on the fly extension for ggplot2

Convenience functions that let you easily do seasonal adjustment on the fly with ggplot.

Depends on the seasonal package to give you access to X13-SEATS-ARIMA.

Travis-CI Build Status CRAN version CRAN RStudio mirror downloads

Installation

Install the stable version the usual way from CRAN:

install.packages("ggseas")

or the latest version (bugs and all) from GitHub:

devtools::install_github("ellisp/ggseas/pkg")

Usage - seasonal adjustment on the fly

So far there are three types of seasonal adjustment possible to be incorporated into a usual ggplot() command, substituting for where you'd normally have geom_line().

X13-SEATS-ARIMA

library(ggseas)
# make demo data with the convenience "time series to data.frame" function tsdf()
ap_df <- tsdf(AirPassengers)

# SEATS with defaults
ggplot(ap_df, aes(x = x, y = y)) +
   geom_line(colour = "grey80") +
   stat_seas() +
   ggtitle("SEATS seasonal adjustment - international airline passengers") +
   ylab("International airline passengers per month")
## Calculating starting date of 1949 from the data.
## Calculating frequency of 12 from the data.

plot of chunk unnamed-chunk-3

# X11 with no outlier treatment
ggplot(ap_df, aes(x = x, y = y)) +
   geom_line(colour = "grey80") +
   stat_seas(x13_params = list(x11 = "", outlier = NULL)) +
   ggtitle("X11 seasonal adjustment - international airline passengers") +
   ylab("International airline passengers per month")
## Calculating starting date of 1949 from the data.
## Calculating frequency of 12 from the data.

plot of chunk unnamed-chunk-3

ggplot(ldeaths_df, aes(x = YearMon, y = deaths, colour = sex)) +
   geom_point(colour = "grey50") +
   geom_line(colour = "grey50") +
   facet_wrap(~sex) +
   stat_seas(size = 2) +
   ggtitle("Seasonally adjusted lung deaths in the UK 1974 - 1979") +
   ylab("Deaths") +
   xlab("(light grey shows original data;\ncoloured line is seasonally adjusted)") +
   theme(legend.position = "none")
## Calculating starting date of 1974 from the data.
## Calculating frequency of 12 from the data.
## Calculating starting date of 1974 from the data.
## Calculating frequency of 12 from the data.

plot of chunk unnamed-chunk-3

STL (LOESS-based decomposition)

# periodic if fixed seasonality; doesn't work well:
ggplot(ap_df, aes(x = x, y = y)) +
   geom_line(colour = "grey80") +
   stat_stl(s.window = "periodic")
## Calculating frequency of 12 from the data.

plot of chunk unnamed-chunk-4

# seasonality varies a bit over time, works better:
ggplot(ap_df, aes(x = x, y = y)) +
   geom_line(colour = "grey80") +
   stat_stl(s.window = 7)
## Calculating frequency of 12 from the data.

plot of chunk unnamed-chunk-4

Classical decomposition

# default additive decomposition (doesn't work well in this case!):
ggplot(ap_df, aes(x = x, y = y)) +
   geom_line(colour = "grey80") +
   stat_decomp()
## Calculating frequency of 12 from the data.

plot of chunk unnamed-chunk-5

# multiplicative decomposition, more appropriate:
ggplot(ap_df, aes(x = x, y = y)) +
   geom_line(colour = "grey80") +
   stat_decomp(type = "multiplicative")
## Calculating frequency of 12 from the data.

plot of chunk unnamed-chunk-5

Usage - seasonal decomposition on the fly

From version 0.2.0 I introduce a summary graphic decomposition, similar to what you'd get with plot(stats::decompose(x)), but in the ggplot2 environment. As well as allowing ggplot2 look and feel of plots, you can also map a variable to the colour (or color) aesthetic, to allow two difference decompositions on the same graphic.

ggsdc(ap_df, aes(x = x, y = y), method = "decompose") +
   geom_line()
## Calculating frequency of 12 from the data.
## Warning: Removed 6 rows containing missing values (geom_path).

plot of chunk unnamed-chunk-6

ggsdc(ap_df, aes(x = x, y = y), method = "stl", s.window = 7) +
   labs(x = "", y = "Air passenger numbers") +
   geom_point()
## Calculating frequency of 12 from the data.

plot of chunk unnamed-chunk-6

ggsdc(ldeaths_df, aes(x = YearMon, y = deaths, colour = sex), method = "seas") +
      geom_line()
## Calculating starting date of 1974 from the data.
## Calculating frequency of 12 from the data.
## Calculating starting date of 1974 from the data.
## Calculating frequency of 12 from the data.

plot of chunk unnamed-chunk-6

library(scales) # for label= comma
serv <- subset(nzbop, Account == "Current account" & 
                  Category %in% c("Services; Exports total", "Services; Imports total"))

ggsdc(serv, aes(x = TimePeriod, y = Value, colour = Category),
      method = "seas", start = c(1971, 2), frequency = 4) +
   geom_line() +
   scale_y_continuous("NZ$ millions\ndecomposition by X13-SEATS-ARIMA", label = comma) +
   labs(x = "") +
   ggtitle("New Zealand services balance of payments -\ngreater seasonality in exports than imports") +
   theme_light()

plot of chunk unnamed-chunk-6

Coming in 0.5.0 - control facet titles during seasonal decomposition on the fly

   ggsdc(serv, aes(x = TimePeriod, y = Value, colour = Category),
         method = "stl", s.window = 7, frequency = 4,
         facet.titles = c("The original series", "The underlying trend", "Regular seasonal patterns", "All the randomness left")) +
      geom_line()

plot of chunk unnamed-chunk-7

ggseas's People

Contributors

ellisp 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

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

ggseas's Issues

Is there any way to change facet names in ggsdc?

My native language in Portuguese and I'm writing my undergraduate thesis in which I will show a plot made by using ggsdc(). I wonder if I can manually change the facets names so I can write what they mean in Portuguese.

CRAN version returns errors

for some reason the CRAN version isn't working, at least on one of my machines. Returns errors even when running the basic examples of stat_stl, stat_decomp, etc. Hopefully this is temporary. In the meantime, the github version (which is identical in theory) works fine.

New ggplot broke ggseas?

I'm getting this error in the first time using ggseas after upgrading to R 3.3.2 and ggplot2 2.2.0

Error: StatSeas was built with an incompatible version of ggproto.
Please reinstall the package that provides this extension.
Execution halted

Exited with status 1.

changing the order of levels of the factor mapped to colour doesn't control it in legend

in ggsdc

     ldeaths_df %>%
        mutate(sex = factor(sex, levels = c("male", "female"))) %>%
   ggsdc(aes(x = YearMon, y = deaths, colour = sex), method = "seas") +
   geom_line()

and

     ldeaths_df %>%
        mutate(sex = factor(sex, levels = c("female", "male"))) %>%
   ggsdc(aes(x = YearMon, y = deaths, colour = sex), method = "seas") +
   geom_line()

both produce the same results and they should change the order on the legend. An "as character" somewhere is killing the factor level ordering.

Add ability to export to plotly

"To add support for your custom geom, say GeomCustom, y ou'll need to export a to_basic.GeomCustom in your extension package. For example, since GeomLine is a special case of GeomPath sorted by x-value, to add support for GeomLine, we just sort the data by x and prefix a GeomPath class. If you can't convert your custom geom to a "basic" geom, you might need to also export a geom2trace.GeomCustom method. This will require some knowledge of the plotly.js JSON spec, but we'd be more than happy to help. If anyone has questions, or suggestions on better ways to do things, please let me know!"

Doesn't work unless source data are in time order

eg

ldeaths_sorted <- ldeaths_df[order(ldeaths_df$deaths), ]

  ggplot(ldeaths_sorted, aes(x = YearMon, y = deaths, colour = sex)) +
       stat_decomp(frequency = 12)

The fix will be to sort it by x during the stat.

Warning on OSX and Solaris with vignette build

https://cran.r-project.org/web/checks/check_results_ggseas.html

Version: 0.5.1 
Check: re-building of vignette outputs 
Result: WARN 
    Error in re-building vignettes:
     ...
    Warning in engine$weave(file, quiet = quiet, encoding = enc) :
     Pandoc (>= 1.12.3) and/or pandoc-citeproc not available. Falling back to R Markdown v1.
    Quitting from lines 125-126 (ggsdc.Rmd) 
    Error: processing vignette 'ggsdc.Rmd' failed with diagnostics:
    Process terminated
    Execution halted 
Flavor: r-patched-solaris-x86

Version: 0.5.1 
Check: re-building of vignette outputs 
Result: WARN 
    Error in re-building vignettes:
     ...
    
    Backtrace for this error:
    #0 0x129b9e579
    #1 0x129b9d945
    #2 0x7fff93ac4529
    sh: line 1: 89501 Trace/BPT trap: 5 /Volumes/SSD-Data/Builds/R-dev-web/QA/Simon/packages/el-capitan-x86_64/Rlib/3.5/x13binary/bin/x13ashtml /var/folders/xn/m8dzdsgs7sg8q7jgflnqm8w80000gn/T//RtmppExnyS/x1315bc558df5aa/iofile -n -s 2> /dev/null
    dyld: lazy symbol binding failed: Symbol not found: _mkostemp
     Referenced from: /Volumes/SSD-Data/Builds/R-dev-web/QA/Simon/packages/el-capitan-x86_64/Rlib/3.5/x13binary/bin/../lib/libgfortran.3.dylib (which was built for Mac OS X 10.12)
     Expected in: /usr/lib/libSystem.B.dylib
    
    dyld: Symbol not found: _mkostemp
     Referenced from: /Volumes/SSD-Data/Builds/R-dev-web/QA/Simon/packages/el-capitan-x86_64/Rlib/3.5/x13binary/bin/../lib/libgfortran.3.dylib (which was built for Mac OS X 10.12)
     Expected in: /usr/lib/libSystem.B.dylib
    
    
    Program received signal SIGTRAP: Trace/breakpoint trap.
    
    Backtrace for this error:
    #0 0x125f5f579
    #1 0x125f5e945
    #2 0x7fff93ac4529
    Quitting from lines 125-126 (ggsdc.Rmd) 
    Error: processing vignette 'ggsdc.Rmd' failed with diagnostics:
    X-13 has returned a non-zero exist status, which means that the current spec file cannot be processed for an unknown reason.
    Execution halted 
Flavor: r-release-osx-x86_64

ggsdc doesn't play nicely with tibble

For exmaple (not reproducible unless you have cam_df2):

This works:

cam_df2 %>%
  data.frame() %>%
  ggsdc(aes(x = TimePeriod, y = Value, color = CV1), start = c(1996,7), frequency = 12, s.window = 7) +
  geom_line()

but this doesn't:

cam_df2 %>%
  ggsdc(aes(x = TimePeriod, y = Value, color = CV1), start = c(1996,7), frequency = 12, s.window = 7) +
  geom_line()

Faceted decomposition graphs for Classic, STL, and X-13 ARIMA SEATS similar to default plotting of class "stl"

Great package. What would you think of possibly implementing functionality that provides a facet of decomposition similar to the default plotting of an "stl" class.

In code, something that mimics this:

some_ts <- ts(rnorm(100), frequency = 12)
plot(stl(some_ts, s.window = "periodic"))

But with ggplot2 styles and similar to your API. Maybe a facet = TRUE flag?

library(ggseas)
# make demo data
ap_df <- data.frame(
   x = as.numeric(time(AirPassengers)),
   y = as.numeric(AirPassengers)
)

ggplot(ap_df, aes(x = x, y = y)) +
   geom_line(colour = "grey80") +
   stat_decomp(frequency = 12, facet = TRUE)

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.