Using causalweight package in R

Viewed 208

I am using a r package named 'causal weight', more specifically 'duntreatDML'

The example is available here( https://github.com/cran/causalweight/blob/master/R/JC.R)

I am using a different dataset but using the same package:

a demo version of my data:

id <- c('1', '2', '3')
ac <- c(23, 41, 32)
ff <- c(1,0,1)
ac1 <- c(34, 21, 11)
air <- c(10,20,20)
d1 <- c(1,0,1)
d2 <- c(1,1,0)
y2 <- c(100,200,300)

mydata <- data.frame(id, ac,ff,ac1,air,d1,d2,y2)
x0 = mydata[,3:4];x1 = mydata[,5:6]

d1 = mydata[,7];d2 = mydata[,8]

y2=mydata[,9]

output=dyntreatDML(y2=y2,d1=d1,d2=d2,x0=x0,x1=x1,
                   d1treat=1,d2treat=1,d1control=0,d2control=0)

I am getting the following error:

Error in split.default(sample(1:N), rep(1:V, length = N)) : 
  group length is 0 but data length > 0

I can't understand the meaning of the above error, so that I can fix it.

my actual dataset : https://github.com/juisen/dyntreat/blob/main/for_dyn.csv                                                               
                                                                                                                                             
mydata4<- read.csv(file = "D:/New folder (2)/for_dyn.csv")
head(mydata4)

x0 = mydata4[,5:8];x1 = mydata4[,11:13]

d1 = mydata4[,19];d2 = mydata4[,20]

y2=mydata4[,14]



output4 =dyntreatDML(
  y2=y2,
  d1=d1,
  d2=d1,
  x0=x0,
  x1=x1,
  fewsplits = TRUE,
  normalized = TRUE
)
cat("dynamic ATE: ",round(c(output4$effect),3),", standard error: ",
    round(c(output4$se),3), ", p-value: ",round(c(output4$pval),3))                                                ```


1 Answers

Edit

Thank you for uploading your actual dataset - it made troubleshooting much easier. I believe the issue stems from the "d1" and "d2" variables. In your dataset every observation for these two variables equals zero. If you create fake data for these two variables (i.e. zeros and ones) the run finishes without error on my system (please let me know if you still get errors).

Another potential issue is the column named "" - I'm not sure how this variable is counted (first/last) as it doesn't have a name, so your selected variables may be off by one. To stop this from being a potential problem, rename that variable to "rownames" or something:

library(causalweight)
library(naniar)

# Load the data
mydata4 <- read.csv("for_dyn.csv", check.names = FALSE)

# Rename the potentially-problematic variable `""`
colnames(mydata4)[1] <- "rownumber"

# Check for missing values
naniar::any_miss(mydata4)

# Select your variables
x0 = mydata4[,4:7]
x1 = mydata4[,10:12]

# Create fake data (either "0" or "1") for these two variables
d1 = sample(x = c(0,1), size = nrow(mydata4), replace = TRUE)
d2 = sample(x = c(0,1), size = nrow(mydata4), replace = TRUE)

y2 = mydata4[,13]

# Run the function
output=dyntreatDML(y2=y2, d1=d1, d2=d2, x0=x0, x1=x1, fewsplits = TRUE, normalized = TRUE)
cat("dynamic ATE: ",round(c(output$effect),3),", standard error: ",
    round(c(output$se),3), ", p-value: ",round(c(output$pval),3))

If that completes without error, the solution is to select variables from your actual dataset with discrete, binomial outcomes as your d1/d2 values to enable the split into training/testing partitions.

--

Original comments

To clarify: do you get the "Error in split.default" error when you run the code from the vignette?

E.g.

#install.packages("causalweight")
library(causalweight)
data(JC)

x0=JC[,2:29]
x1=JC[,30:36]
d1=JC[,37]
d2=JC[,38]
y2=JC[,44]
output=dyntreatDML(y2=y2, d1=d1, d2=d2, x0=x0, x1=x1)
cat("dynamic ATE: ",round(c(output$effect),3),", standard error: ",
    round(c(output$se),3), ", p-value: ",round(c(output$pval),3))
output$ntrimmed

Are you able to post your dataset to help us troubleshoot? Or are you able to make a minimal reproducible example to enable us to replicate the error?

Looking at the source code, there's a chance the issue is with the call to the hddyntreat function on line 75. This function uses a crossfitting procedure to split your data into training/testing partitions. If you don't have enough samples in your dataset, or your dataset is unbalanced, this might result in the error you are seeing. I can't be sure without being able to reproduce the issue, but my guess would be that your dataset doesn't fit the 'criteria' the function requires.

Related