Implement an affine transformaton

affine_transform(source, linear_transform, offset, ..., env = parent.frame())

Arguments

source

relop source (or data.frame source)

linear_transform

matrix with row names taken from source column names (inputs), and column names are outputs.

offset

vector of offsets with names same as column names of linear_transform.

...

force later arguments to bind by name

env

environment to look for values in.

Value

relop node

Examples

if (requireNamespace("DBI", quietly = TRUE) && requireNamespace("RSQLite", quietly = TRUE)) { my_db <- DBI::dbConnect(RSQLite::SQLite(), ":memory:") d <- data.frame(AUC = 0.6, R2 = 0.2) source <- rq_copy_to(my_db, 'd', d, overwrite = TRUE, temporary = TRUE) linear_transform <- matrix(c(1 ,1, 2, -1, 1, 0, 0, 0), nrow = 2) rownames(linear_transform) <- c("AUC", "R2") colnames(linear_transform) <- c("res1", "res2", "res3", "res4") offset <- c(5, 7, 1, 0) names(offset) <- colnames(linear_transform) optree <- affine_transform(source, linear_transform, offset) cat(format(optree)) sql <- to_sql(optree, my_db) cat(sql) print(DBI::dbGetQuery(my_db, sql)) print(as.matrix(d) %*% linear_transform + offset) DBI::dbDisconnect(my_db) }
#> mk_td("d", c( #> "AUC", #> "R2")) %.>% #> affine_op(. %*% linear_transform + offset) #> SELECT #> `AUC`, #> `R2`, #> `AUC` * ( 1 ) + `R2` * ( 1 ) + 5 AS `res1`, #> `AUC` * ( 2 ) + `R2` * ( - ( 1 ) ) + 7 AS `res2`, #> `AUC` * ( 1 ) + 1 AS `res3`, #> 0 AS `res4` #> FROM ( #> SELECT #> `AUC`, #> `R2` #> FROM #> `d` #> ) tsql_69546204050014603667_0000000000 #> AUC R2 res1 res2 res3 res4 #> 1 0.6 0.2 5.8 8 1.6 0 #> res1 res2 res3 res4 #> [1,] 5.8 8 1.6 0