Skip to content

Instantly share code, notes, and snippets.

View christophsax's full-sized avatar

Christoph Sax christophsax

View GitHub Profile
@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), ])
@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 / 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 / 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 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 / 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 / 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 / swiss.csv
Created May 12, 2018 18:57
R dataset swiss
Location Fertility Agriculture Examination Education Catholic Infant.Mortality
Courtelary 80.2 17 15 12 9.96 22.2
Delemont 83.1 45.1 6 9 84.84 22.2
Franches-Mnt 92.5 39.7 5 5 93.4 20.2
Moutier 85.8 36.5 12 7 33.77 20.3
Neuveville 76.9 43.5 17 15 5.16 20.6
Porrentruy 76.1 35.3 9 7 90.57 26.6
Broye 83.8 70.2 16 7 92.85 23.6
Glane 92.4 67.8 14 8 97.16 24.9
Gruyere 82.4 53.3 12 7 97.67 21
@christophsax
christophsax / demo.md
Created May 17, 2018 17:20
Multivariate Time Series from Quantmod
library(tsbox)
suppressMessages(library(quantmod))
suppressMessages(library(ggplot2))

ts_fred <- function(..., class = "data.frame") {
  symb <- c(...)
  dta.env <- new.env()
  suppressMessages(getSymbols(symb, env = dta.env, src = "FRED"))
 z &lt;- data.table::rbindlist(lapply(as.list(dta.env), ts_dt), idcol = "id")
@christophsax
christophsax / download.md
Last active May 21, 2018 14:14
Reading data from Gapminder repo into R

It's relatively straigtforward to read the Gapminder data directly from the Systema Globalis repo into R. Here's the code to produce an extract with the Gapminder core variables.

The result is stored below and can be accessed by:

gapminder <- read.csv("https://gist.githubusercontent.com/christophsax/8d2814a7d3e464d020e38a9cb468930b/raw/361889b5ea7830e2bf8f28f40239a2a08765432d/gapminder.csv", stringsAsFactors = FALSE)

library(ggplot2)