Skip to content

Instantly share code, notes, and snippets.

@joosti
Last active August 29, 2015 14:08
Show Gist options
  • Save joosti/deca295635ce565fca98 to your computer and use it in GitHub Desktop.
Save joosti/deca295635ce565fca98 to your computer and use it in GitHub Desktop.
Linear regression: Example of a GLA that makes use of matrices
# include library
library(gtTranslator)
# define gla
gla <- MakeGLA(types = list(xType),
constants = list(size = xType$size),
representation = list(count = integer, XXT = translator::matrix(ncol = size, nrow = size), XY = xType),
prototype = function(count = 0, XXT = zeros(size, size), XY = zeros(size)){},
# Add item: get called for each tuple in independent processes
AddItem = function(x, y) {
count = count + 1L
XXT = XXT + x * x$t()
XY = XY + x * y
},
# Add state: aggregates results over the different processes
AddState = function(o) {
count = count + o$count
XXT = XXT + o$XXT
XY = XY + o$XY
},
# get results: executed once to obtain final result
GetResult = function(result = JSON) {
return(beta = XXT$i() * XY, count = count)
})
# read lineitem data (TPCH data)
data <- Read(lineitem10g)
# apply gla on data
agg <- gla(data, inputs = c(translator::MakeVector[](l_extendedprice, l_tax, l_discount), l_quantity))
# get result
result <- as.object(agg)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment