Skip to content

Instantly share code, notes, and snippets.

@paulrougieux
Last active August 29, 2015 14:16
Show Gist options
  • Save paulrougieux/30e115503f7196b441d8 to your computer and use it in GitHub Desktop.
Save paulrougieux/30e115503f7196b441d8 to your computer and use it in GitHub Desktop.
Create and execute a STATA do file passing some variables as mustache tags
library(dplyr)
# Estimate a panel data model using plm and STATA
# Transmit some variables to STATA using mustache tags
# Write panel data to csv for use in STATA
write.csv(Grunfeld, path, item_, ".csv", names = FALSE, na = ".")
#' Create and execute a STATA do file
#'
#' variables workingdirectory, filename and
#' allitems are currently used as mustache tag variables.
#' More information on the Mustache format:
#' https://github.com/mustache/mustache
#' See source near STATAcommand to see how the replacement is done.
#' @param dtf a data frame containing at leas an item column
#' @param STATAcommand the stata command with optional mustache
#' variables included
#' @param filename the name of the file without extension
#' @param sleep waiting time after calling stata and before reading the csv result file
#' @examples
#'\dontrun{
#'
#' # Prepare data
#' fp <- loadbalancedpanel2forstata(variable = c("consumption",
#' "gdpconstantusd",
#' "price"))
#' writebalancedpanel2csv(fp)
#'
#' # Stata command
#' STATAcommand <- '
#' cd {{workingdirectory}}
#' file open resultfile using {{filename}}.csv, write replace
#' file write resultfile "item,pesaran,pvalue" _n
#' foreach itemfile in {{allitems}}{
#' clear
#' insheet using "`itemfile\'.csv"
#' xtset country year
#' eststo `itemfile\' : quietly xtreg lcons lgdprusd lprice, fe
#' xtcsd, pesaran
#' file write resultfile (item) "," (r(pesaran)) "," (2*(1-normal(abs(r(pesaran))))) _n
#' }
#' file close resultfile'
#'
#' # Execute the STATA command
#' fp %>% stata(STATAcommand, "pesarantest")
#' fp %>% filter(item == "TotalPaperandPaperboard") %>%
#' stata(STATAcommand, "pesarantest")
#'
#' # View the STATA log file
#' cat(system2("less","data-end/STATA/pesarantest.log", stdout=TRUE), sep="\n")
#'}
#' @export
stata <- function(dtf, STATAcommand, filename, sleep = 0.5){
workingdirectory <- file.path(getwd(),"data-end/STATA/")
allitems <- paste(unique(dtf$item), collapse = " ")
### Create a STATA do file
dofile <- file(paste0("data-end/STATA/", filename, ".do"), "w")
STATAcommand %>%
gsub("\\{\\{workingdirectory\\}\\}", workingdirectory, .) %>%
gsub("\\{\\{filename\\}\\}", filename, .) %>%
gsub("\\{\\{allitems\\}\\}", allitems, .) %>%
cat(file = dofile)
close(dofile)
### Execute the STATA do file
basedir <- getwd()
setwd("data-end/STATA/")
tryCatch(system2("/usr/local/stata/stata",
c("-b", "do", paste0(filename,".do", "&")),
wait = TRUE),
finally = setwd(basedir))
# Even with system(wait = TRUE) the file is not created
# before read.csv tries to read it
# message("Wait 0.5 seconds for the csv file to be written")
Sys.sleep(sleep)
result <- read.csv(paste0("data-end/STATA/",
filename,".csv"))
return(result)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment