
version 10
log using contemp0.log, replace


use contempp, clear
sort id date
replace low_est=1000*low_est
replace high_est=1000*high_est
replace price=1000*price
gen lest=ln(low_est)
gen hest=ln(high_est)
gen lprice=ln(price)
gen lav=ln((low_est+high_est)/2)
gen llow=lest
gen lhigh=hest
        

sort date lot

       *This command groups sessions on a single day into one auction
replace date=int(date)

*This drops the few paintings without estimates
drop if low_est==.

drop if price==0
drop if price==.



        *The following commands ensure that lots are numbered consecutively from 1

sort date lot
gen newlot=1 if date~=date[_n-1]
replace newlot=newlot[_n-1]+1 if date==date[_n-1]






        *Create dummy variables for the different auctions
tab date, gen(dat)

gen age=date_ptg
replace lprice=llow+ln(.8) if sold==0
replace price=low_est*.80 if sold==0
sum price low_est high_est len wid 
sum price low_est high_est len wid if sold==1

      *Transforming sample for regressions with covariates (want artists with at least two paintings to avoid colinearities with artist dummies)
encode med, gen(medium)
keep if age~=. & len~=. & wid~=. & medium~=.
egen count=count(lot), by(artist)
drop if count<2
encode artist, gen(art)
encode vat, gen(en_vat)
replace en_vat=0 if en_vat==.
tab art, gen(art)
tab medium, gen(me)


        *Transform age and other variables


egen minage=min(age)
gen minage1=minage-1
gen newage=age-minage1
gen lage=log(newage)
gen lwid=log(wid)
gen llen=log(len)
 
keep if lage ~=. & llen~=. & lwid~=. 


reg lprice  lage llen lwid me1-me19 art1-art302 hal2-hal25 if sold==1
testparm me1-me19
testparm art1-art302
testparm hal2-hal25

predict pprice 

predict resid, resid
predict flag if e(sample)
sum sold if resid>0 & resid~=.
sum sold if resid~=.
sum sold if resid>0 & resid~=. & sold==1
sum sold if resid~=. & sold==1
sort id date


gen luscpi=ln(uscpi)
gen lukcpi=ln(ukcpi)
gen difdate=date-date[_n-1] if id==id[_n-1] & id~=.
gen difus=-luscpi+luscpi[_n-1] if id==id[_n-1] & id~=.
gen difuk=-lukcpi+lukcpi[_n-1] if id==id[_n-1] & id~=.
replace difdate=difdate/30
sum price low_est high_est len wid difdate
sum price low_est high_est len wid difdate if flag~=.
sum date if lprice>flag & price~=.
sum date if lprice<flag & flag~=.


gen lresid=resid[_n-1] if id==id[_n-1] & id~=.
gen llprice=lprice[_n-1] if id==id[_n-1] & id~=.
gen lpprice=pprice[_n-1] if id==id[_n-1] & id~=.

gen lossgain=lprice[_n-1]-pprice if id==id[_n-1] & sold[_n-1]==1 & id~=.


sum price low_est high_est len wid if id==id[_n-1] & id~=. & lest~=. & pprice~=. & lossgain~=. & lresid~=. 
sum price low_est high_est len wid if sold==1 & id==id[_n-1] & id~=. & lest~=. & pprice~=. & lossgain~=. & lresid~=.


sum sold if llprice>lpprice & lresid~=. & id==id[_n-1] & id~=. & lest~=. & pprice~=. & lossgain~=. & lresid~=. & llprice~=.



sum difdate if lresid~=. & id==id[_n-1] & id~=. & lest~=. & pprice~=. & lossgain~=. & lresid~=.


sum difdate if lresid~=. & sold==1 & id==id[_n-1] & id~=. & lest~=. & pprice~=. & lossgain~=. & lresid~=.

*Note:  difdate is not in logs as the fit is better


keep if lresid~=. & lossgain~=.


*do not want to do all sample because hedonic regressions are performed using only sold sample
regress llow  lossgain  pprice lresid difdate if sold==1, robust
regress lprice lossgain pprice lresid difdate if sold==1, robust
regress llow  lossgain pprice lresid difdate if sold==1 & difdate<42, robust
regress lprice lossgain pprice lresid difdate if sold==1 & difdate<42, robust 
log close

end





