Skip to content

Instantly share code, notes, and snippets.

View christophsax's full-sized avatar

Christoph Sax christophsax

View GitHub Profile
@christophsax
christophsax / ggplot_sec_axis.R
Last active April 28, 2018 10:56
Secondary Axis for Time Series in ggplot2
#' Secondary Axis in ggplot2
#'
#' To use a secondary axis in ggplot, the data shown on the right axis needs to
#' be transformed. Use `sec_scale` to calculate the scale adjutments. Use
#' `apply_sec_scale` to apply the scale adjutments within ggplot. See
#' examples.
#'
#' @param df1 a ts-boxable object, plotted on the left axis
#' @param df2 a ts-boxable object, plotted on the right axis
#' @param x numeric vector
@christophsax
christophsax / life-expectany.md
Last active February 11, 2018 12:05
Comparison of Life Expectancy

Life Expectancy

# devtools::install_github("christophsax/tsbox")
library(tsbox)
stopifnot(packageVersion("tsbox") >= "0.0.11.2")
suppressMessages(library(tidyverse))
library(wbstats)

# # Search
@christophsax
christophsax / demo.md
Last active January 3, 2018 14:00
Evaluating GDP Forecasts
### Preparing the Time Trip

library(timemachine)

# assuming EXP is available one period before
# (for demonstration only)
swiss_history2 <- swiss_history %>% 
  mutate(pub_date = if_else(var == "EXP", add_to_date(pub_date, "-1 quarter"), pub_date)) %>% 
  
@christophsax
christophsax / demo.R
Created December 3, 2017 15:03
Estimating constant seasonality
``` r
library(seasonal)
m <- seas(AirPassengers, regression.variables = c("const", "seasonal"), regression.aictest = NULL)
monthplot(m)
```
![](https://i.imgur.com/v0jYXoO.png)
``` r
@christophsax
christophsax / demo.md
Last active December 3, 2017 15:07
Forecasting monthly time series with common trends
library(tsbox)  # remotes::install_github("christophsax/tsbox")
library(forecast)
library(seasonal)

(Short) Time Series with common pattern

Monthly time series, with 4 to 5 years of data. Here, Monthly Deaths from Lung Diseases in the UK

@christophsax
christophsax / gist:cf2a3e53639e1212fae524891e60accc
Last active September 14, 2017 07:40
10 Year Forecast of AirPassengers seasonality
library(seasonal)

10 Year Forecast of AirPassengers seasonality

m <- seas(AirPassengers,  x11.appendfcst = "yes", forecast.maxlead = 120)
plot(series(m, "x11.adjustfac"))
@christophsax
christophsax / feather-fwrite.R
Last active October 19, 2018 19:26
Comparison data.table::fwrite vs feather::feather
# Some speed comparison of fwrite and feather:
library(data.table)
library(feather)
# On an AWS EC2 m4.10xlarge with 40 cores:
# with 100 mio
size <- 100000000
iris2 <- data.table(iris[sample(1:nrow(iris), size, replace = TRUE), ])