Using Rcall to Call the functions from R Package fdid in STATA
Jan. 25, 2026
STATA users can use Rcall to run the functions from our R package fdid in STATA.
-
Prerequisites:
- Install the basic R into your machine.
install.packages("devtools") devtools::install_github("ccfang2/fdid") - Install the STATA package Rcall.
- To check if you have successfully installed Rcall, you can type
help rcallin STATA, and try out the small examples it has in the help file.
- To check if you have successfully installed Rcall, you can type
In rare cases, when you encounter issues in installation, feel free to paste the errors into an AI chatbot (e.g., ChatGPT, Gemini) to see what it suggests. It often helps a lot.
- Install the basic R into your machine.
-
Once you have R, the R package fdid and Rcall successfully installed, you can run the whole example on the [Home] page in STATA via two different methods.
- Use
rcall script:- First, copy and paste the following code to create an R script file
YourRScript.R, and save it to your working directory of your STATA.
library(fdid) data(Gdata) Gdata$beta[,"event_t"] <- Gdata$beta[,"event_t"]- Gdata$t0 #Recenter the event time on 0 # Classical Event Study Plot fdid_scb_est <- fdid_scb(beta=Gdata$beta, cov=Gdata$cov, t0=0) pdf("Classical_EventStudyPlot.pdf", width = 8, height = 6) par(cex.axis = 1.4, cex.lab = 1.4, cex.main = 1.4, family="Times") EventStudyPlot_Classical(fdid_scb_est, pos.legend="bottom", scale.legend=1.4) dev.off() # Simultaneous Confidence Bands pdf("SCB.pdf", width = 8, height = 6) par(cex.axis = 1.4, cex.lab = 1.4, cex.main = 1.4, family="Times") plot(fdid_scb_est, pos.legend="bottom", scale.legend=1.4, note.pre=FALSE, ci.post=TRUE) dev.off() # Honest Reference Band under Violation of No-anticipation Assumption pdf("SCB_NTA.pdf", width = 8, height = 6) par(cex.axis = 1.4, cex.lab = 1.4, cex.main = 1.4, family="Times") plot(fdid_scb_est, ta.ts=-2, ta.s=c(1.4,2.3), pos.legend="bottom", scale.legend=1.4, ci.post=TRUE, ref.band.pre = TRUE) dev.off() # Honest Reference Band under Violation of Parallel Trends Assumption pdf("SCB_PTA.pdf", width = 8, height = 6) par(cex.axis = 1.4, cex.lab = 1.4, cex.main = 1.4, family="Times") plot(fdid_scb_est, frmtr.m=c(0.25,0.25), pos.legend="bottom", scale.legend=1.4, ci.post=TRUE, ref.band.pre = TRUE) dev.off()- Second, run
rcall script YourRScript.Rin your STATA. - Last, you will see the plots saved in your working directory. You can change the size of your plots by playing with the arguments in
pdf().
- First, copy and paste the following code to create an R script file
- Use
rcall: end:- First, copy and paste the following code to command line in STATA.
rcall: library(fdid) data(Gdata) Gdata\$beta[, "event_t"] <- Gdata\$beta[, "event_t"] - Gdata\$t0 fdid_scb_est <- fdid_scb(beta = Gdata\$beta, cov = Gdata\$cov, t0 = 0) par(cex.axis = 1.4, cex.lab = 1.4, cex.main = 1.4, family = "Times") EventStudyPlot_Classical(fdid_scb_est, pos.legend = "bottom",scale.legend = 1.4) end- Second, you will see teh event study plot saved in your working directory.
- Third, you could generate other plots by changing the plot function above.
Remark:
rcallonly allows us to call R functions in STATA, so you cannot generate the plots in an editable graphic window like those generated by the original STATA functions. - Use
References
- Haghish, E. F. (2019). Seamless interactive language interfacing between R and Stata. The Stata Journal, 19(1), 61-82.