-
Notifications
You must be signed in to change notification settings - Fork 1
/
ts_ipw_function_v3.R
71 lines (66 loc) · 2.31 KB
/
ts_ipw_function_v3.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
library(fst)
library(data.table)
library(parallel)
library(stringr)
library(dplyr)
library(timeDate)
library("ranger")
require("SuperLearner")
require("earth")
require("gam")
require("ranger")
require("rpart")
library("ggplot2")
library("grid")
library("gridExtra")
delta.seq <- exp(seq(-2.3, 2.3, length.out = 50))
sl.lib = c("SL.earth", "SL.gam", "SL.glm", "SL.glm.interaction", "SL.mean", "SL.ranger", "SL.rpart")
ts_ipsi.ipw <- function(dat, x.trt, t_lag = 1, delta.seq, nsplits = 1, model = "glm"){
# setup storage
ntimes <- length(table(dat$time))
n <- length(unique(dat$id))
k <- length(delta.seq)
if (model == "glm") {
trtmod <- glm(a ~ ., data=cbind(x.trt, a=dat$a), family=binomial())
dat$ps <- predict(trtmod, data=x.trt, type = "response")
} else if (model == "ranger") {
# fit treatment model
trtmod <- ranger(a ~ ., data=cbind(x.trt, a = dat$a))
dat$ps <- predict(trtmod, data = x.trt)$predictions
} else if (model == "SL") {
trtmod <- SuperLearner(dat$a, x.trt,
newX = x.trt,
SL.library = sl.lib,
family = binomial)
dat$ps <- trtmod$SL.predict
}
end = max(dat$time)
est.eff.ipw <- matrix(NA, nrow = n*(end-t_lag), ncol = k)
for (j in 1:k){
for (n_i in 1:n){
for (t in 1:(ntimes-t_lag)){
est.eff.ipw[(n_i-1)*(ntimes-t_lag) + t,j] = prod(sapply(t:(t+t_lag),function(s){
delta_t = delta.seq[j]
(dat$a[(n_i-1)*(ntimes-t_lag)+s]*delta_t +
(1-dat$a[(n_i-1)*(ntimes-t_lag)+s])) /
(delta_t*dat$ps[(n_i-1)*(ntimes-t_lag)+s]+1-dat$ps[(n_i-1)*(ntimes-t_lag)+s])
}))*dat$y[(n_i-1)*(ntimes-t_lag) + t + t_lag]
}
}
}
est.var1.ipw <- matrix(NA, nrow = n*(end-t_lag), ncol = k)
for (j in 1:k){
for (n_i in 1:n){
for (t in 1:(ntimes-t_lag)){
est.var1.ipw[(n_i-1)*(ntimes-t_lag) + t, j] = prod(sapply(t:(t+t_lag), function(s){
delta_t = delta.seq[j]
(dat$a[(n_i-1)*(ntimes-t_lag)+s]*delta_t^2 +
(1-dat$a[(n_i-1)*(ntimes-t_lag)+s])) /
(delta_t*dat$ps[(n_i-1)*(ntimes-t_lag)+s]+1-dat$ps[(n_i-1)*(ntimes-t_lag)+s])^2
}))*dat$y[(n_i-1)*(ntimes-t_lag) + t + t_lag]^2
}
}
}
return(list(est.eff = est.eff.ipw,
est.var1.ipw = est.var1.ipw))
}