File: NoFeaturesModel.R

package info (click to toggle)
r-cran-mlr 2.19.2%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 8,264 kB
  • sloc: ansic: 65; sh: 13; makefile: 5
file content (31 lines) | stat: -rw-r--r-- 977 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
makeNoFeaturesModel = function(targets, task.desc) {
  setClasses(list(targets = targets, task.desc = task.desc),
    "NoFeaturesModel")
}


predictNofeatures = function(model, newdata) {
  y = getLearnerModel(model)$targets
  type = model$learner$type
  # for regression return constant mean
  if (type == "regr") {
    return(rep(mean(y), nrow(newdata)))
  }
  if (type == "classif") {
    tab = prop.table(table(y))
    probs = as.numeric(tab)
    if (model$learner$predict.type == "response") {
      return(sample(as.factor(names(tab)), nrow(newdata), prob = probs, replace = TRUE))
    }
    probs = t(replicate(nrow(newdata), probs))
    colnames(probs) = names(tab)
    return(probs)
  }
  if (type == "surv") {
    if (model$learner$predict.type == "response") {
      return(runif(nrow(newdata)))
    }
    # FIXME: probs / brier for survival should use something like median survival time
  }
  stopf("NoFeaturesModel for learner type '%s' not implemented", type)
}