anonymous-submission-acl2025's picture
add 17
8a79f2e
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Environment
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Import libraries
library(tidyverse)
library(magrittr)
library(janitor)
library(rio)
library(rlist)
library(boot)
library(broom)
library(purrr)
library(parallel)
library(ebal)
library(rootSolve)
library(stargazer)
library(haven)
library(latex2exp)
library(estimatr)
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Constants
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Define basic constants for identification
num_days <- 20
hourly_rate <- 50
max_hours <- 3
param_initial <-
list(
delta = 0.997,
p_B = (hourly_rate / num_days) / 60
)
# Define constants for balance
us_adults <- 255200373
avg_income <- 43.01
avg_college <- 0.3009
avg_male <- 0.4867
avg_white <- 0.73581
avg_age <- 47.6
avg_usage <- 186
# Define common random seed for bootstrap replicability
size <- 2000
seed <- 5
RNGkind("L'Ecuyer-CMRG")
set.seed(seed)
# Define colors for plots
maroon <- '#94343c'
grey <- '#848484'
skyblue <- '#87CEEB'
black <- '#000000'
deepskyblue <- '#B0C4DE'
# Define base changes for parameters to be ported to tex
# Note : tex names should not have symbols (_ or - or ,) and should not have numbers
constants <- c('delta', 'p_B')
name_changes <- list("omega" = "omegause",
"omega_est" = "omegahat",
"tau_L" = "tauL",
"avg_use" = "avguse",
"rho" = "rhohat",
"lambda" = "lambdahat",
"rho_res" = "rhoreshat",
"lambda_res" = "lambdareshat",
"delta" = "deltahat",
"p_B" = "pB",
"F_B" = "FB",
"tau_B_1" = "tauBone",
"tau_B_2" = "tauBtwo",
"tau_B_3" = "tauBthree",
"tau_B_4" = "tauBfour",
"tau_B_5" = "tauBfive",
"tau_tilde_B_3_2" = "tautildeBthreetwo",
"tau_tilde_B_2_2" = "tautildeBtwotwo",
"tau_tilde_L_3_2" = "tautildeLthreetwo",
"tau_tilde_L_2_2" = "tautildeLtwotwo",
"tau_L_2" = "tauLtwo",
"tau_L_y" = "tauLy",
"tau_B_y" = "tauBy",
"tau_B_2_full" = "tauBtwofull",
"tau_L_3" = "tauLthree",
"mispredict" = "mispredict",
"x_tilde" = "xtilde",
"x_ss" = "xss",
"limit_effect_last_week" = "limiteffectlastweek",
"d_L" = "dL",
"d_CL" = "dCL",
"tau_tilde_B" = "tautildeB",
"tau_tilde_L" = "tautildeL",
"x_tilde_2_B" = "xtildetwoB",
"v_B" = "vB",
"v_L" = "vL",
"attritionrate" = "attritionrate",
"true_tau_tilde_L" = "truetautildeL",
"MPL_S2" = "MPLStwo",
"number_people_opted_out" = "numberpeopleoptedout",
"percent_opted_out" = "percentoptedout",
"gamma_spec_omega_hour" = "gammaspecomegahour",
"alpha" = "alphahat",
"alpha_res" = "alphares",
"eta" = "etahat",
"zeta" = "zetahat",
"eta_hour" = "etahour",
"zeta_hour" = "zetahour",
"eta_res" = "etares",
"zeta_res" = "zetares",
"eta_res_hour" = "etareshour",
"zeta_res_hour" = "zetareshour",
"tau_B_2_desired" = "tauBtwodesired",
"lambdarho" = "lambdarho",
"lambdarhosquared" = "lambdarhosquared",
"tauBthreehour" = "tauBthreehour",
"pcttaubtwo" = "pcttaubtwo",
"naivete" = "naivete",
"gamma_L_effect" = "gammaLeffect",
"gamma_tilde_L_effect" = "gammatildeLeffect",
"gamma_L" = "gammaL",
"gamma_tilde_L" = "gammatildeL",
"gamma_B" = "gammaB",
"gamma_tilde_B" = "gammatildeB",
"gamma" = "gammacap",
"gamma_tilde" = "gammatilde",
"naivete_hour" = "naivetehour",
"gamma_hour" = "gammahour",
"gamma_tilde_B_hour" = "gammatildeBhour",
"gamma_tilde_L_hour" = "gammatildeLhour",
"gamma_tilde_hour" = "gammatildehour",
"gamma_L_hour" = "gammaLhour",
"gamma_B_hour" = "gammaBhour",
"gamma_L_effect_omega_hour" = "gammaLeffectomegahour",
"gamma_L_omega_hour" = "gammaLomegahour",
"underestimatetemp" = "underestimatetemp",
"attritionrate" = "attritionrate",
"gamma_tilde_L_effect_omega" = "gammatildeLeffectomega",
"gamma_L_effect_omega" = "gammaLeffectomega",
"gamma_tilde_L_omega" = "gammatildeLomega",
"gamma_L_omega" = "gammaLomega",
"gamma_tilde_L_multiple" = "gammatildeLmultiple",
"gamma_L_multiple" = "gammaLmultiple",
"gamma_tilde_B_multiple" = "gammatildeBmultiple",
"gamma_B_multiple" = "gammaBmultiple",
"gamma_tilde_L_multiple_hour" = "gammatildeLmultiplehour",
"gamma_L_multiple_hour" = "gammaLmultiplehour",
"gamma_tilde_B_multiple_hour" = "gammatildeBmultiplehour",
"gamma_B_multiple_hour" = "gammaBmultiplehour",
"naivete_res" = "naiveteres",
"underestimatetemp_res" = "underestimatetempres",
"gamma_L_effect_res" = "gammaLeffectres",
"gamma_tilde_L_effect_res" = "gammatildeLeffectres",
"gamma_L_res" = "gammaLres",
"gamma_B_res" = "gammaBres",
"naivete_res_hour" = "naivetereshour",
"gamma_tilde_L_effect_res_hour" = "gammatildeLeffectreshour",
"gamma_L_res_hour" = "gammaLreshour",
"gamma_B_res_hour" = "gammaBreshour",
"gamma_L_effect_omega_res_hour" = "gammaLeffectomegareshour",
"gamma_L_omega_res_hour" = "gammaLomegareshour",
"gamma_tilde_L_effect_omega_res" = "gammatildeLeffectomegares",
"gamma_L_effect_omega_res" = "gammaLeffectomegares",
"gamma_L_omega_res" = "gammaLomegares",
"gamma_L_multiple_res" = "gammaLmultipleres",
"gamma_B_multiple_res" = "gammaBmultipleres",
"gamma_L_effect_multiple_res" = "gammaLeffectmultipleres",
"gamma_tilde_L_effect_multiple_res" = "gammatildeLeffectmultipleres",
"gamma_L_multiple_res_hour" = "gammaLmultiplereshour",
"gamma_B_multiple_res_hour" = "gammaBmultiplereshour",
"gamma_L_effect_multiple_res_hour" = "gammaLeffectmultiplereshour",
"gamma_tilde_L_effect_multiple_res_hour" = "gammatildeLeffectmultiplereshour",
"x_ss_L_effect" = "xssLeffect",
"x_ss_L_effect_eta_high" = "xssLeffectetahigh",
"x_ss_L_effect_eta_low" = "xssLeffectetalow",
"x_ss_B" = "xssB",
"delta_x_cap" = "deltaxcap",
"delta_x_cap_us" = "deltaxcapus",
"delta_x_tilde_L" = "deltaxtildeL",
"delta_x_tilde_L_us" = "deltaxtildeLus",
"delta_x_tilde_B" = "deltaxtildeB",
"delta_x_tilde_B_us" = "deltaxtildeBus",
"x_ss_L" = "xssL",
"mispredicthour" = "mispredicthour",
"delta_x_L_multiple" = "deltaxLmultiple",
"delta_B_multiple" = "deltaBmultiple",
"delta_x_cap_omega" = "deltaxcapomega",
"delta_x_L_omega" = "deltaxLomega",
"x_ss_L_effect_res" = "xssLeffectres",
"x_ss_L_effect_eta_high_res" = "xssLeffectetahighres",
"x_ss_L_effect_eta_low_res" = "xssLeffectetalowres",
"x_ss_B_res" = "xssBres",
"delta_x_cap_res" = "deltaxcapres",
"delta_x_cap_us_res" = "deltaxcapusres",
"delta_x_tilde_L_res" = "deltaxtildeLres",
"delta_x_tilde_L_us_res" = "deltaxtildeLusres",
"delta_x_tilde_B_res" = "deltaxtildeBres",
"delta_x_tilde_B_us_res" = "deltaxtildeBusres",
"x_ss_L_res" = "xssLres",
"delta_x_cap_multiple_res" = "deltaxcapmultipleres",
"delta_x_L_multiple_res" = "deltaxLmultipleres",
"delta_B_multiple_res" = "deltaBmultipleres",
"delta_x_cap_omega_res" = "deltaxcapomegares",
"delta_x_L_omega_res" = "deltaxLomegares",
"delta_x_temptation" = "deltaxtemptation",
"delta_x_naivete" = "deltaxnaivete",
"delta_x_habit" = "deltaxhabit",
"x_ss_temptation" = "xsstemptation",
"x_ss_temptation_eta_high" = "xsstemptationetahigh",
"x_ss_temptation_eta_low" = "xsstemptationetalow",
"x_ss_naivete" = "xssnaivete",
"x_ss_naivete_eta_high" = "xssnaiveteetahigh",
"x_ss_naivete_eta_low" = "xssnaiveteetalow",
"x_ss_habit" = "xsshabit",
"x_ss_habit_eta_high" = "xsshabitetahigh",
"x_ss_habit_eta_low" = "xsshabitetalow",
"x_ss_habit_est" = "xsshabitest",
"x_ss_habit_temptation" = "xsshabittemptation",
"x_ss_habit_temptation_eta_high" = "xsshabittemptationetahigh",
"x_ss_habit_temptation_eta_low" = "xsshabittemptationetalow",
"x_ss_zero_temp" = "xsszerotemp",
"delta_x_temptation_res" = "deltaxtemptationres",
"delta_x_naivete_res" = "deltaxnaiveteres",
"delta_x_habit_res" = "deltaxhabitres",
"x_ss_temptation_res" = "xsstemptationres",
"x_ss_temptation_eta_high_res" = "xsstemptationetahighres",
"x_ss_temptation_eta_low_res" = "xsstemptationetalowres",
"x_ss_naivete_res" = "xssnaiveteres",
"x_ss_naivete_eta_high_res" = "xssnaiveteetahighres",
"x_ss_naivete_eta_low_res" = "xssnaiveteetalowres",
"x_ss_habit_res" = "xsshabitres",
"x_ss_habit_eta_high_res" = "xsshabitetahighres",
"x_ss_habit_eta_low_res" = "xsshabitetalowres",
"x_ss_habit_est_res" = "xsshabitestres",
"x_ss_habit_temptation_res" = "xsshabittemptationres",
"x_ss_habit_temptation_eta_high_res" = "xsshabittemptationetahighres",
"x_ss_habit_temptation_eta_low_res" = "xsshabittemptationetalowres",
"x_ss_i_data" = "xssidata",
"x_ss_spec" = "xssSpec",
"tau_L_2_signed" = "tauLtwosignednotnice",
"intercept_spec" = "interceptspec",
"intercept_spec_hour" = "interceptspechour",
"gamma_tilde_spec" = "gammatildespec",
"gamma_spec" = "gammaspec",
"delta_x_spec" = "deltaxspec",
"gamma_tilde_spec_hour" = "gammatildespechour",
"gamma_spec_hour" = "gammaspechour",
"intercept_het_L_effect" = "intercepthetLeffect",
"intercept_het_L_effect_eta_high" = "intercepthetLeffectetahigh",
"intercept_het_L_effect_eta_low" = "intercepthetLeffectetalow",
"intercept_het_B" = "intercepthetB",
"intercept_het_L" = "intercepthetL",
"intercept_het_B_hour" = "intercepthetBhour",
"intercept_het_L_hour" = "intercepthetLhour",
"intercept_het_L_effect_hour" = "intercepthetLeffecthour",
"gamma_spec_omega_hour" = "gammaspecomegahour",
"intercept_spec_omega_hour" = "interceptspecomegahour",
"intercept_het_L_effect_omega_hour" = "intercepthetLeffectomegahour",
"intercept_het_L_omega_hour" = "intercepthetLomegahour",
"intercept_het_B_multiple" = "intercepthetBmultiple",
"intercept_het_L_multiple" = "intercepthetLmultiple",
"intercept_het_L_effect_omega" = "intercepthetLeffectomega",
"intercept_het_L_omega" = "intercepthetLomega",
"x_ss_spec_res" = "xssSpecres",
"intercept_spec_res" = "interceptspecres",
"intercept_spec_res_hour" = "interceptspecreshour",
"gamma_tilde_spec_res" = "gammatildespecres",
"gamma_spec_res" = "gammaspecwres",
"delta_x_spec_res" = "deltaxspecres",
"gamma_tilde_spec_res_hour" = "gammatildespecreshour",
"gamma_spec_res_hour" = "gammaspechourres",
"intercept_het_L_effect_res" = "intercepthetLeffectres",
"intercept_het_L_effect_eta_high_res" = "intercepthetLeffectetahighres",
"intercept_het_L_effect_eta_low_res" = "intercepthetLeffectetalowres",
"intercept_het_B_res" = "intercepthetBres",
"intercept_het_L_res" = "intercepthetLres",
"intercept_het_B_res_hour" = "intercepthetBreshour",
"intercept_het_L_res_hour" = "intercepthetLreshour",
"intercept_het_L_effect_res_hour" = "intercepthetLeffectreshour",
"gamma_spec_omega_res_hour" = "gammaspecomegareshour",
"intercept_spec_omega_res_hour" = "interceptspecomegareshour",
"intercept_het_L_effect_omega_res_hour" = "intercepthetLeffectomegareshour",
"intercept_het_L_omega_res_hour" = "intercepthetLomegareshour",
"intercept_het_L_effect_multiple_res" = "intercepthetLeffectmultipleres",
"intercept_het_B_multiple_res" = "intercepthetBmultipleres",
"intercept_het_L_multiple_res" = "intercepthetLmultipleres",
"intercept_het_L_effect_omega_res" = "intercepthetLeffectomegares",
"intercept_het_L_omega_res" = "intercepthetLomegares",
"gamma_L_effect_res_hour" = "gammaLeffectreshour",
"delta_x_temptation_wo_habit" = "deltaxtemptationwohabit",
"delta_x_temptation_wo_habit_res" = "deltaxtemptationwohabitres",
"DWLStatic" = "DWLStatic",
"DWLStaticThreeWeeks" = "DWLStaticThreeWeeks",
"YearlyWelfare" = "YearlyWelfare"
)
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Utility functions
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
find_bottom <- function(x){
return(as.numeric(quantile(x, 0.025, na.rm=TRUE)))
}
find_top <- function(x){
return(as.numeric(quantile(x, 0.975, na.rm=TRUE)))
}
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Data functions
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
import_data <- function(){
# Import data
df <- import('input/final_data_sample.dta')
# Clean data
df %<>%
mutate(L = ifelse(S2_LimitType != 0, 1, 0)) %>%
mutate(B = ifelse(S3_Bonus == 1, 1, 0)) %>%
mutate(S = as.character(Stratifier)) %>%
mutate(w = 1)
return(df)
}
balance_data <- function(df, magnitude=2){
cols_used <- c('balance_income', 'balance_college', 'balance_male',
'balance_white', 'balance_age')
balance_df <- df[, c('UserID', cols_used)]
balance_df$treat <- 0
avg_df <- data.frame(0,
avg_income,
avg_college,
avg_male,
avg_white,
avg_age,
1)
names(avg_df) <- c('UserID', cols_used, 'treat')
balance_df <- rbind(balance_df, avg_df[rep(1, nrow(df)),])
ebal.out <- ebalance(Treatment = balance_df$treat,
X = balance_df[,cols_used],
print.level = 0)
df$w <- ebal.out$w
df$w <- sapply(df$w, function(x){ return(min(magnitude, x))})
df$w <- sapply(df$w, function(x){ return(max(1/magnitude, x))})
return(df)
}
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Solve functions
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#solves for rho and lambda
solve_sys_eq_1 <- function(param) {
# Define
tau_B_2 <- param[['tau_B_2']]
tau_B_3 <- param[['tau_B_3']]
tau_B_4 <- param[['tau_B_4']]
tau_B_5 <- param[['tau_B_5']]
# Solve
a <- tau_B_2*tau_B_4
b <- tau_B_3*tau_B_4 - tau_B_2*tau_B_5
c <- tau_B_4^2 - tau_B_3*tau_B_5
#solve for rho using the quadratic formula and choose the larger value
rho <- ifelse((tau_B_2==0), tau_B_5/tau_B_4 - tau_B_4/tau_B_3, (-b + sqrt(b^2 - 4*a*c)) / (2*a))
lambda <- tau_B_4 / (rho*tau_B_3 + rho^2*tau_B_2)
rho <- ifelse(rho<0, 0, rho)
lambda <- ifelse(lambda<0, 0, lambda)
# Solve for tau_b_2=0, i.e. restricted alpha=1 model
rho_res <- tau_B_5/tau_B_4 - tau_B_4/tau_B_3
rho_res <- ifelse(rho_res<0, 0, rho_res)
lambda_res <- tau_B_4 / (rho_res*tau_B_3)
lambda_res <- ifelse(lambda_res<0, 0, lambda_res)
# Return
solution <- list(
rho = rho,
lambda = lambda,
rho_res = rho_res,
lambda_res = lambda_res
)
return(solution)
}
# Solves for alpha, eta, zeta
solve_sys_eq_2 <- function(param, display_warning=FALSE) {
rho = param[['rho']]
lambda = param[['lambda']]
rho_res = param[['rho_res']]
lambda_res = param[['lambda_res']]
delta = param[['delta']]
tau_B_2 = param[['tau_B_2']]
tau_tilde_B = param[['tau_tilde_B']]
tau_B_3 = param[['tau_B_3']]
tau_B_4 = param[['tau_B_4']]
tau_tilde_B_3_2 = param[['tau_tilde_B_3_2']]
p_B = param[['p_B']]
find_y <- function(alpha, s, rho=rho, lambda=lambda){
num <- -tau_B_4 + (1-alpha) * delta * (rho^2) * lambda * s
denom <- rho * tau_B_3 + (rho^2) * tau_B_2 - (1-alpha) * delta * (rho^2) * (1 - lambda) * s
return(num / denom)
}
find_x <- function(alpha, s, y, rho=rho, lambda=lambda){
x1 <- tau_B_3 - (1-alpha)*delta*(rho^2)*lambda*(rho*tau_B_2 + tau_B_3)
x2 <- rho * tau_B_2 - (1-alpha)*delta*(rho^2)*(1 - lambda)*(rho*tau_B_2 + tau_B_3)
return(x1 + y * x2)
}
solve_alpha <- function(alpha){
s <- (rho^2) * tau_B_2 + rho * tau_B_3 + tau_B_4
y <- find_y(alpha, s, rho=rho, lambda=lambda)
x <- find_x(alpha, s, y, rho=rho, lambda=lambda)
eta <- p_B / x
zeta <- y * eta
num_z <- eta*tau_B_2
den_z <- delta*rho*(-p_B + (eta - zeta)*tau_tilde_B + zeta * rho * tau_B_2)
z <- (num_z / den_z) - (1-alpha)
return(z)
}
#Unrestricted model
print(tau_B_2)
if (tau_B_2 >= 0){
alpha <- 1
} else {
alpha <- uniroot(solve_alpha, interval=c(0,1.2))$root
}
alpha <- ifelse(alpha > 1, 1, alpha)
alpha <- ifelse(alpha<0, 0, alpha)
s <- (rho^2) * tau_B_2 + rho * tau_B_3 + tau_B_4
y <- find_y(alpha, s, rho=rho, lambda=lambda)
x <- find_x(alpha, s, y, rho=rho, lambda=lambda)
eta <- p_B / x
zeta <- y * eta
#Winsorise parameters at 0
eta <- ifelse(eta>0, 0, eta)
zeta <- ifelse(zeta<0, 0, zeta)
denom <- -eta - (1-alpha)*delta*rho*(zeta-eta)-zeta*((rho-(1-alpha)*delta*rho^2)/(1-rho))
#eta <- ifelse(denom <0, NA, eta)
#zeta <- ifelse(denom <0, NA, zeta)
#Restricted model
# note: alpha=1 in restricted model
alpha_res <- 1
eta_res <- p_B / tau_B_3
zeta_res <- (-eta_res*tau_B_4) / (rho_res*tau_B_3)
#Winsorise parameters at 0
eta_res <- ifelse(eta_res>0, 0, eta_res)
zeta_res <- ifelse(zeta_res<0, 0, zeta_res)
# Display error for concavity assumptions
if (display_warning){
if(rho*(1+lambda)>1){
stop("Concavity condition for rho(1+lambda) not satisfied")
}
}
#Get hourly estimates
eta_hour <- eta*3600
zeta_hour <- zeta*3600
eta_res_hour <- eta_res*3600
zeta_res_hour <- zeta_res*3600
#Export desired variables for in-text references
lambdarho <- signif(lambda*rho, digits=2)
lambdarhosquared <- lambda*rho^2
lambdarhosquared <- signif(lambdarhosquared, digits=1)
tauBthreehour <- signif(tau_B_3/60, digits=3)
find_y_tau <- function(tau_B_2){
s <- (rho^2) * tau_B_2 + rho * tau_B_3 + tau_B_4
num <- -tau_B_4 + delta * (rho^2) * lambda * s
denom <- rho * tau_B_3 + (rho^2) * tau_B_2 - delta * (rho^2) * (1 - lambda) * s
return(num / denom)
}
find_x_tau <- function(tau_B_2, y){
s <- (rho^2) * tau_B_2 + rho * tau_B_3 + tau_B_4
x1 <- tau_B_3 - delta*(rho^2)*lambda*(rho*tau_B_2 + tau_B_3)
x2 <- rho * tau_B_2 - delta*(rho^2)*(1 - lambda)*(rho*tau_B_2 + tau_B_3)
return(x1 + y * x2)
}
solve_tau <- function(tau_B_2){
y <- find_y_tau(tau_B_2)
x <- find_x_tau(tau_B_2, y)
left <- delta * rho * (-x + (1-y)*tau_tilde_B)
right <- tau_B_2 * (1 - y * delta * rho^2)
return(left - right)
}
tau_B_2_desired <- NA
try(tau_B_2_desired <- uniroot(solve_tau, interval=c(-60,5))$root, silent = T)
pcttaubtwo <- 100*(tau_B_2)/tau_B_2_desired
oneminusalphapct <- (1-alpha)*100
# Return
solution <- list(
alpha = alpha,
eta = eta,
zeta = zeta,
eta_hour = eta_hour,
zeta_hour = zeta_hour,
alpha_res = alpha_res,
eta_res = eta_res,
zeta_res = zeta_res,
eta_res_hour = eta_res_hour,
zeta_res_hour = zeta_res_hour,
tau_B_2_desired = tau_B_2_desired,
lambdarho = lambdarho,
lambdarhosquared = lambdarhosquared,
tauBthreehour = tauBthreehour,
pcttaubtwo = pcttaubtwo,
oneminusalphapct = oneminusalphapct,
denom = denom
)
# Return
return(solution)
}
solve_sys_eq_3 <- function(param) {
# Define
rho <- param[['rho']]
lambda <- param[['lambda']]
rho_res <- param[['rho_res']]
lambda_res <- param[['lambda_res']]
alpha <- param[['alpha']]
alpha_res <- param[['alpha_res']]
delta <- param[['delta']]
eta <- param[['eta']]
zeta <- param[['zeta']]
eta_res <- param[['eta_res']]
zeta_res <- param[['zeta_res']]
p_B <- param[['p_B']]
F_B <- param[['F_B']]
tau_B_2 <- param[['tau_B_2']]
tau_B_3 <- param[['tau_B_3']]
tau_tilde_B <- param[['tau_tilde_B']]
tau_tilde_L <- param[['tau_tilde_L']]
tau_L_2 <- param[['tau_L_2']]
tau_L_y <- param[['tau_L_y']]
tau_B_y <- param[['tau_B_y']]
mispredict <- param[['mispredict']]
d_L <- param[['d_L']]
d_CL <- param[['d_CL']]
v_L <- param[['v_L']]
v_B <- param[['v_B']]
x_ss <- param[['x_ss']]
x_tilde_2_B <- param[['x_tilde_2_B']]
# Compute estimated Omega
omega_est <- (d_L - d_CL) / -d_CL
omega <- 1
p <- p_B
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Unrestricted alpha
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Naivete
term <- -eta + (1-alpha)*delta*(rho^2)*(eta*lambda + zeta*(1 - lambda))
naivete <- mispredict*term
# Gamma-tilde-L
gamma_tilde_L <- (2*v_L) / (-tau_tilde_L*(2-omega))
gamma_L <- naivete + gamma_tilde_L
# Gamma-tilde-L omega
gamma_tilde_L_omega <- (2*v_L) / (-tau_tilde_L*(2-omega_est))
gamma_L_omega <- naivete + gamma_tilde_L_omega
# Gamma-tilde-L multiple good
gamma_tilde_L_multiple <- v_L/-(tau_tilde_L*(2-omega)/2+tau_L_y)
gamma_L_multiple <- naivete + gamma_tilde_L_multiple
# Gamma-tilde-B
term <- v_B - F_B + p*x_tilde_2_B
gamma_tilde_B <- term / (-tau_tilde_B)
gamma_B <- naivete + gamma_tilde_B
# Gamma-tilde-B multiple good
gamma_tilde_B_multiple <- (v_B - F_B + p*x_tilde_2_B)/-(tau_tilde_B+tau_B_y)
gamma_B_multiple <- naivete + gamma_tilde_B_multiple
# Gamma-L-effect
num <- eta*tau_L_2/omega - (1-alpha)*delta*rho*(((eta-zeta)*tau_tilde_L/omega+zeta*rho*tau_L_2/omega) + (1+lambda)*mispredict*(-eta+(1-alpha)*delta*rho^2*((eta-zeta)*lambda+zeta)))
denom <- 1 - (1-alpha)*delta*rho*(1+lambda)
gamma_L_effect <- num/denom
gamma_tilde_L_effect <- gamma_L_effect - naivete
# Gamma-L-effect omega
num_omega <- eta*tau_L_2/omega_est - (1-alpha)*delta*rho*(((eta-zeta)*tau_tilde_L/omega_est+zeta*rho*tau_L_2/omega_est) + (1+lambda)*mispredict*(-eta+(1-alpha)*delta*rho^2*((eta-zeta)*lambda+zeta)))
gamma_L_effect_omega <- num_omega/denom
gamma_tilde_L_effect_omega <- gamma_L_effect_omega - naivete
# Select Gammas
gamma_tilde <- gamma_tilde_L_effect
gamma <- gamma_L_effect
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Restricted alpha=1
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Naivete
naivete_res <- mispredict*(-eta_res)
# Gamma-tilde-L
gamma_tilde_L <- (2*v_L) / (-tau_tilde_L*(2-omega))
gamma_L_res <- naivete_res + gamma_tilde_L
# Gamma-tilde-L omega
gamma_tilde_L_omega <- (2*v_L) / (-tau_tilde_L*(2-omega_est))
gamma_L_omega_res <- naivete_res + gamma_tilde_L_omega
# Gamma-tilde-L multiple goods
gamma_tilde_L_multiple <- v_L/-(tau_tilde_L*(2-omega)/2+tau_L_y)
gamma_L_multiple_res <- naivete_res + gamma_tilde_L_multiple
# Gamma-tilde-B
gamma_B_res <- naivete_res + gamma_tilde_B
# Gamma tilde-B multiple goods
gamma_B_multiple_res <- naivete_res + gamma_tilde_B_multiple
# Gamma-L-effect
gamma_L_effect_res <- eta_res*tau_L_2
gamma_tilde_L_effect_res <- gamma_L_effect_res - naivete_res
# Gamma-L-effect omega
gamma_L_effect_omega_res <- eta_res*tau_L_2/omega_est
gamma_tilde_L_effect_omega_res <- gamma_L_effect_omega_res - naivete_res
# Gamma-L-effect multiple goods (note: only for restricted case)
gamma_L_effect_multiple_res <- (tau_L_2*p/tau_B_3)/(1+tau_L_y/tau_L_2)
gamma_tilde_L_effect_multiple_res <- gamma_L_effect_multiple_res - naivete_res
#Hourly variables for the tables
naivete_hour <- naivete*60
gamma_hour <- gamma*60
gamma_L_hour <- gamma_L*60
gamma_B_hour <- gamma_B*60
gamma_tilde_hour <- gamma_tilde*60
gamma_tilde_B_hour <- gamma_tilde_B*60
gamma_tilde_L_hour <- gamma_tilde_L*60
gamma_L_effect_omega_hour <- gamma_L_effect_omega*60
gamma_L_omega_hour <- gamma_L_omega*60
underestimatetemp <- naivete_hour/gamma_hour*100
gamma_tilde_L_multiple_hour <- gamma_tilde_L_multiple*60
gamma_L_multiple_hour <- gamma_L_multiple*60
gamma_tilde_B_multiple_hour <- gamma_tilde_B_multiple*60
gamma_B_multiple_hour <- gamma_B_multiple*60
naivete_res_hour <- naivete_res*60
gamma_L_effect_res_hour <- gamma_L_effect_res*60
gamma_L_res_hour <- gamma_L_res*60
gamma_B_res_hour <- gamma_B_res*60
underestimatetemp_res <- naivete_res_hour/gamma_L_effect_res_hour*100
gamma_tilde_L_effect_res_hour <- gamma_tilde_L_effect_res*60
gamma_L_effect_omega_res_hour <- gamma_L_effect_omega_res*60
gamma_L_omega_res_hour <- gamma_L_omega_res*60
gamma_L_multiple_res_hour <- gamma_L_multiple_res*60
gamma_B_multiple_res_hour <- gamma_B_multiple_res*60
gamma_L_effect_multiple_res_hour <- gamma_L_effect_multiple_res*60
gamma_tilde_L_effect_multiple_res_hour <- gamma_tilde_L_effect_multiple_res*60
attritionrate <- (1-1933/2048)
DWLStatic <- -(tau_L_2/60*gamma_L_effect_res_hour/2)
DWLStaticThreeWeeks <- DWLStatic*21
YearlyWelfare <- DWLStaticThreeWeeks*52/3*0.24
# Return
solution <- list(
naivete = naivete,
omega_est = omega_est,
omega = omega,
gamma_L_effect = gamma_L_effect,
gamma_tilde_L_effect = gamma_tilde_L_effect,
gamma_L = gamma_L,
gamma_tilde_L = gamma_tilde_L,
gamma_B = gamma_B,
gamma_tilde_B = gamma_tilde_B,
gamma = gamma,
gamma_tilde = gamma_tilde,
naivete_hour = naivete_hour,
gamma_hour = gamma_hour,
gamma_tilde_B_hour = gamma_tilde_B_hour,
gamma_tilde_L_hour = gamma_tilde_L_hour,
gamma_tilde_hour = gamma_tilde_hour,
gamma_L_hour = gamma_L_hour,
gamma_B_hour = gamma_B_hour,
gamma_L_effect_omega_hour = gamma_L_effect_omega_hour,
gamma_L_omega_hour = gamma_L_omega_hour,
underestimatetemp = underestimatetemp,
attritionrate = attritionrate,
gamma_tilde_L_effect_omega = gamma_tilde_L_effect_omega,
gamma_L_effect_omega = gamma_L_effect_omega,
gamma_tilde_L_omega = gamma_tilde_L_omega,
gamma_L_omega = gamma_L_omega,
gamma_tilde_L_multiple = gamma_tilde_L_multiple,
gamma_L_multiple = gamma_L_multiple,
gamma_tilde_B_multiple = gamma_tilde_B_multiple,
gamma_B_multiple = gamma_B_multiple,
gamma_tilde_L_multiple_hour = gamma_tilde_L_multiple_hour,
gamma_L_multiple_hour = gamma_L_multiple_hour,
gamma_tilde_B_multiple_hour = gamma_tilde_B_multiple_hour,
gamma_B_multiple_hour = gamma_B_multiple_hour,
naivete_res = naivete_res,
underestimatetemp_res = underestimatetemp_res,
gamma_L_effect_res = gamma_L_effect_res,
gamma_tilde_L_effect_res = gamma_tilde_L_effect_res,
gamma_L_res = gamma_L_res,
gamma_B_res = gamma_B_res,
naivete_res_hour = naivete_res_hour,
gamma_tilde_L_effect_res_hour = gamma_tilde_L_effect_res_hour,
gamma_L_res_hour = gamma_L_res_hour,
gamma_B_res_hour = gamma_B_res_hour,
gamma_L_effect_omega_res_hour = gamma_L_effect_omega_res_hour,
gamma_L_omega_res_hour = gamma_L_omega_res_hour,
gamma_tilde_L_effect_omega_res = gamma_tilde_L_effect_omega_res,
gamma_L_effect_omega_res = gamma_L_effect_omega_res,
gamma_L_omega_res = gamma_L_omega_res,
gamma_L_multiple_res = gamma_L_multiple_res,
gamma_B_multiple_res = gamma_B_multiple_res,
gamma_tilde_L_effect_multiple_res = gamma_tilde_L_effect_multiple_res,
gamma_L_effect_multiple_res = gamma_L_effect_multiple_res,
gamma_L_multiple_res_hour = gamma_L_multiple_hour,
gamma_B_multiple_res_hour = gamma_B_multiple_res_hour,
gamma_tilde_L_effect_multiple_res_hour = gamma_tilde_L_effect_multiple_res_hour,
gamma_L_effect_multiple_res_hour = gamma_L_effect_multiple_res_hour,
gamma_L_effect_res_hour = gamma_L_effect_res_hour,
DWLStatic = DWLStatic,
DWLStaticThreeWeeks = DWLStaticThreeWeeks,
YearlyWelfare = YearlyWelfare
)
return(solution)
}
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Calculate effects from different identification strategies
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
solve_effects <- function(param, df){
# Define
eta <- param[['eta']]
zeta <- param[['zeta']]
eta_res <- param[['eta_res']]
zeta_res <- param[['zeta_res']]
alpha <- param[['alpha']]
alpha_res <- param[['alpha_res']]
rho <- param[['rho']]
rho_res <- param[['rho_res']]
lambda <- param[['lambda']]
lambda_res <- param[['lambda_res']]
delta <- param[['delta']]
p_B <- param[['p_B']]
naivete <- param[['naivete']]
naivete_res <- param[['naivete_res']]
mispredict <- param[['mispredict']]
x_ss <- param[['x_ss']]
intercept_het_L_effect <- param[['intercept_het_L_effect']]
intercept_het_L_effect_eta_high <- param[['intercept_het_L_effect_eta_high']]
intercept_het_L_effect_eta_low <- param[['intercept_het_L_effect_eta_low']]
intercept_het_B <- param[['intercept_het_B']]
intercept_het_L <- param[['intercept_het_L']]
intercept_het_B_multiple <- param[['intercept_het_B_multiple']]
intercept_het_L_multiple <- param[['intercept_het_L_multiple']]
intercept_het_L_effect_omega <- param[['intercept_het_L_effect_omega']]
intercept_het_L_omega <- param[['intercept_het_L_omega']]
intercept_het_L_effect_res <- param[['intercept_het_L_effect_res']]
intercept_het_L_effect_eta_high_res <- param[['intercept_het_L_effect_eta_high_res']]
intercept_het_L_effect_eta_low_res <- param[['intercept_het_L_effect_eta_low_res']]
intercept_het_B_res <- param[['intercept_het_B_res']]
intercept_het_L_res <- param[['intercept_het_L_res']]
intercept_het_L_effect_multiple_res <- param[['intercept_het_L_effect_multiple_res']]
intercept_het_B_multiple_res <- param[['intercept_het_B_multiple_res']]
intercept_het_L_multiple_res <- param[['intercept_het_L_multiple_res']]
intercept_het_L_effect_omega_res <- param[['intercept_het_L_effect_omega_res']]
intercept_het_L_omega_res <- param[['intercept_het_L_omega_res']]
gamma_L_effect <- param[['gamma_L_effect']]
gamma_tilde_L_effect <- param[['gamma_tilde_L_effect']]
gamma_L_effect_res <- param[['gamma_L_effect_res']]
gamma_tilde_L_effect_res <- param[['gamma_tilde_L_effect_res']]
gamma_L_res <- param[['gamma_L_res']]
gamma_L <- param[['gamma_L']]
gamma_tilde_L <- param[['gamma_tilde_L']]
gamma_B <- param[['gamma_B']]
gamma_B_res <- param[['gamma_B_res']]
gamma_tilde_B <- param[['gamma_tilde_B']]
gamma_tilde_L_multiple <- param[['gamma_tilde_L_multiple']]
gamma_L_multiple <- param[['gamma_L_multiple']]
gamma_L_multiple_res <- param[['gamma_L_multiple_res']]
gamma_tilde_B_multiple <- param[['gamma_tilde_B_multiple']]
gamma_B_multiple <- param[['gamma_B_multiple']]
gamma_B_multiple_res <- param[['gamma_B_multiple_res']]
gamma_L_effect_multiple_res <- param[['gamma_L_effect_multiple_res']]
gamma_tilde_L_effect_multiple_res <- param[['gamma_tilde_L_effect_multiple_res']]
gamma_tilde_L_effect_omega <- param[['gamma_tilde_L_effect_omega']]
gamma_L_effect_omega <- param[['gamma_L_effect_omega']]
gamma_tilde_L_effect_omega_res <- param[['gamma_tilde_L_effect_omega_res']]
gamma_L_effect_omega_res <- param[['gamma_L_effect_omega_res']]
gamma_tilde_L_omega <- param[['gamma_tilde_L_omega']]
gamma_L_omega <- param[['gamma_L_omega']]
gamma_L_omega_res <- param[['gamma_L_omega_res']]
# set price to zero
p <- 0
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Unrestricted alpha model
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Estimating using gamma from limit effect
x_ss_L_effect <- max(0, calculate_steady_state(param, gamma_tilde_L_effect, gamma_L_effect, alpha, rho, lambda, mispredict, eta, zeta, intercept=intercept_het_L_effect))
delta_x_cap <- x_ss_L_effect - max(0, calculate_steady_state(param, 0, 0, alpha, rho, lambda, 0, eta, zeta, intercept=intercept_het_L_effect))
delta_x_cap_us <- delta_x_cap * 365 * us_adults
# Estimating using gamma from limit effect eta high (eta*1.1) and low (eta*0.9)
x_ss_L_effect_eta_high <- max(0, calculate_steady_state(param, gamma_tilde_L_effect, gamma_L_effect, alpha, rho, lambda, mispredict, eta, zeta, intercept=intercept_het_L_effect_eta_high, eta_scale=1.1))
x_ss_L_effect_eta_low <- max(0, calculate_steady_state(param, gamma_tilde_L_effect, gamma_L_effect, alpha, rho, lambda, mispredict, eta, zeta, intercept=intercept_het_L_effect_eta_low, eta_scale=0.9))
# Estimating using gamma_tilde from limit valuation
x_ss_L <- max(0, calculate_steady_state(param, gamma_tilde_L, gamma_L, alpha, rho, lambda, mispredict, eta, zeta, intercept=intercept_het_L))
delta_x_tilde_L <- x_ss_L - max(0, calculate_steady_state(param, 0, 0, alpha, rho, lambda, 0, eta, zeta, intercept=intercept_het_L))
delta_x_tilde_L_us <- delta_x_tilde_L * 365 * us_adults
# Estimating using gamma_tilde from bonus valuation
x_ss_B <- max(0, calculate_steady_state(param, gamma_tilde_B, gamma_B, alpha, rho, lambda, mispredict, eta, zeta, intercept=intercept_het_B))
delta_x_tilde_B <- x_ss_B - max(0, calculate_steady_state(param, 0, 0, alpha, rho, lambda, 0, eta, zeta, intercept=intercept_het_B))
delta_x_tilde_B_us <- delta_x_tilde_B * 365 * us_adults
mispredicthour <- mispredict/60
# Estimating time effects using multiple goods moments
x_ss_L_multiple <- max(0, calculate_steady_state(param, gamma_tilde_L_multiple, gamma_L_multiple, alpha, rho, lambda, mispredict, eta, zeta, intercept=intercept_het_L_multiple))
delta_x_L_multiple <- max(0,x_ss_L_multiple - max(0, calculate_steady_state(param, 0, 0, alpha, rho, lambda, 0, eta, zeta, intercept=intercept_het_L_multiple)))
x_ss_B_multiple <- max(0, calculate_steady_state(param, gamma_tilde_B_multiple, gamma_B_multiple, alpha, rho, lambda, mispredict, eta, zeta, intercept=intercept_het_B_multiple))
delta_B_multiple <- max(0,x_ss_B_multiple - max(0, calculate_steady_state(param, 0, 0, alpha, rho,lambda, 0, eta, zeta, intercept=intercept_het_B_multiple)))
# Estimating time effects using omega hat moments
# limit effect; omega_hat
x_ss_L_effect_omega <- max(0, calculate_steady_state(param, gamma_tilde_L_effect_omega, gamma_L_effect_omega, alpha, rho, lambda, mispredict, eta, zeta, intercept=intercept_het_L_effect_omega))
delta_x_cap_omega <- max(0,x_ss_L_effect_omega - max(0, calculate_steady_state(param, 0, 0, alpha, rho, lambda, 0, eta, zeta, intercept=intercept_het_L_effect_omega)))
# limit valuation: omega_hat
x_ss_L_omega <- max(0, calculate_steady_state(param, gamma_tilde_L_omega, gamma_L_omega, alpha, rho, lambda, mispredict, eta, zeta, intercept=intercept_het_L_omega))
delta_x_L_omega <- max(0,x_ss_L_omega - max(0, calculate_steady_state(param, 0, 0, alpha, rho, lambda, 0, eta, zeta, intercept=intercept_het_L_omega)))
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Restricted alpha=1 model
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Estimating using gamma from limit effect
x_ss_L_effect_res <- max(0, calculate_steady_state(param, gamma_tilde_L_effect_res, gamma_L_effect_res, alpha_res, rho_res, lambda_res, mispredict, eta_res, zeta_res, intercept=intercept_het_L_effect_res))
delta_x_cap_res <- x_ss_L_effect_res - max(0, calculate_steady_state(param, 0, 0, alpha_res, rho_res, lambda_res, 0, eta_res, zeta_res, intercept=intercept_het_L_effect_res))
delta_x_cap_us_res <- delta_x_cap_res * 365 * us_adults
# Estimating using gamma from limit effect eta high (eta*1.1) and low (eta*0.9)
x_ss_L_effect_eta_high_res <- max(0, calculate_steady_state(param, gamma_tilde_L_effect_res, gamma_L_effect_res, alpha_res, rho_res, lambda_res, mispredict, eta_res, zeta_res, intercept=intercept_het_L_effect_eta_high_res, eta_scale=1.1))
x_ss_L_effect_eta_low_res <- max(0, calculate_steady_state(param, gamma_tilde_L_effect_res, gamma_L_effect_res, alpha_res, rho_res, lambda_res, mispredict, eta_res, zeta_res, intercept=intercept_het_L_effect_eta_low_res, eta_scale=0.9))
# Estimating using gamma_tilde from limit valuation (note: gamma_tilde_L is same for restricted and unrestricted models)
x_ss_L_res <- max(0, calculate_steady_state(param, gamma_tilde_L, gamma_L_res, alpha_res, rho_res, lambda_res, mispredict, eta_res, zeta_res, intercept=intercept_het_L_res))
delta_x_tilde_L_res <- x_ss_L_res - max(0, calculate_steady_state(param, 0, 0, alpha_res, rho_res, lambda_res, 0, eta_res, zeta_res, intercept=intercept_het_L_res))
delta_x_tilde_L_us_res <- delta_x_tilde_L_res * 365 * us_adults
# Estimating using gamma_tilde from bonus valuation (note: gamma_tilde_B is same for restricted and unrestricted models)
x_ss_B_res <- max(0, calculate_steady_state(param, gamma_tilde_B, gamma_B_res, alpha_res, rho_res, lambda_res, mispredict, eta_res, zeta_res, intercept=intercept_het_B_res))
delta_x_tilde_B_res <- x_ss_B_res - max(0, calculate_steady_state(param, 0, 0, alpha_res, rho_res, lambda_res, 0, eta_res, zeta_res, intercept=intercept_het_B_res))
delta_x_tilde_B_us_res <- delta_x_tilde_B_res * 365 * us_adults
# Estimating time effects using multiple goods moments
x_ss_L_effect_multiple_res <- max(0, calculate_steady_state(param, gamma_tilde_L_effect_multiple_res, gamma_L_effect_multiple_res, alpha_res, rho_res, lambda_res, mispredict, eta_res, zeta_res, intercept=intercept_het_L_effect_multiple_res))
delta_x_cap_multiple_res <- max(0,x_ss_L_effect_multiple_res - max(0, calculate_steady_state(param, 0, 0, alpha_res, rho_res, lambda_res, 0, eta_res, zeta_res, intercept=intercept_het_L_effect_multiple_res)))
x_ss_L_multiple_res <- max(0, calculate_steady_state(param, gamma_tilde_L_multiple, gamma_L_multiple_res, alpha_res, rho_res, lambda_res, mispredict, eta_res, zeta_res, intercept=intercept_het_L_multiple_res))
delta_x_L_multiple_res <- max(0,x_ss_L_multiple_res - max(0, calculate_steady_state(param, 0, 0, alpha_res, rho_res, lambda_res, 0, eta_res, zeta_res, intercept=intercept_het_L_multiple_res)))
x_ss_B_multiple_res <- max(0, calculate_steady_state(param, gamma_tilde_B_multiple, gamma_B_multiple_res, alpha_res, rho_res, lambda_res, mispredict, eta_res, zeta_res, intercept=intercept_het_B_multiple_res))
delta_B_multiple_res <- max(0,x_ss_B_multiple_res - max(0, calculate_steady_state(param, 0, 0, alpha_res, rho_res, lambda_res, 0, eta_res, zeta_res, intercept=intercept_het_B_multiple_res)))
# Estimating time effects using omega hat moments (limit effects)
x_ss_L_effect_omega_res <- max(0, calculate_steady_state(param, gamma_tilde_L_effect_omega_res, gamma_L_effect_omega_res, alpha_res, rho_res, lambda_res, mispredict, eta_res, zeta_res, intercept=intercept_het_L_effect_omega_res))
delta_x_cap_omega_res <- max(0,x_ss_L_effect_omega_res - max(0, calculate_steady_state(param, 0, 0, alpha_res, rho_res, lambda_res, 0, eta_res, zeta_res, intercept=intercept_het_L_effect_omega_res)))
x_ss_L_omega_res <- max(0, calculate_steady_state(param, gamma_tilde_L_omega, gamma_L_omega_res, alpha_res, rho_res, lambda_res, mispredict, eta_res, zeta_res, intercept=intercept_het_L_omega_res))
delta_x_L_omega_res <- max(0,x_ss_L_omega_res - max(0, calculate_steady_state(param, 0, 0, alpha_res, rho_res, lambda_res, 0, eta_res, zeta_res, intercept=intercept_het_L_omega_res)))
#xero temp x_ss
x_ss_zero_temp <- max(0, calculate_steady_state(param, 0, 0, alpha, rho, lambda, 0, eta, zeta, intercept=intercept_het_L_effect))
# Return
solution <- list(
x_ss_L_effect = x_ss_L_effect,
x_ss_L_effect_eta_high = x_ss_L_effect_eta_high,
x_ss_L_effect_eta_low = x_ss_L_effect_eta_low,
x_ss_B = x_ss_B,
delta_x_cap = delta_x_cap,
delta_x_cap_us = delta_x_cap_us,
delta_x_tilde_L = delta_x_tilde_L,
delta_x_tilde_L_us = delta_x_tilde_L_us,
delta_x_tilde_B = delta_x_tilde_B,
delta_x_tilde_B_us = delta_x_tilde_B_us,
x_ss_L = x_ss_L,
mispredicthour = mispredicthour,
delta_x_L_multiple = delta_x_L_multiple,
delta_B_multiple = delta_B_multiple,
delta_x_cap_omega = delta_x_cap_omega,
delta_x_L_omega = delta_x_L_omega,
x_ss_L_effect_res = x_ss_L_effect_res,
x_ss_L_effect_eta_high_res = x_ss_L_effect_eta_high_res,
x_ss_L_effect_eta_low_res = x_ss_L_effect_eta_low_res,
x_ss_B_res = x_ss_B_res,
delta_x_cap_res = delta_x_cap_res,
delta_x_cap_us_res = delta_x_cap_us_res,
delta_x_tilde_L_res = delta_x_tilde_L_res,
delta_x_tilde_L_us_res = delta_x_tilde_L_us_res,
delta_x_tilde_B_res = delta_x_tilde_B_res,
delta_x_tilde_B_us_res = delta_x_tilde_B_us_res,
x_ss_L_res = x_ss_L_res,
delta_x_cap_multiple_res = delta_x_cap_multiple_res,
delta_x_L_multiple_res = delta_x_L_multiple_res,
delta_B_multiple_res = delta_B_multiple_res,
delta_x_cap_omega_res = delta_x_cap_omega_res,
delta_x_L_omega_res = delta_x_L_omega_res,
x_ss_zero_temp = x_ss_zero_temp
)
return(solution)
}
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Calculate effects from different counterfactuals (heterogeneous)
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
solve_counterfactuals <- function(param, df){
# Define
alpha <- param[['alpha']]
alpha_res <- param[['alpha_res']]
rho <- param[['rho']]
rho_res <- param[['rho_res']]
eta <- param[['eta']]
eta_res <- param[['eta_res']]
zeta <- param[['zeta']]
zeta_res <- param[['zeta_res']]
lambda <- param[['lambda']]
lambda_res <- param[['lambda_res']]
delta <- param[['delta']]
mispredict <- param[['mispredict']]
x_ss <- param[['x_ss']]
gamma_L_effect <- param[['gamma_L_effect']]
gamma_tilde_L_effect <- param[['gamma_tilde_L_effect']]
gamma_tilde_L_effect_res <- param[['gamma_tilde_L_effect_res']]
intercept_het_L_effect <- param[['intercept_het_L_effect']]
intercept_het_L_effect_eta_high <- param[['intercept_het_L_effect_eta_high']]
intercept_het_L_effect_eta_low <- param[['intercept_het_L_effect_eta_low']]
gamma_L_effect_res <- param[['gamma_L_effect_res']]
intercept_het_L_effect_res <- param[['intercept_het_L_effect_res']]
intercept_het_L_effect_eta_high_res <- param[['intercept_het_L_effect_eta_high_res']]
intercept_het_L_effect_eta_low_res <- param[['intercept_het_L_effect_eta_low_res']]
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Calculate effects for unrestricted model
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Remember the order of the arguments for calculate steady state is
# calculate_steady_state(param, gamma_tilde, gamma, alpha, rho, mispredict, eta, zeta, intercept=NA, eta_scale=1){
x_ss_L_effect <- max(calculate_steady_state(param, gamma_tilde_L_effect, gamma_L_effect, alpha, rho, lambda, mispredict, eta, zeta, intercept=intercept_het_L_effect) ,0)
x_ss_temptation <- max(calculate_steady_state(param, 0, 0, alpha, rho, lambda, 0, eta, zeta, intercept=intercept_het_L_effect) ,0)
x_ss_temptation_eta_high <- max(calculate_steady_state(param, 0, 0, alpha, rho, lambda, 0, eta, zeta, intercept=intercept_het_L_effect_eta_high, eta_scale=1.1) ,0)
x_ss_temptation_eta_low <- max(calculate_steady_state(param, 0, 0, alpha, rho, lambda, 0, eta, zeta, intercept=intercept_het_L_effect_eta_low, eta_scale=0.9) ,0)
x_ss_habit <- max(calculate_steady_state(param, gamma_tilde_L_effect, gamma_L_effect, alpha, 0, lambda, mispredict, eta, zeta, intercept=intercept_het_L_effect) ,0)
x_ss_habit_eta_high <- max(calculate_steady_state(param, gamma_tilde_L_effect, gamma_L_effect, alpha, 0, lambda, mispredict, eta, zeta, intercept=intercept_het_L_effect_eta_high, eta_scale=1.1) ,0)
x_ss_habit_eta_low <- max(calculate_steady_state(param, gamma_tilde_L_effect, gamma_L_effect, alpha, 0, lambda, mispredict, eta, zeta, intercept=intercept_het_L_effect_eta_low, eta_scale=0.9) ,0)
x_ss_naivete <- max(calculate_steady_state(param, gamma_L_effect, gamma_L_effect, alpha, rho, lambda, 0, eta, zeta, intercept=intercept_het_L_effect) ,0)
x_ss_naivete_eta_high <- max(calculate_steady_state(param, gamma_L_effect, gamma_L_effect, alpha, rho, lambda, 0, eta, zeta, intercept=intercept_het_L_effect_eta_high, eta_scale=1.1) ,0)
x_ss_naivete_eta_low <- max(calculate_steady_state(param, gamma_L_effect, gamma_L_effect, alpha, rho, lambda, 0, eta, zeta, intercept=intercept_het_L_effect_eta_low, eta_scale=0.9) ,0)
x_ss_habit_temptation <- max(calculate_steady_state(param, 0, 0, alpha, 0, lambda, 0, eta, zeta, intercept=intercept_het_L_effect) ,0)
x_ss_habit_temptation_eta_high <- max(calculate_steady_state(param, 0, 0, alpha, 0, lambda, 0, eta, zeta, intercept=intercept_het_L_effect_eta_high, eta_scale=1.1) ,0)
x_ss_habit_temptation_eta_low <- max(calculate_steady_state(param, 0, 0, alpha, 0, lambda, 0, eta, zeta, intercept=intercept_het_L_effect_eta_low, eta_scale=0.9) ,0)
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Calculate effects for restricted alpha=1 model
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Remember the order of the arguments for calculate steady state is
# calculate_steady_state(param, gamma_tilde, gamma, alpha, rho, mispredict, eta, zeta, intercept=NA, eta_scale=1){
x_ss_L_effect_res <- max(calculate_steady_state(param, gamma_tilde_L_effect_res, gamma_L_effect_res, alpha_res, rho_res, lambda_res, mispredict, eta_res, zeta_res, intercept=intercept_het_L_effect_res) ,0)
x_ss_temptation_res <- max(calculate_steady_state(param, 0, 0, alpha_res, rho_res, lambda_res, 0, eta_res, zeta_res, intercept=intercept_het_L_effect_res) ,0)
x_ss_temptation_eta_high_res <- max(calculate_steady_state(param, 0, 0, alpha_res, rho_res, lambda_res, 0, eta_res, zeta_res, intercept=intercept_het_L_effect_eta_high_res, eta_scale=1.1) ,0)
x_ss_temptation_eta_low_res <- max(calculate_steady_state(param, 0, 0, alpha_res, rho_res, lambda_res, 0, eta_res, zeta_res, intercept=intercept_het_L_effect_eta_low_res, eta_scale=0.9) ,0)
x_ss_habit_res <- max(calculate_steady_state(param, gamma_tilde_L_effect_res, gamma_L_effect_res, alpha_res, 0, lambda_res, mispredict, eta_res, zeta_res, intercept=intercept_het_L_effect_res) ,0)
x_ss_habit_eta_high_res <- max(calculate_steady_state(param, gamma_tilde_L_effect_res, gamma_L_effect_res, alpha_res, 0, lambda_res, mispredict, eta_res, zeta_res, intercept=intercept_het_L_effect_eta_high_res, eta_scale=1.1) ,0)
x_ss_habit_eta_low_res <- max(calculate_steady_state(param, gamma_tilde_L_effect_res, gamma_L_effect_res, alpha_res, 0, lambda_res, mispredict, eta_res, zeta_res, intercept=intercept_het_L_effect_eta_low_res, eta_scale=0.9) ,0)
x_ss_naivete_res <- max(calculate_steady_state(param, gamma_L_effect_res, gamma_L_effect_res, alpha_res, rho_res, lambda_res, 0, eta_res, zeta_res, intercept=intercept_het_L_effect_res) ,0)
x_ss_naivete_eta_high_res <- max(calculate_steady_state(param, gamma_L_effect_res, gamma_L_effect_res, alpha_res, rho_res, lambda_res, 0, eta_res, zeta_res, intercept=intercept_het_L_effect_eta_high_res, eta_scale=1.1) ,0)
x_ss_naivete_eta_low_res <- max(calculate_steady_state(param, gamma_L_effect_res, gamma_L_effect_res, alpha_res, rho_res, lambda_res, 0, eta_res, zeta_res, intercept=intercept_het_L_effect_eta_low_res, eta_scale=0.9) ,0)
x_ss_habit_temptation_res <- max(calculate_steady_state(param, 0, 0, alpha_res, 0, lambda_res, 0, eta_res, zeta_res, intercept=intercept_het_L_effect_res) ,0)
x_ss_habit_temptation_eta_high_res <- max(calculate_steady_state(param, 0, 0, alpha_res, 0, lambda_res, 0, eta_res, zeta_res, intercept=intercept_het_L_effect_eta_high_res, eta_scale=1.1) ,0)
x_ss_habit_temptation_eta_low_res <- max(calculate_steady_state(param, 0, 0, alpha_res, 0, lambda_res, 0, eta_res, zeta_res, intercept=intercept_het_L_effect_eta_low_res, eta_scale=0.9) ,0)
# Return
solution <- list(
delta_x_temptation = x_ss_L_effect - x_ss_temptation,
delta_x_naivete = x_ss_L_effect - x_ss_naivete,
delta_x_habit = x_ss_L_effect - x_ss_habit,
delta_x_temptation_wo_habit = x_ss_habit - x_ss_habit_temptation,
delta_x_temptation_wo_habit_res = x_ss_habit_res - x_ss_habit_temptation_res,
x_ss_temptation = x_ss_temptation,
x_ss_temptation_eta_high = x_ss_temptation_eta_high,
x_ss_temptation_eta_low = x_ss_temptation_eta_low,
x_ss_naivete = x_ss_naivete,
x_ss_naivete_eta_high = x_ss_naivete_eta_high,
x_ss_naivete_eta_low = x_ss_naivete_eta_low,
x_ss_habit = x_ss_habit,
x_ss_habit_eta_high = x_ss_habit_eta_high,
x_ss_habit_eta_low = x_ss_habit_eta_low,
x_ss_habit_temptation = x_ss_habit_temptation,
x_ss_habit_temptation_eta_high = x_ss_habit_temptation_eta_high,
x_ss_habit_temptation_eta_low = x_ss_habit_temptation_eta_low,
delta_x_temptation_res = x_ss_L_effect_res - x_ss_temptation_res,
delta_x_naivete_res = x_ss_L_effect_res - x_ss_naivete_res,
delta_x_habit_res = x_ss_L_effect_res - x_ss_habit_res,
x_ss_temptation_res = x_ss_temptation_res,
x_ss_temptation_eta_high_res = x_ss_temptation_eta_high_res,
x_ss_temptation_eta_low_res = x_ss_temptation_eta_low_res,
x_ss_naivete_res = x_ss_naivete_res,
x_ss_naivete_eta_high_res = x_ss_naivete_eta_high_res,
x_ss_naivete_eta_low_res = x_ss_naivete_eta_low_res,
x_ss_habit_res = x_ss_habit_res,
x_ss_habit_eta_high_res = x_ss_habit_eta_high_res,
x_ss_habit_eta_low_res = x_ss_habit_eta_low_res,
x_ss_habit_temptation_res = x_ss_habit_temptation_res,
x_ss_habit_temptation_eta_high_res = x_ss_habit_temptation_eta_high_res,
x_ss_habit_temptation_eta_low_res = x_ss_habit_temptation_eta_low_res)
return(solution)
}
solve_effects_individual <- function(x_ss_i_data, param, tau_tilde_L, tau_L_2, w){
rho <- param[['rho']]
lambda <- param[['lambda']]
rho_res <- param[['rho_res']]
lambda_res <- param[['lambda_res']]
delta <- param[['delta']]
alpha <- param[['alpha']]
alpha_res <- param[['alpha_res']]
omega <- param[['omega']]
omega_est <- param[['omega_est']]
mispredict <- param[['mispredict']]
d_L <- param[['d_L']]
d_CL <- param[['d_CL']]
eta <- param[['eta']]
zeta <- param[['zeta']]
naivete <- param[['naivete']]
gamma_L_effect <- param[['gamma_L_effect']]
gamma_tilde_L_effect <- param[['gamma_tilde_L_effect']]
gamma_tilde_L_effect_omega <- param[['gamma_tilde_L_effect_omega']]
gamma_L_effect_omega <- param[['gamma_L_effect_omega']]
gamma_L <- param[['gamma_L']]
gamma_tilde_L <- param[['gamma_tilde_L']]
gamma_tilde_L_omega <- param[['gamma_tilde_L_omega']]
gamma_L_omega <- param[['gamma_L_omega']]
gamma_tilde_L_multiple <- param[['gamma_tilde_L_multiple']]
gamma_L_multiple <- param[['gamma_L_multiple']]
gamma_B <- param[['gamma_B']]
gamma_tilde_B <- param[['gamma_tilde_B']]
gamma_tilde_B_multiple <- param[['gamma_tilde_B_multiple']]
gamma_B_multiple <- param[['gamma_B_multiple']]
eta_res <- param[['eta_res']]
zeta_res <- param[['zeta_res']]
naivete_res <- param[['naivete_res']]
gamma_L_effect_res <- param[['gamma_L_effect_res']]
gamma_tilde_L_effect_res <- param[['gamma_tilde_L_effect_res']]
gamma_tilde_L_effect_omega_res <- param[['gamma_tilde_L_effect_omega_res']]
gamma_L_effect_omega_res <- param[['gamma_L_effect_omega_res']]
gamma_tilde_L_effect_multiple_res <- param[['gamma_tilde_L_effect_multiple_res']]
gamma_L_effect_multiple_res <- param[['gamma_L_effect_multiple_res']]
gamma_L_res <- param[['gamma_L_res']]
gamma_L_omega_res <- param[['gamma_L_omega_res']]
gamma_L_multiple_res <- param[['gamma_L_multiple_res']]
gamma_B_res <- param[['gamma_B_res']]
gamma_B_multiple_res <- param[['gamma_B_multiple_res']]
tau_L_2_signed <- param[['tau_L_2']]*-1
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Calculate individual intercepts and steady states under different strategies - Unrestricted alpha
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Gamma-spec
num <- eta*tau_L_2/omega - (1-alpha)*delta*rho*(((eta-zeta)*tau_tilde_L/omega+zeta*rho*tau_L_2/omega) + (1+lambda)*mispredict*(-eta+(1-alpha)*delta*rho^2*((eta-zeta)*lambda+zeta)))
denom <- 1 - (1-alpha)*delta*rho*(1+lambda)
num_omega <- eta*tau_L_2/omega_est - (1-alpha)*delta*rho*(((eta-zeta)*tau_tilde_L/omega_est+zeta*rho*tau_L_2/omega) + (1+lambda)*mispredict*(-eta+(1-alpha)*delta*rho^2*((eta-zeta)*lambda+zeta)))
gamma_spec <- num/denom
gamma_spec_omega <- num_omega/denom
gamma_tilde_spec <- gamma_spec - naivete
gamma_tilde_spec_omega <- gamma_spec_omega - naivete
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Calculate individual intercepts and steady states under different strategies
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
intercept_spec <- calculate_intercept_spec(x_ss_i_data, param, gamma_tilde_spec, gamma_spec, alpha, rho, lambda, mispredict, eta, zeta)
intercept_het_L_effect <- calculate_intercept_spec(x_ss_i_data, param, gamma_tilde_L_effect, gamma_L_effect, alpha, rho, lambda, mispredict, eta, zeta)
intercept_het_B <- calculate_intercept_spec(x_ss_i_data, param, gamma_tilde_B, gamma_B, alpha, rho, lambda, mispredict, eta, zeta)
intercept_het_L <- calculate_intercept_spec(x_ss_i_data, param, gamma_tilde_L, gamma_L, alpha, rho, lambda, mispredict, eta, zeta)
intercept_spec_omega <- calculate_intercept_spec(x_ss_i_data, param, gamma_tilde_spec_omega, gamma_spec_omega, alpha, rho, lambda, mispredict, eta, zeta)
intercept_het_L_effect_omega <- calculate_intercept_spec(x_ss_i_data, param, gamma_tilde_L_effect_omega, gamma_L_effect_omega, alpha, rho, lambda, mispredict, eta, zeta)
intercept_het_L_omega <- calculate_intercept_spec(x_ss_i_data, param, gamma_tilde_L_omega, gamma_L_omega, alpha, rho, lambda, mispredict, eta, zeta)
intercept_het_B_multiple <- calculate_intercept_spec(x_ss_i_data, param, gamma_tilde_B_multiple, gamma_B_multiple, alpha, rho, lambda, mispredict, eta, zeta)
intercept_het_L_multiple <- calculate_intercept_spec(x_ss_i_data, param, gamma_tilde_L_multiple, gamma_L_multiple, alpha, rho, lambda, mispredict, eta, zeta)
intercept_het_L_effect_eta_high <- calculate_intercept_spec(x_ss_i_data, param, gamma_tilde_L_effect, gamma_L_effect, alpha, rho, lambda, mispredict, eta, zeta, eta_scale=1.1)
intercept_het_L_effect_eta_low <- calculate_intercept_spec(x_ss_i_data, param, gamma_tilde_L_effect, gamma_L_effect, alpha, rho, lambda, mispredict, eta, zeta, eta_scale=0.9)
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Calculate individual counterfactuals
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
x_ss_spec <- calculate_steady_state(param, gamma_tilde_spec, gamma_spec, alpha, rho, lambda, mispredict, eta, zeta, intercept_spec)
x_ss_zero_un <- calculate_steady_state(param, 0, 0, alpha, rho, lambda, 0, eta, zeta, intercept_spec)
x_ss_zero <- ifelse(x_ss_zero_un<0, 0, x_ss_zero_un)
delta_x <- x_ss_spec - x_ss_zero
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Calculate individual intercepts and steady states under different strategies - Restricted alpha
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Gamma-spec
gamma_spec_res <- eta_res*tau_L_2
gamma_spec_omega_res <- eta_res*tau_L_2/omega_est
gamma_tilde_spec_res <- gamma_spec_res - naivete_res
gamma_tilde_spec_omega_res <- gamma_spec_omega_res - naivete_res
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Calculate individual intercepts and steady states under different strategies
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
intercept_spec_res <- calculate_intercept_spec(x_ss_i_data, param, gamma_tilde_spec_res, gamma_spec_res, alpha_res, rho_res, lambda_res, mispredict, eta = eta_res, zeta = zeta_res)
intercept_het_L_effect_res <- calculate_intercept_spec(x_ss_i_data, param, gamma_tilde_L_effect_res, gamma_L_effect_res, alpha_res, rho_res, lambda_res, mispredict, eta = eta_res, zeta = zeta_res)
intercept_het_B_res <- calculate_intercept_spec(x_ss_i_data, param, gamma_tilde_B, gamma_B_res, alpha_res, rho_res, lambda_res, mispredict, eta = eta_res, zeta = zeta_res)
intercept_het_L_res <- calculate_intercept_spec(x_ss_i_data, param, gamma_tilde_L, gamma_L_res, alpha_res, rho_res, lambda_res, mispredict, eta = eta_res, zeta = zeta_res)
intercept_spec_omega_res <- calculate_intercept_spec(x_ss_i_data, param, gamma_tilde_spec_omega_res, gamma_spec_omega_res, alpha_res, rho_res, lambda_res, mispredict, eta = eta_res, zeta = zeta_res)
intercept_het_L_effect_omega_res <- calculate_intercept_spec(x_ss_i_data, param, gamma_tilde_L_effect_omega_res, gamma_L_effect_omega_res, alpha_res, rho_res, lambda_res, mispredict, eta = eta_res, zeta = zeta_res)
intercept_het_L_omega_res <- calculate_intercept_spec(x_ss_i_data, param, gamma_tilde_L_omega, gamma_L_omega_res, alpha_res, rho_res, lambda_res, mispredict, eta = eta_res, zeta = zeta_res)
intercept_het_L_effect_multiple_res <- calculate_intercept_spec(x_ss_i_data, param, gamma_tilde_L_effect_multiple_res, gamma_tilde_L_effect_multiple_res, alpha_res, rho_res, lambda_res, mispredict, eta = eta_res, zeta = zeta_res)
intercept_het_B_multiple_res <- calculate_intercept_spec(x_ss_i_data, param, gamma_tilde_B_multiple, gamma_B_multiple_res, alpha_res, rho_res, lambda_res, mispredict, eta = eta_res, zeta = zeta_res)
intercept_het_L_multiple_res <- calculate_intercept_spec(x_ss_i_data, param, gamma_tilde_L_multiple, gamma_L_multiple_res, alpha_res, rho_res, lambda_res, mispredict, eta = eta_res, zeta = zeta_res)
intercept_het_L_effect_eta_high_res <- calculate_intercept_spec(x_ss_i_data, param, gamma_tilde_L_effect_res, gamma_L_effect_res, alpha_res, rho_res, lambda_res, mispredict, eta = eta_res, zeta = zeta_res, eta_scale=1.1)
intercept_het_L_effect_eta_low_res <- calculate_intercept_spec(x_ss_i_data, param, gamma_tilde_L_effect_res, gamma_L_effect_res, alpha_res, rho_res, lambda_res, mispredict, eta = eta_res, zeta = zeta_res, eta_scale=0.9)
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Calculate individual counterfactuals
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
x_ss_spec_res <- calculate_steady_state(param, gamma_tilde_spec_res, gamma_spec_res, alpha_res, rho_res, lambda_res, mispredict, eta = eta_res, zeta = zeta_res, intercept_spec_res)
x_ss_zero_un_res <- calculate_steady_state(param, 0, 0, alpha_res, rho_res, lambda_res, 0, eta = eta_res, zeta = zeta_res, intercept_spec_res)
x_ss_zero_res <- ifelse(x_ss_zero_un_res<0, 0, x_ss_zero_un_res)
delta_x_res <- x_ss_spec_res - x_ss_zero_res
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Compute population averages
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
x_ss_spec_w <- weighted.mean(x_ss_spec, w, na.rm=T)
gamma_tilde_spec_w <- weighted.mean(gamma_tilde_spec, w, na.rm=T)
gamma_spec_w <- weighted.mean(gamma_spec, w, na.rm=T)
gamma_spec_omega_w <- weighted.mean(gamma_spec_omega, w, na.rm=T)
delta_x_spec <- weighted.mean(delta_x, w, na.rm=T)
x_ss_i_data <- weighted.mean(x_ss_i_data, w, na.rm=T)
intercept_spec <- weighted.mean(intercept_spec, w, na.rm=T)
intercept_spec_omega <- weighted.mean(intercept_spec_omega, w, na.rm=T)
intercept_het_L_effect_omega <- weighted.mean(intercept_het_L_effect_omega, w, na.rm=T)
intercept_het_L_effect <- weighted.mean(intercept_het_L_effect, w, na.rm=T)
intercept_het_L_effect_eta_high <- weighted.mean(intercept_het_L_effect_eta_high, w, na.rm=T)
intercept_het_L_effect_eta_low <- weighted.mean(intercept_het_L_effect_eta_low, w, na.rm=T)
intercept_het_L_omega <- weighted.mean(intercept_het_L_omega, w, na.rm=T)
intercept_het_B <- weighted.mean(intercept_het_B, w, na.rm=T)
intercept_het_L <- weighted.mean(intercept_het_L, w, na.rm=T)
intercept_het_B_multiple <- weighted.mean(intercept_het_B_multiple, w, na.rm=T)
intercept_het_L_multiple <- weighted.mean(intercept_het_L_multiple, w, na.rm=T)
x_ss_spec_w_res <- weighted.mean(x_ss_spec_res, w, na.rm=T)
gamma_tilde_spec_w_res <- weighted.mean(gamma_tilde_spec_res, w, na.rm=T)
gamma_spec_w_res <- weighted.mean(gamma_spec_res, w, na.rm=T)
gamma_spec_omega_w_res <- weighted.mean(gamma_spec_omega_res, w, na.rm=T)
delta_x_spec_res <- weighted.mean(delta_x_res, w, na.rm=T)
intercept_spec_res <- weighted.mean(intercept_spec_res, w, na.rm=T)
intercept_spec_omega_res <- weighted.mean(intercept_spec_omega_res, w, na.rm=T)
intercept_het_L_effect_omega_res <- weighted.mean(intercept_het_L_effect_omega_res, w, na.rm=T)
intercept_het_L_effect_res <- weighted.mean(intercept_het_L_effect_res, w, na.rm=T)
intercept_het_L_effect_eta_high_res <- weighted.mean(intercept_het_L_effect_eta_high_res, w, na.rm=T)
intercept_het_L_effect_eta_low_res <- weighted.mean(intercept_het_L_effect_eta_low_res, w, na.rm=T)
intercept_het_L_omega_res <- weighted.mean(intercept_het_L_omega_res, w, na.rm=T)
intercept_het_B_res <- weighted.mean(intercept_het_B_res, w, na.rm=T)
intercept_het_L_res <- weighted.mean(intercept_het_L_res, w, na.rm=T)
intercept_het_L_effect_multiple_res <- weighted.mean(intercept_het_L_effect_multiple_res, w, na.rm=T)
intercept_het_B_multiple_res <- weighted.mean(intercept_het_B_multiple_res, w, na.rm=T)
intercept_het_L_multiple_res <- weighted.mean(intercept_het_L_multiple_res, w, na.rm=T)
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Compute hourly variables
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
intercept_spec_hour <- intercept_spec*60*-1
gamma_tilde_spec_hour <- gamma_tilde_spec_w*60
gamma_spec_hour <- gamma_spec_w*60
gamma_spec_omega_hour <- gamma_spec_omega_w*60
intercept_het_B_hour <- intercept_het_B*60*-1
intercept_het_L_hour <- intercept_het_L*60*-1
intercept_het_L_effect_hour <- intercept_het_L_effect*60*-1
intercept_spec_omega_hour <- intercept_spec_omega*60*-1
intercept_het_L_effect_omega_hour <- intercept_het_L_effect_omega*60*-1
intercept_het_L_omega_hour <- intercept_het_L_omega*60*-1
intercept_spec_res_hour <- intercept_spec_res*60*-1
gamma_tilde_spec_res_hour <- gamma_tilde_spec_w_res*60
gamma_spec_res_hour <- gamma_spec_w_res*60
gamma_spec_omega_res_hour <- gamma_spec_omega_w_res*60
intercept_het_B_res_hour <- intercept_het_B_res*60*-1
intercept_het_L_res_hour <- intercept_het_L_res*60*-1
intercept_het_L_effect_res_hour <- intercept_het_L_effect_res*60*-1
intercept_spec_omega_res_hour <- intercept_spec_omega_res*60*-1
intercept_het_L_effect_omega_res_hour <- intercept_het_L_effect_omega_res*60*-1
intercept_het_L_omega_res_hour <- intercept_het_L_omega_res*60*-1
# Return
solution <- list(
x_ss_i_data = x_ss_i_data,
x_ss_spec = x_ss_spec_w,
tau_L_2_signed = tau_L_2_signed,
intercept_spec = intercept_spec,
intercept_spec_hour = intercept_spec_hour,
gamma_tilde_spec = gamma_tilde_spec_w,
gamma_spec = gamma_spec_w,
delta_x_spec = delta_x_spec,
gamma_tilde_spec_hour = gamma_tilde_spec_hour,
gamma_spec_hour = gamma_spec_hour,
intercept_het_L_effect = intercept_het_L_effect,
intercept_het_L_effect_eta_high = intercept_het_L_effect_eta_high,
intercept_het_L_effect_eta_low = intercept_het_L_effect_eta_low,
intercept_het_B = intercept_het_B,
intercept_het_L = intercept_het_L,
intercept_het_B_hour = intercept_het_B_hour,
intercept_het_L_hour = intercept_het_L_hour,
intercept_het_L_effect_hour = intercept_het_L_effect_hour,
gamma_spec_omega_hour = gamma_spec_omega_hour,
intercept_spec_omega_hour = intercept_spec_omega_hour,
intercept_het_L_effect_omega_hour = intercept_het_L_effect_omega_hour,
intercept_het_L_omega_hour = intercept_het_L_omega_hour,
intercept_het_B_multiple = intercept_het_B_multiple,
intercept_het_L_multiple = intercept_het_L_multiple,
intercept_het_L_effect_omega = intercept_het_L_effect_omega,
intercept_het_L_omega = intercept_het_L_omega,
x_ss_spec_res = x_ss_spec_w_res,
intercept_spec_res = intercept_spec_res,
intercept_spec_res_hour = intercept_spec_res_hour,
gamma_tilde_spec_res = gamma_tilde_spec_w_res,
gamma_spec_res = gamma_spec_w_res,
delta_x_spec_res = delta_x_spec_res,
gamma_tilde_spec_res_hour = gamma_tilde_spec_res_hour,
gamma_spec_res_hour = gamma_spec_res_hour,
intercept_het_L_effect_res = intercept_het_L_effect_res,
intercept_het_L_effect_eta_high_res = intercept_het_L_effect_eta_high_res,
intercept_het_L_effect_eta_low_res = intercept_het_L_effect_eta_low_res,
intercept_het_B_res = intercept_het_B_res,
intercept_het_L_res = intercept_het_L_res,
intercept_het_B_res_hour = intercept_het_B_res_hour,
intercept_het_L_res_hour = intercept_het_L_res_hour,
intercept_het_L_effect_res_hour = intercept_het_L_effect_res_hour,
gamma_spec_omega_res_hour = gamma_spec_omega_res_hour,
intercept_spec_omega_res_hour = intercept_spec_omega_res_hour,
intercept_het_L_effect_omega_res_hour = intercept_het_L_effect_omega_res_hour,
intercept_het_L_omega_res_hour = intercept_het_L_omega_res_hour,
intercept_het_L_effect_multiple_res = intercept_het_L_effect_multiple_res,
intercept_het_B_multiple_res = intercept_het_B_multiple_res,
intercept_het_L_multiple_res = intercept_het_L_multiple_res,
intercept_het_L_effect_omega_res = intercept_het_L_effect_omega_res,
intercept_het_L_omega_res = intercept_het_L_omega_res
)
return(solution)
}
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Check functions
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
check_sys_eq_1 <- function(param) {
# Define
rho <- param[['rho']]
lambda <- param[['lambda']]
tau_B_2 <- param[['tau_B_2']]
tau_B_3 <- param[['tau_B_3']]
tau_B_4 <- param[['tau_B_4']]
tau_B_5 <- param[['tau_B_5']]
diff <- rho + lambda
# Rho
diff <- diff - tau_B_5 / (tau_B_4*(1 + lambda))
# Lambda
diff <- diff - tau_B_4 / (rho*tau_B_3 + rho^2*tau_B_2)
# Return
return(diff)
}
calculate_steady_state <- function(param, gamma_tilde, gamma, alpha, rho, lambda, mispredict, eta, zeta, intercept=NA, eta_scale=1){
eta <- eta * eta_scale
delta <- param[['delta']]
# Calculate
term1 <- intercept
term2 <- (1-alpha)*delta*rho
term3 <- (zeta-eta) * mispredict - gamma_tilde*(1+lambda)
num <- term1 + term2*term3 + gamma
terma <- -eta -((1-alpha)*delta*rho*(zeta-eta))
termb <- zeta*((rho - (1-alpha)*delta*(rho**2)) / (1-rho))
denom <- terma - termb
x_ss_calc <- num /denom
return(x_ss_calc)
}
calculate_x_ss_i_spec <- function(df){
x_ss_i_df <- df %>%
mutate(x_ss_i_data = PD_P1_UsageFITSBY)
return(x_ss_i_df$x_ss_i_data)
}
get_avg_use <- function(df){
estimate <- list(signif(weighted.mean(df$PD_P1_UsageFITSBY,df$w, na.rm=TRUE), digits=3))
names(estimate) <- c('avg_use')
# Return
return(estimate)
}
get_fb <- function(df){
df <- df %>%
mutate(F_B_uncensored = 50*PD_P1_UsageFITSBY/20) %>%
mutate(F_B_min = ifelse(F_B_uncensored<150, F_B_uncensored, 150)) %>%
mutate(F_B = F_B_min/num_days)
estimate <- list(signif(weighted.mean(df$F_B,df$w, na.rm=TRUE), digits=3))
names(estimate) <- c('F_B')
# Return
return(estimate)
}
calculate_intercept_spec <- function(x_ss_i_data, param, gamma_tilde, gamma, alpha, rho, lambda, mispredict, eta, zeta, eta_scale=1){
delta <- param[['delta']]
eta <- eta*eta_scale
terma <- (1-alpha)*delta*rho
termb <- (rho - terma*rho) / (1-rho)
term1 <- -eta -terma*(zeta-eta)-zeta*termb
term2 <- terma*((zeta-eta)*mispredict - (1+lambda)*gamma_tilde)
intercept <- x_ss_i_data*term1 - term2 - gamma
return(intercept)
}
check_steady_state <- function(param){
# Define
x_ss <- param[['x_ss']]
gamma_L_effect <- param[['gamma_L_effect']]
gamma_tilde_L_effect <- param[['gamma_tilde_L_effect']]
mispredict <- param[['mispredict']]
rho <- param[['rho']]
alpha <- param[['alpha']]
intercept_het_L_effect <- param[['intercept_het_L_effect']]
intercept_het_L_effect_eta_high <- param[['intercept_het_L_effect_eta_high']]
intercept_het_L_effect_eta_low <- param[['intercept_het_L_effect_eta_low']]
print('||||||||||||| Initial calculation |||||||||||||')
print(x_ss)
#print('||||||||||||| Checked calculation rho_tilde=0 |||||||||||||')
#print(calculate_steady_state(param, gamma_tilde_L_effect, gamma_L_effect, alpha, rho, mispredict, intercept = intercept_het_L_effect))
# print(paste0('alpha 1.0eta:', intercept_het_L_effect))
# print('||||||||||||| Checked calculation rho_tilde=0, eta*1.1 |||||||||||||')
# print(calculate_steady_state(param, gamma_tilde_L_effect, gamma_L_effect, alpha, rho, mispredict, intercept = intercept_het_L_effect_eta_high))
# print(paste0('alpha 1.1eta:', intercept_het_L_effect_eta_high))
# print('||||||||||||| Checked calculation rho_tilde=0, eta*0.9 |||||||||||||')
# print(calculate_steady_state(param, gamma_tilde_L_effect, gamma_L_effect, alpha, rho, mispredict, intercept = intercept_het_L_effect_eta_low))
# print(paste0('alpha 0.9eta:', intercept_het_L_effect_eta_low))
}
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Moment functions
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
get_bonus <- function(df, t) {
# Specify regression
dep_var <- sprintf('PD_P%s_UsageFITSBY', t)
if (t == 1) {
eq <- paste0(dep_var, '~ L + B + S')
} else if (t > 1) {
eq <- paste0(dep_var, '~ PD_P1_UsageFITSBY + L + B + S')
}
# Run regression
fit <- lm(data = df,
formula = eq,
weights = w)
estimate <- list(ifelse(fit$coefficients[['B']]>0, 0,fit$coefficients[['B']]))
names(estimate) <- c(sprintf('tau_B_%s', t))
# Return
return(estimate)
}
get_multiple_good_taus <- function(df) {
# Run regression
fit <- lm(data = df,
formula = S4_Substitution_W ~ L+B+S,
weights = w)
estimate <-
list(fit$coefficients[['L']],
fit$coefficients[['B']])
names(estimate) <- c('tau_L_y', 'tau_B_y')
return(estimate)
}
get_full_bonus_2 <- function(df, t) {
# Run regression
fit <- lm(data = df,
formula = 'PD_P2_UsageFITSBY ~ PD_P1_UsageFITSBY + L + B + S',
weights = w)
estimate <- list(ifelse(fit$coefficients[['B']]>0, 0,fit$coefficients[['B']]))
names(estimate) <- c('tau_B_2_full')
# Return
return(estimate)
}
get_bonus_2b <- function(df, full=FALSE) {
if (full){
fit <- lm(data = df,
formula = 'PD_P2_UsageFITSBY ~ PD_P1_UsageFITSBY + L + B + S',
weights = w)
estimate <- list(ifelse(fit$coefficients[['B']]>0, 0,fit$coefficients[['B']]))
names(estimate) <- c('tau_B_2')
} else{
days <- 33:42
fit <- lm(data = df %>%
mutate(PD_P2b_Usage_FITSBY =
rowSums(.[paste0('PD_DailyUsageFITSBY_',days)], na.rm=TRUE)/length(days)),
formula = 'PD_P2b_Usage_FITSBY ~ PD_P1_UsageFITSBY + L + B + S',
weights = w)
coef <- fit$coefficients[['B']]
coef <- ifelse(coef>0, 0, coef)
estimate <- list(coef)
names(estimate) <- c('tau_B_2')
}
# Return
return(estimate)
}
get_bonus_tilde <- function(df) {
# Specify regression
dep_var <- sprintf('S3_PredictUseNext_1_W')
eq <- paste0(dep_var, '~ PD_P1_UsageFITSBY + L + B + S')
# Run regression
fit <- lm(data = df,
formula = eq,
weights = w)
estimate <- list(fit$coefficients[['B']])
names(estimate) <- c(sprintf('tau_tilde_B_3_2', t))
# Return
return(estimate)
}
get_limit_tilde <- function(df) {
# Specify regression
eq <- 'S3_PredictUseNext_1_W ~ L + B + S'
# Run regression
fit <- lm(data = df,
formula = eq,
weights = w)
estimate <- list(ifelse(fit$coefficients[['L']]>0, 0,fit$coefficients[['L']]))
names(estimate) <- c('tau_tilde_L')
# Return
return(estimate)
}
get_true_limit_tilde <- function(df) {
# Specify regression
eq <- 'S3_PredictUseNext_1_W ~ L + B + S'
# Run regression
fit <- lm(data = df,
formula = eq,
weights = w)
estimate <- list(ifelse(fit$coefficients[['L']]>0, 0,fit$coefficients[['L']]))
names(estimate) <- c('true_tau_tilde_L')
# Return
return(estimate)
}
get_limit_tildes <- function(df, t) {
# Specify regression
dep_var <- sprintf('S2_PredictUseNext_%s_W', t-1)
eq <- paste0(dep_var, '~ PD_P1_UsageFITSBY + L + B + S')
# Run regression
fit <- lm(data = df,
formula = eq,
weights = w)
estimate <- list(ifelse(fit$coefficients[['L']]>0, 0,fit$coefficients[['L']]))
names(estimate) <- c(sprintf('tau_tilde_L_%s_2', t))
# Return
return(estimate)
}
get_limit_2b <- function(df) {
# Run regression
fit <- lm(data = df ,
formula = 'PD_P2_UsageFITSBY ~ PD_P1_UsageFITSBY + L + B + S',
weights = w)
estimate <- list(ifelse(fit$coefficients[['L']]>0, 0,fit$coefficients[['L']]))
names(estimate) <- c('tau_L_2')
# Return
return(estimate)
}
get_limit <- function(df) {
#Ryb regressuib
fit <- lm(data = df ,
formula = 'PD_P3_UsageFITSBY ~ PD_P1_UsageFITSBY + L + B + S',
weights = w)
estimate <- list(ifelse(fit$coefficients[['L']]>0, 0,fit$coefficients[['L']]))
names(estimate) <- c('tau_L_3')
# Return
return(estimate)
}
get_MPL_tilde <- function(df) {
regression_df <- df %>%
mutate(S2_reduction = PD_P3_UsageFITSBY - S2_PredictUseInitial_W) %>%
select(S2_reduction, S2_PredictUseInitial_W, S2_PredictUseBonus, PD_P3_UsageFITSBY, B, S, L, PD_P1_UsageFITSBY,w)
# Run regression
fit <- lm(data = regression_df,
formula = S2_reduction ~ B + S + L + PD_P1_UsageFITSBY,
weights = w)
estimate <- list(fit$coefficients[['B']])
names(estimate) <- c(sprintf('MPL_S2'))
# Return
return(estimate)
}
get_limit_last_week <- function(df) {
# Run regression
eq <- 'PD_WeeklyUsageFITSBY_15 ~ PD_P1_UsageFITSBY + L + B + S'
fit <- lm(data = df,
formula = eq,
weights = w)
estimate <- list(fit$coefficients[['L']])
names(estimate) <- c('limit_effect_last_week')
# Return
return(estimate)
}
get_limit_avg <- function(df) {
# Reshape data
df %<>%
select(
UserID,
w,
L,
B,
S,
PD_P1_UsageFITSBY,
PD_P2_UsageFITSBY,
PD_P3_UsageFITSBY,
PD_P4_UsageFITSBY,
PD_P5_UsageFITSBY
)
df %<>%
gather(
key = 'period',
value = 'usage',
-UserID,
-w,
-L,
-B,
-S,
-PD_P1_UsageFITSBY
)
# Run regression
eq <- 'usage ~ PD_P1_UsageFITSBY + L + B + S'
fit <- lm(data = df,
formula = eq,
weights = w)
estimate <- list(ifelse(fit$coefficients[['L']]>0, 0,fit$coefficients[['L']]))
names(estimate) <- c('tau_L')
# Return
return(estimate)
}
get_taus <- function(df, winsorize=F, full=full) {
# Bonus
bonus <-
3:5 %>%
map(~get_bonus(df, .)) %>%
list.flatten
# Bonus Tilde
bonus_tilde <- get_bonus_tilde(df)
# Bonus 2B
bonus_2b <- get_bonus_2b(df, full=full)
bonus_2_full <- get_full_bonus_2(df)
#Get MPL
MPL <- get_MPL_tilde(df)
# Limit
limit <- get_limit(df)
limit_tilde <- get_limit_tilde(df)
true_limit_tilde <- get_true_limit_tilde(df)
limit_tilde[[1]] <- limit[[1]]
# Limit Tilde
limit_tildes <-
2:3 %>%
map(~get_limit_tildes(df, .)) %>%
list.flatten
limit_2 <- get_limit_2b(df)
limit_avg <- get_limit_avg(df)
multiple_good_taus <- get_multiple_good_taus(df)
# Return
tau <- list.merge(bonus, bonus_tilde, bonus_2b, bonus_2_full, limit, limit_tilde, limit_tildes,
limit_2,limit_avg, true_limit_tilde, MPL, multiple_good_taus)
if (winsorize){
tau_tilde_limit <- -4
ratio_limit <- 0.18
tau$tau_tilde_L <- min(-2, tau$tau_tilde_L)
tau$tau_B_5 <- min(tau$tau_B_5, tau$tau_B_4 - ratio_limit * tau$tau_B_3)
}
return(tau)
}
get_mispredict <- function(df) {
# Filter to control
df_unfiltered <- df
df %<>% filter(B == 0, L == 0)
# Reshape data
names(df) <- gsub('PD_(.*)_UsageFITSBY', 'UsageActual_\\1', names(df))
names(df) <- gsub('S(.*)_PredictUseNext_1_W', 'UsagePredicted_P\\1', names(df))
df %<>%
select(
UserID,
w,
starts_with('UsagePredicted'),
starts_with('UsageActual')
) %>%
select(
UserID,
w,
ends_with('P2'),
ends_with('P3'),
ends_with('P4')
)
df %<>%
gather(key = 'period', value = 'value', -UserID, -w) %>%
separate(period, c('key', 'period'), sep = '_') %>%
spread(key = 'key', value = 'value')
# Get mean
df %<>% mutate(mispredict = UsageActual - UsagePredicted)
estimate <-
list(
weighted.mean(df$mispredict, df$w, na.rm = T),
weighted.mean(df_unfiltered$PD_P1_UsageFITSBY, df_unfiltered$w, na.rm=T),
weighted.mean(df$UsagePredicted, df$w, na.rm = T)
)
names(estimate) <- c('mispredict', 'x_ss', 'x_tilde')
# Return
return(estimate)
}
get_ideal <- function(df) {
# Reshape data
df %<>%
select(
UserID,
w,
L,
B,
S3_PhoneUseChange
)
df %<>% gather(key = 'key', value = 'value', -UserID, -w, -L, -B)
# Get mean
estimate <-
list(
weighted.mean(filter(df, L == 1)$value, filter(df, L == 1)$w, na.rm = T),
weighted.mean(filter(df, L == 0)$value, filter(df, L == 0)$w, na.rm = T)
)
names(estimate) <- c('d_L', 'd_CL')
# Return
return(estimate)
}
get_predict <- function(df) {
# Filter to control
df %<>% filter(B == 1)
# Reshape data
df %<>%
select(
UserID,
w,
ends_with('PredictUseInitial'),
ends_with('PredictUseBonus')
)
#reformats any winsorization
names(df) <- gsub('(.*)_W', '\\1', names(df))
df %<>%
gather(key = 'period', value = 'value', -UserID, -w) %>%
separate(period, c('period', 'key'), sep = '_') %>%
spread(key = 'key', value = 'value')
# Get mean
df %<>%
mutate(PredictUseBonus = PredictUseInitial * (1 - (PredictUseBonus / 100))) %>%
mutate(bonus_effect = PredictUseBonus - PredictUseInitial) %>%
mutate(average = (PredictUseBonus + PredictUseInitial) / 2)
estimate <- list(
weighted.mean(df$bonus_effect, df$w, na.rm = T),
weighted.mean(filter(df, period == 'S2')$average, filter(df, period == 'S2')$w, na.rm = T)
)
names(estimate) <- c('tau_tilde_B', 'x_tilde_2_B')
# Return
return(estimate)
}
get_wtp <- function(df) {
df %<>%
select(
UserID,
w,
ends_with('MPL'),
ends_with('MPLLimit')
)
df %<>%
gather(key = 'period', value = 'value', -UserID, -w) %>%
separate(period, c('period', 'key'), sep = '_') %>%
spread(key = 'key', value = 'value')
# Get mean
estimate <- list(
weighted.mean(df$MPL, df$w, na.rm = T) / num_days,
weighted.mean(df$MPLLimit, df$w, na.rm = T) / num_days
)
names(estimate) <- c('v_B', 'v_L')
# Return
return(estimate)
}
find_tau_spec <- function(df, save_reg=FALSE){
tau_data <- df %>%
select(
UserID,
w, L, B, S,
PD_P1_UsageFITSBY,
PD_P2_UsageFITSBY,
PD_P3_UsageFITSBY,
PD_P4_UsageFITSBY,
PD_P5_UsageFITSBY,
PD_P2_LimitTightFITSBY
)
tau_data %<>%
gather(
key = 'period',
value = 'usage',
-UserID,
-w, -L, -B, -S,
-PD_P1_UsageFITSBY,
-PD_P2_LimitTightFITSBY
)
fit <- tau_data %>%
mutate(tightness=ifelse(L,PD_P2_LimitTightFITSBY, 0)) %>%
lm(formula = 'usage ~ PD_P1_UsageFITSBY + L + tightness + B + S',
weights = w)
if(save_reg){
stargazer(fit,
omit.stat = c("adj.rsq","f","ser"),
covariate.labels = c("Period 1 Usage", "Limit Treatment",
"Limit Treatment x Period 2 Limit Tightness",
"Bonus Treatment"),
dep.var.labels = "FITSBY Usage (mins/day)",
title = "",
omit = c("S1", "S2", "S3", "S4",
"S5", "S6", "S7", "S8"),
type = "latex",
omit.table.layout = "n",
float = FALSE,
dep.var.caption = "",
star.cutoffs = NA,
out = "output/heterogeneity_reg.tex"
)
}
const <- fit$coefficients['L']
slope <- fit$coefficients['tightness']
return(const + slope * df$PD_P2_LimitTightFITSBY)
}
find_tau_L2_spec <- function(df, save_reg=FALSE){
tau_data <- df %>%
select(
UserID,
w, L, B, S,
PD_P1_UsageFITSBY,
PD_P2_UsageFITSBY,
PD_P3_UsageFITSBY,
PD_P4_UsageFITSBY,
PD_P5_UsageFITSBY,
PD_P2_LimitTightFITSBY
)
fit <- tau_data %>%
mutate(tightness=ifelse(L,PD_P2_LimitTightFITSBY, 0)) %>%
lm(formula = 'PD_P2_UsageFITSBY ~ PD_P1_UsageFITSBY + L + tightness + B + S',
weights = w)
const <- fit$coefficients['L']
slope <- fit$coefficients['tightness']
return(const + slope * df$PD_P2_LimitTightFITSBY)
}
find_tau_L3_spec <- function(df, save_reg=FALSE){
tau_data <- df %>%
select(
UserID,
w, L, B, S,
PD_P1_UsageFITSBY,
PD_P2_UsageFITSBY,
PD_P3_UsageFITSBY,
PD_P4_UsageFITSBY,
PD_P5_UsageFITSBY,
PD_P2_LimitTightFITSBY
)
fit <- tau_data %>%
mutate(tightness=ifelse(L,PD_P2_LimitTightFITSBY, 0)) %>%
lm(formula = 'PD_P3_UsageFITSBY ~ PD_P1_UsageFITSBY + L + tightness + B + S',
weights = w)
const <- fit$coefficients['L']
slope <- fit$coefficients['tightness']
return(const + slope * df$PD_P3_LimitTightFITSBY)
}
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Model functions
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
estimate_model <- function(df, param, winsorize=F, full=F, display_warning=FALSE) {
# Empirical moments
param %<>%
list.merge(
#get_opt(df),
get_taus(df, winsorize=winsorize, full=full),
get_mispredict(df),
get_ideal(df),
get_predict(df),
get_wtp(df),
get_avg_use(df),
get_fb(df),
get_limit_last_week(df)
)
# Solve system of equation #1
param %<>%
solve_sys_eq_1 %>%
as.list %>%
list.merge(param)
# Solve system of equations #2
param %<>%
solve_sys_eq_2(display_warning=display_warning) %>%
as.list %>%
list.merge(param)
# Solve system of equations #3
param %<>%
solve_sys_eq_3 %>%
as.list %>%
list.merge(param)
# Solve for individual effects
tau_L_2_spec <- find_tau_L2_spec(df)
tau_tilde_spec <- find_tau_L3_spec(df)
x_ss_i_data <- calculate_x_ss_i_spec(df)
param %<>%
solve_effects_individual(x_ss_i_data= x_ss_i_data, tau_tilde_L=tau_tilde_spec, tau_L_2=tau_L_2_spec, w=df$w)%>%
as.list %>%
list.merge(param)
# Solve for effects
param %<>%
solve_effects(df) %>%
as.list %>%
list.merge(param)
# Solve for counterfactuals
param %<>%
solve_counterfactuals(df) %>%
as.list %>%
list.merge(param)
# Return
return(param)
}
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Plotting functions
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
plot_time_effects <- function(median, bottom, top, filename=""){
order_results_time <- function(df1){
r <- c(df1[['delta_x_cap_res']],
df1[['delta_x_tilde_B_res']],
df1[['delta_x_tilde_L_res']],
df1[['delta_x_spec_res']])
return(r)
}
order_results_time_est <- function(df1){
r <- c(df1[['delta_x_cap']],
df1[['delta_x_tilde_B']],
df1[['delta_x_tilde_L']],
df1[['delta_x_spec']])
return(r)
}
x <- c('Limit\n effect', 'Bonus \n valuation', 'Limit \n valuation', 'Heterogeneous\n limit effect')
names <- rep(factor(x, levels=x),2)
counterfactuals_zero <- order_results_time(param_full)
lower_counterfactuals_zero <- order_results_time(bottom)
upper_counterfactuals_zero <- order_results_time(top)
counterfactuals_est <- order_results_time_est(param_full)
lower_counterfactuals_est <- order_results_time_est(bottom)
upper_counterfactuals_est <- order_results_time_est(top)
df <- data.frame(names, counterfactuals_zero, lower_counterfactuals_zero, upper_counterfactuals_zero,
counterfactuals_est, lower_counterfactuals_est, upper_counterfactuals_est)
cols <- c("No projection bias"=maroon,
"Perceived projection bias"=grey)
a <- ggplot(df, aes(x=names, width=.2)) +
geom_point(aes(y=counterfactuals_zero, colour="No projection bias"), stat="identity", position = position_nudge(x = -.1)) +
geom_point(aes(y=counterfactuals_est, colour="Perceived projection bias"), stat="identity", position = position_nudge(x = .1))+
geom_errorbar(aes(ymin=lower_counterfactuals_zero, ymax=upper_counterfactuals_zero), stat="identity", colour=maroon, position = position_nudge(x = -.1)) +
geom_errorbar(aes(ymin=lower_counterfactuals_est, ymax=upper_counterfactuals_est), stat="identity", colour=grey, position=position_nudge(x = .1)) +
scale_y_continuous(name="Time effect (minutes/day)") +
theme_classic() +
labs(x = "") +
scale_colour_manual(name = "", values=cols,
labels = unname(TeX(c(paste0("Zero projection bias", " (", "$\\alpha=1$", ")"),
paste0("Estimated projection bias", " (" ,"$\\alpha=\\hat{\\alpha}$", ")")
))))+
theme(legend.text.align = 0,
legend.key.height = unit(1, "cm"),
legend.position="bottom") +
theme(legend.margin=margin(0,0,0,0),
legend.box.margin=margin(-10,-10,-10,-10)) +
theme(axis.text.x = element_text(colour="black"))
coord_cartesian(ylim = c(0, 180))
ggsave(sprintf('output/%s.pdf', filename), plot=a, width=6.5, height=4.5, units="in")
}
plot_time_effects_bal <- function(param_full, param_balanced, bottom, top, bottom_bal, top_bal, filename=""){
order_results_time <- function(df1){
r <- c(df1[['delta_x_cap_res']],
df1[['delta_x_tilde_B_res']],
df1[['delta_x_tilde_L_res']],
df1[['delta_x_spec_res']])
return(r)
}
x <- c('Limit\n effect', 'Bonus \n valuation', 'Limit \n valuation', 'Heterogeneous\n limit effect')
names <- rep(factor(x, levels=x),2)
counterfactuals_zero <- order_results_time(param_full)
lower_counterfactuals_zero <- order_results_time(bottom)
upper_counterfactuals_zero <- order_results_time(top)
counterfactuals_est <- order_results_time(param_balanced)
lower_counterfactuals_est <- order_results_time(bottom_bal)
upper_counterfactuals_est <- order_results_time(top_bal)
df <- data.frame(names, counterfactuals_zero, lower_counterfactuals_zero, upper_counterfactuals_zero,
counterfactuals_est, lower_counterfactuals_est, upper_counterfactuals_est)
cols <- c("No perceived habit formation"=maroon ,
"Perceived habit formation"=skyblue)
a <- ggplot(df, aes(x=names, width=.2)) +
geom_point(aes(y=counterfactuals_zero, colour="No perceived habit formation"), stat="identity", position = position_nudge(x = -.1)) +
geom_point(aes(y=counterfactuals_est, colour="Perceived habit formation"), stat="identity", position = position_nudge(x = .1))+
geom_errorbar(aes(ymin=lower_counterfactuals_zero, ymax=upper_counterfactuals_zero), stat="identity", colour=maroon, position = position_nudge(x = -.1)) +
geom_errorbar(aes(ymin=lower_counterfactuals_est, ymax=upper_counterfactuals_est), stat="identity", colour=skyblue, position=position_nudge(x = .1)) +
scale_y_continuous(name="Time effect (minutes/day)") +
theme_classic() +
labs(x = "") +
scale_colour_manual(name = "", values=cols,
labels = c("Unweighted sample", "Weighted toward U.S. adults"))+
theme(legend.text.align = 0,
legend.key.height = unit(1, "cm"),
legend.position="bottom") +
theme(legend.margin=margin(0,0,0,0),
legend.box.margin=margin(-10,-10,-10,-10)) +
theme(axis.text.x = element_text(colour="black")) +
coord_cartesian(ylim = c(0, 165))
ggsave(sprintf('output/%s.pdf', filename), plot=a, width=6.5, height=4.5, units="in")
}
plot_time_effects_both <- function(param_full, param_balanced, bottom, top, bottom_bal, top_bal, filename=""){
order_results_time <- function(df1){
r <- c(df1[['delta_x_cap_res']],
df1[['delta_x_tilde_B_res']],
df1[['delta_x_tilde_L_res']],
df1[['delta_x_cap_multiple_res']],
df1[['delta_B_multiple_res']],
df1[['delta_x_L_multiple_res']],
df1[['delta_x_cap_omega_res']],
df1[['delta_x_L_omega_res']],
df1[['delta_x_spec_res']])
return(r)
}
order_results_bal <- function(df1){
r <- c(df1[['delta_x_cap_res']])
return(r)
}
x <- c('Limit effect',
'Bonus valuation',
'Limit valuation',
'Limit effect \n multiple-good model',
'Bonus valuation \n multiple-good model',
'Limit valuation \n multiple-good model',
'Limit effect \n w=w',
'Limit valuation \n w=w',
'Heterogeneous limit effect' )
names <- factor(x, levels=x)
x_bal <- c( 'Limit effect \n reweighted sample')
names_bal <- factor(x_bal, levels=x_bal)
counterfactuals <- order_results_time(param_full)
lower_counterfactuals <- order_results_time(bottom)
upper_counterfactuals <- order_results_time(top)
counterfactuals_bal <- order_results_bal(param_balanced)
lower_counterfactuals_bal <- order_results_bal(bottom_bal)
upper_counterfactuals_bal<- order_results_bal(top_bal)
df_normal <- data.frame(names, counterfactuals,lower_counterfactuals, upper_counterfactuals)
df_bal <- data.frame(names_bal, counterfactuals_bal, lower_counterfactuals_bal, upper_counterfactuals_bal) %>%
rename(names = names_bal,
counterfactuals = counterfactuals_bal,
lower_counterfactuals = lower_counterfactuals_bal,
upper_counterfactuals = upper_counterfactuals_bal)
df <- rbind(df_normal, df_bal)
xaxislabels <- c('Limit effect',
'Bonus valuation',
'Limit valuation',
'Limit effect, \n multiple-good model',
'Bonus valuation, \n multiple-good model',
'Limit valuation, \n multiple-good model',
unname(TeX(paste0("Limit effect,", "$\\omega = \\hat{\\omega}$"))),
unname(TeX(paste0("Limit valuation,", "$\\omega = \\hat{\\omega}$"))),
'Heterogeneous limit effect',
'Limit effect, \n weighted sample')
a <- ggplot(df, aes(x=names, width=.2)) +
geom_point(aes(y=counterfactuals), colour=maroon)+
geom_errorbar(aes(ymin=lower_counterfactuals, ymax=upper_counterfactuals), colour=maroon) +
scale_y_continuous(name="Effect of temptation on FITSBY use (minutes/day)") +
theme_classic() +
labs(x = "") +
theme(axis.text.x = element_text(colour="black")) +
coord_cartesian(ylim = c(0, 180)) +
theme(axis.text.x = element_text(angle = 45, hjust = 1)) +
scale_x_discrete(labels= xaxislabels)+
theme(axis.title.y = element_text(size = 10))
ggsave(sprintf('output/%s.pdf', filename), plot=a, width=6.5, height=4.5, units="in")
}
# plot_time_effects_robust <- function(param_full, bottom, top, filename=""){
# order_results_time <- function(df1){
# r <- c(df1[['delta_x_cap']],
# df1[['delta_x_tilde_B']],
# df1[['delta_x_tilde_L']],
# df1[['delta_x_cap_multiple']],
# df1[['delta_x_L_multiple']],
# df1[['delta_B_multiple']],
# df1[['delta_x_cap_omega']],
# df1[['delta_x_L_omega']],
# df1[['delta_x_spec']])
# return(r)
# }
# x <- c( 'Limit effect',
# 'Bonus valuation',
# 'Limit valuation',
# 'Limit effect \n multiple-good model',
# 'Limit valuation \n multiple-good model',
# 'Bonus valuation \n multiple-good model',
# 'Limit effect \n w=w',
# 'Limit valuation \n w=w',
# 'Heterogeneous limit effect' )
# names <- factor(x, levels=x)
# counterfactuals <- order_results_time(param_full)
# lower_counterfactuals <- order_results_time(bottom)
# upper_counterfactuals <- order_results_time(top)
# df <- data.frame(names, counterfactuals)
# xaxislabels <- c('Limit effect',
# 'Bonus valuation',
# 'Limit valuation',
# 'Limit effect \n multiple-good model',
# 'Limit valuation \n multiple-good model',
# 'Bonus valuation \n multiple-good model',
# unname(TeX(paste0("Limit effect,", "$\\omega = \\hat{\\omega}$"))),
# unname(TeX(paste0("Limit valuation,", "$\\omega = \\hat{\\omega}$"))),
# 'Heterogeneous limit effect')
# a <- ggplot(df, aes(x=names, width=.2)) +
# geom_point(aes(y=counterfactuals), colour=maroon)+
# geom_errorbar(aes(ymin=lower_counterfactuals, ymax=upper_counterfactuals), colour=maroon) +
# scale_y_continuous(name="Effect of temptation on FITSBY use (minutes/day)") +
# theme_classic() +
# labs(x = "") +
# theme(axis.text.x = element_text(colour="black")) +
# #coord_cartesian(ylim = c(-50, 200)) +
# theme(axis.text.x = element_text(angle = 45, hjust = 1)) +
# scale_x_discrete(labels= xaxislabels)+
# theme(axis.title.y = element_text(size = 10))
# ggsave(sprintf('output/%s.pdf', filename), plot=a, width=6.5, height=4.5, units="in")
# }
plot_time_effects_both_est <- function(param_full, bottom, top, filename=""){
order_results_time <- function(df1){
r <- c(df1[['delta_x_cap']],
df1[['delta_x_tilde_B']],
df1[['delta_x_tilde_L']],
df1[['delta_B_multiple']],
df1[['delta_x_L_multiple']],
df1[['delta_x_cap_omega']],
df1[['delta_x_L_omega']],
df1[['delta_x_spec']])
return(r)
}
x <- c('Limit effect',
'Bonus valuation',
'Limit valuation',
'Bonus valuation, \n multiple-good model',
'Limit valuation, \n multiple-good model',
'Limit effect \n w=w',
'Limit valuation \n w=w',
'Heterogeneous limit effect' )
names <- factor(x, levels=x)
counterfactuals <- order_results_time(param_full)
lower_counterfactuals <- order_results_time(bottom)
upper_counterfactuals <- order_results_time(top)
df <- data.frame(names, counterfactuals)
xaxislabels <- c('Limit effect',
'Bonus valuation',
'Limit valuation',
'Bonus valuation, \n multiple-good model',
'Limit valuation, \n multiple-good model',
unname(TeX(paste0("Limit effect,", "$\\omega = \\hat{\\omega}$"))),
unname(TeX(paste0("Limit valuation,", "$\\omega = \\hat{\\omega}$"))),
'Heterogeneous limit effect')
a <- ggplot(df, aes(x=names, width=.2)) +
geom_point(aes(y=counterfactuals), colour=maroon)+
geom_errorbar(aes(ymin=lower_counterfactuals, ymax=upper_counterfactuals), colour=maroon) +
scale_y_continuous(name="Effect of temptation on FITSBY use (minutes/day)") +
theme_classic() +
labs(x = "") +
theme(axis.text.x = element_text(colour="black")) +
coord_cartesian(ylim = c(0, 200)) +
theme(axis.text.x = element_text(angle = 45, hjust = 1)) +
scale_x_discrete(labels= xaxislabels)+
theme(axis.title.y = element_text(size = 10))
ggsave(sprintf('output/%s.pdf', filename), plot=a, width=6.5, height=4.5, units="in")
}
plot_decomposition_boot <- function(median, bottom, top, filename){
order_results_zero <- function(df1){
r <- c(df1[['x_ss_L_effect_res']],
df1[['x_ss_naivete_res']],
df1[['x_ss_temptation_res']],
df1[['x_ss_habit_res']],
df1[['x_ss_habit_temptation_res']])
return(r)
}
order_results_est <- function(df1){
r <- c(df1[['x_ss_L_effect']],
df1[['x_ss_naivete']],
df1[['x_ss_temptation']],
df1[['x_ss_habit']],
df1[['x_ss_habit_temptation']])
return(r)
}
x <- c('Baseline','No naivete', 'No temptation', 'No habit\nformation',
'No temptation\n or habit formation')
names <- rep(factor(x, levels=x),2)
counterfactuals_zero <- order_results_zero(param_full)
lower_counterfactuals_zero <- order_results_zero(bottom)
upper_counterfactuals_zero <- order_results_zero(top)
counterfactuals_est <- order_results_est(param_full)
lower_counterfactuals_est <- order_results_est(bottom)
upper_counterfactuals_est <- order_results_est(top)
df <- data.frame(names, counterfactuals_zero, lower_counterfactuals_zero, upper_counterfactuals_zero,
counterfactuals_est, lower_counterfactuals_est, upper_counterfactuals_est)
cols <- c("No projection bias"=maroon ,
"Perceived projection bias"=grey)
a <- ggplot(df, aes(x=names, width=.2)) +
geom_point(aes(y=counterfactuals_zero, colour="No projection bias"), stat="identity", position = position_nudge(x = -.1)) +
geom_point(aes(y=counterfactuals_est, colour="Perceived projection bias"), stat="identity", position = position_nudge(x = .1))+
geom_errorbar(aes(ymin=lower_counterfactuals_zero, ymax=upper_counterfactuals_zero), stat="identity", colour=maroon, position = position_nudge(x = -.1)) +
geom_errorbar(aes(ymin=lower_counterfactuals_est, ymax=upper_counterfactuals_est), stat="identity", colour=grey, position=position_nudge(x = .1)) +
scale_y_continuous(name="FITSBY use (minutes/day)") +
theme_classic() +
#theme(axis.text.x = element_text(angle = 45, hjust = 1)) +
labs(x = "") +
scale_colour_manual(name = "", values=cols,
labels = unname(TeX(c(paste0("Restricted model", " (", "$\\alpha$", "$=$", "$1$", ")"),
paste0("Unrestricted model", " (" ,"$\\alpha$", "$=$", "$\\hat{\\alpha}$", ")")
))))+
theme(legend.text.align = 0,
legend.key.height = unit(1, "cm"),
legend.position="bottom") +
theme(legend.margin=margin(0,0,0,0),
legend.box.margin=margin(-10,-10,-10,-10)) +
theme(axis.text.x = element_text(colour="black")) +
coord_cartesian(ylim = c(0, 200))
ggsave(sprintf('output/%s.pdf', filename), plot=a, width=6.5, height=4.5, units="in")
}
plot_decomposition_boot_etas <- function(median, bottom, top, filename){
order_results_zero <- function(df1){
r <- c(df1[['x_ss_L_effect_res']],
df1[['x_ss_naivete_res']],
df1[['x_ss_temptation_res']],
df1[['x_ss_habit_res']],
df1[['x_ss_habit_temptation_res']])
return(r)
}
order_results_zero_eta_high <- function(df1){
r <- c(df1[['x_ss_L_effect_eta_high_res']],
df1[['x_ss_naivete_eta_high_res']],
df1[['x_ss_temptation_eta_high_res']],
df1[['x_ss_habit_eta_high_res']],
df1[['x_ss_habit_temptation_eta_high_res']])
return(r)
}
order_results_zero_eta_low <- function(df1){
r <- c(df1[['x_ss_L_effect_eta_low_res']],
df1[['x_ss_naivete_eta_low_res']],
df1[['x_ss_temptation_eta_low_res']],
df1[['x_ss_habit_eta_low_res']],
df1[['x_ss_habit_temptation_eta_low_res']])
return(r)
}
x <- c('Baseline','No naivete', 'No temptation', 'No habit\nformation',
'No temptation\n or habit formation')
names <- rep(factor(x, levels=x),2)
counterfactuals_zero <- order_results_zero(param_full)
lower_counterfactuals_zero <- order_results_zero(bottom)
upper_counterfactuals_zero <- order_results_zero(top)
counterfactuals_zero_eta_high <- order_results_zero_eta_high(param_full)
lower_counterfactuals_zero_eta_high <- order_results_zero_eta_high(bottom)
upper_counterfactuals_zero_eta_high <- order_results_zero_eta_high(top)
counterfactuals_zero_eta_low <- order_results_zero_eta_low(param_full)
lower_counterfactuals_zero_eta_low <- order_results_zero_eta_low(bottom)
upper_counterfactuals_zero_eta_low <- order_results_zero_eta_low(top)
df <- data.frame(names, counterfactuals_zero, lower_counterfactuals_zero, upper_counterfactuals_zero,
counterfactuals_zero_eta_high, lower_counterfactuals_zero_eta_high, upper_counterfactuals_zero_eta_high,
counterfactuals_zero_eta_low, lower_counterfactuals_zero_eta_low, upper_counterfactuals_zero_eta_low)
cols <- c("eta_0.9"=deepskyblue,
"eta_1.0"=maroon,
"eta_1.1"=grey)
a <- ggplot(df, aes(x=names, width=.2)) +
geom_point(aes(y=counterfactuals_zero_eta_low, colour="eta_0.9"), stat="identity", position = position_nudge(x = -.3))+
geom_point(aes(y=counterfactuals_zero, colour="eta_1.0"), stat="identity", position = position_nudge(x = -.1)) +
geom_point(aes(y=counterfactuals_zero_eta_high, colour="eta_1.1"), stat="identity", position = position_nudge(x = .1)) +
geom_errorbar(aes(ymin=lower_counterfactuals_zero_eta_low, ymax=upper_counterfactuals_zero_eta_low), stat="identity", colour=deepskyblue, position=position_nudge(x = -.3)) +
geom_errorbar(aes(ymin=lower_counterfactuals_zero, ymax=upper_counterfactuals_zero), stat="identity", colour=maroon, position = position_nudge(x = -.1)) +
geom_errorbar(aes(ymin=lower_counterfactuals_zero_eta_high, ymax=upper_counterfactuals_zero_eta_high), stat="identity", colour=grey, position=position_nudge(x = .1)) +
scale_y_continuous(name="FITSBY use (minutes/day)") +
theme_classic() +
#theme(axis.text.x = element_text(angle = 45, hjust = 1)) +
labs(x = "") +
scale_colour_manual(name = "", values=cols,
labels = unname(TeX(c(paste0("$\\eta$", "$=0.9\\hat{\\eta}$", " ,(", "$\\alpha$", "$=1$", ")"),
paste0("$\\eta$", "$=1.0\\hat{\\eta}$", " ,(", "$\\alpha$", "$=1$", ")"),
paste0("$\\eta$", "$=1.1\\hat{\\eta}$", " ,(", "$\\alpha$", "$=1$", ")")
)))) +
theme(legend.text.align = 0,
legend.key.height = unit(1, "cm"),
legend.position="bottom") +
theme(legend.margin=margin(0,0,0,0),
legend.box.margin=margin(-10,-10,-10,-10)) +
theme(axis.text.x = element_text(colour="black")) +
coord_cartesian(ylim = c(0, 200))
ggsave(sprintf('output/%s.pdf', filename), plot=a, width=6.5, height=4.5, units="in")
}
plot_decomposition_boot_unique <- function(median, bottom, top, filename){
order_results_zero <- function(df1){
r <- c(df1[['x_ss_L_effect_res']],
df1[['x_ss_temptation_res']],
df1[['x_ss_habit_res']],
df1[['x_ss_habit_temptation_res']])
return(r)
}
x <- c('Baseline', 'No temptation', 'No habit\nformation',
'No temptation\n or habit formation')
names <- rep(factor(x, levels=x),2)
counterfactuals_zero <- order_results_zero(param_full)
lower_counterfactuals_zero <- order_results_zero(bottom)
upper_counterfactuals_zero <- order_results_zero(top)
df <- data.frame(names, counterfactuals_zero, lower_counterfactuals_zero, upper_counterfactuals_zero)
cols <- c("No perceived habit formation"=maroon)
a <- ggplot(df, aes(x=names, width=.2)) +
geom_point(aes(y=counterfactuals_zero, colour="No perceived habit formation"), stat="identity", position = position_nudge(x = -.1)) +
geom_errorbar(aes(ymin=lower_counterfactuals_zero, ymax=upper_counterfactuals_zero), stat="identity", colour=maroon, position = position_nudge(x = -.1)) +
scale_y_continuous(name="FITSBY use (minutes/day)") +
theme_classic() +
#theme(axis.text.x = element_text(angle = 45, hjust = 1)) +
labs(x = "") +
scale_colour_manual(name = "", values=cols,
labels = unname(TeX(c(paste0("Restricted model", " (", "$\\alpha$ ", "$=1$", ")")
))))+
theme(legend.text.align = 0,
legend.key.height = unit(1, "cm"),
legend.position="bottom") +
theme(legend.margin=margin(0,0,0,0),
legend.box.margin=margin(-10,-10,-10,-10)) +
theme(axis.text.x = element_text(colour="black")) +
coord_cartesian(ylim = c(0, 165))
ggsave(sprintf('output/%s.pdf', filename), plot=a, width=6.5, height=4.5, units="in")
}
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Bootstraps functions
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
run_boot_procedure <- function(fun){
results <- mclapply(1:(size*1.1), fun, mc.set.seed = TRUE)
results %<>% bind_rows
nrowsunfiltered <- nrow(results)
winsortaubtwopre <- nrow(results %>% filter(tau_B_2 ==0))
winsortaubthreepre <- nrow(results %>% filter(tau_B_3 ==0))
percenttaubthreepre <- winsortaubthreepre/nrowsunfiltered*100
percenttaubthreepre <- signif(percenttaubthreepre, digits=2)
winsortaubfourpre <- nrow(results %>% filter(tau_B_4 ==0))
percenttaubfourpre <- winsortaubfourpre/nrowsunfiltered*100
percenttaubfourpre <- signif(percenttaubfourpre, digits=2)
winsortaubfivepre <- nrow(results %>% filter(tau_B_5 ==0))
percenttaubfivepre <- winsortaubfivepre/nrowsunfiltered*100
percenttaubfivepre <- signif(percenttaubfivepre, digits=2)
winsortaultwopre <- nrow(results %>% filter(tau_L_2 ==0))
percenttaultwopre <- winsortaultwopre/nrowsunfiltered*100
percenttaultwopre <- signif(percenttaultwopre, digits=2)
winsortautildelpre <- nrow(results %>% filter(tau_tilde_L ==0))
percenttautildelpre <- winsortautildelpre/nrowsunfiltered*100
percenttautildelpre <- signif(percenttautildelpre, digits=2)
winsortautildebthreewtwopre <- nrow(results %>% filter(tau_tilde_B_3_2 ==0))
percenttautildebthreewtwopre <- winsortautildebthreewtwopre/nrowsunfiltered*100
percenttautildebthreewtwopre <- signif(percenttautildebthreewtwopre, digits=2)
winsoreta <- nrow(results %>% filter(eta ==0))
percenteta <- winsoreta/nrowsunfiltered*100
percenteta <- signif(percenteta, digits=2)
winsorzeta <- nrow(results %>% filter(zeta ==0))
percentzeta <- winsorzeta/nrowsunfiltered*100
percentzeta <- signif(percentzeta, digits=2)
winsorlambda <- nrow(results %>% filter(lambda ==0))
percentlambda <- winsorlambda/nrowsunfiltered*100
percentlambda <- signif(percentlambda, digits=2)
winsorlambdares <- nrow(results %>% filter(lambda_res ==0))
percentlambdares <- winsorlambdares/nrowsunfiltered*100
percentlambdares <- signif(percentlambdares, digits=2)
winsorrho <- nrow(results %>% filter(rho ==0))
percentrho <- winsorrho/nrowsunfiltered*100
percentrho <- signif(percentrho, digits=2)
winsoretares <- nrow(results %>% filter(eta_res ==0))
percentetares <- winsoretares/nrowsunfiltered*100
percentetares <- signif(percentetares, digits=2)
winsorzetares <- nrow(results %>% filter(zeta_res ==0))
percentzetares <- winsorzetares/nrowsunfiltered*100
percentzetares <- signif(percentzetares, digits=2)
winsorrhores <- nrow(results %>% filter(rho_res ==0))
percentrhores <- winsorrhores/nrowsunfiltered*100
percentrhores <- signif(percentrhores, digits=2)
#dropdrawsdenom <- nrow(results %>% filter(is.na(rho_tilde)))
#percentdenom <- dropdrawsdenom/nrowsunfiltered*100
# percentdenom <- signif(percentdenom, digits=2)
# dropdrawsdenomstm <- nrow(results %>% filter(is.na(rho_tilde_658)))
#percentdenomstm <- dropdrawsdenomstm/nrowsunfiltered*100
#percentdenomstm <- signif(percentdenomstm, digits=2
winsortauspre <- nrow(results %>% filter(tau_tilde_B_3_2==0 | tau_tilde_L==0 | tau_L_2==0 |tau_B_3 ==0 | tau_B_5 ==0 | tau_B_4 ==0))
paramwinsorisepre <- nrow(results %>% filter(eta==0 | lambda ==0 | zeta==0 | rho==0 | alpha==1 | eta_res==0 | zeta_res==0 |rho_res==0))
percenttaubtwopre <- winsortaubtwopre/nrowsunfiltered*100
percenttaubtwopre <- signif(percenttaubtwopre, digits=2)
alphapre <- nrow(results %>% filter(alpha == 0 | alpha == 1))
percentalphapre <- alphapre/nrowsunfiltered*100
percentalphapre <- signif(percentalphapre, digits=2)
dropdrawsdenom <- nrow(results %>% filter(is.na(eta)))
NegSSDenom <- dropdrawsdenom/nrowsunfiltered*100
NegSSDenom <- signif(NegSSDenom, digits=2)
alphaundefined <- nrow(results %>% filter(is.na(alpha)))
percentalphaundefined <- alphaundefined/nrowsunfiltered*100
percentalphaundefined <- signif(percentalphaundefined, digits=2)
percenttauspre <- winsortauspre/nrowsunfiltered*100
percenttauspre <- signif(percenttauspre, digits=2)
percentparampre <- paramwinsorisepre/nrowsunfiltered*100
percentparampre <- signif(percentparampre, digits=2)
xsszeropre <- nrow(results %>% filter(x_ss_zero_temp==0))
xsszeropre <- xsszeropre/nrowsunfiltered*100
xsszeropre <- signif(xsszeropre, digits=2)
combined <- list(nrowsunfiltered,
percenttaubtwopre, percenttauspre, percentparampre, xsszeropre, percenttaubthreepre, percenttaubfourpre,
percenttaubfivepre, percenttaultwopre, percenttautildelpre, percenttautildebthreewtwopre,percenteta,
percentzeta, percentlambda, percentrho, percentetares, percentzetares, percentlambdares, percentrhores,
percentalphapre, alphapre, NegSSDenom, dropdrawsdenom,
paramwinsorisepre, percentalphaundefined)
names(combined) <- c("nrowsunfiltered",
"percenttaubtwopre", "percenttauspre", "percentparampre", "xsszeropre", "percenttaubthreepre", "percenttaubfourpre",
"percenttaubfivepre", "percenttaultwopre", "percenttautildelpre", "percenttautildebthreewtwopre","percenteta",
"percentzeta", "percentlambda", "percentrho", "percentetares", "percentzetares", "percentlambdares", "percentrhores",
"percentalphapre", "alphapre", "NegSSDenom", "dropdrawsdenom",
"paramwinsorisepre", "percentalphaundefined")
save_nrow(combined, filename ="bootsdrawsnumbers", suffix="")
return(results)
}
run_boot_iter <- function(s) {
if (s %% 50 == 0){ print(s) }
sample <- sample_n(df, nrow(df), replace = T)
sample %<>%
mutate(UserID = 1:n())
param <- param_initial
#You can chose to print statements that display warning when concavity conditions are not met when estimating the model
try(param <- estimate_model(sample, param, display_warning=F))
return(param)
}
run_boot_iter_full <- function(s) {
if (s %% 50 == 0){ print(s) }
sample <- sample_n(df, nrow(df), replace = T)
sample %<>%
mutate(UserID = 1:n())
param <- param_initial
try(param <- estimate_model(sample, param, full=T))
return(param)
}
run_boot_iter_bal2 <- function(s) {
if (s %% 50 == 0){ print(s) }
sample <- sample_n(df, nrow(df), replace = T)
sample %<>%
mutate(UserID = 1:n())
sample %<>% balance_data(magnitude=2)
param <- param_initial
try(param <- estimate_model(sample, param, winsorize=T))
return(param)
}
run_boot_iter_bal <- function(s) {
if (s %% 50 == 0){ print(s) }
sample <- sample_n(df, nrow(df), replace = T)
sample %<>%
mutate(UserID = 1:n())
sample %<>% balance_data(magnitude=3)
param <- param_initial
try(param <- estimate_model(sample, param, winsorize=T))
return(param)
}
run_boot_iter_bal4 <- function(s) {
if (s %% 50 == 0){ print(s) }
sample <- sample_n(df, nrow(df), replace = T)
sample %<>%
mutate(UserID = 1:n())
sample %<>% balance_data(magnitude=4)
param <- param_initial
try(param <- estimate_model(sample, param, winsorize=T))
return(param)
}
run_boot_iter_bal5 <- function(s) {
if (s %% 50 == 0){ print(s) }
sample <- sample_n(df, nrow(df), replace = T)
sample %<>%
mutate(UserID = 1:n())
sample %<>% balance_data(magnitude=5)
param <- param_initial
try(param <- estimate_model(sample, param, winsorize=T))
return(param)
}
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Latex saving functions for different formating numbers
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
signnumber <- function(x){
significant <- ifelse(x==0, sprintf("0"),sprintf(paste0("%0.", pmax(floor(3 - log(abs(x)+0.001, 10)), 0), "f"), x))
return(significant)
}
signnumber2 <- function(x){
significant <- ifelse(x==0, sprintf("0"),sprintf(paste0("%0.", pmax(floor(2 - log(abs(x)+0.01, 10)), 0), "f"), x))
return(significant)
}
save_tex <- function(param, filename, suffix=""){
tex_string <- ""
for(x in names(param)){
name <- x
if (x %in% names(name_changes)){
name <- name_changes[[x]]
}
new_line <- (sprintf("\\newcommand{\\%s%s}{%s}", name, suffix, signnumber(param[[x]])))
tex_string <- sprintf("%s\n%s", tex_string, new_line)
}
# saves it as a .tex file
sink(file=sprintf('output/%s.tex', filename))
cat(tex_string)
sink(NULL)
}
save_tex_nice <- function(param, filename, suffix=""){
tex_string <- ""
for(x in names(param)){
name <- x
if (x %in% names(name_changes)){
name <- name_changes[[x]]
}
new_line <- (sprintf("\\newcommand{\\%s%s}{%s}", name, suffix, signif(param[[x]], digits=2)))
tex_string <- sprintf("%s\n%s", tex_string, new_line)
}
# saves it as a .tex file
sink(file=sprintf('output/%s.tex', filename))
cat(tex_string)
sink(NULL)
}
save_tex_one <- function(param, filename, suffix=""){
tex_string <- ""
for(x in names(param)){
name <- x
if (x %in% names(name_changes)){
name <- name_changes[[x]]
}
new_line <- (sprintf("\\newcommand{\\%s%s}{%s}", name, suffix, format(param[[x]], digits=1)))
tex_string <- sprintf("%s\n%s", tex_string, new_line)
}
# saves it as a .tex file
sink(file=sprintf('output/%s.tex', filename))
cat(tex_string)
sink(NULL)
}
save_tex2 <- function(param, filename, suffix=""){
tex_string <- ""
for(x in names(param)){
name <- x
if (x %in% names(name_changes)){
name <- name_changes[[x]]
}
new_line <- (sprintf("\\newcommand{\\%s%s}{%s}", name, suffix, signnumber2(param[[x]])))
tex_string <- sprintf("%s\n%s", tex_string, new_line)
}
# saves it as a .tex file
sink(file=sprintf('output/%s.tex', filename))
cat(tex_string)
sink(NULL)
}
save_boot_tex <- function(median, se, filename, suffix=""){
tex_string <- ""
for(x in names(median)){
name <- x
if (x %in% names(name_changes)){
name <- name_changes[[x]]
}
if (x %in% constants){
new_line <- (sprintf("\\newcommand{\\%s%s}{%s}", name, suffix, signnumber(median[[x]])))
} else {
new_line <- (sprintf("\\newcommand{\\%s%s}{%s\\:(%s)}", name, suffix, signnumber(median[[x]]), signnumber(se[[x]])))
}
tex_string <- sprintf("%s\n%s",tex_string, new_line)
}
sink(file=sprintf('output/%s.tex', filename))
cat(tex_string)
sink(NULL)
}
save_boot_tex_percentile <- function(bottom, top, filename, suffix=""){
tex_string <- ""
for(x in names(bottom)){
name <- x
if (x %in% names(name_changes)){
name <- name_changes[[x]]
}
new_line <- (sprintf("\\newcommand{\\%s%s}{[%s, %s]}", name, suffix, signnumber(bottom[[x]]), signnumber(top[[x]])))
tex_string <- sprintf("%s\n%s",tex_string, new_line)
}
sink(file=sprintf('output/%s.tex', filename))
cat(tex_string)
sink(NULL)
}
save_nrow <- function(nrows_list, filename, suffix=""){
tex_string <- ""
for(x in names(nrows_list)){
name <- x
new_line <- (sprintf("\\newcommand{\\%s%s}{%s}", name, suffix, nrows_list[[x]]))
tex_string <- sprintf("%s\n%s",tex_string, new_line)
}
sink(file=sprintf('output/%s.tex', filename))
cat(tex_string)
sink(NULL)
}