Skip to content

Commit 5f2a221

Browse files
committed
version 1.0 checked
1 parent 3e9bf56 commit 5f2a221

File tree

11 files changed

+121
-45
lines changed

11 files changed

+121
-45
lines changed

R/base.R

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -142,16 +142,19 @@ varimp <- function(X,
142142
#' @export
143143
#' @importFrom stats pnorm gaussian binomial
144144
#' @examples
145-
#' \dontrun{
145+
#'
146+
#' library(future)
147+
#' plan(multisession) # fit models in parallel
146148
#' data(metals, package="qgcomp")
147-
#' XYlist = list(X=metals[,c(1:22, 23)], Y=metals$y)
149+
#' XYlist = list(X=metals[,c(1:10, 15:23)], Y=metals$y)
148150
#' Y_learners = .default_continuous_learners_big()
149151
#' Xbinary_learners = .default_binary_learners_big()
150152
#' Xdensity_learners = .default_density_learners_big()[c(1:4,6:7)]
151153
#' vi <- varimp(X=XYlist$X,Y=XYlist$Y, delta=0.1, Y_learners = Y_learners,
152154
#' Xdensity_learners=Xdensity_learners, Xbinary_learners=Xbinary_learners,
153155
#' estimator="TMLE", updatetype="unweighted",estimand="diff")
154156
#' vi
157+
#' plan(transparent) # go back to standard evaluation
155158
#' vi1 <- varimp_refit(vi, X=XYlist$X,Y=XYlist$Y, delta=0.1,
156159
#' estimator="TMLE", updatetype="weighted", estimand="diff")
157160
#' vi1
@@ -165,15 +168,25 @@ varimp <- function(X,
165168
#' estimator="IPW")
166169
#' vi4
167170
#'
168-
#' hist(metals$y)
169-
#' hist(metals$calcium)
170-
#' hist(metals$total_hardness)
171+
#' # find the fit corresponding to calcium
171172
#' caidx <- which(names(XYlist$X)=="calcium")
172-
#' plot(metals$calcium, vi1$gfits[[4]]$predict()[[1]], pch=19, cex=0.2)
173-
#' plot(metals$calcium, metals$y, pch=19, cex=0.2)
174-
#' plot(metals$total_hardness, vi1$gfits[[21]]$predict()[[1]], pch=19, cex=0.2)
175-
#' plot(metals$total_hardness, metals$y, pch=19, cex=0.2)
176-
#' }
173+
#' thidx <- which(names(XYlist$X)=="total_hardness")
174+
#' # can confirm
175+
#' # vi1$gfits[[caidx]]$training_task$nodes$outcome
176+
#' calpredict = vi1$gfits[[caidx]]$predict()[[1]]
177+
#' thpredict = vi1$gfits[[thidx]]$predict()[[1]]
178+
#' # plot predicted density (not predicted value!) against original value,
179+
#' # compare with kernel density
180+
#' plot(metals$calcium, calpredict/max(calpredict), pch=19, cex=0.2,
181+
#' ylab="scaled conditional density")
182+
#' lines(density(metals$calcium))
183+
#' plot(metals$total_hardness, thpredict/max(thpredict), pch=19, cex=0.2,
184+
#' ylab="scaled conditional density")
185+
#' lines(density(metals$total_hardness))
186+
#' # note these are effectively measuring much of the same quantity
187+
#' plot(metals$calcium, metals$total_hardness)
188+
#' plot(calpredict, thpredict)
189+
#'
177190
varimp_refit <- function(vibr_fit,
178191
X,
179192
W = NULL,

R/base_identify.R

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#' @param vibr_fit a fit from varimp
1212
#' @param Acol (integer) which column of predictors in call to varimp to diagnose
1313
#' @param delta (numeric, default=0.01) change in each column of predictors in call to varimp corresponding to stochastic intervention
14+
#' @param ... not used
1415
#'
1516
#' @return ggplot2 plot object
1617
#' @export
@@ -30,7 +31,7 @@
3031
#' estimator="TMLE")
3132
#' plotshift_dens(vi_ipw, Acol=1, delta=0.01)
3233
#' }
33-
plotshift_dens <- function(vibr_fit, Acol=1, delta){
34+
plotshift_dens <- function(vibr_fit, Acol=1, delta, ...){
3435
if(is.null(delta)) delta <- vibr_fit$delta
3536
if(!is.null(vibr_fit$qfit)){
3637
task <- vibr_fit$qfit$training_task
@@ -64,8 +65,8 @@ plotshift_dens <- function(vibr_fit, Acol=1, delta){
6465

6566
p1 <-
6667
ggplot() + theme_classic() + scale_color_grey(name="") +
67-
geom_step(data=X1,aes(x=ord, y=dens, color="Observed"))+
68-
geom_step(data=X2,aes(x=ord, y=densshift, color="Shifted"))+
68+
geom_step(data=X1,aes_string(x="ord", y="dens", color='"Observed"'))+
69+
geom_step(data=X2,aes_string(x="ord", y="densshift", color='"Shifted"'))+
6970
scale_y_continuous(name=paste0("density(",xnm[Acol[1]],")"), expand = expansion(0))+
7071
scale_x_continuous(name="Sorted index", expand = expansion(.01))
7172
print(p1)
@@ -80,6 +81,7 @@ plotshift_dens <- function(vibr_fit, Acol=1, delta){
8081
#' @param vibr_fit a vibr_fit object from varimp
8182
#' @param Acol (integer) which column of predictors in call to varimp to diagnose (can only be continuous column of predictors in call to varimp)
8283
#' @param delta (numeric, default=0.01) change in each column of predictors in call to varimp corresponding to stochastic intervention
84+
#' @param ... not used
8385
#'
8486
#' @export
8587
#' @import ggplot2
@@ -110,7 +112,7 @@ plotshift_dens <- function(vibr_fit, Acol=1, delta){
110112
#' # or density estimation (not very many non-zero weights even with large
111113
#' # value of delta, or really large weights)
112114
#' }
113-
plotshift_wt <- function(vibr_fit, Acol=1, delta=0.01){
115+
plotshift_wt <- function(vibr_fit, Acol=1, delta=0.01, ...){
114116
if(is.null(delta)) delta <- vibr_fit$delta
115117
if(!is.null(vibr_fit$qfit)){
116118
task <- vibr_fit$qfit$training_task
@@ -120,8 +122,8 @@ plotshift_wt <- function(vibr_fit, Acol=1, delta=0.01){
120122
varnms <- c(task$nodes$outcome, task$nodes$covariates)
121123
}
122124
dat <- task$data
123-
ft <- vibr_fit$gfits[[Acol[1]]]
124125
X <- data.frame(dat)[,varnms,drop=FALSE]
126+
ft <- vibr_fit$gfits[[Acol[1]]]
125127
xnm = names(X)
126128
Xc <- .shift(X,Acol,shift = -delta)
127129
X$set="obs"
@@ -139,7 +141,7 @@ plotshift_wt <- function(vibr_fit, Acol=1, delta=0.01){
139141
X$wt = X$densshift/X$dens
140142
p1 <-
141143
ggplot(data = X) + theme_classic() + scale_color_grey() +
142-
geom_point(aes(x=dens, y=wt), pch=19, size=1, alpha=0.5)+
144+
geom_point(aes_string(x="dens", y="wt"), pch=19, size=1, alpha=0.5)+
143145
scale_x_continuous(name=paste0("density(observed ",xnm[Acol[1]],")"))+
144146
scale_y_continuous(name="density(shifted)/density(observed)")
145147
print(p1)
@@ -153,6 +155,7 @@ plotshift_wt <- function(vibr_fit, Acol=1, delta=0.01){
153155
#' @param Acol (integer) which column of predictors in call to varimp to diagnose
154156
#' @param Bcol (integer) second column of predictors in call to varimp to diagnose
155157
#' @param delta (numeric, default=0.01) change in each column of predictors in call to varimp corresponding to stochastic intervention
158+
#' @param ... not used
156159
#'
157160
#' @export
158161
#' @import ggplot2
@@ -177,7 +180,7 @@ plotshift_wt <- function(vibr_fit, Acol=1, delta=0.01){
177180
#' plotshift_scatter(vi_ipw, Acol=1, Bcol=2, delta=1)
178181
#' plotshift_scatter(vi_ipw, Acol=1, Bcol=5, delta=1)
179182
#' }
180-
plotshift_scatter <- function(vibr_fit, Acol, Bcol, delta=NULL, joint=FALSE){
183+
plotshift_scatter <- function(vibr_fit, Acol, Bcol, delta=NULL, ...){
181184
if(is.null(delta)) delta <- vibr_fit$delta
182185
if(!is.null(vibr_fit$qfit)){
183186
task <- vibr_fit$qfit$training_task
@@ -192,7 +195,7 @@ plotshift_scatter <- function(vibr_fit, Acol, Bcol, delta=NULL, joint=FALSE){
192195
requireNamespace("ggplot2")
193196
Xint <- data.frame(
194197
x1a = X[,Acol]+delta,
195-
x2a = X[,Bcol]+ifelse(joint, delta, 0)
198+
x2a = X[,Bcol]
196199
)
197200
X$col <- "Observed"
198201
Xint$col <- "Shifted"
@@ -214,10 +217,9 @@ plotshift_scatter <- function(vibr_fit, Acol, Bcol, delta=NULL, joint=FALSE){
214217
#'
215218
#' Give a numerical (but cruder) version of the diagnostics in plotshift_dens, where one can track the change in estimated exposure mass/density following a stochastic intervention on exposure.
216219
#' @param vibr_fit a fit from varimp
217-
#' @param X predictors from a varimp fit
218220
#' @param Acol (integer) which column of predictors in call to varimp to diagnose
219221
#' @param delta (numeric, default=0.01) change in each column of predictors in call to varimp corresponding to stochastic intervention
220-
#' @param quantiles (numeric vector, default=c(0, 0.1, 0.9, 1)) cutpoints in the closed interval [0,1] that correspond to quantiles of the estimated density of observed values of a predictor. The length of this vector determines the size of the table. Using values close to 0 or 1 allows one to track whether "intervened" predictors are pushed toward the extreme of the estimated predictor density, which could indicate lack of support for the scale of the implied intervention (e.g. delta is too big).
222+
#' @param quantiles (numeric vector, default=c(0, 0.1, 0.9, 1)) cutpoints in the closed interval \[0,1\] that correspond to quantiles of the estimated density of observed values of a predictor. The length of this vector determines the size of the table. Using values close to 0 or 1 allows one to track whether "intervened" predictors are pushed toward the extreme of the estimated predictor density, which could indicate lack of support for the scale of the implied intervention (e.g. delta is too big).
221223
#'
222224
#' @export
223225
#' @examples
@@ -245,6 +247,8 @@ dx_dens <- function(vibr_fit, Acol=1, delta=0.01, quantiles=c(0, 0.1, 0.9, 1)){
245247
task <- vibr_fit$gfits[[1]]$training_task
246248
varnms <- c(task$nodes$outcome, task$nodes$covariates)
247249
}
250+
dat <- task$data
251+
X <- data.frame(dat)[,varnms,drop=FALSE]
248252
ft <- vibr_fit$gfits[[Acol[1]]]
249253
if(is.null(delta)) delta = vibr_fit$delta
250254
xnm = names(X)
@@ -337,6 +341,8 @@ dx_dens <- function(vibr_fit, Acol=1, delta=0.01, quantiles=c(0, 0.1, 0.9, 1)){
337341
#' @param Xdensity_learners list of sl3 learners used to estimate the density of continuous predictors, conditional on all other predictors in X
338342
#' @param Xbinary_learners list of sl3 learners used to estimate the probability mass of continuous predictors, conditional on all other predictors in X
339343
#' @param verbose (logical) print extra information
344+
#' @param scale_continuous (logical) scale continuous variables in X to have standard deviation of 0.5
345+
#' @param threshold (numeric, default=10) threshold for high weights
340346
#' @param ... passed to sl3::make_sl3_Task (e.g. weights)
341347
#'
342348
#' @export

R/base_ipw.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@
149149
isbin=FALSE,
150150
...){
151151
obj = .prelims(X=X, Y=Y, V=V, whichcols=whichcols, delta=delta, Y_learners=NULL, Xbinary_learners, Xdensity_learners, verbose=verbose, isbin=isbin, ...)
152-
res = .trained_ipw(obj=obj,X=X,Y=Y,delta=delta,qfun=qfun,gfun=gfun,estimand=estimand,bounded=bounded,updatetype=updatetype)
152+
res = .trained_ipw(obj=obj,X=X,Y=Y,delta=delta,qfun=.qfunction,gfun=.gfunction,estimand=estimand,bounded=bounded,updatetype=NULL)
153153
res
154154
}
155155

R/base_tmle.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -291,7 +291,7 @@
291291
isbin=FALSE,
292292
...){
293293
obj = .prelims(X=X, Y=Y, V=V, whichcols=whichcols, delta=delta, Y_learners, Xbinary_learners, Xdensity_learners, verbose=verbose, isbin=isbin, ...)
294-
res = .trained_tmle(obj,X,Y,delta,qfun,gfun,estimand,bounded,updatetype)
294+
res = .trained_tmle(obj=obj,X=X, Y=Y,delta=delta,qfun=.qfunction,gfun=.gfunction,estimand=estimand,bounded=bounded,updatetype=updatetype)
295295
res
296296
}
297297

man/dx_dens.Rd

Lines changed: 1 addition & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/plotshift_dens.Rd

Lines changed: 3 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/plotshift_scatter.Rd

Lines changed: 3 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/plotshift_wt.Rd

Lines changed: 3 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/precheck_identification.Rd

Lines changed: 4 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/varimp_refit.Rd

Lines changed: 23 additions & 10 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)