
# R Code Documentation for “Durables and Lemons: Private Information and the Market for Cars” by Richard Blundell, Ran Gu, Soren Leth-Petersen, Hamish Low, and Costas Meghir

* renv

  1. The "R/" folder is managed by the R package "renv 0.15.4" (https://rstudio.github.io/renv/index.html), which provides reproducible environments for R projects.

  2.  For background, see "Collaborating with renv" (https://rstudio.github.io/renv/articles/collaborating.html). 
  
  3.  The project includes a lockfile renv.lock in the R/ folder, along with the auto-generated .Rprofile and renv/activate.R.

  4. When the project is first opened in R/, renv will automatically bootstrap itself and install the correct version of renv into the project library.

  5. After this has completed, run renv::restore() to install the required package versions and recreate the project library locally.


* Makevars

  1. The ~/.R/Makevars file tells R what compiler should be used and what options are available. See reference at: 
      https://cran.r-project.org/doc/manuals/r-release/R-exts.html#Using-Makevars
  
  2. On Yale’s Grace cluster, we used the following specification in my ~/.R/Makevars for performance: 

      CXXFLAGS = -O3 -msse2 -mfpmath=sse
      CFLAGS   = -O3 -msse2 -mfpmath=sse
  
***********************************************************************
# 1. estimate external parameters of separation shock on income
***********************************************************************
  mpit73/scar.r


*********************************************************
# 2. MSM estimation of baseline Asymmetric information case
*********************************************************
  mpit73/inc.prepare.est.sim.r


*********************************************************
# 3. estimation of Symmetric information case with transaction cost
*********************************************************
  mpit55/inc.prepare.est.lem_bor.r 


*********************************************************
# 4. estimation of Asymmetric information case with transaction cost
*********************************************************
  mpit67/inc.prepare.est.sim.r


*********************************************************
# 5. estimation of Asymmetric information case without saving
*********************************************************
  mpit80/inc.prepare.est.sim.r


*********************************************************
# 6. get the score and standard deviation
*********************************************************
  #1. get the score 
  mpit73/run.modelC.score.r 

  #2. get the std from score
  mpit73/run.modelC.std.HL.r


*********************************************************
# 7. get lemons penalty in Symmetric information without re-estimation
*********************************************************
  mpit73/run.modelC.lem_bor.r


**********************************************
# 8. report estimated parameters, moments, standard deviation, and lemons penalty
**********************************************
  mpit73/fun.analysis.report.r section 1, 2, and 3


**********************************************
# 9. plot quality distribution
**********************************************
  #1. plot quality distribution from stata for Asymmetric information case
  do mpit73/fun.stata.qualdis.do

  #2. plot quality distribution from stata for Symmetric information case
  do mpit73/fun.stata.qualdis_keepp0.do


**********************************************
# 10. Identification
**********************************************
  mpit73/run.modelC.ident.r


**********************************************
# 11. event study, Asymmetric information
**********************************************
  mpit73/run.modelC.sim_track_scar.r


**********************************************
# 12. event study, Symmetric information without re-estimation
**********************************************
  mpit73/run.modelC.lem_bor_track_scar.r


**********************************************
# 13. event study, Symmetric information with re-estimation
**********************************************
  mpit55/run.modelC.lem_bor_track_scar.r


**********************************************
# 14. event study, Asymmetric information without saving
**********************************************
  mpit80/run.modelC.sim_track_scar.nosave.r


**********************************************
# 15. plot event study
**********************************************
  #1. prepare the data for Asymmetric and Symmetric information
  mpit73/fun.stata.goodq.r

  #2. construct the sample for event study plot around unemployment
  do mpit73/fun.stata.myevent_cons.do

  #3. plot data, Asymmetric and Symmetric information with transaction cost
  do mpit73/fun.stata.myevent_plot_est_sym.do

  #4. plot data, Asymmetric information and Nosaving case
  do mpit73/fun.stata.myevent_plot.do

  #5. both Asymmetric and Symmetric information by ownership duration
  do mpit73/fun.stata.myevent_cons_own1.do

  #6. both Asymmetric and Symmetric information by ownership duration and quality
  do mpit73/fun.stata.myevent_cons_goodq_own1.do

  #7. plot data by level of financial asset
  do mpit73/fun.stata.myevent_plot_fa.do


**********************************************
# 16. unique existence, plot single crossing graph
**********************************************
  #1. vary one price at a time
  mpit73/run.modelC.unique.one.r 

  #2. plot profit varying on one price
  mpit73/fun.analysis.report.r section 4

  #3. vary one price while fixing the 2nd price at different values
  mpit73/run.modelC.unique.cube.r 

  #4. plot profit varying prices 1 and 2 separately
  mpit73/fun.analysis.report.r section 8

**********************************************
# 17. impact of different variance of income shocks
**********************************************
  #1. simulate with different variance of income shocks
  mpit73/inc.prepare.cycle_2020.r

  #2. get statistics from simulation
  mpit73/fun.analysis.report.r section 5

  #3.plot
  do mpit73/fun.stata.cycle_plot.do


**********************************************
# 18. parametrize the borrowing constraint
**********************************************
  #1. simulate with different borrowing constraints
  mpit73/run.modelC.sim_borrowConst.r

  #2. get statistics from simulation
  mpit73/fun.analysis.report.r section 6 and 7

  #3. plot 
  do mpit73/fun.stata.borrowConst_plot.do


**********************************************
# 19. find the standard error for the lemons premium
**********************************************
  #1. draw potential parameter sets according to standard errors of parameters
  fun.analysis.report.r section 9

  #2. simulate the model using potential parameter sets
  mpit73/run.modelC.alt_para.r
 
  #3. calculate standard error for the lemons premium 
  fun.analysis.report.r section 9

