R/fitNAcounter.R

Defines functions fitNAcounter

fitNAcounter <- function(TD, par0 = NULL, TDout = NULL) {

  n <- nrow(TD)
  iin <- sample(1:n, floor(n * 0.6))

  l <- fitXGB(TD[iin], par0 = list( eval_metric = "mae",
                                    type = "XGBREG"),
              TDout = TD[-iin]
  )

  if(is.null(par0$nfeats)){ nfeats <- 20 }

  fts <- l$feature_imp[1:nfeats, Feature]

  TD[-iin, n_NA := rowSums(is.na(.SD)), .SDcols = fts]

  pred0 <- l$predf(l, TD[-iin])


  optf <- function(par1) {
    pred1 <- ifelse(TD[-iin, n_NA] < par1[1], pred0 * par1[2], pred0)
    mean(abs( TD[-iin, logerror] - pred1 ))
  }

  opt0 <- optim(par = c(nfeats/4, .5), fn = optf)

  l$fts <- fts
  l$NApar <- opt0$par

  l

}
steinarv/k1 documentation built on Oct. 19, 2017, 4:41 a.m.