Skip to content
Snippets Groups Projects
Commit e12d6977 authored by marius-lucas braun's avatar marius-lucas braun
Browse files

refactored preprocessing of welfare effects data, added comments

parent b5bb5739
No related branches found
No related tags found
No related merge requests found
File deleted
File added
File deleted
File added
File deleted
File added
File deleted
This diff is collapsed.
# This R code generates random draws from a +-10% interval around the # This R code generates parameter spaces for the distributional
# elasticities of substitution esubd(i) for each sector in the Pothen and # sensivitity analyses of the main results of Hübler et al. (2022).
# Huebler model (2018). It then writes these draws to a CSV file to be # More specifically, 1000 random draws from a +-10% interval around
# used as the parameter space for sensitivity analysis in Snakemake. # each of the sector-specific elasiticity estimates are generated.
# This results in 1000 sets of sectoral parameter values to be used
# in a parameter sweep in Snakemake.
#
# A separate parameter space is generated for each of the three sets
# of sector-specific elasticity parameters present in the model:
# - esubd(i): elasticities between domestically produced versus
# imported goods
# - esubm(i): Armington elasticities
# - esubva(j): elasticities between production factors
# #
# Marius Braun, May 2022 # Marius Braun, April 2023
# load packages
library(ggplot2)
library(readr) library(readr)
library(extrafont)
library(openxlsx)
library(Rcpp)
library(tictoc)
library(moments)
library(confintr)
library(stringr)
library(dplyr) library(dplyr)
# clear workspace
rm(list = ls()) rm(list = ls())
num_draws = 1000 # number of draws from interval num_draws = 1000 # number of draws from interval
set.seed(127) # set random seed set.seed(127) # set random seed
variation = 0.1 variation = 0.1 # size of variation
num_decimals = 3 num_decimals = 3 # max. number of decimals (important due to file path length restrictions)
# create vector of elasticity names # create vector of elasticity names
elasticities = c("esubd", "esubm", "esubva") elasticities = c("esubd", "esubm", "esubva")
for(i in 1:length(elasticities)) { for(i in 1:length(elasticities)) {
# load CSV file with baseline parameter values # load CSV file with baseline parameter values
filename = paste("intervals_10p", elasticities[i], sep = "_") filename = paste("intervals_10p", elasticities[i], sep = "_")
...@@ -58,5 +78,4 @@ for(i in 1:length(elasticities)) { ...@@ -58,5 +78,4 @@ for(i in 1:length(elasticities)) {
row.names = FALSE row.names = FALSE
) )
} }
rm(i, j, filename, file, paramspace) rm(i, j, filename, file, paramspace)
\ No newline at end of file
\ No newline at end of file
# This R code extracts welfare effects from the output files of the
# distributional sensitivity analysis of the main results of Hübler
# et al. (2022). Welfare effects are then stored in a separate data
# frame for esubd(i), esubm(i) and esubva(j) and saved in RDS files
# to be used in subsequent analyses.
#
# Marius Braun, April 2023
# load packages
library(ggplot2)
library(readr)
library(extrafont)
library(openxlsx)
library(Rcpp)
library(tictoc)
library(moments)
library(confintr)
library(stringr)
library(dplyr)
# clear workspace
rm(list = ls())
# this is where you put the output files from the sensitivity analysis
# (use separate subdirectories for esubd(i), esubm(i), and esubva(j))
dir = "C:/Users/Marius Braun/output_sensitivity"
# string vectors for elasticity, policy and income group names
elasticities = c("esubd", "esubm", "esubva")
policies = c("policy", "cbam")
inc_groups = c("lo", "mi", "hi")
for(i in 1:length(elasticities)) {
# load file paths of output files
filenames = as.data.frame(
list.files(
paste(
dir,
elasticities[i],
sep = "/"
),
pattern = "output.xlsx",
full.names = T,
recursive = T)
)
colnames(filenames) = "files"
# create empty data frame to store welfare effects
welf_name = paste(
"welf",
elasticities[i],
sep = "_"
)
welf = as.data.frame(
matrix(,
nrow = nrow(filenames),
ncol = length(policies) * length(inc_groups)
)
)
# name columns of data frame according to policy and income group
for(j in 1:length(policies)) {
for(k in 1:length(inc_groups)) {
col_num = k +
(as.numeric(policies[j] == "cbam") * length(inc_groups))
colnames(welf)[col_num] = paste(
policies[j],
inc_groups[k],
sep = "_"
)
}
}
# get welfare effects from output files
for(l in 1:nrow(filenames)) {
f = filenames$files[l]
if(file.exists(f)) {
welfare = read.xlsx(f, sheet = "welfp") %>%
filter(TOC == "DEU" &
str_detect(X3,
paste(
paste0("\\b",
policies),
collapse = "|")
)
)
welfare = welfare %>% arrange(desc(X3))
}
welf[l, ] = t(welfare$X4)
}
# name welfare effects data frame
assign(
x = welf_name,
value = welf
)
# save welfare effects data frame as RDS file
saveRDS(get(welf_name),
paste0(
"prepared/",
welf_name,
".rds")
)
}
rm(i, j, k, l, f, col_num, welf_name, welf, filenames,
welfare, dir, elasticities, inc_groups, policies)
\ No newline at end of file
...@@ -20,7 +20,7 @@ renv::init(bare = TRUE) ...@@ -20,7 +20,7 @@ renv::init(bare = TRUE)
# Install the packages # Install the packages
install.packages(c( install.packages(c(
"ggplot2", "readr", "extrafont", "openxlsx", "Rcpp", "tictoc", "moments", "ggplot2", "readr", "extrafont", "openxlsx", "Rcpp", "tictoc", "moments",
"confintr", "dplyr" "confintr", "dplyr", "stringr"
)) ))
# Take a snapshot of the renv # Take a snapshot of the renv
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment