rename pkg --> aggexp
[aggexp.git] / aggexp / R / m_MLPoly.R
diff --git a/aggexp/R/m_MLPoly.R b/aggexp/R/m_MLPoly.R
new file mode 100644 (file)
index 0000000..a19a2c9
--- /dev/null
@@ -0,0 +1,51 @@
+#' @include b_LinearAlgorithm.R
+
+#' @title MLpoly Algorithm
+#'
+#' @description MLpoly Algorithm.
+#' Inherits \code{\link{LinearAlgorithm}}
+#'
+#' @field alpha Importance of weights redistribution, in [0,1]. Default: 0
+#' @field grad Whether to use or not the (sub)gradient trick. Default: FALSE
+#'
+MLpoly = setRefClass(
+       Class = "MLpoly",
+
+       fields = c(
+               alpha = "numeric",
+               grad = "logical"
+       ),
+
+       contains = "LinearAlgorithm",
+
+       methods = list(
+               initialize = function(...)
+               {
+                       callSuper(...)
+                       if (length(alpha) == 0 || alpha < 0. || alpha > 1.)
+                               alpha <<- 0. #no redistribution
+                       if (length(grad) == 0)
+                               grad <<- FALSE
+               },
+               predict_noNA = function(XY, x)
+               {
+                       K = ncol(XY) - 1
+                       if (K == 1)
+                       {
+                               #shortcut: nothing to combine
+                               finalWeight = 1.
+                       }
+
+                       else
+                       {
+                               X = XY[,names(XY) != "Measure"]
+                               Y = XY[,"Measure"]
+                               finalWeight = .C("ml_predict_noNA", X = as.double(t(X)), Y = as.double(Y), n = as.integer(nrow(XY)), 
+                                       K = as.integer(K), alpha=as.double(alpha), grad = as.integer(grad), weight=double(K))$weight
+                       }
+
+                       appendWeight(finalWeight)
+                       return (matricize(x) %*% finalWeight)
+               }
+       )
+)