File size: 371,911 Bytes
f81d4f9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517
4518
4519
4520
4521
4522
4523
4524
4525
4526
4527
4528
4529
4530
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
4541
4542
4543
4544
4545
4546
4547
4548
4549
4550
4551
4552
4553
4554
4555
4556
4557
4558
4559
4560
4561
4562
4563
4564
4565
4566
4567
4568
4569
4570
4571
4572
4573
4574
4575
4576
4577
4578
4579
4580
4581
4582
4583
4584
4585
4586
4587
4588
4589
4590
4591
4592
4593
4594
4595
4596
4597
4598
4599
4600
4601
4602
4603
4604
4605
4606
4607
4608
4609
4610
4611
4612
4613
4614
4615
4616
4617
4618
4619
4620
4621
4622
4623
4624
4625
4626
4627
4628
4629
4630
4631
4632
4633
4634
4635
4636
4637
4638
4639
4640
4641
4642
4643
4644
4645
4646
4647
4648
4649
4650
4651
4652
4653
4654
4655
4656
4657
4658
4659
4660
4661
4662
4663
4664
4665
4666
4667
4668
4669
4670
4671
4672
4673
4674
4675
4676
4677
4678
4679
4680
4681
4682
4683
4684
4685
4686
4687
4688
4689
4690
4691
4692
4693
4694
4695
4696
4697
4698
4699
4700
4701
4702
4703
4704
4705
4706
4707
4708
4709
4710
4711
4712
4713
4714
4715
4716
4717
4718
4719
4720
4721
4722
4723
4724
4725
4726
4727
4728
4729
4730
4731
4732
4733
4734
4735
4736
4737
4738
4739
4740
4741
4742
4743
4744
4745
4746
4747
4748
4749
4750
4751
4752
4753
4754
4755
4756
4757
4758
4759
4760
4761
4762
4763
4764
4765
4766
4767
4768
4769
4770
4771
4772
4773
4774
4775
4776
4777
4778
4779
4780
4781
4782
4783
4784
4785
4786
4787
4788
4789
4790
4791
4792
4793
4794
4795
4796
4797
4798
4799
4800
4801
4802
4803
4804
4805
4806
4807
4808
4809
4810
4811
4812
4813
4814
4815
4816
4817
4818
4819
4820
4821
4822
4823
4824
4825
4826
4827
4828
4829
4830
4831
4832
4833
4834
4835
4836
4837
4838
4839
4840
4841
4842
4843
4844
4845
4846
4847
4848
4849
4850
4851
4852
4853
4854
4855
4856
4857
4858
4859
4860
4861
4862
4863
4864
4865
4866
4867
4868
4869
4870
4871
4872
4873
4874
4875
4876
4877
4878
4879
4880
4881
4882
4883
4884
4885
4886
4887
4888
4889
4890
4891
4892
4893
4894
4895
4896
4897
4898
4899
4900
4901
4902
4903
4904
4905
4906
4907
4908
4909
4910
4911
4912
4913
4914
4915
4916
4917
4918
4919
4920
4921
4922
4923
4924
4925
4926
4927
4928
4929
4930
4931
4932
4933
4934
4935
4936
4937
4938
4939
4940
4941
4942
4943
4944
4945
4946
4947
4948
4949
4950
4951
4952
4953
4954
4955
4956
4957
4958
4959
4960
4961
4962
4963
4964
4965
4966
4967
4968
4969
4970
4971
4972
4973
4974
4975
4976
4977
4978
4979
4980
4981
4982
4983
4984
4985
4986
4987
4988
4989
4990
4991
4992
4993
4994
4995
4996
4997
4998
4999
5000
5001
5002
5003
5004
5005
5006
5007
5008
5009
5010
5011
5012
5013
5014
5015
5016
5017
5018
5019
5020
5021
5022
5023
5024
5025
5026
5027
5028
5029
5030
5031
5032
5033
5034
5035
5036
5037
5038
5039
5040
5041
5042
5043
5044
5045
5046
5047
5048
5049
5050
5051
5052
5053
5054
5055
5056
5057
5058
5059
5060
5061
5062
5063
5064
5065
5066
5067
5068
5069
5070
5071
5072
5073
5074
5075
5076
5077
5078
5079
5080
5081
5082
5083
5084
5085
5086
5087
5088
5089
5090
5091
5092
5093
5094
5095
5096
5097
5098
5099
5100
5101
5102
5103
5104
5105
5106
5107
5108
5109
5110
5111
5112
5113
5114
5115
5116
5117
5118
5119
5120
5121
5122
5123
5124
5125
5126
5127
5128
5129
5130
5131
5132
5133
5134
5135
5136
5137
5138
5139
5140
5141
5142
5143
5144
5145
5146
5147
5148
5149
5150
5151
5152
5153
5154
5155
5156
5157
5158
5159
5160
5161
5162
5163
5164
5165
5166
5167
5168
5169
5170
5171
5172
5173
5174
5175
5176
5177
5178
5179
5180
5181
5182
5183
5184
5185
5186
5187
5188
5189
5190
5191
5192
5193
5194
5195
5196
5197
5198
5199
5200
5201
5202
5203
5204
5205
5206
5207
5208
5209
5210
5211
5212
5213
5214
5215
5216
5217
5218
5219
5220
5221
5222
5223
5224
5225
5226
5227
5228
5229
5230
5231
5232
5233
5234
5235
5236
5237
5238
5239
5240
5241
5242
5243
5244
5245
5246
5247
5248
5249
5250
5251
5252
5253
5254
5255
5256
5257
5258
5259
5260
5261
5262
5263
5264
5265
5266
5267
5268
5269
5270
5271
5272
5273
5274
5275
5276
5277
5278
5279
5280
5281
5282
5283
5284
5285
5286
5287
5288
5289
5290
5291
5292
5293
5294
5295
5296
5297
5298
5299
5300
5301
5302
5303
5304
5305
5306
5307
5308
5309
5310
5311
5312
5313
5314
5315
5316
5317
5318
5319
5320
5321
5322
5323
5324
5325
5326
5327
5328
5329
5330
5331
5332
5333
5334
5335
5336
5337
5338
5339
5340
5341
5342
5343
5344
5345
5346
5347
5348
5349
5350
5351
5352
5353
5354
5355
5356
5357
5358
5359
5360
5361
5362
5363
5364
5365
5366
5367
5368
5369
5370
5371
5372
5373
5374
5375
5376
5377
5378
5379
5380
5381
5382
5383
5384
5385
5386
5387
5388
5389
5390
5391
5392
5393
5394
5395
5396
5397
5398
5399
5400
5401
5402
5403
5404
5405
5406
5407
5408
5409
5410
5411
5412
5413
5414
5415
5416
5417
5418
5419
5420
5421
5422
5423
5424
5425
5426
5427
5428
5429
5430
5431
5432
5433
5434
5435
5436
5437
5438
5439
5440
5441
5442
5443
5444
5445
5446
5447
5448
5449
5450
5451
5452
5453
5454
5455
5456
5457
5458
5459
5460
5461
5462
5463
5464
5465
5466
5467
5468
5469
5470
5471
5472
5473
5474
5475
5476
5477
5478
5479
5480
5481
5482
5483
5484
5485
5486
5487
5488
5489
5490
5491
5492
5493
5494
5495
5496
5497
5498
5499
5500
5501
5502
5503
5504
5505
5506
5507
5508
5509
5510
5511
5512
5513
5514
5515
5516
5517
5518
5519
5520
5521
5522
5523
5524
5525
5526
5527
5528
5529
5530
5531
5532
5533
5534
5535
5536
5537
5538
5539
5540
5541
5542
5543
5544
5545
5546
5547
5548
5549
5550
5551
5552
5553
5554
5555
5556
5557
5558
5559
5560
5561
5562
5563
5564
5565
5566
5567
5568
5569
5570
5571
5572
5573
5574
5575
5576
5577
5578
5579
5580
5581
5582
5583
5584
5585
5586
5587
5588
5589
5590
5591
5592
5593
5594
5595
5596
5597
5598
5599
5600
5601
5602
5603
5604
5605
5606
5607
5608
5609
5610
5611
5612
5613
5614
5615
5616
5617
5618
5619
5620
5621
5622
5623
5624
5625
5626
5627
5628
5629
5630
5631
5632
5633
5634
5635
5636
5637
5638
5639
5640
5641
5642
5643
5644
5645
5646
5647
5648
5649
5650
5651
5652
5653
5654
5655
5656
5657
5658
5659
5660
5661
5662
5663
5664
5665
5666
5667
5668
5669
5670
5671
5672
5673
5674
5675
5676
5677
5678
5679
5680
5681
5682
5683
5684
5685
5686
5687
5688
5689
5690
5691
5692
5693
5694
5695
5696
5697
5698
5699
5700
5701
5702
5703
5704
5705
5706
5707
5708
5709
5710
5711
5712
5713
5714
5715
5716
5717
5718
5719
5720
5721
5722
5723
5724
5725
5726
5727
5728
5729
5730
5731
5732
5733
5734
5735
5736
5737
5738
5739
5740
5741
5742
5743
5744
5745
5746
5747
5748
5749
5750
5751
5752
5753
5754
5755
5756
5757
5758
5759
5760
5761
5762
5763
5764
5765
5766
5767
5768
5769
5770
5771
5772
5773
5774
5775
5776
5777
5778
5779
5780
5781
5782
5783
5784
5785
5786
5787
5788
5789
5790
5791
5792
5793
5794
5795
5796
5797
5798
5799
5800
5801
5802
5803
5804
5805
5806
5807
5808
5809
5810
5811
5812
5813
5814
5815
5816
5817
5818
5819
5820
5821
5822
5823
5824
5825
5826
5827
5828
5829
5830
5831
5832
5833
5834
5835
5836
5837
5838
5839
5840
5841
5842
5843
5844
5845
5846
5847
5848
5849
5850
5851
5852
5853
5854
5855
5856
5857
5858
5859
5860
5861
5862
5863
5864
5865
5866
5867
5868
5869
5870
5871
5872
5873
5874
5875
5876
5877
5878
5879
5880
5881
5882
5883
5884
5885
5886
5887
5888
5889
5890
5891
5892
5893
5894
5895
5896
5897
5898
5899
5900
5901
5902
5903
5904
5905
5906
5907
5908
5909
5910
5911
5912
5913
5914
5915
5916
5917
5918
5919
5920
5921
5922
5923
5924
5925
5926
5927
5928
5929
5930
5931
5932
5933
5934
5935
5936
5937
5938
5939
5940
5941
5942
5943
5944
5945
5946
5947
5948
5949
5950
5951
5952
5953
5954
5955
5956
5957
5958
5959
5960
5961
5962
5963
5964
5965
5966
5967
5968
5969
5970
5971
5972
5973
5974
5975
5976
5977
5978
5979
5980
5981
5982
5983
5984
5985
5986
5987
5988
5989
5990
5991
5992
5993
5994
5995
5996
5997
5998
5999
6000
6001
6002
6003
6004
6005
6006
6007
6008
6009
6010
6011
6012
6013
6014
6015
6016
6017
6018
6019
6020
6021
6022
6023
6024
6025
6026
6027
6028
6029
6030
6031
6032
6033
6034
6035
6036
6037
6038
6039
6040
6041
6042
6043
6044
6045
6046
6047
6048
6049
6050
6051
6052
6053
6054
6055
6056
6057
6058
6059
6060
6061
6062
6063
6064
6065
6066
6067
6068
6069
6070
6071
6072
6073
6074
6075
6076
6077
6078
6079
6080
6081
6082
6083
6084
6085
6086
6087
6088
6089
6090
6091
6092
6093
6094
6095
6096
6097
6098
6099
6100
6101
6102
6103
6104
6105
6106
6107
6108
6109
6110
6111
6112
6113
6114
6115
6116
6117
6118
6119
6120
6121
6122
6123
6124
6125
6126
6127
6128
6129
6130
6131
6132
6133
6134
6135
6136
6137
6138
6139
6140
6141
6142
6143
6144
6145
6146
6147
6148
6149
6150
6151
6152
6153
6154
6155
6156
6157
6158
6159
6160
6161
6162
6163
6164
6165
6166
6167
6168
6169
6170
6171
6172
6173
6174
6175
6176
6177
6178
6179
6180
6181
6182
6183
6184
6185
6186
6187
6188
6189
6190
6191
6192
6193
6194
6195
6196
6197
6198
6199
6200
6201
6202
6203
6204
6205
6206
6207
6208
6209
6210
6211
6212
6213
6214
6215
6216
6217
6218
6219
6220
6221
6222
6223
6224
6225
6226
6227
6228
6229
6230
6231
6232
6233
6234
6235
6236
6237
6238
6239
6240
6241
6242
6243
6244
6245
6246
6247
6248
6249
6250
6251
6252
6253
6254
6255
6256
6257
6258
6259
6260
6261
6262
6263
6264
6265
6266
6267
6268
6269
6270
6271
6272
6273
6274
6275
6276
6277
6278
6279
6280
6281
6282
6283
6284
6285
6286
6287
6288
6289
6290
6291
6292
6293
6294
6295
6296
6297
6298
6299
6300
6301
6302
6303
6304
6305
6306
6307
6308
6309
6310
6311
6312
6313
6314
6315
6316
6317
6318
6319
6320
6321
6322
6323
6324
6325
6326
6327
6328
6329
6330
6331
6332
6333
6334
6335
6336
6337
6338
6339
6340
6341
6342
6343
6344
6345
6346
6347
6348
6349
6350
6351
6352
6353
6354
6355
6356
6357
6358
6359
6360
6361
6362
6363
6364
6365
6366
6367
6368
6369
6370
6371
6372
6373
6374
6375
6376
6377
6378
6379
6380
6381
6382
6383
6384
6385
6386
6387
6388
6389
6390
6391
6392
6393
6394
6395
6396
6397
6398
6399
6400
6401
6402
6403
6404
6405
6406
6407
6408
6409
6410
6411
6412
6413
6414
6415
6416
6417
6418
6419
6420
6421
6422
6423
6424
6425
6426
6427
6428
6429
6430
6431
6432
6433
6434
6435
6436
6437
6438
6439
6440
6441
6442
6443
6444
6445
6446
6447
6448
6449
6450
6451
6452
6453
6454
6455
6456
6457
6458
6459
6460
6461
6462
6463
6464
6465
6466
6467
6468
6469
6470
6471
6472
6473
6474
6475
6476
6477
6478
6479
6480
6481
6482
6483
6484
6485
6486
6487
6488
6489
6490
6491
6492
6493
6494
6495
6496
6497
6498
6499
6500
6501
6502
6503
6504
6505
6506
6507
6508
6509
6510
6511
6512
6513
6514
6515
6516
6517
6518
6519
6520
6521
6522
6523
6524
6525
6526
6527
6528
6529
6530
6531
6532
6533
6534
6535
6536
6537
6538
6539
6540
6541
6542
6543
6544
6545
6546
6547
6548
6549
6550
6551
6552
6553
6554
6555
6556
6557
6558
6559
6560
6561
6562
6563
6564
6565
6566
6567
6568
6569
6570
6571
6572
6573
6574
6575
6576
6577
6578
6579
6580
6581
6582
6583
6584
6585
6586
6587
6588
6589
6590
6591
6592
6593
6594
6595
6596
6597
6598
6599
6600
6601
6602
6603
6604
6605
6606
6607
6608
6609
6610
6611
6612
6613
6614
6615
6616
6617
6618
6619
6620
6621
6622
6623
6624
6625
6626
6627
6628
6629
6630
6631
6632
6633
6634
6635
6636
6637
6638
6639
6640
6641
6642
6643
6644
6645
6646
6647
6648
6649
6650
6651
6652
6653
6654
6655
6656
6657
6658
6659
6660
6661
6662
6663
6664
6665
6666
6667
6668
6669
6670
6671
6672
6673
6674
6675
6676
6677
6678
6679
6680
6681
6682
6683
6684
6685
6686
6687
6688
6689
6690
6691
6692
6693
6694
6695
6696
6697
6698
6699
6700
6701
6702
6703
6704
6705
6706
6707
6708
6709
6710
6711
6712
6713
6714
6715
6716
6717
6718
6719
6720
6721
6722
6723
6724
6725
6726
6727
6728
6729
6730
6731
6732
6733
6734
6735
6736
6737
6738
6739
6740
6741
6742
6743
6744
6745
6746
6747
6748
6749
6750
6751
6752
6753
6754
6755
6756
6757
6758
6759
6760
6761
6762
6763
6764
6765
6766
6767
6768
6769
6770
6771
6772
6773
6774
6775
6776
6777
6778
6779
6780
6781
6782
6783
6784
6785
6786
6787
6788
6789
6790
6791
6792
6793
6794
6795
6796
6797
6798
6799
6800
6801
6802
6803
6804
6805
6806
6807
6808
6809
6810
6811
6812
6813
6814
6815
6816
6817
6818
6819
6820
6821
6822
6823
6824
6825
6826
6827
6828
6829
6830
6831
6832
6833
6834
6835
6836
6837
6838
6839
6840
6841
6842
6843
6844
6845
6846
6847
6848
6849
6850
6851
6852
6853
6854
6855
6856
6857
6858
6859
6860
6861
6862
6863
6864
6865
6866
6867
6868
6869
6870
6871
6872
6873
6874
6875
6876
6877
6878
6879
6880
6881
6882
6883
6884
6885
6886
6887
6888
6889
6890
6891
6892
6893
6894
6895
6896
6897
6898
6899
6900
6901
6902
6903
6904
6905
6906
6907
6908
6909
6910
6911
6912
6913
6914
6915
6916
6917
6918
6919
6920
6921
6922
6923
6924
6925
6926
6927
6928
6929
6930
6931
6932
6933
6934
6935
6936
6937
6938
6939
6940
6941
6942
6943
6944
6945
6946
6947
6948
6949
6950
6951
6952
6953
6954
6955
6956
6957
6958
6959
6960
6961
6962
6963
6964
6965
6966
6967
6968
6969
6970
6971
6972
6973
6974
6975
6976
6977
6978
6979
6980
6981
6982
6983
6984
6985
6986
6987
6988
6989
6990
6991
6992
6993
6994
6995
6996
6997
6998
6999
7000
7001
7002
7003
7004
7005
7006
7007
7008
7009
7010
7011
7012
7013
7014
7015
7016
7017
7018
7019
7020
7021
7022
7023
7024
7025
7026
7027
7028
7029
7030
7031
7032
7033
7034
7035
7036
7037
7038
7039
7040
7041
7042
7043
7044
7045
7046
7047
7048
7049
7050
7051
7052
7053
7054
7055
7056
7057
7058
7059
7060
7061
7062
7063
7064
7065
7066
7067
7068
7069
7070
7071
7072
7073
7074
7075
7076
7077
7078
7079
7080
7081
7082
7083
7084
7085
7086
7087
7088
7089
7090
7091
7092
7093
7094
7095
7096
7097
7098
7099
7100
7101
7102
7103
7104
7105
7106
7107
7108
7109
7110
7111
7112
7113
7114
7115
7116
7117
7118
7119
7120
7121
7122
7123
7124
7125
7126
7127
7128
7129
7130
7131
7132
7133
7134
7135
7136
7137
7138
7139
7140
7141
7142
7143
7144
7145
7146
7147
7148
7149
7150
7151
7152
7153
7154
7155
7156
7157
7158
7159
7160
7161
7162
7163
7164
7165
7166
7167
7168
7169
7170
7171
7172
7173
7174
7175
7176
7177
7178
7179
7180
7181
7182
7183
7184
7185
7186
7187
7188
7189
7190
7191
7192
7193
7194
7195
7196
7197
7198
7199
7200
7201
7202
7203
7204
7205
7206
7207
7208
7209
7210
7211
7212
7213
7214
7215
7216
7217
7218
7219
7220
7221
7222
7223
7224
7225
7226
7227
7228
7229
7230
7231
7232
7233
7234
7235
7236
7237
7238
7239
7240
7241
7242
7243
7244
7245
7246
7247
7248
7249
7250
7251
7252
7253
7254
7255
7256
7257
7258
7259
7260
7261
7262
7263
7264
7265
7266
7267
7268
7269
7270
7271
7272
7273
7274
7275
7276
7277
7278
7279
7280
7281
7282
7283
7284
7285
7286
7287
7288
7289
7290
7291
7292
7293
7294
7295
7296
7297
7298
7299
7300
7301
7302
7303
7304
7305
7306
7307
7308
7309
7310
7311
7312
7313
7314
7315
7316
7317
7318
7319
7320
7321
7322
7323
7324
7325
7326
7327
7328
7329
7330
7331
7332
7333
7334
7335
7336
7337
7338
7339
7340
7341
7342
7343
7344
7345
7346
7347
7348
7349
7350
7351
7352
7353
7354
7355
7356
7357
7358
7359
7360
7361
7362
7363
7364
7365
7366
7367
7368
7369
7370
7371
7372
7373
7374
7375
7376
7377
7378
7379
7380
7381
7382
7383
7384
7385
7386
7387
7388
7389
7390
7391
7392
7393
7394
7395
7396
7397
7398
7399
7400
7401
7402
7403
7404
7405
7406
7407
7408
7409
7410
7411
7412
7413
7414
7415
7416
7417
7418
7419
7420
7421
7422
7423
7424
7425
7426
7427
7428
7429
7430
7431
7432
7433
7434
7435
7436
7437
7438
7439
7440
7441
7442
7443
7444
7445
7446
7447
7448
7449
7450
7451
7452
7453
7454
7455
7456
7457
7458
7459
7460
7461
7462
7463
7464
7465
7466
7467
7468
7469
7470
7471
7472
7473
7474
7475
7476
7477
7478
7479
7480
7481
7482
7483
7484
7485
7486
7487
7488
7489
7490
7491
7492
7493
7494
7495
7496
7497
7498
7499
7500
7501
7502
7503
7504
7505
7506
7507
7508
7509
7510
7511
7512
7513
7514
7515
7516
7517
7518
7519
7520
7521
7522
7523
7524
7525
7526
7527
7528
7529
7530
7531
7532
7533
7534
7535
7536
7537
7538
7539
7540
7541
7542
7543
7544
7545
7546
7547
7548
7549
7550
7551
7552
7553
7554
7555
7556
7557
7558
7559
7560
7561
7562
7563
7564
7565
7566
7567
7568
7569
7570
7571
7572
7573
7574
7575
7576
7577
7578
7579
7580
7581
7582
7583
7584
7585
7586
7587
7588
7589
7590
7591
7592
7593
7594
7595
7596
7597
7598
7599
7600
7601
7602
7603
7604
7605
7606
7607
7608
7609
7610
7611
7612
7613
7614
7615
7616
7617
7618
7619
7620
7621
7622
7623
7624
7625
7626
7627
7628
7629
7630
7631
7632
7633
7634
7635
7636
7637
7638
7639
7640
7641
7642
7643
7644
7645
7646
7647
7648
7649
7650
7651
7652
7653
7654
7655
7656
7657
7658
7659
7660
7661
7662
7663
7664
7665
7666
7667
7668
7669
7670
7671
7672
7673
7674
7675
7676
7677
7678
7679
7680
7681
7682
7683
7684
7685
7686
7687
7688
7689
7690
7691
7692
7693
7694
7695
7696
7697
7698
7699
7700
7701
7702
7703
7704
7705
7706
7707
7708
7709
7710
7711
7712
7713
7714
7715
7716
7717
7718
7719
7720
7721
7722
7723
7724
7725
7726
7727
7728
7729
7730
7731
7732
7733
7734
7735
7736
7737
7738
7739
7740
7741
7742
7743
7744
7745
7746
7747
7748
7749
7750
7751
7752
7753
7754
7755
7756
7757
7758
7759
7760
7761
7762
7763
7764
7765
7766
7767
7768
7769
7770
7771
7772
7773
7774
7775
7776
7777
7778
7779
7780
7781
7782
7783
7784
7785
7786
7787
7788
7789
7790
7791
7792
7793
7794
7795
7796
7797
7798
7799
7800
7801
7802
7803
7804
7805
7806
7807
7808
7809
7810
7811
7812
7813
7814
7815
7816
7817
7818
7819
7820
7821
7822
7823
7824
7825
7826
7827
7828
7829
7830
7831
7832
7833
7834
7835
7836
7837
7838
7839
7840
7841
7842
7843
7844
7845
7846
7847
7848
7849
7850
7851
7852
7853
7854
7855
7856
7857
7858
7859
7860
7861
7862
7863
7864
7865
7866
7867
7868
7869
7870
7871
7872
7873
7874
7875
7876
7877
7878
7879
7880
7881
7882
7883
7884
7885
7886
7887
7888
7889
7890
7891
7892
7893
7894
7895
7896
7897
7898
7899
7900
7901
7902
7903
7904
7905
7906
7907
7908
7909
7910
7911
7912
7913
7914
7915
7916
7917
7918
7919
7920
7921
7922
7923
7924
7925
7926
7927
7928
7929
7930
7931
7932
7933
7934
7935
7936
7937
7938
7939
7940
7941
7942
7943
7944
7945
7946
7947
7948
7949
7950
7951
7952
7953
7954
7955
7956
7957
7958
7959
7960
7961
7962
7963
7964
7965
7966
7967
7968
7969
7970
7971
7972
7973
7974
7975
7976
7977
7978
7979
7980
7981
7982
7983
7984
7985
7986
7987
7988
7989
7990
7991
7992
7993
7994
7995
7996
7997
7998
7999
8000
8001
8002
8003
8004
8005
8006
8007
8008
8009
8010
8011
8012
8013
8014
8015
8016
8017
8018
8019
8020
8021
8022
8023
8024
8025
8026
8027
8028
8029
8030
8031
8032
8033
8034
8035
8036
8037
8038
8039
8040
8041
8042
8043
8044
8045
8046
8047
8048
8049
8050
8051
8052
8053
8054
8055
8056
8057
8058
8059
8060
8061
8062
8063
8064
8065
8066
8067
8068
8069
8070
8071
8072
8073
8074
8075
8076
8077
8078
8079
8080
8081
8082
8083
8084
8085
8086
8087
8088
8089
8090
8091
8092
8093
8094
8095
8096
8097
8098
8099
8100
8101
8102
8103
8104
8105
8106
8107
8108
8109
8110
8111
8112
8113
8114
8115
8116
8117
8118
8119
8120
8121
8122
8123
8124
8125
8126
8127
8128
8129
8130
8131
8132
8133
8134
8135
8136
8137
8138
8139
8140
8141
8142
8143
8144
8145
8146
8147
8148
8149
8150
8151
8152
8153
8154
8155
8156
8157
8158
8159
8160
8161
8162
8163
8164
8165
8166
8167
8168
8169
8170
8171
8172
8173
8174
/*******************************************************************************



This file contains the code to replicate the tables and figures in the appendix.



	Contents:

	0. Generate or import final dataset



	A. DATA APPENDIX

	Table A1: Summary statistics

	

	B. ROBUSTNESS

	

	C. IV ESTIMATION FOR MEN

	

	D. ADDITIONAL DESCRIPTIVE EVIDENCE

	

		

The file "3 Replication package\programs00_setup.do" installs the required ado 

packages sets. It also sets the path to all directories. If you want to run this 

do file individually, after installing the required packages unnmark and specify 

location of parent directory here:



	*global dirroot = "SPECIFY\LOCATION\OF\PARENT\DIRECTORY\HERE"



and then run the following lines for the secondary directories (specified as 

downloaded in replication package):



	global dirdata = "${dirroot}\data"

	global dirdta  = "${dirroot}\programs\01_dataprep\dta-temp-files"

	global dirresu = "${dirroot}\programs\results"



*******************************************************************************/


* Set-up:

	do "${dirroot}\programs\00_setup.do"

* ==============================================================================
* 0. GENERATE OR IMPORT FINAL DATASET:
* ------------------------------------------------------------------------------
clear all
set more off
set matsize 10000

* To generate the final dataset run the following line:

	do "${dirroot}\programs\01_dataprep\master-dataprep.do"

* Alternatively, you can import the final dataset data directly by writting
* use "${dirdata}\final-data.dta", clear

* ==============================================================================




********************************************************************************
********************************************************************************
********************************************************************************
*																			   *
*	 							A. DATA APPENDIX							   *
*																			   *
********************************************************************************
********************************************************************************
********************************************************************************

	

	
* ==============================================================================
* TABLE A1: 
* Summary statistics
* ------------------------------------------------------------------------------

* Import final dataset
use "${dirdata}\final-data.dta", clear
gen pr5 = (pr4!=1)
label var pr5 "Viscount's/baron's daughter"
global tosum cOut mheir fmissmatch fmissmatch2 fdown celibacy syntheticT mourn pr4 pr5 biorder hengpee hscotpee hiripee

* Summary stats (baseline)
preserve
foreach x in fmissmatch fmissmatch2 fdown celibacy{ 	// just so that variabels appear in the desired order
replace `x'=.
}
eststo sumbase: qui estpost summ $tosum if base_sample==1
estadd local obs = `e(N)'
restore

* Summary stats (married in landed elite)
preserve
replace celibacy = .
eststo sumlelite: qui estpost summ $tosum if base_sample==1 & fmissmatch!=.
estadd local obs = `e(N)'
restore

* Summary stats (including unmarried)
preserve
foreach x in cOut mheir fmissmatch fmissmatch2 fdown mourn{
replace `x'=.
}
eststo sumunmarr: qui estpost summ $tosum if marital_rates_sample==1
estadd local obs = `e(N)'
restore

* Output
esttab sumbase sumlelite sumunmarr using "${dirresu}\Appendix\TableA1.tex", ///
	mlabels("Baseline" "Land elite" "Inc unmarr") ///
	cells("mean(pattern(1 1 1) fmt(2)) sd(pattern(1 1 1) fmt(2))") ///
	stats(obs, labels("Observations") fmt(%9.0gc)) ///
	noobs label replace

	
	
* ==============================================================================
* TABLE A2: 
* Summary statistics, women with a family seat
* ------------------------------------------------------------------------------

* Import final dataset
use "${dirdata}\final-data.dta", clear
gen pr5 = (pr4!=1)
label var pr5 "Viscount's/baron's daughter"
global tosum cOut mheir fmissmatch fmissmatch2 fdown celibacy syntheticT mourn distlondon pr4 pr5 biorder hengpee hscotpee hiripee

* Sample: women with a family seat
keep if distlondon!=.

* Summary stats (baseline)
preserve
foreach x in fmissmatch fmissmatch2 fdown celibacy{ 	// just so that variabels appear in the desired order
replace `x'=.
}
eststo sumbase: qui estpost summ $tosum if base_sample==1
estadd local obs = `e(N)'
restore

* Summary stats (married in landed elite)
preserve
replace celibacy = .
eststo sumlelite: qui estpost summ $tosum if base_sample==1 & fmissmatch!=.
estadd local obs = `e(N)'
restore

* Summary stats (including unmarried)
preserve
foreach x in cOut mheir fmissmatch fmissmatch2 fdown mourn{
replace `x'=.
}
eststo sumunmarr: qui estpost summ $tosum if marital_rates_sample==1
estadd local obs = `e(N)'
restore

* Output
esttab sumbase sumlelite sumunmarr using "${dirresu}\Appendix\TableA2.tex", ///
	mlabels("Baseline" "Land elite" "Inc unmarr") ///
	cells("mean(pattern(1 1 1) fmt(2)) sd(pattern(1 1 1) fmt(2))") ///
	stats(obs, labels("Observations") fmt(%9.0gc)) ///
	noobs label replace

	
	
* ==============================================================================
* TABLE A3: 
* Summary statistics for marriage and political power
* ------------------------------------------------------------------------------

* Import data
use "${dirdata}/final-data-sec4.dta", clear
gen pr5 = (pr4!=1)
label var pr5 "Viscount's/baron's daughter"
global tosum d_MPb_aft MPb_years_aft d_MPb_locaft2 MPb_years_locaft d_MPh_aft MPh_years_aft ///
			 cOut syntheticT pr4 pr5 nsib biorder distlondon ///
			 d_MPf_bef MPf_years_bef d_MPf_local2  MPf_years_locbef d_MPh_bef MPh_years_bef

* Collapse data such that a woman (and her family) is the unit of observation
collapse $tosum, by(refno)
label var d_MPb_aft "Any brother is MP (after)"
label var MPb_years_aft "All brothers MP years (after)"
label var d_MPb_locaft2 "Any brother is local MP (after)"
label var MPb_years_locaft "All brothers' local MP years (after)"
label var d_MPh_aft "Family head is MP (after)"
label var MPh_years_aft "Family head's MP years (after)"
label var cOut "Married a commoner"
label var syntheticT "Treatment (synthetic prob)"
label var pr4 "Duke, marquis, earl's family"
label var pr5 "Viscount, baron's family"
label var nsib "Number of brothers"
label var biorder "Woman's birth order (excl. heirs)"
label var distlondon "Distance to London"
label var d_MPf_bef "Father is MP (before)"
label var MPf_years_bef "Father's MP years (before)"
label var d_MPf_local2 "Father is local MP (before)"
label var MPf_years_locbef "Father's local MP years (before)"
label var d_MPh_bef	"Family head is MP (before)"
label var MPh_years_bef "Family head's MP years (before)"

	/*  Note: the following measures of political power before marriage are used 

	only for regressions of brothers' political power.  */

foreach x in d_MPb_aft MPb_years_aft d_MPb_locaft2 MPb_years_locaft d_MPf_bef MPf_years_bef d_MPf_local2 MPf_years_locbef{ 
replace `x' = . if nsib==.
}

* Summary stats
eststo sumA3: qui estpost summ $tosum

* Summary stats for county controls (Hechter)
preserve
use "${dirdata}/final-data-sec4.dta", clear
collapse propman100 logincome propcon propnonc relig, by(countyN) // Collapse data such that a county is the unit of observation
gen propcon100 = propcon*100 // to have them as percentages (%)
gen propnonc100 = propnonc*100
label var propman100 "perc. working in manufacturing"
label var logincome "income pc (in logs)"
label var propcon100 "perc. voting conservative, 1885"
label var propnonc100 "perc. non-conformists"
label var relig "religiosity"

* Summary stats
eststo sumhech: qui estpost summ propman100 logincome propcon100 propnonc100 relig
restore

* Output
esttab sumA3 sumhech using "${dirresu}\Appendix\TableA3.tex", ///
	cells("mean(fmt(2)) sd(fmt(2)) min(fmt(2)) max(fmt(2)) count(fmt(0))") ///
	noobs label replace

	
	
* ==============================================================================
* TABLE A4: 
* MP elections, by type of constituency
* ------------------------------------------------------------------------------

* PANEL A: Distance from family seat to closest enfranchised constituency:

* import auxiliary appendix data with all dyads between constituencies' centroids
* and family seats in Section IV's sample. To consider enfranchized constituencies
* only, I exclude rotten boroughs.

import delimited "${dirroot}\programs\03_appendix\aux data appendix\seat_constituency_pairs_same_projection.txt", clear 

	/*  Note: For consistency, in this data file both seats and constituencies 

	are geolocated using the WGS84 projection.  */
	
* Calculate distance from seats to enfranchised constituencies
forvalues i=1/179{
geodist wgslat_sea wgslon_sea wgslat_cnt`i' wgslon_cnt`i', gen(dist`i') miles
}
egen dist_min = rowmin(dist1	dist2	dist3	dist4	dist5	dist6	dist7	dist8	dist9	dist10	dist11	dist12	dist13	dist14	dist15	dist16	dist17	dist18	dist19	dist20	dist21	dist22	dist23	dist24	dist25	dist26	dist27	dist28	dist29	dist30	dist31	dist32	dist33	dist34	dist35	dist36	dist37	dist38	dist39	dist40	dist41	dist42	dist43	dist44	dist45	dist46	dist47	dist48	dist49	dist50	dist51	dist52	dist53	dist54	dist55	dist56	dist57	dist58	dist59	dist60	dist61	dist62	dist63	dist64	dist65	dist66	dist67	dist68	dist69	dist70	dist71	dist72	dist73	dist74	dist75	dist76	dist77	dist78	dist79	dist80	dist81	dist82	dist83	dist84	dist85	dist86	dist87	dist88	dist89	dist90	dist91	dist92	dist93	dist94	dist95	dist96	dist97	dist98	dist99	dist100	dist101	dist102	dist103	dist104	dist105	dist106	dist107	dist108	dist109	dist110	dist111	dist112	dist113	dist114	dist115	dist116	dist117	dist118	dist119	dist120	dist121	dist122	dist123	dist124	dist125	dist126	dist127	dist128	dist129	dist130	dist131	dist132	dist133	dist134	dist135	dist136	dist137	dist138	dist139	dist140	dist141	dist142	dist143	dist144	dist145	dist146	dist147	dist148	dist149	dist150	dist151	dist152	dist153	dist154	dist155	dist156	dist157	dist158	dist159	dist160	dist161	dist162	dist163	dist164	dist165	dist166	dist167	dist168	dist169	dist170	dist171	dist172	dist173	dist174	dist175	dist176	dist177	dist178	dist179)
label var dist_min "Distance (in miles)"

* Summary stats
eststo sumdist: qui estpost summ dist_min
esttab sumdist using "${dirresu}\Appendix\TableA4_A.tex", ///
	cells("mean(fmt(2)) sd(fmt(2)) max(fmt(2))") ///
	noobs label replace
	


* PANEL B: MP elections, by type of constituency:

	/*  Note: first, reshape peers' biographies dataset to have one entry for 

	    each election (i.e., seat in parliament)  */

use "${dirdta}/Peers_biographies.dta", clear
sort refno
* 1. drop sisters
drop if refno==157408
encode fbio1, gen(nfam)
duplicates drop nfam, force
* 2. drop repeated biographies
foreach x in bio1 bio2{
rename f`x' `x'_0
rename h`x' `x'_11
replace `x'_11 = "" if fhead_1872!="other"
}
foreach x in 1 2 3 4 5 6 7 8 9 10{
rename b`x'bio1 bio1_`x'
rename b`x'bio2 bio2_`x'
}
qui sum MPf
local y = r(max)
if `y'>0{
	forvalues j = 1/`y'{
	gen term1_`j'_0 = term1f_`j'
	gen cnsty_`j'_0 = cnstyf_`j'
	gen cnstycounty_`j'_0 = cnstycountyf_`j' 
	gen cnstywgs_`j'_0 = cnstywgsf_`j' 
	}
}
*
qui sum MPh
local y = r(max)
if `y'>0{
	forvalues j = 1/`y'{
	gen term1_`j'_11 = term1h_`j'
	gen cnsty_`j'_11 = cnstyh_`j'
	gen cnstycounty_`j'_11 = cnstycountyh_`j' 
	gen cnstywgs_`j'_11 = cnstywgsh_`j' 
	}
}
*
foreach b in 1 2 3 4 5 6 7 8 9 10{
qui sum MPb`b'
local y = r(max)
if `y'>0{
	forvalues j = 1/`y'{
	gen term1_`j'_`b' = term1b`b'_`j'
	gen cnsty_`j'_`b' = cnstyb`b'_`j'
	gen cnstycounty_`j'_`b' = cnstycountyb`b'_`j' 
	gen cnstywgs_`j'_`b' = cnstywgsb`b'_`j' 
	}
}
}
drop firstnames surname title byear dyear myear wom mourn cOut cs fhead_1872 fhead_1878 relation_f relation_b1 heir_b1 note relation_b2 heir_b2 relation_b3 heir_b3 relation_b4 heir_b4 relation_b5 heir_b5 relation_b6 heir_b6 relation_b7 heir_b7 relation_b8 heir_b8 relation_b9 heir_b9 relation_b10 heir_b10 fh b1h b2h b3h b4h b5h b6h b7h b8h b9h b10h relation_b11 heir_b11 b11h byear_b01 byear_b1 byear_b02 byear_b2 byear_b03 byear_b3 byear_b04 byear_b4 byear_b05 byear_b5 byear_b06 byear_b6 byear_b07 byear_b7 byear_b08 byear_b8 byear_b09 byear_b9 byear_b010 byear_b10 myear_f_0 myear_f_alt myear_f myear_h_0 myear_h_alt myear_h myear_b1_0 myear_b1_alt myear_b1 myear_b2_0 myear_b2_alt myear_b2 myear_b3_0 myear_b3_alt myear_b3 myear_b4_0 myear_b4_alt myear_b4 myear_b5_0 myear_b5_alt myear_b5 myear_b6_0 myear_b6_alt myear_b6 myear_b7_0 myear_b7_alt myear_b7 myear_b8_0 myear_b8_alt myear_b8 myear_b9_0 myear_b9_alt myear_b9 myear_b10_0 myear_b10_alt myear_b10 d_MPf MP1fcount MP1fpos1 MP1fpos2 MP1fpos3 MP1fpos4 MP1fpos5 MPf d_MPh MP1hcount MP1hpos1 MP1hpos2 MP1hpos3 MP1hpos4 MPh d_MPb1 MP1b1count MP1b1pos1 MP1b1pos2 MP1b1pos3 MP1b1pos4 MP1b1pos5 MPb1 d_MPb2 MP1b2count MP1b2pos1 MP1b2pos2 MP1b2pos3 MP1b2pos4 MPb2 d_MPb3 MP1b3count MP1b3pos1 MP1b3pos2 MP1b3pos3 MP1b3pos4 MP1b3pos5 MP1b3pos6 MP1b3pos7 MP1b3pos8 MPb3 d_MPb4 MP1b4count MP1b4pos1 MP1b4pos2 MP1b4pos3 MPb4 d_MPb5 MP1b5count MP1b5pos1 MPb5 d_MPb6 MP1b6count MP1b6pos1 MPb6 d_MPb7 MP1b7count MP1b7pos1 MPb7 d_MPb8 MP1b8count MP1b8pos1 MPb8 d_MPb9 MP1b9count MPb9 d_MPb10 MP1b10count MPb10 MPb d_MPb libf toryf MPallf_1 partyf_1 cnstyf_1 term1f_1 term2f_1 MPallf_2 partyf_2 cnstyf_2 term1f_2 term2f_2 MPallf_3 partyf_3 cnstyf_3 term1f_3 term2f_3 MPallf_4 partyf_4 cnstyf_4 term1f_4 term2f_4 MPallf_5 partyf_5 cnstyf_5 term1f_5 term2f_5 libh toryh MPallh_1 partyh_1 cnstyh_1 term1h_1 term2h_1 MPallh_2 partyh_2 cnstyh_2 term1h_2 term2h_2 MPallh_3 partyh_3 cnstyh_3 term1h_3 term2h_3 MPallh_4 partyh_4 cnstyh_4 term1h_4 term2h_4 libb1 toryb1 MPallb1_1 partyb1_1 cnstyb1_1 term1b1_1 term2b1_1 MPallb1_2 partyb1_2 cnstyb1_2 term1b1_2 term2b1_2 MPallb1_3 partyb1_3 cnstyb1_3 term1b1_3 term2b1_3 MPallb1_4 partyb1_4 cnstyb1_4 term1b1_4 term2b1_4 MPallb1_5 partyb1_5 cnstyb1_5 term1b1_5 term2b1_5 libb2 toryb2 MPallb2_1 partyb2_1 cnstyb2_1 term1b2_1 term2b2_1 MPallb2_2 partyb2_2 cnstyb2_2 term1b2_2 term2b2_2 MPallb2_3 partyb2_3 cnstyb2_3 term1b2_3 term2b2_3 MPallb2_4 partyb2_4 cnstyb2_4 term1b2_4 term2b2_4 libb3 toryb3 MPallb3_1 partyb3_1 cnstyb3_1 term1b3_1 term2b3_1 MPallb3_2 partyb3_2 cnstyb3_2 term1b3_2 term2b3_2 MPallb3_3 partyb3_3 cnstyb3_3 term1b3_3 term2b3_3 MPallb3_4 partyb3_4 cnstyb3_4 term1b3_4 term2b3_4 MPallb3_5 partyb3_5 cnstyb3_5 term1b3_5 term2b3_5 MPallb3_6 partyb3_6 cnstyb3_6 term1b3_6 term2b3_6 MPallb3_7 partyb3_7 cnstyb3_7 term1b3_7 term2b3_7 MPallb3_8 partyb3_8 cnstyb3_8 term1b3_8 term2b3_8 libb4 toryb4 MPallb4_1 partyb4_1 cnstyb4_1 term1b4_1 term2b4_1 MPallb4_2 partyb4_2 cnstyb4_2 term1b4_2 term2b4_2 MPallb4_3 partyb4_3 cnstyb4_3 term1b4_3 term2b4_3 libb5 toryb5 MPallb5_1 partyb5_1 cnstyb5_1 term1b5_1 term2b5_1 libb6 toryb6 MPallb6_1 partyb6_1 cnstyb6_1 term1b6_1 term2b6_1 libb7 toryb7 MPallb7_1 partyb7_1 cnstyb7_1 term1b7_1 term2b7_1 libb8 toryb8 MPallb8_1 partyb8_1 cnstyb8_1 term1b8_1 term2b8_1 libb9 toryb9 libb10 toryb10 MPh_years MPh_years_aft d_MPh_bef d_MPh_aft MPf_years MPf_years_aft d_MPf_bef d_MPf_aft MPb1_years MPb1_years_aft d_MPb1_bef d_MPb1_aft MPb2_years MPb2_years_aft d_MPb2_bef d_MPb2_aft MPb3_years MPb3_years_aft d_MPb3_bef d_MPb3_aft MPb4_years MPb4_years_aft d_MPb4_bef d_MPb4_aft MPb5_years MPb5_years_aft d_MPb5_bef d_MPb5_aft MPb6_years MPb6_years_aft d_MPb6_bef d_MPb6_aft MPb7_years MPb7_years_aft d_MPb7_bef d_MPb7_aft MPb8_years MPb8_years_aft d_MPb8_bef d_MPb8_aft MPb9_years MPb9_years_aft d_MPb9_bef d_MPb9_aft MPb10_years MPb10_years_aft d_MPb10_bef d_MPb10_aft d_MPb_aft d_MPb_bef MPb_years MPb_years_aft cnstycountyh_1 cnstywgsh_1 cnstycountyh_2 cnstywgsh_2 cnstycountyh_3 cnstywgsh_3 cnstycountyh_4 cnstywgsh_4 cnstycountyf_1 cnstywgsf_1 cnstycountyf_2 cnstywgsf_2 cnstycountyf_3 cnstywgsf_3 cnstycountyf_4 cnstywgsf_4 cnstycountyf_5 cnstywgsf_5 cnstycountyb1_1 cnstywgsb1_1 cnstycountyb1_2 cnstywgsb1_2 cnstycountyb1_3 cnstywgsb1_3 cnstycountyb1_4 cnstywgsb1_4 cnstycountyb1_5 cnstywgsb1_5 cnstycountyb2_1 cnstywgsb2_1 cnstycountyb2_2 cnstywgsb2_2 cnstycountyb2_3 cnstywgsb2_3 cnstycountyb2_4 cnstywgsb2_4 cnstycountyb3_1 cnstywgsb3_1 cnstycountyb3_2 cnstywgsb3_2 cnstycountyb3_3 cnstywgsb3_3 cnstycountyb3_4 cnstywgsb3_4 cnstycountyb3_5 cnstywgsb3_5 cnstycountyb3_6 cnstywgsb3_6 cnstycountyb3_7 cnstywgsb3_7 cnstycountyb3_8 cnstywgsb3_8 cnstycountyb4_1 cnstywgsb4_1 cnstycountyb4_2 cnstywgsb4_2 cnstycountyb4_3 cnstywgsb4_3 cnstycountyb5_1 cnstywgsb5_1 cnstycountyb6_1 cnstywgsb6_1 cnstycountyb7_1 cnstywgsb7_1 cnstycountyb8_1 cnstywgsb8_1 fhead_1872_2 nfhead_1872 nsib nfam b11bio1 b11bio2
* reshape to have one entry per relative (i.e., fathers and borthers)
reshape long bio1_ bio2_ term1_1_ term1_2_ term1_3_ term1_4_ term1_5_ term1_6_ term1_7_ term1_8_ ///
						 cnsty_1_ cnsty_2_ cnsty_3_ cnsty_4_ cnsty_5_ cnsty_6_ cnsty_7_ cnsty_8_ ///
						 cnstycounty_1_ cnstycounty_2_ cnstycounty_3_ cnstycounty_4_ cnstycounty_5_ cnstycounty_6_ cnstycounty_7_ cnstycounty_8_ ///
						 cnstywgs_1_ cnstywgs_2_ cnstywgs_3_ cnstywgs_4_ cnstywgs_5_ cnstywgs_6_ cnstywgs_7_ cnstywgs_8_ ///
						 , i(refno) j(nrelative)
drop if bio1_=="" & bio2_==""
sort bio1_ bio2_ refno nrelative
by bio1_ bio2_: gen tokeep = _n
keep if tokeep==1
count // As before, I have 674 unique biographies
* 3. Reshape to have one election per entry
forvalues i=1/8{
rename term1_`i'_ term`i'
rename cnsty_`i'_ pc`i'
rename cnstycounty_`i'_ pccounty`i'
rename cnstywgs_`i'_ pcwgs`i'
}
gen idbio = _n
drop refno bio1_ bio2_ tokeep
reshape long term pc pccounty pcwgs, i(idbio) j(nelection)
sort idbio term nelection
keep if term!=.
rename pc cnsty
replace cnsty = "Egremont" if idbio==90
replace cnsty = "South Ayrshire" if idbio==587

* merge with constituency data (including type of constituency)
preserve 
import excel "${dirdata}\constituencies.xlsx", sheet("Sheet1") firstrow clear
keep cnsty note
label var cnsty "Electoral constituency"
label var note "Type of constituency"
save "${dirdta}\constituencies.dta", replace
restore
merge m:1 cnsty using "${dirdta}\constituencies.dta"
* housekeeping
replace note = "County constituency" if cnsty=="Ayrshire" | cnsty=="Caernarvonshire" | cnsty=="Hertfordshire" | cnsty=="Cumberland, Egremont Divisio"
replace note = "Rotten borough" if cnsty=="Newport Cornwall"
replace note = "Borough constituency" if cnsty=="Scarborough" | cnsty=="Brentwood" | cnsty=="Enfield"

* Panel B's sumary stats
capture log close				// save results in log file
cd "${dirresu}\Appendix"
log using TableA4_B.log, replace

*  All
tab note

*  Fathers
tab note if nrelative==0

*  Brothers
tab note if nrelative!=0

log close

	
	
* ==============================================================================
* TABLE A5: 
* Summary statistics for determinants of education provision
* ------------------------------------------------------------------------------

* Import data
use "${dirdata}/final-data-sec4.dta", clear
gen pr5 = (pr4!=1)
label var pr5 "Viscount's/baron's daughter"

* Add total funds raised from rates (pounds)
preserve 
import delimited "${dirroot}\programs\03_appendix\aux data appendix\funds_from_rates.txt", encoding(ISO-8859-2) clear 
label var refno "Reference number in Hollingsworth"
label var fid_b10sea "FID identifier for family seats of women in baseline sample (ArcGIS)"
label var rate_pound "Funds from rates (pound)"
save "${dirdta}/funds-from-rates.dta", replace
restore 
merge 1:1 refno fid_b10sea using "${dirdta}/funds-from-rates.dta", nogen
gen rate_log = log(rate_pound)

* Labels
label var rateperpou "Tax rate average"
label var rate_pound "Funds from rates (pound)"
label var numsb "Number of School Boards"
label var d_MPb_aft "Any brother is MP (after)"
label var MPb_years_aft "All brothers MP years (after)"
label var d_MPb_locaft "Any brother is local MP (after)"
label var MPb_years_locaft "All brothers' local MP years (after)"
label var d_MPh_aft "Family head is MP (after)"
label var MPh_years_aft "Family head's MP years (after)"
label var cOut "Married a commoner"
label var syntheticT "Treatment (synthetic prob)"
label var pr4 "Duke, marquis, earl's family"
label var pr5 "Viscount, baron's family"
label var nsib "Number of brothers"
label var biorder "Woman's birth order (excl. heirs)"
label var distlondon "Distance to London"
label var d_MPf_bef "Father is MP (before)"
label var MPf_years_bef "Father's MP years (before)"
label var d_MPf_local2 "Father is local MP (before)"
label var MPf_years_locbef "Father's local MP years (before)"
label var d_MPh_bef	"Family head is MP (before)"
label var MPh_years_bef "Family head's MP years (before)"

global tosum rateperpou rate_pound numsb ///
			 d_MPb_aft MPb_years_aft d_MPb_locaft MPb_years_locaft d_MPh_aft MPh_years_aft ///
			 cOut syntheticT pr4 pr5 nsib biorder distlondon  ///
			 d_MPf_bef MPf_years_bef d_MPf_local MPf_years_locbef d_MPh_bef MPh_years_bef

	/*  Note: the following measures of political power before marriage are used 

	only for regressions of brothers' political power.  */

foreach x in d_MPb_aft MPb_years_aft d_MPb_locaft MPb_years_locaft MPf_years_bef MPf_years_locbef d_MPf_bef d_MPf_local{
replace `x' = . if nsib==.
}

* Summary stats
eststo sumA5: qui estpost summ $tosum

* Output
esttab sumA5 using "${dirresu}\Appendix\TableA5.tex", ///
	cells("mean(fmt(2)) sd(fmt(2)) min(fmt(2)) max(fmt(2)) count(fmt(0))") ///
	noobs label replace

	
* ==============================================================================
* FIGURE A7: 
* MP elections overtime.
* ------------------------------------------------------------------------------

* Import data
use "${dirdta}/Peers_biographies.dta", clear

* Variables for time series figure
gen heir_f = 1
local i = 1
local rel = 1
foreach z in f b1 b2 b3 b4 b5 b6 b7 b8 b9 b10{
qui sum MP`z'
local y = r(max)
if `y'>0{
	forvalues j = 1/`y'{
	gen year0_`i' = term1`z'_`j'
	if `rel'<2{
	gen whoel_`i' = 0
	}
	if `rel'>1{
	gen whoel_`i' = .
	replace whoel_`i' = 1 if heir_`z'==1
	replace whoel_`i' = 2 if heir_`z'==0
	}
	local i = `i'+1
	}
}
local rel = `rel'+1
}
gen MPtot = MPb+MPf
keep refno fbio1 MPtot year0_1 whoel_1 year0_2 whoel_2 year0_3 whoel_3 year0_4 whoel_4 year0_5 whoel_5 year0_6 whoel_6 year0_7 whoel_7 year0_8 whoel_8 year0_9 whoel_9 year0_10 whoel_10 year0_11 whoel_11 year0_12 whoel_12 year0_13 whoel_13 year0_14 whoel_14 year0_15 whoel_15 year0_16 whoel_16 year0_17 whoel_17 year0_18 whoel_18 year0_19 whoel_19 year0_20 whoel_20 year0_21 whoel_21 year0_22 whoel_22 year0_23 whoel_23 year0_24 whoel_24 year0_25 whoel_25 year0_26 whoel_26 year0_27 whoel_27 year0_28 whoel_28 year0_29 whoel_29
bysort fbio1: gen sis = _n
keep if sis==1
reshape long year0_ whoel_, i(refno) j(j)
gen MPi = j
drop j
drop if year0_ == .
gen MPcount = 1
gen MPfcount = 1 if whoel_==0
gen MPhcount = 1 if whoel_==1
gen MPbcount = 1 if whoel_==2
collapse (sum) MPcount (sum) MPfcount (sum) MPhcount (sum) MPbcount, by(year0_)
tsset year0_
drop if year0_>=1889
tssmooth ma MPcount_ma = MPcount, window(2,1,2)
tssmooth ma MPfcount_ma = MPfcount, window(2,1,2)
tssmooth ma MPhcount_ma = MPhcount, window(2,1,2)
tssmooth ma MPbcount_ma = MPbcount, window(2,1,2)

* figure
twoway 	(scatter MPfcount year0_ if MPfcount>0 & year0_>1820 & year0_<1890, mcolor(black) ms(circle_hollow)) ///
		(scatter MPhcount year0_ if MPhcount>0 & year0_>1820 & year0_<1890, mcolor(black)) ///
		(scatter MPbcount year0_ if MPbcount>0 & year0_>1820 & year0_<1890, mcolor(black) ms(X)) ///
		(line MPcount_ma year0_ if MPcount_ma>0 & year0_>1820 & year0_<1890, lcolor(black)) ///
		(scatteri 0.5 1867 0.56 1867 , recast(line) lpattern(dash) lcolor(dkgreen)) ///
		(scatteri 0.5 1837 0.56 1837 , recast(line) lpattern(dot) lcolor(gray)), ///
		xlabel(1825(10)1885) xtitle("") ytitle("MP appointments") ylabel(0(5)20, nogrid) ///
		legend(on order(1 "Fathers" 2 "Brothers (heirs)" 3 "Brothers (non-heirs)" 4 "Moving avg.(2,2), total" 5 "Reform Act" 6 "General Election") rows(6) ring(0) position(1) bmargin(large)) ///
		xline(1832, lpattern(dash) lcolor(dkgreen)) xline(1867, lpattern(dash) lcolor(dkgreen)) ///
		xline(1884, lpattern(dash) lcolor(dkgreen))  ///
		xline(1826, lpattern(dot) lcolor(gray)) xline(1830, lpattern(dot) lcolor(gray)) ///		
		xline(1831, lpattern(dot) lcolor(gray)) xline(1832, lpattern(dot) lcolor(gray)) ///		
		xline(1835, lpattern(dot) lcolor(gray)) xline(1837, lpattern(dot) lcolor(gray)) ///		
		xline(1841, lpattern(dot) lcolor(gray)) xline(1847, lpattern(dot) lcolor(gray)) ///		
		xline(1852, lpattern(dot) lcolor(gray)) xline(1857, lpattern(dot) lcolor(gray)) ///		
		xline(1859, lpattern(dot) lcolor(gray)) xline(1865, lpattern(dot) lcolor(gray)) ///		
		xline(1868, lpattern(dot) lcolor(gray)) xline(1874, lpattern(dot) lcolor(gray)) ///		
		xline(1880, lpattern(dot) lcolor(gray)) xline(1885, lpattern(dot) lcolor(gray)) ///
		xline(1886, lpattern(dot) lcolor(gray)) graphregion(color(white))
graph export "${dirresu}\Appendix\FigureA7.pdf", as(pdf) replace



* ==============================================================================
* FIGURE A8: 
* Marriage and political power.
* ------------------------------------------------------------------------------

* Import data
use "${dirdata}/final-data-sec4.dta", clear


* Reshape data to have each brother as a separate data entry
local i = 1
foreach z in b1 b2 b3 b4 b5 b6 b7 b8 b9 b10{
qui sum MP`z'
local y = r(max)
if `y'>0{
	forvalues j = 1/`y'{
	gen year0_`i' = term1`z'_`j'-myear
	local i = `i'+1
	}
}
}
keep refno cOut myear MPb year0_1 year0_2 year0_3 year0_4 year0_5 year0_6 year0_7 year0_8 year0_9 year0_10 year0_11 year0_12 year0_13 year0_14 year0_15 year0_16 year0_17 year0_18 year0_19 year0_20 year0_21 year0_22 year0_23 year0_24 
duplicates drop
reshape long year0_ , i(refno) j(j)


* Indicators for MP elections, by years after sister's marriage
gen MPi = j
drop j
drop if year0_ == .
gen MPcount = 1
gen MPout = 1 if cOut==1
gen MPin  = 1 if cOut==0
gen year0_five = .
forvalues i = -40(10)40{
replace year0_five = `i' if year0_>=`i' & year0_<`i'+10
}
collapse (sum) MPcount (sum) MPout (sum) MPin, by(cOut year0_five)
drop if year0_five==-40


* Figure
twoway 	(connect MPcount year0_five if cOut==0 & year0_five<=20, lcolor(blue) ms(smdiamond) mc(blue)) 	///
		(connect MPcount year0_five if cOut==1 & year0_five<=20, lcolor(red) ms(smdiamond) mc(red) lw(thick)), 	///
		xtitle("years after sister's marriage") ytitle("Number of brothers elected MP") ///
		legend(on order(1 "sister married in the peerage" 2 "sister married a commoner")) ///
		xlabels(-30 "-30 to -20" -20 "-20 to -10" -10 "-10 to 0" 0 "0 to 10" 10 "10 to 20" 20 "20 to 30", angle(45)) ///
		graphregion(color(white)) xline(-10, lpattern(dash) lcolor(gray))
graph export "${dirresu}\Appendix\FigureA8.pdf", as(pdf) replace



********************************************************************************
********************************************************************************
********************************************************************************
*																			   *
*	 							B. ROBUSTNESS								   *
*																			   *
********************************************************************************
********************************************************************************
********************************************************************************

	
	
* ==============================================================================
* TABLE B1: 
* Synthetic probability based on alternative benchmark cohorts

* TABLE B2: 
* Synthetic probability based on alternative benchmark cohorts, controlling for distance to London.
* ------------------------------------------------------------------------------


* Compute alternative treatment using different benchmark cohorts

clear
set obs 21
gen mage = _n+14
gen freq = 0
label var mage "Age at marriage"
label var freq "Frequency (=0 as starting point of calculations)"
save "${dirdta}\agessyntheic.dta", replace
use "${dirdata}\final-data.dta", clear

foreach i in 1810 1815 1820 1835 1840 1845{

preserve
local j = `i'+15
local d b
if `i'>1820{
local d m
}
keep if wom==1 & nthismarriage<2 & `d'year<=`j' & `d'year>=`i' & dage>30
replace mage = myear-byear if nthismarr==1 & mage==.
drop if myear>=1861 & myear!=.
replace mage = 999 if nthismarr==0
replace nthismarr = 1 if nthismarr==0
collapse (sum) freq = nthismarr, by(mage)
sort mage
drop if mage==. 
replace freq = 0 if freq==.
merge 1:1 mage using "${dirdta}\agessyntheic.dta", nogen
egen tot = sum(freq)
sort mage	
gen prob = 100*freq/tot
drop if mage == 999
keep mage prob
sort mage
gen syntheticT_alt = prob+prob[_n+1]+prob[_n+2]
drop prob
rename mage age1861
sort age1861
keep if age1861>=15 & age1861<=35
label var syntheticT_alt "Alternative treatment based on `d' cohort (`i'-`j')"
label var age1861 "Age at the start of the 1861 Season"
save "${dirdta}\syntheticprob`i'_`j'_alt.dta", replace
restore
}


* Regressions

global controls pr4 biorder hengpee

local ite = 1

do "${dirauxp}\cgmreg.do"		// bootstrap-t procedure (Cameron, Gelbach and Miller, 2008)


foreach i in 1815 1810 1820 1845 1840 1835{

local panel : word `ite' of `c(ALPHA)'
local j = `i'+15
use "${dirdata}\final-data.dta", clear
merge m:1 age1861 using "${dirdta}\syntheticprob`i'_`j'_alt.dta"
label var syntheticT_alt "Alternative treatment"
label var _merge "Merge results for alternative treatment"
keep if _merge==3
keep if wom==1 & nthismarriage<2 & age1861>=15 & age1861<=35
save "${dirdta}\dataforregs.dta", replace
local q = 1

foreach x in "" distlondon{

use "${dirdta}\dataforregs.dta", clear
keep if nthismarr==1 & myear!=.
drop if prankspo==0 // no foreign spouses (+ drops saxe-coburg)
qui probit cOut syntheticT_alt $controls `x', cluster(byear)
	qui estat classification
	local pcp1 = round(r(P_corr),1)
	qui margins, dydx(*) post
	outreg2 using "${dirresu}\Appendix\TableB`q'_`panel'", keep(syntheticT_alt) nocons label dec(3) coefastr ctitle("M common") nor2 tex replace
qui probit mheir syntheticT_alt $controls `x', cluster(byear)
	qui estat classification
	local pcp1 = round(r(P_corr),1)
	qui margins, dydx(*) post
	outreg2 using "${dirresu}\Appendix\TableB`q'_`panel'", keep(syntheticT_alt) nocons label dec(3) coefastr ctitle("M heir") nor2 tex
qui reg fmissmatch syntheticT_alt $controls `x', cluster(byear)
	outreg2 using "${dirresu}\Appendix\TableB`q'_`panel'", keep(syntheticT_alt) nocons label dec(3) coefastr ctitle("Diff abs") nor2 tex
qui reg fmissmatch2 syntheticT_alt $controls `x', cluster(byear)
	outreg2 using "${dirresu}\Appendix\TableB`q'_`panel'", keep(syntheticT_alt) nocons label dec(3) coefastr ctitle("Diff h-w") nor2 tex
qui probit fdown syntheticT_alt $controls `x', cluster(byear)
	qui estat classification
	local pcp1 = round(r(P_corr),1)
	qui margins, dydx(*) post
	outreg2 using "${dirresu}\Appendix\TableB`q'_`panel'", keep(syntheticT_alt) nocons label dec(3) coefastr ctitle("M down") nor2 tex
use "${dirdta}\dataforregs.dta", clear
keep if dage>35 & dyear>byear+1 & prank>1
qui probit celibacy syntheticT_alt $controls `x', cluster(byear)
	qui estat classification
	local pcp1 = round(r(P_corr),1)
	qui margins, dydx(*) post
	outreg2 using "${dirresu}\Appendix\TableB`q'_`panel'", keep(syntheticT_alt) nocons label dec(3) coefastr ctitle("never M") nor2 tex title("T based on women from `i' to `j'")

* small cluster

	/* NOTE 1: IF THE REPLICATOR WISHES TO SKIP THE CALCULATION OF SMALL

	CLUSTERS (WHICH TAKES ABOUT 6H), TYPE "0" IN THE FOLLOWING LOCAL: */
	
local smallcluster = 1

if `smallcluster' == 1{

	/* NOTE 2: Results stored in log file in the following locations:

	3 Replication package\results\Appendix\...

		... TableB1_A_smallcluster.log; ... TableB1_B_smallcluster.log; 

		... TableB1_C_smallcluster.log; ... TableB1_D_smallcluster.log. 

		... TableB1_E_smallcluster.log; ... TableB1_F_smallcluster.log.

		

		... TableB2_A_smallcluster.log; ... TableB2_B_smallcluster.log; 

		... TableB2_C_smallcluster.log; ... TableB2_D_smallcluster.log.  

		... TableB2_E_smallcluster.log; ... TableB2_F_smallcluster.log.  

	See also Table 2 for small cluster p-values for benchmark cohorts (panel A 

	in both tables B1 and B2)  */


use "${dirdta}\dataforregs.dta", clear
keep if nthismarr==1 & myear!=.
drop if prankspo==0 // no foreign spouses (+ drops saxe-coburg)
qui probit cOut syntheticT_alt $controls `x', cluster(byear)
local d born
if `i'>1820{
local d marr
}
keep if e(sample)==1

if `ite'>1{
    
capture log close				// save results in log file
cd "${dirresu}\Appendix"
log using TableB`q'_`panel'_smallcluster.log, replace

	display "=================================================================="
	display "Table B`q', panel `panel', col [1]: `d'. `i' to `j' (M common)"
	display "=================================================================="
	clustse probit cOut syntheticT_alt $controls `x', cluster(byear) method(pairs) reps(5000)
	display "=================================================================="
	display "Table B`q', panel `panel', col [2]:  `d'. `i' to `j' (M heir) "
	display "=================================================================="
	clustse probit mheir syntheticT_alt $controls `x', cluster(byear) method(pairs) reps(5000)
	display "=================================================================="
	display "Table B`q', panel `panel', col [3]:  `d'. `i' to `j' (Diff abs) "
	display "=================================================================="
	****
	qui reg fmissmatch syntheticT_alt $controls `x', cluster(byear)
	keep if e(sample)==1
	****
	cgmwildboot fmissmatch syntheticT_alt $controls `x', cluster(byear) bootcluster(byear) reps(5000)
	display "=================================================================="
	display "Table B`q', panel `panel', col [4]:  `d'. `i' to `j' (Diff h-w) "
	display "=================================================================="
	cgmwildboot fmissmatch2 syntheticT_alt $controls `x', cluster(byear) bootcluster(byear) reps(5000)
	display "=================================================================="
	display "Table B`q', panel `panel', col [5]:  `d'. `i' to `j' (M down)  "
	display "=================================================================="
	clustse probit fdown syntheticT_alt $controls `x', cluster(byear) method(pairs) reps(5000)
use "${dirdta}\dataforregs.dta", clear
keep if dage>35 & dyear>byear+1 & prank>1
qui probit celibacy syntheticT_alt $controls `x', cluster(byear)
keep if e(sample)==1
	display "=================================================================="
	display "Table B`q', panel `panel', col [6]:  `d'. `i' to `j' (never M)  "
	display "=================================================================="
	clustse probit celibacy syntheticT_alt $controls `x', cluster(byear) method(pairs) reps(5000)
log close
}
}
local q = `q'+1
}
local ite = `ite'+1
}

	/*	Table B1, Panel A and Table B2, Panel A small clusters' p-values are the 

	same as in Table 2 (both consider benchmark cohorts). Hence, the code is the

	same as before: */
	
if `smallcluster' == 1{
use "${dirdata}\final-data.dta", clear
gsort surname title -nthismarr 	// to get stable results
***do "${dirauxp}\cgmreg.do"		// if not run in setup
capture log close				// save results in log file
cd "${dirresu}\Appendix"
log using TableB1_A_smallcluster.log, replace
keep if wom==1 & nthismarriage<2 & age1861>=15 & age1861<=35	// sample
preserve
keep if nthismarr==1 & myear!=.	// married
qui probit cOut syntheticT $controls, cluster(byear)
keep if e(sample)==1	// keep sample of non-missing variables
clustse probit cOut syntheticT $controls, cluster(byear) method(pairs) reps(5000)
clustse probit mheir syntheticT $controls, cluster(byear) method(pairs) reps(5000)
cgmwildboot fmissmatch syntheticT $controls, cluster(byear) bootcluster(byear) reps(5000)
cgmwildboot fmissmatch2 syntheticT $controls, cluster(byear) bootcluster(byear) reps(5000)
clustse probit fdown syntheticT $controls, cluster(byear) method(pairs) reps(5000)
restore
keep if dage>35 & dyear>byear+1 & prank>1 // married and unmarried women
qui probit celibacy syntheticT $controls, cluster(byear)
keep if e(sample)==1	// keep sample of non-missing variables
clustse probit celibacy syntheticT $controls, cluster(byear) method(pairs) reps(5000)
log close
*
use "${dirdata}\final-data.dta", clear
gsort surname title -nthismarr 	// to get stable results
capture log close				// save results in log file
cd "${dirresu}\Appendix"
log using TableB2_A_smallcluster.log, replace
keep if wom==1 & nthismarriage<2 & age1861>=15 & age1861<=35	// sample
preserve
keep if nthismarr==1 & myear!=.	// married
qui probit cOut syntheticT $controls distlondon, cluster(byear)
keep if e(sample)==1	// keep sample of non-missing variables
clustse probit cOut syntheticT $controls distlondon, cluster(byear) method(pairs) reps(5000)
clustse probit mheir syntheticT $controls distlondon, cluster(byear) method(pairs) reps(5000)
cgmwildboot fmissmatch syntheticT $controls distlondon, cluster(byear) bootcluster(byear) reps(5000)
cgmwildboot fmissmatch2 syntheticT $controls distlondon, cluster(byear) bootcluster(byear) reps(5000)
clustse probit fdown syntheticT $controls distlondon, cluster(byear) method(pairs) reps(5000)
restore
keep if dage>35 & dyear>byear+1 & prank>1 // married and unmarried women
qui probit celibacy syntheticT $controls distlondon, cluster(byear)
keep if e(sample)==1	// keep sample of non-missing variables
clustse probit celibacy syntheticT $controls distlondon, cluster(byear) method(pairs) reps(5000)
log close
}


* Erase temporary dta files
erase "${dirdta}\agessyntheic.dta"
erase "${dirdta}\dataforregs.dta"
erase "${dirdta}\syntheticprob1810_1825_alt.dta"
erase "${dirdta}\syntheticprob1815_1830_alt.dta"
erase "${dirdta}\syntheticprob1820_1835_alt.dta"
erase "${dirdta}\syntheticprob1835_1850_alt.dta"
erase "${dirdta}\syntheticprob1840_1855_alt.dta"
erase "${dirdta}\syntheticprob1845_1860_alt.dta"


	
	
* ==============================================================================
* TABLE B3: 
* Contingency tables, treatment based on marriage cohorts
* ------------------------------------------------------------------------------


* Import final data
use "${dirdata}\final-data.dta", clear

* Sample
keep if wom==1 & prank>1 & myear>=1858 & myear<=1863 & nthismarr==1 & mage<40

	/* Note: The sample is all peers' daughters marrying in 1858-63, excluding 

	second marriages and women married above 40.  */

* Contingency tables
label var grank "Wife"
capture log close				// save results in log file
cd "${dirresu}\Appendix"
log using TableB3.log, replace

*  Panel A: Control group (Season): peers' daughters marrying in 1858-60
tab grank grankspo if myear>=1858 & myear<=1860 & crankspo!=0 & nthismarr==1, expect

*  Panel B: Treatment group (Season): peers' daughters marrying in 1861-63
tab grank grankspo if myear>=1861 & myear<=1863 & crankspo!=0 & nthismarr==1, expect

log close

	
	
* ==============================================================================
* TABLE B4: 
* Sorting by title, non-parametric tests based on marriage cohorts.
* ------------------------------------------------------------------------------


* Import final data
use "${dirdata}\final-data.dta", clear

* Sample
keep if wom==1 & prank>1 & myear>=1858 & myear<=1863 & nthismarr==1 & mage<40

	/* Note: The sample is all peers' daughters marrying in 1858-63, excluding 

	second marriages and women married above 40.  */

* Add results to matrix tab5
mat define tabB4 = (.,.,.\.,.,.\.,.,.\.,.,.\.,.,.\.,.,.\.,.,.)

* Control group, m. in 1858-60
qui tab prank grankspo if myear>=1858 & myear<=1860, expect
	qui bootstrap r(taub), reps(1000): tab prank grankspo if mourn==0 & prank>1 & crankspo!=0, taub
	matrix taub0 = r(table)
	local taub = taub0[1,1]
	local taub_p = round(taub0[4,1],0.0001)
	qui tab prank grankspo if mourn==0 & prank>1 & crankspo!=0, chi2 lrchi2 taub nofreq

mat tabB4[1,1] = `r(chi2)'
mat tabB4[2,1] = `r(p)'
mat tabB4[3,1] = `r(chi2_lr)'
mat tabB4[4,1] = `r(p_lr)'
mat tabB4[5,1] = `taub'
mat tabB4[6,1] = `taub_p'
mat tabB4[7,1] = `r(N)'

* Treatment group, m. in 1861-63
qui tab prank grankspo if myear>=1861 & myear<=1863, expect
	qui bootstrap r(taub), reps(1000): tab prank grankspo if mourn==1 & prank>1 & crankspo!=0, taub
	matrix taub0 = r(table)
	local taub = taub0[1,1]
	local taub_p = round(taub0[4,1],0.0001)
	qui tab prank grankspo if mourn==1 & prank>1 & crankspo!=0, chi2 lrchi2 taub nofreq

mat tabB4[1,2] = `r(chi2)'
mat tabB4[2,2] = `r(p)'
mat tabB4[3,2] = `r(chi2_lr)'
mat tabB4[4,2] = `r(p_lr)'
mat tabB4[5,2] = `taub'
mat tabB4[6,2] = `taub_p'
mat tabB4[7,2] = `r(N)'

* Difference

mat tabB4[1,3] = tabB4[1,1]-tabB4[1,2]
mat tabB4[3,3] = tabB4[3,1]-tabB4[3,2]
mat tabB4[5,3] = tabB4[5,1]-tabB4[5,2]
mat tabB4[7,3] = tabB4[7,1]+tabB4[7,2]

	forvalues i=0/1{
	qui tab prank grankspo if mourn==`i' & prank>1 & crankspo!=0, all
	local conv_chi`i' = sqrt(r(chi2)/e(N))
	local conv_lrc`i' = sqrt(r(chi2_lr)/e(N))
	}
	forvalues i=0/1{
	qui bootstrap r(taub), reps(500000): tab prank grankspo if mourn==`i' & prank>1 & crankspo!=0, taub
	mat b`i' = e(b)
	local taub`i' = b`i'[1,1]
	local conv`i' = sin(3.141592654*`taub`i''*0.5)
	}
	* difference: pearson chi2
	cortesti `conv_chi0' 95 `conv_chi1' 87
	mat tabB4[2,3] = 0.0577
	* difference: likelihood chi2
	cortesti `conv_lrc0' 95 `conv_lrc1' 87
	mat tabB4[4,3] = 0.0459
	* difference: taub
	cortesti `conv0' 95 `conv1' 87
	mat tabB4[6,3] = 0.0017


* Table B4
mat colnames tabB4 = "Control" "Treat" "Diff"
mat rownames tabB4 = "Pearson chi-squared" "  p-val" "Likelihood ratio" "  p-val" "Kendall rank correlation" "  p-val" "N"
mat list tabB4
esttab matrix(tabB4, fmt(3 3)) using "${dirresu}\Appendix\TableB4.tex", mlabel("Table B4") replace


	
* ==============================================================================
* FIGURE B1: 
* Sorting by landholdings, K-S tests based on marriage cohorts
* ------------------------------------------------------------------------------


* Import final data
use "${dirdata}\final-data.dta", clear

* Sample
keep if myear>=1858 & myear<=1866 & prank>1 & mage<40 & fatotal!=. & sfatotal!=.

	/* Note: The sample is all peers' daughters marrying in 1858-63, excluding 

	second marriages and women married above 40.  */

* Re-calculate percentiles in new sample
drop sfATot fATot
xtile sfATot = sfatotal, nq(100)
xtile fATot  = fatotal, nq(100)
gen fmissmatchA  = abs(fATot-sfATot)
gen fmissmatchA2 = sfATot-fATot

* Treatment and control groups
gen prepost = 2 	if myear>=1861 & myear<=1863
replace prepost = 1 if myear<1861
keep if prepost!=.

* PANEL A (ABSOLUTE VALUE)
preserve
* cumulative plot
sort fmissmatchA prepost
qui ksmirnov fmissmatchA, by(prepost) exact
local KS1`x' = round(`r(D)',0.1)
local pval1`x' = round(`r(p_exact)',0.01)
cumul fmissmatchA if prepost==1, gen(cum1)
cumul fmissmatchA if prepost==2, gen(cum2)
twoway 	(line cum1 fmissmatchA, c(J) lpattern(solid) lcolor(blue) lwidth(thin)) ///
		(line cum2 fmissmatchA, c(J) lpattern(solid) lcolor(red) lwidth(thick)), ///
		xtitle("|wife's percentile rank of acres - husband's percentile rank of acres|", size(med))	///
		ytitle("cumulative", size(med))	///
		legend(on order(1 "Control (Season)" 2 "Treatment (no Season)") rows(2) ring(0) position(5) bmargin(large) region(lstyle(none))) ///
		graphregion(color(white)) ///
		note("KS = `KS1' (`pval1')") name(A, replace)
restore

* PANEL B (DIFFERENCE)
preserve
* cumulative plot
sort fmissmatchA2 prepost
qui ksmirnov fmissmatchA2, by(prepost) exact
local KS2`x' = round(`r(D)',0.01)
local pval2`x' = round(`r(p_exact)',0.01)
cumul fmissmatchA2 if prepost==1, gen(cum1)
cumul fmissmatchA2 if prepost==2, gen(cum2)
twoway 	(line cum1 fmissmatchA2, c(J) lpattern(solid) lcolor(blue) lwidth(thin)) ///
		(line cum2 fmissmatchA2, c(J) lpattern(solid) lcolor(red) lwidth(thick)) ///
		(pcarrowi 0.95 -40 0.95 -60 "Marrying down", color(black)), ///
		xtitle("|wife's percentile rank of acres - husband's percentile rank of acres|", size(med))	///
		xline(0, lcolor(black) lwidth(thick)) ytitle("cumulative", size(med))	///
		legend(on order(1 "Control (Season)" 2 "Treat (no Season)") rows(2) ring(0) position(5) bmargin(large) region(lstyle(none))) ///
		graphregion(color(white)) ///
		note("KS = `KS2' (`pval2')") name(B, replace)
restore

* COMBINE
graph combine A B, graphregion(color(white)) rows(2) altshrink
graph export "${dirresu}\Appendix\FigureB1.pdf", as(pdf) replace
graph drop _all


	
* ==============================================================================
* TABLE B5: 
* Contingency tables separating wives' titles
* ------------------------------------------------------------------------------


* Import data and keep baseline sample
use "${dirdata}\final-data.dta", clear
keep if base_sample==1


* Treatment indicator for non-parametric analysis
_pctile syntheticT, nq(100)
gen treatment = (syntheticT > r(r80))

	/* Note: For the non-parametric estimation, I compare a high- vs. a low-treatment

	cohort. The high-treatment cohort are women with a synthetic probability to

	marry during the interruption above 20 percent. This corresponds to the top

	quintile; the 20% of women with the highest synthetic probability to marry in

	1861-63. Conversely, the low-treatment cohort are women below the top quintile. */


* Contingency tables
label var ownrank "Wife"
capture log close				// save results in log file
cd "${dirresu}\Appendix"
log using TableB5.log, replace

*  Panel A: Low-Treatment cohorts (T < 80th percentile)
tab ownrank grankspo if treatment==0, expected matcell(ofreqa)

*  Panel B: High-Treatment cohorts (T >= 80th percentile)
tab ownrank grankspo if treatment==1, expected matcell(ofreqa)
log close


	
* ==============================================================================
* TABLE B6: 
* Non-parametric tests for contingency Table B.V
* ------------------------------------------------------------------------------


* Import data and keep baseline sample
use "${dirdata}\final-data.dta", clear
keep if base_sample==1


* Treatment indicator for non-parametric analysis
_pctile syntheticT, nq(100)
gen treatment = (syntheticT > r(r80))

* Non-parametric stats
forvalues i=0/1{
tab ownrank grankspo if treatment==`i', chi2 lrchi2
local chi2_`i' = round(`r(chi2)',0.01)
local chi2LR_`i' = round(`r(chi2_lr)',0.01)
local chi2p_`i' = r(p)
local chi2LRp_`i' = r(p_lr)
local df_`i' = e(df_r)
local N`i' = r(N)
ktau ownrank grankspo if treatment==`i', stats(taub)
local taub_`i' = round(`r(tau_b)',0.01)
local taubp_`i' = r(p)
}


* Add results to matrix tab5
mat define tabB6 = (.,.,.\.,.,.\.,.,.\.,.,.\.,.,.\.,.,.\.,.,.)
forvalues j =0/1{
local col = `j'+1
mat tabB6[1,`col'] = `chi2_`j''
mat tabB6[2,`col'] = `chi2p_`j''
mat tabB6[3,`col'] = `chi2LR_`j''
mat tabB6[4,`col'] = `chi2LRp_`j''
mat tabB6[5,`col'] = `taub_`j''
mat tabB6[6,`col'] = `taubp_`j''
mat tabB6[7,`col'] = `N`j''
}
mat tabB6[5,3] = `taub_0'-`taub_1'
mat tabB6[7,3] = `N0'+`N1'


* Difference chi-squared and LR-chi2
local chi2_diff = `chi2_0'-`chi2_1'
local chi2_diffp = chi2tail(3,`chi2_diff')
local chi2LR_diff = `chi2LR_0'-`chi2LR_1'
local chi2LR_diffp = chi2tail(3,`chi2LR_diff')
mat tabB6[1,3] = `chi2_diff'
mat tabB6[2,3] = `chi2_diffp'
mat tabB6[3,3] = `chi2LR_diff'
mat tabB6[4,3] = `chi2LR_diffp'


* Difference tau-b
qui ktau ownrank grankspo if treatment==0
local convtaub_0 = sin(3.141592654*`r(tau_b)'*0.5)
qui ktau ownrank grankspo if treatment==1
local convtaub_1 = sin(3.141592654*`r(tau_b)'*0.5)
cortesti `convtaub_0' `N0' `convtaub_1' `N1'
mat tabB6[6,3] = 0.1047


* Table B6
mat colnames tabB6 = "Low-treat" "High-treat" "Diff"
mat rownames tabB6 = "Pearson chi-squared" "  p-val" "Likelihood ratio" "  p-val" "Kendall rank correlation" "  p-val" "N"
mat list tabB6
esttab matrix(tabB6, fmt(3 3)) using "${dirresu}\Appendix\TableB6.tex", mlabel("Table B6") replace


	
* ==============================================================================
* TABLE B7: 
* Alternative contingency tables
* ------------------------------------------------------------------------------


* DATA PREPARATION
* ----------------
* Append (i) men and women who did not marry and (ii) men who failed to marry 
* peers' daughters for the non-parametric analysis of appendix BIV.


* Sample 1: All women (married and unmarried)

* Import data:
use "${dirdata}\final-data.dta", clear
_pctile syntheticT, nq(100)		
gen treatment = (syntheticT > r(r80)) 	// treatment variable for non-parametric analysis
* Sample:
keep if wom==1 & nthismarriage<2 & age1861>=15 & age1861<=35
drop if nthismarr==0 & dage<35
drop if prankspo==0
* Relabel marriage outcomes (to incorporate unmarried + spouse's rank at age 15):
gen rank_hus = 0 if nthismarr==0 | myear==.
replace rank_hus = grankspo if rank_hus==.
label define marroutcome_label 0 "Unmarried" 1 "Commoner" 2 "Gentry" 3 "Peer son" 4 "Peer heir"
label values rank_hus marroutcome_label
keep grank rank_hus treatment
rename grank rank_wif
gen married = (rank_hus==0)
gen sample = 1
* Save:
label var rank_hus "Unmarried / Husband's rank at age 15"
label var treatment "Treatment: =0 if low (T<80 pctile); =1 if high (T>80 pctile)"
label var married "Ever married (=0 no; =1 yes)"
label var sample "Sample (=1 women; =2 men)"
save "${dirdta}\sampleCT_allwomen.dta", replace


* Sample 2: All married men (incl. those who did not marry peers' daughters')

	/*  Note: For all women in the baseline sample, I searched the entry (refno) 

	their corresponding husbands in the hollingsworth dataset. I added husbands 

	whose wives wer not peers' daughters but who were also aged 15 to 35 in 1861.

	The information is stored in the data file:

	"${dirroot}\programs\03_appendix\aux data appendix\husbands_contingency_table.txt"	

	*/

* Import data and append to baseline sample of women
preserve
import delimited "${dirroot}\programs\03_appendix\aux data appendix\husbands_contingency_table.txt", clear
label var refno_hus "Reference number in Hollingsworth (Husbands)"
label var grank "Wife's parental rank"
	label define prankspo2_lbl2 1 "Commoner dau." 2 "Gentry dau." 4 "Baron dau." 5 "Duke dau.", replace
	label values grank prankspo2_lbl2
label var agewife1861 "Wife's age at the start of the 1861 Season"
label var grankspo "Peer son / Peer heir"
	label define grankspohusb_lbl 3 "Peer son" 4 "Peer heir"
	label values grankspo grankspohusb_lbl
label var prank_husb "Husband's parental rank"
label var rank_husb "Husband's highest title succeeded to/was heir to at 15/was created for"
	label define rnk 0 "Foreign" 1 "Commoner" 2 "Knight" 3 "Baronet" 4 "Baron" 5 "Duke"
	label values prank_husb rank_husb rnk
label var crank_husb "Husband's highest rank heir to at 15"
	label define crnk 0 "Foreign" 1 "Pure commoner" 2 "Baron younger son" 3 "Duke younger son" 4 "Knight" 5 "Baronet" 6 "Baron" 7 "Duke" // labels
	label values crank_husb crnk
label var hrank_husb "Husband's highest rank heir to at 15 (alt. categories)"
	label define hnk 0 "Foreign" 1 "Commoner at 15" 2 "Knight" 3 "Baronet" 4 "Baron" 5 "Duke"
	label values hrank_husb hnk
save "${dirdta}\husbands_contingency_table.dta", replace
restore
use "${dirdata}\final-data.dta", clear
keep if base_sample==1
gen agewife1861 = age1861
append using "${dirdta}\husbands_contingency_table.dta"
* Re-label and treatment for men
label define prankspo2_lbl2 1 "Commoner dau." 2 "Gentry dau." 4 "Baron dau." 5 "Duke dau.", replace
label values grank prankspo2_lbl2
gen treatment_m = 1 if agewife1861>=19 & agewife1861<=22
replace treatment_m = 0 if agewife1861<19 | agewife1861>22
keep if grankspo>=3
rename grank marroutcome
gen agemen1861 = floor((td(31mar1861)-sbdate)/365)
replace agemen1861 = 1861-sbyear if agemen1861==.
* Save and erase temp files
label var agewife1861 "Wife's age at the start of the 1861 Season"
label var treatment_m "Treatment for men (=0 low; =1 high)"
label var agemen1861 "Men's age at the start of the 1861 Season"
save "${dirdta}\sample_CT_marriedmen.dta", replace
erase "${dirdta}\husbands_contingency_table.dta"

* Sample 3: Unmarried men

* Import data and select unmarried men aged 15-35 in 1861:
use "${dirdata}\final-data.dta", clear
keep if wom==0 & nthismarr==0
gen agemen1861 = floor((td(31mar1861)-bdate)/365)
replace agemen1861 = 1861-byear if agemen1861==.
keep if agemen1861>=15 & agemen1861<=35
replace dage = dyear-byear if dage==.
keep if dage>=35 // for consistency with women's sample
* Re-label marriage outcomes:
gen marroutcome = 0
drop grankspo
gen grankspo = 3 if crank==2 | crank==3 	// husband
replace grankspo = 4 if crank==6 | crank==7	
drop if prank==1
* Save:
label var agemen1861 "Men's age at the start of the 1861 Season"
label var marroutcome "Marriage outcome (0=unmarried)"
label var grankspo "Peer son / Peer heir"
	label define grankspohusb_lbl 3 "Peer son" 4 "Peer heir"
	label values grankspo grankspohusb_lbl
save "${dirdta}\sample_CT_unmarriedmen.dta", replace


* Append all samples

* Married and unmarried men
use "${dirdta}\sample_CT_marriedmen.dta", clear
append using "${dirdta}\sample_CT_unmarriedmen.dta"
* Re-label and treatment for men
label define marrout_men_label 0 "Unmarried" 1 "Commoner dau." 2 "Gentry dau." 4 "Baron dau." 5 "Duke dau.", replace
label values marroutcome marrout_men_label
label var marroutcome "Marriage outcome"
replace treatment_m = 1 if agemen1861>=19 & agemen1861<=22 & nthismarr==0
replace treatment_m = 0 if (agemen1861<19 & nthismarr==0) | (agemen1861>22 & nthismarr==0)
keep grankspo marroutcome treatment_m
rename grankspo rank_hus
rename marroutcome rank_wif
rename treatment_m treatment
gen married = (rank_wif==0)
gen sample = 2
drop if rank_wif==4 | rank_wif==5
* Append women, married and unmarried men
append using "${dirdta}\sampleCT_allwomen.dta"
replace rank_wif = rank_wif-1 if rank_wif>=4
label define wom_lbl 0 "No wife" 1 "Commoner dau." 2 "Gentry dau." 3 "Baron dau." 4 "Duke dau."
label values rank_wif wom_lbl
label define men_lbl 0 "No husb." 1 "Commoner" 2 "Gentry" 3 "Peer son" 4 "Peer heir"
label values rank_hus men_lbl
* Save and erase temp files
label var treatment "Treatment (0=low; 1=high)"
label var married "Ever married (0= no; 1= yes)"
label var sample "Sample (=1 women; =2 men)"
save "${dirdta}\sampleCT_allwomen-men.dta", replace
erase "${dirdta}\sample_CT_marriedmen.dta"
erase "${dirdta}\sample_CT_unmarriedmen.dta"
erase "${dirdta}\sampleCT_allwomen.dta"


* CONTINGENCY TABLES
* ------------------

* Import data
use "${dirdta}\sampleCT_allwomen-men.dta", clear


* Contingency table
forvalues T=0/1{
tab rank_wif rank_hus if treatment==`T', matcell(ofreq)

* Re-arrange cells for beautification
local ite = `T'+1 
local panel : word `ite' of `c(ALPHA)'
mat define tabB7_`panel' = (.,.,.,.,.,.\.,.,.,.,.,.\.,.,.,.,.,.\.,.,.,.,.,.\.,.,.,.,.,.\.,.,.,.,.,.)
mat list tabB7_`panel'
* Re-arrange marriages
forvalues i=1/4{
local i2 = `i'+1
mat tabB7_`panel'[`i',1] = ofreq[`i2',2]
mat tabB7_`panel'[`i',2] = ofreq[`i2',3]
mat tabB7_`panel'[`i',3] = ofreq[`i2',4]
mat tabB7_`panel'[`i',4] = ofreq[`i2',5]
}
* Re-arrange N/A values
mat tabB7_`panel'[1,1] = .
mat tabB7_`panel'[2,1] = .
mat tabB7_`panel'[1,2] = .
mat tabB7_`panel'[2,2] = .
* Re-arrange totals
mat tabB7_`panel'[5,1] = tabB7_`panel'[3,1]+tabB7_`panel'[4,1]
mat tabB7_`panel'[5,2] = tabB7_`panel'[3,2]+tabB7_`panel'[4,2]
mat tabB7_`panel'[5,3] = tabB7_`panel'[1,3]+tabB7_`panel'[2,3]+tabB7_`panel'[3,3]+tabB7_`panel'[4,3]
mat tabB7_`panel'[5,4] = tabB7_`panel'[1,4]+tabB7_`panel'[2,4]+tabB7_`panel'[3,4]+tabB7_`panel'[4,4]
mat tabB7_`panel'[1,5] = tabB7_`panel'[1,3]+tabB7_`panel'[1,4]
mat tabB7_`panel'[2,5] = tabB7_`panel'[2,3]+tabB7_`panel'[2,4]
mat tabB7_`panel'[3,5] = tabB7_`panel'[3,1]+tabB7_`panel'[3,2]+tabB7_`panel'[3,3]+tabB7_`panel'[3,4]
mat tabB7_`panel'[4,5] = tabB7_`panel'[4,1]+tabB7_`panel'[4,2]+tabB7_`panel'[4,3]+tabB7_`panel'[4,4]
mat tabB7_`panel'[5,5] = tabB7_`panel'[5,1]+tabB7_`panel'[5,2]+tabB7_`panel'[5,3]+tabB7_`panel'[5,4]
* Re-arrange totals including unmarried
mat tabB7_`panel'[6,1] = tabB7_`panel'[5,1]+ofreq[1,2]
mat tabB7_`panel'[6,2] = tabB7_`panel'[5,2]+ofreq[1,3]
mat tabB7_`panel'[6,3] = tabB7_`panel'[5,3]+ofreq[1,4]
mat tabB7_`panel'[6,4] = tabB7_`panel'[5,4]+ofreq[1,5]
mat tabB7_`panel'[6,5] = tabB7_`panel'[5,5]+ofreq[1,6]
mat tabB7_`panel'[1,6] = tabB7_`panel'[1,5]+ofreq[2,1]
mat tabB7_`panel'[2,6] = tabB7_`panel'[2,5]+ofreq[3,1]
mat tabB7_`panel'[3,6] = tabB7_`panel'[3,5]+ofreq[4,1]
mat tabB7_`panel'[4,6] = tabB7_`panel'[4,5]+ofreq[5,1]
mat tabB7_`panel'[5,6] = tabB7_`panel'[5,5]+ofreq[6,1]
mat tabB7_`panel'[6,6] = `r(N)'
* Row and column labels
mat colnames tabB7_`panel' = "Commoner" "Gentry" "Peer son" "Peer heir" "Total" "Total + unmarr"
mat rownames tabB7_`panel' = "Commoner dau" "Gentry dau" "Baron/Visc dau" "Duke/Earl/Mar dau" "Total" "Total + unmarr"
}


* Table B6
esttab matrix(tabB7_A, fmt(0)) using "${dirresu}\Appendix\TableB7_A.tex", mlabel("Table B7 (Panel A)") replace
esttab matrix(tabB7_B, fmt(0)) using "${dirresu}\Appendix\TableB7_B.tex", mlabel("Table B7 (Panel B)") replace


	
* ==============================================================================
* TABLE B8: 
* Non-parametric tests for contingency Table B7
* ------------------------------------------------------------------------------

* Import data
use "${dirdta}\sampleCT_allwomen-men.dta", clear


* Matrix to export results
mat define tabB8_ = (24.6,0.000,3.50,0.320,21.1,0.000\24.9,0.000,3.60,0.315,21.3,0.000\0.20,0.000,0.11,0.191,0.09,0.164\.,.,.,.,.,.\.,.,.,.,.,.\0.20,0.000,0.11,0.191,0.09,0.164\.,.,.,.,.,.\.,.,.,.,.,.\0.20,0.000,0.11,0.191,0.09,0.164\.,.,.,.,.,.\.,.,.,.,.,.\.,.,.,.,.,.\.,.,.,.,.,.\.,.,.,.,.,.\.,.,.,.,.,.)

	/* Note: Panel A's results equivalent to those reported in Table 5 in paper 

	and reproduced in the master-analysis.do do-file. Results on Kendall's tau-b 

	for Panels B and C also equivalent to those reported in Table 5 in paper 

	and reproduced in the master-analysis.do do-file. The reason is that: 

	``Kendall's rank correlation is based on the number of concordant, discordant, 

	and tied marriages [...] Hence, estimates are not affected whether unmarried 

	individuals are included or not'' (see appendix B4 for details). */


* Set globals
global rank_wif rank_wif
global rank_hus rank_hus


* Panel B
* ------------------------------------------------------------------------------
global nacells  1 	// (=0 for 4x4 tables; =1 if n/a cells)
global sampleM	0	// (=0 baseline; =1 baseline+peer son)
global sampleRM 1	// (=0 baseline; =1 baseline+peer son; =2 baseline+peer son+unmarried)

if $nacells == 0{
global wom 1
global sampleM 999
global sampleRM 999
}

if $sampleM == 0 & $sampleRM == 0{
keep if $rank_wif>2 & $rank_hus>0
}
if $sampleM == 0{
global wom 3
}
if $sampleM == 1{
global wom 1
}

forvalues x = 1(-1)0{
* observed cell counts 
forvalues i = 1/4{
forvalues j = 1/4{
count if treatment==`x' & $rank_wif==`i' & $rank_hus==`j'
local O`i'`j' = r(N)
}
}
* expected cell counts 
forvalues z = 1/4{
local c`z' = `O1`z''+`O2`z''+`O3`z''+`O4`z''
local r`z' = `O`z'1'+`O`z'2'+`O`z'3'+`O`z'4'
}
if $sampleRM == 2{							// to include unmarried
	if `x'==0{				
	local c3 = `c3' + 75
	local c4 = `c4' + 17
	local r3 = `r3' + 82
	local r4 = `r4' + 59
	}
	if `x'==1{
	local c3 = `c3' + 25
	local c4 = `c4' + 3
	local r3 = `r3' + 28
	local r4 = `r4' + 10
	}
}
local Nmen = `c1'+`c2'+`c3'+`c4'
forvalues i = 1/4{
forvalues j = 1/4{
local E`i'`j' = `r`i''*`c`j''/`Nmen'
}
}
if $nacells == 1{
	local E11 = 0
	local E12 = 0
	local E21 = 0
	local E22 = 0
}
* concordant and discordant pairs
forvalues j = 1/3{
local y = `j'+1
forvalues i = 1/3{
local C`i'`j' = 0
local z = `i'+1
forvalues i0=`z'/4{
forvalues j0=`y'/4{
local C`i'`j' = `C`i'`j''+`O`i'`j''*`O`i0'`j0''
}
}
}
}
forvalues j = 4(-1)2{
local y = `j'-1
forvalues i = 1/3{
local D`i'`j' = 0
local z = `i'+1
forvalues i0=`z'/4{
forvalues j0=1/`y'{
local D`i'`j' = `D`i'`j''+`O`i'`j''*`O`i0'`j0''
}    
}
}
}
local C = 0
local D = 0
forvalues i=3/3{	// rows 3 (and 4, where C D YY are zeros) because sample restricted to women
	local C  = `C' + `C`i'1'+`C`i'2'+`C`i'3' 
	local D  = `D' + `D`i'2'+`D`i'3'+`D`i'4' 
}
if $sampleM == 1{	// to add all peer's sons cells
	local C  = `C' +`O13'*(`O24'+`O34'+`O44')+`O23'*(`O34'+`O44')
	local D  = `D' +`O14'*(`O23'+`O33'+`O43')+`O24'*(`O33'+`O43') 
}
if $nacells == 0{	// redo for the 4x4 table
	local C = 0
	local D = 0
	forvalues i=1/3{
	local C  = `C' + `C`i'1'+`C`i'2'+`C`i'3' 
	local D  = `D' + `D`i'2'+`D`i'3'+`D`i'4' 
}
}
* ties
local T = 0
local S = 0
if $nacells == 0 | $sampleM == 1{
	forvalues i=1/4{
	local j = `i'
	local t`i' = `O`i'1'+`O`i'2'+`O`i'3'+`O`i'4'
	local T = `T' + `t`i''*(`t`i''-1)/2
	local s`j' = `O1`j''+`O2`j''+`O3`j''+`O4`j''
	local S = `S' + `s`j''*(`s`j''-1)/2
	}
}
if $sampleM == 0{
	forvalues i=3/4{
	local t`i' = `O`i'1'+`O`i'2'+`O`i'3'+`O`i'4'
	local T = `T' + `t`i''*(`t`i''-1)/2
	}
	forvalues j=1/4{
	local s`j' = `O3`j''+`O4`j''
	local S = `S' + `s`j''*(`s`j''-1)/2
	}
}
* total number of pairs
if $nacells == 0{
	qui tab $rank_wif $rank_hus if treatment==`x' & rank_wif>0 & rank_hus>0 // for 4x4 tables
	local n`x'= r(N)
	local Np_`x' = `n`x''*(`n`x''-1)/2
}
if $sampleM == 0{
	qui tab $rank_wif $rank_hus if treatment==`x' & rank_wif>2 & rank_hus>0 // analysis restricted to women
	local n`x'= r(N)
	local Np_`x' = `n`x''*(`n`x''-1)/2
}
if $sampleM == 1{
	qui tab $rank_wif $rank_hus if treatment==`x' & rank_wif>0 & rank_hus>0 // remove artificial pairs due to n/a
	local n`x'= r(N)
	local Np_`x' = `n`x''*(`n`x''-1)/2 - (`D12'+`D13'+`D14'+`D22'+`D23'+`D24'+`D32'+`D33'+`D34'-`D')
}
* Chi-2 statistics
local chi2_`x' = 0
local chi2LR_`x' = 0
forvalues i=$wom / 4{ 	// rows 3 and 4 because analysis restricted to women
forvalues j=1/4{
if `E`i'`j''!=0{
local chi2_`x' = `chi2_`x'' + ((`O`i'`j''-`E`i'`j'')^2)/`E`i'`j''
local chi2LR_`x' = `chi2LR_`x'' + 2*`O`i'`j''*log(0.000000000001+`O`i'`j''/`E`i'`j'')
}
}
}
* Chi-2 p-values
local pvchi2_`x' = chi2tail(3,`chi2_`x'')
local pvchi2LR_`x' = chi2tail(3,`chi2LR_`x'')

* Kendall's tau-b
local taub_`x' = (`C'-`D')/(sqrt((`Np_`x''-`T')*(`Np_`x''-`S')))
* Z-score for tau-b significance
foreach xx in vt vs sum1v1 sum2v1 sum1v2 sum2v2{
local `xx' = 0
}
local v0 = 2*`Np_`x''*(2*`n`x''+5)
if $nacells == 0 | $sampleM == 1{
local row2 = 1
}
if $sampleM == 0{
local row2 = 3
}
forvalues i=`row2'/4{
local vt = `vt'+`t`i''*(`t`i''-1)*(2*`t`i''+5)
local sum1v1 = `sum1v1'+`t`i''*(`t`i''-1)
local sum1v2 = `sum1v2'+`t`i''*(`t`i''-1)*(`t`i''-2)
}
forvalues j=1/4{
local vs = `vs'+`s`j''*(`s`j''-1)*(2*`s`j''+5)
local sum2v1 = `sum2v1'+`s`j''*(`s`j''-1)
local sum2v2 = `sum1v2'+`s`j''*(`s`j''-1)*(`s`j''-2)
}
local v1 = `sum1v1'*`sum2v1'/(2*2*`Np_`x'')
local v2 = `sum1v2'*`sum2v2'/(9*2*`Np_`x''*(`n`x''-2))
local v = (`v0'-`vt'-`vs')/18 + `v1' + `v2'
local z_`x' = (`C'-`D')/(sqrt(`v'))
local pv_`x'= 2*(normal(-`z_`x''))
}
* Chi2 difference tests
local chi2_diff = `chi2_0'-`chi2_1'
local pvchi2_diff = chi2tail(3,`chi2_diff')
local chi2LR_diff = `chi2LR_0'-`chi2LR_1'
local pvchi2LR_diff = chi2tail(3,`chi2LR_diff')

* Store results
mat tabB8_[4,1] = `chi2_0'
mat tabB8_[4,2] = `pvchi2_0'
mat tabB8_[4,3] = `chi2_1'
mat tabB8_[4,4] = `pvchi2_1'
mat tabB8_[4,5] = `chi2_0'-`chi2_1'
mat tabB8_[4,6] = `pvchi2_diff'
mat tabB8_[5,1] = `chi2LR_0'
mat tabB8_[5,2] = `pvchi2LR_0'
mat tabB8_[5,3] = `chi2LR_1'
mat tabB8_[5,4] = `pvchi2LR_1'
mat tabB8_[5,5] = `chi2LR_0'-`chi2LR_1'
mat tabB8_[5,6] = `pvchi2LR_diff'


* Panel C
* ------------------------------------------------------------------------------
global nacells  1 	// (=0 for 4x4 tables; =1 if n/a cells)
global sampleM	0	// (=0 baseline; =1 baseline+peer son)
global sampleRM 2	// (=0 baseline; =1 baseline+peer son; =2 baseline+peer son+unmarried)

if $nacells == 0{
global wom 1
global sampleM 999
global sampleRM 999
}

if $sampleM == 0 & $sampleRM == 0{
keep if $rank_wif>2 & $rank_hus>0
}
if $sampleM == 0{
global wom 3
}
if $sampleM == 1{
global wom 1
}

forvalues x = 1(-1)0{
* observed cell counts 
forvalues i = 1/4{
forvalues j = 1/4{
count if treatment==`x' & $rank_wif==`i' & $rank_hus==`j'
local O`i'`j' = r(N)
}
}
* expected cell counts 
forvalues z = 1/4{
local c`z' = `O1`z''+`O2`z''+`O3`z''+`O4`z''
local r`z' = `O`z'1'+`O`z'2'+`O`z'3'+`O`z'4'
}
if $sampleRM == 2{							// to include unmarried
	if `x'==0{				
	local c3 = `c3' + 75
	local c4 = `c4' + 17
	local r3 = `r3' + 82
	local r4 = `r4' + 59
	}
	if `x'==1{
	local c3 = `c3' + 25
	local c4 = `c4' + 3
	local r3 = `r3' + 28
	local r4 = `r4' + 10
	}
}
local Nmen = `c1'+`c2'+`c3'+`c4'
forvalues i = 1/4{
forvalues j = 1/4{
local E`i'`j' = `r`i''*`c`j''/`Nmen'
}
}
if $nacells == 1{
	local E11 = 0
	local E12 = 0
	local E21 = 0
	local E22 = 0
}
* concordant and discordant pairs
forvalues j = 1/3{
local y = `j'+1
forvalues i = 1/3{
local C`i'`j' = 0
local z = `i'+1
forvalues i0=`z'/4{
forvalues j0=`y'/4{
local C`i'`j' = `C`i'`j''+`O`i'`j''*`O`i0'`j0''
}
}
}
}
forvalues j = 4(-1)2{
local y = `j'-1
forvalues i = 1/3{
local D`i'`j' = 0
local z = `i'+1
forvalues i0=`z'/4{
forvalues j0=1/`y'{
local D`i'`j' = `D`i'`j''+`O`i'`j''*`O`i0'`j0''
}    
}
}
}
local C = 0
local D = 0
forvalues i=3/3{	// rows 3 (and 4, where C D YY are zeros) because sample restricted to women
	local C  = `C' + `C`i'1'+`C`i'2'+`C`i'3' 
	local D  = `D' + `D`i'2'+`D`i'3'+`D`i'4' 
}
if $sampleM == 1{	// to add all peer's sons cells
	local C  = `C' +`O13'*(`O24'+`O34'+`O44')+`O23'*(`O34'+`O44')
	local D  = `D' +`O14'*(`O23'+`O33'+`O43')+`O24'*(`O33'+`O43') 
}
if $nacells == 0{	// redo for the 4x4 table
	local C = 0
	local D = 0
	forvalues i=1/3{
	local C  = `C' + `C`i'1'+`C`i'2'+`C`i'3' 
	local D  = `D' + `D`i'2'+`D`i'3'+`D`i'4' 
}
}
* ties
local T = 0
local S = 0
if $nacells == 0 | $sampleM == 1{
	forvalues i=1/4{
	local j = `i'
	local t`i' = `O`i'1'+`O`i'2'+`O`i'3'+`O`i'4'
	local T = `T' + `t`i''*(`t`i''-1)/2
	local s`j' = `O1`j''+`O2`j''+`O3`j''+`O4`j''
	local S = `S' + `s`j''*(`s`j''-1)/2
	}
}
if $sampleM == 0{
	forvalues i=3/4{
	local t`i' = `O`i'1'+`O`i'2'+`O`i'3'+`O`i'4'
	local T = `T' + `t`i''*(`t`i''-1)/2
	}
	forvalues j=1/4{
	local s`j' = `O3`j''+`O4`j''
	local S = `S' + `s`j''*(`s`j''-1)/2
	}
}
* total number of pairs
if $nacells == 0{
	qui tab $rank_wif $rank_hus if treatment==`x' & rank_wif>0 & rank_hus>0 // for 4x4 tables
	local n`x'= r(N)
	local Np_`x' = `n`x''*(`n`x''-1)/2
}
if $sampleM == 0{
	qui tab $rank_wif $rank_hus if treatment==`x' & rank_wif>2 & rank_hus>0 // analysis restricted to women
	local n`x'= r(N)
	local Np_`x' = `n`x''*(`n`x''-1)/2
}
if $sampleM == 1{
	qui tab $rank_wif $rank_hus if treatment==`x' & rank_wif>0 & rank_hus>0 // remove artificial pairs due to n/a
	local n`x'= r(N)
	local Np_`x' = `n`x''*(`n`x''-1)/2 - (`D12'+`D13'+`D14'+`D22'+`D23'+`D24'+`D32'+`D33'+`D34'-`D')
}
* Chi-2 statistics
local chi2_`x' = 0
local chi2LR_`x' = 0
forvalues i=$wom / 4{ 	// rows 3 and 4 because analysis restricted to women
forvalues j=1/4{
if `E`i'`j''!=0{
local chi2_`x' = `chi2_`x'' + ((`O`i'`j''-`E`i'`j'')^2)/`E`i'`j''
local chi2LR_`x' = `chi2LR_`x'' + 2*`O`i'`j''*log(0.000000000001+`O`i'`j''/`E`i'`j'')
}
}
}
* Chi-2 p-values
local pvchi2_`x' = chi2tail(3,`chi2_`x'')
local pvchi2LR_`x' = chi2tail(3,`chi2LR_`x'')

* Kendall's tau-b
local taub_`x' = (`C'-`D')/(sqrt((`Np_`x''-`T')*(`Np_`x''-`S')))
* Z-score for tau-b significance
foreach xx in vt vs sum1v1 sum2v1 sum1v2 sum2v2{
local `xx' = 0
}
local v0 = 2*`Np_`x''*(2*`n`x''+5)
if $nacells == 0 | $sampleM == 1{
local row2 = 1
}
if $sampleM == 0{
local row2 = 3
}
forvalues i=`row2'/4{
local vt = `vt'+`t`i''*(`t`i''-1)*(2*`t`i''+5)
local sum1v1 = `sum1v1'+`t`i''*(`t`i''-1)
local sum1v2 = `sum1v2'+`t`i''*(`t`i''-1)*(`t`i''-2)
}
forvalues j=1/4{
local vs = `vs'+`s`j''*(`s`j''-1)*(2*`s`j''+5)
local sum2v1 = `sum2v1'+`s`j''*(`s`j''-1)
local sum2v2 = `sum1v2'+`s`j''*(`s`j''-1)*(`s`j''-2)
}
local v1 = `sum1v1'*`sum2v1'/(2*2*`Np_`x'')
local v2 = `sum1v2'*`sum2v2'/(9*2*`Np_`x''*(`n`x''-2))
local v = (`v0'-`vt'-`vs')/18 + `v1' + `v2'
local z_`x' = (`C'-`D')/(sqrt(`v'))
local pv_`x'= 2*(normal(-`z_`x''))
}
* Chi2 difference tests
local chi2_diff = `chi2_0'-`chi2_1'
local pvchi2_diff = chi2tail(3,`chi2_diff')
local chi2LR_diff = `chi2LR_0'-`chi2LR_1'
local pvchi2LR_diff = chi2tail(3,`chi2LR_diff')

* Store results
mat tabB8_[7,1] = `chi2_0'
mat tabB8_[7,2] = `pvchi2_0'
mat tabB8_[7,3] = `chi2_1'
mat tabB8_[7,4] = `pvchi2_1'
mat tabB8_[7,5] = `chi2_0'-`chi2_1'
mat tabB8_[7,6] = `pvchi2_diff'
mat tabB8_[8,1] = `chi2LR_0'
mat tabB8_[8,2] = `pvchi2LR_0'
mat tabB8_[8,3] = `chi2LR_1'
mat tabB8_[8,4] = `pvchi2LR_1'
mat tabB8_[8,5] = `chi2LR_0'-`chi2LR_1'
mat tabB8_[8,6] = `pvchi2LR_diff'

esttab matrix(tabB8_, fmt(3)), mlabel("Table B8 (Panel B)")


* Panel D
* ------------------------------------------------------------------------------
global nacells  1 	// (=0 for 4x4 tables; =1 if n/a cells)
global sampleM	1	// (=0 baseline; =1 baseline+peer son)
global sampleRM 1	// (=0 baseline; =1 baseline+peer son; =2 baseline+peer son+unmarried)

if $nacells == 0{
global wom 1
global sampleM 999
global sampleRM 999
}

if $sampleM == 0 & $sampleRM == 0{
keep if $rank_wif>2 & $rank_hus>0
}
if $sampleM == 0{
global wom 3
}
if $sampleM == 1{
global wom 1
}

forvalues x = 1(-1)0{
* observed cell counts 
forvalues i = 1/4{
forvalues j = 1/4{
count if treatment==`x' & $rank_wif==`i' & $rank_hus==`j'
local O`i'`j' = r(N)
}
}
* expected cell counts 
forvalues z = 1/4{
local c`z' = `O1`z''+`O2`z''+`O3`z''+`O4`z''
local r`z' = `O`z'1'+`O`z'2'+`O`z'3'+`O`z'4'
}
if $sampleRM == 2{							// to include unmarried
	if `x'==0{				
	local c3 = `c3' + 75
	local c4 = `c4' + 17
	local r3 = `r3' + 82
	local r4 = `r4' + 59
	}
	if `x'==1{
	local c3 = `c3' + 25
	local c4 = `c4' + 3
	local r3 = `r3' + 28
	local r4 = `r4' + 10
	}
}
local Nmen = `c1'+`c2'+`c3'+`c4'
forvalues i = 1/4{
forvalues j = 1/4{
local E`i'`j' = `r`i''*`c`j''/`Nmen'
}
}
if $nacells == 1{
	local E11 = 0
	local E12 = 0
	local E21 = 0
	local E22 = 0
}
* concordant and discordant pairs
forvalues j = 1/3{
local y = `j'+1
forvalues i = 1/3{
local C`i'`j' = 0
local z = `i'+1
forvalues i0=`z'/4{
forvalues j0=`y'/4{
local C`i'`j' = `C`i'`j''+`O`i'`j''*`O`i0'`j0''
}
}
}
}
forvalues j = 4(-1)2{
local y = `j'-1
forvalues i = 1/3{
local D`i'`j' = 0
local z = `i'+1
forvalues i0=`z'/4{
forvalues j0=1/`y'{
local D`i'`j' = `D`i'`j''+`O`i'`j''*`O`i0'`j0''
}    
}
}
}
local C = 0
local D = 0
forvalues i=3/3{	// rows 3 (and 4, where C D YY are zeros) because sample restricted to women
	local C  = `C' + `C`i'1'+`C`i'2'+`C`i'3' 
	local D  = `D' + `D`i'2'+`D`i'3'+`D`i'4' 
}
if $sampleM == 1{	// to add all peer's sons cells
	local C  = `C' +`O13'*(`O24'+`O34'+`O44')+`O23'*(`O34'+`O44')
	local D  = `D' +`O14'*(`O23'+`O33'+`O43')+`O24'*(`O33'+`O43') 
}
if $nacells == 0{	// redo for the 4x4 table
	local C = 0
	local D = 0
	forvalues i=1/3{
	local C  = `C' + `C`i'1'+`C`i'2'+`C`i'3' 
	local D  = `D' + `D`i'2'+`D`i'3'+`D`i'4' 
}
}
* ties
local T = 0
local S = 0
if $nacells == 0 | $sampleM == 1{
	forvalues i=1/4{
	local j = `i'
	local t`i' = `O`i'1'+`O`i'2'+`O`i'3'+`O`i'4'
	local T = `T' + `t`i''*(`t`i''-1)/2
	local s`j' = `O1`j''+`O2`j''+`O3`j''+`O4`j''
	local S = `S' + `s`j''*(`s`j''-1)/2
	}
}
if $sampleM == 0{
	forvalues i=3/4{
	local t`i' = `O`i'1'+`O`i'2'+`O`i'3'+`O`i'4'
	local T = `T' + `t`i''*(`t`i''-1)/2
	}
	forvalues j=1/4{
	local s`j' = `O3`j''+`O4`j''
	local S = `S' + `s`j''*(`s`j''-1)/2
	}
}
* total number of pairs
if $nacells == 0{
	qui tab $rank_wif $rank_hus if treatment==`x' & rank_wif>0 & rank_hus>0 // for 4x4 tables
	local n`x'= r(N)
	local Np_`x' = `n`x''*(`n`x''-1)/2
}
if $sampleM == 0{
	qui tab $rank_wif $rank_hus if treatment==`x' & rank_wif>2 & rank_hus>0 // analysis restricted to women
	local n`x'= r(N)
	local Np_`x' = `n`x''*(`n`x''-1)/2
}
if $sampleM == 1{
	qui tab $rank_wif $rank_hus if treatment==`x' & rank_wif>0 & rank_hus>0 // remove artificial pairs due to n/a
	local n`x'= r(N)
	local Np_`x' = `n`x''*(`n`x''-1)/2 - (`D12'+`D13'+`D14'+`D22'+`D23'+`D24'+`D32'+`D33'+`D34'-`D')
}
* Chi-2 statistics
local chi2_`x' = 0
local chi2LR_`x' = 0
forvalues i=$wom / 4{ 	// rows 3 and 4 because analysis restricted to women
forvalues j=1/4{
if `E`i'`j''!=0{
local chi2_`x' = `chi2_`x'' + ((`O`i'`j''-`E`i'`j'')^2)/`E`i'`j''
local chi2LR_`x' = `chi2LR_`x'' + 2*`O`i'`j''*log(0.000000000001+`O`i'`j''/`E`i'`j'')
}
}
}
* Chi-2 p-values
local pvchi2_`x' = chi2tail(3,`chi2_`x'')
local pvchi2LR_`x' = chi2tail(3,`chi2LR_`x'')

* Kendall's tau-b
local taub_`x' = (`C'-`D')/(sqrt((`Np_`x''-`T')*(`Np_`x''-`S')))
* Z-score for tau-b significance
foreach xx in vt vs sum1v1 sum2v1 sum1v2 sum2v2{
local `xx' = 0
}
local v0 = 2*`Np_`x''*(2*`n`x''+5)
if $nacells == 0 | $sampleM == 1{
local row2 = 1
}
if $sampleM == 0{
local row2 = 3
}
forvalues i=`row2'/4{
local vt = `vt'+`t`i''*(`t`i''-1)*(2*`t`i''+5)
local sum1v1 = `sum1v1'+`t`i''*(`t`i''-1)
local sum1v2 = `sum1v2'+`t`i''*(`t`i''-1)*(`t`i''-2)
}
forvalues j=1/4{
local vs = `vs'+`s`j''*(`s`j''-1)*(2*`s`j''+5)
local sum2v1 = `sum2v1'+`s`j''*(`s`j''-1)
local sum2v2 = `sum1v2'+`s`j''*(`s`j''-1)*(`s`j''-2)
}
local v1 = `sum1v1'*`sum2v1'/(2*2*`Np_`x'')
local v2 = `sum1v2'*`sum2v2'/(9*2*`Np_`x''*(`n`x''-2))
local v = (`v0'-`vt'-`vs')/18 + `v1' + `v2'
local z_`x' = (`C'-`D')/(sqrt(`v'))
local pv_`x'= 2*(normal(-`z_`x''))
}
* Chi2 difference tests
local chi2_diff = `chi2_0'-`chi2_1'
local pvchi2_diff = chi2tail(3,`chi2_diff')
local chi2LR_diff = `chi2LR_0'-`chi2LR_1'
local pvchi2LR_diff = chi2tail(3,`chi2LR_diff')

* Tau-b
* step 1: convert tau-b into Pearson's r (Kendall 1970 Rank correlation methods: p.126)
local r_0   = sin(3.1416*`taub_0'*0.5)
local r_1   = sin(3.1416*`taub_1'*0.5)
* step 2:  compute Spearman’s rho (Gilpin 1993 "Table for conversion of Kendall’s tau to Spearman’s rho": p. 91)
local rho_0 = 3* `taub_0' * asin(`r_0'/2) / asin(`r_0')
local rho_1 = 3* `taub_1' * asin(`r_1'/2) / asin(`r_1')
* step 2:  convert r into Fisher Zr (Rosenthal 1994 "Parametric measures of effect size": p.237) 
local Zr_0  = 0.5*log((1+`r_0')/(1-`r_0'))
local Zr_1  = 0.5*log((1+`r_1')/(1-`r_1'))
local zobs = (`Zr_0'-`Zr_1') / sqrt(1/(`n0'-3) + 1/(`n1'-3))
* step 3: calculate p-value
local pvtaub_diff = 2*(1-normal(abs(`zobs')))

* Store results
mat tabB8_[10,1] = `chi2_0'
mat tabB8_[10,2] = `pvchi2_0'
mat tabB8_[10,3] = `chi2_1'
mat tabB8_[10,4] = `pvchi2_1'
mat tabB8_[10,5] = `chi2_0'-`chi2_1'
mat tabB8_[10,6] = `pvchi2_diff'
mat tabB8_[11,1] = `chi2LR_0'
mat tabB8_[11,2] = `pvchi2LR_0'
mat tabB8_[11,3] = `chi2LR_1'
mat tabB8_[11,4] = `pvchi2LR_1'
mat tabB8_[11,5] = `chi2LR_0'-`chi2LR_1'
mat tabB8_[11,6] = `pvchi2LR_diff'
mat tabB8_[12,1] = `taub_0'
mat tabB8_[12,2] = `pv_0'
mat tabB8_[12,3] = `taub_1'
mat tabB8_[12,4] = `pv_1'
mat tabB8_[12,5] = `taub_0'-`taub_1'
mat tabB8_[12,6] = `pvtaub_diff'

esttab matrix(tabB8_, fmt(3)), mlabel("Table B8 (Panel B)")


* Panel E
* ------------------------------------------------------------------------------
global nacells  1 	// (=0 for 4x4 tables; =1 if n/a cells)
global sampleM	1	// (=0 baseline; =1 baseline+peer son)
global sampleRM 2	// (=0 baseline; =1 baseline+peer son; =2 baseline+peer son+unmarried)

if $nacells == 0{
global wom 1
global sampleM 999
global sampleRM 999
}

if $sampleM == 0 & $sampleRM == 0{
keep if $rank_wif>2 & $rank_hus>0
}
if $sampleM == 0{
global wom 3
}
if $sampleM == 1{
global wom 1
}

forvalues x = 1(-1)0{
* observed cell counts 
forvalues i = 1/4{
forvalues j = 1/4{
count if treatment==`x' & $rank_wif==`i' & $rank_hus==`j'
local O`i'`j' = r(N)
}
}
* expected cell counts 
forvalues z = 1/4{
local c`z' = `O1`z''+`O2`z''+`O3`z''+`O4`z''
local r`z' = `O`z'1'+`O`z'2'+`O`z'3'+`O`z'4'
}
if $sampleRM == 2{							// to include unmarried
	if `x'==0{				
	local c3 = `c3' + 75
	local c4 = `c4' + 17
	local r3 = `r3' + 82
	local r4 = `r4' + 59
	}
	if `x'==1{
	local c3 = `c3' + 25
	local c4 = `c4' + 3
	local r3 = `r3' + 28
	local r4 = `r4' + 10
	}
}
local Nmen = `c1'+`c2'+`c3'+`c4'
forvalues i = 1/4{
forvalues j = 1/4{
local E`i'`j' = `r`i''*`c`j''/`Nmen'
}
}
if $nacells == 1{
	local E11 = 0
	local E12 = 0
	local E21 = 0
	local E22 = 0
}
* concordant and discordant pairs
forvalues j = 1/3{
local y = `j'+1
forvalues i = 1/3{
local C`i'`j' = 0
local z = `i'+1
forvalues i0=`z'/4{
forvalues j0=`y'/4{
local C`i'`j' = `C`i'`j''+`O`i'`j''*`O`i0'`j0''
}
}
}
}
forvalues j = 4(-1)2{
local y = `j'-1
forvalues i = 1/3{
local D`i'`j' = 0
local z = `i'+1
forvalues i0=`z'/4{
forvalues j0=1/`y'{
local D`i'`j' = `D`i'`j''+`O`i'`j''*`O`i0'`j0''
}    
}
}
}
local C = 0
local D = 0
forvalues i=3/3{	// rows 3 (and 4, where C D YY are zeros) because sample restricted to women
	local C  = `C' + `C`i'1'+`C`i'2'+`C`i'3' 
	local D  = `D' + `D`i'2'+`D`i'3'+`D`i'4' 
}
if $sampleM == 1{	// to add all peer's sons cells
	local C  = `C' +`O13'*(`O24'+`O34'+`O44')+`O23'*(`O34'+`O44')
	local D  = `D' +`O14'*(`O23'+`O33'+`O43')+`O24'*(`O33'+`O43') 
}
if $nacells == 0{	// redo for the 4x4 table
	local C = 0
	local D = 0
	forvalues i=1/3{
	local C  = `C' + `C`i'1'+`C`i'2'+`C`i'3' 
	local D  = `D' + `D`i'2'+`D`i'3'+`D`i'4' 
}
}
* ties
local T = 0
local S = 0
if $nacells == 0 | $sampleM == 1{
	forvalues i=1/4{
	local j = `i'
	local t`i' = `O`i'1'+`O`i'2'+`O`i'3'+`O`i'4'
	local T = `T' + `t`i''*(`t`i''-1)/2
	local s`j' = `O1`j''+`O2`j''+`O3`j''+`O4`j''
	local S = `S' + `s`j''*(`s`j''-1)/2
	}
}
if $sampleM == 0{
	forvalues i=3/4{
	local t`i' = `O`i'1'+`O`i'2'+`O`i'3'+`O`i'4'
	local T = `T' + `t`i''*(`t`i''-1)/2
	}
	forvalues j=1/4{
	local s`j' = `O3`j''+`O4`j''
	local S = `S' + `s`j''*(`s`j''-1)/2
	}
}
* total number of pairs
if $nacells == 0{
	qui tab $rank_wif $rank_hus if treatment==`x' & rank_wif>0 & rank_hus>0 // for 4x4 tables
	local n`x'= r(N)
	local Np_`x' = `n`x''*(`n`x''-1)/2
}
if $sampleM == 0{
	qui tab $rank_wif $rank_hus if treatment==`x' & rank_wif>2 & rank_hus>0 // analysis restricted to women
	local n`x'= r(N)
	local Np_`x' = `n`x''*(`n`x''-1)/2
}
if $sampleM == 1{
	qui tab $rank_wif $rank_hus if treatment==`x' & rank_wif>0 & rank_hus>0 // remove artificial pairs due to n/a
	local n`x'= r(N)
	local Np_`x' = `n`x''*(`n`x''-1)/2 - (`D12'+`D13'+`D14'+`D22'+`D23'+`D24'+`D32'+`D33'+`D34'-`D')
}
* Chi-2 statistics
local chi2_`x' = 0
local chi2LR_`x' = 0
forvalues i=$wom / 4{ 	// rows 3 and 4 because analysis restricted to women
forvalues j=1/4{
if `E`i'`j''!=0{
local chi2_`x' = `chi2_`x'' + ((`O`i'`j''-`E`i'`j'')^2)/`E`i'`j''
local chi2LR_`x' = `chi2LR_`x'' + 2*`O`i'`j''*log(0.000000000001+`O`i'`j''/`E`i'`j'')
}
}
}
* Chi-2 p-values
local pvchi2_`x' = chi2tail(3,`chi2_`x'')
local pvchi2LR_`x' = chi2tail(3,`chi2LR_`x'')

* Kendall's tau-b
local taub_`x' = (`C'-`D')/(sqrt((`Np_`x''-`T')*(`Np_`x''-`S')))
* Z-score for tau-b significance
foreach xx in vt vs sum1v1 sum2v1 sum1v2 sum2v2{
local `xx' = 0
}
local v0 = 2*`Np_`x''*(2*`n`x''+5)
if $nacells == 0 | $sampleM == 1{
local row2 = 1
}
if $sampleM == 0{
local row2 = 3
}
forvalues i=`row2'/4{
local vt = `vt'+`t`i''*(`t`i''-1)*(2*`t`i''+5)
local sum1v1 = `sum1v1'+`t`i''*(`t`i''-1)
local sum1v2 = `sum1v2'+`t`i''*(`t`i''-1)*(`t`i''-2)
}
forvalues j=1/4{
local vs = `vs'+`s`j''*(`s`j''-1)*(2*`s`j''+5)
local sum2v1 = `sum2v1'+`s`j''*(`s`j''-1)
local sum2v2 = `sum1v2'+`s`j''*(`s`j''-1)*(`s`j''-2)
}
local v1 = `sum1v1'*`sum2v1'/(2*2*`Np_`x'')
local v2 = `sum1v2'*`sum2v2'/(9*2*`Np_`x''*(`n`x''-2))
local v = (`v0'-`vt'-`vs')/18 + `v1' + `v2'
local z_`x' = (`C'-`D')/(sqrt(`v'))
local pv_`x'= 2*(normal(-`z_`x''))
}
* Chi2 difference tests
local chi2_diff = `chi2_0'-`chi2_1'
local pvchi2_diff = chi2tail(3,`chi2_diff')
local chi2LR_diff = `chi2LR_0'-`chi2LR_1'
local pvchi2LR_diff = chi2tail(3,`chi2LR_diff')

* Tau-b
* step 1: convert tau-b into Pearson's r (Kendall 1970 Rank correlation methods: p.126)
local r_0   = sin(3.1416*`taub_0'*0.5)
local r_1   = sin(3.1416*`taub_1'*0.5)
* step 2:  compute Spearman’s rho (Gilpin 1993 "Table for conversion of Kendall’s tau to Spearman’s rho": p. 91)
local rho_0 = 3* `taub_0' * asin(`r_0'/2) / asin(`r_0')
local rho_1 = 3* `taub_1' * asin(`r_1'/2) / asin(`r_1')
* step 2:  convert r into Fisher Zr (Rosenthal 1994 "Parametric measures of effect size": p.237) 
local Zr_0  = 0.5*log((1+`r_0')/(1-`r_0'))
local Zr_1  = 0.5*log((1+`r_1')/(1-`r_1'))
local zobs = (`Zr_0'-`Zr_1') / sqrt(1/(`n0'-3) + 1/(`n1'-3))
* step 3: calculate p-value
local pvtaub_diff = 2*(1-normal(abs(`zobs')))

* Store results
mat tabB8_[13,1] = `chi2_0'
mat tabB8_[13,2] = `pvchi2_0'
mat tabB8_[13,3] = `chi2_1'
mat tabB8_[13,4] = `pvchi2_1'
mat tabB8_[13,5] = `chi2_0'-`chi2_1'
mat tabB8_[13,6] = `pvchi2_diff'
mat tabB8_[14,1] = `chi2LR_0'
mat tabB8_[14,2] = `pvchi2LR_0'
mat tabB8_[14,3] = `chi2LR_1'
mat tabB8_[14,4] = `pvchi2LR_1'
mat tabB8_[14,5] = `chi2LR_0'-`chi2LR_1'
mat tabB8_[14,6] = `pvchi2LR_diff'
mat tabB8_[15,1] = `taub_0'
mat tabB8_[15,2] = `pv_0'
mat tabB8_[15,3] = `taub_1'
mat tabB8_[15,4] = `pv_1'
mat tabB8_[15,5] = `taub_0'-`taub_1'
mat tabB8_[15,6] = `pvtaub_diff'


* TABLE B8 RESULTS
* ----------------
mat colnames tabB8_ = "Low-treat" "(pval)" "High-treat" "(pval)" "Diff" "(pval)"
mat rownames tabB8_ = "Panel A: Pearson chi2" "Panel A: LR chi2" "Panel A: Kendall taub" "Panel B: Pearson chi2" "Panel B: LR chi2" "Panel B: Kendall taub" "Panel C: Pearson chi2" "Panel C: LR chi2" "Panel C: Kendall taub" "Panel D: Pearson chi2" "Panel D: LR chi2" "Panel D: Kendall taub" "Panel E: Pearson chi2" "Panel E: LR chi2" "Panel E: Kendall taub"

esttab matrix(tabB8_, fmt(3)) using "${dirresu}\Appendix\TableB8.tex", mlabel("Table B8") replace

* erase temp files
erase "${dirdta}\sampleCT_allwomen-men.dta"

	
	
* ==============================================================================
* TABLE B9: 
* Alternative outcome variables based on age at marriage
* ------------------------------------------------------------------------------


* Import data and keep baseline sample
use "${dirdata}\final-data.dta", clear
keep if base_sample==1


* Age difference between spouses
replace mage = myear-byear if mage==.
replace smage = myear-sbyear if smage==.
gen agediff0 = smage-mage
gen agediff1 = abs(smage-mage)
label var distlondon "Distance to London"


* Regressions
global controls pr4 biorder hengpee
qui reg mage syntheticT $controls, cluster(byear)
	qui summ mage if e(sample)==1
	local mdepvar = round(r(mean),0.01)
	outreg2 using "${dirresu}\Appendix\TableB9", keep(syntheticT distlondon) nocons label dec(3) coefastr ctitle("age at marr") addtext(Mean DP, `mdepvar', Controls, YES) tex replace
qui reg mage syntheticT $controls distlondon, cluster(byear)
	qui summ mage if e(sample)==1
	local mdepvar = round(r(mean),0.01)
	outreg2 using "${dirresu}\Appendix\TableB9", keep(syntheticT distlondon) nocons label dec(3) coefastr ctitle("age at marr") addtext(Mean DP, `mdepvar', Controls, YES) tex
local ct = "h-w"
forvalues j=0/1{
if `j'==1{
local ct = "abs"    
}
foreach x in "" distlondon{
qui reg agediff`j' syntheticT $controls `x', cluster(byear)
	qui summ agediff`j' if e(sample)==1
	local mdepvar`j'`x' = round(r(mean),0.01)
	outreg2 using "${dirresu}\Appendix\TableB9", keep(syntheticT distlondon) nocons label dec(3) coefastr ctitle("age diff `ct'") addtext(Mean DP, `mdepvar`j'`x'', Controls, YES) tex
}
}


	
* ==============================================================================
* FIGURE B2: 
* Distance to London, marrying a commoner, and family landholdings
* ------------------------------------------------------------------------------


* Project england map (albers projection)

cd "${dirdta}"
shp2dta using "${dirdata}\shapefiles\eng_dissolved", database(ukmap1) coordinates(ukcoord1) genid(id) replace
use ukcoord1, clear
drop if (_X>-2.63 & _X<0 & _Y<53.4937 & _Y>51.0959) | (_X>0 & _X<0.505281 & _Y>50.9604 & _Y<52.7362)  | (_X>-3.3 & _X<-2 & _Y>54.3 & _Y<54.8)  | (_X>-2.6 & _X<-0.47 & _Y>50.9 & _Y<54.3) | (_X>-4 & _X<-1.5 & _Y>50.8 & _Y<51.1) | (_X>-1 & _X<-0.7 & _Y>50.8 & _Y<51)
geo2xy _Y _X, replace proj(albers)
save ukcoord1, replace


* Project seats (albers projection)

use "${dirdata}/final-data-sec4.dta", clear

rename wgslat WGSlat
rename wgslon WGSlon

set obs `=_N+1'		// Add London marker
replace WGSlat = 51.507222 if WGSlat==.
replace WGSlon = -0.1275 if WGSlon==.
gen london = (_n==_N)
replace fatotal = 10000 if london==1

geo2xy WGSlat WGSlon, gen(y_lat x_lon) proj(albers)
gen id_point=_n


* Map married a commoner

preserve
duplicates drop countryseat distlondon WGSlat WGSlon cOut, force
replace cOut = 3 if countryseat==""	// missing values for cOut
graph drop _all
spmap using ukcoord1, id(id_point) ///
	  point(xcoord(x_lon) ycoord(y_lat) sh(smdiamond smdiamond_hollow square) oc(blue%30 red%30 red) fc(blue%30 red%30 red) by(cOut)) ///
	  oc(black) fc(white) os(thin) ///
	  legend(on order(3 "Woman married in peerage" 4 "Woman married a commoner" 5 "London") rows(3)) name(gA, replace)
restore
preserve
use "${dirdata}/final-data-sec4.dta", clear
collapse cOut distlondon, by(refno)
spearman cOut distlondon
local corr = round(r(rho),0.001)
local pval = round(r(p),0.1)
twoway (scatter distlondon cOut, msymbol(circle_hollow)) ///
	(lfit distlondon cOut), ///
	xlabel(-1 " " 0 "M. peerage" 1 "M. commoner" 2 " ") ///
	ytitle("Distance from family seat to London (miles)") ///
	xtitle(" ") ///
	ylabel(0(50)300) ///
	text(290 1.25 "corr. 0`corr'", size(medium)) ///
	text(275 1.25 "(p-value = 0`pval')", size(medium)) ///
	title(" ") legend(off) name(gB, replace) graphregion(color(white))
restore
graph combine gA gB, graphregion(color(white))
graph export "${dirresu}\Appendix\FigureB2_A.pdf", as(pdf) replace


* Map landholdings

preserve
duplicates drop countryseat distlondon WGSlat WGSlon fatotal, force
spmap using ukcoord1, id(id_point) ///
	  point(xcoord(x_lon) ycoord(y_lat) prop(fatotal) sh(smcircle square) oc(blue%30 red) fc(blue%30 red) by(london)) ///
	  oc(black) fc(white) os(thin) legend(on order(3 "Family landholdings" 4 "London") rows(1)) name(gC, replace)
pwcorr fatotal distlondon, sig
local corr = round(r(rho),0.001)
mat define C = r(sig)
local pval = round(C[2,1],0.001)
twoway (scatter fatotal distlondon, msymbol(circle_hollow)) ///
	(lfit fatotal distlondon), ///
	xtitle("Distance from family seat to London (miles)") ///
	ytitle("Family landholdings (acres)") ///
	text(270000 225 "corr. `corr'", size(medium)) ///
	text(250000 225 "(p-value = 0`pval')", size(medium)) ///
	title(" ") legend(off) name(gD, replace) graphregion(color(white))
restore
graph combine gC gD, graphregion(color(white))
graph export "${dirresu}\Appendix\FigureB2_B.pdf", as(pdf) replace
graph drop _all
erase "${dirdta}\ukcoord1.dta"
erase "${dirdta}\ukmap1.dta"


	
* ==============================================================================
* TABLE B10: 
* [Appendix: Political power and sorting by landholdings]
* Dep. variable is a family's political power after woman's marriage.
* ------------------------------------------------------------------------------


* Import data
use "${dirdata}/final-data-sec4.dta", clear


* Control variables:
gen nsib2 = nsib^2 											// Number of siblings squared
global controlsb distlondon i.prank nsib nsib2				// controls for regressions with brothers
global controlsh distlondon i.prank							// controls for regressions with family heads
global hechter propman100 logincome propcon propnonc relig	// county controls from Hechter


* Collapse data such that a woman (and her family) is the unit of observation
encode fbio1, gen(nfam) // clusters at the family level
collapse cOut fmissmatch fmissmatch2 fdown ///
		 syntheticT byear nfam d_MPb_aft d_MPf_bef MPb_years_aft MPf_years_bef d_MPb_locaft2 d_MPf_local2 MPb_years_locaft MPf_years_locbef d_MPh_aft d_MPh_bef MPh_years_aft MPh_years_bef ///
		 biorder distlondon prank nsib nsib2 $hechter, by(refno)


* Regressions
gen nfmissmatch2 = -fmissmatch2	// such that difference in landholdings is wom - husband (Panel C)

	/* Note: Panel A's results equivalent to those reported in Table 6 in paper 

	and reproduced in the master-analysis.do do-file.  */

	label var cOut "W married a commoner"
	global polit d_MPb_aft
	global polbefore d_MPf_bef
	* Col. [1]
	qui ivprobit $polit (cOut = syntheticT biorder) $hechter $controlsb $polbefore, first twostep
		weakiv, null(0) small
		local CLR1 = round(e(clr_p), 0.001)
	qui ivregress liml $polit (cOut = syntheticT biorder) $hechter $controlsb $polbefore, first vce(cluster nfam)
		local Nbobs = `e(N)'	// just so that N appears after CLR p-values in table
		weakiv, null(0) small
		local CLR2 = round(e(clr_p), 0.001)
	qui reg cOut syntheticT biorder $hechter $controlsb $polbefore
		local F1 = round(e(F),0.1)
	qui cmp ($polit = cOut $hechter $controlsb $polbefore) (cOut = syntheticT biorder $hechter $controlsb $polbefore), ind(4 1) quietly cluster(byear)
		qui margins, dydx(*) predict(pr) post
		outreg2 using "${dirresu}\Appendix\TableB10_A", keep(cOut) nocons label nose dec(2) noas noobs	///
		ctitle("MP (b)") addtext(CLR-pval, `CLR1', CLR-pval 2, `CLR2', F-stat, `F1', Nb observations, `Nbobs') tex replace
	* Col [2]
	global polit MPb_years_aft
	global polbefore MPf_years_bef
	qui ivregress liml $polit (cOut = syntheticT biorder) $hechter $controlsb $polbefore, first
		qui weakiv, null(0) small
		local CLR1 = round(e(clr_p), 0.001)
	qui ivregress liml $polit (cOut = syntheticT biorder) $hechter $controlsb $polbefore, first vce(cluster nfam)
		local Nbobs = `e(N)'	// just so that N appears after CLR p-values in table
		qui weakiv, null(0) small
		local CLR2 = round(e(clr_p), 0.001)
	qui reg cOut syntheticT biorder $hechter $controlsb $polbefore
		local F1 = round(e(F),0.1)
	qui cmp ($polit = cOut $hechter $controlsb $polbefore) (cOut = syntheticT biorder $hechter $controlsb $polbefore), ind(1 1) quietly cluster(byear)
		qui margins, dydx(*) post
		outreg2 using "${dirresu}\Appendix\TableB10_A", keep(cOut) nocons label nose dec(2) noas noobs	///
		ctitle("Years (b)") addtext(CLR-pval, `CLR1', CLR-pval 2, `CLR2', F-stat, `F1', Nb observations, `Nbobs') tex
	* Col [3]
	global polit d_MPb_locaft2
	global polbefore d_MPf_local2
	qui ivprobit $polit (cOut = syntheticT biorder) $hechter $controlsb $polbefore, first twostep
		qui weakiv, null(0) small
		local CLR1 = round(e(clr_p), 0.001)
	qui ivregress liml $polit (cOut = syntheticT biorder) $hechter $controlsb $polbefore, first vce(cluster nfam)
		local Nbobs = `e(N)'	// just so that N appears after CLR p-values in table
		qui weakiv, null(0) small
		local CLR2 = round(e(clr_p), 0.001)
	qui reg cOut syntheticT biorder $hechter $controlsb $polbefore
		local F1 = round(e(F),0.1)
	qui cmp ($polit = cOut $hechter $controlsb $polbefore) (cOut = syntheticT biorder $hechter $controlsb $polbefore), ind(4 1) quietly cluster(byear)
		qui margins, dydx(*) predict(pr) post
		outreg2 using "${dirresu}\Appendix\TableB10_A", keep(cOut) nocons label nose dec(2) noas noobs	///
		ctitle("local MP (b)") addtext(CLR-pval, `CLR1', CLR-pval 2, `CLR2', F-stat, `F1', Nb observations, `Nbobs') tex
	* Col [4]
	global polit MPb_years_locaft
	global polbefore MPf_years_locbef
	qui ivregress liml $polit (cOut = syntheticT biorder) $hechter $controlsb $polbefore, first
		qui weakiv, null(0) small
		local CLR1 = round(e(clr_p), 0.001)
	qui ivregress liml $polit (cOut = syntheticT biorder) $hechter $controlsb $polbefore, vce(cluster nfam)
		local Nbobs = `e(N)'	// just so that N appears after CLR p-values in table
		qui weakiv, null(0) small
		local CLR2 = round(e(clr_p), 0.001)
	qui reg cOut syntheticT biorder $hechter $controlsb $polbefore
		local F1 = round(e(F),0.1)
	qui cmp ($polit = cOut $hechter $controlsb $polbefore) (cOut = syntheticT biorder $hechter $controlsb $polbefore), ind(1 1) quietly cluster(byear)
		qui margins, dydx(*) post
		outreg2 using "${dirresu}\Appendix\TableB10_A", keep(cOut) nocons label nose dec(3) noas noobs	///
		ctitle("lyears (b)") addtext(CLR-pval, `CLR1', CLR-pval 2, `CLR2', F-stat, `F1', Nb observations, `Nbobs') tex
	* Col [5]
	global polit d_MPh_aft
	global polbefore d_MPh_bef
	qui ivprobit $polit (cOut = syntheticT biorder) $hechter $controlsh $polbefore, first twostep
		qui weakiv, null(0) small
		local CLR1 = round(e(clr_p), 0.001)
	qui ivregress liml $polit (cOut = syntheticT biorder) $hechter $controlsh $polbefore, vce(cluster nfam)
		local Nbobs = `e(N)'	// just so that N appears after CLR p-values in table
		qui weakiv, null(0) small
		local CLR2 = round(e(clr_p), 0.001)
	qui reg cOut syntheticT biorder $hechter $controlsh $polbefore
		local F1 = round(e(F),0.1)
	qui cmp ($polit = cOut $hechter $controlsh $polbefore) (cOut = syntheticT biorder $hechter $controlsh $polbefore), ind(4 1) quietly cluster(byear)
		qui margins, dydx(*) predict(pr) post
		outreg2 using "${dirresu}\Appendix\TableB10_A", keep(cOut) nocons label nose dec(2) noas noobs	///
		ctitle("MP (h)") addtext(CLR-pval, `CLR1', CLR-pval 2, `CLR2', F-stat, `F1', Nb observations, `Nbobs') tex
	* Col [6]
	global polit MPh_years_aft
	global polbefore MPh_years_bef
	qui ivregress liml $polit (cOut = syntheticT biorder) $hechter $controlsh $polbefore, first 	//
		qui weakiv, null(0) small
		local CLR1 = round(e(clr_p), 0.001)
	qui ivregress liml $polit (cOut = syntheticT biorder) $hechter $controlsh $polbefore, vce(cluster nfam)
		local Nbobs = `e(N)'	// just so that N appears after CLR p-values in table
		qui weakiv, null(0) small
		local CLR2 = round(e(clr_p), 0.001)
	qui reg cOut syntheticT biorder $hechter $controlsh $polbefore
		local F1 = round(e(F),0.1)
	qui cmp ($polit = cOut $hechter $controlsb $polbefore) (cOut = syntheticT biorder $hechter $controlsb $polbefore), ind(1 1) quietly cluster(byear)
		qui margins, dydx(*) post
		outreg2 using "${dirresu}\Appendix\TableB10_A", keep(cOut) nocons label nose dec(2) noas noobs	///
		ctitle("Years (h)") addtext(CLR-pval, `CLR1', CLR-pval 2, `CLR2', F-stat, `F1', Nb observations, `Nbobs') tex

		
local ite = 2
foreach x in fmissmatch nfmissmatch2 fdown{
local panel : word `ite' of `c(ALPHA)'
global Mvar `x'
label var $Mvar "coef"

* Col [1]: Any brother is MP after woman's marriage
global polit d_MPb_aft
global polbefore d_MPf_bef
qui ivregress liml $polit ($Mvar = syntheticT biorder) $hechter $controlsb $polbefore, first
	qui weakiv, null(0) small
	local CLR1 = round(e(clr_p), 0.001)
qui ivregress liml $polit ($Mvar = syntheticT biorder) $hechter $controlsb $polbefore, first vce(cluster nfam)
	local Nbobs = `e(N)'	// just so that N appears after CLR p-values in table
	qui weakiv, null(0) small
	local CLR2 = round(e(clr_p), 0.001)
qui reg $Mvar syntheticT biorder $hechter $controlsb $polbefore
	local F1 = round(e(F),0.1)
qui cmp ($polit = $Mvar $hechter $controlsb $polbefore) ($Mvar = syntheticT biorder $hechter $controlsb $polbefore), ind(4 1) quietly cluster(byear)
	qui margins, dydx(*) predict(pr) post
	outreg2 using "${dirresu}\Appendix\TableB10_`panel'", keep($Mvar) nocons label nose dec(3) noas noobs	///
	ctitle("MP (b)") addtext(CLR-pval, `CLR1', CLR-pval 2, `CLR2', F-stat, `F1', Nb observations, `Nbobs') tex replace
	
* Col [2]: Years MP after woman's marriage (brothers)
global polit MPb_years_aft
global polbefore MPf_years_bef
qui ivregress liml $polit ($Mvar = syntheticT biorder) $hechter $controlsb $polbefore, first
	qui weakiv, null(0) small
	local CLR1 = round(e(clr_p), 0.001)
qui ivregress liml $polit ($Mvar = syntheticT biorder) $hechter $controlsb $polbefore, first vce(cluster nfam)
	local Nbobs = `e(N)'	// just so that N appears after CLR p-values in table
	qui weakiv, null(0) small
	local CLR2 = round(e(clr_p), 0.001)
qui reg $Mvar syntheticT biorder $hechter $controlsb $polbefore
	local F1 = round(e(F),0.1)
qui cmp ($polit = $Mvar $hechter $controlsb $polbefore) ($Mvar = syntheticT biorder $hechter $controlsb $polbefore), ind(1 1) quietly cluster(byear)
	qui margins, dydx(*) post
	outreg2 using "${dirresu}\Appendix\TableB10_`panel'", keep($Mvar) nocons label nose dec(3) noas noobs	///
	ctitle("Years (b)") addtext(CLR-pval, `CLR1', CLR-pval 2, `CLR2', F-stat, `F1', Nb observations, `Nbobs') tex

	
* Col [3]: Any brother is local MP after woman's marriage
global polit d_MPb_locaft2
global polbefore d_MPf_local2
qui ivregress liml $polit ($Mvar = syntheticT biorder) $hechter $controlsb $polbefore, first
	qui weakiv, null(0) small
	local CLR1 = round(e(clr_p), 0.001)
qui ivregress liml $polit ($Mvar = syntheticT biorder) $hechter $controlsb $polbefore, first vce(cluster nfam)
	local Nbobs = `e(N)'	// just so that N appears after CLR p-values in table
	qui weakiv, null(0) small
	local CLR2 = round(e(clr_p), 0.001)
qui reg $Mvar syntheticT biorder $hechter $controlsb $polbefore
	local F1 = round(e(F),0.1)
qui cmp ($polit = $Mvar $hechter $controlsb $polbefore) ($Mvar = syntheticT biorder $hechter $controlsb $polbefore), ind(4 1) quietly cluster(byear)
	qui margins, dydx(*) predict(pr) post
	outreg2 using "${dirresu}\Appendix\TableB10_`panel'", keep($Mvar) nocons label nose dec(3) noas noobs	///
	ctitle("local MP (b)") addtext(CLR-pval, `CLR1', CLR-pval 2, `CLR2', F-stat, `F1', Nb observations, `Nbobs') tex
	

* Col [4]: Years local MP after woman's marriage (brothers)
global polit MPb_years_locaft
global polbefore MPf_years_locbef
qui ivregress liml $polit ($Mvar = syntheticT biorder) $hechter $controlsb $polbefore, first
	qui weakiv, null(0) small
	local CLR1 = round(e(clr_p), 0.001)
qui ivregress liml $polit ($Mvar = syntheticT biorder) $hechter $controlsb $polbefore, vce(cluster nfam)
	local Nbobs = `e(N)'	// just so that N appears after CLR p-values in table
	qui weakiv, null(0) small
	local CLR2 = round(e(clr_p), 0.001)
qui reg $Mvar syntheticT biorder $hechter $controlsb $polbefore
	local F1 = round(e(F),0.1)
qui cmp ($polit = $Mvar $hechter $controlsb $polbefore) ($Mvar = syntheticT biorder $hechter $controlsb $polbefore), ind(1 1) quietly cluster(byear)
	qui margins, dydx(*) post
	outreg2 using "${dirresu}\Appendix\TableB10_`panel'", keep($Mvar) nocons label nose dec(3) noas noobs	///
	ctitle("lyears (b)") addtext(CLR-pval, `CLR1', CLR-pval 2, `CLR2', F-stat, `F1', Nb observations, `Nbobs') tex


* Col [5]: Family head is MP after woman's marriage
global polit d_MPh_aft
global polbefore d_MPh_bef
qui ivregress liml $polit ($Mvar = syntheticT biorder) $hechter $controlsh $polbefore, first
	qui weakiv, null(0) small
	local CLR1 = round(e(clr_p), 0.001)
qui ivregress liml $polit ($Mvar = syntheticT biorder) $hechter $controlsh $polbefore, vce(cluster nfam)
	local Nbobs = `e(N)'	// just so that N appears after CLR p-values in table
	qui weakiv, null(0) small
	local CLR2 = round(e(clr_p), 0.001)
qui reg $Mvar syntheticT biorder $hechter $controlsh $polbefore
	local F1 = round(e(F),0.1)
qui cmp ($polit = $Mvar $hechter $controlsh $polbefore) ($Mvar = syntheticT biorder $hechter $controlsh $polbefore), ind(4 1) quietly cluster(byear)
	qui margins, dydx(*) predict(pr) post
	outreg2 using "${dirresu}\Appendix\TableB10_`panel'", keep($Mvar) nocons label nose dec(3) noas noobs	///
	ctitle("MP (h)") addtext(CLR-pval, `CLR1', CLR-pval 2, `CLR2', F-stat, `F1', Nb observations, `Nbobs') tex


* Col [6]: Years MP after woman's marriage (family head)
global polit MPh_years_aft
global polbefore MPh_years_bef
qui ivregress liml $polit ($Mvar = syntheticT biorder) $hechter $controlsh $polbefore, first 	//
	qui weakiv, null(0) small
	local CLR1 = round(e(clr_p), 0.001)
qui ivregress liml $polit ($Mvar = syntheticT biorder) $hechter $controlsh $polbefore, vce(cluster nfam)
	local Nbobs = `e(N)'	// just so that N appears after CLR p-values in table
	qui weakiv, null(0) small
	local CLR2 = round(e(clr_p), 0.001)
qui reg $Mvar syntheticT biorder $hechter $controlsh $polbefore
	local F1 = round(e(F),0.1)
qui cmp ($polit = $Mvar $hechter $controlsb $polbefore) ($Mvar = syntheticT biorder $hechter $controlsb $polbefore), ind(1 1) quietly cluster(byear)
	qui margins, dydx(*) post
	outreg2 using "${dirresu}\Appendix\TableB10_`panel'", keep($Mvar) nocons label nose dec(3) noas noobs	///
	ctitle("years (h)") addtext(CLR-pval, `CLR1', CLR-pval 2, `CLR2', F-stat, `F1', Nb observations, `Nbobs') tex

local ite = `ite'+1
}


	
* ==============================================================================
* TABLE B11: 
* Marriage and political power: Families where women were exposed to the
* interruption but brothers married before 1861.
* ------------------------------------------------------------------------------


* 1. DATA EXCLUDING BROTHERS MARRIED BEFORE 1861
* ----------------------------------------------

import excel "${dirdata}\biographies", sheet("Sheet1") firstrow clear


* Brother's birth year

forvalues i=1/10{
qui gen byear_b0`i' = substr(b`i'bio1,strpos(b`i'bio1,"b."),.)
qui gen byear_b`i' = substr(byear_b0`i',1,strpos(byear_b0`i',", ")-1) if regexm(byear_b0`i',", ")==1
qui replace byear_b`i' = substr(byear_b0`i',1,strpos(byear_b0`i',"Last")-1) if byear_b`i'==""
qui replace byear_b`i' = substr(byear_b`i',length(byear_b`i')-4,.)
qui replace byear_b`i' = strrtrim(byear_b`i')
qui replace byear_b`i' = strltrim(byear_b`i')
destring byear_b`i', replace
}


* Marriage year

foreach x in f h b1 b2 b3 b4 b5 b6 b7 b8 b9 b10{
qui gen myear_`x'_0 = substr(`x'bio2,strpos(`x'bio2,"married"),.) if regexm(`x'bio2,"unmarried")==0
gen myear_`x'_alt = regexs(1) if regexm(myear_`x'_0, "([0-9]?[0-9]? [A-Z]*[a-z]* [0-9][0-9][0-9][0-9])")
replace myear_`x'_alt=strtrim(myear_`x'_alt)
gen myear_`x' = substr(myear_`x'_alt,length(myear_`x'_alt)-4,.)
destring myear_`x', replace
}


* Robustness: to drop brothers who married after 1861
forvalues i=1/10{
replace b`i'bio2=" " if myear_b`i'>1861 & myear_b`i'!=.
}
drop if myear_b1>1861 & myear_b2>1861 & myear_b3>1861 & myear_b4>1861 & myear_b5>1861 & myear_b6>1861 & myear_b7>1861 & myear_b8>1861 & myear_b9>1861 & myear_b10>1861 


* MP count(s) and indicator(s)

foreach x in f h b1 b2 b3 b4 b5 b6 b7 b8 b9 b10{
gen d_MP`x' = regexm(`x'bio2,"(M.P.)")
moss `x'bio2, match("(M.P.)") prefix(MP1`x')
gen MP`x' = MP1`x'count
replace MP`x' = 0 if MP`x'==. 
}
gen MPb = MPb1+MPb2+MPb3+MPb4+MPb5+MPb6+MPb7+MPb8+MPb9+MPb10
gen d_MPb = (MPb>0)


* MP information (party, terms, constituency)

foreach x in f h b1 b2 b3 b4 b5 b6 b7 b8 b9 b10{
gen `x'bio2_1 = `x'bio2
gen lib`x' = 0
gen tory`x' = 0
qui sum MP`x'
local y=r(max)
forvalues j=1/`y'{
* all
	gen MPall`x'_`j' = regexs(1) if regexm(`x'bio2_`j',"[Member of Parliament ]+[\(]+[M]+[\.]+[P]+[\.]+[\)]+[1]*[ ]*[for ]*(.+)")
	replace MPall`x'_`j' = subinstr(MPall`x'_`j', "St.", "St",.) 
	replace MPall`x'_`j' = substr(MPall`x'_`j',1,strpos(MPall`x'_`j',"."))
	local i = `j' + 1
	gen `x'bio2_`i' = substr(`x'bio2_`j',strpos(`x'bio2_`j',"Member of Parliament")+28,.)
	drop `x'bio2_`j'
* party
	gen party`x'_`j' = substr(MPall`x'_`j',strpos(MPall`x'_`j',"(")+1,strpos(MPall`x'_`j',")")-2)
	replace lib`x' = 1 if party`x'_`j'=="Lib" | party`x'_`j'=="Liberal" | party`x'_`j'=="Whig" | party`x'_`j'=="Liberal Union"
	replace tory`x' = 1 if party`x'_`j'=="C" | party`x'_`j'=="Conservative" | party`x'_`j'=="Tory" | party`x'_`j'=="U" | party`x'_`j'=="Unionist"
* constituency
	gen cnsty`x'_`j' = regexs(1) if regexm(MPall`x'_`j',"[the ]*(.+)") & party`x'_`j'==""
	replace cnsty`x'_`j' = regexs(1) if regexm(MPall`x'_`j',"[\(]+[a-zA-Z]+[\)]+[ ]+[for ]*[between ]*[the ]*(.+)") & party`x'_`j'!=""
	foreach endword in "between " "from " "1"{
	replace cnsty`x'_`j' = strrtrim(substr(cnsty`x'_`j',1,strpos(cnsty`x'_`j',"`endword'")-1)) if regexm(cnsty`x'_`j',"`endword'")
	}
	replace cnsty`x'_`j' = strrtrim(substr(cnsty`x'_`j',1,length(cnsty`x'_`j')-1)) if regexm(cnsty`x'_`j',"\.") | regexm(cnsty`x'_`j',",") 
	replace cnsty`x'_`j' = subinstr(cnsty`x'_`j' , " in", "",.)
* term
	gen term1`x'_`j' = substr(MPall`x'_`j',strpos(MPall`x'_`j',"1"),4)
	gen term2`x'_`j' = substr(MPall`x'_`j',strpos(MPall`x'_`j',"1"),.)
	replace term2`x'_`j' = regexs(1) if regexm(term2`x'_`j',"[0-9]*[ ]*[and ]*[to ]*[–]*[a-zA-Z]*[ ]*[a-zA-Z]*[ ]*([0-9]+)")
	replace term2`x'_`j' = term1`x'_`j' if length(term2`x'_`j')<2
	replace term2`x'_`j' = substr(term1`x'_`j',1,2) + substr(term2`x'_`j',1,2) if length(term2`x'_`j')==2
	destring term1`x'_`j' term2`x'_`j', replace
}
}


* Housekeeping

qui sum MPb1
local y=r(max)
if `y'>0{
* Arthur Edward Holland Grey Egerton, 3rd Earl of Wilton
replace term1b1_2 = 1859 if (refno==263156 & cnstyb1_2=="Weymouth") | (refno==263157 & cnstyb1_2=="Weymouth") | (refno==263159 & cnstyb1_2=="Weymouth")
replace term2b1_2 = 1865 if (refno==263156 & cnstyb1_2=="Weymouth") | (refno==263157 & cnstyb1_2=="Weymouth") | (refno==263159 & cnstyb1_2=="Weymouth")
* Arthur Edward Holland Grey Egerton, 3rd Earl of Wilton
replace term1b1_1 = 1873 if (refno==263156 & cnstyb1_1=="Bath") | (refno==263157 & cnstyb1_1=="Bath") | (refno==263159 & cnstyb1_1=="Bath")
replace term2b1_1 = 1874 if (refno==263156 & cnstyb1_1=="Bath") | (refno==263157 & cnstyb1_1=="Bath") | (refno==263159 & cnstyb1_1=="Bath")
* George Augustus Frederick Louis Curzon-Howe, 2nd Earl Howe
replace term1b1_1 = 1857 if (refno==136403 & cnstyb1_1=="South Lancashire") | (refno==136407 & cnstyb1_1=="South Lancashire") | (refno==136408 & cnstyb1_1=="South Lancashire")
replace term2b1_1 = 1870 if (refno==136403 & cnstyb1_1=="South Lancashire") | (refno==136407 & cnstyb1_1=="South Lancashire") | (refno==136408 & cnstyb1_1=="South Lancashire")
}
qui sum MPb2
local y=r(max)
if `y'>0{
* Lord Augustus Henry Charles Hervey
replace term1b2_1 = 1864 if refno==35306 & cnstyb2_1=="West Suffolk"
replace term2b2_1 = 1875 if refno==35306 & cnstyb2_1=="West Suffolk"
}
qui sum MPb3
local y=r(max)
if `y'>0{
* Rt. Hon. Lord Frederick Charles Cavendish
replace term1b3_1 = 1865 if refno==79852 & cnstyb3_1=="North and West Ridings of Yorkshire"
replace term2b3_1 = 1882 if refno==79852 & cnstyb3_1=="North and West Ridings of Yorkshire"
* Commander Hon. Frederick Walpole
replace term1b3_1 = 1868 if refno==196005 & cnstyb3_1=="North Norfolk"
replace term2b3_1 = 1885 if refno==196005 & cnstyb3_1=="North Norfolk"
* Lord Arthur Pelham-Clinton
replace term1b3_1 = 1865 if refno==189002 & cnstyb3_1=="Newark"
replace term2b3_1 = 1868 if refno==189002 & cnstyb3_1=="Newark"
}
qui sum MPb4
local y=r(max)
if `y'>0{
* Hon. Elliot Constantine Yorke
replace term1b4_1 = 1874 if (refno==126701 & cnstyb4_1=="Cambridgeshire") | (refno==126702 & cnstyb4_1=="Cambridgeshire") | (refno==126703 & cnstyb4_1=="Cambridgeshire")
replace term2b4_1 = 1878 if (refno==126701 & cnstyb4_1=="Cambridgeshire") | (refno==126702 & cnstyb4_1=="Cambridgeshire") | (refno==126703 & cnstyb4_1=="Cambridgeshire")
}
qui sum MPb5
local y=r(max)
if `y'>0{
* Lord George Charles Gordon-Lennox
replace term1b5_1 = 1860 if (refno==209856 & cnstyb5_1=="Lymington") | (refno==209860 & cnstyb5_1=="Lymington")
replace term2b5_1 = 1874 if (refno==209856 & cnstyb5_1=="Lymington") | (refno==209860 & cnstyb5_1=="Lymington")
}
qui sum MPh
local y=r(max)
if `y'>0{
* George Augustus Frederick Louis Curzon-Howe, 2nd Earl Howe
replace term1h_1 = 1857 if (refno==136403 & cnstyh_1=="South Lancashire") | (refno==136407 & cnstyh_1=="South Lancashire") | (refno==136408 & cnstyh_1=="South Lancashire")
replace term2h_1 = 1870 if (refno==136403 & cnstyh_1=="South Lancashire") | (refno==136407 & cnstyh_1=="South Lancashire") | (refno==136408 & cnstyh_1=="South Lancashire")
}
qui sum MPf
local y=r(max)
if `y'>0{
* Hon. Robert Henry Clive
replace term1f_1 = 1832 if refno==264509 & cnstyf_1=="South Shropshire"
replace term2f_1 = 1854 if refno==264509 & cnstyf_1=="South Shropshire"
}


* MPs before and after marriage (and total years)

foreach z in h f b1 b2 b3 b4 b5 b6 b7 b8 b9 b10{
local i=1
gen MP`z'_aft = 0
gen MP`z'_bef = 0
gen MP`z'_years = 0
gen MP`z'_years_aft = 0
gen d_MP`z'_bef = 0
qui sum MP`z'
local y = r(max)
if `y'>0{
	forvalues j = 1/`y'{
	summ term1`z'_`j' term2`z'_`j'
	replace MP`z'_aft = 1 if (term1`z'_`j'>=myear & term1`z'_`j'!=.) | (term2`z'_`j'>=myear & term2`z'_`j'!=.)
	replace MP`z'_bef = 1 if term2`z'_`j'< myear & term2`z'_`j'!=.
	replace MP`z'_years = MP`z'_years + term2`z'_`j'-term1`z'_`j' if term1`z'_`j'!=.
	replace MP`z'_years_aft = MP`z'_years_aft + term2`z'_`j'-term1`z'_`j' if myear<=term1`z'_`j' & term1`z'_`j'!=.
	replace MP`z'_years_aft = MP`z'_years_aft + term2`z'_`j'-myear if myear>term1`z'_`j' & myear<=term2`z'_`j' & term1`z'_`j'!=.
	}
}
gen d_MP`z'_aft = (MP`z'_aft>0 & MP`z'_aft!=.)
if `y'>0{
	replace d_MP`z'_bef = (term2`z'_1<myear & term2`z'_1!=.)
}
}
foreach x in aft bef years years_aft{
gen MPb_`x' = MPb1_`x'+MPb2_`x'+MPb3_`x'+MPb4_`x'+MPb5_`x'+MPb6_`x'+MPb7_`x'+MPb8_`x'+MPb9_`x'+MPb10_`x'
gen d_MPb_`x' = (MPb_`x'>0)
}
foreach z in h f b1 b2 b3 b4 b5 b6 b7 b8 b9 b10{
qui sum MP`z'
local y = r(max)
if `y'>0{
forvalues i=1/`y'{


* Constituencies where elected

preserve 
import excel "${dirdata}\constituencies.xlsx", sheet("Sheet1") firstrow clear
rename cnsty cnsty`z'_`i'
rename countyN cnstycounty`z'_`i'
rename wgs cnstywgs`z'_`i'
label var cnsty`z'_`i' "`z''s electoral constituency (`i' M.P. occurrance)"
label var cnstycounty`z'_`i' "`z''s county of electoral constituency (`i' M.P. occurrance)"
label var cnstywgs`z'_`i' "`z''s lat & lon of electoral constituency (`i' M.P. occurrance)"
label var note "`z''s type of electoral constituency (`i' M.P. occurrance)"
label var history "`z''s electoral constituency (`i' M.P. occurrance) active from . to ."
save "${dirdta}\constituencies.dta", replace
restore
merge m:1 cnsty`z'_`i' using "${dirdta}\constituencies.dta", gen(cnsty`z'_merge)
drop if cnsty`z'_merge==2
drop cnsty`z'_merge
}
}
}


* Relation to family head

gen fhead_1872_2 = fhead_1872
replace fhead_1872_2 = "brother" if fhead_1872=="brother (1)" | fhead_1872=="brother (2)" | fhead_1872=="brother (3)" | fhead_1872=="brother (4)"
encode fhead_1872_2, gen(nfhead_1872)


* Number of brothers

gen nsib = .
forvalues i=1/10{
replace nsib = `i' if b`i'bio1!=""
}


* Save temporary data
	label var refno "Reference number in Hollingsworth"
	label var firstnames "First Names"
	label var surname "Surname"
	label var title "Title"
	label var byear "Birth/baptism Year"
	label var dyear "Death/burial Year"
	label var myear "Marriage year"
	label var wom "Woman (1=yes;0=no)"
	label var mourn "Married in 1861-63"
	label var cOut "Married a commoner"
	label var cs "Recorded family seat indicator (1=yes;0=no)"
	label var fhead_1872 "Family member who was family head in 1872 (brother 1;brother 2... father;other)"
	label var fhead_1878 "Family member who was family head in 1878 (brother 1;brother 2... father;other)"
	label var relation_f "Woman's family relation to f variables"
	label var fh "Father is family head in 1872 (=0 no, =1 yes)"
	forvalues i=1/10{
	label var relation_b`i' "Woman's family relation to b`i' variables"
	label var heir_b`i' "Brother `i' is heir (=0 no; =1 yes)"
	label var b`i'h "Brother `i' is family head in 1872 (=0 no; =1 yes)"
	}
	label var relation_b11 "Woman's relation to B11 (ie, to 'other' relative who is family head in 1872)"
	label var heir_b11 "B11 is heir (=0 no; =1 yes)"
	label var b11bio1 "B11's short biography in thepeerage.com"
	label var b11bio2 "B11's long biography in thepeerage.com"
	label var b11h "B11 is family head in 1872 (=0 no; =1 yes)"
	label var note "Notes on thepeerage.com biographies"
	local j = -1
	foreach x in f h b1 b2 b3 b4 b5 b6 b7 b8 b9 b10{
		if `j'==-1{
		local xlab = "Father"
		}
		if `j'==0{
		local xlab = "Family head in 1872"
		}
		if `j'>0{
		local xlab = "Brother `j'"
		}
	label var `x'bio1 "`xlab''s short biography in thepeerage.com"
	label var `x'bio2 "`xlab''s long biography in thepeerage.com"
	label var myear_`x'_0 "`xlab''s marriage info in thepeerage.com biography"
	label var myear_`x'_alt "`xlab''s marriage date (day month year)"
	label var myear_`x' "`xlab''s marriage year"
	label var d_MP`x' "`xlab' was ever MP (=0 no; 1=yes)"
	label var MP1`x'count "N occurrances of 'M.P.' string in long biography for `xlab'"
	label var MP`x' "N occurrances of 'M.P.' string for `xlab' (incl. zeros)"
	label var lib`x' "`xlab' was ever Liberal/Whig/Liberal Union MP (=0 no; 1=yes)"
	label var tory`x' "`xlab' was ever Conservative/Unionist MP (=0 no; 1=yes)"
	label var MP`x'_years "Total years MP (`xlab')"
	label var MP`x'_years_aft "Years MP after a woman's marriage (`xlab')"
	label var d_MP`x'_bef "`xlab' is MP before woman's marriage"
	label var d_MP`x'_aft "`xlab' is MP after woman's marriage"
	qui summ MP1`x'count
	if `r(max)'>0{
		forvalues k=1/`r(max)'{
		local kord : word `k' of 1st 2nd 3rd 4th 5th 6th 7th 8th 9th 10th
		label var MP1`x'pos`k' "Position in biography of `kord' occurrance of 'M.P.' for `xlab'"
		label var MPall`x'_`k' "`xlab''s MP info in thepeerage.com biography (`kord' M.P. occurrance)"
		label var party`x'_`k' "`xlab''s political party (`kord' M.P. occurrance)"
		label var cnsty`x'_`k' "`xlab''s electoral constituency (`kord' M.P. occurrance)"
		label var term1`x'_`k' "`xlab''s term start year (`kord' M.P. occurrance)"
		label var term2`x'_`k' "`xlab''s term end year (`kord' M.P. occurrance)"
		label var cnstycounty`x'_`k' "`xlab''s county of electoral constituency (`kord' M.P. occurrance)"
		label var cnstywgs`x'_`k' "`xlab''s lat & lon of electoral constituency (`kord' M.P. occurrance)"
		}
	}
	local j = `j'+1 
	}
	forvalues i=1/10{
	label var byear_b0`i' "Notes on Brother `i''s birth & death info in thepeerage.com"
	label var byear_b`i' "Brother `i''s birth year"
	}
	label var MPb "N occurrances of 'M.P.' string in long biography for all brothers"
	label var d_MPb "Any brother was ever MP (=0 no; 1=yes)"
	label var d_MPb_aft "Any brother is MP after woman's marriage"
	label var d_MPb_bef "Any brother is MP before woman's marriage"
	label var MPb_years "Total years MP (all brothers)"
	label var MPb_years_aft "Years MP after a woman's marriage (all brothers)"
	label var fhead_1872_2 "Family member who was family head in 1872 (brother; father; other)"
	label var nfhead_1872 "Encoded family member who was family head in 1872 (brother; father; other)"
	label var nsib "Number of brothers"
	drop fbio2_6 hbio2_5 b1bio2_6 b3bio2_9 b6bio2_2 b7bio2_2 b9bio2_1 b10bio2_1 b2bio2_3 b4bio2_3 b5bio2_1 b8bio2_1 MPh_aft MPh_bef MPf_aft MPf_bef MPb1_aft MPb1_bef MPb2_bef MPb2_aft MPb3_aft MPb3_bef MPb4_aft MPb4_bef MPb5_aft MPb5_bef MPb6_aft MPb6_bef MPb7_aft MPb7_bef MPb8_aft MPb8_bef MPb9_aft MPb9_bef MPb10_aft MPb10_bef MPb_aft MPb_bef d_MPb_years d_MPb_years_aft
save "${dirdta}/Peers_biographies_appB11_exclbro.dta", replace


* 2. DATA EXCLUDING FAMILY HEADS MARRIED BEFORE 1861
* --------------------------------------------------

import excel "${dirdata}\biographies", sheet("Sheet1") firstrow clear


* Brother's birth year

forvalues i=1/10{
qui gen byear_b0`i' = substr(b`i'bio1,strpos(b`i'bio1,"b."),.)
qui gen byear_b`i' = substr(byear_b0`i',1,strpos(byear_b0`i',", ")-1) if regexm(byear_b0`i',", ")==1
qui replace byear_b`i' = substr(byear_b0`i',1,strpos(byear_b0`i',"Last")-1) if byear_b`i'==""
qui replace byear_b`i' = substr(byear_b`i',length(byear_b`i')-4,.)
qui replace byear_b`i' = strrtrim(byear_b`i')
qui replace byear_b`i' = strltrim(byear_b`i')
destring byear_b`i', replace
}


* Marriage year

foreach x in f h b1 b2 b3 b4 b5 b6 b7 b8 b9 b10{
qui gen myear_`x'_0 = substr(`x'bio2,strpos(`x'bio2,"married"),.) if regexm(`x'bio2,"unmarried")==0
gen myear_`x'_alt = regexs(1) if regexm(myear_`x'_0, "([0-9]?[0-9]? [A-Z]*[a-z]* [0-9][0-9][0-9][0-9])")
replace myear_`x'_alt=strtrim(myear_`x'_alt)
gen myear_`x' = substr(myear_`x'_alt,length(myear_`x'_alt)-4,.)
destring myear_`x', replace
}


* Robustness: to drop family heads marrying after 1861
replace hbio2=" " if myear_h>1861 & myear_h!=.


* MP count(s) and indicator(s)

foreach x in f h b1 b2 b3 b4 b5 b6 b7 b8 b9 b10{
gen d_MP`x' = regexm(`x'bio2,"(M.P.)")
moss `x'bio2, match("(M.P.)") prefix(MP1`x')
gen MP`x' = MP1`x'count
replace MP`x' = 0 if MP`x'==. 
}
gen MPb = MPb1+MPb2+MPb3+MPb4+MPb5+MPb6+MPb7+MPb8+MPb9+MPb10
gen d_MPb = (MPb>0)


* MP information (party, terms, constituency)

foreach x in f h b1 b2 b3 b4 b5 b6 b7 b8 b9 b10{
gen `x'bio2_1 = `x'bio2
gen lib`x' = 0
gen tory`x' = 0
qui sum MP`x'
local y=r(max)
forvalues j=1/`y'{
* all
	gen MPall`x'_`j' = regexs(1) if regexm(`x'bio2_`j',"[Member of Parliament ]+[\(]+[M]+[\.]+[P]+[\.]+[\)]+[1]*[ ]*[for ]*(.+)")
	replace MPall`x'_`j' = subinstr(MPall`x'_`j', "St.", "St",.) 
	replace MPall`x'_`j' = substr(MPall`x'_`j',1,strpos(MPall`x'_`j',"."))
	local i = `j' + 1
	gen `x'bio2_`i' = substr(`x'bio2_`j',strpos(`x'bio2_`j',"Member of Parliament")+28,.)
	drop `x'bio2_`j'
* party
	gen party`x'_`j' = substr(MPall`x'_`j',strpos(MPall`x'_`j',"(")+1,strpos(MPall`x'_`j',")")-2)
	replace lib`x' = 1 if party`x'_`j'=="Lib" | party`x'_`j'=="Liberal" | party`x'_`j'=="Whig" | party`x'_`j'=="Liberal Union"
	replace tory`x' = 1 if party`x'_`j'=="C" | party`x'_`j'=="Conservative" | party`x'_`j'=="Tory" | party`x'_`j'=="U" | party`x'_`j'=="Unionist"
* constituency
	gen cnsty`x'_`j' = regexs(1) if regexm(MPall`x'_`j',"[the ]*(.+)") & party`x'_`j'==""
	replace cnsty`x'_`j' = regexs(1) if regexm(MPall`x'_`j',"[\(]+[a-zA-Z]+[\)]+[ ]+[for ]*[between ]*[the ]*(.+)") & party`x'_`j'!=""
	foreach endword in "between " "from " "1"{
	replace cnsty`x'_`j' = strrtrim(substr(cnsty`x'_`j',1,strpos(cnsty`x'_`j',"`endword'")-1)) if regexm(cnsty`x'_`j',"`endword'")
	}
	replace cnsty`x'_`j' = strrtrim(substr(cnsty`x'_`j',1,length(cnsty`x'_`j')-1)) if regexm(cnsty`x'_`j',"\.") | regexm(cnsty`x'_`j',",") 
	replace cnsty`x'_`j' = subinstr(cnsty`x'_`j' , " in", "",.)
* term
	gen term1`x'_`j' = substr(MPall`x'_`j',strpos(MPall`x'_`j',"1"),4)
	gen term2`x'_`j' = substr(MPall`x'_`j',strpos(MPall`x'_`j',"1"),.)
	replace term2`x'_`j' = regexs(1) if regexm(term2`x'_`j',"[0-9]*[ ]*[and ]*[to ]*[–]*[a-zA-Z]*[ ]*[a-zA-Z]*[ ]*([0-9]+)")
	replace term2`x'_`j' = term1`x'_`j' if length(term2`x'_`j')<2
	replace term2`x'_`j' = substr(term1`x'_`j',1,2) + substr(term2`x'_`j',1,2) if length(term2`x'_`j')==2
	destring term1`x'_`j' term2`x'_`j', replace
}
}


* Housekeeping

qui sum MPb1
local y=r(max)
if `y'>0{
* Arthur Edward Holland Grey Egerton, 3rd Earl of Wilton
replace term1b1_2 = 1859 if (refno==263156 & cnstyb1_2=="Weymouth") | (refno==263157 & cnstyb1_2=="Weymouth") | (refno==263159 & cnstyb1_2=="Weymouth")
replace term2b1_2 = 1865 if (refno==263156 & cnstyb1_2=="Weymouth") | (refno==263157 & cnstyb1_2=="Weymouth") | (refno==263159 & cnstyb1_2=="Weymouth")
* Arthur Edward Holland Grey Egerton, 3rd Earl of Wilton
replace term1b1_1 = 1873 if (refno==263156 & cnstyb1_1=="Bath") | (refno==263157 & cnstyb1_1=="Bath") | (refno==263159 & cnstyb1_1=="Bath")
replace term2b1_1 = 1874 if (refno==263156 & cnstyb1_1=="Bath") | (refno==263157 & cnstyb1_1=="Bath") | (refno==263159 & cnstyb1_1=="Bath")
* George Augustus Frederick Louis Curzon-Howe, 2nd Earl Howe
replace term1b1_1 = 1857 if (refno==136403 & cnstyb1_1=="South Lancashire") | (refno==136407 & cnstyb1_1=="South Lancashire") | (refno==136408 & cnstyb1_1=="South Lancashire")
replace term2b1_1 = 1870 if (refno==136403 & cnstyb1_1=="South Lancashire") | (refno==136407 & cnstyb1_1=="South Lancashire") | (refno==136408 & cnstyb1_1=="South Lancashire")
}
qui sum MPb2
local y=r(max)
if `y'>0{
* Lord Augustus Henry Charles Hervey
replace term1b2_1 = 1864 if refno==35306 & cnstyb2_1=="West Suffolk"
replace term2b2_1 = 1875 if refno==35306 & cnstyb2_1=="West Suffolk"
}
qui sum MPb3
local y=r(max)
if `y'>0{
* Rt. Hon. Lord Frederick Charles Cavendish
replace term1b3_1 = 1865 if refno==79852 & cnstyb3_1=="North and West Ridings of Yorkshire"
replace term2b3_1 = 1882 if refno==79852 & cnstyb3_1=="North and West Ridings of Yorkshire"
* Commander Hon. Frederick Walpole
replace term1b3_1 = 1868 if refno==196005 & cnstyb3_1=="North Norfolk"
replace term2b3_1 = 1885 if refno==196005 & cnstyb3_1=="North Norfolk"
* Lord Arthur Pelham-Clinton
replace term1b3_1 = 1865 if refno==189002 & cnstyb3_1=="Newark"
replace term2b3_1 = 1868 if refno==189002 & cnstyb3_1=="Newark"
}
qui sum MPb4
local y=r(max)
if `y'>0{
* Hon. Elliot Constantine Yorke
replace term1b4_1 = 1874 if (refno==126701 & cnstyb4_1=="Cambridgeshire") | (refno==126702 & cnstyb4_1=="Cambridgeshire") | (refno==126703 & cnstyb4_1=="Cambridgeshire")
replace term2b4_1 = 1878 if (refno==126701 & cnstyb4_1=="Cambridgeshire") | (refno==126702 & cnstyb4_1=="Cambridgeshire") | (refno==126703 & cnstyb4_1=="Cambridgeshire")
}
qui sum MPb5
local y=r(max)
if `y'>0{
* Lord George Charles Gordon-Lennox
replace term1b5_1 = 1860 if (refno==209856 & cnstyb5_1=="Lymington") | (refno==209860 & cnstyb5_1=="Lymington")
replace term2b5_1 = 1874 if (refno==209856 & cnstyb5_1=="Lymington") | (refno==209860 & cnstyb5_1=="Lymington")
}
qui sum MPh
local y=r(max)
if `y'>0{
* George Augustus Frederick Louis Curzon-Howe, 2nd Earl Howe
replace term1h_1 = 1857 if (refno==136403 & cnstyh_1=="South Lancashire") | (refno==136407 & cnstyh_1=="South Lancashire") | (refno==136408 & cnstyh_1=="South Lancashire")
replace term2h_1 = 1870 if (refno==136403 & cnstyh_1=="South Lancashire") | (refno==136407 & cnstyh_1=="South Lancashire") | (refno==136408 & cnstyh_1=="South Lancashire")
}
qui sum MPf
local y=r(max)
if `y'>0{
* Hon. Robert Henry Clive
replace term1f_1 = 1832 if refno==264509 & cnstyf_1=="South Shropshire"
replace term2f_1 = 1854 if refno==264509 & cnstyf_1=="South Shropshire"
}


* MPs before and after marriage (and total years)

foreach z in h f b1 b2 b3 b4 b5 b6 b7 b8 b9 b10{
local i=1
gen MP`z'_aft = 0
gen MP`z'_bef = 0
gen MP`z'_years = 0
gen MP`z'_years_aft = 0
gen d_MP`z'_bef = 0
qui sum MP`z'
local y = r(max)
if `y'>0{
	forvalues j = 1/`y'{
	summ term1`z'_`j' term2`z'_`j'
	replace MP`z'_aft = 1 if (term1`z'_`j'>=myear & term1`z'_`j'!=.) | (term2`z'_`j'>=myear & term2`z'_`j'!=.)
	replace MP`z'_bef = 1 if term2`z'_`j'< myear & term2`z'_`j'!=.
	replace MP`z'_years = MP`z'_years + term2`z'_`j'-term1`z'_`j' if term1`z'_`j'!=.
	replace MP`z'_years_aft = MP`z'_years_aft + term2`z'_`j'-term1`z'_`j' if myear<=term1`z'_`j' & term1`z'_`j'!=.
	replace MP`z'_years_aft = MP`z'_years_aft + term2`z'_`j'-myear if myear>term1`z'_`j' & myear<=term2`z'_`j' & term1`z'_`j'!=.
	}
}
gen d_MP`z'_aft = (MP`z'_aft>0 & MP`z'_aft!=.)
if `y'>0{
	replace d_MP`z'_bef = (term2`z'_1<myear & term2`z'_1!=.)
}
}
foreach x in aft bef years years_aft{
gen MPb_`x' = MPb1_`x'+MPb2_`x'+MPb3_`x'+MPb4_`x'+MPb5_`x'+MPb6_`x'+MPb7_`x'+MPb8_`x'+MPb9_`x'+MPb10_`x'
gen d_MPb_`x' = (MPb_`x'>0)
}
foreach z in h f b1 b2 b3 b4 b5 b6 b7 b8 b9 b10{
qui sum MP`z'
local y = r(max)
if `y'>0{
forvalues i=1/`y'{


* Constituencies where elected

preserve 
import excel "${dirdata}\constituencies.xlsx", sheet("Sheet1") firstrow clear
rename cnsty cnsty`z'_`i'
rename countyN cnstycounty`z'_`i'
rename wgs cnstywgs`z'_`i'
label var cnsty`z'_`i' "`z''s electoral constituency (`i' M.P. occurrance)"
label var cnstycounty`z'_`i' "`z''s county of electoral constituency (`i' M.P. occurrance)"
label var cnstywgs`z'_`i' "`z''s lat & lon of electoral constituency (`i' M.P. occurrance)"
label var note "`z''s type of electoral constituency (`i' M.P. occurrance)"
label var history "`z''s electoral constituency (`i' M.P. occurrance) active from . to ."
save "${dirdta}\constituencies.dta", replace
restore
merge m:1 cnsty`z'_`i' using "${dirdta}\constituencies.dta", gen(cnsty`z'_merge)
drop if cnsty`z'_merge==2
drop cnsty`z'_merge
}
}
}


* Relation to family head

gen fhead_1872_2 = fhead_1872
replace fhead_1872_2 = "brother" if fhead_1872=="brother (1)" | fhead_1872=="brother (2)" | fhead_1872=="brother (3)" | fhead_1872=="brother (4)"
encode fhead_1872_2, gen(nfhead_1872)


* Number of brothers

gen nsib = .
forvalues i=1/10{
replace nsib = `i' if b`i'bio1!=""
}

* Labels
	label var refno "Reference number in Hollingsworth"
	label var firstnames "First Names"
	label var surname "Surname"
	label var title "Title"
	label var byear "Birth/baptism Year"
	label var dyear "Death/burial Year"
	label var myear "Marriage year"
	label var wom "Woman (1=yes;0=no)"
	label var mourn "Married in 1861-63"
	label var cOut "Married a commoner"
	label var cs "Recorded family seat indicator (1=yes;0=no)"
	label var fhead_1872 "Family member who was family head in 1872 (brother 1;brother 2... father;other)"
	label var fhead_1878 "Family member who was family head in 1878 (brother 1;brother 2... father;other)"
	label var relation_f "Woman's family relation to f variables"
	label var fh "Father is family head in 1872 (=0 no, =1 yes)"
	forvalues i=1/10{
	label var relation_b`i' "Woman's family relation to b`i' variables"
	label var heir_b`i' "Brother `i' is heir (=0 no; =1 yes)"
	label var b`i'h "Brother `i' is family head in 1872 (=0 no; =1 yes)"
	}
	label var relation_b11 "Woman's relation to B11 (ie, to 'other' relative who is family head in 1872)"
	label var heir_b11 "B11 is heir (=0 no; =1 yes)"
	label var b11bio1 "B11's short biography in thepeerage.com"
	label var b11bio2 "B11's long biography in thepeerage.com"
	label var b11h "B11 is family head in 1872 (=0 no; =1 yes)"
	label var note "Notes on thepeerage.com biographies"
	local j = -1
	foreach x in f h b1 b2 b3 b4 b5 b6 b7 b8 b9 b10{
		if `j'==-1{
		local xlab = "Father"
		}
		if `j'==0{
		local xlab = "Family head in 1872"
		}
		if `j'>0{
		local xlab = "Brother `j'"
		}
	label var `x'bio1 "`xlab''s short biography in thepeerage.com"
	label var `x'bio2 "`xlab''s long biography in thepeerage.com"
	label var myear_`x'_0 "`xlab''s marriage info in thepeerage.com biography"
	label var myear_`x'_alt "`xlab''s marriage date (day month year)"
	label var myear_`x' "`xlab''s marriage year"
	label var d_MP`x' "`xlab' was ever MP (=0 no; 1=yes)"
	label var MP1`x'count "N occurrances of 'M.P.' string in long biography for `xlab'"
	label var MP`x' "N occurrances of 'M.P.' string for `xlab' (incl. zeros)"
	label var lib`x' "`xlab' was ever Liberal/Whig/Liberal Union MP (=0 no; 1=yes)"
	label var tory`x' "`xlab' was ever Conservative/Unionist MP (=0 no; 1=yes)"
	label var MP`x'_years "Total years MP (`xlab')"
	label var MP`x'_years_aft "Years MP after a woman's marriage (`xlab')"
	label var d_MP`x'_bef "`xlab' is MP before woman's marriage"
	label var d_MP`x'_aft "`xlab' is MP after woman's marriage"
	if `j'<10{
	qui summ MP1`x'count
	if `r(max)'>0{
		forvalues k=1/`r(max)'{
		local kord : word `k' of 1st 2nd 3rd 4th 5th 6th 7th 8th 9th 10th
		label var MP1`x'pos`k' "Position in biography of `kord' occurrance of 'M.P.' for `xlab'"
		label var MPall`x'_`k' "`xlab''s MP info in thepeerage.com biography (`kord' M.P. occurrance)"
		label var party`x'_`k' "`xlab''s political party (`kord' M.P. occurrance)"
		label var cnsty`x'_`k' "`xlab''s electoral constituency (`kord' M.P. occurrance)"
		label var term1`x'_`k' "`xlab''s term start year (`kord' M.P. occurrance)"
		label var term2`x'_`k' "`xlab''s term end year (`kord' M.P. occurrance)"
		label var cnstycounty`x'_`k' "`xlab''s county of electoral constituency (`kord' M.P. occurrance)"
		label var cnstywgs`x'_`k' "`xlab''s lat & lon of electoral constituency (`kord' M.P. occurrance)"
		}
	}
	}
	local j = `j'+1 
	}
	forvalues i=1/10{
	label var byear_b0`i' "Notes on Brother `i''s birth & death info in thepeerage.com"
	label var byear_b`i' "Brother `i''s birth year"
	}
	label var MPb "N occurrances of 'M.P.' string in long biography for all brothers"
	label var d_MPb "Any brother was ever MP (=0 no; 1=yes)"
	label var d_MPb_aft "Any brother is MP after woman's marriage"
	label var d_MPb_bef "Any brother is MP before woman's marriage"
	label var MPb_years "Total years MP (all brothers)"
	label var MPb_years_aft "Years MP after a woman's marriage (all brothers)"
	label var fhead_1872_2 "Family member who was family head in 1872 (brother; father; other)"
	label var nfhead_1872 "Encoded family member who was family head in 1872 (brother; father; other)"
	label var nsib "Number of brothers"
	drop b8bio2_2 b5bio2_2 b4bio2_4 b2bio2_5 fbio2_6 hbio2_5 b1bio2_6 b3bio2_9 b6bio2_2 b7bio2_2 b9bio2_1 b10bio2_1 MPh_aft MPh_bef MPf_aft MPf_bef MPb1_aft MPb1_bef MPb2_bef MPb2_aft MPb3_aft MPb3_bef MPb4_aft MPb4_bef MPb5_aft MPb5_bef MPb6_aft MPb6_bef MPb7_aft MPb7_bef MPb8_aft MPb8_bef MPb9_aft MPb9_bef MPb10_aft MPb10_bef MPb_aft MPb_bef d_MPb_years d_MPb_years_aft

	
* Robustness: to drop family heads marrying after 1861
drop if myear_h>1861


* Save temporary data
save "${dirdta}/Peers_biographies_appB11_exclheads.dta", replace
erase "${dirdta}/constituencies.dta"



* 3. DATA FOR REGS EXCLUDING BROTHERS M BEFORE 1861
* --------------------------------------------------

* Import data with geo-located family seats

use "${dirdta}\eduXseatsaverages.dta", clear


* Merge with Hollingsworth final data

gen nthismarriage = 1 // by construction, all women in this sample correspond to first-marriages
merge m:1 refno nthismarriage using "${dirdata}\final-data.dta", keep(mas mat) nogen


* Merge with Peers' biographies data (excluding brothers)

merge m:1 refno using "${dirdta}/Peers_biographies_appB11_exclbro.dta", nogen
drop distlondon // use distance from each seat to London
rename distlondon2 distlondon


* Local political power

	/* Note: This is defined here to use geo-located family seats and check 

	whether individuals were elected MP in the county where the family seat is 

	located */

foreach z in h f b1 b2 b3 b4 b5 b6 b7 b8{
gen MP`z'_local = 0
gen MP`z'_locaft = 0
qui sum MP`z'
local y = r(max)
forvalues i=1/`y'{
replace MP`z'_local = 1 if cnstycounty`z'_`i'==countyN
replace MP`z'_locaft = 1 if (cnstycounty`z'_`i'==countyN & term2`z'_`i'>=myear) | (cnstycounty`z'_`i'==countyN & term1`z'_`i'>=myear)
}
gen d_MP`z'_local = (MP`z'_local>0)
gen d_MP`z'_locaft = (MP`z'_locaft>0)
}


* Years local political power

	/* Note: idem */

foreach z in h f b1 b2 b3 b4 b5 b6 b7 b8 b9 b10{
gen MP`z'_years_loc = 0
gen MP`z'_years_locaft = 0
qui sum MP`z'
local y = r(max)
if `y'>0{
	forvalues j = 1/`y'{
	summ term1`z'_`j' term2`z'_`j'
	replace MP`z'_years_loc = MP`z'_years_loc+term2`z'_`j'-term1`z'_`j' if term1`z'_`j'!=. & cnstycounty`z'_`j'==countyN
	replace MP`z'_years_locaft = MP`z'_years_locaft + term2`z'_`j'-term1`z'_`j' if myear<=term1`z'_`j' & term1`z'_`j'!=. & cnstycounty`z'_`j'==countyN
	replace MP`z'_years_locaft = MP`z'_years_locaft + term2`z'_`j'-myear if myear>term1`z'_`j' & myear<=term2`z'_`j' & term1`z'_`j'!=. & cnstycounty`z'_`j'==countyN
	}
}
}
foreach x in years_loc years_locaft{
gen MPb_`x' = MPb1_`x'+MPb2_`x'+MPb3_`x'+MPb4_`x'+MPb5_`x'+MPb6_`x'+MPb7_`x'+MPb8_`x'+MPb9_`x'+MPb10_`x'
}
gen MPb_local = MPb1_local+MPb2_local+MPb3_local+MPb4_local+MPb5_local+MPb6_local+MPb7_local+MPb8_local
gen d_MPb_local = (MPb_local>0)
gen MPb_locaft = MPb1_locaft+MPb2_locaft+MPb3_locaft+MPb4_locaft+MPb5_locaft+MPb6_locaft+MPb7_locaft+MPb8_locaft
gen d_MPb_locaft = (MPb_locaft>0)
drop MPb1_locaft d_MPb1_locaft MPb2_locaft d_MPb2_locaft MPb3_locaft d_MPb3_locaft MPb4_locaft d_MPb4_locaft MPb5_locaft d_MPb5_locaft MPb6_locaft d_MPb6_locaft MPb7_locaft d_MPb7_locaft MPb8_locaft d_MPb8_locaft MPb1_local MPb2_local MPb3_local MPb4_local MPb5_local MPb6_local MPb7_local MPb8_local d_MPb1_local d_MPb2_local d_MPb3_local d_MPb4_local d_MPb5_local d_MPb6_local d_MPb7_local d_MPb8_local 
bysort refno: egen d_MPb_locaft2 = max(d_MPb_locaft) // for women with more than one seat, take the max local political power (this variable is then collapsed)
bysort refno: egen d_MPf_local2  = max(d_MPf_local)


* Labels 

label var d_MPb_aft "Any brother is MP after woman's marriage"
label var d_MPf_bef "MP indicator before woman's marriage"
label var MPb_years_aft "Years MP after a woman's marriage (all brothers)"
gen MPf_years_bef = MPf_years-MPf_years_aft
label var MPf_years_bef "Years MP before a woman's marriage"
label var d_MPb_locaft "Any brother is local MP after woman's marriage"
label var d_MPb_locaft2 "Any brother is local MP after woman's marriage"
label var d_MPf_local2 "Local MP indicator before woman's marriage"
label var MPb_years_locaft "Years local MP after a woman's marriage (all brothers)"
gen MPf_years_locbef = MPf_years_loc-MPf_years_locaft 
label var MPf_years_locbef "Years local MP before a woman's marriage (all brothers)"
label var d_MPh_aft "Family head is MP after woman's marriage"
label var d_MPh_bef	"Family head is MP before woman's marriage"
label var MPh_years_aft "Years MP after a woman's marriage (family heads)"
gen MPh_years_bef = MPh_years-MPh_years_aft
label var MPh_years_bef "Years MP before a woman's marriage (family heads)"
label var d_MPf_local  "Local MP indicator before woman's marriage, by family seat"
label var d_MPf_local2 "Local MP indicator before woman's marriage, by woman"
label var MPb_years_loc "Total years local MP (all brothers)"
label var MPb_local "N occurrances of 'M.P.' string in same county as family seat (all brothers)"
label var d_MPb_local "Any brother was ever local MP (=0 no; 1=yes)"
label var MPb_locaft "N occurrances of 'M.P.' string in same county as family seat (all brothers)"
label var d_MPb_locaft "Any brother is local MP after woman's marriage, by family seat"
label var d_MPb_locaft2 "Any brother is local MP after woman's marriage, by woman"
label var MPf_years_bef "Years MP before a woman's marriage"
label var MPf_years_locbef "Years local MP before a woman's marriage (father)"
label var MPh_years_bef "Years MP before a woman's marriage (family heads)"
local j = -1
foreach x in f h b1 b2 b3 b4 b5 b6 b7 b8 b9 b10{
	if `j'==-1{
	local xlab = "Father"
	}
	if `j'==0{
	local xlab = "Family head in 1872"
	}
	if `j'>0{
	local xlab = "Brother `j'"
	}
label var MP`x'_years_loc "Total years local MP (`xlab'), by family seat"
label var MP`x'_years_locaft "Years local MP after woman's marriage (`xlab'), by family seat"
local j = `j'+1 
}
label var nthismarriage "Number this marriage"
drop d_MPh_local MPh_local d_MPh_locaft MPh_locaft MPf_local MPf_locaft d_MPf_locaft


save "${dirdta}/final-data-sec4-robust1.dta", replace


* 4. DATA FOR REGS EXCLUDING FAMILY HEADS M BEFORE 1861
* -----------------------------------------------------

* Import data with geo-located family seats

use "${dirdta}\eduXseatsaverages.dta", clear


* Merge with Hollingsworth final data

gen nthismarriage = 1 // by construction, all women in this sample correspond to first-marriages
merge m:1 refno nthismarriage using "${dirdata}\final-data.dta", keep(mas mat) nogen


* Merge with Peers' biographies data (excluding brothers)

merge m:1 refno using "${dirdta}/Peers_biographies_appB11_exclheads.dta", nogen
drop distlondon // use distance from each seat to London
rename distlondon2 distlondon


* Local political power

	/* Note: This is defined here to use geo-located family seats and check 

	whether individuals were elected MP in the county where the family seat is 

	located */

foreach z in h f b1 b2 b3 b4 b5 b6 b7 b8{
gen MP`z'_local = 0
gen MP`z'_locaft = 0
qui sum MP`z'
local y = r(max)
forvalues i=1/`y'{
replace MP`z'_local = 1 if cnstycounty`z'_`i'==countyN
replace MP`z'_locaft = 1 if (cnstycounty`z'_`i'==countyN & term2`z'_`i'>=myear) | (cnstycounty`z'_`i'==countyN & term1`z'_`i'>=myear)
}
gen d_MP`z'_local = (MP`z'_local>0)
gen d_MP`z'_locaft = (MP`z'_locaft>0)
}


* Years local political power

	/* Note: idem */

foreach z in h f b1 b2 b3 b4 b5 b6 b7 b8 b9 b10{
gen MP`z'_years_loc = 0
gen MP`z'_years_locaft = 0
qui sum MP`z'
local y = r(max)
if `y'>0{
	forvalues j = 1/`y'{
	summ term1`z'_`j' term2`z'_`j'
	replace MP`z'_years_loc = MP`z'_years_loc+term2`z'_`j'-term1`z'_`j' if term1`z'_`j'!=. & cnstycounty`z'_`j'==countyN
	replace MP`z'_years_locaft = MP`z'_years_locaft + term2`z'_`j'-term1`z'_`j' if myear<=term1`z'_`j' & term1`z'_`j'!=. & cnstycounty`z'_`j'==countyN
	replace MP`z'_years_locaft = MP`z'_years_locaft + term2`z'_`j'-myear if myear>term1`z'_`j' & myear<=term2`z'_`j' & term1`z'_`j'!=. & cnstycounty`z'_`j'==countyN
	}
}
}
foreach x in years_loc years_locaft{
gen MPb_`x' = MPb1_`x'+MPb2_`x'+MPb3_`x'+MPb4_`x'+MPb5_`x'+MPb6_`x'+MPb7_`x'+MPb8_`x'+MPb9_`x'+MPb10_`x'
}
gen MPb_local = MPb1_local+MPb2_local+MPb3_local+MPb4_local+MPb5_local+MPb6_local+MPb7_local+MPb8_local
gen d_MPb_local = (MPb_local>0)
gen MPb_locaft = MPb1_locaft+MPb2_locaft+MPb3_locaft+MPb4_locaft+MPb5_locaft+MPb6_locaft+MPb7_locaft+MPb8_locaft
gen d_MPb_locaft = (MPb_locaft>0)
drop MPb1_locaft d_MPb1_locaft MPb2_locaft d_MPb2_locaft MPb3_locaft d_MPb3_locaft MPb4_locaft d_MPb4_locaft MPb5_locaft d_MPb5_locaft MPb6_locaft d_MPb6_locaft MPb7_locaft d_MPb7_locaft MPb8_locaft d_MPb8_locaft MPb1_local MPb2_local MPb3_local MPb4_local MPb5_local MPb6_local MPb7_local MPb8_local d_MPb1_local d_MPb2_local d_MPb3_local d_MPb4_local d_MPb5_local d_MPb6_local d_MPb7_local d_MPb8_local 
bysort refno: egen d_MPb_locaft2 = max(d_MPb_locaft) // for women with more than one seat, take the max local political power (this variable is then collapsed)
bysort refno: egen d_MPf_local2  = max(d_MPf_local)


* Labels 

* Labels 

label var d_MPb_aft "Any brother is MP after woman's marriage"
label var d_MPf_bef "MP indicator before woman's marriage"
label var MPb_years_aft "Years MP after a woman's marriage (all brothers)"
gen MPf_years_bef = MPf_years-MPf_years_aft
label var MPf_years_bef "Years MP before a woman's marriage"
label var d_MPb_locaft "Any brother is local MP after woman's marriage"
label var d_MPb_locaft2 "Any brother is local MP after woman's marriage"
label var d_MPf_local2 "Local MP indicator before woman's marriage"
label var MPb_years_locaft "Years local MP after a woman's marriage (all brothers)"
gen MPf_years_locbef = MPf_years_loc-MPf_years_locaft 
label var MPf_years_locbef "Years local MP before a woman's marriage (all brothers)"
label var d_MPh_aft "Family head is MP after woman's marriage"
label var d_MPh_bef	"Family head is MP before woman's marriage"
label var MPh_years_aft "Years MP after a woman's marriage (family heads)"
gen MPh_years_bef = MPh_years-MPh_years_aft
label var MPh_years_bef "Years MP before a woman's marriage (family heads)"
label var d_MPf_local  "Local MP indicator before woman's marriage, by family seat"
label var d_MPf_local2 "Local MP indicator before woman's marriage, by woman"
label var MPb_years_loc "Total years local MP (all brothers)"
label var MPb_local "N occurrances of 'M.P.' string in same county as family seat (all brothers)"
label var d_MPb_local "Any brother was ever local MP (=0 no; 1=yes)"
label var MPb_locaft "N occurrances of 'M.P.' string in same county as family seat (all brothers)"
label var d_MPb_locaft "Any brother is local MP after woman's marriage, by family seat"
label var d_MPb_locaft2 "Any brother is local MP after woman's marriage, by woman"
label var MPf_years_bef "Years MP before a woman's marriage"
label var MPf_years_locbef "Years local MP before a woman's marriage (father)"
label var MPh_years_bef "Years MP before a woman's marriage (family heads)"
local j = -1
foreach x in f h b1 b2 b3 b4 b5 b6 b7 b8 b9 b10{
	if `j'==-1{
	local xlab = "Father"
	}
	if `j'==0{
	local xlab = "Family head in 1872"
	}
	if `j'>0{
	local xlab = "Brother `j'"
	}
label var MP`x'_years_loc "Total years local MP (`xlab'), by family seat"
label var MP`x'_years_locaft "Years local MP after woman's marriage (`xlab'), by family seat"
local j = `j'+1 
}
label var nthismarriage "Number this marriage"
drop d_MPh_local MPh_local d_MPh_locaft MPh_locaft MPf_local MPf_locaft d_MPf_locaft

save "${dirdta}/final-data-sec4-robust2.dta", replace


* 5. REGRESSIONS (BROTHERS)
* ----------------------------------------------

* Import data
use "${dirdta}/final-data-sec4-robust1.dta", clear


* Control variables:
gen nsib2 = nsib^2 											// Number of siblings squared
global controlsb distlondon i.prank nsib nsib2				// controls for regressions with brothers
global controlsh distlondon i.prank							// controls for regressions with family heads
global hechter propman100 logincome propcon propnonc relig	// county controls from Hechter


* Collapse data such that a woman (and her family) is the unit of observation
encode fbio1, gen(nfam) // clusters at the family level
collapse cOut syntheticT byear nfam d_MPb_aft d_MPf_bef MPb_years_aft MPf_years_bef d_MPb_locaft2 d_MPf_local2 MPb_years_locaft MPf_years_locbef d_MPh_aft d_MPh_bef MPh_years_aft MPh_years_bef ///
		 biorder distlondon prank nsib nsib2 $hechter, by(refno)
label var cOut "W married a commoner"
label var syntheticT "Treatment"


* Col [1]: Any brother is MP after woman's marriage

global polit d_MPb_aft
global polbefore d_MPf_bef
* CLR p-values
qui ivprobit $polit (cOut = syntheticT biorder) $hechter $controlsb $polbefore, first twostep
	weakiv, null(0) small
	local CLR1 = round(e(clr_p), 0.001)
	local Nbobs = `e(N)'	// just so that N appears after CLR p-values in table
	summ $polit if e(sample)==1
	local mdepvar = round(r(mean),0.01)
* Second stage coefficients
qui cmp ($polit = cOut $hechter $controlsb $polbefore) (cOut = syntheticT biorder $hechter $controlsb $polbefore), ind(4 1) quietly cluster(byear)
	qui margins, dydx(*) predict(pr) post
	outreg2 using "${dirresu}\Appendix\TableB11_A", keep(cOut) nocons label nose dec(2) noas noobs	///
	ctitle("MP (b)") addtext(CLR-pval, `CLR1', Mean DP, `mdepvar', Nb observations, `Nbobs') tex replace
* First stage coefs in Panel B
qui reg cOut syntheticT biorder $hechter $controlsb $polbefore
	local F1 = round(e(F),0.1)
	outreg2 using "${dirresu}\Appendix\TableB11_B", keep(syntheticT) nocons label noas nor2 dec(3) coefastr ctitle("M common") addtext(F-stat, `F1') tex replace


* Col [2]: Years MP after woman's marriage (brothers)

global polit MPb_years_aft
global polbefore MPf_years_bef
* CLR p-values
qui ivregress liml $polit (cOut = syntheticT biorder) $hechter $controlsb $polbefore, first
	qui weakiv, null(0) small
	local CLR1 = round(e(clr_p), 0.001)
	local Nbobs = `e(N)'	// just so that N appears after CLR p-values in table
	summ $polit if e(sample)==1
	local mdepvar = round(r(mean),0.01)
* Second stage coefficients
qui cmp ($polit = cOut $hechter $controlsb $polbefore) (cOut = syntheticT biorder $hechter $controlsb $polbefore), ind(1 1) quietly cluster(byear)
	qui margins, dydx(*) post
	outreg2 using "${dirresu}\Appendix\TableB11_A", keep(cOut) nocons label nose dec(2) noas noobs	///
	ctitle("Years (b)") addtext(CLR-pval, `CLR1', Mean DP, `mdepvar', Nb observations, `Nbobs') tex
* First stage coefs in Panel B
qui reg cOut syntheticT biorder $hechter $controlsb $polbefore
	local F1 = round(e(F),0.1)
	outreg2 using "${dirresu}\Appendix\TableB11_B", keep(syntheticT) nocons label noas nor2 dec(3) coefastr ctitle("M common") addtext(F-stat, `F1') tex

	
* Col [3]: Any brother is local MP after woman's marriage

global polit d_MPb_locaft2
global polbefore d_MPf_local2
* CLR p-values
qui ivprobit $polit (cOut = syntheticT biorder) $hechter $controlsb $polbefore, first twostep
	qui weakiv, null(0) small
	local CLR1 = round(e(clr_p), 0.001)
	local Nbobs = `e(N)'	// just so that N appears after CLR p-values in table
	summ $polit if e(sample)==1
	local mdepvar = round(r(mean),0.01)
* Second stage coefficients
qui cmp ($polit = cOut $hechter $controlsb $polbefore) (cOut = syntheticT biorder $hechter $controlsb $polbefore), ind(4 1) quietly cluster(byear)
	qui margins, dydx(*) predict(pr) post
	outreg2 using "${dirresu}\Appendix\TableB11_A", keep(cOut) nocons label nose dec(2) noas noobs	///
	ctitle("local MP (b)") addtext(CLR-pval, `CLR1', Mean DP, `mdepvar', Nb observations, `Nbobs') tex
* First stage coefs in Panel B
qui reg cOut syntheticT biorder $hechter $controlsb $polbefore
	local F1 = round(e(F),0.1)
	outreg2 using "${dirresu}\Appendix\TableB11_B", keep(syntheticT) nocons label noas nor2 dec(3) coefastr ctitle("M common") addtext(F-stat, `F1') tex
	

* Col [4]: Years local MP after woman's marriage (brothers)

global polit MPb_years_locaft
global polbefore MPf_years_locbef
* CLR p-values
qui ivregress liml $polit (cOut = syntheticT biorder) $hechter $controlsb $polbefore, first
	qui weakiv, null(0) small
	local CLR1 = round(e(clr_p), 0.001)
	local Nbobs = `e(N)'	// just so that N appears after CLR p-values in table
	summ $polit if e(sample)==1
	local mdepvar = round(r(mean),0.01)
* Second stage coefficients
qui cmp ($polit = cOut $hechter $controlsb $polbefore) (cOut = syntheticT biorder $hechter $controlsb $polbefore), ind(1 1) quietly cluster(byear)
	qui margins, dydx(*) post
	outreg2 using "${dirresu}\Appendix\TableB11_A", keep(cOut) nocons label nose dec(3) noas noobs	///
	ctitle("loc years (b)") addtext(CLR-pval, `CLR1', Mean DP, `mdepvar', Nb observations, `Nbobs') tex
* First stage coefs in Panel B
qui reg cOut syntheticT biorder $hechter $controlsb $polbefore
	local F1 = round(e(F),0.1)
	outreg2 using "${dirresu}\Appendix\TableB11_B", keep(syntheticT) nocons label noas nor2 dec(3) coefastr ctitle("M common") addtext(F-stat, `F1') tex


* 5. REGRESSIONS (FAMILY HEADS)
* ----------------------------------------------

* Import data
use "${dirdta}/final-data-sec4-robust2.dta", clear


* Control variables:
gen nsib2 = nsib^2 											// Number of siblings squared
global controlsb distlondon i.prank nsib nsib2				// controls for regressions with brothers
global controlsh distlondon i.prank							// controls for regressions with family heads
global hechter propman100 logincome propcon propnonc relig	// county controls from Hechter


* Collapse data such that a woman (and her family) is the unit of observation
encode fbio1, gen(nfam) // clusters at the family level
collapse cOut syntheticT byear nfam d_MPb_aft d_MPf_bef MPb_years_aft MPf_years_bef d_MPb_locaft2 d_MPf_local2 MPb_years_locaft MPf_years_locbef d_MPh_aft d_MPh_bef MPh_years_aft MPh_years_bef ///
		 biorder distlondon prank nsib nsib2 $hechter, by(refno)
label var cOut "W married a commoner"
label var syntheticT "Treatment"


* Col [5]: Family head is MP after woman's marriage

global polit d_MPh_aft
global polbefore d_MPh_bef
* CLR p-values
qui ivprobit $polit (cOut = syntheticT biorder) $hechter $controlsh $polbefore, first twostep
	qui weakiv, null(0) small
	local CLR1 = round(e(clr_p), 0.001)
	local Nbobs = `e(N)'	// just so that N appears after CLR p-values in table
	summ $polit if e(sample)==1
	local mdepvar = round(r(mean),0.01)
* Second stage coefficients
qui cmp ($polit = cOut $hechter $controlsh $polbefore) (cOut = syntheticT biorder $hechter $controlsh $polbefore), ind(4 1) quietly cluster(byear)
	qui margins, dydx(*) predict(pr) post
	outreg2 using "${dirresu}\Appendix\TableB11_A", keep(cOut) nocons label nose dec(2) noas noobs	///
	ctitle("MP (h)") addtext(CLR-pval, `CLR1', Mean DP, `mdepvar', Nb observations, `Nbobs') tex
* First stage coefs in Panel B
qui reg cOut syntheticT biorder $hechter $controlsh $polbefore
	local F1 = round(e(F),0.1)
	outreg2 using "${dirresu}\Appendix\TableB11_B", keep(syntheticT) nocons label noas nor2 dec(3) coefastr ctitle("M common") addtext(F-stat, `F1') tex


* Col [6]: Years MP after woman's marriage (family head)

global polit MPh_years_aft
global polbefore MPh_years_bef
* CLR p-values
qui ivregress liml $polit (cOut = syntheticT biorder) $hechter $controlsh $polbefore, first 	//
	qui weakiv, null(0) small
	local CLR1 = round(e(clr_p), 0.001)
	local Nbobs = `e(N)'	// just so that N appears after CLR p-values in table
* Second stage coefficients
qui cmp ($polit = cOut $hechter $controlsb $polbefore) (cOut = syntheticT biorder $hechter $controlsb $polbefore), ind(1 1) quietly cluster(byear)
	summ $polit if e(sample)==1
	local mdepvar = round(r(mean),0.01)
	qui margins, dydx(*) post
	outreg2 using "${dirresu}\Appendix\TableB11_A", keep(cOut) nocons label nose dec(2) noas noobs	///
	ctitle("Years (h)") addtext(CLR-pval, `CLR1', Mean DP, `mdepvar', Nb observations, `Nbobs') tex
* First stage coefs in Panel B
qui reg cOut syntheticT biorder $hechter $controlsh $polbefore
	local F1 = round(e(F),0.1)
	outreg2 using "${dirresu}\Appendix\TableB11_B", keep(syntheticT) nocons label noas nor2 dec(3) coefastr ctitle("M common") addtext(F-stat, `F1') tex

	
* erase temp files
erase "${dirdta}\Peers_biographies_appB11_exclbro.dta"
erase "${dirdta}\Peers_biographies_appB11_exclheads.dta"
erase "${dirdta}\eduXseatsaverages.dta"


* ==============================================================================
* FIGURE B3: 
* Age difference between brothers and sisters.
* ------------------------------------------------------------------------------

* Import data
use "${dirdta}/Peers_biographies.dta", clear


* Generate age difference between women and her brothers
local i = 1
forvalues i=1/10{
gen year0_`i' = byear-byear_b`i'
}


* Reshape to have one entry for each woman-brother pair
keep refno cOut myear MPb year0_1 year0_2 year0_3 year0_4 year0_5 year0_6 year0_7 year0_8 year0_9 year0_10
reshape long year0_ , i(refno) j(j)
gen MPi = j
drop j
drop if year0_ == .


* Figure
label var year0_ "age difference with brothers"
kdensity year0_ if cOut==0, lcolor(blue) ///
		 addplot(kdensity year0_ if cOut==1, lcolor(red)) ///
		 graphregion(color(white)) title("") xtitle("") legend(on order(1 "sister married in the peerage" 2 "sister married a commoner") rows(1)) name(p1, replace)
gen year_0 = year0_ if cOut==0
gen year_1 = year0_ if cOut==1
graph 	hbox year_0 year_1, box(1, fcolor(white) lcolor(blue)) box(2, fcolor(white) lcolor(red)) ///
		noout graphregion(color(white)) name(p2, replace)
grc1leg p1 p2, legendfrom(p1) graphregion(color(white)) title("Age difference with brothers", color(black))
graph export "${dirresu}\Appendix\FigureB3.pdf", as(pdf) replace

erase "${dirdta}/Peers_biographies.dta" // not used any more


* ==============================================================================
* FIGURE B4: 
* Loss of political power, men
* ------------------------------------------------------------------------------

* Import data

import excel "${dirroot}\programs\03_appendix\aux data appendix\biographies_men_sample_appB10", first clear

	/* Note: This file contains the data used in Appendix B10: the biographies 

	of 389 men marrying during the interruption of the Season (1861-63) and 

	their brothers (offspring universe, N=288) and their fathers (fathers 

	universe, N=101).  */


* Was the father an heir?

set more off
drop if fbio1==""
gen heir_f = 0
replace heir_f = 1 if regexm(fbio1,"Baron")==1 | regexm(fbio1,"Earl")==1 | regexm(fbio1,"Viscount")==1 | regexm(fbio1,"Marquess")==1 | regexm(fbio1,"Duke")==1 | regexm(fbio1,"Lord")==1
replace heir_f = 1 if refno==335100


* Age at death of brothers

foreach x in b1 b2 b3 b4 b5 b6 b7 b8 b9{
foreach dd in b d{
gen `x'`dd'year = regexs(1) if regexm(`x'bio1,"[`dd']+[\.]+[ ]+[0-9]?[0-9]?[ ]*[a-zA-Z]*[ ]*([0-9]+)")
destring `x'`dd'year, replace
}
replace `x'byear = 1800 + `x'byear if `x'byear<100 
replace `x'dyear = 1800 + `x'dyear if `x'dyear<100 & `x'dyear>=`x'byear-1800 
replace `x'dyear = 1900 + `x'dyear if `x'dyear<100 & `x'dyear<`x'byear-1800 
gen `x'dage = `x'dyear-`x'byear
}
replace b1dage = 1000 if refno==20203  | refno==32050
replace b2dage = 1000 if refno==153950 | refno==160900 | refno==301700
replace b3dage = 1000 if refno==229657 | refno==357000
replace b4dage = 1000 if refno==162150 | refno==357000
foreach x in b1 b2 b3 b4 b5 b6 b7 b8 b9{
replace `x'bio2 = "" if `x'dage<21 | `x'dage==.
}


* Positions in the administration

foreach x in f h b1 b2 b3 b4 b5 b6 b7 b8 b9{

* Academic
moss `x'bio2, match("(F.R.S.)") prefix(`x'acd1)
moss `x'bio2, match("(F.S.A.)") prefix(`x'acd2)
moss `x'bio2, match("President of the British Archaeological Association") prefix(`x'acd3)
moss `x'bio2, match("Trustee of the British Museum") prefix(`x'acd4)
moss `x'bio2, match("Trustee of the National Gallery") prefix(`x'acd5)
gen `x'acd = `x'acd1count+`x'acd2count+`x'acd3count+`x'acd4count+`x'acd5count

* Colonial
moss `x'bio2, match("Colonial Secretary") prefix(`x'col1)								
moss `x'bio2, match("Governor and Commander-in-Chief of Madras") prefix(`x'col2)
moss `x'bio2, match("Parliamentary Under-Secretary, Colonies") prefix(`x'col3)
moss `x'bio2, match("Under-Secretary of the Colonies") prefix(`x'col4)	
moss `x'bio2, match("U-Sec Colonies") prefix(`x'col5)
gen `x'col = `x'col1count+`x'col2count+`x'col3count+`x'col4count+`x'col5count

* Diplomatic
moss `x'bio2, match("Ambassador") prefix(`x'dip1)
moss `x'bio2, match("Ambassador Extraordinary to the Ottoman Empire") prefix(`x'dip2)
moss `x'bio2, match("British Minister at") prefix(`x'dip3)
moss `x'bio2, match("Envoy") prefix(`x'dip4)
moss `x'bio2, match("Envoy Extraordinary and Minister Plenipotentiary") prefix(`x'dip5)
moss `x'bio2, match("Secretary of Embassy") prefix(`x'dip6)
moss `x'bio2, match("Secretary of Legation") prefix(`x'dip7)
moss `x'bio2, match("Under-Secretary of Foreign Affairs") prefix(`x'dip8)
moss `x'bio2, match("U-Sec For Affrs") prefix(`x'dip9)
gen `x'dip = `x'dip1count+`x'dip2count+`x'dip3count+`x'dip4count+`x'dip5count+`x'dip6count+`x'dip7count+`x'dip8count+`x'dip9count

* Great Officer of State
moss `x'bio2, match("High Steward") prefix(`x'gos1)
moss `x'bio2, match("Lord Chancellor") prefix(`x'gos2)
moss `x'bio2, match("Lord of the Treasury") prefix(`x'gos3)
moss `x'bio2, match("Lord Privy Seal") prefix(`x'gos4)
moss `x'bio2, match("First Lord of the Admiralty") prefix(`x'gos5)
gen `x'gos = `x'gos1count+`x'gos2count+`x'gos3count+`x'gos4count+`x'gos5count
moss `x'bio2, match("Lord President of the Council") prefix(w`x'gos1)
moss `x'bio2, match("Lord Great Chamberlain") prefix(w`x'gos2)
moss `x'bio2, match("Earl Marshal") prefix(w`x'gos3)
moss `x'bio2, match("Lord High Constable") prefix(w`x'gos4)
replace `x'gos = `x'gos+w`x'gos1count+w`x'gos2count+w`x'gos3count+w`x'gos4count

* Judicial
moss `x'bio2, match("(J.P.)") prefix(`x'jud1)
moss `x'bio2, match("(K.C.)") prefix(`x'jud2)
moss `x'bio2, match("(Q.C.)") prefix(`x'jud3)
moss `x'bio2, match("Attorney-General") prefix(`x'jud4)
moss `x'bio2, match("Barrister") prefix(`x'jud5)
moss `x'bio2, match("Crown Prosecutor") prefix(`x'jud6)
moss `x'bio2, match("Master in Chancery") prefix(`x'jud7)
moss `x'bio2, match("Solicitor-General") prefix(`x'jud8)
moss `x'bio2, match("Treasurer of the Inner Temple") prefix(`x'jud9)
gen `x'jud = `x'jud1count+`x'jud2count+`x'jud3count+`x'jud4count+`x'jud5count+`x'jud6count+`x'jud7count+`x'jud8count+`x'jud9count

* Military
moss `x'bio2, match("Aide-de-Camp") prefix(`x'mil1)
moss `x'bio2, match("Assistant Military Secretary") prefix(`x'mil2)
moss `x'bio2, match("Brevet Lieutenant-Colonel") prefix(`x'mil3)
moss `x'bio2, match("Brevet Major") prefix(`x'mil4)
moss `x'bio2, match("Captain") prefix(`x'mil5)
moss `x'bio2, match("Colonel") prefix(`x'mil6)
moss `x'bio2, match("commissioned") prefix(`x'mil7)
moss `x'bio2, match("Commodore") prefix(`x'mil8)
moss `x'bio2, match("Ensign") prefix(`x'mil9)
moss `x'bio2, match("Extra Aide-de-Camp") prefix(`x'mil10)
moss `x'bio2, match("Extra Militia Aide-de-Camp") prefix(`x'mil11)
moss `x'bio2, match("Field Marshal") prefix(`x'mil12)
moss `x'bio2, match("General") prefix(`x'mil13)
moss `x'bio2, match("Lieutenant-Colonel") prefix(`x'mil14)
moss `x'bio2, match("Lieutenant-General") prefix(`x'mil15)
moss `x'bio2, match("Lieutenant") prefix(`x'mil16)
moss `x'bio2, match("Major") prefix(`x'mil17)
moss `x'bio2, match("Major-General") prefix(`x'mil18)
moss `x'bio2, match("Militia Aide-de-Camp") prefix(`x'mil19)
moss `x'bio2, match("Midshipman") prefix(`x'mil20)
moss `x'bio2, match("Officer") prefix(`x'mil21)
moss `x'bio2, match("Vicar") prefix(`x'mil22)
moss `x'bio2, match("Vice-Admiral") prefix(`x'mil23)
moss `x'bio2, match("Vice-Admiral of the Yorkshire Coast") prefix(`x'mil24)			
moss `x'bio2, match("Secretary of State for War") prefix(`x'mil25)
gen `x'mil = `x'mil1count+`x'mil2count+`x'mil3count+`x'mil4count+`x'mil5count+`x'mil6count+`x'mil7count+`x'mil8count+`x'mil9count+`x'mil10count+`x'mil11count+`x'mil12count+`x'mil13count+`x'mil14count+`x'mil15count+`x'mil16count+`x'mil17count+`x'mil18count+`x'mil19count+`x'mil20count+`x'mil21count+`x'mil22count+`x'mil23count+`x'mil24count+`x'mil25count
		
* Order
moss `x'bio2, match("(K.C.B.)") prefix(`x'ord1)
moss `x'bio2, match("(K.C.H.)") prefix(`x'ord2)
moss `x'bio2, match("(K.G.)") prefix(`x'ord3)
moss `x'bio2, match("(C.B.)") prefix(`x'ord4)
moss `x'bio2, match("(G.C.B.)") prefix(`x'ord5)
moss `x'bio2, match("(K.P.)") prefix(`x'ord6)
moss `x'bio2, match("(K.T.)") prefix(`x'ord7)
gen `x'ord = `x'ord1count+`x'ord2count+`x'ord3count+`x'ord4count+`x'ord5count+`x'ord6count+`x'ord7count

* Cabinet
moss `x'bio2, match("(P.C.)") prefix(`x'cab1)
moss `x'bio2, match("Chairman of the Customs Board") prefix(`x'cab2)
moss `x'bio2, match("Commissioner of National Education") prefix(`x'cab3)
moss `x'bio2, match("Deputy Chairman of the Customs Board") prefix(`x'cab4)
moss `x'bio2, match("Home Dept") prefix(`x'cab5)
moss `x'bio2, match("Home Secretary") prefix(`x'cab6)
moss `x'bio2, match("Paymaster General") prefix(`x'cab7)
moss `x'bio2, match("Paymaster-Gen") prefix(`x'cab8)
moss `x'bio2, match("PMG with Cabinet seat") prefix(`x'cab9)
moss `x'bio2, match("Postmaster General") prefix(`x'cab10)
moss `x'bio2, match("Pres BOT") prefix(`x'cab11)
moss `x'bio2, match("President of the Board of Trade") prefix(`x'cab12)
moss `x'bio2, match("Vice-President of the Board of Trade") prefix(`x'cab13)
moss `x'bio2, match("V-Pres BOT") prefix(`x'cab14)
moss `x'bio2, match("Sec Treasury") prefix(`x'cab15)
moss `x'bio2, match("Secretary of the Treasury") prefix(`x'cab16)
moss `x'bio2, match("Prime Minister") prefix(`x'cab17)
gen `x'cab = `x'cab1count+`x'cab2count+`x'cab3count+`x'cab4count+`x'cab5count+`x'cab6count+`x'cab7count+`x'cab8count+`x'cab9count+`x'cab10count+`x'cab11count+`x'cab12count+`x'cab13count+`x'cab14count+`x'cab15count+`x'cab16count+`x'cab17count

* Local
moss `x'bio2, match("(D.L.)") prefix(`x'loc1)
moss `x'bio2, match("Burgess of Glasgow") prefix(`x'loc2)
moss `x'bio2, match("Constable") prefix(`x'loc3)
moss `x'bio2, match("Custos Rotulorum") prefix(`x'loc4)
moss `x'bio2, match("Governor") prefix(`x'loc5)
moss `x'bio2, match("Hereditary Sheriff") prefix(`x'loc6)
moss `x'bio2, match("High Sheriff") prefix(`x'loc7)
moss `x'bio2, match("Lord Lieutenant") prefix(`x'loc8)
moss `x'bio2, match("Lord Warden of the Stannaries") prefix(`x'loc9)
moss `x'bio2, match("Sheriff") prefix(`x'loc10)
moss `x'bio2, match("Chief Secretary of Ireland") prefix(`x'loc11)
gen `x'loc = `x'loc1count+`x'loc2count+`x'loc3count+`x'loc4count+`x'loc5count+`x'loc6count+`x'loc7count+`x'loc8count+`x'loc9count+`x'loc10count+`x'loc11count

* Royal Household
moss `x'bio2, match("Gold Stick in Waiting") prefix(`x'hou1)
moss `x'bio2, match("Lord in Waiting") prefix(`x'hou2)
moss `x'bio2, match("Lord of the Bedchamber") prefix(`x'hou3)
moss `x'bio2, match("Treasurer of the Household") prefix(`x'hou4)
moss `x'bio2, match("Equerry") prefix(e`x'hou1)
moss `x'bio2, match("Groom-in-Waiting") prefix(e`x'hou2)
gen `x'hou = `x'hou1count+`x'hou2count+`x'hou3count+`x'hou4count+e`x'hou1count+e`x'hou2count

* Religious
moss `x'bio2, match("Bishop") prefix(`x'rel1)
moss `x'bio2, match("Canon Residentiary") prefix(`x'rel2)
moss `x'bio2, match("Chaplain") prefix(`x'rel3)
moss `x'bio2, match("Dean") prefix(`x'rel4)
moss `x'bio2, match("Ecclesiastical Commissioner") prefix(`x'rel5)
moss `x'bio2, match("Lord High Commissioner of the General Assembly of the Church of Scotland") prefix(`x'rel6)
moss `x'bio2, match("Minister") prefix(`x'rel7)
moss `x'bio2, match("Rector") prefix(`x'rel8)
moss `x'bio2, match("Rector Poole") prefix(`x'rel9)
gen `x'rel = `x'rel1count+`x'rel2count+`x'rel3count+`x'rel4count+`x'rel5count+`x'rel6count+`x'rel7count+`x'rel8count+`x'rel9count

* Other
moss `x'bio2, match("president of the Highland Agricultural Society") prefix(`x'other1)
moss `x'bio2, match("Chief Commissioner of Woods and Forests") prefix(`x'other2)
moss `x'bio2, match("First Commissioner of Woods and Forests") prefix(`x'other3)
gen `x'other = `x'other1count+`x'other2count+`x'other3count

* Parliament
moss `x'bio2, match("(M.P.)") prefix(`x'par1)
gen `x'par = `x'par1count
}
*
keep refno firstnames surname title wom byear dyear myear mourn cOut cs 		///
	 relation_f fbio1 fbio2 relation_h heir_h hbio1 hbio2 						///
	 relation_b1 heir_b1 b1bio1 b1bio2 relation_b2 heir_b2 b2bio1 b2bio2 		///
	 relation_b3 heir_b3 b3bio1 b3bio2 relation_b4 heir_b4 b4bio1 b4bio2 		///
	 relation_b5 heir_b5 b5bio1 b5bio2 relation_b6 heir_b6 b6bio1 b6bio2 		///
	 relation_b7 heir_b7 b7bio1 b7bio2 relation_b8 heir_b8 b8bio1 b8bio2 		///
	 relation_b9 heir_b9 b9bio1 b9bio2 heir_f 									///
	 b1byear b1dyear b1dage b2byear b2dyear b2dage b3byear b3dyear b3dage 		///
	 b4byear b4dyear b4dage b5byear b5dyear b5dage b6byear b6dyear b6dage		/// 
	 b7byear b7dyear b7dage b8byear b8dyear b8dage b9byear b9dyear b9dage		///
	 facd fcol fdip fgos fjud fmil ford fcab floc fhou frel fother fpar				///
	 hacd hcol hdip hgos hjud hmil hord hcab hloc hhou hrel hother hpar				///
	 b1acd b1col b1dip b1gos b1jud b1mil b1ord b1cab b1loc b1hou b1rel b1other b1par	///
	 b2acd b2col b2dip b2gos b2jud b2mil b2ord b2cab b2loc b2hou b2rel b2other b2par	///
	 b3acd b3col b3dip b3gos b3jud b3mil b3ord b3cab b3loc b3hou b3rel b3other b3par	///
	 b4acd b4col b4dip b4gos b4jud b4mil b4ord b4cab b4loc b4hou b4rel b4other b4par	///
	 b5acd b5col b5dip b5gos b5jud b5mil b5ord b5cab b5loc b5hou b5rel b5other b5par	///
	 b6acd b6col b6dip b6gos b6jud b6mil b6ord b6cab b6loc b6hou b6rel b6other b6par	///
	 b7acd b7col b7dip b7gos b7jud b7mil b7ord b7cab b7loc b7hou b7rel b7other b7par	///
	 b8acd b8col b8dip b8gos b8jud b8mil b8ord b8cab b8loc b8hou b8rel b8other b8par	///
	 b9acd b9col b9dip b9gos b9jud b9mil b9ord b9cab b9loc b9hou b9rel b9other b9par

	 
* Replace for missing value if no brother & generate dummies

foreach x in f h b1 b2 b3 b4 b5 b6 b7 b8 b9{
foreach y in acd col dip gos jud mil ord par cab loc hou rel other{
replace `x'`y' = . if `x'bio2==""
gen d_`x'`y' = 1 if `x'`y'>0 & `x'`y'!=.
replace d_`x'`y' = 0 if `x'`y'==0
}
replace relation_`x' = "" if `x'bio2==""
}


* Globals

global all_vars 		loc 		office		rel			jud 	 forg 		hou 		other 	 	par
global d_all_vars 		d_loc 		d_office 	d_rel		d_jud 	 d_forg 	d_hou 		d_other  	d_par
forvalues i=1/10{
global all_vars`i' 		loc`i' 		office`i'	rel`i' 		jud`i' 	 forg`i' 	hou`i' 		other`i' 	par`i'
global d_all_vars`i' 	d_loc`i' 	d_office`i' d_rel`i' 	d_jud`i' d_forg`i' 	d_hou`i' 	d_other`i' 	d_par`i'
}
global allrel_vars 		relloc 		reloffice 	relrel		reljud	 relforg  	relhou 	 	relother 	relpar
global d_allrel_vars 	d_relloc 	d_reloffice d_relrel	d_reljud d_relforg 	d_relhou 	d_relother 	d_relpar
label define lpos 1 "Total" 2 "local" 3 "office" 4 "relig." 5 "judicial" 6 "foreign" 7 "house." 8 "other" 9 "MP"
global num_vars pos1 pos2 pos3 pos4 pos5 pos6 pos7 pos8 pos9
global d_num_vars d_pos1 d_pos2 d_pos3 d_pos4 d_pos5 d_pos6 d_pos7 d_pos8 d_pos9


* Reshape data

local i = 0
foreach x in f h b1 b2 b3 b4 b5 b6 b7 b8 b9{
rename heir_`x' `x'heir
*
gen `x'forg = `x'col+`x'dip
gen d_`x'forg = d_`x'col+d_`x'dip
drop `x'col `x'dip d_`x'col d_`x'dip
*
gen `x'office = `x'cab+`x'gos
gen d_`x'office = d_`x'cab+d_`x'gos
drop `x'cab `x'gos d_`x'cab d_`x'gos
*
local i = `i'+1
rename `x'heir heir`i'
foreach y in $all_vars{
rename `x'`y' `y'`i'
rename d_`x'`y' d_`y'`i'
}
}
keep refno cOut	heir1 heir2 heir3 heir4 heir5 heir6 heir7 heir8 heir9 heir10 		///
	 $all_vars1 $all_vars2 $all_vars3 $all_vars4 $all_vars5 $all_vars6 $all_vars7 	///
	 $all_vars8 $all_vars9 $all_vars10 $d_all_vars1 $d_all_vars2 $d_all_vars3 		///
	 $d_all_vars4 $d_all_vars5 $d_all_vars6 $d_all_vars7 $d_all_vars8 $d_all_vars9 $d_all_vars10
gen id = _n
reshape long heir $all_vars $d_all_vars, i(id) j(j)
keep if heir!=.
drop if forg==. & office==. & jud==. & par==. & loc==. & hou==. & rel==. & other==.
replace heir = 2 if j==1


* Panels A and B: ALL OFFSPRING

preserve
replace heir = 1 if heir==0
collapse j cOut $all_vars $d_all_vars, by(refno heir)
egen tot = rowtotal($all_vars)
egen d_tot = rowtotal($d_all_vars)
gsort refno heir 
foreach x in tot $all_vars{
by refno: gen rel`x' = `x'-`x'[_N]
by refno: gen d_rel`x' = d_`x'-d_`x'[_N]
}
drop if j==1
* reshape
local j = 0
foreach x in tot $all_vars{
local j = `j'+1
rename rel`x' pos`j'
rename d_rel`x' d_pos`j'
}
gen idi = _n
sort idi id heir cOut $num_vars $d_num_vars
keep idi id heir cOut $num_vars $d_num_vars
qui reshape long pos d_pos, i(idi) j(ptype)
label values ptype lpos
* collapse for bar graph
collapse pos d_pos, by(ptype cOut)
sort ptype cOut
drop if ptype==8
replace ptype = 8 if ptype==9
gen out_type = cOut + 3*(ptype-1)
* confidence intervals
gen ciL 	  = -.2725088 if cOut==0 & ptype==1
gen ciH 	  = .8795029  if cOut==0 & ptype==1
replace ciL   = -1.740913 if cOut==1 & ptype==1
replace ciH   = -.4484183 if cOut==1 & ptype==1
gen d_ciL 	  = -0.1107418 if cOut==0 & ptype==1
gen d_ciH 	  = 0.4650573  if cOut==0 & ptype==1
replace d_ciL = -0.7535773 if cOut==1 & ptype==1
replace d_ciH = -0.1454582 if cOut==1 & ptype==1
* figures
global ylab -1.8(0.6)1.8
global ylabd_ -1(0.5)1
global title0 Other offspring
global title1 Heirs
global stitle total number of appointments
global stitled_ appointments indicator
foreach d in "" d_{
local l = "ylab`d'"
local CD = "stitle`d'"
twoway (bar `d'pos out_type if cOut==0) 											 ///
	   (bar `d'pos out_type if cOut==1, fi(30)) 									 ///
	   (rcap `d'ciH `d'ciL out_type, lcolor(black)), 									 ///
	   ylabel(#3, labsize(small))														 ///
	   xlabel(0.5 "total" 3.5 "local" 6.5 "office" 9.5 "relig." 12.5 "judicial" ///
	   15.5 "foreign" 18.5 "house." 21.5 "MP", labs(small) angle(vertical) notick) ///
	   xline(2,  lcolor(gray) lpattern(dot)) xline(5, lcolor(gray) lpattern(dot))  ///
	   xline(8,  lcolor(gray) lpattern(dot)) xline(11, lcolor(gray) lpattern(dot)) ///
	   xline(14, lcolor(gray) lpattern(dot)) xline(17, lcolor(gray) lpattern(dot)) ///
	   xline(20, lcolor(gray) lpattern(dot))									 ///
       legend( order(1 "families marrying in" 2 "families marrying out" 3 "90 c.i.") col(3) size(small))		 ///
	   title("All offspring", color(black) size(medlarge))	///
	   subtitle("$`CD'", color(black) size(small))	///
	   ytitle("son vs. father")  							 ///
	   xtitle("")																 ///	
	   graphregion(color(white)) name(`d'panel_all)
}	   
restore


* Panels C to F: HEIRS AND OTHER OFFSPRING


collapse j refno cOut $all_vars $d_all_vars, by(id heir)
egen tot = rowtotal($all_vars)
egen d_tot = rowtotal($d_all_vars)
gsort id heir 
foreach x in tot $all_vars{
by id: gen rel`x' = `x'-`x'[_N]
by id: gen d_rel`x' = d_`x'-d_`x'[_N]
}
drop if j==1
* reshape
local j = 0
foreach x in tot $all_vars{
local j = `j'+1
rename rel`x' pos`j'
rename d_rel`x' d_pos`j'
}
gen idi = _n
sort idi id heir cOut $num_vars $d_num_vars
keep idi id heir cOut $num_vars $d_num_vars
qui reshape long pos d_pos, i(idi) j(ptype)
label values ptype lpos
* t-tests
ttest pos if ptype==1 & heir==0, by(cOut) level(90)
ttest pos if ptype==1 & heir==1, by(cOut) level(90)
ttest d_pos if ptype==1 & heir==0, by(cOut) level(90)
ttest d_pos if ptype==1 & heir==1, by(cOut) level(90)
* collapse for bar graph
collapse pos d_pos, by(ptype cOut heir)
sort ptype cOut
drop if ptype==8
replace ptype = 8 if ptype==9
gen out_type = cOut + 3*(ptype-1)
* figures
forvalues i=0/1{
foreach d in "" d_{
local l = "ylab`d'"
local L = "title`i'"
local CD = "stitle`d'"
twoway (bar `d'pos out_type if cOut==0 & heir==`i') 											 ///
	   (bar `d'pos out_type if cOut==1 & heir==`i', fi(30)), 									 ///
	   ylabel(#3, labsize(small))														 ///
	   xlabel(0.5 "total" 3.5 "local" 6.5 "office" 9.5 "relig." 12.5 "judicial" ///
	   15.5 "foreign" 18.5 "house." 21.5 "MP", labs(small) angle(vertical) notick) ///
	   xline(2,  lcolor(gray) lpattern(dot)) xline(5, lcolor(gray) lpattern(dot))  ///
	   xline(8,  lcolor(gray) lpattern(dot)) xline(11, lcolor(gray) lpattern(dot)) ///
	   xline(14, lcolor(gray) lpattern(dot)) xline(17, lcolor(gray) lpattern(dot)) ///
	   xline(20, lcolor(gray) lpattern(dot))									 ///
       legend( order(1 "families marrying in" 2 "families marrying out" 3 "90 c.i.") col(3) size(small))		 ///
	   title("$`L'", color(black) size(medlarge))	///
	   subtitle("$`CD'", color(black) size(small))	///
	   ytitle("son vs. father")  							 ///
	   xtitle("")																 ///	
	   graphregion(color(white)) name(`d'panel_`i')
}
}


* Combine into Figure B4

grc1leg panel_all panel_1 panel_0 d_panel_all d_panel_1 d_panel_0, graphregion(color(white))
graph drop panel_all panel_1 panel_0 d_panel_all d_panel_1 d_panel_0
graph export "${dirresu}\Appendix\FigureB4.pdf", as(pdf) replace




* ==============================================================================
* Table B13: 
* Determinants of investments in state education, robustness checks.
* ------------------------------------------------------------------------------

* Import data
use "${dirdata}/final-data-sec4.dta", clear


* Control variables:
gen nsib2 = nsib^2 											// Number of siblings squared
global controlsrf distlondon i.prank						// controls for reduced-form effect
global controlsb  distlondon i.prank nsib nsib2				// controls for regressions with brothers
global controlsh  distlondon i.prank							// controls for regressions with family heads
global hechter propman100 logincome propcon propnonc relig	// county controls from Hechter
encode fbio1, gen(nfam) 									// clusters at the family level
label var cOut "W married a commoner"


* Rename political variables to use in loop
rename d_MPb_aft politaft_col2
rename d_MPf_bef politbef_col2
rename MPb_years_aft politaft_col3
rename MPf_years_bef politbef_col3
rename d_MPb_locaft politaft_col4
rename d_MPf_local politbef_col4
rename MPb_years_locaft politaft_col5
rename MPf_years_locbef politbef_col5
rename d_MPh_aft politaft_col6
rename d_MPh_bef politbef_col6
rename MPh_years_aft politaft_col7
rename MPh_years_bef politbef_col7


* Panel A: BASELINE
* -----------------

* Col. [1]: Reduced form effect

gen effect = cOut
label var effect "coef"
ivregress liml rateperpou (effect = syntheticT biorder) $hechter $controlsrf , first
	qui weakiv, null(0) small
	local CLR1 = round(e(clr_p), 0.001)
	qui reg effect syntheticT biorder $hechter $controlsrf
	local F1 = round(e(F),0.01)
	qui ivregress liml rateperpou (effect = syntheticT biorder) $hechter $controlsrf
	outreg2 using "${dirresu}\Appendix\TableB13_A", keep(effect) stats(coef) noobs nocons label dec(2) noas ///
	ctitle("tax") addtext(CLR-pval, `CLR1', F stage 1, `F1', Nb observations, `e(N)', Effect of, M common) tex replace

	
* Cols [2] to [5]

local listxvars "Mcommon MP MPyears localMP LocalMPyears HeadMP HeadMPyears"
forvalues i=2/5{
local effectof : word `i' of `listxvars'
replace effect = politaft_col`i'
global polbefore politbef_col`i'
ivregress liml rateperpou (effect = syntheticT biorder) $hechter $controlsb $polbefore , first
	qui weakiv, null(0) small
	local CLR1 = round(e(clr_p), 0.001)
	qui reg effect syntheticT biorder $hechter $controlsb $polbefore
	local F1 = round(e(F),0.01)
	qui ivregress liml rateperpou (effect = syntheticT biorder) $hechter $controlsb $polbefore
	outreg2 using "${dirresu}\Appendix\TableB13_A", keep(effect) stats(coef) noobs nocons label dec(2) noas ///
	ctitle("tax") addtext(CLR-pval, `CLR1', F stage 1, `F1', Nb observations, `e(N)', Effect of, `effectof') tex
}


* Cols [6] and [7]

forvalues i=6/7{
local effectof : word `i' of `listxvars'
replace effect = politaft_col`i'
global polbefore politbef_col`i'
ivregress liml rateperpou (effect = syntheticT biorder) $hechter $controlsh $polbefore , first
	qui weakiv, null(0) small
	local CLR1 = round(e(clr_p), 0.001)
	qui reg effect syntheticT biorder $hechter $controlsh $polbefore
	local F1 = round(e(F),0.01)
	qui ivregress liml rateperpou (effect = syntheticT biorder) $hechter $controlsh $polbefore
	outreg2 using "${dirresu}\Appendix\TableB13_A", keep(effect) stats(coef) noobs nocons label dec(2) noas ///
	ctitle("tax") addtext(CLR-pval, `CLR1', F stage 1, `F1', Nb observations, `e(N)', Effect of, `effectof') tex title("Panel A")
}


* Panel B: 50x50 MILES GRID FIXED EFFECT:
* ---------------------------------------

* Import grid cell of 50x50 miles

	/* Note: this is generated with ArcGis over the rovided map of England and 

	exported as a text file in 03_appendix\aux data appendix\grid50x50miles.txt"  */
	
preserve 
import delimited "${dirroot}\programs\03_appendix\aux data appendix\grid50x50miles.txt", clear 
label var fid_b10sea "FID identifier for family seats of women in baseline sample (ArcGIS)"
label var fid_grid50 "FID identifier for 50x50 miles grid"
save "${dirdta}\grid50x50miles.dta", replace
restore
merge 1:1 fid_b10sea using "${dirdta}\grid50x50miles.dta", nogen
* Geographical controls
gen inter = wgslat*wgslon
global geocontrols i.fid_grid wgslat wgslon inter
global hechter propman100 logincome propcon propnonc relig 
global controlsrf i.prank
global controlsb i.prank nsib nsib2
global controlsh i.prank


* Col. [1]: Reduced form effect

replace effect = cOut
label var effect "coef"
ivregress liml rateperpou (effect = syntheticT biorder) $hechter $controlsrf $geocontrols, first
	qui weakiv, null(0) small
	local CLR1 = round(e(clr_p), 0.001)
	qui reg effect syntheticT biorder $hechter $controlsrf $geocontrols
	local F1 = round(e(F),0.01)
	qui ivregress liml rateperpou (effect = syntheticT biorder) $hechter $controlsrf $geocontrols
	outreg2 using "${dirresu}\Appendix\TableB13_B", keep(effect) stats(coef) nor2 noobs nocons label dec(2) noas ///
	ctitle("tax") addtext(CLR-pval, `CLR1', F stage 1, `F1', Nb observations, `e(N)', Effect of, M common) tex replace

	
* Cols [2] to [5]

local listxvars "Mcommon MP MPyears localMP LocalMPyears HeadMP HeadMPyears"
forvalues i=2/5{
local effectof : word `i' of `listxvars'
replace effect = politaft_col`i'
global polbefore politbef_col`i'
ivregress liml rateperpou (effect = syntheticT biorder) $hechter $controlsb $polbefore $geocontrols, first
	qui weakiv, null(0) small
	local CLR1 = round(e(clr_p), 0.001)
	qui reg effect syntheticT biorder $hechter $controlsb $polbefore $geocontrols
	local F1 = round(e(F),0.01)
	qui ivregress liml rateperpou (effect = syntheticT biorder) $hechter $controlsb $polbefore $geocontrols
	outreg2 using "${dirresu}\Appendix\TableB13_B", keep(effect) stats(coef) nor2 noobs nocons label dec(2) noas ///
	ctitle("tax") addtext(CLR-pval, `CLR1', F stage 1, `F1', Nb observations, `e(N)', Effect of, `effectof') tex
}


* Cols [6] and [7]

forvalues i=6/7{
local effectof : word `i' of `listxvars'
replace effect = politaft_col`i'
global polbefore politbef_col`i'
ivregress liml rateperpou (effect = syntheticT biorder) $hechter $controlsh $polbefore $geocontrols, first
	qui weakiv, null(0) small
	local CLR1 = round(e(clr_p), 0.001)
	qui reg effect syntheticT biorder $hechter $controlsh $polbefore $geocontrols
	local F1 = round(e(F),0.01)
	qui ivregress liml rateperpou (effect = syntheticT biorder) $hechter $controlsh $polbefore $geocontrols
	outreg2 using "${dirresu}\Appendix\TableB13_B", keep(effect) stats(coef) nor2 noobs nocons label dec(2) noas ///
	ctitle("tax") addtext(CLR-pval, `CLR1', F stage 1, `F1', Nb observations, `e(N)', Effect of, `effectof') tex title("Panel A")
}


* Panel C: excluding cities
* -------------------------

* Calculate average tax rate excluding School Boards in cities:

preserve
import delimited "${dirdata}\shapefiles\IseatXsb_10mi.txt", clear // Import all family seat-School Board dyads within 10 miles.
foreach x in refno wgslat wgslon wgs1_lat wgs1_lon wgs2_lat wgs2_lon wgs3_lat wgs3_lon wgs4_lat wgs4_lon rateperpou{	
replace `x'=subinstr(`x',",",".",.) 	// Replace , for . for decimals
destring `x', replace
}
rename fid_seats_ fid_b10sea
duplicates tag refno countrysea wgslon wgslat schoolboar wgs1_lat wgs1_lon, gen(dup1)
drop if dup1 & countyn!=county
* Exclude School Boards in cities:
gen city = 0
foreach x in "Reading" "Macclesfield" "Stalybridge" "Stockport" "Carlisle" "Chesterfield" "Derby" "Exeter" "Plymouth" "Darlington" "Hartlepool" "South Shields" "Stockton-on-Tees" "Sunderland" "Bristol" "Gloucester" "Aldershot" "Portsmouth" "Southampton" "Canterbury" "Folkestone" "Gravesend" "Maidstone" "Ashton-under-Lyne" "Blackburn" "Bolton" "Burnley" "Liverpool" "Manchester" "Salford" "Oldham" "Rochdale" "Wigan" "Leicester" "London" "Great Yarmouth" "Norwich" "Northampton" "Newcastle-on-Tyne" "Tynemouth" "Nottingham" "Oxford" "Bath" "Burslem" "Burton-upon-Trent" "Hanley" "Longton" "Rowley Regis" "Tipton" "Walsall" "Wednesbury" "West Bromwich" "Wolverhampton" "Ipswich" "Brighton" "Hove" "Hastings" "Birmingham" "Coventry" "Dudley" "Worcester" "Kingston-on-Hull" "Middlesborough" "Scarborough" "Barnsley" "Batley" "Bradford" "Dewsbury" "Halifax" "Huddersfield" "Leeds" "Rotherham" "Sheffield" "Todmorden" "Wakefield"{
qui replace city = 1 if regexm(schoolboar,"`x'")
}
keep if city==0
* Collapse by refno family seat to get the average tax rate:
collapse rateperpound = rateperpou, by(refno countyn fid_b10sea)
rename rateperpound rate_exclcities
keep refno fid_b10sea rate_exclcities
label var fid_b10sea "FID identifier for family seats of women in baseline sample (ArcGIS)"
label var refno "Reference number in Hollingsworth"
label var rate_exclcities "Av tax rate % 1872-78 by School Boards <10mi from seat (Excluding SB in cities)"
save "${dirdta}\rate_exclcities.dta", replace
restore
* Merge average tax rate excluding School Boards in cities
merge 1:1 refno fid_b10sea using "${dirdta}\rate_exclcities.dta", nogen
* Globals
global controlsrf distlondon i.prank
global controlsb distlondon i.prank nsib nsib2
global controlsh distlondon i.prank
global hechter propman100 logincome propcon propnonc relig


* Col. [1]: Reduced form effect

replace effect = cOut
label var effect "coef"
ivregress liml rate_exclcities (effect = syntheticT biorder) $hechter $controlsrf, first
	qui weakiv, null(0) small
	local CLR1 = round(e(clr_p), 0.001)
	qui reg effect syntheticT biorder $hechter $controlsrf
	local F1 = round(e(F),0.01)
	qui ivregress liml rate_exclcities (effect = syntheticT biorder) $hechter $controlsrf
	outreg2 using "${dirresu}\Appendix\TableB13_C", keep(effect) stats(coef) nor2 noobs nocons label dec(2) noas ///
	ctitle("tax") addtext(CLR-pval, `CLR1', F stage 1, `F1', Nb observations, `e(N)', Effect of, M common) tex replace

* Cols [2] to [5]
local listxvars "Mcommon MP MPyears localMP LocalMPyears HeadMP HeadMPyears"
forvalues i=2/5{
local effectof : word `i' of `listxvars'
replace effect = politaft_col`i'
global polbefore politbef_col`i'
ivregress liml rate_exclcities (effect = syntheticT biorder) $hechter $controlsb $polbefore, first
	qui weakiv, null(0) small
	local CLR1 = round(e(clr_p), 0.001)
	qui reg effect syntheticT biorder $hechter $controlsb $polbefore
	local F1 = round(e(F),0.01)
	qui ivregress liml rate_exclcities (effect = syntheticT biorder) $hechter $controlsb $polbefore
	outreg2 using "${dirresu}\Appendix\TableB13_C", keep(effect) stats(coef) nor2 noobs nocons label dec(2) noas ///
	ctitle("tax") addtext(CLR-pval, `CLR1', F stage 1, `F1', Nb observations, `e(N)', Effect of, `effectof') tex
}
* Cols [6] and [7]
forvalues i=6/7{
local effectof : word `i' of `listxvars'
replace effect = politaft_col`i'
global polbefore politbef_col`i'
ivregress liml rate_exclcities (effect = syntheticT biorder) $hechter $controlsh $polbefore, first
	qui weakiv, null(0) small
	local CLR1 = round(e(clr_p), 0.001)
	qui reg effect syntheticT biorder $hechter $controlsh $polbefore
	local F1 = round(e(F),0.01)
	qui ivregress liml rate_exclcities (effect = syntheticT biorder) $hechter $controlsh $polbefore
	outreg2 using "${dirresu}\Appendix\TableB13_C", keep(effect) stats(coef) nor2 noobs nocons label dec(2) noas ///
	ctitle("tax") addtext(CLR-pval, `CLR1', F stage 1, `F1', Nb observations, `e(N)', Effect of, `effectof') tex title("Panel A")
}


* Panel D
* ----------

* Use auxiliary data with all seat - school board pairs:

import delimited "${dirroot}\programs\03_appendix\aux data appendix\sbXseats_allpairs.txt", clear 
sort fid_b10sea
geodist wgs1_lat wgs1_lon wgslat wgslon, miles gen(distSB_S)
sort fid_b10sea distSB_S

	/* Note: To calculate the weighted average tax rate, I use weights that 

	decay exponentially by the distance between each School Board and the 

	corresponding seat. Speciffcally, weights are exp(-distance/14:43), where 

	14.43 is set such that School Boards 10 miles away from a seat receive a 

	weight of 0.5.  */
	
gen prox_per = exp(-distSB_S/14.4269504089)
collapse (mean) rateperpou [iweight = prox_per], by(fid_b10sea)
rename rateperpou rateperpou_weight
label var fid_b10sea "FID identifier for family seats of women in baseline sample (ArcGIS)"
label var rateperpou_weight "Weighted average tax rate for all seat-SB pairs, weights are exp(-dist/14:43)"
save "${dirdta}\peersdau_weights.dta", replace


* Import data and add weighted average tax rate

use "${dirdata}/final-data-sec4.dta", clear
merge m:1 fid_b10sea using "${dirdta}\peersdau_weights.dta", nogen


* Control variables:

gen nsib2 = nsib^2 											// Number of siblings squared
global controlsrf distlondon i.prank						// controls for reduced-form effect
global controlsb  distlondon i.prank nsib nsib2				// controls for regressions with brothers
global controlsh  distlondon i.prank						// controls for regressions with family heads
global hechter propman100 logincome propcon propnonc relig	// county controls from Hechter
encode fbio1, gen(nfam) 									// clusters at the family level
label var cOut "W married a commoner"


* Rename political variables to use in loop

rename d_MPb_aft politaft_col2
rename d_MPf_bef politbef_col2
rename MPb_years_aft politaft_col3
rename MPf_years_bef politbef_col3
rename d_MPb_locaft politaft_col4
rename d_MPf_local politbef_col4
rename MPb_years_locaft politaft_col5
rename MPf_years_locbef politbef_col5
rename d_MPh_aft politaft_col6
rename d_MPh_bef politbef_col6
rename MPh_years_aft politaft_col7
rename MPh_years_bef politbef_col7

	
* Panel A: BASELINE
* -----------------

* Col. [1]: Reduced form effect

gen effect = cOut
label var effect "coef"
qui ivregress liml rateperpou_weight (effect = syntheticT biorder) $hechter $controlsrf , first
	qui weakiv, null(0) small
	local CLR1 = round(e(clr_p), 0.001)
	qui reg effect syntheticT biorder $hechter $controlsrf
	local F1 = round(e(F),0.01)
	qui ivregress liml rateperpou_weight (effect = syntheticT biorder) $hechter $controlsrf
	outreg2 using "${dirresu}\Appendix\TableB13_D", keep(effect) stats(coef) nor2 noobs nocons label dec(2) noas ///
	ctitle("tax") addtext(CLR-pval, `CLR1', F stage 1, `F1', Nb observations, `e(N)', Effect of, M common) tex replace

	
* Cols [2] to [5]

local listxvars "Mcommon MP MPyears localMP LocalMPyears HeadMP HeadMPyears"
forvalues i=2/5{
local effectof : word `i' of `listxvars'
replace effect = politaft_col`i'
global polbefore politbef_col`i'
qui ivregress liml rateperpou_weight (effect = syntheticT biorder) $hechter $controlsb $polbefore , first
	qui weakiv, null(0) small
	local CLR1 = round(e(clr_p), 0.001)
	qui reg effect syntheticT biorder $hechter $controlsb $polbefore
	local F1 = round(e(F),0.01)
	qui ivregress liml rateperpou_weight (effect = syntheticT biorder) $hechter $controlsb $polbefore
	outreg2 using "${dirresu}\Appendix\TableB13_D", keep(effect) stats(coef) nor2 noobs nocons label dec(2) noas ///
	ctitle("tax") addtext(CLR-pval, `CLR1', F stage 1, `F1', Nb observations, `e(N)', Effect of, `effectof') tex
}


* Cols [6] and [7]

forvalues i=6/7{
local effectof : word `i' of `listxvars'
replace effect = politaft_col`i'
global polbefore politbef_col`i'
qui ivregress liml rateperpou_weight (effect = syntheticT biorder) $hechter $controlsh $polbefore , first
	qui weakiv, null(0) small
	local CLR1 = round(e(clr_p), 0.001)
	qui reg effect syntheticT biorder $hechter $controlsh $polbefore
	local F1 = round(e(F),0.01)
	qui ivregress liml rateperpou_weight (effect = syntheticT biorder) $hechter $controlsh $polbefore
	outreg2 using "${dirresu}\Appendix\TableB13_D", keep(effect) stats(coef) nor2 noobs nocons label dec(2) noas ///
	ctitle("tax") addtext(CLR-pval, `CLR1', F stage 1, `F1', Nb observations, `e(N)', Effect of, `effectof') tex title("Panel A")
}


* erase temp files
erase "${dirdta}\rate_exclcities.dta"
erase "${dirdta}\grid50x50miles.dta"
erase "${dirdta}\peersdau_weights.dta"




* ==============================================================================
* Table B14: 
* Determinants of state education, measured with total funds raised from taxes.
* ------------------------------------------------------------------------------


* Import data and add total education funds raised from taxes

use "${dirdata}/final-data-sec4.dta", clear
merge 1:1 refno fid_b10sea using "${dirdta}/funds-from-rates.dta", nogen 

	/* Note: This file contains the total education funds raised from taxes 

	raised by the average School Board within 10 miles of each family seat.  */

gen rate_log = log(rate_pound)	// dep. var. in logs


* Control variables:

gen nsib2 = nsib^2 											// Number of siblings squared
global controlsrf distlondon i.prank						// controls for reduced-form effect
global controlsb  distlondon i.prank nsib nsib2				// controls for regressions with brothers
global controlsh  distlondon i.prank						// controls for regressions with family heads
global hechter propman100 logincome propcon propnonc relig	// county controls from Hechter
encode fbio1, gen(nfam) 									// clusters at the family level
label var cOut "W married a commoner"


* Col [1]: Reduced-form effect

* CLR p-values
qui ivregress liml rate_log (cOut = syntheticT biorder) $hechter $controlsrf , first
	qui weakiv, null(0) small
	local CLR1 = round(e(clr_p), 0.001)
* CLR p-values clustered by family
qui ivregress liml rate_log (cOut = syntheticT biorder) $hechter $controlsrf , first vce(cluster nfam)
	local Nbobs = `e(N)'	// just so that N appears after CLR p-values in table
	weakiv, null(0) small
	local CLR2 = round(e(clr_p), 0.001)
* Second-stage coefficients
qui ivregress liml rate_log (cOut = syntheticT biorder) $hechter $controlsrf , first
	outreg2 using "${dirresu}\Appendix\TableB14_A", keep(cOut) stats(coef) nocons label noobs dec(2) noas ///
	ctitle("log funds") addtext(CLR-pval, `CLR1', CLR-pval 2, `CLR2', Nb observations, `Nbobs') tex replace	
* First stage coefs in Panel B
qui reg cOut syntheticT biorder $hechter $controlsrf
	local F1 = round(e(F),0.01)
ivreg2 rate_log (cOut = syntheticT biorder) $hechter $controlsrf , first cluster(nfam) savefirst
	est restore _ivreg2_cOut
	local Nobs = `e(N)'
	local pval2 = round(2 * ttail(e(df_r), abs(_b[syntheticT]/_se[syntheticT])),0.001)
qui ivreg2 rate_log (cOut = syntheticT biorder) $hechter $controlsrf , first savefirst
	est restore _ivreg2_cOut
	outreg2 using "${dirresu}\Appendix\TableB14_B", keep(syntheticT) tex ctitle("M common") addtext(pfc, [`pval2'], Birth order, "YES", F-stat, `F1', Obs, `Nobs') label dec(3) noaster nocons nor2 stats(coef pval) noobs replace


* Col [2]: Any brother is MP after marriage

global polit d_MPb_aft
global polbefore d_MPf_bef
* CLR p-values
qui ivregress liml rate_log ($polit = syntheticT biorder) $hechter $controlsb $polbefore , first
	qui weakiv, null(0) small
	local CLR1 = round(e(clr_p), 0.001)
* CLR p-values clustered by family
qui ivregress liml rate_log ($polit = syntheticT biorder) $hechter $controlsb $polbefore , first vce(cluster nfam)
	local Nbobs = `e(N)'	// just so that N appears after CLR p-values in table
	qui weakiv, null(0) small
	local CLR2 = round(e(clr_p), 0.001)
* Second-stage coefficients
qui ivregress liml rate_log ($polit = syntheticT biorder) $hechter $controlsb $polbefore , first
	outreg2 using "${dirresu}\Appendix\TableB14_A", keep($polit) stats(coef) nocons label noobs dec(2) noas ///
	ctitle("log funds") addtext(CLR-pval, `CLR1', CLR-pval 2, `CLR2', Nb observations, `Nbobs') tex
* First stage coefs in Panel B
qui reg $polit syntheticT biorder $hechter $controlsb $polbefore
	local F1 = round(e(F),0.01)
qui ivreg2 rate_log ($polit = syntheticT biorder) $hechter $controlsb $polbefore , first cluster(nfam) savefirst
	est restore _ivreg2_d_MPb_aft
	local Nobs = `e(N)'
	local pval2 = round(2 * ttail(e(df_r), abs(_b[syntheticT]/_se[syntheticT])),0.001)
qui ivreg2 rate_log ($polit = syntheticT biorder) $hechter $controlsb $polbefore , first savefirst
	est restore _ivreg2_d_MPb_aft
	outreg2 using "${dirresu}\Appendix\TableB14_B", keep(syntheticT) tex ctitle("MP (b)") addtext(pfc, [`pval2'], Birth order, "YES", F-stat, `F1', Obs, `Nobs') label dec(3) noaster nocons nor2 stats(coef pval) noobs

	
* Col [3]: Years brother is MP after marriage

global polit MPb_years_aft
global polbefore MPf_years_bef
* CLR p-values
qui ivregress liml rate_log ($polit = syntheticT biorder) $hechter $controlsb $polbefore , first
	qui weakiv, null(0) small
	local CLR1 = round(e(clr_p), 0.001)
* CLR p-values clustered by family
qui ivregress liml rate_log ($polit = syntheticT biorder) $hechter $controlsb $polbefore , first vce(cluster nfam)
	local Nbobs = `e(N)'	// just so that N appears after CLR p-values in table
	qui weakiv, null(0) small
	local CLR2 = round(e(clr_p), 0.001)
* Second-stage coefficients
qui ivregress liml rate_log ($polit = syntheticT biorder) $hechter $controlsb $polbefore , first
	outreg2 using "${dirresu}\Appendix\TableB14_A", keep($polit) stats(coef) nocons label noobs dec(2) noas ///
	ctitle("log funds") addtext(CLR-pval, `CLR1', CLR-pval 2, `CLR2', Nb observations, `Nbobs') tex
* First stage coefs in Panel B
qui reg $polit syntheticT biorder $hechter $controlsb $polbefore
	local F1 = round(e(F),0.01)
qui ivreg2 rate_log ($polit = syntheticT biorder) $hechter $controlsb $polbefore , first cluster(nfam) savefirst
	est restore _ivreg2_MPb_years_aft
	local Nobs = `e(N)'
	local pval2 = round(2 * ttail(e(df_r), abs(_b[syntheticT]/_se[syntheticT])),0.001)
qui ivreg2 rate_log ($polit = syntheticT biorder) $hechter $controlsb $polbefore , first savefirst
	est restore _ivreg2_MPb_years_aft
	outreg2 using "${dirresu}\Appendix\TableB14_B", keep(syntheticT) tex ctitle("Years MP (b)") addtext(pfc, [`pval2'], Birth order, "YES", F-stat, `F1', Obs, `Nobs') label dec(3) noaster nocons nor2 stats(coef pval) noobs


* Col [4]: Any brother is local MP after marriage

global polit d_MPb_locaft
global polbefore d_MPf_local
label var d_MPb_locaft "Any brother is local MP after woman's marriage"
* CLR p-values
qui ivregress liml rate_log ($polit = syntheticT biorder) $hechter $controlsb $polbefore , first
	qui weakiv, null(0) small
	local CLR1 = round(e(clr_p), 0.001)
* CLR p-values clustered by family
qui ivregress liml rate_log ($polit = syntheticT biorder) $hechter $controlsb $polbefore , first vce(cluster nfam)
	local Nbobs = `e(N)'	// just so that N appears after CLR p-values in table
	qui weakiv, null(0) small
	local CLR2 = round(e(clr_p), 0.001)
* Second-stage coefficients
qui ivregress liml rate_log ($polit = syntheticT biorder) $hechter $controlsb $polbefore , first
	outreg2 using "${dirresu}\Appendix\TableB14_A", keep($polit) stats(coef) nocons label noobs dec(2) noas ///
	ctitle("log funds") addtext(CLR-pval, `CLR1', CLR-pval 2, `CLR2', Nb observations, `Nbobs') tex
* First stage coefs in Panel B
qui reg $polit syntheticT biorder $hechter $controlsb $polbefore
	local F1 = round(e(F),0.01)
qui ivreg2 rate_log ($polit = syntheticT biorder) $hechter $controlsb $polbefore , first cluster(nfam) savefirst
	est restore _ivreg2_d_MPb_locaft
	local Nobs = `e(N)'
	local pval2 = round(2 * ttail(e(df_r), abs(_b[syntheticT]/_se[syntheticT])),0.001)
qui ivreg2 rate_log ($polit = syntheticT biorder) $hechter $controlsb $polbefore , first savefirst
	est restore _ivreg2_d_MPb_locaft
	outreg2 using "${dirresu}\Appendix\TableB14_B", keep(syntheticT) tex ctitle("local MP (b)") addtext(pfc, [`pval2'], Birth order, "YES", F-stat, `F1', Obs, `Nobs') label dec(3) noaster nocons nor2 stats(coef pval) noobs


* Col [5]: Years brother is local MP after marriage

global polit MPb_years_locaft
global polbefore MPf_years_locbef
* CLR p-values
qui ivregress liml rate_log ($polit = syntheticT biorder) $hechter $controlsb $polbefore , first
	qui weakiv, null(0) small
	local CLR1 = round(e(clr_p), 0.001)
* CLR p-values clustered by family
qui ivregress liml rate_log ($polit = syntheticT biorder) $hechter $controlsb $polbefore , first vce(cluster nfam)
	local Nbobs = `e(N)'	// just so that N appears after CLR p-values in table
	qui weakiv, null(0) small
	local CLR2 = round(e(clr_p), 0.001)
* Second-stage coefficients
qui ivregress liml rate_log ($polit = syntheticT biorder) $hechter $controlsb $polbefore , first
	outreg2 using "${dirresu}\Appendix\TableB14_A", keep($polit) stats(coef) nocons label noobs dec(3) noas ///
	ctitle("log funds") addtext(CLR-pval, `CLR1', CLR-pval 2, `CLR2', Nb observations, `Nbobs') tex
* First stage coefs in Panel B
qui reg $polit syntheticT biorder $hechter $controlsb $polbefore
	local F1 = round(e(F),0.01)
qui ivreg2 rate_log ($polit = syntheticT biorder) $hechter $controlsb $polbefore , first cluster(nfam) savefirst
	est restore _ivreg2_MPb_years_locaft
	local Nobs = `e(N)'
	local pval2 = round(2 * ttail(e(df_r), abs(_b[syntheticT]/_se[syntheticT])),0.001)
qui ivreg2 rate_log ($polit = syntheticT biorder) $hechter $controlsb $polbefore , first savefirst
	est restore _ivreg2_MPb_years_locaft
	outreg2 using "${dirresu}\Appendix\TableB14_B", keep(syntheticT) tex ctitle("local years (b)") addtext(pfc, [`pval2'], Birth order, "YES", F-stat, `F1', Obs, `Nobs') label dec(3) noaster nocons nor2 stats(coef pval) noobs


* Col [6]: Family head is local MP after marriage

global polit d_MPh_aft
global polbefore d_MPh_bef
* CLR p-values
qui ivregress liml rate_log ($polit = syntheticT biorder) $hechter $controlsh $polbefore , first
	qui weakiv, null(0) small
	local CLR1 = round(e(clr_p), 0.001)
* CLR p-values clustered by family
qui ivregress liml rate_log ($polit = syntheticT biorder) $hechter $controlsh $polbefore , first vce(cluster nfam)
	local Nbobs = `e(N)'	// just so that N appears after CLR p-values in table
	qui weakiv, null(0) small
	local CLR2 = round(e(clr_p), 0.001)
* Second-stage coefficients
qui ivregress liml rate_log ($polit = syntheticT biorder) $hechter $controlsh $polbefore , first
	outreg2 using "${dirresu}\Appendix\TableB14_A", keep($polit) stats(coef) nocons label noobs dec(2) noas ///
	ctitle("log funds") addtext(CLR-pval, `CLR1', CLR-pval 2, `CLR2', Nb observations, `Nbobs') tex
* First stage coefs in Panel B
qui reg $polit syntheticT biorder $hechter $controlsh $polbefore
	local F1 = round(e(F),0.01)
qui ivreg2 rate_log ($polit = syntheticT biorder) $hechter $controlsh $polbefore , first cluster(nfam) savefirst
	est restore _ivreg2_d_MPh_aft
	local Nobs = `e(N)'
	local pval2 = round(2 * ttail(e(df_r), abs(_b[syntheticT]/_se[syntheticT])),0.001)
qui ivreg2 rate_log ($polit = syntheticT biorder) $hechter $controlsh $polbefore , first savefirst
	est restore _ivreg2_d_MPh_aft
	outreg2 using "${dirresu}\Appendix\TableB14_B", keep(syntheticT) tex ctitle("MP (h)") addtext(pfc, [`pval2'], Birth order, "YES", F-stat, `F1', Obs, `Nobs') label dec(3) noaster nocons nor2 stats(coef pval) noobs


* Col [7]: Years family head is local MP after marriage

global polit MPh_years_aft
global polbefore MPh_years_bef
* CLR p-values
qui ivregress liml rate_log ($polit = syntheticT biorder) $hechter $controlsh $polbefore , first
	qui weakiv, null(0) small
	local CLR1 = round(e(clr_p), 0.001)
* CLR p-values clustered by family
qui ivregress liml rate_log ($polit = syntheticT biorder) $hechter $controlsh $polbefore , first vce(cluster nfam)
	local Nbobs = `e(N)'	// just so that N appears after CLR p-values in table
	qui weakiv, null(0) small
	local CLR2 = round(e(clr_p), 0.001)
* Second-stage coefficients
qui ivregress liml rate_log ($polit = syntheticT biorder) $hechter $controlsh $polbefore , first
	outreg2 using "${dirresu}\Appendix\TableB14_A", keep($polit) stats(coef) nocons label noobs dec(3) noas ///
	ctitle("log funds") addtext(CLR-pval, `CLR1', CLR-pval 2, `CLR2', Nb observations, `Nbobs') tex
* First stage coefs in Panel B
qui reg $polit syntheticT biorder $hechter $controlsh $polbefore
	local F1 = round(e(F),0.01)
qui ivreg2 rate_log ($polit = syntheticT biorder) $hechter $controlsh $polbefore , first cluster(nfam) savefirst
	est restore _ivreg2_MPh_years_aft
	local Nobs = `e(N)'
	local pval2 = round(2 * ttail(e(df_r), abs(_b[syntheticT]/_se[syntheticT])),0.001)
qui ivreg2 rate_log ($polit = syntheticT biorder) $hechter $controlsh $polbefore , first savefirst
	est restore _ivreg2_MPh_years_aft
	outreg2 using "${dirresu}\Appendix\TableB14_B", keep(syntheticT) tex ctitle("Years MP (h)") addtext(pfc, [`pval2'], Birth order, "YES", F-stat, `F1', Obs, `Nobs') label dec(3) noaster nocons nor2 stats(coef pval) noobs


* erase temp files:
erase "${dirdta}/funds-from-rates.dta"




* ==============================================================================
* Figure B5: 
* Funds raised from taxes and other measures of state education.
* ------------------------------------------------------------------------------


* Import data on other education measures at the county level.

import delimited "${dirroot}\programs\03_appendix\aux data appendix\other_edu.txt", clear 

	/* Note: This file contains different measures of education for 38 

	historic counties in England (excluding the London area). Source is

	Goni (2017) "Landed elites and education provision in England. Evidence

	from School Boards, 1870-99."  */

label var county "county"
label var countyabb "county (abbreviation)"
label var ratepc_d "funds raised from taxes pence per child)"
label var expenpc_d "total expenditure in state schools (pence per child)"
label var statepriv "state to private schools (ratio)"
label var salary "teachers' salary (pence per teacher)"
label var arith "% pass in national arithmetics exam"


* Figure

pwcorr expenpc_d ratepc_d, star(0.01)
local corr1 = round(`r(rho)', 0.01)
twoway  (scatter expenpc_d ratepc_d, mlabel(countyabb) mlabp(0) msymbol(i) mlabc(black) mlabsize(vsmall)) ///
		(lfit expenpc_d ratepc_d, lwidth(thin)), ///
		graphregion(color(white)) xtitle("funds raised from taxes" "(pence per child)", size(small)) ///
		xlabel(10(30)170, labsize(vsmall)) ylabel(0(100)300, labsize(vsmall)) ytitle("total expend. in state schools" "(pence per child)", size(small)) legend(off) ///
		note("correlation = 0`corr1'***") name(pA, replace)
pwcorr statepriv ratepc_d, star(0.01)
local corr1 = round(`r(rho)', 0.01)
twoway  (scatter statepriv ratepc_d if countyabb!="CON", mlabel(countyabb) mlabp(0) msymbol(i) mlabc(black) mlabsize(vsmall)) ///
		(lfit statepriv ratepc_d, lwidth(thin)), ///
		graphregion(color(white)) xtitle("funds raised from taxes" "(pence per child)", size(small)) ///
		xlabel(10(30)170, labsize(vsmall)) ylabel(0.10(0.10)0.70, labsize(vsmall)) ytitle("state / private schools" "(ratio)", size(small)) legend(off) ///
		note("correlation = 0`corr1'***") name(pB, replace)
pwcorr salary ratepc_d, star(0.01)
local corr1 = round(`r(rho)', 0.01)
twoway  (scatter salary ratepc_d, mlabel(countyabb) mlabp(0) msymbol(i) mlabc(black) mlabsize(vsmall)) ///
		(lfit salary ratepc_d, lwidth(thin)), ///
		graphregion(color(white)) xtitle("funds raised from taxes" "(pence per child)", size(small)) ///
		xlabel(10(30)170, labsize(vsmall)) ylabel(0(2000)6000, labsize(vsmall)) ytitle("teachers' salary" "(pence per teacher)", size(small)) legend(off) ///
		note("correlation = 0`corr1'***") name(pC, replace)
pwcorr arithm ratepc_d, star(0.01)
local corr1 = round(`r(rho)', 0.01)
twoway  (scatter arithm ratepc_d, mlabel(countyabb) mlabp(0) msymbol(i) mlabc(black) mlabsize(vsmall)) ///
		(lfit arithm ratepc_d, lwidth(thin)), ///
		graphregion(color(white)) xtitle("funds raised from taxes" "(pence per child)", size(small)) ///
		xlabel(10(30)170, labsize(vsmall)) ylabel(70(5)85, labsize(vsmall)) ytitle("national arithm. exam" "(% pass)", size(small)) legend(off) ///
		note("correlation = 0`corr1'***") name(pD, replace)
graph combine pA pB pC pD, graphregion(color(white))


* Export figure

graph export "${dirresu}\Appendix\FigureB5.pdf", as(pdf) replace
graph drop _all


	
********************************************************************************
********************************************************************************
********************************************************************************
*																			   *
*	 						C. IV ESTIMATION FOR MEN					  	   *
*																			   *
********************************************************************************
********************************************************************************
********************************************************************************



* ==============================================================================
* DATA PREPARATION FOR MEN'S ANALYSIS
* ------------------------------------------------------------------------------


use "${dirdata}\final-data.dta", clear


* COHORT VARIABLES
* ----------------

* Relative number of marriageable age in one's class

foreach x in "" spo{
gen brank`x' = crank`x'
qui replace brank`x' = 6 if brank`x'==2
qui replace brank`x' = 7 if brank`x'==3
forvalues i=0/7{
gen b`i'`x' = (brank`x'==`i')
}
}
gen rel1 = .
forvalues i=1527/1945 {
foreach var of varlist b0 b1 b2 b3 b4 b5 b6 b7 {
qui summ `var' if byear>`i'-3 & byear<`i'+3 & nthismarriage<2 & dage>18
qui replace rel1 = r(mean) if byear==`i' & `var'==1 & dage>18 & nthismarriage<2
}
}
drop brank brankspo b0 b1 b2 b3 b4 b5 b6 b7 b0spo b1spo b2spo b3spo b4spo b5spo b6spo b7spo


* Sex ratio 

gen all = 1
gen mrisk = . 	// Men at risk of marriage
gen wrisk2 = .	// Women at risk of marriage using primary sex ratio (ratio of 105)
forvalues i=1527/1959{
qui summ all if nthismarr<2 & `i'-byear>=19 & `i'-byear<=24 & dyear>=`i' & wom==0 	// # boys aged 18-23 when the LS starts
scalar define a`i' = r(N)*0.952380952											  	// sex ratio conversion
qui replace wrisk2 = a`i' if myear==`i'											  	// estimated number of girls between 18-23
}
forvalues i=1527/1959{
qui summ all if nthismarr<2 & `i'-byear>=23 & `i'-byear<=28 & dyear>=`i' & wom==0 	// # boys aged 19-24 when the LS starts
qui replace mrisk = r(N) if myear==`i'
}
gen riskMin = min(wrisk, mrisk)		// cohort size (min)
gen sexr = mrisk/wrisk2				// sex ratio


* Ratio of heirs to peers' daughters

sort refno
gen id_h = 0
replace id_h = 1 if refno[_n]!=refno[_n-1]+1

	/* Note: The reference number (refno) of non-heirs is a consecutive number

	of the refno of their fathers (who are peers or peers' heirs). The reference 

	number of heirs is not a consecutive number of their fathers (it is their 

	fathers +20, +50, or +more). Here I use the consecutiveness of the reference

	number to distinguish these two groups. */

gen heirstatus = (wom==0 & id_h==1 & crank>=4)
gen heirrisk = . 							
forvalues i=1527/1959{
qui summ all if nthismarr<2 & `i'-byear>=23 & `i'-byear<=28 & dyear>=`i' & heirstatus==1	// # heirs aged 19-24 when the LS starts
qui replace heirrisk = r(N) if myear==`i'
}
gen heirratio = heirrisk/wrisk2
drop heirrisk all id_h


* Decade indicators, trend, and other Crystal Palace fair indicator (1851)

gen dec50 = (myear>1850 & myear<=1860)
gen dec60 = (myear>1860 & myear<=1870)
gen dec70 = (myear>1850 & myear<=1880)
egen t=group(myear) if myear>=1851 & myear<=1875
gen d1851 = (myear==1851)	// Crystal Palace fair (1851)


* GEOGRAPHIC ENDOGAMY VARIABLES
* -----------------------------

* spouse's seat with 100 miles of one's seat

gen disthom = (dist<=100)


* relative size of the marriageable cohort in one's division (the same first-level NUTS in England and Wales, electoral region in Scotland, or province in Ireland)

preserve
import delimited "${dirroot}\programs\03_appendix\aux data appendix\seat_division.txt", clear 
label var refno "Reference number in Hollingsworth"
label var nthismarriage "Number this Marriage in Hollingsworth dataset"
local j = 1
foreach x in "" 2 3 4{
label var division`x' "NUTS1, electoral region (SCO), or province (IRE) of family seat `j'/n"
label var division`x'spo "NUTS1, electoral region (SCO), or province (IRE) of spouse's family seat `j'/n"
local j = `j'+1
}
label var division4 "NUTS1, electoral region (SCO), or province (IRE) of family seat 4/4"
label var division5spo "NUTS1, electoral region (SCO), or province (IRE) of spouse's family seat 5/5"
label var numseat "Number of seats in entry"
save "${dirdta}\seat_division.dta", replace
restore

	/* Note: This file contains the division in which each country seat is 

	located. Divisions are first-level NUTS (if seat is in England and Wales), 

	electoral regions (if seat is in Scotland), or province (if seat is in 

	Ireland)*/

merge 1:1 refno nthismarr using "${dirdta}\seat_division.dta", nogen
foreach k in "" 2 3 4{
encode(division`k'), gen(edivision`k')
}
qui gen reldivision = 0 if numseat>0
qui gen divisiontot = 0 if numseat>0
forvalues i=1845/1880{
display `i'
foreach k in "" 2 3 4{
qui summ edivision`k' if byear>=`i'-2 & byear<=`i'+2 & numseat>0
qui replace divisiontot = r(N)+ divisiontot if byear==`i'
}
}
forvalues i=1845/1880{
display `i'
forvalues j=1/100{
foreach k in "" 2 3 4{
qui summ edivision`k' if edivision`k'==`j' & byear>=`i'-2 & byear<=`i'+2
qui replace reldivision = r(N)/divisiontot + reldivision if byear==`i' & edivision`k'==`j'
}
}
}
gen reldiv = reldivision*100


* Same division

gen divhom  = 0 if countryseat!="" & countryseatspo!=""		// Division level homogamy
foreach y in "" 2 3 4{
foreach z in "" 2 3 4 5{
replace divhom  = 1 if division`y'==division`z'spo & division`y'!=""
}
}

drop division division2 division3 division4 divisionspo division2spo division3spo division4spo division5spo numseat edivision edivision2 edivision3 edivision4 reldivision divisiontot
erase "${dirdta}\seat_division.dta"


* LANDHOLDINGS' VARIABLES
* -----------------------

* Rename acres and gross annual rents' variables
rename acrestotal ATotal
rename valuetotal VTotal
rename sacrestotal sATotal
rename svaluetotal sVTotal


* Bateman's classes (acres)

foreach x in "" s{
gen `x'landclassA = .
replace `x'landclassA = 6 if `x'ATotal>=100000 & `x'ATotal!=.
replace `x'landclassA = 5 if `x'ATotal< 100000 & `x'ATotal>=50000 & `x'ATotal!=.
replace `x'landclassA = 4 if `x'ATotal< 50000  & `x'ATotal>=20000 & `x'ATotal!=.
replace `x'landclassA = 3 if `x'ATotal< 20000  & `x'ATotal>=10000 & `x'ATotal!=.
replace `x'landclassA = 2 if `x'ATotal< 10000  & `x'ATotal>=6000  & `x'ATotal!=.
replace `x'landclassA = 1 if `x'ATotal< 6000   & `x'ATotal>=1000  & `x'ATotal!=.
replace `x'landclassA = 0 if `x'ATotal==.
}
label define bat 0 "Not GL" 1 "2,000-6,000" 2 "6,000-10,000" 3 "10,000-20,000" 4 "20,000-50,000" 5 "50,000-100,000" 6 ">100,000"
label values landclassA slandclassA


* percentiles and deciles for land rents' classes

foreach x in "" s{
xtile `x'landclassV  = `x'VTotal,  nq(10)
foreach z in A V{
xtile `x'`z'Tot  = `x'`z'Total  if myear>=1851 & myear<=1875 & nthismarr==1 & wom==0 & `x'`z'Total!=., nq(100)
}
}


* sorting by land variables

gen landhoA = .
replace landhoA = 0 if landclassA!=slandclassA & landclassA!=. & slandclassA!=.
replace landhoA = 1 if landclassA==slandclassA & landclassA!=. & slandclassA!=.

gen landhoV = (landclassV == slandclassV | landclassV == slandclassV+1 | landclassV == slandclassV-1)
replace landhoV = 0 if sVTotal==.

foreach x in A V{
gen miss`x' = abs(`x'Tot-s`x'Tot) if `x'Tot!=. & s`x'Tot!=.
}


* relative size of land class

gen relandA = .
forvalues t=1800/1875{
	qui summ sATotal if sbyear<`t'-18 & sbyear>=`t'-24 & nthismarr==1 & wom==0
	gen num_`t'A = r(N)
	forvalues i=1/6{
		qui summ sATotal if slandclassA==`i' & byear<`t'-18 & byear>=`t'-24 & nthismarr==1 & wom==0
		gen num`i'_`t'A = r(N)
		gen reland`i'_`t'A = num`i'_`t'A/num_`t'A
		replace relandA = reland`i'_`t'A if myear==`t' & landclassA==`i'
		drop num`i'_`t'A reland`i'_`t'A
	}
	drop num_`t'A
}
gen relandV = .
forvalues t=1800/1875{
	qui summ sVTotal if sbyear<`t'-18 & sbyear>=`t'-24 & nthismarr==1 & wom==0
	gen num_`t'V = r(N)
	forvalues i=1/10{
		qui summ sVTotal if slandclassV==`i' & byear<`t'-18 & byear>=`t'-24 & nthismarr==1 & wom==0 | slandclassV==`i'-1 & byear<`t'-18 & byear>=`t'-24 & nthismarr==1 & wom==0 | slandclassV==`i'+1 & byear<`t'-18 & byear>=`t'-24 & nthismarr==1 & wom==0
		gen num`i'_`t'V = r(N)
		gen q20rel`i'_`t'V = num`i'_`t'V/num_`t'V
		replace relandV = q20rel`i'_`t'V if myear==`t' & landclassV==`i'
		drop num`i'_`t'V q20rel`i'_`t'V
	}
	drop num_`t'V
}


* heirness status of wives

gen heirspo = .
replace heirspo = 1 if heir==3
replace heirspo = 2 if heir==2
replace heirspo = 3 if heir==1
replace heirspo = 4 if heir==5
replace heirspo = 5 if heir==4
label define hhh 1 "Wife not" 2 "Wife coheiress" 3 "Wife sole heiress" 4 "Husband not" 5 "Husband heir"
label values heirspo hhh



* OTHER CONTROLS
* --------------

* Railway length

	/* Note: This file contains the number of miles of railway in Britain and

	Ireland each year. The source is Mitchell (1988).  */

preserve
import delimited "${dirroot}\programs\03_appendix\aux data appendix\mitchell_railway.txt", clear 
label var myear "Year"
label var raillength "Length of railway (miles) in England and Ireland"
save "${dirdta}\mitchell_railway.dta", replace
restore 

merge m:1 myear using "${dirdta}\mitchell_railway.dta", nogen
gen railLength100 = raillength/100 	// in hundreds of mi


* SAMPLE
* ------

	/* Note: The sample for appendix C is all peers and peers' offspring 

	marrying in 1851-75. I exclude second marriages.  */

keep if myear>=1851 & myear<=1875 & mage!=. & nthismarriage<2

* labels

label var rel1   "Relative no. of own marriageble age in one's class"
label var mrisk  "Size of the marriageble cohort (men)" 
label var wrisk2 "Size of the marriageble cohort calculated using birth ratios (women)"
label var riskMin "Min size of marriageable cohort (men vs. women)"
label var sexr  "Sex ratio"
label var heirstatus "Heir to Baronet, Baron/Visc., Earl/Marquis/Duke (based on refno)"
label var heirratio "Ratio of heirs to peers' daughters"
label var dec50 "Married in 1850s"
label var dec60 "Married in 1860s" 
label var dec70 "Married in 1870s" 
label var t "Time trend (1851-75)"
label var d1851 "Married during Crystal Palace fair (1851)"					 	
label var disthom "Wife's seat < 100 miles further than husband's seat'"
label var reldiv "Relative size of marriageable cohort in one's division (the same first-level NUTS in England and Wales, electoral region in Scotland, or province in Ireland)"
label var reldiv "Relative size of marr cohort in NUTS1, electoral region (SCO), or province (IRE)"
label var divhom "Married in same NUTS1, electoral region (SCO), or province (IRE)"
label var landclassA "Acreage class (Bateman)"
label var slandclassA "Spouse's acreage class (Bateman)"
label var landclassV "Land value class (deciles)"
label var ATot "Acreage, percentile (1851-75)"
label var VTot "Gross annual rents from land, percentile (1851-75)"
label var slandclassV "Spouse's land value (Bateman)"
label var sATot "Spouses' family acreage, percentil (1851-75)"
label var sVTot "Spouses' family gross annual rents from land, percentil (1851-75)"
label var landhoA "Spouses' from same acreage class (Bateman)"
label var landhoV "Spouses' from same land value class (deciles)"
label var missA "Absolute value of the difference in spouses' acreage (percentiles)"
label var missV "Absolute value of the difference in spouses' land value (percentiles)"
label var relandA "Relative size of marriage cohort in own land class (Bateman acreage classes)"
label var relandV "Relative size of marriage cohort in own land class (land value deciles)"
label var heirspo "Wife is a heir or co-heir"
label var raillength "Railway length (mi)"
label var railLength100 "Railway length (100s mi)"
label var ATotal "Acreage"
label var VTotal "Gross annual rents from land"
label var sATotal "Spouse's family acreage"
label var sVTotal "Spouse's family gross annual rents from land"
label var woods "Estates include woods"
label var oldfamily "Family holds land since the time of Henry VIII"
label var soldfamily "Spouse's family holds land since the time of Henry VIII"
label var swoods "Spouse's family has woods"


* save final data for men

save "${dirdta}\final-data-men.dta", replace
erase "${dirdta}\mitchell_railway.dta"


* ==============================================================================
* Figure C1: 
* No selection for peers' sons.
* ------------------------------------------------------------------------------


* Import final data

use "${dirdata}\final-data.dta", clear
keep if nthismarriage<2


* Add father's reference number to identify brothers

preserve
import delimited "${dirroot}\programs\03_appendix\aux data appendix\father_son_refno.txt", clear 
label var refno "Reference number in Hollingsworth"
label var refno_p "Father's reference number in Hollingsworth"
save "${dirdta}\father_son_refno.dta", replace
restore

	/* Note: This file contains an individual's reference number (refno) and

	their fathers' reference number (refno_p) in the Hollingsworth dataset. 

	Source is Gobbi and Goni (2020) "Childless aristocrats."  */

merge 1:1 refno using "${dirdta}\father_son_refno.dta"
keep if _merge==3


* Sample of men married during, before, and after the interruption and their brothers

forvalues y=1858(3)1864{
	local j = `y'+2
	gen i`y' = (wom==0 & nthismarriage==1 & myear>=`y' & myear<=`j')
	bysort refno_p: egen f`y' = max(i`y')
}
keep if wom==0 & refno_p!=. & (f1858==1 | f1861==1 | f1864==1)


* Birth order for ALL brothers

sort refno_p byear refno
by refno_p: gen birthorder = _n		// birth order among brothers (including heirs)


* Panel A

preserve
replace birthorder = birthorder-0.2 if i1858==1	// just for visual purpuses
replace birthorder = birthorder+0.2 if i1864==1
twoway (histogram birthorder if i1864==1, width(0.2) discrete frac color(eltblue) lcolor(black) lwidth(thin) lpattern(dot)) ///
       (histogram birthorder if i1858==1, width(0.2) discrete frac color(grey*.1) lcolor(black) lwidth(thin) lpattern(dash)) ///
       (histogram birthorder if i1861==1, width(0.2) discrete frac color(black) lcolor(black) lwidth(vvvthin)), ///
	   legend(order(3 "Married during interruption (1861-63)" 2 "Married before interruption (1858-60)" 1 "Married after interruption (1864-66)" ) rows(3)) ///
	   xtitle("Birth order (among brothers)") xlabel(1(1)10) ///
	   title("Panel A", color(black)) graphregion(color(white)) name(gbirthorder, replace)
restore


* Panel B

gen rank_hus = prank
replace rank_hus = rank if prank<4
preserve
replace rank_hus = 10*rank_hus
replace rank_hus = rank_hus-1 if i1858==1	// just for visual purpuses
replace rank_hus = rank_hus+1 if i1864==1
twoway (histogram rank_hus if i1864==1, width(1) frac color(eltblue) lcolor(black) lwidth(thin) lpattern(dot)) ///
       (histogram rank_hus if i1858==1, width(1) frac color(grey*.1) lcolor(black) lwidth(thin) lpattern(dash)) ///
       (histogram rank_hus if i1861==1, width(1) frac color(black) lcolor(black) lwidth(vvvthin)), ///
	   legend(off) ///
	   xlabel(35 " " 40.5 "Baron/Viscount" 49.5 "Duke/Earl/Marquis" 55 " ") ///
	   title("Panel B", color(black)) graphregion(color(white)) xtitle(" ") name(gtitles, replace)
restore


* Combine and export

grc1leg gbirthorder gtitles, graphregion(color(white)) legendfrom(gbirthorder)
graph export "${dirresu}\Appendix\FigureC1.pdf", as(pdf) replace
graph drop _all



* ==============================================================================
* Table C1: 
* No selection for peers' sons.
* ------------------------------------------------------------------------------


* Import final data

use "${dirdata}\final-data.dta", clear
keep if nthismarriage<2


* Add father's reference number to identify brothers

merge 1:1 refno using "${dirdta}\father_son_refno.dta"
keep if _merge==3


* Sample of men married during, before, and after the interruption and their brothers

forvalues y=1858(3)1864{
	local j = `y'+2
	gen i`y' = (wom==0 & nthismarriage==1 & myear>=`y' & myear<=`j')
	bysort refno_p: egen f`y' = max(i`y')
}
keep if wom==0 & refno_p!=. & (f1858==1 | f1861==1 | f1864==1)


* Birth order for ALL brothers

sort refno_p byear refno
by refno_p: gen birthorder = _n		// birth order among brothers (including heirs)


* Age at marriage regressions

replace mage = myear-byear if mage==.
qui reg mage i.i1861 i.birthorder i.refno_p if wom==0 & f1861==1
	outreg2 using "${dirresu}\Appendix\TableC1", keep(i.i1861) nocons label dec(3) ///
	ctitle("M age") addtext(% correct, `e(r2)', Birth order indicators, YES, Family FE, YES, Cluster, no) tex replace
qui reg mage i.i1861 i.birthorder i.refno_p if wom==0 & f1861==1, cluster(refno_p)
	outreg2 using "${dirresu}\Appendix\TableC1", keep(i.i1861) nocons label dec(3) ///
	ctitle("M age") addtext(% correct, `e(r2)', Birth order indicators, YES, Family FE, YES, Cluster, by family) tex

	
* Erase temp files

erase "${dirdta}\father_son_refno.dta"



* ==============================================================================
* FIGURE C2: 
* Cohort size instrument and Season attendance.
* ------------------------------------------------------------------------------


* Import final data
use "${dirdata}\final-data.dta", clear

* Collapse attendees and cohort size by year, between 1851-75
keep if myear>=1851 & myear<=1875
collapse wrisk attend100, by(myear)

* Detrend cohort size
qui reg wrisk myear
predict wrisk_detrend, resid
reg attend100 myear
predict attend100_detrend, resid

* Jitter myear for visual purposes (shaded areas)
set obs `=_N+1'
replace myear = 1860.5 in l
set obs `=_N+1'
replace myear = 1863.5 in l
set obs `=_N+1'
replace myear = 1850.75 in l
set obs `=_N+1'
replace myear = 1851.25 in l
sort myear
gen low  = -48  if myear>1860 & myear<1864
gen high = 37 	if myear>1860 & myear<1864
gen Glow  = -48  if myear>1850 & myear<1852
gen Ghigh = 37 	if myear>1850 & myear<1852

* Figure
twoway 	(rarea low high myear, color(gs14)) ///
		(rarea Glow Ghigh myear, color(gs14)) ///
		(bar attend100_detrend myear, yaxis(1) barw(0.3) color(sand) ylabel(-40(10)40, nogrid)) ///
		(connect wrisk_detrend myear, yaxis(2) lcolor(black) lwidth(thin) msize(small) mcolor(black) msymbol(smdiamond) ylabel(-10(5)10, axis(2) nogrid)), ///
		legend(order(3 "Attendees Season (detrended)" 4 "Cohort size (detrended)")) ///
		text(40 1850.5  "Great Exhibition", place(e) size(small))	///
		text(40 1860.35  "Interruption", place(e) size(small))	///
		xlabel(1851(1)1875, angle(90)) ///
		graphregion(color(white)) ytitle("attendees", axis(1)) ytitle("cohort", axis(2)) xtitle("")
graph export "${dirresu}\Appendix\FigureC2.pdf", as(pdf) replace



* ==============================================================================
* TABLE C2: 
* Attendance to the Season and peer-commoner intermarriage, IV estimation.
* ------------------------------------------------------------------------------


* Import data for men's analysis

use "${dirdta}\final-data-men.dta", clear

* Globals
global controls1 i.crank biorder rel1 mage hengpee sexr railLength100 t dec50 dec60
global controls2 i.crank biorder rel1 mage hengpee dec50 t
global controlsMM rel1 sexr railLength100 
global controlsTIME t dec50 dec60
label var attend100 "Attendees Season"

* Cols. [1] to [3] (women)

* probit
preserve
qui probit cOut attend100 $controls1 if wom==1 & cOut!=., cluster(myear)
	keep if e(sample)==1
	qui estat classification
	local pcp1 = round(r(P_corr),1)
	qui margins, dydx(*) pred(pr) post
	outreg2 using "${dirresu}\Appendix\TableC2_A", keep(attend100) nocons label dec(3) coefastr ctitle("probit") addtext(% correctly predicted, `pcp1', Sargan test, ., ., ., Individual controls, YES, Cohort controls, YES, Decade FE, YES, Trend, YES, Sample years, 1851-75)	tex replace
* IV probit (1858-66)
qui ivregress 2sls cOut $controlsTIME (attend100 = mourn) if myear>=1858 & myear<=1866 & wom==1 & cOut!=., robust cluster(myear) first
	qui estat firststage
	matrix fist1 = r(singleresults)
	local F1 = round(fist1[1,4],1)
cmp (cOut=  attend100 $controls2) (attend100= mourn $controlsTIME) if myear>=1858 & myear<=1866 & wom==1 & cOut!=., ind(4 1) cluster(myear)
	predict p1, pr
	gen p2 = round(p1)
	gen correct = .
	replace correct = 1 if p2==cOut & p1!=.
	replace correct = 0 if p2!=cOut & p1!=.
	qui summ correct if myear>=1858 & myear<=1866 & wom==1 & cOut!=.
	local pcp1 = round(100*r(mean),1)
	drop p1 p2 correct
	qui margins, dydx(*) pred(pr) post
	outreg2 using "${dirresu}\Appendix\TableC2_A", keep(attend100) nocons label dec(3) coefastr ctitle("IV-probit") addtext(% correctly predicted, `pcp1', Sargan test, ., ., ., Individual controls, YES, Cohort controls, YES, Decade FE, YES, Trend, YES, Sample years, 1858-66) tex
cmp (cOut = attend100 $controls2) (attend100 = mourn $controlsTIME) if myear>=1858 & myear<=1866 & wom==1 & cOut!=., ind(4 1) cluster(myear)
	qui margins, dydx(*) predict(eq(#2)) post
	outreg2 using "${dirresu}\Appendix\TableC2_B", keep(mourn wrisk) nocons label dec(1) coefastr ctitle("attendees") addtext(F-stat, `F1', Cohort controls, NO, Decade FE and trend, YES, Indicator Great Exhibition, YES) tex replace
* IV probit (1851-75)
ivreg2 cOut (attend100 = wrisk d1851 mourn) $controlsTIME $controlsMM heirratio if wom==1 & cOut!=., liml robust cluster(myear) partial(t)
	local F1 = round(e(widstat),1)
	local J1 = round(e(j),0.01)
	local Jp1 = round(e(jp),0.01)
cmp (cOut=  attend100 $controls1 heirratio) (attend100= mourn wrisk d1851 $controlsTIME $controlsMM heirratio) if wom==1 & cOut!=., ind(4 1) cluster(myear)
	predict p1, pr
	gen p2 = round(p1)
	gen correct = .
	replace correct = 1 if p2==cOut & p1!=.
	replace correct = 0 if p2!=cOut & p1!=.
	qui summ correct if wom==1 & cOut!=.
	local pcp1 = round(100*r(mean),1)
	drop p1 p2 correct
	margins, dydx(*) pred(pr) post
	outreg2 using "${dirresu}\Appendix\TableC2_A", keep(attend100) nocons label dec(3) coefastr ctitle("IV-probit")  addtext(% correctly predicted, `pcp1', Sargan test, `J1', ., p=0`Jp1', Individual controls, YES, Cohort controls, YES, Decade FE, YES, Trend, YES, Sample years, 1851-75) tex
cmp (cOut=  attend100 $controls1 heirratio) (attend100 = mourn wrisk d1851 $controlsTIME $controlsMM heirratio) if wom==1 & cOut!=., ind(4 1) cluster(myear)
	qui margins, dydx(*) predict(eq(#2)) post
	outreg2 using "${dirresu}\Appendix\TableC2_B", keep(mourn wrisk) nocons label dec(1) coefastr ctitle("attendees") addtext(F-stat, `F1', Cohort controls, YES, Decade FE and trend, YES, Indicator Great Exhibition, YES) tex
restore


* Cols. [4] to [6] (men)

* probit (1851-75)
preserve
qui probit cOut attend100 $controls1 if wom==0, cluster(myear)
	keep if e(sample)==1
	qui estat classification
	local pcp1 = round(r(P_corr),1)
	qui margins, dydx(*) pred(pr) post
outreg2 using "${dirresu}\Appendix\TableC2_A", keep(attend100) nocons label dec(3) coefastr ctitle("probit") addtext(% correctly predicted, `pcp1', Sargan test, ., ., ., Individual controls, YES, Cohort controls, YES, Decade FE, YES, Trend, YES, Sample years, 1851-75) tex
* IV probit (1858-66)
qui ivregress 2sls cOut  $controlsTIME (attend100 = mourn) if myear>=1858 & myear<=1866 & wom==0 & cOut!=., robust cluster(myear) first
	qui estat firststage
	matrix fist1 = r(singleresults)
	local F1 = round(fist1[1,4],1)
cmp (cOut=  attend100 $controls2) (attend100 = mourn $controlsTIME) if myear>=1858 & myear<=1866 & wom==0 & cOut!=., ind(4 1) cluster(myear)
	predict p1, pr
	gen p2 = round(p1)
	gen correct = .
	replace correct = 1 if p2==cOut & p1!=.
	replace correct = 0 if p2!=cOut & p1!=.
	qui summ correct if myear>=1858 & myear<=1866 & wom==0 & cOut!=.
	local pcp1 = round(100*r(mean),1)
	drop p1 p2 correct
	qui margins, dydx(*) pred(pr) post
	outreg2 using "${dirresu}\Appendix\TableC2_A", keep(attend100) nocons label dec(3) coefastr ctitle("IV-probit")  addtext(% correctly predicted, `pcp1', Sargan test, ., ., ., Individual controls, YES, Cohort controls, YES, Decade FE, YES, Trend, YES, Sample years, 1858-66) tex
cmp (cOut=  attend100 $controls2) (attend100 = mourn $controlsTIME) if myear>=1858 & myear<=1866 & wom==0 & cOut!=., ind(4 1) cluster(myear)
	qui margins, dydx(*) predict(eq(#2)) post
	outreg2 using "${dirresu}\Appendix\TableC2_B", keep(mourn wrisk) nocons label dec(1) coefastr ctitle("attendees") addtext(F-stat, `F1', Cohort controls, YES, Decade FE and trend, YES, Indicator Great Exhibition, YES) tex
* IV probit (1851-75)
qui ivreg2 cOut (attend100 = wrisk d1851 mourn) $controlsTIME $controlsMM if wom==0, liml robust cluster(myear) partial(t)
	local F1 = round(e(widstat),1)
	local J1 = round(e(j),0.01)
	local Jp1 = round(e(jp),0.01)
cmp (cOut=  attend100 $controls1) (attend100 = mourn wrisk d1851 $controlsTIME $controlsMM) if wom==0 & cOut!=., ind(4 1) cluster(myear)
	predict p1, pr
	gen p2 = round(p1)
	gen correct = .
	replace correct = 1 if p2==cOut & p1!=.
	replace correct = 0 if p2!=cOut & p1!=.
	qui summ correct if wom==0 & cOut!=.
	local pcp1 = round(100*r(mean),1)
	drop p1 p2 correct
	qui margins, dydx(*) pred(pr) post
	outreg2 using "${dirresu}\Appendix\TableC2_A", keep(attend100) nocons label dec(3) coefastr ctitle("IV-probit")  addtext(% correctly predicted, `pcp1', Sargan test, `J1', ., p=0`Jp1', Individual controls, YES, Cohort controls, YES, Decade FE, YES, Trend, YES, Sample years, 1851-75) tex
cmp (cOut=  attend100 $controls1) (attend100 = mourn wrisk d1851 $controlsTIME $controlsMM) if wom==0 & cOut!=., ind(4 1) cluster(myear)
	qui margins, dydx(*) predict(eq(#2)) post
	outreg2 using "${dirresu}\Appendix\TableC2_B", keep(mourn wrisk) nocons label dec(1) coefastr ctitle("attendees") addtext(F-stat, `F1', Cohort controls, YES, Decade FE and trend, YES, Indicator Great Exhibition, YES) tex
restore



* ==============================================================================
* TABLE C3: 
* Attendance to the Season and other sorting patterns, IV estimation for men.
* ------------------------------------------------------------------------------


* Import data for men's analysis

use "${dirdta}\final-data-men.dta", clear
keep if wom==0


* Globals

global controlsA1 ATot VTot relandA mage hengpee sexr railLength100 t dec50 dec60
global controlsA2 ATot VTot relandA mage hengpee dec50 t
global controlsV1 ATot VTot relandV mage hengpee sexr railLength100 t dec50 dec60
global controlsV2 ATot VTot relandV mage hengpee dec50 t
global controlsMMa relandA sexr railLength100 
global controlsMMv relandV sexr railLength100 
global controlsTIME t dec50 dec60
label var attend100 "Attendees at the Season (100's)"


* Panel A: Sorting by acres
* -------------------------
* probit (1851-75)
qui probit landhoA attend100 $controlsA1 if landhoA!=., cluster(myear)
qui estat classification
	local pcp1 = round(r(P_corr),1)
qui margins, dydx(*) pred(pr) post
outreg2 using "${dirresu}\Appendix\TableC3_A", keep(attend100) nocons label dec(3) coefastr 			///
		ctitle("probit") addtext(% correctly predicted, `pcp1', F-stat from first stage, ., Sargan test pval, ., Individual controls, YES, Cohort controls, YES, Decade FE and trend, YES, Interruption IV, ., Cohort size IV, ., Sample years, 1851-75)	///
		tex replace
* IV probit (1858-66)
qui ivregress 2sls landhoA  $controlsTIME (attend100 = mourn) if myear>=1858 & myear<=1866 & landhoA!=., robust cluster(myear) first
	qui estat firststage
	matrix fist5 = r(singleresults)
	local F5 = round(fist5[1,4],1)
qui cmp (landhoA=  attend100 $controlsA2) (attend100 = mourn $controlsTIME) ///
	if myear>=1858 & myear<=1866 & landhoA!=., ind(4 1) cluster(myear)
predict p1, pr
	gen p2 = round(p1)
	gen correct = .
	replace correct = 1 if p2==landhoA & p1!=.
	replace correct = 0 if p2!=landhoA & p1!=.
	summ correct if myear>=1858 & myear<=1866 & landhoA!=.
	local pcp7 = round(100*r(mean),1)
	drop p1 p2 correct
qui margins, dydx(*) pred(pr) post
outreg2 using "${dirresu}\Appendix\TableC3_A", keep(attend100) nocons label dec(3) coefastr 			///
		ctitle("IV-probit") addtext(% correctly predicted, `pcp7', F-stat from first stage, `F5', Sargan test pval, ., Individual controls, YES, Cohort controls, NO, Decade FE and trend, YES, Interruption IV, YES, Cohort size IV, NO, Sample years, 1858-66) tex	
* IV probit (1851-75)
qui ivreg2 landhoA (attend100 = wrisk d1851 mourn)  $controlsTIME $controlsMMa if landhoA!=., liml robust cluster(myear) partial(t)
	local F6 = round(e(widstat),1)
	local J6 = round(e(j),0.01)
	local Jp6 = round(e(jp),0.01)
qui ivprobit landhoA $controlsA1 (attend100 = wrisk d1851 mourn) ///
	if landhoA!=., vce(cluster myear)
qui cmp (landhoA=  attend100 $controlsA1) (attend100 = wrisk d1851 mourn $controlsTIME $controlsMMa) ///
	if landhoA!=., ind(4 1) cluster(myear)
predict p1, pr
	gen p2 = round(p1)
	gen correct = .
	replace correct = 1 if p2==landhoA & p1!=.
	replace correct = 0 if p2!=landhoA & p1!=.
	qui summ correct if landhoA!=.
	local pcp8 = round(100*r(mean),1)
	drop p1 p2 correct	
qui margins, dydx(*) pred(pr) post
outreg2 using "${dirresu}\Appendix\TableC3_A", keep(attend100) nocons label dec(3) coefastr 			///
		ctitle("IV-probit") addtext(% correctly predicted, `pcp8', F-stat from first stage, `F6', Sargan test pval, `Jp6', Individual controls, YES, Cohort controls, YES, Decade FE and trend, YES, Interruption IV, YES, Cohort size IV, YES, Sample years, 1851-75) tex	
* ols (1851-75)
qui reg missA attend100 $controlsA1 if missA!=., vce(cluster myear)
outreg2 using "${dirresu}\Appendix\TableC3_A", keep(attend100) nocons label dec(3) coefastr nor2 		///
		ctitle("OLS") addtext(F-stat from first stage, ., % correctly predicted, ., Sargan test pval, ., Individual controls, YES, Cohort controls, YES, Decade FE and trend, YES, Interruption IV, ., Cohort size IV, ., Sample years, 1851-75) tex
* IV (1858-66)
qui ivregress 2sls missA (attend100 = mourn) $controlsTIME						///
	if myear>=1858 & myear<=1866 & missA!=., vce(cluster myear)
qui estat firststage
	matrix fist7 = r(singleresults)
	local F7 = round(fist7[1,4],1)
qui cmp (missA=  attend100 $controlsA2) (attend100 = mourn $controlsTIME) ///
	if myear>=1858 & myear<=1866 & missA!=., ind(1 1) cluster(myear)
qui margins, dydx(*) post
outreg2 using "${dirresu}\Appendix\TableC3_A", keep(attend100) nocons label dec(3) coefastr 		///
		ctitle("IV") addtext(% correctly predicted, ., F-stat from first stage, `F7', Sargan test pval, ., Individual controls, YES, Cohort controls, NO, Decade FE and trend, YES, Interruption IV, YES, Cohort size IV, NO, Sample years, 1858-66) tex
* IV (1851-75)
qui ivreg2 missA (attend100 = wrisk d1851 mourn) $controlsTIME $controlsMMa if missA!=., liml robust cluster(myear) partial(t)
	local F8 = round(e(widstat),1)
	local J8 = round(e(j),0.01)
	local Jp8 = round(e(jp),0.01)
qui cmp (missA=  attend100 $controlsA1) (attend100 = wrisk d1851 mourn $controlsTIME $controlsMMa) ///
	if missA!=., ind(1 1) cluster(myear)
qui margins, dydx(*) post
outreg2 using "${dirresu}\Appendix\TableC3_A", keep(attend100) nocons label dec(3) coefastr 		///
		ctitle("IV") addtext(% correctly predicted, ., F-stat from first stage, `F8', Sargan test pval, `Jp8', Individual controls, YES, Cohort controls, YES, Decade FE and trend, YES, Interruption IV, YES, Cohort size IV, YES, Sample years, 1851-75) tex

		
* Panel B: Sorting by land rents
* -------------------------
* probit (1851-75)
qui probit landhoV attend100 $controlsV1 if landhoA!=., cluster(myear)
qui estat classification
	local pcp1 = round(r(P_corr),1)
qui margins, dydx(*) pred(pr) post
outreg2 using "${dirresu}\Appendix\TableC3_B", keep(attend100) nocons label dec(3) coefastr 			///
		ctitle("probit") addtext(% correctly predicted, `pcp1', F-stat from first stage, ., Sargan test pval, ., Individual controls, YES, Cohort controls, YES, Decade FE and trend, YES, Interruption IV, ., Cohort size IV, ., Sample years, 1851-75)	///
		tex replace
* IV probit (1858-66)
qui ivregress 2sls landhoV $controlsTIME (attend100 = mourn) if myear>=1858 & myear<=1866 & landhoA!=., robust cluster(myear)
	qui estat firststage
	matrix fist9 = r(singleresults)
	local F9 = round(fist9[1,4],1)
qui cmp (landhoV=  attend100 $controlsV2) (attend100 = mourn $controlsTIME) ///
	if myear>=1858 & myear<=1866 & landhoA!=., ind(4 1) cluster(myear)
predict p1, pr
	gen p2 = round(p1)
	gen correct = .
	replace correct = 1 if p2==landhoV & p1!=.
	replace correct = 0 if p2!=landhoV & p1!=.
	qui summ correct if myear>=1858 & myear<=1866 & landhoA!=.
	local pcp9 = round(100*r(mean),1)
	drop p1 p2 correct	
qui margins, dydx(*) pred(pr) post
outreg2 using "${dirresu}\Appendix\TableC3_B", keep(attend100) nocons label dec(3) coefastr 			///
		ctitle("IV-probit") addtext(% correctly predicted, `pcp9', F-stat from first stage, `F9', Sargan test pval, ., Individual controls, YES, Cohort controls, NO, Decade FE and trend, YES, Interruption IV, YES, Cohort size IV, NO, Sample years, 1858-66) tex	
* IV probit (1851-75)
qui ivreg2 landhoV $controlsTIME $controlsMMv (attend100 = wrisk d1851 mourn) if landhoV!=., liml robust cluster(myear) partial(t)
	local F10 = round(e(widstat),1)
	local J10 = round(e(j),0.01)
	local Jp10 = round(e(jp),0.01)
qui cmp (landhoV=  attend100 $controlsV1) (attend100 = wrisk d1851 mourn $controlsTIME $controlsMMv) ///
	if landhoV!=., ind(4 1) cluster(myear)
predict p1, pr
	gen p2 = round(p1)
	gen correct = .
	replace correct = 1 if p2==landhoV & p1!=.
	replace correct = 0 if p2!=landhoV & p1!=.
	qui summ correct if landhoV!=.
	local pcp10 = round(100*r(mean),1)
	drop p1 p2 correct	
qui margins, dydx(*) pred(pr) post
outreg2 using "${dirresu}\Appendix\TableC3_B", keep(attend100) nocons label dec(3) coefastr 			///
		ctitle("IV-probit") addtext(% correctly predicted, `pcp10', F-stat from first stage, `F10', Sargan test pval, `Jp10', Individual controls, YES, Cohort controls, YES, Decade FE and trend, YES, Interruption IV, YES, Cohort size IV, YES, Sample years, 1851-75) tex
* ols (1851-75)
qui reg missV attend100 $controlsV1 if missA!=., vce(cluster myear)
outreg2 using "${dirresu}\Appendix\TableC3_B", keep(attend100) nocons label dec(3) coefastr nor2 		///
		ctitle("OLS") addtext(% correctly predicted, ., F-stat from first stage, ., Sargan test pval, ., Individual controls, YES, Cohort controls, YES, Decade FE and trend, YES, Interruption IV, ., Cohort size IV, ., Sample years, 1851-75) tex
* IV (1858-66)
qui ivregress 2sls missV (attend100 = mourn) $controlsTIME						///
	if myear>=1858 & myear<=1866 & missA!=., vce(cluster myear)
qui estat firststage
	matrix fist11 = r(singleresults)
	local F11 = round(fist11[1,4],1)
qui cmp (missV=  attend100 $controlsV2) (attend100 = mourn $controlsTIME) ///
	if myear>=1858 & myear<=1866 & missA!=., ind(1 1) cluster(myear)
qui margins, dydx(*) post
outreg2 using "${dirresu}\Appendix\TableC3_B", keep(attend100) nocons label dec(3) coefastr 		///
		ctitle("IV") addtext(% correctly predicted, ., F-stat from first stage, `F11', Sargan test pval, ., Individual controls, YES, Cohort controls, NO, Decade FE and trend, YES, Interruption IV, YES, Cohort size IV, NO, Sample years, 1858-66) tex
* IV (1851-75)
qui ivreg2 missV (attend100 = wrisk d1851 mourn) $controlsTIME $controlsMMv if missA!=., liml robust cluster(myear) partial(t)
	local F12 = round(e(widstat),1)
	local J12 = round(e(j),0.01)
	local Jp12 = round(e(jp),0.01)
qui cmp (missV=  attend100 $controlsV1) (attend100 = wrisk d1851 mourn $controlsTIME $controlsMMv) ///
	if missV!=., ind(1 1) cluster(myear)
qui margins, dydx(*) post
outreg2 using "${dirresu}\Appendix\TableC3_B", keep(attend100) nocons label dec(3) coefastr 		///
		ctitle("IV") addtext(% correctly predicted, ., F-stat from first stage, `F12', Sargan test pval, `Jp12', Individual controls, YES, Cohort controls, YES, Decade FE and trend, YES, Interruption IV, YES, Cohort size IV, YES, Sample years, 1851-75) tex

		
* Panel C: PCA
* -------------------------

* Data and variables for PCA
use "${dirdta}\final-data-men.dta", clear
keep if ATotal!=. & wom==0 // Keep men with landholdings
foreach x in "" s{
gen `x'old = 1 if `x'oldfamily=="yes"
replace `x'old = 0 if `x'oldfamily=="no"
gen `x'wood2 = 1 if `x'woods=="yes"
replace `x'wood2 = 0 if `x'woods=="no"
}
gen hengpeespo = (hpeeragespo==2)
gen hscopeespo = (hpeeragespo==3)
gen hiripeespo = (hpeeragespo==4)

* Globals
global controlsPCA1 i.crank ATot VTot wood2 old relandV mage hengpee sexr railLength100 t dec50 dec60
global controlsPCA2 i.crank ATot VTot wood2 old relandV mage hengpee t dec50 dec60
global controlsMM relandV sexr railLength100 
global controlsTIME t dec50 dec60
label var attend100 "Attendees at the Season (100's)"
global ses   crank 		ATotal  VTotal	hengpee 	hscotpee   hiripee    old  			wood2
global sespo prankspo	sATotal sVTotal	hengpeespo	hscopeespo hiripeespo sold heirspo 	swood2

* PCA
gsort crank ATotal VTotal hengpee hscotpee hiripee old wood2 -refno
qui pca $ses
qui pca $ses, mineigen(1)
qui predict ses ses2 ses3 ses4 ses5 ses6 ses7, score
gsort prankspo sATotal sVTotal hengpeespo hscopeespo hiripeespo sold heirspo swood2 -refno
qui pca $sesppo0
qui pca $sespo, mineigen(1)
qui predict sespo sespo2 sespo3 sespo4 sespo5 sespo6 sespo7, score

* PCA sorting (missmatch by SES rank, based on PCA)

	/* Note: refno included in the sorting to have stable results. */
	
gsort ses -refno
gen ranking = _n if wom==0 & ses!=.
gsort sespo -refno
gen sranking = _n if wom==0 & sespo!=.
gen pcasort = abs(ranking-sranking)

* OLS (1851-75)
qui reg pcasort attend100 $controlsPCA1  if wom==0 & pcasort!=., cluster(myear)
outreg2 using "${dirresu}\Appendix\TableC3_C", keep(attend100) nocons label dec(3) coefastr nor2	///
		ctitle("OLS") addtext(F-stat from first stage, ., Sargan test pval, ., Individual controls, YES, Cohort controls, YES, Decade FE and trend, YES, Interruption IV, ., Cohort size IV, ., Sample years, 1851-75)	///
		tex replace

* IV (1858-66)
ivregress 2sls pcasort (attend100 = mourn) $controlsTIME			///
	if myear>=1858 & myear<=1866 & wom==0 & pcasort!=., vce(cluster myear)
qui estat firststage
	matrix fist13 = r(singleresults)
	local F13 = round(fist13[1,4],1)
qui cmp (pcasort =  attend100 $controlsPCA2) (attend100 = mourn $controlsTIME) ///
	if myear>=1858 & myear<=1866 & wom==0 & pcasort!=., ind(1 1) cluster(myear)
qui margins, dydx(*) predict(eq(#1)) post
outreg2 using "${dirresu}\Appendix\TableC3_C", keep(attend100) nocons label dec(3) coefastr 		///
		ctitle("IV") addtext(F-stat from first stage, `F13', Sargan test pval, ., Individual controls, YES, Cohort controls, NO, Decade FE and trend, YES, Interruption IV, YES, Cohort size IV, NO, Sample years, 1858-66) tex

* IV (1851-75)
qui ivreg2 pcasort (attend100 = wrisk d1851 mourn) $controlsTIME $controlsMM if wom==0 & pcasort!=., liml robust cluster(myear) partial(t)
	local F14 = round(e(widstat),1)
	local J14 = round(e(j),0.01)
	local Jp14 = round(e(jp),0.01)
qui cmp (pcasort =  attend100 $controlsPCA1) (attend100 = wrisk d1851 mourn $controlsTIME $controlsMM) ///
	if wom==0 & pcasort!=., ind(1 1) cluster(myear)
qui margins, dydx(*) predict(eq(#1)) post
outreg2 using "${dirresu}\Appendix\TableC3_C", keep(attend100) nocons label dec(3) coefastr 		///
	ctitle("IV") addtext(F-stat from first stage, `F14', Sargan test pval,`Jp14', Individual controls, YES, Cohort controls, YES, Decade FE and trend, YES, Interruption IV, YES, Cohort size IV, YES, Sample years, 1851-75) tex


* Panel D: Geographic endogamy
* ----------------------------

* Data

use "${dirdta}\final-data-men.dta", clear
keep if dist!=. & wom==0 // Keep spouses with seats


* Globals and labels

global controlsG1 i.crank reldiv mage wom hengpee sexr railLength100 t dec50 dec60
global controlsG2 i.crank reldiv mage wom hengpee t dec50 dec60
global controlsTIME t dec50 dec60
global controlsMM reldiv sexr railLength100
label var attend100 "Attendees at the Season (100's)"


* Wife < 100 miles

* Probit (1851-75)
qui probit disthom attend100 $controlsG1, vce(cluster myear)
qui estat classification
	local pcp15 = round(r(P_corr),1)
qui margins, dydx(*) pred(pr) post
outreg2 using "${dirresu}\Appendix\TableC3_D", keep(attend100) nocons label dec(3) coefastr 		///
		ctitle("probit") addtext(% correctly predicted, `pcp15', F-stat from first stage, ., Sargan test pval, ., Individual controls, YES, Cohort controls, YES, Decade FE and trend, YES, Interruption IV, ., Cohort size IV, ., Sample years, 1851-75)	///
		tex replace
* IV probit (1858-66)
qui ivregress 2sls disthom $controlsTIME (attend100 = mourn) if myear>=1858 & myear<=1866, robust cluster(myear)
	qui estat firststage
	matrix fist16 = r(singleresults)
	local F16 = round(fist16[1,4],1)
qui cmp (disthom = attend100 $controlsG2) (attendLC = mourn $controlsTIME)	///
	if myear>=1858 & myear<=1866, ind(4 1) cluster(myear)
predict p1, pr
	gen p2 = round(p1)
	gen correct = .
	replace correct = 1 if p2==disthom & p1!=.
	replace correct = 0 if p2!=disthom & p1!=.
	qui summ correct if myear>=1858 & myear<=1866
	local pcp16 = round(100*r(mean),1)
	drop p1 p2 correct	
qui margins, dydx(*) pred(pr) post
outreg2 using "${dirresu}\Appendix\TableC3_D", keep(attend100) nocons label dec(3) coefastr 			///
		ctitle("IV-probit") addtext(% correctly predicted, `pcp16', F-stat from first stage, `F16', Sargan test pval, ., Individual controls, YES, Cohort controls, NO, Decade FE and trend, YES, Interruption IV, YES, Cohort size IV, no, Sample years, 1858-66) tex	
* IV probit (1851-75)
qui ivreg2 disthom $controlsTIME $controlsMM (attend100 = wrisk mourn d1851), liml robust cluster(myear) partial(t)
	local F17 = round(e(widstat),1)
	local J17 = round(e(j),0.01)
	local Jp17 = round(e(jp),0.01)
qui cmp (disthom = attend100 $controlsG1) (attendLC = wrisk mourn d1851 $controlsTIME $controlsMM)	///
	, ind(4 1) cluster(myear)
predict p1, pr
	gen p2 = round(p1)
	gen correct = .
	replace correct = 1 if p2==disthom & p1!=.
	replace correct = 0 if p2!=disthom & p1!=.
	qui summ correct
	local pcp17 = round(100*r(mean),1)
	drop p1 p2 correct	
qui margins, dydx(*) pred(pr) post
outreg2 using "${dirresu}\Appendix\TableC3_D", keep(attend100) nocons label dec(3) coefastr 			///
		ctitle("IV-probit") addtext(% correctly predicted, `pcp17', F-stat from first stage, `F17', Sargan test pval, `Jp17', Individual controls, YES, Cohort controls, NO, Decade FE and trend, YES, Interruption IV, YES, Cohort size IV, YES, Sample years, 1851-75) tex	

		
* Distance between spouses' seats

* OLS (1851-75)
qui reg dist attend100 $controlsG1, cluster(myear)
outreg2 using "${dirresu}\Appendix\TableC3_D", keep(attend100) nocons label dec(3) coefastr nor2	///
		ctitle("OLS") addtext(F-stat from first stage, ., Sargan test pval, ., Individual controls, YES, Cohort controls, YES, Decade FE and trend, YES, Interruption IV, ., Cohort size IV, ., Sample years, 1851-75) tex
* IV (1858-66)
ivregress 2sls dist (attend100 = mourn) $controlsTIME			///
	if myear>=1858 & myear<=1866, vce(cluster myear)
qui estat firststage
	matrix fist18 = r(singleresults)
	local F18 = round(fist18[1,4],1)
qui cmp (dist=attend100 $controlsG2) (attendLC= mourn $controlsTIME)	///
	if myear>=1858 & myear<=1866, ind(1 1) cluster(myear)
qui margins, dydx(*) predict(eq(#1)) post
outreg2 using "${dirresu}\Appendix\TableC3_D", keep(attend100) nocons label dec(3) coefastr 		///
		ctitle("IV") addtext(F-stat from first stage, `F18', Sargan test pval, ., Individual controls, YES, Cohort controls, NO, Decade FE and trend, YES, Interruption IV, YES, Cohort size IV, NO, Sample years, 1858-66) tex
* IV (1851-75)
qui ivreg2 dist (attend100 = wrisk d1851 mourn) $controlsTIME $controlsMM, liml robust cluster(myear) partial(t)
	local F19 = round(e(widstat),1)
	local J19 = round(e(j),0.01)
	local Jp19 = round(e(jp),0.01)
cmp (dist=attend100 $controlsG1) (attendLC= wrisk d1851 mourn $controlsTIME $controlsMM)	///
	, ind(1 1) cluster(myear)
qui margins, dydx(*) predict(eq(#1)) post
outreg2 using "${dirresu}\Appendix\TableC3_D", keep(attend100) nocons label dec(3) coefastr 		///
		ctitle("IV") addtext(F-stat from first stage, `F19', Sargan test pval, `Jp19', Individual controls, YES, Cohort controls, YES, Decade FE and trend, YES, Interruption IV, YES, Cohort size IV, YES, Sample years, 1851-75) tex



* ==============================================================================
* TABLE C4: 
* Attendance to the Season and sorting patterns, IV estimation controlling for distance to London.
* ------------------------------------------------------------------------------


* Import data for men's analysis

use "${dirdta}\final-data-men.dta", clear


* Globals

global controls1 i.crank biorder rel1 mage hengpee sexr railLength100 t dec50 dec60
global controlsMM rel1 sexr railLength100 
global controlsTIME t dec50 dec60
global controlsA1 ATot VTot relandA mage hengpee sexr railLength100 t dec50 dec60
global controlsMMa relandA sexr railLength100
global controlsPCA1 i.crank ATot VTot wood2 old relandV mage hengpee sexr railLength100 t dec50 dec60
global controlsG2 i.crank reldiv mage wom hengpee t dec50 dec60
label var attend100 "Attendees Season"


* Col. [1]: Peer-commoner intermarriage (women)

preserve
qui probit cOut attend100 $controls1 distlondon if wom==1 & cOut!=., cluster(myear)
keep if e(sample)==1
qui ivreg2 cOut (attend100 = wrisk d1851 mourn) $controlsTIME $controlsMM heirratio distlondon if wom==1 & cOut!=., liml robust cluster(myear) partial(t)
local F2 = round(e(widstat),1)
local J2 = round(e(j),0.01)
local Jp2 = round(e(jp),0.01)
qui cmp (cOut=  attend100 $controls1 heirratio distlondon) (attend100= wrisk d1851 mourn $controlsTIME $controlsMM heirratio distlondon) if wom==1 & cOut!=., ind(4 1) cluster(myear)
predict p1, pr
gen p2 = round(p1)
gen correct = .
replace correct = 1 if p2==cOut & p1!=.
replace correct = 0 if p2!=cOut & p1!=.
qui summ correct if wom==1 & cOut!=.
local pcp3 = round(100*r(mean),1)
drop p1 p2 correct
margins, dydx(*) pred(pr) post
outreg2 using "${dirresu}\Appendix\TableC4", keep(attend100 distlondon) nocons label dec(3) coefastr 			///
ctitle("cOut (wom)") addtext(F-stat from first stage, `F2', Individual controls, YES, Cohort controls, YES, Decade FE and trend, YES, Sample, women, Sample years, 1851-75, Model, IV probit) tex replace
restore

* distance to london for men

drop distlondon
gsort surname title wgslat -nthismarr // distance London
foreach x in wgslat wgslon wgslat2 wgslon2 wgslat3 wgslon3 wgslat4 wgslon4{
by surname title: gen `x'0 = `x'[1]
}
foreach i in "" 2 3 4{
geodist wgslat`i'0 wgslon`i'0 51.507222 -0.1275, miles gen(distlnd`i')
}
egen distlondon = rowmin(distlnd distlnd2 distlnd3 distlnd4)


* Col. [2]: Peer-commoner intermarriage (men)
qui probit cOut attend100 $controls1 distlondon if wom==0, cluster(myear)
keep if e(sample)==1
qui ivreg2 cOut (attend100 = wrisk d1851 mourn) $controlsTIME $controlsMM distlondon if wom==0, liml robust cluster(myear) partial(t)
	local F4 = round(e(widstat),1)
qui cmp (cOut=  attend100 $controls1 distlondon) (attend100 = wrisk d1851 mourn $controlsTIME $controlsMM distlondon) if wom==0 & cOut!=., ind(4 1) cluster(myear)
	qui margins, dydx(*) pred(pr) post
	outreg2 using "${dirresu}\Appendix\TableC4", keep(attend100 distlondon) nocons label dec(3) coefastr 			///
	ctitle("cOut (men)")	addtext(F-stat from first stage, `F4', Individual controls, YES, Cohort controls, YES, Decade FE and trend, YES, Sample, men, Sample years, 1851-75, Model, IV probit) tex

* Col. [3]: Acres homogamy
use "${dirdta}\final-data-men.dta", clear
label var attend100 "Attendees Season"
* distance to london for men
	drop distlondon
	gsort surname title wgslat -nthismarr // distance London
	foreach x in wgslat wgslon wgslat2 wgslon2 wgslat3 wgslon3 wgslat4 wgslon4{
	by surname title: gen `x'0 = `x'[1]
	}
	foreach i in "" 2 3 4{
	geodist wgslat`i'0 wgslon`i'0 51.507222 -0.1275, miles gen(distlnd`i')
	}
	egen distlondon = rowmin(distlnd distlnd2 distlnd3 distlnd4)
keep if myear>=1851 & myear<=1875 & mage!=. & nthismarriage<2 & ATotal!=. & wom==0
qui ivreg2 landhoA (attend100 = wrisk d1851 mourn) distlondon $controlsTIME $controlsMMa if landhoA!=., liml robust cluster(myear) partial(t)
	local F6 = round(e(widstat),1)
qui cmp (landhoA=  attend100 $controlsA1 distlondon) (attend100 = wrisk d1851 mourn $controlsTIME $controlsMMa distlondon) if landhoA!=., ind(4 1) cluster(myear)
	qui margins, dydx(*) pred(pr) post
	outreg2 using "${dirresu}\Appendix\TableC4", keep(attend100 distlondon) nocons label dec(3) coefastr 			///
	ctitle("acres Ho") addtext(F-stat from first stage, `F6', Individual controls, YES, Cohort controls, YES, Decade FE and trend, YES, Sample, men, Sample years, 1851-75, Model, IV probit) tex	

* Col. [4]: Acres missmatch
qui ivreg2 missA (attend100 = wrisk d1851 mourn) $controlsTIME $controlsMMa distlondon if missA!=., liml robust cluster(myear) partial(t)
	local F8 = round(e(widstat),1)
qui cmp (missA=  attend100 $controlsA1 distlondon) (attend100 = wrisk d1851 mourn $controlsTIME $controlsMMa distlondon) if missA!=., ind(1 1) cluster(myear)
	qui margins, dydx(*) post
	outreg2 using "${dirresu}\Appendix\TableC4", keep(attend100 distlondon) nocons label dec(3) coefastr 		///
	ctitle("acres miss") addtext(F-stat from first stage, `F8', Individual controls, YES, Cohort controls, YES, Decade FE and trend, YES, Sample, men, Sample years, 1851-75, Model, IV) tex
		
* Col. [5]: Land rents homogamy
qui ivreg2 landhoV $controlsTIME $controlsMMv distlondon (attend100 = wrisk d1851 mourn) if landhoV!=., liml robust cluster(myear) partial(t)
	local F10 = round(e(widstat),1)
qui cmp (landhoV=  attend100 $controlsV1 distlondon) (attend100 = wrisk d1851 mourn $controlsTIME $controlsMMv distlondon) if landhoV!=., ind(4 1) cluster(myear)
	qui margins, dydx(*) pred(pr) post
	outreg2 using "${dirresu}\Appendix\TableC4", keep(attend100 distlondon) nocons label dec(3) coefastr 			///
	ctitle("rent ho") addtext(F-stat from first stage, `F10', Individual controls, YES, Cohort controls, YES, Decade FE and trend, YES, Sample, men, Sample years, 1851-75, Model, IV probit) tex
		
* Col. [6]: Land rents missmatch
qui ivreg2 missV (attend100 = wrisk d1851 mourn) distlondon $controlsTIME $controlsMMv if missA!=., liml robust cluster(myear) partial(t)
	local F12 = round(e(widstat),1)
qui cmp (missV=  attend100 $controlsV1 distlondon) (attend100 = wrisk d1851 mourn $controlsTIME $controlsMMv distlondon) if missV!=., ind(1 1) cluster(myear)
	qui margins, dydx(*) post
	outreg2 using "${dirresu}\Appendix\TableC4", keep(attend100 distlondon) nocons label dec(3) coefastr 		///
	ctitle("rent miss") addtext(F-stat from first stage, `F12', Individual controls, YES, Cohort controls, YES, Decade FE and trend, YES, Sample, men, Sample years, 1851-75, Model, IV) tex

* Col. [7]: PCA
use "${dirdta}\final-data-men.dta", clear
label var attend100 "Attendees Season"
* distance to london for men
	drop distlondon
	gsort surname title wgslat -nthismarr // distance London
	foreach x in wgslat wgslon wgslat2 wgslon2 wgslat3 wgslon3 wgslat4 wgslon4{
	by surname title: gen `x'0 = `x'[1]
	}
	foreach i in "" 2 3 4{
	geodist wgslat`i'0 wgslon`i'0 51.507222 -0.1275, miles gen(distlnd`i')
	}
	egen distlondon = rowmin(distlnd distlnd2 distlnd3 distlnd4)
* sample
keep if ATotal!=. & wom==0
* PCA variables
foreach x in "" s{
gen `x'old = 1 if `x'oldfamily=="yes"
replace `x'old = 0 if `x'oldfamily=="no"
gen `x'wood2 = 1 if `x'woods=="yes"
replace `x'wood2 = 0 if `x'woods=="no"
}
gen hengpeespo = (hpeeragespo==2)
gen hscopeespo = (hpeeragespo==3)
gen hiripeespo = (hpeeragespo==4)
global controlsPCA1 i.crank ATot VTot wood2 old relandV mage hengpee sexr railLength100 t dec50 dec60
global controlsPCA2 i.crank ATot VTot wood2 old relandV mage hengpee t dec50 dec60
global controlsMM relandV sexr railLength100 
global controlsTIME t dec50 dec60
label var attend100 "Attendees at the Season (100's)"
global ses   crank 		ATotal  VTotal	hengpee 	hscotpee   hiripee    old  			wood2
global sespo prankspo	sATotal sVTotal	hengpeespo	hscopeespo hiripeespo sold heirspo 	swood2
gsort crank ATotal VTotal hengpee hscotpee hiripee old wood2 -refno
qui pca $ses
qui pca $ses, mineigen(1)
qui predict ses ses2 ses3 ses4 ses5 ses6 ses7, score
gsort prankspo sATotal sVTotal hengpeespo hscopeespo hiripeespo sold heirspo swood2 -refno
qui pca $sesppo0
qui pca $sespo, mineigen(1)
qui predict sespo sespo2 sespo3 sespo4 sespo5 sespo6 sespo7, score

	/* Note: refno included in the sorting to have stable results. */
	
gsort ses -refno
gen ranking = _n if wom==0 & ses!=.
gsort sespo -refno
gen sranking = _n if wom==0 & sespo!=.
gen pcasort = abs(ranking-sranking)
* regression
qui ivreg2 pcasort (attend100 = wrisk d1851 mourn) distlondon $controlsTIME $controlsMM if wom==0 & pcasort!=., liml robust cluster(myear) partial(t)
	local F14 = round(e(widstat),1)
qui cmp (pcasort =  attend100 $controlsPCA1 distlondon) (attend100 = wrisk d1851 mourn $controlsTIME $controlsMM distlondon) if wom==0 & pcasort!=., ind(1 1) cluster(myear)
	qui margins, dydx(*) predict(eq(#1)) post
	outreg2 using "${dirresu}\Appendix\TableC4", keep(attend100 distlondon) nocons label dec(3) coefastr 		///
	ctitle("PCA") addtext(F-stat from first stage, `F14', Individual controls, YES, Cohort controls, YES, Decade FE and trend, YES, Sample, men, Sample years, 1851-75, Model, IV) tex



* ==============================================================================
* FIGURE C3: 
* Increasing returns to scale, Probit estimation.
* ------------------------------------------------------------------------------


* Import data for men's analysis

use "${dirdta}\final-data-men.dta", clear

	
* Dep. Variable: Wife in same acres class

cmp (landhoA = attend100 ATot VTot relandA mage hengpee sexr railLength100 t dec50 dec60) ///
	(attend100 = riskMin d1851 mourn sexr t dec50 dec60 railLength100) if wom==0 & landhoA!=., ind(4 1) cluster(myear)
margins, dydx(attend100) predict(eq(#1) pr) at(attend100=(0(1)60)) level(90) post
marginsplot, ///
			 recast(line) plotopts(lcolor(dknavy)) recastci(rline) ciopts(lcolor(dknavy) lpattern(dash)) ///
			 title("Dep. Var.: Wife in same class (acres)", color(black) size(medlarge))		 ///
			 xtitle("Attendees at the Season", size(medlarge)) ytitle("marginal effect", size(medlarge))				 ///
			 xlabel(0 "0" 10 "1,000" 20 "2,000" 30 "3,000" 40 "4,000" 50 "5,000" 60 "6,000") yline(0)	 ///
			 graphregion(color(white)) name(panelA_IRS, replace)


* Dep. Variable: Wife in same rents class

cmp (landhoV = attend100 ATot VTot relandV mage hengpee sexr railLength100 t dec50 dec60) 	///
	(attend100 = riskMin d1851 mourn sexr t dec50 dec60 railLength100) if wom==0 & landhoV!=., ind(4 1) cluster(myear)
margins, dydx(attend100) predict(eq(#1) pr) at(attend100=(0(1)60)) level(90) post
marginsplot, ///
			 recast(line) plotopts(lcolor(dknavy)) recastci(rline) ciopts(lcolor(dknavy) lpattern(dash)) ///
			 yscale(range(0/0.012))	///
			 title("Dep. Var.: Wife in same class (land rents)", color(black) size(medlarge))		 ///
			 xtitle("Attendees at the Season", size(medlarge)) ytitle("marginal effect", size(medlarge))				 ///
			 xlabel(0 "0" 10 "1,000" 20 "2,000" 30 "3,000" 40 "4,000" 50 "5,000" 60 "6,000") yline(0)	 ///
			 graphregion(color(white)) name(panelB_IRS, replace)


* Combine and export
graph combine panelA_IRS panelB_IRS, graphregion(color(white)) altshrink
graph export "${dirresu}\Appendix\FigureC3.pdf", replace
graph drop _all



* ==============================================================================
* TABLE C5: 
* Sample stratification.
* ------------------------------------------------------------------------------


* Import data for men's analysis

use "${dirdta}\final-data-men.dta", clear


* SES ranking, using pca

foreach x in "" s{
gen `x'old = 1 if `x'oldfamily=="yes"
replace `x'old = 0 if `x'oldfamily=="no"
gen `x'wood2 = 1 if `x'woods=="yes"
replace `x'wood2 = 0 if `x'woods=="no"
}
gen hengpeespo = (hpeeragespo==2)
gen hscopeespo = (hpeeragespo==3)
gen hiripeespo = (hpeeragespo==4)
global ses   crank 		ATotal  VTotal	hengpee 	hscotpee   hiripee    old  			wood2
global sespo prankspo	sATotal sVTotal	hengpeespo	hscopeespo hiripeespo sold heirspo 	swood2
gsort $ses -refno
pca $ses if wom==0, mineigen(1)
predict ses ses2 ses3 ses4 ses5 ses6 ses7 if wom==0, score
gsort $sespo -refno
pca $sespo if wom==0, mineigen(1)
predict sespo sespo2 sespo3 sespo4 sespo5 sespo6 sespo7 if wom==0, score
gsort ses -refno
gen ranking = _n if wom==0 & ses!=.
gsort sespo -refno
gen sranking = _n if wom==0 & sespo!=.
gen pcasort = abs(ranking-sranking) if wom==0


* Results "wife in acres class" (row 1)

preserve
keep if ATotal!=. & wom==0
qui cmp (landhoA = attend100 ATot VTot relandA mage hengpee sexr railLength100 t dec50 dec60) (attend100 = riskMin d1851 mourn sexr t dec50 dec60 railLength100) if landhoA!=., ind(4 1) cluster(myear)
qui margins, dydx(*) predict(eq(#1) pr) post
	scalar define B11 = _b[attend100]
	scalar define S11 = _se[attend100]
	scalar define N11 = r(N)
qui cmp (landhoA = attend100 ATot VTot relandA mage hengpee sexr railLength100 t dec50 dec60) (attend100 = riskMin d1851 mourn sexr t dec50 dec60 railLength100) if landhoA!=. & crank>=6, ind(4 1) cluster(myear)
qui margins, dydx(*) predict(eq(#1) pr) post
	scalar define B12 = _b[attend100]
	scalar define S12 = _se[attend100]
	scalar define N12 = r(N)
qui cmp (landhoA = attend100 ATot VTot relandA mage hengpee sexr railLength100 t dec50 dec60) (attend100 = riskMin d1851 mourn sexr t dec50 dec60 railLength100) if landhoA!=. & crank<6, ind(4 1) cluster(myear)
qui margins, dydx(*) predict(eq(#1) pr) post
	scalar define B13 = _b[attend100]
	scalar define S13 = _se[attend100]
	scalar define N13 = r(N)
qui cmp (landhoA = attend100 ATot VTot relandA mage hengpee sexr railLength100 t dec50 dec60) (attend100 = riskMin d1851 mourn sexr t dec50 dec60 railLength100) if landhoA!=. & ATotal>=15270.5, ind(4 1) cluster(myear)
qui margins, dydx(*) predict(eq(#1) pr) post
	scalar define B14 = _b[attend100]
	scalar define S14 = _se[attend100]
	scalar define N14 = r(N)
qui cmp (landhoA = attend100 ATot VTot relandA mage hengpee sexr railLength100 t dec50 dec60) (attend100 = riskMin d1851 mourn sexr t dec50 dec60 railLength100) if landhoA!=. & ATotal<15270.5, ind(4 1) cluster(myear)
qui margins, dydx(*) predict(eq(#1) pr) post
	scalar define B15 = _b[attend100]
	scalar define S15 = _se[attend100]
	scalar define N15 = r(N)
qui cmp (landhoA = attend100 ATot VTot relandA mage hengpee sexr railLength100 t dec50 dec60) (attend100 = riskMin d1851 mourn sexr t dec50 dec60 railLength100) if landhoA!=. & VTotal>=15157, ind(4 1) cluster(myear)
qui margins, dydx(*) predict(eq(#1) pr) post
	scalar define B16 = _b[attend100]
	scalar define S16 = _se[attend100]
	scalar define N16 = r(N)
qui cmp (landhoA = attend100 ATot VTot relandA mage hengpee sexr railLength100 t dec50 dec60) (attend100 = riskMin d1851 mourn sexr t dec50 dec60 railLength100) if landhoA!=. & VTotal<15157, ind(4 1) cluster(myear)
qui margins, dydx(*) predict(eq(#1) pr) post
	scalar define B17 = _b[attend100]
	scalar define S17 = _se[attend100]
	scalar define N17 = r(N)
qui cmp (landhoA = attend100 ATot VTot relandA mage hengpee sexr railLength100 t dec50 dec60) (attend100 = riskMin d1851 mourn sexr t dec50 dec60 railLength100) if landhoA!=. & ses>=-.669556, ind(4 1) cluster(myear)
qui margins, dydx(*) predict(eq(#1) pr) post
	scalar define B18 = _b[attend100]
	scalar define S18 = _se[attend100]
	scalar define N18 = r(N)
qui cmp (landhoA = attend100 ATot VTot relandA mage hengpee sexr railLength100 t dec50 dec60) (attend100 = riskMin d1851 mourn sexr t dec50 dec60 railLength100) if landhoA!=. & ses<-.669556, ind(4 1) cluster(myear)
qui margins, dydx(*) predict(eq(#1) pr) post
	scalar define B19 = _b[attend100]
	scalar define S19 = _se[attend100]
	scalar define N19 = r(N)
restore


* Results "wife in rents class" (row 2)

preserve
keep if ATotal!=. & wom==0
label var attend100 "Wife in rents class"
qui cmp (landhoV = attend100 ATot VTot relandV mage hengpee sexr railLength100 t dec50 dec60) (attend100 = riskMin d1851 mourn sexr t dec50 dec60 railLength100) if landhoV!=., ind(4 1) cluster(myear)
qui margins, dydx(*) predict(eq(#1) pr) post
	scalar define B21 = _b[attend100]
	scalar define S21 = _se[attend100]
	scalar define N21 = r(N)
qui cmp (landhoV = attend100 ATot VTot relandV mage hengpee sexr railLength100 t dec50 dec60) (attend100 = riskMin d1851 mourn sexr t dec50 dec60 railLength100) if landhoV!=. & crank>=6, ind(4 1) cluster(myear)
qui margins, dydx(*) predict(eq(#1) pr) post
	scalar define B22 = _b[attend100]
	scalar define S22 = _se[attend100]
	scalar define N22 = r(N)
qui cmp (landhoV = attend100 ATot VTot relandV mage hengpee sexr railLength100 t dec50 dec60) (attend100 = riskMin d1851 mourn sexr t dec50 dec60 railLength100) if landhoV!=. & crank<6, ind(4 1) cluster(myear)
qui margins, dydx(*) predict(eq(#1) pr) post
	scalar define B23 = _b[attend100]
	scalar define S23 = _se[attend100]
	scalar define N23 = r(N)
qui cmp (landhoV = attend100 ATot VTot relandV mage hengpee sexr railLength100 t dec50 dec60) (attend100 = riskMin d1851 mourn sexr t dec50 dec60 railLength100) if landhoV!=. & ATotal>=15270.5, ind(4 1) cluster(myear)
qui margins, dydx(*) predict(eq(#1) pr) post
	scalar define B24 = _b[attend100]
	scalar define S24 = _se[attend100]
	scalar define N24 = r(N)
qui cmp (landhoV = attend100 ATot VTot relandV mage hengpee sexr railLength100 t dec50 dec60) (attend100 = riskMin d1851 mourn sexr t dec50 dec60 railLength100) if landhoV!=. & ATotal<15270.5, ind(4 1) cluster(myear)
qui margins, dydx(*) predict(eq(#1) pr) post
	scalar define B25 = _b[attend100]
	scalar define S25 = _se[attend100]
	scalar define N25 = r(N)
qui cmp (landhoV = attend100 ATot VTot relandV mage hengpee sexr railLength100 t dec50 dec60) (attend100 = riskMin d1851 mourn sexr t dec50 dec60 railLength100) if landhoV!=. & VTotal>=15157, ind(4 1) cluster(myear)
qui margins, dydx(*) predict(eq(#1) pr) post
	scalar define B26 = _b[attend100]
	scalar define S26 = _se[attend100]
	scalar define N26 = r(N)
qui cmp (landhoV = attend100 ATot VTot relandV mage hengpee sexr railLength100 t dec50 dec60) (attend100 = riskMin d1851 mourn sexr t dec50 dec60 railLength100) if landhoV!=. & VTotal<15157, ind(4 1) cluster(myear)
qui margins, dydx(*) predict(eq(#1) pr) post
	scalar define B27 = _b[attend100]
	scalar define S27 = _se[attend100]
	scalar define N27 = r(N)
qui cmp (landhoV = attend100 ATot VTot relandV mage hengpee sexr railLength100 t dec50 dec60) (attend100 = riskMin d1851 mourn sexr t dec50 dec60 railLength100) if landhoV!=. & ses>=-.669556, ind(4 1) cluster(myear)
qui margins, dydx(*) predict(eq(#1) pr) post
	scalar define B28 = _b[attend100]
	scalar define S28 = _se[attend100]
	scalar define N28 = r(N)
qui cmp (landhoV = attend100 ATot VTot relandV mage hengpee sexr railLength100 t dec50 dec60) (attend100 = riskMin d1851 mourn sexr t dec50 dec60 railLength100) if landhoV!=. & ses<-.669556, ind(4 1) cluster(myear)
qui margins, dydx(*) predict(eq(#1) pr) post
	scalar define B29 = _b[attend100]
	scalar define S29 = _se[attend100]
	scalar define N29 = r(N)
restore


* Results "missmatch in acres" (row 3)

preserve
keep if ATotal!=. & wom==0
label var attend100 "Missmatch in acres"
qui cmp (missA = attend100 ATot VTot relandA mage hengpee sexr railLength100 t dec50 dec60) (attend100 = riskMin d1851 mourn sexr t dec50 dec60 railLength100) if missA!=., ind(1 1) cluster(myear)
qui margins, dydx(*) predict(eq(#1)) post
	scalar define B31 = _b[attend100]
	scalar define S31 = _se[attend100]
	scalar define N31 = r(N)
qui cmp (missA = attend100 ATot VTot relandA mage hengpee sexr railLength100 t dec50 dec60) (attend100 = riskMin d1851 mourn sexr t dec50 dec60 railLength100) if missA!=. & crank>=6, ind(1 1) cluster(myear)
qui margins, dydx(*) predict(eq(#1)) post
	scalar define B32 = _b[attend100]
	scalar define S32 = _se[attend100]
	scalar define N32 = r(N)
qui cmp (missA = attend100 ATot VTot relandA mage hengpee sexr railLength100 t dec50 dec60) (attend100 = riskMin d1851 mourn sexr t dec50 dec60 railLength100) if missA!=. & crank<6, ind(1 1) cluster(myear)
qui margins, dydx(*) predict(eq(#1)) post
	scalar define B33 = _b[attend100]
	scalar define S33 = _se[attend100]
	scalar define N33 = r(N)
qui cmp (missA = attend100 ATot VTot relandA mage hengpee sexr railLength100 t dec50 dec60) (attend100 = riskMin d1851 mourn sexr t dec50 dec60 railLength100) if missA!=. & ATotal>=15270.5, ind(1 1) cluster(myear)
qui margins, dydx(*) predict(eq(#1)) post
	scalar define B34 = _b[attend100]
	scalar define S34 = _se[attend100]
	scalar define N34 = r(N)
qui cmp (missA = attend100 ATot VTot relandA mage hengpee sexr railLength100 t dec50 dec60) (attend100 = riskMin d1851 mourn sexr t dec50 dec60 railLength100) if missA!=. & ATotal<15270.5, ind(1 1) cluster(myear)
qui margins, dydx(*) predict(eq(#1)) post
	scalar define B35 = _b[attend100]
	scalar define S35 = _se[attend100]
	scalar define N35 = r(N)
qui cmp (missA = attend100 ATot VTot relandA mage hengpee sexr railLength100 t dec50 dec60) (attend100 = riskMin d1851 mourn sexr t dec50 dec60 railLength100) if missA!=. & VTotal>= 15157, ind(1 1) cluster(myear)
qui margins, dydx(*) predict(eq(#1)) post
	scalar define B36 = _b[attend100]
	scalar define S36 = _se[attend100]
	scalar define N36 = r(N)
qui cmp (missA = attend100 ATot VTot relandA mage hengpee sexr railLength100 t dec50 dec60) (attend100 = riskMin d1851 mourn sexr t dec50 dec60 railLength100) if missA!=. & VTotal<15157, ind(1 1) cluster(myear)
qui margins, dydx(*) predict(eq(#1)) post
	scalar define B37 = _b[attend100]
	scalar define S37 = _se[attend100]
	scalar define N37 = r(N)
qui cmp (missA = attend100 ATot VTot relandA mage hengpee sexr railLength100 t dec50 dec60) (attend100 = riskMin d1851 mourn sexr t dec50 dec60 railLength100) if missA!=. & ses>=-.669556, ind(1 1) cluster(myear)
qui margins, dydx(*) predict(eq(#1)) post
	scalar define B38 = _b[attend100]
	scalar define S38 = _se[attend100]
	scalar define N38 = r(N)
qui cmp (missA = attend100 ATot VTot relandA mage hengpee sexr railLength100 t dec50 dec60) (attend100 = riskMin d1851 mourn sexr t dec50 dec60 railLength100) if missA!=. & ses<-.669556, ind(1 1) cluster(myear)
qui margins, dydx(*) predict(eq(#1)) post
	scalar define B39 = _b[attend100]
	scalar define S39 = _se[attend100]
	scalar define N39 = r(N)
restore


* Results "missmatch in rents" (row 4)

preserve
keep if ATotal!=. & wom==0
label var attend100 "Missmatch in rents"
qui cmp (missV = attend100 ATot VTot relandV mage hengpee sexr railLength100 t dec50 dec60) (attend100 = riskMin d1851 mourn sexr t dec50 dec60 railLength100) if missV!=., ind(1 1) cluster(myear)
qui margins, dydx(*) predict(eq(#1)) post
	scalar define B41 = _b[attend100]
	scalar define S41 = _se[attend100]
	scalar define N41 = r(N)
qui cmp (missV = attend100 ATot VTot relandV mage hengpee sexr railLength100 t dec50 dec60) (attend100 = riskMin d1851 mourn sexr t dec50 dec60 railLength100) if missV!=. & crank>=6, ind(1 1) cluster(myear)
qui margins, dydx(*) predict(eq(#1)) post
	scalar define B42 = _b[attend100]
	scalar define S42 = _se[attend100]
	scalar define N42 = r(N)
qui cmp (missV = attend100 ATot VTot relandV mage hengpee sexr railLength100 t dec50 dec60) (attend100 = riskMin d1851 mourn sexr t dec50 dec60 railLength100) if missV!=. & crank<6, ind(1 1) cluster(myear)
qui margins, dydx(*) predict(eq(#1)) post
	scalar define B43 = _b[attend100]
	scalar define S43 = _se[attend100]
	scalar define N43 = r(N)
qui cmp (missV = attend100 ATot VTot relandV mage hengpee sexr railLength100 t dec50 dec60) (attend100 = riskMin d1851 mourn sexr t dec50 dec60 railLength100) if missV!=. & ATotal>= 15270.5, ind(1 1) cluster(myear)
qui margins, dydx(*) predict(eq(#1)) post
	scalar define B44 = _b[attend100]
	scalar define S44 = _se[attend100]
	scalar define N44 = r(N)
qui cmp (missV = attend100 ATot VTot relandV mage hengpee sexr railLength100 t dec50 dec60) (attend100 = riskMin d1851 mourn sexr t dec50 dec60 railLength100) if missV!=. & ATotal<15270.5, ind(1 1) cluster(myear)
qui margins, dydx(*) predict(eq(#1)) post
	scalar define B45 = _b[attend100]
	scalar define S45 = _se[attend100]
	scalar define N45 = r(N)
qui cmp (missV = attend100 ATot VTot relandV mage hengpee sexr railLength100 t dec50 dec60) (attend100 = riskMin d1851 mourn sexr t dec50 dec60 railLength100) if missV!=. & VTotal>= 15157, ind(1 1) cluster(myear)
qui margins, dydx(*) predict(eq(#1)) post
	scalar define B46 = _b[attend100]
	scalar define S46 = _se[attend100]
	scalar define N46 = r(N)
qui cmp (missV = attend100 ATot VTot relandV mage hengpee sexr railLength100 t dec50 dec60) (attend100 = riskMin d1851 mourn sexr t dec50 dec60 railLength100) if missV!=. & VTotal<15157, ind(1 1) cluster(myear)
qui margins, dydx(*) predict(eq(#1)) post
	scalar define B47 = _b[attend100]
	scalar define S47 = _se[attend100]
	scalar define N47 = r(N)
qui cmp (missV = attend100 ATot VTot relandV mage hengpee sexr railLength100 t dec50 dec60) (attend100 = riskMin d1851 mourn sexr t dec50 dec60 railLength100) if missV!=. & ses>=-.669556, ind(1 1) cluster(myear)
qui margins, dydx(*) predict(eq(#1)) post
	scalar define B48 = _b[attend100]
	scalar define S48 = _se[attend100]
	scalar define N48 = r(N)
qui cmp (missV = attend100 ATot VTot relandV mage hengpee sexr railLength100 t dec50 dec60) (attend100 = riskMin d1851 mourn sexr t dec50 dec60 railLength100) if missV!=. & ses<-.669556, ind(1 1) cluster(myear)
qui margins, dydx(*) predict(eq(#1)) post
	scalar define B49 = _b[attend100]
	scalar define S49 = _se[attend100]
	scalar define N49 = r(N)
restore


* Results "missmatch in SES" (row 5)

preserve
keep if wom==0
label var attend100 "Missmatch in SES"
qui cmp (pcasort =  attend100 i.crank ATot VTot wood2 old relandA mage hengpee sexr railLength100 t dec50 dec60) (attend100=riskMin mourn d1851 sexr railLength100 t dec50 dec60) if pcasort!=., ind(1 1) cluster(myear)
qui margins, dydx(*) predict(eq(#1)) post
	scalar define B51 = _b[attend100]
	scalar define S51 = _se[attend100]
	scalar define N51 = r(N)
qui cmp (pcasort =  attend100 i.crank ATot VTot wood2 old relandA mage hengpee sexr railLength100 t dec50 dec60) (attend100=riskMin mourn d1851 sexr railLength100 t dec50 dec60) if pcasort!=. & crank>=6, ind(1 1) cluster(myear)
qui margins, dydx(*) predict(eq(#1)) post
	scalar define B52 = _b[attend100]
	scalar define S52 = _se[attend100]
	scalar define N52 = r(N)
qui cmp (pcasort =  attend100 i.crank ATot VTot wood2 old relandA mage hengpee sexr railLength100 t dec50 dec60) (attend100=riskMin mourn d1851 sexr railLength100 t dec50 dec60) if pcasort!=. & crank<6, ind(1 1) cluster(myear)
qui margins, dydx(*) predict(eq(#1)) post
	scalar define B53 = _b[attend100]
	scalar define S53 = _se[attend100]
	scalar define N53 = r(N)
qui cmp (pcasort =  attend100 i.crank ATot VTot wood2 old relandA mage hengpee sexr railLength100 t dec50 dec60) (attend100=riskMin mourn d1851 sexr railLength100 t dec50 dec60) if pcasort!=. & ATotal>= 15270.5, ind(1 1) cluster(myear)
qui margins, dydx(*) predict(eq(#1)) post
	scalar define B54 = _b[attend100]
	scalar define S54 = _se[attend100]
	scalar define N54 = r(N)
qui cmp (pcasort =  attend100 i.crank ATot VTot wood2 old relandA mage hengpee sexr railLength100 t dec50 dec60) (attend100=riskMin mourn d1851 sexr railLength100 t dec50 dec60) if pcasort!=. & ATotal<15270.5, ind(1 1) cluster(myear)
qui margins, dydx(*) predict(eq(#1)) post
	scalar define B55 = _b[attend100]
	scalar define S55 = _se[attend100]
	scalar define N55 = r(N)
qui cmp (pcasort =  attend100 i.crank ATot VTot wood2 old relandA mage hengpee sexr railLength100 t dec50 dec60) (attend100=riskMin mourn d1851 sexr railLength100 t dec50 dec60) if pcasort!=. & VTotal>= 15157, ind(1 1) cluster(myear)
qui margins, dydx(*) predict(eq(#1)) post
	scalar define B56 = _b[attend100]
	scalar define S56 = _se[attend100]
	scalar define N56 = r(N)
qui cmp (pcasort =  attend100 i.crank ATot VTot wood2 old relandA mage hengpee sexr railLength100 t dec50 dec60) (attend100=riskMin mourn d1851 sexr railLength100 t dec50 dec60) if pcasort!=. & VTotal<15157, ind(1 1) cluster(myear)
qui margins, dydx(*) predict(eq(#1)) post
	scalar define B57 = _b[attend100]
	scalar define S57 = _se[attend100]
	scalar define N57 = r(N)
qui cmp (pcasort =  attend100 i.crank ATot VTot wood2 old relandA mage hengpee sexr railLength100 t dec50 dec60) (attend100=riskMin mourn d1851 sexr railLength100 t dec50 dec60) if pcasort!=. & pcasort!=. & ses>=-.669556, ind(1 1) cluster(myear)
qui margins, dydx(*) predict(eq(#1)) post
	scalar define B58 = _b[attend100]
	scalar define S58 = _se[attend100]
	scalar define N58 = r(N)
qui cmp (pcasort =  attend100 i.crank ATot VTot wood2 old relandA mage hengpee sexr railLength100 t dec50 dec60) (attend100=riskMin mourn d1851 sexr railLength100 t dec50 dec60) if pcasort!=. & ses<-.669556, ind(1 1) cluster(myear)
qui margins, dydx(*) predict(eq(#1)) post
	scalar define B59 = _b[attend100]
	scalar define S59 = _se[attend100]
	scalar define N59 = r(N)
restore


* Results "distance btw seats" (row 6)

preserve
keep if dist!=. & wom==0
label var attend100 "Distance between spouses' seats"
qui cmp (dist=attend100 i.crank reldiv mage wom hengpee sexr railLength100 t dec50 dec60) (attendLC=riskMin mourn d1851 sexr t dec50 dec60 railLength100), ind(1 1) cluster(myear)
qui margins, dydx(*) predict(eq(#1)) post
	scalar define B61 = _b[attend100]
	scalar define S61 = _se[attend100]
	scalar define N61 = r(N)
qui cmp (dist=attend100 i.crank reldiv mage wom hengpee sexr railLength100 t dec50 dec60) (attendLC=riskMin mourn d1851 sexr t dec50 dec60 railLength100) if crank>=6, ind(1 1) cluster(myear)
qui margins, dydx(*) predict(eq(#1)) post
	scalar define B62 = _b[attend100]
	scalar define S62 = _se[attend100]
	scalar define N62 = r(N)
qui cmp (dist=attend100 i.crank reldiv mage wom hengpee sexr railLength100 t dec50 dec60) (attendLC=riskMin mourn d1851 sexr t dec50 dec60 railLength100) if crank<6, ind(1 1) cluster(myear)
qui margins, dydx(*) predict(eq(#1)) post
	scalar define B63 = _b[attend100]
	scalar define S63 = _se[attend100]
	scalar define N63 = r(N)
qui cmp (dist=attend100 i.crank reldiv mage wom hengpee sexr railLength100 t dec50 dec60) (attendLC=riskMin mourn d1851 sexr t dec50 dec60 railLength100) if ATotal>= 15270.5, ind(1 1) cluster(myear)
qui margins, dydx(*) predict(eq(#1)) post
	scalar define B64 = _b[attend100]
	scalar define S64 = _se[attend100]
	scalar define N64 = r(N)
qui cmp (dist=attend100 i.crank reldiv mage wom hengpee sexr railLength100 t dec50 dec60) (attendLC=riskMin mourn d1851 sexr t dec50 dec60 railLength100) if ATotal<15270.5, ind(1 1) cluster(myear)
qui margins, dydx(*) predict(eq(#1)) post
	scalar define B65 = _b[attend100]
	scalar define S65 = _se[attend100]
	scalar define N65 = r(N)
qui cmp (dist=attend100 i.crank reldiv mage wom hengpee sexr railLength100 t dec50 dec60) (attendLC=riskMin mourn d1851 sexr t dec50 dec60 railLength100) if VTotal>= 15157, ind(1 1) cluster(myear)
qui margins, dydx(*) predict(eq(#1)) post
	scalar define B66 = _b[attend100]
	scalar define S66 = _se[attend100]
	scalar define N66 = r(N)
qui cmp (dist=attend100 i.crank reldiv mage wom hengpee sexr railLength100 t dec50 dec60) (attendLC=riskMin mourn d1851 sexr t dec50 dec60 railLength100) if VTotal<15157, ind(1 1) cluster(myear)
qui margins, dydx(*) predict(eq(#1)) post
	scalar define B67 = _b[attend100]
	scalar define S67 = _se[attend100]
	scalar define N67 = r(N)
qui cmp (dist=attend100 i.crank reldiv mage wom hengpee sexr railLength100 t dec50 dec60) (attendLC=riskMin mourn d1851 sexr t dec50 dec60 railLength100) if ses>= -.669556, ind(1 1) cluster(myear)
qui margins, dydx(*) predict(eq(#1)) post
	scalar define B68 = _b[attend100]
	scalar define S68 = _se[attend100]
	scalar define N68 = r(N)
qui cmp (dist=attend100 i.crank reldiv mage wom hengpee sexr railLength100 t dec50 dec60) (attendLC=riskMin mourn d1851 sexr t dec50 dec60 railLength100) if ses< -.669556, ind(1 1) cluster(myear)
qui margins, dydx(*) predict(eq(#1)) post
	scalar define B69 = _b[attend100]
	scalar define S69 = _se[attend100]
	scalar define N69 = r(N)
restore


* Table:

matrix strat = (B11, B12, B13, B14, B15, B16, B17, B18, B19\					///
				S11, S12, S13, S14, S15, S16, S17, S18, S19\					///
				N11, N12, N13, N14, N15, N16, N17, N18, N19\					///				
				B21, B22, B23, B24, B25, B26, B27, B28, B29\					///
				S21, S22, S23, S24, S25, S26, S27, S28, S29\					///
				N21, N22, N23, N24, N25, N26, N27, N28, N29\					///				
				B31, B32, B33, B34, B35, B36, B37, B38, B39\					///
				S31, S32, S33, S34, S35, S36, S37, S38, S39\					///
				N31, N32, N33, N34, N35, N36, N37, N38, N39\					///				
				B41, B42, B43, B44, B45, B46, B47, B48, B49\					///
				S41, S42, S43, S44, S45, S46, S47, S48, S49\					///
				N41, N42, N43, N44, N45, N46, N47, N48, N49\					///				
				B51, B52, B53, B54, B55, B56, B57, B58, B59\					///
				S51, S52, S53, S54, S55, S56, S57, S58, S59\					///
				N51, N52, N53, N54, N55, N56, N57, N58, N59\					///				
				B61, B62, B63, B64, B65, B66, B67, B68, B69\					///
				S61, S62, S63, S64, S65, S66, S67, S68, S69\					///
				N61, N62, N63, N64, N65, N66, N67, N68, N69					///				
				)
	

mat colnames strat = "Baseline" "Heir" "No-heir" "Acres above med" "Acres below med" "Rents above med" "Rents below med" "PCA above med" "PCA below med"
mat rownames strat = "Wife in acres class" "se" "N" "Wife in rents class" "se" "N" "Missmatch in acres" "se" "N" "Missmatch in rents" "se" "N" "Missmatch in SES" "se" "N" "Distance btw seats" "se" "N" 

esttab matrix(strat, fmt(3)) using "${dirresu}\Appendix\TableC5.tex", ///
	mlabel("Table C5") replace


* ==============================================================================
* TABLE C6: 
* The Season and the quality of the match (1851-75).
* ------------------------------------------------------------------------------


* Import data

use "${dirdta}\final-data-men.dta", clear
label var attend100 "Attendees Season"


* Code number of births variable as a numeric variable

foreach x in livethismarr{
replace `x' = "." if `x'=="yy"
replace `x' = "0" if `x'=="xx"
destring `x', replace
}


* Total number of births (cols. [1] and [2])

qui reg  livethismarr attend100 i.crank biorder mage hengpee sexr railLength100 t dec50 dec60 if wom==1 & mage<=30 & livethismarr>1 & crank!=1, cluster(myear)
outreg2 using "${dirresu}\Appendix\TableC6", keep(attend100) nocons label dec(3) coefastr 			///
		ctitle("OLS(tot)")				///
		addtext(Sargan test, ., ., ., Individual controls, YES, Marriage market co., YES, Decade FE, YES, Year trend, YES, Interruption IV, .,Cohort size IV, .,F-stat from first stage, .)	///
		tex replace
preserve 
collapse attend100 riskMin d1851 mourn sexr t dec50 dec60 railLength100, by(myear)
reg attend100 riskMin d1851 mourn sexr t dec50 dec60 railLength100
local F1 = round(`e(F)',0.1)
restore 
qui ivregress 2sls livethismarr (attend100 = riskMin d1851 mourn sexr t dec50 dec60 railLength100) i.crank biorder mage hengpee sexr railLength100 t dec50 dec60 if wom==1 & mage<=30 & livethismarr>1 & crank!=1
estat overid
local S1 = round(`r(sargan)',0.01)
local P1 = round(`r(p_sargan)',0.1)
cmp (livethismarr = attend100 i.crank biorder mage hengpee sexr railLength100 t dec50 dec60) (attend100=riskMin d1851 mourn sexr t dec50 dec60 railLength100) if wom==1 & mage<=30 & livethismarr>1 & crank!=1, ind(1 1) cluster(myear)
qui margins, dydx(*) predict(eq(#1)) post
outreg2 using "${dirresu}\Appendix\TableC6", keep(attend100) nocons label dec(3) coefastr 			///
		ctitle("IV(tot)")													///
		addtext(Sargan test, `S1', ., p=0`P1', Individual controls, YES, Marriage market co., YES, Decade FE, YES, Year trend, YES, Interruption IV, YES, Cohort size IV, YES,F-stat from first stage, `F1')	///
		tex

		
* Births over fertile marriage lifetime (cols. [3] and [4])

gen fert_years = 40-mage							// fertile years after marriage
gen wage0 = sddate-bdate
gen wage = floor(wage0/365)
drop wage0
replace wage = sdyear - byear if sddate==. | bdate==.
egen dagemin = rowmin(dage wage)
replace fert_years = dagemin-mage if dage<40		// fertile years married (i.e., considering widowhood)
gen births_fert = livethismarr/fert_years			// Births / fertile marr. lifetime
replace births_fert = . if fert_years<=0
*
qui reg  births_fert   attend100 i.crank biorder mage hengpee sexr railLength100 t dec50 dec60 if wom==1 & mage<=30 & livethismarr>1 & crank!=1, cluster(myear)
outreg2 using "${dirresu}\Appendix\TableC6", keep(attend100) nocons label dec(3) coefastr 			///
		ctitle("OLS(p.year)")												///
		addtext(Sargan test, ., ., ., Individual controls, YES, Marriage market co., YES, Decade FE, YES, Year trend, YES, Interruption IV, ., Cohort size IV, .,F-stat from first stage, .)	///
		tex
preserve 
collapse attend100 riskMin d1851 mourn sexr t dec50 dec60 railLength100, by(myear)
reg attend100 riskMin d1851 mourn sexr t dec50 dec60 railLength100
local F1 = round(`e(F)',0.1)
restore 
qui ivregress 2sls births_fert (attend100 = riskMin d1851 mourn sexr t dec50 dec60 railLength100) i.crank biorder mage hengpee sexr railLength100 t dec50 dec60 if wom==1 & mage<=30 & livethismarr>1 & crank!=1
estat overid
local S1 = round(`r(sargan)',0.01)
local P1 = round(`r(p_sargan)',0.1)
cmp (births_fert = attend100 i.crank biorder mage hengpee sexr railLength100 t dec50 dec60) (attend100=riskMin d1851 mourn sexr t dec50 dec60 railLength100) if wom==1 & mage<=30 & livethismarr>1 & crank!=1, ind(1 1) cluster(myear)
qui margins, dydx(*) predict(eq(#1)) post
outreg2 using "${dirresu}\Appendix\TableC6", keep(attend100) nocons label dec(3) coefastr 			///
		ctitle("IV(p.year)")												///
		addtext(Sargan test, `S1', ., p=0`P1', Individual controls, YES, Marriage market co., YES, Decade FE, YES, Year trend, YES, Interruption IV, YES, Cohort size IV, YES,F-stat from first stage, `F1')	///
		tex
		

* Births relative to hutterites (cols. [5] and [6])

label define bmonth1 1 "x" 2 "y" 3 "0" 4 "1" 5 "2" 6 "3" 7 "4" 8 "5" 9 "6" 10 "7" 11 "8" 12 "9"
encode bmonth, generate(bmonth1)
gen a10date = mdy(bmonth1, bday, byear+15)			// years married at each age group (20-24; 25-29; 30-34; 35-39; 40-44)
forvalues t=15(5)50{
gen a`t'date = mdy(bmonth1, bday, byear+`t'+5)
format a`t'date %td
gen yb`t' = `t'+5-mage if mage<`t'+5 & mage>=`t' & sddate>a`t'date
replace yb`t' = 0 if mage>=`t'+5 & sddate>a`t'date
replace yb`t' = 5 if mage<`t' & sddate>a`t'date
replace yb`t' = sdyear-mage if mage<`t'+5 & mage>=`t' & sddate<a`t'date & sddate>a`t-5'date
gen a`t'years0 = sddate-a`t'date
gen a`t'years = floor(a`t'years0/365)
replace yb`t' = 5 - a`t'years if mage<`t' & sddate<a`t'date & sddate>a`t-5'date
replace yb`t' = . if wage>=dage & ddate<a`t'date & ddate<a`t-5'date
gen a`t'years02 = ddate-a`t'date
gen a`t'years2 = floor(a`t'years02/365)
replace yb`t' = 5 - a`t'years2 if wage>=dage & ddate<a`t'date & ddate>a`t-5'date
}
gen huttyb20 = yb20*0.55							// Calculate the number of births a Hutterite couple would have had at each age group
gen huttyb25 = yb25*0.502
gen huttyb30 = yb30*0.447
gen huttyb35 = yb35*0.406
gen huttyb40 = yb40*0.222
forvalues t=20(5)40{
replace huttyb`t' = 0 if yb`t'==.
replace yb`t' = 0 if yb`t'==.
}
gen hutt = huttyb20 + huttyb25 + huttyb30 + huttyb35 + huttyb40		// Total number of births a Hutterite couple would have had
gen births_hutt = livethismarr/hutt									// Births relative to a Hutterite couple
replace births_hutt = . if hutt==0
label var births_hutt "Number of births rel. to Hutterities"
*
qui reg births_hutt    attend100 i.crank biorder mage hengpee sexr railLength100 t dec50 dec60 if wom==1 & mage<=30 & livethismarr>1 & crank!=1 & hutt>1, cluster(myear)
outreg2 using "${dirresu}\Appendix\TableC6", keep(attend100) nocons label dec(3) coefastr 			///
		ctitle("OLS(Hutt.)")												///
		addtext(Sargan test, ., ., ., Individual controls, YES, Marriage market co., YES, Decade FE, YES, Year trend, YES, Interruption IV, ., Cohort size IV, .,F-stat from first stage, .)	///
		tex
preserve 
collapse attend100 riskMin d1851 mourn sexr t dec50 dec60 railLength100, by(myear)
reg attend100 riskMin d1851 mourn sexr t dec50 dec60 railLength100
local F1 = round(`e(F)',0.1)
restore 
qui ivregress 2sls births_hutt (attend100 = riskMin d1851 mourn sexr t dec50 dec60 railLength100) i.crank biorder mage hengpee sexr railLength100 t dec50 dec60 if wom==1 & mage<=30 & livethismarr>1 & crank!=1 & hutt>1
estat overid
local S1 = round(`r(sargan)',0.01)
local P1 = round(`r(p_sargan)',0.1)
cmp (births_hutt = attend100 i.crank biorder mage hengpee sexr railLength100 t dec50 dec60) (attend100=riskMin d1851 mourn sexr t dec50 dec60 railLength100) if wom==1 & mage<=30 & livethismarr>1 & crank!=1 & hutt>1, ind(1 1) cluster(myear)
qui margins, dydx(*) predict(eq(#1)) post
outreg2 using "${dirresu}\Appendix\TableC6", keep(attend100) nocons label dec(3) coefastr 			///
		ctitle("IV(Hutt.)") title("Dep. Variable: Number of births")												///
		addtext(Sargan test, `S1', ., p=0`P1', Individual controls, YES, Marriage market co., YES, Decade FE, YES, Year trend, YES, Interruption IV, YES, Cohort size IV, YES,F-stat from first stage, `F1')	///
		tex



* ==============================================================================
* TABLE C7: 
* Marriage and political preferences (1817-75).
* ------------------------------------------------------------------------------


* Import data

use "${dirdata}\final-data.dta", clear
keep if myear<=1875
keep if nthismarriage<2


* Political clubs

	/* Note:  This is based on the clubs reported in Bateman's data. Specifically, 

	Brook's, Reform, and Devonshire are liberal Clubs; Carlton, Junior Carlton, 

	Conservative, or St. Stephen's are conservative Clubs (Bateman 1883: 497).  */

* Club names housekeeping
replace club2 = "St. James'" 		if club2=="St. Jas."
replace club3 = "St. James'" 		if club3=="St. Jas."
replace club3 = "St. Stephen's" 	if club3=="Stephen's"
replace club4 = "Beaconsfield" 		if club4=="Beacsfd."
replace club7 = "United Service" 	if club7=="U. Serv."

replace sclub = "Carlton" 			if sclub=="Carlton'"
replace sclub = "Marlborough"		if sclub=="Marlborough'"
replace sclub2 = "Army and Navy'"	if sclub2=="Army and Navy'"
replace sclub2 = "Brooks's"			if sclub2=="Brooks'"
replace sclub2 = "Conservative"		if sclub2=="Conservative'"
replace sclub2 = "Naval and Military" if sclub2=="N. And M."
replace sclub2 = "St. James'"		if sclub2=="St. Jas."
replace sclub2 = "White's"			if sclub2=="White's'"
replace sclub3 = "Dev."				if sclub3=="Devonshire"
replace sclub3 = "Guards"			if sclub3=="Guard"
replace sclub3 = "St. James's"		if sclub3=="St. Jas."
replace sclub3 = "Turf"				if sclub3=="Turf'"
replace sclub4 = "Beaconsfield"		if sclub4=="Beacsfd."

foreach x in "" 2 3 4 5 6 7 {
replace sclub`x' = "Army and Navy"  if sclub`x'=="Army and Navy'"
replace sclub`x' = "Devonshire"	 	if sclub`x'=="Dev."
replace sclub`x' = "Gun." 			if sclub`x'=="Gun"
replace sclub`x' = "New Edinburgh" 	if sclub`x'=="New Edinborough"
replace sclub`x' = "St. James'" 	if sclub`x'=="St. James's"
}
* Conservative vs liberal clubs
foreach s in "" s{
gen str60 `s'polit = ""
foreach x in "" 2 3 4 5 6 7{
replace `s'polit = "Tory" 	 if `s'club`x'=="Carlton"  | `s'club`x'=="Jun. Carlton" | `s'club`x'=="Conservative" | `s'club`x'=="St. Stephen's"
replace `s'polit = "Liberal" if `s'club`x'=="Brooks's" | `s'club`x'=="Devonshire"   | `s'club`x'=="Reform"
}
}


* Results matrix

mat define tabC7 = (.,.,.,.,.\.,.,.,.,.\.,.,.,.,.\.,.,.,.,.\.,.,.,.,.\.,.,.,.,.\.,.,.,.,.\.,.,.,.,.\.,.,.,.,.)


* observed frequencies

tab polit spolit, matcell(ofreq)
mat tabC7[1,3] = ofreq[1,1]+ofreq[1,2]
mat tabC7[4,3] = ofreq[2,1]+ofreq[2,2]
mat tabC7[7,1] = ofreq[1,1]+ofreq[2,1]
mat tabC7[7,2] = ofreq[1,2]+ofreq[2,2]
mat tabC7[7,3] = tabC7[7,1]+tabC7[7,2]


* observed percentages 

mat tabC7[1,1] = round(100*ofreq[1,1]/tabC7[1,3],0.1)
mat tabC7[1,2] = round(100*ofreq[1,2]/tabC7[1,3],0.1)
mat tabC7[4,1] = round(100*ofreq[2,1]/tabC7[4,3],0.1)
mat tabC7[4,2] = round(100*ofreq[2,2]/tabC7[4,3],0.1)


* observed percentages 

tab polit spolit, expected
mat tabC7[2,1] = round(100*12.6/tabC7[1,3],0.1)
mat tabC7[2,2] = round(100*30.4/tabC7[1,3],0.1)
mat tabC7[5,1] = round(100*28.4/tabC7[4,3],0.1)
mat tabC7[5,2] = round(100*68.6/tabC7[4,3],0.1)


* differences

mat tabC7[3,1] = round(tabC7[1,1]-tabC7[2,1],0.1)
mat tabC7[3,2] = round(tabC7[1,2]-tabC7[2,2],0.1)
mat tabC7[6,1] = round(tabC7[4,1]-tabC7[5,1],0.1)
mat tabC7[6,2] = round(tabC7[4,2]-tabC7[5,2],0.1)


* chi-squared test of association
tab polit spolit, all cchi

mat tabC7[8,4] = round(r(chi2),0.001)
mat tabC7[8,5] = round(r(p),0.01)
mat tabC7[9,4] = round(r(gamma),0.01)
mat tabC7[9,5] = round(r(ase_gam),0.01)


* Export results

mat colnames tabC7 = "Liberal club" "Tory club" "N" " . " " . "
mat rownames tabC7 = "Liberal club: observed" "Liberal club: expected" "Liberal club: diff" "Tory club: observed" "Tory club: expected" "Tory club: diff" "Total: N" "Tests: Chi2" "Tests: Gamma"

esttab matrix(tabC7, fmt(2)) using "${dirresu}\Appendix\TableC7.tex", mlabel("Table C7") replace



* ==============================================================================
* FIGURE C4: 
* Political endogamy over time.
* ------------------------------------------------------------------------------


* Import data

use "${dirdata}\final-data.dta", clear
keep if myear<=1875
keep if nthismarriage<2


* Political clubs

	/* Note:  This is based on the clubs reported in Bateman's data. Specifically, 

	Brook's, Reform, and Devonshire are liberal Clubs; Carlton, Junior Carlton, 

	Conservative, or St. Stephen's are conservative Clubs (Bateman 1883: 497).  */

* Club names housekeeping
replace club2 = "St. James'" 		if club2=="St. Jas."
replace club3 = "St. James'" 		if club3=="St. Jas."
replace club3 = "St. Stephen's" 	if club3=="Stephen's"
replace club4 = "Beaconsfield" 		if club4=="Beacsfd."
replace club7 = "United Service" 	if club7=="U. Serv."

replace sclub = "Carlton" 			if sclub=="Carlton'"
replace sclub = "Marlborough"		if sclub=="Marlborough'"
replace sclub2 = "Army and Navy'"	if sclub2=="Army and Navy'"
replace sclub2 = "Brooks's"			if sclub2=="Brooks'"
replace sclub2 = "Conservative"		if sclub2=="Conservative'"
replace sclub2 = "Naval and Military" if sclub2=="N. And M."
replace sclub2 = "St. James'"		if sclub2=="St. Jas."
replace sclub2 = "White's"			if sclub2=="White's'"
replace sclub3 = "Dev."				if sclub3=="Devonshire"
replace sclub3 = "Guards"			if sclub3=="Guard"
replace sclub3 = "St. James's"		if sclub3=="St. Jas."
replace sclub3 = "Turf"				if sclub3=="Turf'"
replace sclub4 = "Beaconsfield"		if sclub4=="Beacsfd."

foreach x in "" 2 3 4 5 6 7 {
replace sclub`x' = "Army and Navy"  if sclub`x'=="Army and Navy'"
replace sclub`x' = "Devonshire"	 	if sclub`x'=="Dev."
replace sclub`x' = "Gun." 			if sclub`x'=="Gun"
replace sclub`x' = "New Edinburgh" 	if sclub`x'=="New Edinborough"
replace sclub`x' = "St. James'" 	if sclub`x'=="St. James's"
}
* Conservative vs liberal clubs
foreach s in "" s{
gen str60 `s'polit = ""
foreach x in "" 2 3 4 5 6 7{
replace `s'polit = "Tory" 	 if `s'club`x'=="Carlton"  | `s'club`x'=="Jun. Carlton" | `s'club`x'=="Conservative" | `s'club`x'=="St. Stephen's"
replace `s'polit = "Liberal" if `s'club`x'=="Brooks's" | `s'club`x'=="Devonshire"   | `s'club`x'=="Reform"
}
}
* Political homogamy
gen polhom = (polit==spolit & polit!="")
* other
gen decadeclub = .
forvalues i=1851(5)1875{
replace decadeclub = `i' if myear>=`i' & myear<`i'+5
}

* Figure C4
keep if polit!="" & spolit!="" & attendLC!=.
collapse (sum) attendLC (sd) sd1= attendLC (count) n1=attendLC (mean) polhom (sd) sd2=polhom (count) n2=polhom, by(decadeclub)	
gen hi2 = polhom + invttail(n2-1,0.025)*(sd2 / sqrt(n2))
gen lo2 = polhom - invttail(n2-1,0.025)*(sd2 / sqrt(n2))
drop if decadeclub<1850
replace hi2=1 if hi2>1
replace lo2=0 if lo2<0
twoway 	(bar attendLC decadeclub, yaxis(2) color(sand)) 												///
	 	(connect polhom decadeclub, yaxis(1) mcolor(black) lcolor(black) msymbol(diamond))				///
	 	(connect hi2 decadeclub, yaxis(1) mcolor(black) lcolor(black) msymbol(none) lpattern(dash))		///
	 	(connect lo2 decadeclub, yaxis(1) mcolor(black) lcolor(black) msymbol(none) lpattern(dash)),	///
	 	ylabel(0(40000)130000, axis(2)) ylabel(0.2 "20" 0.4 "40" 0.6 "60" 0.8 "80" 1 "100", axis(1)) 	///
	 	xlabel(1851 "1851-55" 1856 "1856-60" 1861 "1861-65" 1866 "1866-70" 1871 "1871-75") 				///
	 	xtitle("") ytitle("%", axis(1)) ytitle("attendees", axis(2)) graphregion(color(white))			///
	 	legend(on order(1 "Royal parties" 2 "Political endogamy" 3 "c.i. (95%)"))
graph export "${dirresu}\Appendix\FigureC4.pdf", as(pdf) replace



* ==============================================================================
* TABLE C8: 
* Reduced-form effects of the interruption of the Season.
* ------------------------------------------------------------------------------


* Import data

use "${dirdta}\final-data-men.dta", clear


* Globals

global controls1 i.crank biorder rel1 mage hengpee sexr railLength100 t dec50 dec60
global controls2 i.crank biorder rel1 mage hengpee dec50 t
global controlsA1 ATot VTot relandA mage hengpee sexr railLength100 t dec50 dec60
global controlsA2 ATot VTot relandA mage hengpee dec50 t
global controlsV1 ATot VTot relandV  mage hengpee sexr railLength100 t dec50 dec60
global controlsV2 ATot VTot relandV  mage hengpee dec50 t
global controlsPCA1 i.crank ATot VTot wood2 old relandV mage hengpee sexr railLength100 t dec50 dec60
global controlsPCA2 i.crank ATot VTot wood2 old relandV mage hengpee t dec50 dec60
global controlsG1 i.crank reldiv mage wom hengpee sexr railLength100 t dec50 dec60
global controlsG2 i.crank reldiv mage wom hengpee t dec50 dec60


* Peer-comoner intermarriage (women)
* (1858-66)
probit cOut mourn $controls2 if myear>=1858 & myear<=1866 & wom==1 & cOut!=., cluster(myear)
qui margins, dydx(*) pred(pr) post
outreg2 using "${dirresu}\Appendix\TableC8_A.tex", keep(mourn) nocons label dec(3) coefastr 	///
ctitle("Mcommon (wom)") addtext(Individual controls, YES, Decade FE, YES, Trend, YES, model, probit) ///
tex replace
* (1851-75)
qui probit cOut mourn $controls1 if wom==1 & cOut!=., cluster(myear)
qui margins, dydx(*) pred(pr) post
outreg2 using "${dirresu}\Appendix\TableC8_B.tex", keep(mourn) nocons label dec(3) coefastr 			///
ctitle("Mcommon (wom)") addtext(Individual controls, YES, Decade FE, YES, Trend, YES, model, probit) ///
tex replace


* Peer-comoner intermarriage (men)
* (1858-66)
probit cOut mourn $controls2 if myear>=1858 & myear<=1866 & wom==0 & cOut!=., cluster(myear)
qui margins, dydx(*) pred(pr) post
outreg2 using "${dirresu}\Appendix\TableC8_A.tex", keep(mourn) nocons label dec(3) coefastr 			///
ctitle("Mcommon (men)") addtext(Individual controls, YES, Decade FE, YES, Trend, YES, model, probit) tex
* (1851-75)
qui probit cOut mourn $controls1 if wom==0, cluster(myear)
qui margins, dydx(*) pred(pr) post
outreg2 using "${dirresu}\Appendix\TableC8_B.tex", keep(mourn) nocons label dec(3) coefastr 			///
ctitle("Mcommon (men)") addtext(Individual controls, YES, Decade FE, YES, Trend, YES, model, probit) tex


* Acres homogamy
use "${dirdta}\final-data-men.dta", clear
keep if ATotal!=. & wom==0
* (1858-66)
probit landhoA mourn $controlsA2 if myear>=1858 & myear<=1866 & landhoA!=., cluster(myear)
qui margins, dydx(*) pred(pr) post
outreg2 using "${dirresu}\Appendix\TableC8_A.tex", keep(mourn) nocons label dec(3) coefastr 			///
ctitle("Same class (acres)") addtext(Individual controls, YES, Decade FE, YES, Trend, YES, model, probit) tex
* (1851-75)
qui probit landhoA mourn $controlsA1 if landhoA!=., cluster(myear)
qui margins, dydx(*) pred(pr) post
outreg2 using "${dirresu}\Appendix\TableC8_B.tex", keep(mourn) nocons label dec(3) coefastr 			///
ctitle("Same class (acres)") addtext(Individual controls, YES, Decade FE, YES, Trend, YES, model, probit) tex


* Acres missmatch
* (1858-66)
qui reg missA mourn $controlsA2 if myear>=1858 & myear<=1866 & missA!=., cluster(myear)
outreg2 using "${dirresu}\Appendix\TableC8_A.tex", keep(mourn) nocons label dec(3) coefastr nor2 		///
ctitle("Miss (acres)") addtext(Individual controls, YES, Decade FE, YES, Trend, YES, model, OLS) tex
* (1851-75)
qui reg missA mourn $controlsA1 if missA!=., vce(cluster myear)
outreg2 using "${dirresu}\Appendix\TableC8_B.tex", keep(mourn) nocons label dec(3) coefastr nor2 		///
ctitle("Miss (acres)") addtext(Individual controls, YES, Decade FE, YES, Trend, YES, model, OLS) tex

		
* Land rents homogamy
* (1858-66)
probit landhoV mourn $controlsV2 if myear>=1858 & myear<=1866 & landhoV!=., cluster(myear)
qui margins, dydx(*) pred(pr) post
outreg2 using "${dirresu}\Appendix\TableC8_A.tex", keep(mourn) nocons label dec(3) coefastr 			///
ctitle("Same class (rent)") addtext(Individual controls, YES, Decade FE, YES, Trend, YES, model, probit) tex
* (1851-75)
qui probit landhoV mourn $controlsV1 if landhoV!=., cluster(myear)
qui margins, dydx(*) pred(pr) post
outreg2 using "${dirresu}\Appendix\TableC8_B.tex", keep(mourn) nocons label dec(3) coefastr 			///
ctitle("Same class (rent)") addtext(Individual controls, YES, Decade FE, YES, Trend, YES, model, probit) tex


* Land rents missmatch
* (1858-66)
reg missV mourn $controlsV2 if myear>=1858 & myear<=1866 & missV!=., cluster(myear)
outreg2 using "${dirresu}\Appendix\TableC8_A.tex", keep(mourn) nocons label dec(3) coefastr nor2 		///
ctitle("Miss (rent)") addtext(Individual controls, YES, Decade FE, YES, Trend, YES, model, OLS) tex
* (1851-75)
qui reg missV mourn $controlsV1 if missV!=., vce(cluster myear)
outreg2 using "${dirresu}\Appendix\TableC8_B.tex", keep(mourn) nocons label dec(3) coefastr nor2 		///
ctitle("Miss (rent)") addtext(Individual controls, YES, Decade FE, YES, Trend, YES, model, OLS) tex


* PCA
use "${dirdta}\final-data-men.dta", clear
gsort surname title wgslat -nthismarr
keep if ATotal!=. & wom==0
foreach x in "" s{
gen `x'old = 1 if `x'oldfamily=="yes"
replace `x'old = 0 if `x'oldfamily=="no"
gen `x'wood2 = 1 if `x'woods=="yes"
replace `x'wood2 = 0 if `x'woods=="no"
}
gen hengpeespo = (hpeeragespo==2)
gen hscopeespo = (hpeeragespo==3)
gen hiripeespo = (hpeeragespo==4)
global ses   crank 		ATotal  VTotal	hengpee 	hscotpee   hiripee    old  			wood2
global sespo prankspo	sATotal sVTotal	hengpeespo	hscopeespo hiripeespo sold heirspo 	swood2
gsort crank ATotal VTotal hengpee hscotpee hiripee old wood2 -refno
qui pca $ses
qui pca $ses, mineigen(1)
qui predict ses ses2 ses3 ses4 ses5 ses6 ses7, score
gsort prankspo sATotal sVTotal hengpeespo hscopeespo hiripeespo sold heirspo swood2 -refno
qui pca $sesppo0
qui pca $sespo, mineigen(1)
qui predict sespo sespo2 sespo3 sespo4 sespo5 sespo6 sespo7, score
gsort ses -refno
gen ranking = _n if wom==0 & ses!=.
gsort sespo -refno
gen sranking = _n if wom==0 & sespo!=.
gen pcasort = abs(ranking-sranking)
* (1858-66)
reg pcasort mourn $controlsPCA2 if myear>=1858 & myear<=1866 & wom==0 & pcasort!=., cluster(myear)
outreg2 using "${dirresu}\Appendix\TableC8_A.tex", keep(mourn) nocons label dec(3) coefastr nor2	///
ctitle("Miss (pca)") addtext(Individual controls, YES, Decade FE, YES, Trend, YES, model, OLS) tex
* (1851-75)
qui reg pcasort mourn $controlsPCA1  if wom==0 & pcasort!=., cluster(myear)
outreg2 using "${dirresu}\Appendix\TableC8_B.tex", keep(mourn) nocons label dec(3) coefastr nor2	///
ctitle("Miss (pca)") addtext(Individual controls, YES, Decade FE, YES, Trend, YES, model, OLS) tex


* region homogamy
use "${dirdta}\final-data-men.dta", clear
keep if dist!=. & wom==0
* (1858-66)
probit disthom mourn $controlsG2 if myear>=1858 & myear<=1866, cluster(myear)
qui margins, dydx(*) pred(pr) post
outreg2 using "${dirresu}\Appendix\TableC8_A.tex", keep(mourn) nocons label dec(3) coefastr 			///
ctitle("<100mi") addtext(Individual controls, YES, Decade FE, YES, Trend, YES, model, probit) tex
* (1851-75)
qui probit disthom mourn $controlsG1, vce(cluster myear)
qui margins, dydx(*) pred(pr) post
outreg2 using "${dirresu}\Appendix\TableC8_B.tex", keep(mourn) nocons label dec(3) coefastr 		///
ctitle("<100mi") addtext(Individual controls, YES, Decade FE, YES, Trend, YES, model, probit) tex


* distance
* (1858-66)
reg dist mourn $controlsG2 if myear>=1858 & myear<=1866, cluster(myear)
outreg2 using "${dirresu}\Appendix\TableC8_A.tex", keep(mourn) nocons label dec(3) coefastr nor2		///
ctitle("dist") addtext(Individual controls, YES, Decade FE, YES, Trend, YES, model, OLS) tex
* (1851-75)
qui reg dist mourn $controlsG1, cluster(myear)
outreg2 using "${dirresu}\Appendix\TableC8_B.tex", keep(mourn) nocons label dec(3) coefastr nor2	///
ctitle("dist") addtext(Individual controls, YES, Decade FE, YES, Trend, YES, model, OLS) tex



* ==============================================================================
* TABLE C9: 
* Alternative measures of attendance to the Season.
* ------------------------------------------------------------------------------


* Import data

use "${dirdta}\final-data-men.dta", clear


* SES ranking, using pca

foreach x in "" s{
gen `x'old = 1 if `x'oldfamily=="yes"
replace `x'old = 0 if `x'oldfamily=="no"
gen `x'wood2 = 1 if `x'woods=="yes"
replace `x'wood2 = 0 if `x'woods=="no"
}
gen hengpeespo = (hpeeragespo==2)
gen hscopeespo = (hpeeragespo==3)
gen hiripeespo = (hpeeragespo==4)
global ses   crank 		ATotal  VTotal	hengpee 	hscotpee   hiripee    old  			wood2
global sespo prankspo	sATotal sVTotal	hengpeespo	hscopeespo hiripeespo sold heirspo 	swood2
gsort $ses -refno
pca $ses if wom==0, mineigen(1)
predict ses ses2 ses3 ses4 ses5 ses6 ses7 if wom==0, score
gsort $sespo -refno
pca $sespo if wom==0, mineigen(1)
predict sespo sespo2 sespo3 sespo4 sespo5 sespo6 sespo7 if wom==0, score
gsort ses -refno
gen ranking = _n if wom==0 & ses!=.
gsort sespo -refno
gen sranking = _n if wom==0 & sespo!=.
gen pcasort = abs(ranking-sranking) if wom==0


* Regressions

foreach x in attend100 invited100 attended_bc100 invited_bc100{
qui cmp (cOut=  `x' i.crank biorder rel1 mage hengpee sexr railLength100 t dec50 dec60) (`x'=riskMin mourn d1851 sexr railLength100 t dec50 dec60) if wom==1 & cOut!=., ind(4 1) cluster(myear)
qui margins, dydx(*) predict(eq(#1) pr) post
scalar define B1`x' = _b[`x']
scalar define S1`x' = _se[`x']
*
qui cmp (landhoA = `x' ATot VTot relandA mage hengpee sexr railLength100 t dec50 dec60) (`x' = riskMin d1851 mourn sexr t dec50 dec60 railLength100) if wom==0 & landhoA!=., ind(4 1) cluster(myear)
qui margins, dydx(*) predict(eq(#1) pr) post
scalar define B2`x' = _b[`x']
scalar define S2`x' = _se[`x']
*
qui cmp (missA = `x' ATot VTot relandA mage hengpee sexr railLength100 t dec50 dec60) (`x' = riskMin d1851 mourn sexr t dec50 dec60 railLength100) if wom==0 & missA!=., ind(1 1) cluster(myear)
qui margins, dydx(*) predict(eq(#1)) post
scalar define B3`x' = _b[`x']
scalar define S3`x' = _se[`x']
*
qui cmp (landhoV = `x' ATot VTot relandV mage hengpee sexr railLength100 t dec50 dec60) (`x' = riskMin d1851 mourn sexr t dec50 dec60 railLength100) if wom==0 & landhoV!=., ind(4 1) cluster(myear)
qui margins, dydx(*) predict(eq(#1) pr) post
scalar define B4`x' = _b[`x']
scalar define S4`x' = _se[`x']
*
qui cmp (missV = `x' ATot VTot relandV mage hengpee sexr railLength100 t dec50 dec60) (`x' = riskMin d1851 mourn sexr t dec50 dec60 railLength100) if wom==0 & missV!=., ind(1 1) cluster(myear)
qui margins, dydx(*) predict(eq(#1)) post
scalar define B5`x' = _b[`x']
scalar define S5`x' = _se[`x']
*
qui cmp (pcasort =  `x' i.crank ATot VTot wood2 old relandV mage hengpee sexr railLength100 t dec50 dec60) (`x'=riskMin mourn d1851 sexr railLength100 t dec50 dec60) if wom==0 & pcasort!=., ind(1 1) cluster(myear)
qui margins, dydx(*) predict(eq(#1)) post
scalar define B6`x' = _b[`x']
scalar define S6`x' = _se[`x']
*
qui cmp (divhom = `x' i.crank reldiv mage wom hengpee sexr railLength100 t dec50 dec60) (`x'=riskMin mourn d1851 sexr t dec50 dec60 railLength100) if wom==0 & dist!=., ind(4 1) cluster(myear)
qui margins, dydx(*) predict(eq(#1) pr) post
scalar define B7`x' = _b[`x']
scalar define S7`x' = _se[`x']
*
qui cmp (dist=`x' i.crank reldiv mage wom hengpee sexr railLength100 t dec50 dec60) (`x'=riskMin mourn d1851 sexr t dec50 dec60 railLength100) if wom==0 & dist!=., ind(1 1) cluster(myear)
qui margins, dydx(*) predict(eq(#1)) post
scalar define B8`x' = _b[`x']
scalar define S8`x' = _se[`x']
}
matrix alternative = (B1attend100, B1invited100, B1attended_bc100, B1invited_bc100\	///
					  S1attend100, S1invited100, S1attended_bc100, S1invited_bc100\	///
					  B2attend100, B2invited100, B2attended_bc100, B2invited_bc100\	///	
					  S2attend100, S2invited100, S2attended_bc100, S2invited_bc100\	///
					  B4attend100, B4invited100, B4attended_bc100, B4invited_bc100\	///
					  S4attend100, S4invited100, S4attended_bc100, S4invited_bc100\	///
					  B3attend100, B3invited100, B3attended_bc100, B3invited_bc100\	///
					  S3attend100, S3invited100, S3attended_bc100, S3invited_bc100\	///
					  B5attend100, B5invited100, B5attended_bc100, B5invited_bc100\	///
					  S5attend100, S5invited100, S5attended_bc100, S5invited_bc100\	///
					  B6attend100, B6invited100, B6attended_bc100, B6invited_bc100\	///
					  S6attend100, S6invited100, S6attended_bc100, S6invited_bc100\	///
					  B7attend100, B7invited100, B7attended_bc100, B7invited_bc100\	///
					  S7attend100, S7invited100, S7attended_bc100, S7invited_bc100\	///
					  B8attend100, B8invited100, B8attended_bc100, B8invited_bc100\	///
					  S8attend100, S8invited100, S8attended_bc100, S8invited_bc100)
mat rown alternative = "Husb is commoner" "se" "Wife in acres class" "se" "Wife in rents class" "se" "Missmatch (acres)" "se" "Missmatch (rents)" "se" "Missmatch (pca)" "se" "Wife <100 mi" "se" "Distance btw seats" "se"
mat coln alternative = "baseline" "invited" "attend balls concerts" "invited balls concenrts"
esttab matrix(alternative, fmt(4)) using "${dirresu}\Appendix\TableC9.tex", mlabel("Table C9") replace



* ==============================================================================
* TABLE C10: 
* IV estimates for plausibly exogenous cohort size instrument.
* ------------------------------------------------------------------------------


* Import data

use "${dirdta}\final-data-men.dta", clear


* SES ranking, using pca

foreach x in "" s{
gen `x'old = 1 if `x'oldfamily=="yes"
replace `x'old = 0 if `x'oldfamily=="no"
gen `x'wood2 = 1 if `x'woods=="yes"
replace `x'wood2 = 0 if `x'woods=="no"
}
gen hengpeespo = (hpeeragespo==2)
gen hscopeespo = (hpeeragespo==3)
gen hiripeespo = (hpeeragespo==4)
global ses   crank 		ATotal  VTotal	hengpee 	hscotpee   hiripee    old  			wood2
global sespo prankspo	sATotal sVTotal	hengpeespo	hscopeespo hiripeespo sold heirspo 	swood2
gsort $ses -refno
pca $ses if wom==0, mineigen(1)
predict ses ses2 ses3 ses4 ses5 ses6 ses7 if wom==0, score
gsort $sespo -refno
pca $sespo if wom==0, mineigen(1)
predict sespo sespo2 sespo3 sespo4 sespo5 sespo6 sespo7 if wom==0, score
gsort ses -refno
gen ranking = _n if wom==0 & ses!=.
gsort sespo -refno
gen sranking = _n if wom==0 & sespo!=.
gen pcasort = abs(ranking-sranking) if wom==0


* Regressions

qui cmp (cOut = 	attend100 i.crank biorder rel1 mage hengpee sexr railLength100 t dec50 dec60) (attend100=riskMin mourn d1851 sexr railLength100 t dec50 dec60) if wom==1 & cOut!=., ind(4 1) cluster(myear)
	scalar define B11 = _b[attend100]
	scalar define S11 = _se[attend100]
nlcom pla: [cOut]_b[attend100]+0.1 *[cOut]_b[attend100]/[attend100]_b[riskMin], post
	scalar define B21 = _b[pla]
	scalar define S21 = _se[pla]
qui cmp (cOut = 	attend100 i.crank biorder rel1 mage hengpee sexr railLength100 t dec50 dec60) (attend100=riskMin mourn d1851 sexr railLength100 t dec50 dec60) if wom==1 & cOut!=., ind(4 1) cluster(myear)
nlcom pla: [cOut]_b[attend100]+0.25*[cOut]_b[attend100]/[attend100]_b[riskMin], post
	scalar define B31 = _b[pla]
	scalar define S31 = _se[pla]
qui cmp (cOut = 	attend100 i.crank biorder rel1 mage hengpee sexr railLength100 t dec50 dec60) (attend100=riskMin mourn d1851 sexr railLength100 t dec50 dec60) if wom==1 & cOut!=., ind(4 1) cluster(myear)
nlcom pla: [cOut]_b[attend100]+0.5 *[cOut]_b[attend100]/[attend100]_b[riskMin], post
	scalar define B41 = _b[pla]
	scalar define S41 = _se[pla]
qui cmp (cOut = 	attend100 i.crank biorder rel1 mage hengpee sexr railLength100 t dec50 dec60) (attend100=riskMin mourn d1851 sexr railLength100 t dec50 dec60) if wom==1 & cOut!=., ind(4 1) cluster(myear)
nlcom pla: [cOut]_b[attend100]+0.75*[cOut]_b[attend100]/[attend100]_b[riskMin], post
	scalar define B51 = _b[pla]
	scalar define S51 = _se[pla]
*
qui cmp (landhoA = 	attend100 ATot VTot relandA mage hengpee sexr railLength100 t dec50 dec60) (attend100 = riskMin d1851 mourn sexr t dec50 dec60 railLength100) if wom==0 & landhoA!=., ind(4 1) cluster(myear)
	scalar define B12 = _b[attend100]
	scalar define S12 = _se[attend100]
nlcom pla: [landhoA]_b[attend100]+0.1 *[landhoA]_b[attend100]/[attend100]_b[riskMin], post
	scalar define B22 = _b[pla]
	scalar define S22 = _se[pla]
qui cmp (landhoA = 	attend100 ATot VTot relandA mage hengpee sexr railLength100 t dec50 dec60) (attend100 = riskMin d1851 mourn sexr t dec50 dec60 railLength100) if wom==0 & landhoA!=., ind(4 1) cluster(myear)
nlcom pla: [landhoA]_b[attend100]+0.25*[landhoA]_b[attend100]/[attend100]_b[riskMin], post
	scalar define B32 = _b[pla]
	scalar define S32 = _se[pla]
qui cmp (landhoA = 	attend100 ATot VTot relandA mage hengpee sexr railLength100 t dec50 dec60) (attend100 = riskMin d1851 mourn sexr t dec50 dec60 railLength100) if wom==0 & landhoA!=., ind(4 1) cluster(myear)
nlcom pla: [landhoA]_b[attend100]+0.5 *[landhoA]_b[attend100]/[attend100]_b[riskMin], post
	scalar define B42 = _b[pla]
	scalar define S42 = _se[pla]
qui cmp (landhoA = 	attend100 ATot VTot relandA mage hengpee sexr railLength100 t dec50 dec60) (attend100 = riskMin d1851 mourn sexr t dec50 dec60 railLength100) if wom==0 & landhoA!=., ind(4 1) cluster(myear)
nlcom pla: [landhoA]_b[attend100]+0.75*[landhoA]_b[attend100]/[attend100]_b[riskMin], post
	scalar define B52 = _b[pla]
	scalar define S52 = _se[pla]
*
qui cmp (landhoV = 	attend100 ATot VTot relandV mage hengpee sexr railLength100 t dec50 dec60) (attend100 = riskMin d1851 mourn sexr t dec50 dec60 railLength100) if wom==0 & landhoV!=., ind(4 1) cluster(myear)
	scalar define B13 = _b[attend100]
	scalar define S13 = _se[attend100]
nlcom pla: [landhoV]_b[attend100]+0.1 *[landhoV]_b[attend100]/[attend100]_b[riskMin], post
	scalar define B23 = _b[pla]
	scalar define S23 = _se[pla]
qui cmp (landhoV = 	attend100 ATot VTot relandV mage hengpee sexr railLength100 t dec50 dec60) (attend100 = riskMin d1851 mourn sexr t dec50 dec60 railLength100) if wom==0 & landhoV!=., ind(4 1) cluster(myear)
nlcom pla: [landhoV]_b[attend100]+0.25*[landhoV]_b[attend100]/[attend100]_b[riskMin], post
	scalar define B33 = _b[pla]
	scalar define S33 = _se[pla]
qui cmp (landhoV = 	attend100 ATot VTot relandV mage hengpee sexr railLength100 t dec50 dec60) (attend100 = riskMin d1851 mourn sexr t dec50 dec60 railLength100) if wom==0 & landhoV!=., ind(4 1) cluster(myear)
nlcom pla: [landhoV]_b[attend100]+0.5 *[landhoV]_b[attend100]/[attend100]_b[riskMin], post
	scalar define B43 = _b[pla]
	scalar define S43 = _se[pla]
qui cmp (landhoV = 	attend100 ATot VTot relandV mage hengpee sexr railLength100 t dec50 dec60) (attend100 = riskMin d1851 mourn sexr t dec50 dec60 railLength100) if wom==0 & landhoV!=., ind(4 1) cluster(myear)
nlcom pla: [landhoV]_b[attend100]+0.75*[landhoV]_b[attend100]/[attend100]_b[riskMin], post
	scalar define B53 = _b[pla]
	scalar define S53 = _se[pla]
*
qui cmp (missA = 	attend100 ATot VTot relandA mage hengpee sexr railLength100 t dec50 dec60) (attend100 = riskMin d1851 mourn sexr t dec50 dec60 railLength100) if wom==0 & missA!=., ind(1 1) cluster(myear)
	scalar define B14 = _b[attend100]
	scalar define S14 = _se[attend100]
nlcom pla: [missA]_b[attend100]+0.1 *[missA]_b[attend100]/[attend100]_b[riskMin], post
	scalar define B24 = _b[pla]
	scalar define S24 = _se[pla]
qui cmp (missA = 	attend100 ATot VTot relandA mage hengpee sexr railLength100 t dec50 dec60) (attend100 = riskMin d1851 mourn sexr t dec50 dec60 railLength100) if wom==0 & missA!=., ind(1 1) cluster(myear)
nlcom pla: [missA]_b[attend100]+0.25*[missA]_b[attend100]/[attend100]_b[riskMin], post
	scalar define B34 = _b[pla]
	scalar define S34 = _se[pla]
qui cmp (missA = 	attend100 ATot VTot relandA mage hengpee sexr railLength100 t dec50 dec60) (attend100 = riskMin d1851 mourn sexr t dec50 dec60 railLength100) if wom==0 & missA!=., ind(1 1) cluster(myear)
nlcom pla: [missA]_b[attend100]+0.5 *[missA]_b[attend100]/[attend100]_b[riskMin], post
	scalar define B44 = _b[pla]
	scalar define S44 = _se[pla]
qui cmp (missA = 	attend100 ATot VTot relandA mage hengpee sexr railLength100 t dec50 dec60) (attend100 = riskMin d1851 mourn sexr t dec50 dec60 railLength100) if wom==0 & missA!=., ind(1 1) cluster(myear)
nlcom pla: [missA]_b[attend100]+0.75*[missA]_b[attend100]/[attend100]_b[riskMin], post
	scalar define B54 = _b[pla]
	scalar define S54 = _se[pla]
*
qui cmp (missV = 	attend100 ATot VTot relandV mage hengpee sexr railLength100 t dec50 dec60) (attend100 = riskMin d1851 mourn sexr t dec50 dec60 railLength100) if wom==0 & missV!=., ind(1 1) cluster(myear)
	scalar define B15 = _b[attend100]
	scalar define S15 = _se[attend100]
nlcom pla: [missV]_b[attend100]+0.1 *[missV]_b[attend100]/[attend100]_b[riskMin], post
	scalar define B25 = _b[pla]
	scalar define S25 = _se[pla]
qui cmp (missV = 	attend100 ATot VTot relandV mage hengpee sexr railLength100 t dec50 dec60) (attend100 = riskMin d1851 mourn sexr t dec50 dec60 railLength100) if wom==0 & missV!=., ind(1 1) cluster(myear)
nlcom pla: [missV]_b[attend100]+0.25*[missV]_b[attend100]/[attend100]_b[riskMin], post
	scalar define B35 = _b[pla]
	scalar define S35 = _se[pla]
qui cmp (missV = 	attend100 ATot VTot relandV mage hengpee sexr railLength100 t dec50 dec60) (attend100 = riskMin d1851 mourn sexr t dec50 dec60 railLength100) if wom==0 & missV!=., ind(1 1) cluster(myear)
nlcom pla: [missV]_b[attend100]+0.5 *[missV]_b[attend100]/[attend100]_b[riskMin], post
	scalar define B45 = _b[pla]
	scalar define S45 = _se[pla]
qui cmp (missV = 	attend100 ATot VTot relandV mage hengpee sexr railLength100 t dec50 dec60) (attend100 = riskMin d1851 mourn sexr t dec50 dec60 railLength100) if wom==0 & missV!=., ind(1 1) cluster(myear)
nlcom pla: [missV]_b[attend100]+0.75*[missV]_b[attend100]/[attend100]_b[riskMin], post
	scalar define B55 = _b[pla]
	scalar define S55 = _se[pla]
*
qui cmp (pcasort =  attend100 i.crank ATot VTot wood2 old relandV mage hengpee sexr railLength100 t dec50 dec60) (attend100=riskMin mourn d1851 sexr railLength100 t dec50 dec60) if wom==0 & pcasort!=., ind(1 1) cluster(myear)
	scalar define B16 = _b[attend100]
	scalar define S16 = _se[attend100]
nlcom pla: [pcasort]_b[attend100]+0.1 *[pcasort]_b[attend100]/[attend100]_b[riskMin], post
	scalar define B26 = _b[pla]
	scalar define S26 = _se[pla]
qui cmp (pcasort =  attend100 i.crank ATot VTot wood2 old relandV mage hengpee sexr railLength100 t dec50 dec60) (attend100=riskMin mourn d1851 sexr railLength100 t dec50 dec60) if wom==0 & pcasort!=., ind(1 1) cluster(myear)
nlcom pla: [pcasort]_b[attend100]+0.25*[pcasort]_b[attend100]/[attend100]_b[riskMin], post
	scalar define B36 = _b[pla]
	scalar define S36 = _se[pla]
qui cmp (pcasort =  attend100 i.crank ATot VTot wood2 old relandV mage hengpee sexr railLength100 t dec50 dec60) (attend100=riskMin mourn d1851 sexr railLength100 t dec50 dec60) if wom==0 & pcasort!=., ind(1 1) cluster(myear)
nlcom pla: [pcasort]_b[attend100]+0.5 *[pcasort]_b[attend100]/[attend100]_b[riskMin], post
	scalar define B46 = _b[pla]
	scalar define S46 = _se[pla]
qui cmp (pcasort =  attend100 i.crank ATot VTot wood2 old relandV mage hengpee sexr railLength100 t dec50 dec60) (attend100=riskMin mourn d1851 sexr railLength100 t dec50 dec60) if wom==0 & pcasort!=., ind(1 1) cluster(myear)
nlcom pla: [pcasort]_b[attend100]+0.75*[pcasort]_b[attend100]/[attend100]_b[riskMin], post
	scalar define B56 = _b[pla]
	scalar define S56 = _se[pla]
*
qui cmp (divhom = 	attend100 i.crank reldiv mage wom hengpee sexr railLength100 t dec50 dec60) (attend100 =riskMin mourn d1851 sexr t dec50 dec60 railLength100) if wom==0 & dist!=., ind(4 1) cluster(myear)
	scalar define B17 = _b[attend100]
	scalar define S17 = _se[attend100]
nlcom pla: [divhom]_b[attend100]+0.1 *[divhom]_b[attend100]/[attend100]_b[riskMin], post
	scalar define B27 = _b[pla]
	scalar define S27 = _se[pla]
qui cmp (divhom = 	attend100 i.crank reldiv mage wom hengpee sexr railLength100 t dec50 dec60) (attend100 =riskMin mourn d1851 sexr t dec50 dec60 railLength100) if wom==0 & dist!=., ind(4 1) cluster(myear)
nlcom pla: [divhom]_b[attend100]+0.25*[divhom]_b[attend100]/[attend100]_b[riskMin], post
	scalar define B37 = _b[pla]
	scalar define S37 = _se[pla]
qui cmp (divhom = 	attend100 i.crank reldiv mage wom hengpee sexr railLength100 t dec50 dec60) (attend100 =riskMin mourn d1851 sexr t dec50 dec60 railLength100) if wom==0 & dist!=., ind(4 1) cluster(myear)
nlcom pla: [divhom]_b[attend100]+0.5 *[divhom]_b[attend100]/[attend100]_b[riskMin], post
	scalar define B47 = _b[pla]
	scalar define S47 = _se[pla]
qui cmp (divhom = 	attend100 i.crank reldiv mage wom hengpee sexr railLength100 t dec50 dec60) (attend100 =riskMin mourn d1851 sexr t dec50 dec60 railLength100) if wom==0 & dist!=., ind(4 1) cluster(myear)
nlcom pla: [divhom]_b[attend100]+0.75*[divhom]_b[attend100]/[attend100]_b[riskMin], post
	scalar define B57 = _b[pla]
	scalar define S57 = _se[pla]
*
qui cmp (dist=	attend100 i.crank reldiv mage wom hengpee sexr railLength100 t dec50 dec60) (attend100 =riskMin mourn d1851 sexr t dec50 dec60 railLength100) if wom==0 & dist!=., ind(1 1) cluster(myear)
	scalar define B18 = _b[attend100]
	scalar define S18 = _se[attend100]
nlcom pla: [dist]_b[attend100]+0.1 *[dist]_b[attend100]/[attend100]_b[riskMin], post
	scalar define B28 = _b[pla]
	scalar define S28 = _se[pla]
qui cmp (dist=	attend100 i.crank reldiv mage wom hengpee sexr railLength100 t dec50 dec60) (attend100 =riskMin mourn d1851 sexr t dec50 dec60 railLength100) if wom==0 & dist!=., ind(1 1) cluster(myear)
nlcom pla: [dist]_b[attend100]+0.25*[dist]_b[attend100]/[attend100]_b[riskMin], post
	scalar define B38 = _b[pla]
	scalar define S38 = _se[pla]
qui cmp (dist=	attend100 i.crank reldiv mage wom hengpee sexr railLength100 t dec50 dec60) (attend100 =riskMin mourn d1851 sexr t dec50 dec60 railLength100) if wom==0 & dist!=., ind(1 1) cluster(myear)
nlcom pla: [dist]_b[attend100]+0.5 *[dist]_b[attend100]/[attend100]_b[riskMin], post
	scalar define B48 = _b[pla]
	scalar define S48 = _se[pla]
qui cmp (dist=	attend100 i.crank reldiv mage wom hengpee sexr railLength100 t dec50 dec60) (attend100 =riskMin mourn d1851 sexr t dec50 dec60 railLength100) if wom==0 & dist!=., ind(1 1) cluster(myear)
nlcom pla: [dist]_b[attend100]+0.75*[dist]_b[attend100]/[attend100]_b[riskMin], post
	scalar define B58 = _b[pla]
	scalar define S58 = _se[pla]
matrix plau = (B11, B21, B31, B41, B51\											///
				S11, S21, S31, S41, S51\										///
				B12, B22, B32, B42, B52\										///
				S12, S22, S32, S42, S52\										///
				B13, B23, B33, B43, B53\										///
				S13, S23, S33, S43, S53\										///
				B14, B24, B34, B44, B54\										///
				S14, S24, S34, S44, S54\										///
				B15, B25, B35, B45, B55\										///
				S15, S25, S35, S45, S55\										///
				B16, B26, B36, B46, B56\										///
				S16, S26, S36, S46, S56\										///
				B17, B27, B37, B47, B57\										///
				S17, S27, S37, S47, S57\										///
				B18, B28, B38, B48, B58\										///
				S18, S28, S38, S48, S58											///
				)
mat coln plau = "baseline" "0.1 X Beta" "0.25 X Beta" "0.5 X Beta" "0.75 X Beta"
mat rown plau = "Husb is commoner" "se" "Wife in acres class" "se" "Wife in rents class" "se" "Missmatch (acres)" "se" "Missmatch (rents)" "se" "Missmatch (pca)" "se" "Wife <100 mi" "se" "Distance btw seats" "se"
esttab matrix(plau, fmt(3)) using "${dirresu}\Appendix\TableC10.tex", mlabel("Table C10") replace



* ==============================================================================
* TABLE C11: 
* Using selection from observables to assess the selection on unobservables.
* ------------------------------------------------------------------------------


* Import data

use "${dirdta}\final-data-men.dta", clear


* SES ranking, using pca

foreach x in "" s{
gen `x'old = 1 if `x'oldfamily=="yes"
replace `x'old = 0 if `x'oldfamily=="no"
gen `x'wood2 = 1 if `x'woods=="yes"
replace `x'wood2 = 0 if `x'woods=="no"
}
gen hengpeespo = (hpeeragespo==2)
gen hscopeespo = (hpeeragespo==3)
gen hiripeespo = (hpeeragespo==4)
global ses   crank 		ATotal  VTotal	hengpee 	hscotpee   hiripee    old  			wood2
global sespo prankspo	sATotal sVTotal	hengpeespo	hscopeespo hiripeespo sold heirspo 	swood2
gsort $ses -refno
pca $ses if wom==0, mineigen(1)
predict ses ses2 ses3 ses4 ses5 ses6 ses7 if wom==0, score
gsort $sespo -refno
pca $sespo if wom==0, mineigen(1)
predict sespo sespo2 sespo3 sespo4 sespo5 sespo6 sespo7 if wom==0, score
gsort ses -refno
gen ranking = _n if wom==0 & ses!=.
gsort sespo -refno
gen sranking = _n if wom==0 & sespo!=.
gen pcasort = abs(ranking-sranking) if wom==0


* Full set of controls

qui reg cOut		attend100 i.crank biorder				rel1 	 mage     hengpee sexr railLength100 t dec50 dec60 if wom==1, cluster(myear)
scalar define Bf1 = _b[attend100]
*
preserve
keep if ATotal!=.
qui reg landhoA 	attend100 ATot VTot 					relandA mage     hengpee sexr railLength100 t dec50 dec60 if wom==0, cluster(myear)
scalar define Bf2 = _b[attend100]
qui reg missA 		attend100 ATot VTot 					relandA mage     hengpee sexr railLength100 t dec50 dec60 if wom==0, cluster(myear)
scalar define Bf3 = _b[attend100]
restore
*
preserve
keep if VTotal!=.
qui reg landhoV 	attend100 ATot VTot 					relandV	 mage     hengpee sexr railLength100 t dec50 dec60 if wom==0, cluster(myear)
scalar define Bf4 = _b[attend100]
qui reg missV 		attend100 ATot VTot 					relandV  mage     hengpee sexr railLength100 t dec50 dec60 if wom==0, cluster(myear)
scalar define Bf5 = _b[attend100]
restore
*
preserve
keep if ses!=.
qui reg pcasort 	attend100 i.crank ATot VTot wood2 old 	relandV  mage     hengpee sexr railLength100 t dec50 dec60 if wom==0, cluster(myear)
scalar define Bf6 = _b[attend100]
restore
*
preserve
keep if dist!=.
qui reg divhom 		attend100 i.crank 	 					reldiv 	 mage wom hengpee sexr railLength100 t dec50 dec60		     , cluster(myear)
scalar define Bf7 = _b[attend100]
qui reg dist	 	attend100 i.crank 	 					reldiv	 mage wom hengpee sexr railLength100 t dec50 dec60		 	 , cluster(myear)
scalar define Bf8 = _b[attend100]
restore
* Restricted set of controls
* (none)
qui reg cOut	 	attend100																	 						if wom==1, cluster(myear)
scalar define Br11 = _b[attend100]
preserve
keep if ATotal!=.
qui reg landhoA	 	attend100																	 						if wom==0, cluster(myear)
scalar define Br12 = _b[attend100]
qui reg missA	 	attend100																	 						if wom==0, cluster(myear)
scalar define Br13 = _b[attend100]
restore
*
preserve
keep if VTotal!=.
qui reg landhoV	 	attend100																	 						if wom==0, cluster(myear)
scalar define Br14 = _b[attend100]
qui reg missV	 	attend100																							if wom==0, cluster(myear)
scalar define Br15 = _b[attend100]
restore
*
preserve
keep if ses!=.
qui reg pcasort 	attend100 	 	 																					if wom==0, cluster(myear)
scalar define Br16 = _b[attend100]
restore
preserve
keep if dist!=.
qui reg divhom 		attend100 																	 								  , cluster(myear)
scalar define Br17 = _b[attend100]
qui reg dist	 	attend100 																	 								  , cluster(myear)
scalar define Br18 = _b[attend100]
restore
* (time effects)
reg cOut		attend100 		 	 																  t dec50 dec60 if wom==1, cluster(myear)
scalar define Br21= _b[attend100]
*
preserve
keep if ATotal!=.
qui reg landhoA 	attend100 		 	 															  t dec50 dec60 if wom==0, cluster(myear)
scalar define Br22= _b[attend100]
qui reg missA 		attend100 		 	 															  t dec50 dec60 if wom==0, cluster(myear)
scalar define Br23= _b[attend100]
restore
*
preserve
keep if VTotal!=.
qui reg landhoV 	attend100 		 	 															  t dec50 dec60 if wom==0, cluster(myear)
scalar define Br24= _b[attend100]
qui reg missV 		attend100 		 	 															  t dec50 dec60 if wom==0, cluster(myear)
scalar define Br25= _b[attend100]
restore
*
preserve
keep if ses!=.
qui reg pcasort 	attend100 		 	 															  t dec50 dec60 if wom==0, cluster(myear)
scalar define Br26= _b[attend100]
restore
*
preserve
keep if dist!=.
qui reg divhom 		attend100 		 	 															  t dec50 dec60		     , cluster(myear)
scalar define Br27= _b[attend100]
qui reg dist	 	attend100 		 	 															  t dec50 dec60		 	 , cluster(myear)
scalar define Br28= _b[attend100]
restore
* (time effects + cohort controls)
qui reg cOut		attend100 		 	 					rel1 	 	     	 	  sexr 			  t dec50 dec60 if wom==1, cluster(myear)
scalar define Br31= _b[attend100]
*
preserve
keep if ATotal!=.
qui reg landhoA 	attend100 			 					relandA 				  sexr 			  t dec50 dec60 if wom==0, cluster(myear)
scalar define Br32= _b[attend100]
qui reg missA 		attend100 			 					relandA 				  sexr 			  t dec50 dec60 if wom==0, cluster(myear)
scalar define Br33= _b[attend100]
restore
*
preserve
keep if VTotal!=.
qui reg landhoV 	attend100 			 					relandV	 				  sexr 			  t dec50 dec60 if wom==0, cluster(myear)
scalar define Br34= _b[attend100]
qui reg missV 		attend100 			 					relandV  				  sexr 			  t dec50 dec60 if wom==0, cluster(myear)
scalar define Br35= _b[attend100]
restore
*
preserve
keep if ses!=.
qui reg pcasort 	attend100 							 	relandV  				  sexr 			  t dec50 dec60 if wom==0, cluster(myear)
scalar define Br36= _b[attend100]
restore
*
preserve
keep if dist!=.
qui reg divhom 		attend100 		 	 					reldiv 	 				  sexr 			  t dec50 dec60		     , cluster(myear)
scalar define Br37= _b[attend100]
qui reg dist	 	attend100 		 	 					reldiv	 				  sexr 			  t dec50 dec60		 	 , cluster(myear)
scalar define Br38= _b[attend100]
restore
* (time effects + class controls)
qui reg cOut		attend100 i.crank 	 					rel1									  t dec50 dec60 if wom==1, cluster(myear)
scalar define Br41= _b[attend100]
*
preserve
keep if ATotal!=.
qui reg landhoA 	attend100 ATot VTot 					relandA									  t dec50 dec60 if wom==0, cluster(myear)
scalar define Br42= _b[attend100]
qui reg missA 		attend100 ATot VTot 					relandA									  t dec50 dec60 if wom==0, cluster(myear)
scalar define Br43= _b[attend100]
restore
*
preserve
keep if VTotal!=.
qui reg landhoV 	attend100 ATot VTot 					relandV									  t dec50 dec60 if wom==0, cluster(myear)
scalar define Br44= _b[attend100]
qui reg missV 		attend100 ATot VTot 					relandV 								  t dec50 dec60 if wom==0, cluster(myear)
scalar define Br45= _b[attend100]
restore
*
preserve
keep if ses!=.
qui reg pcasort 	attend100 i.crank ATot VTot wood2 old 	relandV									  t dec50 dec60 if wom==0, cluster(myear)
scalar define Br46= _b[attend100]
restore
*
preserve
keep if dist!=.
qui reg divhom 		attend100 i.crank 	 					reldiv 									  t dec50 dec60		     , cluster(myear)
scalar define Br47= _b[attend100]
qui reg dist	 	attend100 i.crank 	 					reldiv 									  t dec50 dec60		 	 , cluster(myear)
scalar define Br48= _b[attend100]
restore
matrix altonji = J(4,6,0)
mat rown altonji = none time timecohort timeclass
mat coln altonji = out landhoA missA landhoV missV pcasort
forvalues i=1/6{
forvalues j=1/4{
matrix altonji[`j',`i'] = abs(Bf`i'/(Br`j'`i' - Bf`i'))
}
}
mat list altonji
mat coln altonji = "H is common" "Wife acres class" "Wife rent class" "Miss acres" "Miss rent" "PCA"
mat rown altonji = "None" "Time effects" "Time effects + cohort co" "Time effects + class co"
esttab matrix(altonji, fmt(1)) using "${dirresu}\Appendix\TableC11.tex", mlabel("Table C11") replace



* ==============================================================================
* TABLE C12: 
* Robustness using standard IV.
* ------------------------------------------------------------------------------


* Import data

use "${dirdta}\final-data-men.dta", clear


* Golbals

global controls1 i.crank biorder rel1 mage hengpee sexr railLength100 t dec50 dec60
global controls2 i.crank biorder rel1 mage hengpee dec50 t
label var attend100 "Attendees Season"


* Panel A. Spouse is a commoner
* ---------------------------------
* women, probit
qui probit cOut attend100 $controls1 if wom==1 & cOut!=., cluster(myear)
qui estat classification
	local pcp1 = round(r(P_corr),1)
qui margins, dydx(*) pred(pr) post
outreg2 using "${dirresu}\Appendix\TableC12_A.tex", keep(attend100) nocons label dec(3) coefastr 			///
		ctitle("probit") addtext(F-stat from first stage, ., % correctly predicted, `pcp1', Sargan test, ., ., ., Individual controls, YES, Marriage market controls, YES, Decade FE, YES, Year trend, YES, years, 1851-75)	///
		tex replace
* women, IV probit (1858-66)
qui ivregress 2sls cOut $controls2 (attend100 = mourn) if myear>=1858 & myear<=1866 & wom==1 & cOut!=., robust cluster(myear) first
	qui estat firststage
	matrix fist1 = r(singleresults)
	local F1 = round(fist1[1,4],1)
qui ivprobit cOut $controls2 (attend100 = mourn) ///
	if myear>=1858 & myear<=1866 & wom==1 & cOut!=., vce(cluster myear) first
qui estat classification
	local pcp2 = round(r(P_corr),1)
qui margins, dydx(*) pred(pr) post
outreg2 using "${dirresu}\Appendix\TableC12_A.tex", keep(attend100) nocons label dec(3) coefastr 			///
		ctitle("IV-probit") addtext(F-stat from first stage, `F1', % correctly predicted, `pcp2', Sargan test, ., ., ., Individual controls, YES, Marriage market controls, NO, Decade FE, YES, Year trend, YES, years, 1858-66) tex
* women, IV probit (1851-75)
qui ivreg2 cOut (attend100 = riskMin d1851 mourn) $controls1 if wom==1 & cOut!=., liml robust cluster(myear) partial(t)
	local F2 = round(e(widstat),1)
	local J2 = round(e(j),0.01)
	local Jp2 = round(e(jp),0.01)
qui ivregress 2sls cOut $controls1 (attend100 = riskMin d1851 mourn) if wom==1 & cOut!=.
	estat endog
qui ivprobit cOut $controls1 (attend100 = riskMin d1851 mourn) ///
	if wom==1 & cOut!=., vce(cluster myear)
qui estat classification
	local pcp3 = round(r(P_corr),1)
qui margins, dydx(*) pred(pr) post
outreg2 using "${dirresu}\Appendix\TableC12_A.tex", keep(attend100) nocons label dec(3) coefastr 			///
		ctitle("IV-probit") addtext(F-stat from first stage, `F2', % correctly predicted, `pcp3', Sargan test, `J2', ., p=0`Jp2', Individual controls, YES, Marriage market controls, YES, Decade FE, YES, Year trend, YES, years, 1851-75) tex
* men, probit (1851-75)
qui probit cOut attend100 $controls1 if wom==0, cluster(myear)
qui estat classification
	local pcp4 = round(r(P_corr),1)
qui margins, dydx(*) pred(pr) post
outreg2 using "${dirresu}\Appendix\TableC12_A.tex", keep(attend100) nocons label dec(3) coefastr 			///
		ctitle("probit") addtext(F-stat from first stage, ., % correctly predicted, `pcp4', Sargan test, ., ., ., Individual controls, YES, Marriage market controls, YES, Decade FE, YES, Year trend, YES, years, 1851-75) tex
* men, IV probit (1858-66)
qui ivregress 2sls cOut $controls2 (attend100 = mourn) if myear>=1858 & myear<=1866 & wom==0 & cOut!=., robust cluster(myear) first
	qui estat firststage
	matrix fist3 = r(singleresults)
	local F3 = round(fist3[1,4],1)
qui ivprobit cOut $controls2 (attend100 = mourn) ///
	if myear>=1858 & myear<=1866 & wom==0 & cOut!=., vce(cluster myear)
qui estat classification
	local pcp5 = round(r(P_corr),1)
qui margins, dydx(*) pred(pr) post
outreg2 using "${dirresu}\Appendix\TableC12_A.tex", keep(attend100) nocons label dec(3) coefastr 			///
		ctitle("IV-probit") addtext(F-stat from first stage, `F3', % correctly predicted, `pcp5', Sargan test, ., ., ., Individual controls, YES, Marriage market controls, NO, Decade FE, YES, Year trend, YES, years, 1858-66) tex
* men, IV probit (1851-75)
qui ivreg2 cOut (attend100 = riskMin d1851 mourn) $controls1 if wom==0, liml robust cluster(myear) partial(t)
	local F4 = round(e(widstat),1)
	local J4 = round(e(j),0.01)
	local Jp4 = round(e(jp),0.01)
qui ivprobit cOut $controls1 (attend100 = riskMin d1851 mourn) ///
	 if wom==0, vce(cluster myear)
qui estat classification
local pcp6 = round(r(P_corr),1)
qui margins, dydx(*) pred(pr) post
outreg2 using "${dirresu}\Appendix\TableC12_A.tex", keep(attend100) nocons label dec(3) coefastr 			///
		ctitle("IV-probit")	addtext(F-stat from first stage, `F4', % correctly predicted, `pcp6', Sargan test, `J4', ., p=0`Jp4', Individual controls, YES, Marriage market controls, YES, Decade FE, YES, Year trend, YES, years, 1851-75) tex

* Panel B. Acreage
* ---------------------------------
use "${dirdta}\final-data-men.dta", clear
keep if ATotal!=. & wom==0
global controlsA1 ATot VTot relandA mage hengpee sexr railLength100 t dec50 dec60
global controlsA2 ATot VTot relandA mage hengpee dec50 t
global controlsV1 ATot VTot relandV  mage hengpee sexr railLength100 t dec50 dec60
global controlsV2 ATot VTot relandV  mage hengpee dec50 t
label var attend100 "Attendees Season"
* wife in land class (acres), probit (1851-75)
qui probit landhoA attend100 $controlsA1 if landhoA!=., cluster(myear)
qui estat classification
	local pcp1 = round(r(P_corr),1)
qui margins, dydx(*) pred(pr) post
outreg2 using "${dirresu}\Appendix\TableC12_B.tex", keep(attend100) nocons label dec(3) coefastr 			///
		ctitle("probit") addtext(F-stat from first stage, ., % correctly predicted, `pcp1', Sargan test, ., ., ., Individual controls, YES, Marriage market controls, YES, Decade FE and trend, YES, years, 1851-75)	///
		tex replace
* wife in land class (acres), IV probit (1858-66)
qui ivregress 2sls landhoA $controlsA2 (attend100 = mourn) if myear>=1858 & myear<=1866 & landhoA!=., robust cluster(myear) first
	qui estat firststage
	matrix fist5 = r(singleresults)
	local F5 = round(fist5[1,4],1)
qui ivprobit landhoA $controlsA2 (attend100 = mourn) ///
	 if myear>=1858 & myear<=1866 & landhoA!=., vce(cluster myear)
qui estat classification
	local pcp2 = round(r(P_corr),1)
qui margins, dydx(*) pred(pr) post
outreg2 using "${dirresu}\Appendix\TableC12_B.tex", keep(attend100) nocons label dec(3) coefastr 			///
		ctitle("IV-probit") addtext(F-stat from first stage, `F5', % correctly predicted, `pcp2', Sargan test, ., ., ., Individual controls, YES, Marriage market controls, NO, Decade FE and trend, YES, years, 1858-66) tex	
* wife in land class (acres), (1851-75)
qui ivreg2 landhoA (attend100 = riskMin d1851 mourn) $controlsA1 if landhoA!=., liml robust cluster(myear) partial(t)
	local F6 = round(e(widstat),1)
	local J6 = round(e(j),0.01)
	local Jp6 = round(e(jp),0.01)
qui ivprobit landhoA $controlsA1 (attend100 = riskMin d1851 mourn) ///
	if landhoA!=., vce(cluster myear)
qui estat classification
	local pcp3 = round(r(P_corr),1)
qui margins, dydx(*) pred(pr) post
outreg2 using "${dirresu}\Appendix\TableC12_B.tex", keep(attend100) nocons label dec(3) coefastr 			///
		ctitle("IV-probit") addtext(F-stat from first stage, `F6', % correctly predicted, `pcp3', Sargan test, `J6', ., p=0`Jp6', Individual controls, YES, Marriage market controls, NO, Decade FE and trend, YES, years, 1851-75) tex	
* missmatch (acres), ols (1851-75)
qui reg missA attend100 $controlsA1 if missA!=., vce(cluster myear)
outreg2 using "${dirresu}\Appendix\TableC12_B.tex", keep(attend100) nocons label dec(3) coefastr nor2 		///
		ctitle("OLS") addtext(F-stat from first stage, ., % correctly predicted, ., Sargan test, ., ., ., Individual controls, YES, Marriage market controls, YES, Decade FE and trend, YES, years, 1851-75) tex
* missmatch (acres), IV (1858-66)
qui ivregress 2sls missA (attend100 = mourn) $controlsA2						///
	if myear>=1858 & myear<=1866 & missA!=., vce(cluster myear)
qui estat firststage
	matrix fist7 = r(singleresults)
	local F7 = round(fist7[1,4],1)
qui margins, dydx(*) post
outreg2 using "${dirresu}\Appendix\TableC12_B.tex", keep(attend100) nocons label dec(3) coefastr 		///
		ctitle("IV") addtext(F-stat from first stage, `F7', % correctly predicted, ., Sargan test, ., ., ., Individual controls, YES, Marriage market controls, NO, Decade FE and trend, YES, years, 1858-66) tex
* missmatch (acres), IV (1851-75)
qui ivreg2 missA (attend100 = riskMin d1851 mourn) $controlsA1 if missA!=., liml robust cluster(myear) partial(t)
	local F8 = round(e(widstat),1)
	local J8 = round(e(j),0.01)
	local Jp8 = round(e(jp),0.01)
qui ivregress 2sls missA (attend100 = riskMin d1851 mourn) $controlsA1						///
	if missA!=., vce(cluster myear)
qui margins, dydx(*) post
outreg2 using "${dirresu}\Appendix\TableC12_B.tex", keep(attend100) nocons label dec(3) coefastr 		///
		ctitle("IV") addtext(F-stat from first stage, `F8', % correctly predicted, ., Sargan test, `J8', ., p=0`Jp8', Individual controls, YES, Marriage market controls, YES, Decade FE and trend, YES, years, 1851-75) tex
		
* Panel C: Land rents
* ---------------------------------
* wife in land class (land rents), probit (1851-75)
qui probit landhoV attend100 $controlsV1 if landhoA!=., cluster(myear)
qui estat classification
	local pcp1 = round(r(P_corr),1)
qui margins, dydx(*) pred(pr) post
outreg2 using "${dirresu}\Appendix\TableC12_C.tex", keep(attend100) nocons label dec(3) coefastr 			///
		ctitle("probit") addtext(F-stat from first stage, ., % correctly predicted, `pcp1', Sargan test, ., ., ., Individual controls, YES, Marriage market controls, YES, Decade FE and trend, YES, years, 1851-75)	///
		tex replace
* wife in land class (land rents), IV probit (1858-66)
qui ivregress 2sls landhoV $controlsV2 (attend100 = mourn) if myear>=1858 & myear<=1866 & landhoA!=., robust cluster(myear)
	qui estat firststage
	matrix fist9 = r(singleresults)
	local F9 = round(fist9[1,4],1)
qui ivprobit landhoV $controlsV2 (attend100 = mourn) ///
	 if myear>=1858 & myear<=1866 & landhoA!=., vce(cluster myear)
qui estat classification
	local pcp2 = round(r(P_corr),1)
qui margins, dydx(*) pred(pr) post
outreg2 using "${dirresu}\Appendix\TableC12_C.tex", keep(attend100) nocons label dec(3) coefastr 			///
		ctitle("IV-probit") addtext(F-stat from first stage, `F9', % correctly predicted, `pcp2', Sargan test, ., ., ., Individual controls, YES, Marriage market controls, NO, Decade FE and trend, YES, years, 1858-66) tex	
* wife in land class (land rents), IV probit (1851-75)
qui ivreg2 landhoV $controlsV1 (attend100 = riskMin d1851 mourn) if landhoV!=., liml robust cluster(myear) partial(t)
	local F10 = round(e(widstat),1)
	local J10 = round(e(j),0.01)
	local Jp10 = round(e(jp),0.01)
qui ivprobit landhoV $controlsV1 (attend100 = riskMin d1851 mourn) ///
	 if landhoV!=., vce(cluster myear)
qui estat classification
	local pcp3 = round(r(P_corr),1)
qui margins, dydx(*) pred(pr) post
outreg2 using "${dirresu}\Appendix\TableC12_C.tex", keep(attend100) nocons label dec(3) coefastr 			///
		ctitle("IV-probit") addtext(F-stat from first stage, `F10', % correctly predicted, `pcp3', Sargan test, `J10', ., p=0`Jp10', Individual controls, YES, Marriage market controls, YES, Decade FE and trend, YES, years, 1851-75) tex
* missmatch (land rents), ols (1851-75)
qui reg missV attend100 $controlsV1 if missA!=., vce(cluster myear)
outreg2 using "${dirresu}\Appendix\TableC12_C.tex", keep(attend100) nocons label dec(3) coefastr nor2 		///
		ctitle("OLS") addtext(F-stat from first stage, ., % correctly predicted, ., Sargan test, ., ., ., Individual controls, YES, Marriage market controls, YES, Decade FE and trend, YES, years, 1851-75) tex
* missmatch (land rents), IV (1858-66)
qui ivregress 2sls missV (attend100 = mourn) $controlsV2						///
	if myear>=1858 & myear<=1866 & missA!=., vce(cluster myear)
qui estat firststage
	matrix fist11 = r(singleresults)
	local F11 = round(fist11[1,4],1)
qui margins, dydx(*) post
outreg2 using "${dirresu}\Appendix\TableC12_C.tex", keep(attend100) nocons label dec(3) coefastr 		///
		ctitle("IV") addtext(F-stat from first stage, `F11', % correctly predicted, ., Sargan test, ., ., ., Individual controls, YES, Marriage market controls, NO, Decade FE and trend, YES, years, 1858-66) tex
* missmatch (land rents), IV (1851-75)
qui ivreg2 missV (attend100 = riskMin d1851 mourn) $controlsV1 if missA!=., liml robust cluster(myear) partial(t)
	local F12 = round(e(widstat),1)
	local J12 = round(e(j),0.01)
	local Jp12 = round(e(jp),0.01)
qui ivregress 2sls missV (attend100 = riskMin d1851 mourn) $controlsV1						///
	if missA!=., vce(cluster myear)
qui margins, dydx(*) post
outreg2 using "${dirresu}\Appendix\TableC12_C.tex", keep(attend100) nocons label dec(3) coefastr 		///
		ctitle("IV") addtext(F-stat from first stage, `F12', % correctly predicted, ., Sargan test, `J12', ., p=0`Jp12', Individual controls, YES, Marriage market controls, YES, Decade FE and trend, YES, years, 1851-75) tex

* Panel D: Principal components analysis (PCA)
* --------------------------------------------
use "${dirdta}\final-data-men.dta", clear
keep if ATotal!=. & wom==0
global controlsPCA1 i.crank ATot VTot wood2 old relandV mage hengpee sexr railLength100 t dec50 dec60
global controlsPCA2 i.crank ATot VTot wood2 old relandV mage hengpee t dec50 dec60
label var attend100 "Attendees Season"

foreach x in "" s{
gen `x'old = 1 if `x'oldfamily=="yes"
replace `x'old = 0 if `x'oldfamily=="no"
gen `x'wood2 = 1 if `x'woods=="yes"
replace `x'wood2 = 0 if `x'woods=="no"
}
gen hengpeespo = (hpeeragespo==2)
gen hscopeespo = (hpeeragespo==3)
gen hiripeespo = (hpeeragespo==4)
global ses   crank 		ATotal  VTotal	hengpee 	hscotpee   hiripee    old  			wood2
global sespo prankspo	sATotal sVTotal	hengpeespo	hscopeespo hiripeespo sold heirspo 	swood2
gsort crank ATotal VTotal hengpee hscotpee hiripee old wood2 -refno
qui pca $ses
qui pca $ses, mineigen(1)
qui predict ses ses2 ses3 ses4 ses5 ses6 ses7, score
gsort prankspo sATotal sVTotal hengpeespo hscopeespo hiripeespo sold heirspo swood2 -refno
qui pca $sesppo
qui pca $sespo, mineigen(1)
qui predict sespo sespo2 sespo3 sespo4 sespo5 sespo6 sespo7, score
gsort ses -refno
gen ranking = _n if wom==0 & ses!=.
gsort sespo -refno
gen sranking = _n if wom==0 & sespo!=.
gen pcasort = abs(ranking-sranking)
* PCA missmatch, OLS (1851-75)
qui reg pcasort attend100 $controlsPCA1  if wom==0 & pcasort!=., cluster(myear)
outreg2 using "${dirresu}\Appendix\TableC12_D.tex", keep(attend100) nocons label dec(2) coefastr nor2	///
		ctitle("OLS") addtext(F-stat from first stage, ., Sargan test, ., ., ., Individual controls, YES, Marriage market controls, YES, Decade FE and trend, YES, years, 1851-75)	///
		tex replace
* PCA missmatch, IV (1858-66)
ivregress 2sls pcasort (attend100 = mourn) $controlsPCA2			///
	if myear>=1858 & myear<=1866 & wom==0 & pcasort!=., vce(cluster myear)
qui estat firststage
	matrix fist13 = r(singleresults)
	local F13 = round(fist13[1,4],1)
outreg2 using "${dirresu}\Appendix\TableC12_D.tex", keep(attend100) nocons label dec(2) coefastr 		///
		ctitle("IV") addtext(F-stat from first stage, `F13', Sargan test, ., ., ., Individual controls, YES, Marriage market controls, NO, Decade FE and trend, YES, years, 1858-66) tex
* PCA missmatch, IV (1851-75)
qui ivreg2 pcasort (attend100 = riskMin d1851 mourn) $controlsPCA1 if wom==0 & pcasort!=., liml robust cluster(myear) partial(t)
	local F14 = round(e(widstat),1)
	local J14 = round(e(j),0.01)
	local Jp14 = round(e(jp),0.01)
outreg2 using "${dirresu}\Appendix\TableC12_D.tex", keep(attend100) nocons label dec(2) coefastr 		///
		ctitle("IV") addtext(F-stat from first stage, `F14', Sargan test, `J14', ., p=0`Jp14', Individual controls, YES, Marriage market controls, YES, Decade FE and trend, YES, years, 1851-75) tex

* Panel E: Geographic endogamy
* --------------------------------------------
use "${dirdta}\final-data-men.dta", clear
keep if dist!=. & wom==0
global controlsG1 i.crank reldiv mage wom hengpee sexr railLength100 t dec50 dec60
global controlsG2 i.crank reldiv mage wom hengpee t dec50 dec60
label var attend100 "Attendees Season"
* Wife < 100 miles, probit (1851-75)
qui probit disthom attend100 $controlsG1, vce(cluster myear)
qui estat classification
	local pcp15 = round(r(P_corr),1)
qui margins, dydx(*) pred(pr) post
outreg2 using "${dirresu}\Appendix\TableC12_E.tex", keep(attend100) nocons label dec(3) coefastr 		///
		ctitle("probit") addtext(F-stat from first stage, ., % correctly predicted, `pcp15', Sargan test, ., ., ., Individual controls, YES, Marriage market controls, YES, Decade FE and trend, YES, years, 1851-75)	///
		tex replace
* Wife < 100 miles, IV probit (1858-66)
qui ivregress 2sls disthom $controlsG2 (attend100 = mourn) if myear>=1858 & myear<=1866, robust cluster(myear)
	qui estat firststage
	matrix fist16 = r(singleresults)
	local F16 = round(fist16[1,4],1)
qui ivprobit disthom $controlsG2 (attend100 = mourn) ///
	 if myear>=1858 & myear<=1866, vce(cluster myear)
qui estat classification
	local pcp16 = round(r(P_corr),1)
qui margins, dydx(*) pred(pr) post
outreg2 using "${dirresu}\Appendix\TableC12_E.tex", keep(attend100) nocons label dec(3) coefastr 			///
		ctitle("IV-probit") addtext(F-stat from first stage, `F16', % correctly predicted, `pcp16', Sargan test, ., ., ., Individual controls, YES, Marriage market controls, NO, Decade FE and trend, YES, years, 1851-75) tex	
* Wife < 100 miles, IV probit (1851-75)
qui ivreg2 disthom $controlsG1 (attend100 = riskMin mourn d1851), liml robust cluster(myear) partial(t)
	local F17 = round(e(widstat),1)
	local J17 = round(e(j),0.01)
	local Jp17 = round(e(jp),0.01)
qui ivprobit disthom $controlsG1 (attend100 = riskMin mourn d1851) ///
	 , vce(cluster myear)
qui estat classification
	local pcp17 = round(r(P_corr),1)
qui margins, dydx(*) pred(pr) post
outreg2 using "${dirresu}\Appendix\TableC12_E.tex", keep(attend100) nocons label dec(3) coefastr 			///
		ctitle("IV-probit") addtext(F-stat from first stage, `F17', % correctly predicted, `pcp17', Sargan test, `J17', ., p=0`Jp17', Individual controls, YES, Marriage market controls, NO, Decade FE and trend, YES, years, 1858-66) tex	
* Distance, OLS (1851-75)
qui reg dist attend100 $controlsG1, cluster(myear)
outreg2 using "${dirresu}\Appendix\TableC12_E.tex", keep(attend100) nocons label dec(3) coefastr nor2	///
		ctitle("OLS") addtext(F-stat from first stage, ., Sargan test, ., ., ., Individual controls, YES, Marriage market controls, YES, Decade FE and trend, YES, years, 1851-75) tex
* Distance, IV (1858-66)
ivregress 2sls dist (attend100 = mourn) $controlsG2			///
	if myear>=1858 & myear<=1866, vce(cluster myear)
qui estat firststage
	matrix fist18 = r(singleresults)
	local F18 = round(fist18[1,4],1)
outreg2 using "${dirresu}\Appendix\TableC12_E.tex", keep(attend100) nocons label dec(3) coefastr 		///
		ctitle("IV") addtext(F-stat from first stage, `F18', Sargan test, ., ., ., Individual controls, YES, Marriage market controls, NO, Decade FE and trend, YES, years, 1858-66) tex
* Distance, IV (1851-75)
qui ivreg2 dist (attend100 = riskMin d1851 mourn) $controlsG1, liml robust cluster(myear) partial(t)
	local F19 = round(e(widstat),1)
	local J19 = round(e(j),0.01)
	local Jp19 = round(e(jp),0.01)
outreg2 using "${dirresu}\Appendix\TableC12_E.tex", keep(attend100) nocons label dec(3) coefastr 		///
		ctitle("IV") addtext(F-stat from first stage, `F19', Sargan test, `J19', ., p=0`Jp19', Individual controls, YES, Marriage market controls, YES, Decade FE and trend, YES, years, 1851-75) tex



********************************************************************************
********************************************************************************
********************************************************************************
*																			   *
*	 					D. ADDITIONAL DESCRIPTIVE EVIDENCE					   *
*																			   *
********************************************************************************
********************************************************************************
********************************************************************************



* ==============================================================================
* FIGURE D1: 
* Top wealth-holders by source of wealth.
* ------------------------------------------------------------------------------


* Rubinstein data

	/* Note:  This data is from Rubinstein, W. D. 1977. "Wealth, Elites and the 

	Class Structure of Modern Britain." Past and Present, no. 76: pp. 99-126.  */

clear
input str7 period t	manu	nmanu	food	nfood	comm	ncomm	prof	nprof	land	nland	wealth
1809-58	1	2.631578947	5	0	0	1.578947368	3	0.526315789	1	95.26315789	181	1
1858-79	2	8.843537415	13	0.680272109	1	10.88435374	16	0	0	79.59183673	117	1
1880-99	3	22.68041237	22	14.43298969	14	23.71134021	23	0	0	39.17525773	38	1
1900-14	4	20	20	14	14	38	38	1	1	27	27	1
1809-58	1	2.770780856	11	0.251889169	1	7.052896725	28	2.01511335	8	87.9093199	349	2
1858-79	2	12.03007519	32	0.751879699	2	22.55639098	60	2.631578947	7	62.03007519	165	2
1880-99	3	20.33898305	60	7.796610169	23	22.37288136	66	3.050847458	9	46.44067797	137	2
1900-14	4	22.60536398	59	8.429118774	22	34.86590038	91	3.448275862	9	30.651341	80	2
1809-58	1	2.72572402	16	0.170357751	1	5.28109029	31	1.533219761	9	90.28960818	530	3
1858-79	2	10.89588378	45	0.726392252	3	18.40193705	76	1.694915254	7	68.28087167	282	3
1880-99	3	20.91836735	82	9.43877551	37	22.70408163	89	2.295918367	9	44.64285714	175	3
1900-14	4	21.88365651	79	9.972299169	36	35.73407202	129	2.770083102	10	29.6398892	107	3
end
gen manufood = manu+food
gen nmanufood = nmanu+nfood
label var manu "Manufacture"
label var nmanu "Manufacture (#)"
label var manufood "Manufacture, food, drink, tobacco"
label var nmanufood "Manufacture, food, drink, tobacco (#)"
label var food "Food, drink, tobacco"
label var nfood "Food, drink, tobacco (#)"
label var comm "Commercial"
label var ncomm "Commercial (#)"
label var prof "Professional, public administration, defence"
label var nprof "Professional, public administration, defence (#)"
label var land "Land"
label var nland "Land (#)"
label var wealth "Probate wealth category"
label define wlab 1 "millionaire" 2 "half-millionaire" 3 "total"
label values wealth wlab
label define tlab 1 "1809-58" 2 "1858-79" 3 "1880-99" 4 "1900-14"
label values t tlab
label var t "Time period"
drop period


* Figure

replace manufood = 4 if t==1	// for the sake of clarity
replace manufood = 8 if t==2
twoway 	(connected manufood t if wealth==1, mcolor(black) lcolor(black) msymbol(diamond_hollow) jitter(0 0)) 	///
		(connected comm t if wealth==1, mcolor(black) lcolor(black) msymbol(circle) jitter(1 1)) 	///
		(connected prof t if wealth==1, mcolor(black) lcolor(black) msymbol(diamond) jitter(1 1)) 	///
		(connected land t if wealth==1, mcolor(black) lcolor(black) msymbol(triangle)), 	///
		xlabel(1 "1809-58" 2 "1858-79" 3 "1880-99" 4 "1900-14") ylabel(0(20)100) 	///
		ytitle(% of Millionaires) xtitle("") graphregion(color(white)) legend(size(small))
graph export "${dirresu}\Appendix\FigureD1.pdf", as(pdf) replace



* ==============================================================================
* TABLE D1: 
* Top wealth-holders by source of wealth.
* ------------------------------------------------------------------------------


	/* Note: For the US top 1 percent, see references in the table notes. 

	Specifically, for rows 1 and 2 see the Statistics of Income division of the 

	Internal Revenue Center in 2005 (Bakija et al. 2012: Table 4). Rows 3 and 4 

	consider households surveyed in the Gallup polls in 2009-11 with incomes of 

	at least $516,633. Row 4 uses households in the top 1 percent from the 2010 

	Survey of Consumer Finances (Wolff 2013).  */
	
local UStop1row1 = 12.5
local UStop1row2 = 24
local UStop1row3 = 20
local UStop1row4 = 39
local UStop1row5 = 1300

	
* Import data

use "${dirdata}\final-data.dta", clear


* Political clubs

replace club2 = "St. James'" 		if club2=="St. Jas."
replace club3 = "St. James'" 		if club3=="St. Jas."
replace club3 = "St. Stephen's" 	if club3=="Stephen's"
replace club4 = "Beaconsfield" 		if club4=="Beacsfd."
replace club7 = "United Service" 	if club7=="U. Serv."
replace sclub = "Carlton" 			if sclub=="Carlton'"
replace sclub = "Marlborough"		if sclub=="Marlborough'"
replace sclub2 = "Army and Navy'"	if sclub2=="Army and Navy'"
replace sclub2 = "Brooks's"			if sclub2=="Brooks'"
replace sclub2 = "Conservative"		if sclub2=="Conservative'"
replace sclub2 = "Naval and Military" if sclub2=="N. And M."
replace sclub2 = "St. James'"		if sclub2=="St. Jas."
replace sclub2 = "White's"			if sclub2=="White's'"
replace sclub3 = "Dev."				if sclub3=="Devonshire"
replace sclub3 = "Guards"			if sclub3=="Guard"
replace sclub3 = "St. James's"		if sclub3=="St. Jas."
replace sclub3 = "Turf"				if sclub3=="Turf'"
replace sclub4 = "Beaconsfield"		if sclub4=="Beacsfd."
foreach x in "" 2 3 4 5 6 7 {
replace sclub`x' = "Army and Navy"  if sclub`x'=="Army and Navy'"
replace sclub`x' = "Devonshire"	 	if sclub`x'=="Dev."
replace sclub`x' = "Gun." 			if sclub`x'=="Gun"
replace sclub`x' = "New Edinburgh" 	if sclub`x'=="New Edinborough"
replace sclub`x' = "St. James'" 	if sclub`x'=="St. James's"
}


* Conservative vs liberal clubs

foreach s in "" s{
gen str60 `s'polit = ""
foreach x in "" 2 3 4 5 6 7{
replace `s'polit = "Tory" 	 if `s'club`x'=="Carlton"  | `s'club`x'=="Jun. Carlton" | `s'club`x'=="Conservative" | `s'club`x'=="St. Stephen's"
replace `s'polit = "Liberal" if `s'club`x'=="Brooks's" | `s'club`x'=="Devonshire"   | `s'club`x'=="Reform"
}
}
replace polit = "none" if polit==""


* Results matrix

mat def tabD1 = (`UStop1row1' , . \ `UStop1row2' , . \ `UStop1row3' , . \ `UStop1row4' , . \ `UStop1row5' , . \ . , . )
mat l tabD1

	/* Note: For the US top 1 percent, see references in the table notes: 

	Statistics of Income division of the Internal Revenue Center in 2005 (Bakija

	et al. 2012: Table 4); Gallup polls in 2009-11; 2010 Survey of Consumer 

	Finances (Wolff 2013).  */

	
* British peerage (% not married)

preserve
keep if byear>=1820 & byear<=1845 & dage>35 & nthismarr<2 & wom==0 
tab nthismarr
local tot = `r(N)'
tab nthismarr if nthismarr==0
local nm = `r(N)'
mat tabD1[1,2]=round(100*`nm'/`tot',0.1)
restore


* British peerage (% spouse is a peer's daughter')

preserve
keep if myear>=1851 & myear<=1875 & nthismarr==1 & wom==0
tab cOut
local tot = `r(N)'
tab cOut if cOut==0
local mp = `r(N)'
mat tabD1[2,2]=round(100*`mp'/`tot',0.1)
restore


* British peerage (% member of a liberal club)

preserve
keep if myear>=1851 & myear<=1875 & acrestotal!=. & nthismarr==1 & wom==0
tab polit
local tot = `r(N)'
tab polit if polit=="Liberal"
local lib = `r(N)'
mat tabD1[3,2]=round(100*`lib'/`tot',0.1)
restore


* British peerage (% member of a tory club)

preserve
keep if myear>=1851 & myear<=1875 & acrestotal!=. & nthismarr==1 & wom==0
tab polit
local tot = `r(N)'
tab polit if polit=="Tory"
local tor = `r(N)'
mat tabD1[4,2]=round(100*`tor'/`tot',0.1)
restore


* British peerage (average annual income)

preserve
keep if myear>=1851 & myear<=1875 & acrestotal!=. & nthismarr==1 & wom==0
gen VTotalk = valuetotal/1000
summ VTotalk
mat tabD1[5,2] = round(`r(mean)',0.1)
mat tabD1[6,2] = 2500
restore

mat coln tabD1 = "US top 1 percent" "British peerage"
mat rown tabD1 = "Not married (\%)" "Spouse in top-notch job | is peer daughter (\%)" "Liberal | Member of a liberal club (\%)" "Conservative | Member of a tory club (\%)" "Av annual income dollars | Rents from land 1870 pounds (k)"  "Rents from land (2008 dollar, k)"
*

esttab matrix(tabD1, fmt(1)) using "${dirresu}\Appendix\TableD1.tex", mlabel("Table D1") replace


* clear
erase "${dirdta}/final-data-sec4-robust1.dta"
erase "${dirdta}/final-data-sec4-robust2.dta"
***erase "${dirdta}\final-data-men.dta"



********************************************************************************
********************************************************************************
********************************************************************************
*																			   *
*	 					E. CONCEPTUAL FRAMEWORK APPENDIX					   *
*																			   *
********************************************************************************
********************************************************************************
********************************************************************************



* ==============================================================================
* FIGURE E1: 
* Comparative statics on search costs.
* ------------------------------------------------------------------------------


	/* Note: This figure is based on simulating the model described in Appendix 

	E. Specifically, the equilibrium class bounds (i.e., the reservation 

	strategies) are calculated using Proposition 1, equations E8 and E9.  */
	
	
clear
set obs 1000
gen x = _n/1000


* Parameters
* ----------

* Baseline:
local beta_A = 0.8
local lambda_A = 1
local alpha_A = 0.5

* High alpha:
local beta_B = 0.8
local lambda_B = 1
local alpha_B = 1.0

* High lambda:
local beta_C = 0.8
local lambda_C = 1.5
local alpha_C = 0.5


* Equilibrium class bounds:
* -------------------------
	* Class bounds (i.e., reservation strategies) calculated using Proposition 1

* Baseline equilibrium:
local A0 = 1
local delta_A = (`beta_A'/(1-`beta_A'))*`alpha_A'*(`lambda_A^2')
local A1 = (1+`delta_A'*`A0'-sqrt(1+2*`delta_A'*`A0'))/`delta_A'
local A2 = (1+`delta_A'*`A1'-sqrt(1+2*`delta_A'*`A1'))/`delta_A'
local A3 = (1+`delta_A'*`A2'-sqrt(1+2*`delta_A'*`A2'))/`delta_A'
local A4 = (1+`delta_A'*`A3'-sqrt(1+2*`delta_A'*`A3'))/`delta_A'

* High alpha equilibrium:
local B0 = 1
local delta_B = (`beta_B'/(1-`beta_B'))*`alpha_B'*(`lambda_B^2')
local B1 = (1+`delta_B'*`B0'-sqrt(1+2*`delta_B'*`B0'))/`delta_B'
local B2 = (1+`delta_B'*`B1'-sqrt(1+2*`delta_B'*`B1'))/`delta_B'
local B3 = (1+`delta_B'*`B2'-sqrt(1+2*`delta_B'*`B2'))/`delta_B'
local B4 = (1+`delta_B'*`B3'-sqrt(1+2*`delta_B'*`B3'))/`delta_B'

* High lambda equilibrium:
local C0 = 1
local delta_C = (`beta_C'/(1-`beta_C'))*`alpha_C'*(`lambda_C^2')
local C1 = (1+`delta_C'*`C0'-sqrt(1+2*`delta_C'*`C0'))/`delta_C'
local C2 = (1+`delta_C'*`C1'-sqrt(1+2*`delta_C'*`C1'))/`delta_C'
local C3 = (1+`delta_C'*`C2'-sqrt(1+2*`delta_C'*`C2'))/`delta_C'
local C4 = (1+`delta_C'*`C3'-sqrt(1+2*`delta_C'*`C3'))/`delta_C'


* Figure:
* -------
foreach z in A B C{
gen hi_`z' = ``z'0' if x>=``z'1'
replace hi_`z' = ``z'1' if x>=``z'2' & x<``z'1'
replace hi_`z' = ``z'2' if x>=``z'3' & x<``z'2'
replace hi_`z' = ``z'3' if x>=0 & x<``z'3'
gen lo_`z' = ``z'1' if x>=``z'1'
replace lo_`z' = ``z'2' if x>=``z'2' & x<``z'1'
replace lo_`z' = ``z'3' if x>=``z'3' & x<``z'2'
replace lo_`z' = ``z'4' if x>=0 & x<``z'3'
}
twoway 	(rarea hi_A lo_A x, fc(none) lc(black) lp(dash)) ///
		(rarea hi_B lo_B x, fc(none) lc(blue)), ///
		legend(order(1 "beta = `beta_A'; lambda = `lambda_A'; alpha = `alpha_A'" 2 "beta = `beta_B'; lambda = `lambda_B'; alpha = `alpha_B'") row(2) lc(none)) ///
		title("Panel A: Encounter speed (alpha)", size(medsmall) color(black)) aspect(1) ///
		xtitle("x") ytitle("y") graphregion(color(white)) name(PanelA, replace)
twoway 	(rarea hi_A lo_A x, fc(none) lc(black) lp(dash)) ///
		(rarea hi_C lo_C x, fc(none) lc(red)), ///
		legend(order(1 "beta = `beta_A'; lambda = `lambda_A'.0; alpha = `alpha_A'" 2 "beta = `beta_C'; lambda = `lambda_C'; alpha = `alpha_C'") row(2) lc(none)) ///
		title("Panel B: Num of attendees (lambda)", size(medsmall) color(black)) ///
		aspect(1) xtitle("x") ytitle("y") graphregion(color(white)) name(PanelB, replace)
graph combine PanelA PanelB, graphregion(color(white))
graph export "${dirresu}\Appendix\FigureE1.pdf", as(pdf) replace



* ==============================================================================
* FIGURE E2: 
* Comparative statics on market segmentation.
* ------------------------------------------------------------------------------


	/* Note: This figure is based on simulating the model described in Appendix 

	E. Specifically, the equilibrium class bounds (i.e., the reservation 

	strategies) are calculated using Proposition 1, equations E8 and E9. For the 

	model with market segregation, the equilibrium classes are derived in page 

	86 of the online appendix */
	
	
clear
set obs 1000
gen x = _n/1000


* Parameters
* ----------

* Baseline:
local beta_A = 0.8
local lambda_A = 1
local alpha_A = 1
local z_A = 0

* Market segmentation (z=0.24):
local beta_B = 0.8
local lambda_B = 1
local alpha_B = 1
local z_B = 0.24


* Equilibrium class bounds:
* -------------------------
	* Class bounds (i.e., reservation strategies) calculated using Proposition 1
	* and the eq. reservation strategies derived in page 86 of the online appendix.

* Baseline equilibrium:
local A0 = 1
local coef1 = (((1-`z_A')^2) * sqrt(1-`beta_A')) / (`beta_A'*`alpha_A'*(1-`z_A')*(`lambda_A'^1.1))
local coef2 = `beta_A'*`alpha_A'*(1-`z_A')*(`lambda_A'^(1.1)) / ((1-`z_A')^2)
local A1 = `A0' - `coef1' * (sqrt(1-`beta_A'+2*`A0'*`coef2') - sqrt(1-`beta_A'))
local A2 = `A1' - `coef1' * (sqrt(1-`beta_A'+2*`A1'*`coef2') - sqrt(1-`beta_A'))
local A3 = `A2' - `coef1' * (sqrt(1-`beta_A'+2*`A2'*`coef2') - sqrt(1-`beta_A'))
local A4 = `A3' - `coef1' * (sqrt(1-`beta_A'+2*`A3'*`coef2') - sqrt(1-`beta_A'))

* Market segmentation equilibrium:
local B0 = 1
local coef1 = (((1-`z_B')^2) * sqrt(1-`beta_B')) / (`beta_B'*`alpha_B'*(1-`z_B')*(`lambda_B'^1.1))
local coef2 = `beta_B'*`alpha_B'*(1-`z_B')*(`lambda_B'^(1.1)) / ((1-`z_B')^2)
local B1 = `B0' - `coef1' * (sqrt(1-`beta_B'+2*`B0'*`coef2') - sqrt(1-`beta_B'))
local B2 = `B1' - `coef1' * (sqrt(1-`beta_B'+2*`B1'*`coef2') - sqrt(1-`beta_B'))
local B3 = `B2' - `coef1' * (sqrt(1-`beta_B'+2*`B2'*`coef2') - sqrt(1-`beta_B'))
local B4 = `B3' - `coef1' * (sqrt(1-`beta_B'+2*`B3'*`coef2') - sqrt(1-`beta_B'))


* Figure:
* -------
foreach q in A B{
gen hi_`q' = ``q'0' if x>=``q'1'
replace hi_`q' = ``q'1' if x>=``q'2' & x<``q'1'
replace hi_`q' = ``q'2' if x>=``q'3' & x<``q'2'
replace hi_`q' = ``q'3' if x>=0 & x<``q'3'
gen lo_`q' = ``q'1' if x>=``q'1'
replace lo_`q' = ``q'2' if x>=``q'2' & x<``q'1'
replace lo_`q' = ``q'3' if x>=``q'3' & x<``q'2'
replace lo_`q' = ``q'4' if x>=0 & x<``q'3'
* mark those excluded from the market as missing values:
replace hi_`q' = . if x<`z_`q''
replace lo_`q' = . if x<`z_`q''
}

twoway 	(rarea hi_A lo_A x, fc(none) lc(black) lp(dash)) ///
		(rarea hi_B lo_B x, fc(none) lc(blue)), ///
		xline(`z_B', lpattern(dot) lc(blue)) yline(`z_B', lpattern(dot) lc(blue)) ///
		legend(order(1 "beta = `beta_A'; lambda = `lambda_A'; alpha = `alpha_A'; z = `z_A'" 2 "beta = `beta_B'; lambda = `lambda_B'; alpha = `alpha_B'; z = `z_B'") row(2) lc(none)) ///
		aspect(1) xtitle("x") ytitle("y") graphregion(color(white))
graph export "${dirresu}\Appendix\FigureE2.pdf", as(pdf) replace