BARTpred_CV: A helper function. Allows you to fit observed joint...

View source: R/mono_bart.R

BARTpred_CVR Documentation

A helper function. Allows you to fit observed joint probabilities with BART, or with a monotonicity constraint

Description

A helper function. Allows you to fit observed joint probabilities with BART, or with a monotonicity constraint

Usage

BARTpred_CV(
  df,
  treat = "G",
  Outcome = "B",
  vars,
  mono = T,
  nd_post = 20,
  n_skip = 20,
  nfold = 5
)

Arguments

df

dataframe including the covariates plus the treatment and outcome columns

treat

String of name of treatment column

Outcome

string, name of outcome column

vars

list of names of columns of covariates

mono

Boolean, whether or not to use monotonicity constraint

nd_post

number of posterior draws to keep

n_skip

number of burn in samples

nfold

how many cross validation folds you want, integer value

Examples

N <- 500 # Number of random samples
a=1

x1=runif(N, -a,a)
x2=runif(N, -a,a)
x3=runif(N,-a,a)
x4=runif(N,-a,a)
x5=runif(N, -a,a)
beta1= -0.2
alpha1= 0.7
beta0= -0
alpha0= -0.5
mu1 <- beta0+beta1*(x1+x2+x3+x4+x5)
mu2 <- alpha0+alpha1*(x1+x2+x3+x4+x5)
mu<-matrix(c(mu1, mu2), nrow=N, ncol=2)
rho=.5
gamma=1
B1.true=pnorm(mu2+gamma)
B0.true=pnorm(mu2)
sigma <- matrix(c(1, rho,rho,1),
               2) # Covariance matrix
sim_data=t(sapply(1:N, function(i)MASS::mvrnorm(1, mu = mu[i,], Sigma = sigma )))
#generate the binary treatments
G=sapply(1:N, function(i)ifelse(sim_data[i,1]>=0, 1,0))
#generate the binary outcomes
B=sapply(1:N, function(i)ifelse(sim_data[i,2]>=-1*gamma*G[i], 1,0))
print(table(G,B))
covariates=data.frame(x1,x2,x3,x4,x5,B, G)
vars=c('x1','x2','x3','x4','x5')
CV_pred=BARTpred_CV(covariates, treat='G', Outcome='B', vars=vars,mono=T,
 nd_post=200, n_skip=500, nfold=5)
output=do.call('rbind', CV_pred$imp_frame)
cc=ggrocs(list(PB1G0=pROC::roc(output$B[output$G==0],
                   output$BG0[output$G==0]),
              PB1G1=pROC::roc(output$B[output$G==1],
                        output$BG1[output$G==1]),
              PG1=pROC::roc(output$G[],
                        output$G1)),
         breaks = seq(0,1,0.1),
         tittle = "ROC perf. predicting our probs")
#pick a function to integrate over
#here the standard deviation is chosen to match the generated data
f=function(u){
  dnorm(u, mean=0, sd=sqrt(rho/(1-rho)))
}
treat_frame=integrate_function(as.matrix(output), constraint=T, f=f, n_cores=1)

demetrios1/Causallysensitive documentation built on Nov. 18, 2022, 5:27 p.m.