version 9.0
#delimit;
set more off;
log using montecarlo-dyads02, replace text;

/*	************************************************************************	*/
/*     	File Name:	montecarlo-dyads02.do						*/
/*     	Date:   	April 13, 2006 							*/
/*      Author: 	Frederick J. Boehmke						*/
/*      Purpose:	Test directed dyad approach to state policy adoption. 	 	*/
/*			This file uses state01 variables only.				*/
/*      Input File:	statedata1975-2002.dta					 	*/
/*      Output File:	montecarlo-dyads02.log, montecarlo-dyads02.dta 			*/
/*	Previous File:	None.								*/
/*	************************************************************************	*/

	/********************************************************/
	/* This program generates the dependent variable in 	*/
	/* the original state data set.				*/
	/********************************************************/

program define depvar;

  syntax, demcoeff(real);

  generat u = uniform();
  generat e = log(u/(1-u));

  generat ystar = -4 + 0.1*rpcpinc + e;

  generat y = (ystar >= 0);

  tsset stateno year;

  generat adopt = y;
  replace adopt = . if l.adopt == 1 & stateno==l.stateno;
  replace adopt = . if l.adopt == . & stateno==l.stateno;

  generat adopted = 0;
  replace adopted = 1 if adopt==1;
  replace adopted = 1 if l.adopted==1 & stateno==l.stateno;

  generat adopted_lag = 0;
  replace adopted_lag = 1 if adopt==. & stateno==l.stateno;

end;


	/********************************************************/
	/* This program takes the original state data set and 	*/
	/* creates the directed dyad data set.			*/
	/********************************************************/

program define mkdyads;

  save montecarlo-dyads, replace;

  	foreach var of varlist _all {;

	  rename `var' `var'02;

	  };

  	rename year02 year;
  	rename time02 time;

  	sort year stateno02;

  	save montecarlo-dyads-state02, replace;

  use montecarlo-dyads, clear;

	expand 50;

  	foreach var of varlist _all {;

	  rename `var' `var'01;

	  };

  	rename year01 year;
  	rename time01 time;

  	sort year stateno01;
  	generate stateno02 = 1;

  quietly by year stateno01: replace stateno02 = stateno02[_n-1] + 1 
	if stateno01==stateno01[_n-1] & year==year[_n-1];

  sort year stateno02;

  joinby year stateno02 using montecarlo-dyads-state02;

  	generat emulate = 0 if year > 1975;
  	replace emulate = 1 if adopt01 == 1 & adopted02 == 1 & adopt02==.; 
  	replace emulate = . if adopt01 == .;

	generat emul_opp = 0 if year > 1975;
  	replace emul_opp = 1 if adopted02 == 1 & adopt02==.; 

  	foreach var of newlist rpcpinc {;

  	  generat `var'_jnt = `var'01*`var'02;
  	  generat `var'02_emul = `var'02*emul_opp;
  	  generat `var'_jnt_emul = `var'_jnt*emul_opp;

	  };

  save montecarlo-dyads, replace;

end;

	/********************************************************/
	/* This program runs the monte carlo analysis and  	*/
	/* returns the coefficient estimates.			*/
	/********************************************************/

program define emulate, rclass;

  version 9;
  syntax, demcoeff(real);

  use montecarlo-dyads-indvars, clear;

  	depvar, demcoeff(`demcoeff');

  	mkdyads;

  logit emulate rpcpinc01 if state01!=state02, cluster(state01);


  return scalar b_rpcpinc01 	= _b[rpcpinc01];


  return scalar se_rpcpinc01 	= _se[rpcpinc01];


end;


use statedata1975-2002;



  keep stateno state year rpcpinc totpop;

  drop if year < 1975 | year > 2000;

  generat time = year - 1970;

  save montecarlo-dyads-indvars, replace;

simulate 
	b_rpcpinc01=_b[rpcpinc01] 

	se_rpcpinc01=_se[rpcpinc01]
	
	numobs = e(N),

	reps(1000) seed(54) saving(montecarlo-dyads02-sims, replace): emulate, demcoeff(1);



  generat t_rpcpinc01 	= b_rpcpinc01 		/ se_rpcpinc01;

  local crit = 1.96;


  generat sig_rpcpinc01 	= sign(t_rpcpinc01) 	* (abs(t_rpcpinc01) 	>= `crit');

  summarize;

  tab1 sig_*;

  save montecarlo-dyads02-sims, replace;

log close;
clear;
exit;		