-
Notifications
You must be signed in to change notification settings - Fork 13
/
Likelihood.R
197 lines (181 loc) Β· 6.2 KB
/
Likelihood.R
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
#' Class for Likelihood
#'
#' This object represents an estimate of the relevant factors of the likelihood estimated from data, or based on \emph{a priori} knowledge where appropriate.
#' That is, it represents some subset of $P_n$. This object inherits from \code{\link[sl3]{Lrnr_base}}, and so shares some properties with \code{sl3} learners.
#' Specifically, to fit a likelihood object to data, one calls \code{likelihood$train(tmle3_task)}.
#' Each likelihood factor is represented by an object inheriting from \code{\link{LF_base}}.
#'
#' @docType class
#'
#' @importFrom R6 R6Class
#' @importFrom sl3 Lrnr_base
#' @importFrom assertthat assert_that is.count is.flag
#' @importFrom delayed bundle_delayed
#' @import data.table
#' @family Likelihood objects
#' @export
#'
#' @keywords data
#'
#' @return \code{Likelihood} object
#'
#' @format \code{\link{R6Class}} object.
#'
#' @template Likelihood_extra
#'
#' @export
Likelihood <- R6Class(
classname = "Likelihood",
portable = TRUE,
class = TRUE,
inherit = Lrnr_base,
public = list(
initialize = function(factor_list, cache = NULL, ...) {
params <- args_to_list()
if (inherits(factor_list, "LF_base")) {
factor_list <- list(factor_list)
}
factor_names <- sapply(factor_list, `[[`, "name")
names(factor_list) <- factor_names
params$factor_list <- factor_list
if (is.null(cache)) {
cache <- Likelihood_cache$new()
}
private$.cache <- cache
super$initialize(params)
},
print = function() {
lapply(self$factor_list, print)
invisible(NULL)
},
validate_task = function(tmle_task) {
assert_that(is(tmle_task, "tmle3_Task"))
factor_list <- self$factor_list
factor_names <- names(factor_list)
task_nodes <- names(tmle_task$npsem)
if (!all(factor_names %in% task_nodes)) {
stop("factor_list and task$npsem must have matching names")
}
},
get_likelihood = function(tmle_task, node, fold_number = "full") {
likelihood_factor <- self$factor_list[[node]]
# first check for cached values for this task
likelihood_values <- self$cache$get_values(likelihood_factor, tmle_task, fold_number)
if (is.null(likelihood_values)) {
# if not, generate new ones
likelihood_values <- likelihood_factor$get_likelihood(tmle_task, fold_number)
self$cache$set_values(likelihood_factor, tmle_task, 0, fold_number, likelihood_values)
}
return(likelihood_values)
},
get_likelihoods = function(tmle_task, nodes = NULL, fold_number = "full") {
if (is.null(nodes)) {
nodes <- self$nodes
}
if (length(nodes) > 1) {
all_likelihoods <- lapply(nodes, function(node) {
self$get_likelihood(tmle_task, node, fold_number)
})
likelihood_dt <- as.data.table(all_likelihoods)
setnames(likelihood_dt, nodes)
return(likelihood_dt)
} else {
return(self$get_likelihood(tmle_task, nodes[[1]], fold_number))
}
},
get_possible_counterfactuals = function(nodes = NULL) {
# get factors for nodes
factor_list <- self$factor_list
if (!is.null(nodes)) {
factor_list <- factor_list[nodes]
}
all_levels <- lapply(factor_list, function(likelihood_factor) {
likelihood_factor$variable_type$levels
})
all_levels <- all_levels[!(sapply(all_levels, is.null))]
level_grid <- expand.grid(all_levels)
return(level_grid)
},
base_train = function(task, pretrain) {
self$validate_task(task)
fit_object <- private$.train(task, pretrain)
new_object <- self$clone() # copy parameters, and whatever else
new_object$set_train(fit_object, task)
return(new_object)
},
add_factors = function(factor_list) {
if (inherits(factor_list, "LF_base")) {
factor_list <- list(factor_list)
}
factor_names <- sapply(factor_list, `[[`, "name")
# train factors if necessary
factor_list <- lapply(factor_list, train_lf, self$training_task)
# add factors to list of factors
private$.params$factor_list[factor_names] <- factor_list
},
sample = function(tmle_task = NULL, sample_lib = NULL) {
# for now assume nodes are in order
# TODO: order nodes based on dependencies
if (is.NULL(sample_lib = NULL)) {
nodes <- names(self$factor_list)
sample_lib <- rep(list(NULL), length(nodes))
names(sample_lib) <- nodes
}
for (node in names(self$factor_list)) {
tmle_task <- factor_list$node$sample(tmle_task, sample_lib$node)
}
return(tmle_task)
}
),
active = list(
factor_list = function() {
return(self$params$factor_list)
},
nodes = function() {
return(names(self$factor_list))
},
cache = function() {
return(private$.cache)
},
censoring_nodes = function() {
return(private$.censoring_nodes)
}
),
private = list(
.train_sublearners = function(tmle_task) {
factor_fits <- lapply(self$factor_list, function(factor) factor$delayed_train(tmle_task))
result <- bundle_delayed(factor_fits)
return(result)
},
.train = function(tmle_task, factor_fits) {
factor_list <- self$factor_list
for (i in seq_along(factor_list)) {
factor_list[[i]]$train(tmle_task, factor_fits[[i]])
}
# TODO: mutating factor list of Lrnr_object instead of returning a fit
# which is not what sl3 Lrnrs usually do
censoring_nodes <- lapply(tmle_task$npsem, function(node) {
node$censoring_node$name
})
names(censoring_nodes) <- names(tmle_task$npsem)
private$.censoring_nodes <- censoring_nodes
return("trained")
},
.predict = function(tmle_task) {
stop("predict method doesn't work for Likelihood. See Likelihood$get_likelihoods for analogous method")
},
.chain = function(tmle_task) {
stop("chain method doesn't work for Likelihood. Currently, no analogous functionality")
},
.cache = NULL,
.censoring_nodes = NULL
)
)
#' @param ... Passes all arguments to the constructor. See documentation for the
#' Constructor below.
#'
#' @rdname Likelihood
#'
#' @export
#
make_Likelihood <- Likelihood$new