/
tmle3_Fit.R
149 lines (145 loc) Β· 4 KB
/
tmle3_Fit.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
#' TMLE fit object
#'
#' A tmle_fit object, containing initial and updated estimates, as well as data
#' about the fitting procedure. TMLE updates are calculated when the object is
#' constructed.
#'
#' @importFrom R6 R6Class
#' @importFrom uuid UUIDgenerate
#' @importFrom methods is
#'
#' @family Parameters
#'
#' @keywords data
#'
#' @return \code{Param_base} object
#'
#' @format \code{\link{R6Class}} object.
#'
#' @template tmle3_Fit_extra
#'
#' @export
#
tmle3_Fit <- R6Class(
classname = "tmle3_Fit",
public = list(
initialize = function(tmle_task, likelihood, tmle_params, updater, ...) {
if (inherits(tmle_params, "Param_base")) {
tmle_params <- list(tmle_params)
}
private$.tmle_task <- tmle_task
private$.likelihood <- likelihood
private$.tmle_params <- tmle_params
updater$tmle_params <- tmle_params
private$.updater <- updater
initial_psi <- sapply(
self$tmle_params,
function(tmle_param) {
tmle_param$estimates(self$tmle_task)$psi
}
)
private$.initial_psi <- unlist(initial_psi)
private$.tmle_fit(max_it)
},
print = function() {
cat(sprintf("A tmle3_Fit that took %s step(s)\n", self$steps))
print(self$summary)
},
set_timings = function(start_time, task_time, likelihood_time, params_time,
fit_time) {
timings <- list(
make_tmle_task = task_time - start_time,
fit_likelihood = likelihood_time - task_time,
define_params = params_time - likelihood_time,
tmle_update = fit_time - params_time
)
private$.timings <- do.call(rbind, timings)
}
),
active = list(
tmle_task = function() {
return(private$.tmle_task)
},
likelihood = function() {
return(private$.likelihood)
},
tmle_params = function() {
return(private$.tmle_params)
},
tmle_param_names = function() {
if (is.null(private$.tmle_param_names)) {
private$.tmle_param_names <- unlist(sapply(self$tmle_params, `[[`, "name"))
}
return(private$.tmle_param_names)
},
tmle_param_types = function() {
if (is.null(private$.tmle_param_types)) {
private$.tmle_param_types <- sapply(self$tmle_params, `[[`, "type")
}
return(private$.tmle_param_types)
},
updater = function() {
return(private$.updater)
},
steps = function() {
return(self$updater$step_number)
},
ED = function() {
ED <- private$.ED
# names(ED) <- self$tmle_param_names
return(ED)
},
initial_psi = function() {
initial_psi <- private$.initial_psi
# names(initial_psi) <- self$tmle_param_names
return(initial_psi)
},
estimates = function() {
estimates <- private$.estimates
# names(estimates) <- self$tmle_param_names
return(estimates)
},
summary = function() {
return(summary_from_estimates(
task = self$tmle_task, estimates = self$estimates,
param_names = self$tmle_param_names,
param_types = self$tmle_param_types,
init_psi = self$initial_psi
))
},
timings = function() {
return(private$.timings)
}
),
private = list(
.tmle_task = NULL,
.likelihood = NULL,
.tmle_params = NULL,
.tmle_param_names = NULL,
.tmle_param_types = NULL,
.updater = NULL,
.steps = NULL,
.ED = NULL,
.initial_psi = NULL,
.estimates = NULL,
.timings = NULL,
.tmle_fit = function(max_it = 100) {
self$updater$update(self$likelihood, self$tmle_task)
private$.steps <- self$updater$steps
estimates <- lapply(
self$tmle_params,
function(tmle_param) {
tmle_param$estimates(self$tmle_task, self$updater$update_fold)
}
)
private$.estimates <- estimates
private$.ED <- ED_from_estimates(estimates)
}
)
)
#' @param ... Passes all arguments to the constructor. See documentation for the
#' Constructor.
#' @rdname tmle3_Fit
#' @export
#
fit_tmle3 <- tmle3_Fit$new