Skip to content

Commit

Permalink
#85: Updates /extensions/ and initializes README
Browse files Browse the repository at this point in the history
  • Loading branch information
snairdesai committed Dec 5, 2023
1 parent 4e763f7 commit d20f9bf
Show file tree
Hide file tree
Showing 5 changed files with 180 additions and 0 deletions.
7 changes: 7 additions & 0 deletions extensions/excel_tables/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# README

This directory includes code which can be used to convert `gentzkow/template` from running on Python to R or Stata. Make-scripts (e.g. `make.py` and `run_all.py`) are always in Python, but files called by these scripts can be written in other languages.

The structure of subdirectories (`/extensions/<LANGUAGE>`) parallels that of the overall repository. Files in each submodule directory (e.g. `/extensions/<LANGUAGE>/analysis`) should be copied to the main submodule directory (e.g. `/analysis`). Then executing make-scripts, for example with `python run_all.py`, will build outputs using `<LANGUAGE>` instead of the default Python.

See the main `README` for more information.
33 changes: 33 additions & 0 deletions extensions/excel_tables/analysis/code/gen_scalars.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
install.packages("openxlsx", repos = 'http://cran.us.r-project.org')
library(tidyverse)
library(openxlsx)
set.seed(123)

#### Scalars for gps_primary.xlsx ####

# This is the primary example, based on Table 1 from the paper:
# Pricing Power in Advertising Markets: Theory and Evidence (see link below, table on pg. 90):
# https://scholar.harvard.edu/files/shapiro/files/ad-price-drivers.pdf

#### Scalars for gs_widetable.xlsx ####

# Here, we are manually transcribing values from the paper here. Of course
# in practice, users should compute these scalars themselves and store
# the outputs in variables (in other ~/analysis/code scripts).
data <- t(data.frame(

c("-1.5556", "(0.2913)", "0.0973", "(0.0292)", "0.0124", "(0.0031)", "103", "809", "", "", "", ""),
c("-0.0285", "(0.0079)", "-0.4690", "(0.2599)", "0.1221", "(0.0306)", "0.0152", "(0.0034)", "103", "809", "", ""),
c("-1.6799", "(0.0607)", "0.0082", "(0.0044)", "0.0002", "(0.0004)", "103", "809", "", "", "", ""),
c("-0.0028", "(0.0024)", "-0.3056", "(0.0933)", "0.0418", "(0.0109)", "0.0057", "(0.0016)", "103", "809", "", ""),
c("-1.8388", "(0.1027)", "0.0198", "(0.0075)", "0.0102", "(0.0008)", "103", "809", "", "", "", ""),
c("-0.0020", "(0.0029)", "-0.5230", "(0.1228)", "0.0628", "(0.0125)", "0.0152", "(0.0018)", "103", "809", "", "")

))

# Convert the matrix into a dataframe.
vals_df <- as.data.frame(data, stringsAsFactors = FALSE)

# We write the data frame to a .csv and .xlsx file (this allows for git tracking).
write.csv(vals_df, "output/tables/gs_primary_scalars.csv", row.names = FALSE, sep = ";", quote = TRUE)
write.xlsx(vals_df, "output/tables/gs_primary_scalars.xlsx", sheetName = "Placeholders", row.names = FALSE, col.names = FALSE, quote = TRUE)
58 changes: 58 additions & 0 deletions extensions/excel_tables/analysis/make.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
###################
### ENVIRONMENT ###
###################
import os
import sys

### LOAD GSLAB MAKE
ROOT = '..'
gslm_path = os.path.join(ROOT, 'lib', 'gslab_make')

sys.path.append(gslm_path)
import gslab_make as gs

### PULL PATHS FROM CONFIG
PATHS = {
'root': ROOT,
'config': os.path.join(ROOT, 'config.yaml')
}
PATHS = gs.update_internal_paths(PATHS)

### LOAD CONFIG USER
PATHS = gs.update_external_paths(PATHS)
gs.update_executables(PATHS)

############
### MAKE ###
############

### START MAKE
gs.remove_dir(['input', 'external'])
gs.clear_dir(['output', 'log'])
gs.start_makelog(PATHS)

### PRODUCES ADDITIONAL SUBFOLDERS
os.makedirs('output/tables', exist_ok = True)

### MAKE LINKS TO INPUT AND EXTERNAL FILES
inputs = gs.link_inputs(PATHS, ['input.txt'])
externals = gs.link_externals(PATHS, ['external.txt'])
gs.write_source_logs(PATHS, inputs + externals)
gs.get_modified_sources(PATHS, inputs + externals)

## MAKE VERSION LOGS
gs.write_version_logs(PATHS)

### RUN SCRIPTS
gs.run_python(PATHS, program = 'code/analyze_data.py')
gs.run_r(PATHS, program = 'code/gen_scalars.R')
# gs.run_julia(PATHS, program = 'code/plot.jl')

### LOG OUTPUTS
gs.log_files_in_output(PATHS)

### CHECK FILE SIZES
gs.check_module_size(PATHS)

### END MAKE
gs.end_makelog(PATHS)
20 changes: 20 additions & 0 deletions extensions/excel_tables/paper_slides/input.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Each line of instruction in this `inputs.txt` file should contain a destination and source delimited by a `|`.
# Lines beginning with # are ignored.
#
# For example, suppose your `inputs.txt` file contains the following lines of text:
# > destination | source
#
# Now suppose you run the following line of code:
# > link_inputs(paths, ['inputs.txt'])
#
# This will create a link in `paths['input_dir']` named `destination` that links to `source`.
#
# Alternative, suppose you run the following line of code:
# > copy_inputs(paths, ['inputs.txt'])
#
# This will create a file in `paths['input_dir']` named `destination` that is a copy of `source`.

# Destination | Source
regression.csv | {root}/analysis/output/regression.csv
chips_sold.pdf | {root}/data/output/chips_sold.pdf
tables | {root}/analysis/output/tables
62 changes: 62 additions & 0 deletions extensions/excel_tables/paper_slides/make.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
###################
### ENVIRONMENT ###
###################
import os
import sys

### LOAD GSLAB MAKE
ROOT = '..'
gslm_path = os.path.join(ROOT, 'lib', 'gslab_make')

sys.path.append(gslm_path)
import gslab_make as gs

### PULL PATHS FROM CONFIG
PATHS = {
'root': ROOT,
'config': os.path.join(ROOT, 'config.yaml')
}
PATHS = gs.update_internal_paths(PATHS)

### LOAD CONFIG USER
PATHS = gs.update_external_paths(PATHS)
gs.update_executables(PATHS)

############
### MAKE ###
############

### START MAKE
gs.remove_dir(['input', 'external'])
gs.clear_dir(['output', 'log'])
gs.start_makelog(PATHS)

### MAKE LINKS TO INPUT AND EXTERNAL FILES
inputs = gs.copy_inputs(PATHS, ['input.txt'])
externals = gs.copy_externals(PATHS, ['external.txt'])
gs.write_source_logs(PATHS, inputs + externals)
gs.get_modified_sources(PATHS, inputs + externals)

## MAKE VERSION LOGS
gs.write_version_logs(PATHS)

### FILL TABLES
gs.tablefill(template = 'code/tables.tex',
inputs = 'input/regression.csv',
output = 'output/tables_filled.tex')

### RUN SCRIPTS
gs.run_latex(PATHS, program = 'code/paper.tex')
gs.run_latex(PATHS, program = 'code/online_appendix.tex')
gs.run_latex(PATHS, program = 'code/slides.tex')
gs.export_excel_tables(PATHS, template = 'gs_primary', scalar = 'gs_primary_scalars.xlsx')
gs.quit_excel(PATHS)

### LOG OUTPUTS
gs.log_files_in_output(PATHS)

### CHECK FILE SIZES
gs.check_module_size(PATHS)

### END MAKE
gs.end_makelog(PATHS)

0 comments on commit d20f9bf

Please sign in to comment.