library("RcppDynProg")
library("WVPlots")
library("ggplot2")
library("microbenchmark")
library("rqdatatable")

set.seed(2018)

# data
mk_data <- function(g) {
  d <- data.frame(
    x = 0.05*(1:(3*g))) # ordered in x
  n <- nrow(d)
  d$y_ideal <- sin((0.3*d$x)^2)
  d$y_observed <- d$y_ideal + 0.25*rnorm(n)
  d
}

d <- mk_data(100)
n <- nrow(d)
w <- 1 + numeric(n)


timings <- microbenchmark(
  Rcpp = xlin_fits(d$x, d$y_observed, w, 0, n-1),
  R_lm = xlin_fits_lm(d$x, d$y_observed, w),
  R_rowwise = xlin_fits_R(d$x, d$y_observed, w),
  R_vectorized = xlin_fits_V(d$x, d$y_observed, w),
  times = 10L)

print(timings)

p <- data.frame(timings)
p$seconds <- p$time/1e+9
p$method <- as.factor(p$expr)
p$method <- reorder(p$method, p$seconds)

summary <- p %.>%
  project(., 
          mean_seconds = mean(seconds),
          groupby = "method")
print(summary)


WVPlots::ScatterBoxPlotH(
  p, 
  "seconds", "method", 
  paste0("performance of PRESS statistic in R and Rcpp (n=", n, ")"))

WVPlots::ScatterBoxPlotH(
  p, 
  "seconds", "method", 
  paste0("performance of PRESS statistic in R and Rcpp (n=", n, ")")) + 
  scale_y_log10()

d <- mk_data(1000000)
n <- nrow(d)
w <- 1 + numeric(n)

# make data available for Python version
write.csv(d, file = gzfile("xlin_data.csv.gz"), row.names = FALSE, quote = FALSE)

timings <- microbenchmark(
  Rcpp = xlin_fits(d$x, d$y_observed, w, 0, n-1),
  R_rowwise = xlin_fits_R(d$x, d$y_observed, w),
  R_vectorized = xlin_fits_V(d$x, d$y_observed, w),
  times = 10L)

print(timings)

p <- data.frame(timings)
p$seconds <- p$time/1e+9
p$method <- as.factor(p$expr)
p$method <- reorder(p$method, p$seconds)

summary <- p %.>%
  project(., 
          mean_seconds = mean(seconds),
          groupby = "method")
print(summary)


WVPlots::ScatterBoxPlotH(
  p, 
  "seconds", "method", 
  paste0("performance of PRESS statistic in R and Rcpp (n=", n, ")"))

WVPlots::ScatterBoxPlotH(
  p, 
  "seconds", "method", 
  paste0("performance of PRESS statistic in R and Rcpp (n=", n, ")")) + 
  scale_y_log10()

Timings on a 2018 Dell XPS-13 laptop, 16 Gib RAM, LPDDR3, 2133 MT/s, Intel(R) Core(TM) i5-8250U CPU @ 1.60GHz (8 cores reported), idle, charged, and plugged into power supply. Ubuntu 18.04.1 LTS.

R.version.string

R.version

sessionInfo()


WinVector/RcppDynProg documentation built on Aug. 26, 2023, 4:47 a.m.