/* LabMPLidr.do evaluates the IDR responses */

*	Variable definitions in data
* id		subject id
* DRtask	Discount rate task (first task=1, second task=2, etc.)
* Task#		Dummy for # Task number 
* chLO		Choice low
* chHI          Choice high               
* choiceLO	lower bound on probability interval
* choiceHI	higher bound on probability interval
* choice	midpoint of probability interval
* idrLO		lower bound on IDR interval
* idrHI		higher bound on IDR interval
* crra		midpoint of CRRA interval
* crraDIFF	CRRA interval size
* n_a		number of early payment choices
* n_i		number of indifferent choices
* n_b		number of late payment choices
* treat		framing effect (skewLO=-1, symmetric=0, skewHI=1)
* skewLO        Dummy for skewLO treatment
* skewHI        Dummy for skewHI treatment          
* horizon       Horizon for late payment in days
* horizonM	Horizon for late payment in months
* RApay		RA payments
* Final		final response
* Initial	initial response
* experimenter	experimenter effect (Lau=0 and Andersen=1)
* iteration 	iteration number of MPL (0=first, 1 second, 2, third)
* Date		Date of experiment
* Vprize	Prize of the lottery 
* session	Experiment session 
* endowment	Random initial endowment
* second	Dummy for risk experiment being second
* impl		Dummy for subjects facing Iterative Multiple Price List
* smpl		Dummy for subjects facing Swithcing Multiple Price List
* mpl		Dummy for subjects facing normal    Multiple Price List
* uslab		Dummy for US subjects
* female	Dummy for females
* young		Dummy for subjects aged less than 30
* middle 	Dummy for subjects aged between 40 and 50
* old		Dummy for subjects aged over 50
* copen         Dummy for subjects that lives in Copenhagen area
* city          Dummy for subjects that lives in larger city of 20,000 or more
* owner         Dummy for subjects that owns home or apartment
* retired       Dummy for subjects that are retired
* student       Dummy for subjects that are students
* skilled       Dummy for subjects that have some post-secondary education
* longedu	Dummy for subjects that have substantial higher education
* kids          Dummy for subjects that have children
* single        Dummy for subjeccts which lives alone
* IncLow        Dummy for subjeccts with Lower level income
* IncMed        Dummy for subjeccts with Medium level income
* IncHigh       Dummy for subjeccts with Higher level income
* hhd		household characteristics (singles/households,children...)
* nhhd		number of people in household
* record	control variable

* GENERATE EXTRA VARIABLES FOR IDENTIFICATION OF EFFECTS AND SPECIFY GLOBALS FOR REGRESSIONS

* get data
use LabMPLidr, replace

* force lower limit on open intervals
replace idrLO=0 if idrLO==.

* Dummies with respect to interaction between format and framing (reference is impl and symmetric)
foreach F in impl smpl mpl {
generate `F'_lo=0
replace  `F'_lo=1 if `F'==1 & skewLO==1 
generate `F'_hi=0
replace  `F'_hi=1 if `F'==1 & skewHI==1 
generate `F'_sym=0
replace `F'_sym=1 if `F'==1 & skewLO==0 & skewHI==0
}
generate nonimpl_lo=0
replace  nonimpl_lo=1 if impl==0 & skewLO==1 
generate nonimpl_hi=0
replace  nonimpl_hi=1 if impl==0 & skewHI==1
generate nonimpl=0
replace nonimpl=1 if impl==0
generate nonimpl_sym=0
replace nonimpl_sym=1 if impl==0 & skewLO==0 & skewHI==0

* Dummies with respect to interaction between format and order (reference is impl and task1)
foreach F in 2 3 {
generate impl_task`F'=0
replace  impl_task`F'=1 if impl==1 & Task`F'==1
generate nonimpl_task`F'=0
replace  nonimpl_task`F'=1 if impl==0 & Task`F'==1 
}

* Dummies with respect to interaction between format and horizon (reference is impl and 1 month horizon)
foreach F in 4 6 {
generate impl_horizon`F'=0
replace  impl_horizon`F'=1 if impl==1 & horizon`F'==1
generate nonimpl_horizon`F'=0
replace  nonimpl_horizon`F'=1 if impl==0 & horizon`F'==1
generate mpl_horizon`F'=0
replace  mpl_horizon`F'=1 if mpl==1 & horizon`F'==1
generate smpl_horizon`F'=0
replace  smpl_horizon`F'=1 if smpl==1 & horizon`F'==1
}

* identify individual characteristics on demographics
global rhs "female single nhhd owner student skilled longedu IncLow IncHigh copen city"

* identify tasks by order of presentation (numbers adjusted for earlier trainers)
global tasks "Task2 Task3"

* identify MPLs, normalized on mpl
global mpls "smpl impl"

* identify interactions effects between format and framing (reference is impl and symmetric)
global formfra	"impl_sym impl_hi impl_lo nonimpl_sym nonimpl_hi nonimpl_lo"

* identify interaction between format and order (reference is impl and task1)
global formorder "impl_task2 impl_task3 nonimpl_task2 nonimpl_task3 "

* identify  interaction effect between format and horizon pooling smpl and mpl
global formhoripool "impl_horizon4 impl_horizon6 nonimpl_horizon4 nonimpl_horizon6"

* identify  interaction effect between format and horizon
global formhori     "impl_horizon4 impl_horizon6 mpl_horizon4 mpl_horizon6 smpl_horizon4"


* Save data
save Data, replace

* ANALYSIS ON DANISH DATA

* calculate mean and median censored idr for final response
summ idr if treat==0  & Final==1, detail
summ idr if treat==-1 & Final==1, detail
summ idr if treat==1  & Final==1, detail
summ idr if Final==1, detail /* PAGE 15, mean 29.2%, median 27.9% */

* calculate mean and median censored idr for initial response
summ idr if treat==0  & Initial==1, detail
summ idr if treat==-1 & Initial==1, detail
summ idr if treat==1  & Initial==1, detail
summ idr if Initial==1, detail

* Figure 5 - Part A: Midpoints of individual responses, symmetric frame only
histogram idr if Final==1 & treat==0, start(0) width(5) ytitle("") ylabel(, angle(horizontal)) xtitle("") xlabel(0(10)80, noticks nolabels) /*
*/ title("Mid-Points of Responses", ring(0) pos(1) bfcolor(gs14) blcolor(gs14) box) saving(ExtraGraphs\Temp1_Figure4, replace)

* Table 4
xtintreg idrLO idrHI horizon4 horizon6 impl smpl skewLO skewHI endowment $tasks experimenter $rhs if Final==1, i(id) quad(26) /* Table 4 */
estimates store SMPL1
parmest, list(*) saving(Tables/Table4, replace) idstr("Table 4") label

* test effects of treatments
test horizon4=horizon6          /* test no change in IDR on longer horizons: H0: no difference in 4 and 6 months horizons, Footnote 28 */
test impl=smpl=0                /* test effect of iterations from mpl: H0 smpl and iMPL zero, Footnote 29 */
test impl=smpl                  /* test effect of iterations from mpl: H0 smpl and iMPL same */
test skewLO=skewHI=0            /* test effects of framing: H0 no effect of framing, Footnote 30 */
test Task2=Task3=0              /* test changes in order of tasks: H0 no order/learning effect */
test Task2=Task3                /* test changes in order of tasks: H0 no change in order/learning effect, p=0.42 in text */

* predictions
predict idrP0, xb

* Figure 4 - Part B: Estimated IDR, symmetric frame only.
histogram idrP0 if Final==1 & treat==0 & idrP0>0, fraction start(0) width(5) ytitle("") ylabel(, angle(horizontal)) xtitle("") xlabel(0(10)80) /*
*/ title("Estimated Responses", ring(0) pos(1) bfcolor(gs14) blcolor(gs14) box) saving(ExtraGraphs\Temp2_Figure4, replace)

* Figure 4: Elicited and estimated IDR values (NEW FIGURE 2)
gr combine ExtraGraphs\Temp1_Figure4.gph ExtraGraphs\Temp2_Figure4.gph, ycommon xcommon iscale(*.9) imargin(vsmall) cols(1) /*
*/ title("Figure 4: Elicited Individual Discount Rates in the Lab") note("Individual Discount Rate (%)", size(medlarge) angle(horizontal) pos(6) ring(1)) /*
*/ saving(Graphs/Figure_4_wp, replace) /* FIGURE 4 in WP version*/

gr combine ExtraGraphs\Temp1_Figure4.gph ExtraGraphs\Temp2_Figure4.gph, xcommon iscale(*.9) imargin(vsmall) cols(1) /*
*/ title("Figure 2: Elicited Individual Discount Rates in the Lab") note("Individual Discount Rate (%)", size(medlarge) angle(horizontal) pos(6) ring(1)) /*
*/ saving(Graphs/Figure_2, replace) /* FIGURE 2 in final version*/

* Figure 5: Estimated IDR values and format
histogram idrP0 if Final==1  & mpl==1 & idrP0>0,  		fraction start(0) width(5) title(MPL format,  ring(0) pos(1) bfcolor(gs14) blcolor(gs14) box) /*
*/ ytitle("") ylabel(, angle(horizontal)) xtitle("") xlabel(0(10)80, noticks nolabels) 	saving(tmp1, replace)
histogram idrP0 if Final==1  & smpl==1 & idrP0>0, 		fraction start(0) width(5) title(sMPL format, ring(0) pos(1) bfcolor(gs14) blcolor(gs14) box) /*
*/ ytitle("") ylabel(, angle(horizontal)) xtitle("") xlabel(0(10)80, noticks nolabels) 	saving(tmp2, replace)
histogram idrP0 if Final==1  & impl==1 & idrP0>0, 		fraction start(0) width(5) title(iMPL format, ring(0) pos(1) bfcolor(gs14) blcolor(gs14) box) /*
*/ ytitle("") ylabel(, angle(horizontal)) xtitle("") xlabel(0(10)80)     				   		saving(tmp3, replace)
gr combine tmp1.gph tmp2.gph tmp3.gph, ycommon xcommon iscale(*.9) imargin(vsmall) cols(1) title(Figure 5: Elicited Discount Rates and Format) /*
*/ subtitle(Estimated Responses) note("Individual Discount Rate (%)", size(medlarge) angle(horizontal) pos(6) ring(1)) saving(Graphs\Figure_5_wp, replace) /* FIGURE 5 in WP version */

* Figure 6: Estimated IDR values and framing
histogram idrP0 if Final==1  & skewLO==1 & idrP0>0,             fraction start(0) width(5) title("Skewness to lower IDR", ring(0) pos(1) bfcolor(gs14) blcolor(gs14) box) /*
*/ ytitle("") ylabel(, angle(horizontal)) xtitle("") xlabel(0(10)80, noticks nolabels) saving(tmp1, replace)
histogram idrP0 if Final==1  & skewLO==0 & skewHI==0 & idrP0>0, fraction start(0) width(5) title("Symmetric framing",     ring(0) pos(1) bfcolor(gs14) blcolor(gs14) box) /*
*/ ytitle("") ylabel(, angle(horizontal)) xtitle("") xlabel(0(10)80, noticks nolabels) saving(tmp2, replace)
histogram idrP0 if Final==1  & skewHI==1 & idrP0>0,             fraction start(0) width(5) title("Skewness to raise IDR", ring(0) pos(1) bfcolor(gs14) blcolor(gs14) box) /*
*/ ytitle("") ylabel(, angle(horizontal)) xtitle("") xlabel(0(10)80)                   saving(tmp3, replace)
gr combine tmp1.gph tmp2.gph tmp3.gph, ycommon xcommon iscale(*.9) imargin(vsmall) cols(1) title(Figure 6: Elicited Discount Rates and Framing) /*
*/ subtitle(Estimated Responses) note("Individual Discount Rate (%)", size(medlarge) angle(horizontal) pos(6) ring(1)) saving(Graphs\Figure_6_wp, replace) /* FIGURE 6 in WP version */

* Figure 7: Estimated IDR values and order
histogram idrP0 if Final==1  & Task1==1 & idrP0>0, 		fraction start(0) width(5) title("Task 1", ring(0) pos(1) bfcolor(gs14) blcolor(gs14) box) /*
*/ ylabel(, angle(horizontal)) ytitle("") xtitle("") xlabel(0(10)80, noticks nolabels) saving(tmp1, replace)
histogram idrP0 if Final==1  & Task2==1 & idrP0>0, 		fraction start(0) width(5) title("Task 2", ring(0) pos(1) bfcolor(gs14) blcolor(gs14) box) /*
*/ ylabel(, angle(horizontal)) ytitle("") xtitle("") xlabel(0(10)80, noticks nolabels) saving(tmp2, replace)
histogram idrP0 if Final==1  & Task3==1 & idrP0>0, 		fraction start(0) width(5) title("Task 3", ring(0) pos(1) bfcolor(gs14) blcolor(gs14) box) /*
*/ ylabel(, angle(horizontal)) ytitle("") xtitle("") xlabel(0(10)80)                   saving(tmp3, replace)
gr combine tmp1.gph tmp2.gph tmp3.gph, ycommon xcommon iscale(*.9) imargin(vsmall) cols(1) title("Figure 7: Elicited Discount Rates and Task Order") /*
*/ subtitle(Estimated Responses) note("Individual Discount Rate (%)", size(medlarge) angle(horizontal) pos(6) ring(1)) saving(Graphs\Figure_7_wp, replace) /* FIGURE 7 in WP version */

* remove temporary graph files
erase tmp1.gph
erase tmp2.gph
erase tmp3.gph

* TEST FOR POOLING OF SMPL AND MPL
* estimation on final responses pooling mpl and smpl
xtintreg  idrLO idrHI horizon4 horizon6 impl skewLO skewHI endowment $tasks experimenter $rhs if Final==1, i(id) quad(26)
estimates store SMPL2
* likelihood ratio test of pooling MPL and sMPL (H0: no difference)
lrtest SMPL1 SMPL2, stats

* CHECK INTERACTION EFFECTS (Footnote 34)

* see interaction effect between format and framing
xtintreg idrLO idrHI horizon4 horizon6 $formfra endowment $tasks experimenter $rhs if Final==1, i(id) quad(26)
* test the interaction effects
qui test impl_lo=nonimpl_lo
    test impl_hi=nonimpl_hi, accumulate
    test impl_lo=nonimpl_lo
    test impl_hi=nonimpl_hi

* see interaction effect between format and order
xtintreg idrLO idrHI horizon4 horizon6 impl skewLO skewHI $formorder endowment experimenter $rhs if Final==1, i(id) quad(26)

* test the interaction effects
qui test impl_task2=nonimpl_task2
    test impl_task3=nonimpl_task3, accumulate
foreach T in 2 3 {
    test impl_task`T'=nonimpl_task`T'
} 

* extra control for interaction effects of iMPL and order, controlling for horizon
xtintreg idrLO idrHI horizon4 horizon6 impl skewLO skewHI impl_task2 impl_task3 endowment experimenter $tasks $rhs if Final==1, i(id) quad(26)

* see interaction effect between format and horizon
xtintreg idrLO idrHI impl skewLO skewHI impl_horizon4 impl_horizon6 nonimpl_horizon4 nonimpl_horizon6 endowment experimenter $rhs if Final==1, i(id) quad(26)

* test the interaction effects
qui test impl_horizon4=nonimpl_horizon4
    test impl_horizon6=nonimpl_horizon6, accumulate
foreach T in 4 6 {
    test impl_horizon`T'=nonimpl_horizon`T'
}

erase Data.dta
exit

** SENSIVITY ANALYSIS
use Data, replace

* specification test: Hausman test of random effect model
qui xtreg idr horizon4 horizon6 experimenter $tasks1 $rhs if Final==1, i(id) fe
estimates store FE1
qui xtreg idr horizon4 horizon6 experimenter $tasks1 $rhs if Final==1, i(id) re
estimates store RE1
xttest0
hausman FE1 RE1

* Do table 4, just with initial responses to compare
xtintreg idrLO idrHI horizon4 horizon6 impl smpl skewLO skewHI endowment $tasks experimenter $rhs if Initial==1, i(id) quad(26)

* test effects of treatments
test horizon4=horizon6          /* test no change in IDR on longer horizons: H0: no difference in 4 and 6 months horizons */
test impl=smpl=0                /* test effect of iterations from mpl: H0 smpl and iMPL zero */
test impl=smpl                  /* test effect of iterations from mpl: H0 smpl and iMPL same */
test skewLO=skewHI=0            /* test effects of framing: H0 no effect of framing */
test Task2=Task3=0              /* test changes in order of tasks: H0 no order/learning effect */
test Task2=Task3                /* test changes in order of tasks: H0 no change in order/learning effect */

* HETEROSKEDASTICITY TEST
* Basic
intreg idrLO idrHI horizon4 horizon6 impl smpl skewLO skewHI endowment $tasks experimenter $rhs if Initial==1, robust cluster(id) het(horizon4 horizon6 impl smpl skewLO skewHI endowment $tasks experimenter)

* only horizon 4
intreg idrLO idrHI                   impl smpl skewLO skewHI endowment $tasks experimenter $rhs if Initial==1 & horizon4==1, robust cluster(id) het(impl smpl skewLO skewHI endowment $tasks experimenter)

* only horizon 6
intreg idrLO idrHI                   impl smpl skewLO skewHI endowment $tasks experimenter $rhs if Initial==1 & horizon6==1, robust cluster(id) het(impl smpl skewLO skewHI endowment $tasks experimenter)

* only impl responses
intreg idrLO idrHI horizon4 horizon6           skewLO skewHI endowment $tasks experimenter $rhs if Initial==1 & impl==1, robust cluster(id) het(horizon4 horizon6 skewLO skewHI endowment $tasks experimenter)

* only smpl responses 
intreg idrLO idrHI horizon4 horizon6           skewLO skewHI endowment $tasks experimenter $rhs if Initial==1 & smpl==1, robust cluster(id) het(horizon4 horizon6 skewLO skewHI endowment $tasks experimenter)

* only mpl responses
intreg idrLO idrHI horizon4 horizon6           skewLO skewHI endowment $tasks experimenter $rhs if Initial==1 & mpl==1, robust cluster(id) het(horizon4 horizon6 skewLO skewHI endowment $tasks experimenter)