Skip to content

Instantly share code, notes, and snippets.

@neerajnagi
Last active October 3, 2016 11:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save neerajnagi/9176b3c4934e9eb4193b3a2d4660427a to your computer and use it in GitHub Desktop.
Save neerajnagi/9176b3c4934e9eb4193b3a2d4660427a to your computer and use it in GitHub Desktop.
R persistence & running stats

Db

Db is a typical key-value store, key is a vector so you can track any number of metrics with various combinations

Db.new()
Db.get(Vector) 
Db.put(Vector, Object)

Stats

Stats can be used to track moments calculation over every N batches

Stats(WINDOW_SIZE)  #constructor
Stats.add(double point)
Stats.mean()  
Stats.variance()
Stats.skewness()
Stats.kurtosis()

usage

use case: for each machine, we want to use variance of R of RMS over a 100 batch window. every 100 batch we do the calculation
json  # input json
stat = Db.get(c("machine-1234k-234j-32432k", "R", "RMS"))
if(!obj)  #obj not found , maybe you adding first point. So initialize that key.
  {
  stat = NStats(100)
  Db.put(c("machine-1234k-234j-32432k","R","RMS"), stat)
  }
 # add current point
 stat.add(json$ac1$R$RMS)


data = stat.get()
if(data!=FALSE)
  {
  use data$variance() in your algo
  }
#if data available use otherwise next time
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment