Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
##
#Finding the hottest observations per day (or a _single_ maxima for each day)
##
r.db("telemetry_pi").table("observations").create_index
r.db('telemetry_pi').table("observations").group({index: "datetime"}).max("temp").ungroup()("reduction")
##
#Finding some basic statistics and calculating a simple linear regression (y_humidity = alpha + beta_temp * temp)
##
# https://en.wikipedia.org/wiki/Simple_linear_regression
# https://en.wikipedia.org/wiki/Pearson_product-moment_correlation_coefficient
##
r.object(
"sum_humidity", r.db("telemetry_pi").table("observations").sum("humidity"),
"sum_temp", r.db("telemetry_pi").table("observations").sum("temp"),
"avg_humidity", r.db("telemetry_pi").table("observations").avg("humidity"),
"avg_temp", r.db("telemetry_pi").table("observations").avg("temp"),
"sumsq_humidity", r.db('telemetry_pi').table("observations")("humidity").map(function(d){ return d.mul(d); }).sum(),
"sumsq_temp", r.db('telemetry_pi').table("observations")("temp").map(function(d){ return d.mul(d); }).sum(),
"sum_products_temp_humidity",
r.map(
r.db("telemetry_pi").table("observations")("temp"),
r.db("telemetry_pi").table("observations")("humidity"),
function(temp, humidity){
return temp.mul(humidity);
}).sum(),
"n", r.db("telemetry_pi").table("observations").count()
).do(function(doc){
//Calculate the squared averages
return doc.merge({
beta_hat: doc("n").mul(doc("sum_products_temp_humidity")).sub((doc("sum_humidity").mul(doc("sum_temp")))).div(
//=========================================================================================
doc("n").mul(doc("sumsq_temp")).sub((doc("sum_temp").mul(doc("sum_temp"))))
),
"sd_humidity": r.db('telemetry_pi')
.table("observations")
("humidity")
.map(function(d){
var x = d.sub(doc("avg_humidity"));
return x.mul(x);
}).avg().do(r.js('(function(x) { return Math.sqrt(x); })')),
"sd_temperature": r.db('telemetry_pi')
.table("observations")
("temp")
.map(function(d){
var x = d.sub(doc("avg_temp"));
return x.mul(x);
}).avg().do(r.js('(function(x) { return Math.sqrt(x); })')),
"temp_humidity_correlation": doc("sum_products_temp_humidity").div(
doc("sumsq_temp").mul(doc("sumsq_humidity")).do(r.js('(function(x) { return Math.sqrt(x); })'))
)
});
}).do(function(doc){
return doc.merge({
alpha_hat: (r.expr(1).div(doc("n")).mul(doc("sum_humidity"))).sub(doc("beta_hat").mul(r.expr(1).div(doc("n"))).mul(doc("sum_temp")))
});
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.