DeclareDesign Community

Lm_robust very slow when se_type = "CR2"

Hi all,
I’m running a power simulation for my experiment using lm_robust with se_type = “CR2” option. However, it takes much longer time compared to se_type = “CR0” option (about 5.4 seconds vs. 0.02 seconds with my Macbook Pro). Is this normal or something is wrong with the way I run the code? Is there any way to speed up the running time? (My R version is 4.0.2. and estimatr version is 0.22.0.)

MWE:

library(estimatr)
library(tidyverse) # for rdunif
MyDGP <- function(N,x,a,SD) {
  # time periods
  T <- 7
  # parameters
  b0 <- 0.1; b1 <- -x*SD; b2 <- -a*x*SD; b3 <- 0; sig <- SD
  # variables
  v <- rdunif(N,1,3); Ability <- rbinom(n = N*T,size = 15,prob = 0.5)
  e <- rnorm(N*T,0,SD); T2 <- rbinom(N*T,1,0.5)
  v <- rep(v,each=T)
  T1 <- rpois(N*T,lambda=(1+0.2*(v-1)))
  # ids
  id <- rep(1:N,each=T); tid <- rep(1:T,N)
  # latent outcome
  Latent <- b0 + b1*T1*T2 + b2*T1 + b3*T2 + 0.02*(Ability-7.5) + -0.02*(v-2) + e
  # observed outcome
  Y <- rep(0,N*T)
  Y[Latent>0] <- 1
  # data
  MyData <- as.data.frame(cbind(id,tid,Y,T1,T2,Ability))
  MyData <- MyData[order(id,tid),]
  MyData
}
MyData <- MyDGP(N=300,x=0.3,a=1,SD=0.1)
MyFormula <- Y~T1*T2+T1+T2+factor(Ability)
# this takes about 5.4 seconds
system.time(
mod <- lm_robust(formula=MyFormula,data = MyData,
                 clusters = id, fixed_effects = ~id, se_type = "CR2")
)
# this takes about 0.02 seconds
system.time(
  mod <- lm_robust(formula=MyFormula,data = MyData,
                   clusters = id, fixed_effects = ~id, se_type = "CR0")
)

Hi Yuki – no, there’s nothing wrong with your code. CR2 is great, but it’s a complicated estimation procedure that we don’t think we can seed up to CR0 levels. When we run simulations, we typically select something other than CR2 for speed. Thanks for your question!

1 Like