Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

R session quits when the number of strata < 10 #8

Open
jialul opened this issue Mar 9, 2023 · 0 comments
Open

R session quits when the number of strata < 10 #8

jialul opened this issue Mar 9, 2023 · 0 comments

Comments

@jialul
Copy link

jialul commented Mar 9, 2023

I am using wfe version 1.9.1 and found that the current version of the package does not allow the number of strata to be less than 10 while also printing the helping message ("verbose = TRUE"). With N < 10 and verbose = TRUE, the R session will automatically exit. But if we do not print the helping message ("verbose = FALSE"), the package works fine. To reproduce the error, I use the original sample code from the package and changed the number of distinct units from 10 to 7. All the other parts stay the same as the original sample code.

My hunch is that the sudden exit of R session is cased by these few lines in C which relates to "verbose" but I could be wrong.

wfe/src/wfe.c

Lines 1060 to 1067 in d7c0b76

if(*verbose && *len_t_index>10) {
int percent = *len_u_index/10;
if (i % percent == 0){
/* Rprintf("- %d th year calculated:\n", (j+1)); */
Rprintf(".");
R_FlushConsole();
}
}

N <- 7 # number of distinct units 
Time <- 20 # number of distinct time
beta <- 1
treat <- matrix(rbinom(N*Time, size = 1, 0.25), ncol = N)
while ((sum(apply(treat, 2, mean) == 0) > 0) | (sum(apply(treat, 2, mean) == 1) > 0) |
       (sum(apply(treat, 1, mean) == 0) > 0) | (sum(apply(treat, 1, mean) == 1) > 0)) {
  treat <- matrix(rbinom(N*Time, size = 1, 0.25), ncol = N)
}
treat.vec <- c(treat)
alphai <- rnorm(N, mean = apply(treat, 2, mean))
x1 <- matrix(rnorm(N*Time, 0.5,1), ncol=N)
x2 <- matrix(rbeta(N*Time, 5,1), ncol=N)
x1.vec <- c(x1)
x2.vec <- c(x2)
y <- matrix(NA, ncol = N, nrow = Time)
for (i in 1:N) {
    y[, i] <- alphai[i] + treat[, i] + x1[,i] + x2[,i] + rnorm(Time)
}
y.vec <- c(y)
unit.index <- rep(1:N, each = Time)
time.index <- rep(1:Time, N)
Data.str <- as.data.frame(cbind(y.vec, treat.vec, unit.index, x1.vec, x2.vec))
colnames(Data.str) <- c("y", "tr", "strata.id", "x1", "x2")
Data.obs <- as.data.frame(cbind(y.vec, treat.vec, unit.index, time.index, x1.vec, x2.vec))
colnames(Data.obs) <- c("y", "tr", "unit", "time", "x1", "x2")

library(wfe)
mod.ate <- wfe(y~ tr+x1+x2, data = Data.str, treat = "tr",
               unit.index = "strata.id", method = "unit",
               qoi = "ate", hetero.se=TRUE, auto.se=TRUE)
summary(mod.ate)

@jialul jialul changed the title R session quits when N < 10 R session quits when the number of strata < 10 Mar 9, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant