* This file generates findings for Manuscript AEA PandP2025-1091 "Metacognitive Awareness and Academic Performance" according to AEARCTR-0004840. 
* Email questions or comments to Angelino Viceisza at aviceisz@spelman.edu

clear
set more off

****************************************************
* Set Working Directory                            *
****************************************************

cd "/Users/aviceisz/Documents/my documents/my documents (spelman)/research/doe fitw/analysis"

****************************************************
* Load Data                                        *
****************************************************

use "Deidentified/AEAPP.dta", clear

****************************************************
* General Descriptive Statistics                   *
****************************************************

foreach var in fall_pre_reg fall_pre_mai_overreg fall_pre_mai_overknow high_school_reported_gpa sat_act_combined pelleligible ///
			   parent_college maj_stem maj_soc_sci maj_lang maj_human maj_arts ///
			   west midwest northeast south two_parent racially_diverse_hs ///
			   public_hs age_sept_freshman {
			   
sum `var'
matrix `var' = r(mean), r(sd), r(N)
matrix rownames `var' = `var'			   
}

matrix describe = fall_pre_reg \ fall_pre_mai_overreg \ fall_pre_mai_overknow \ high_school_reported_gpa \ sat_act_combined \ pelleligible \ ///
			   parent_college \  ///
			   west \ midwest \ northeast \ south \ two_parent \ racially_diverse_hs \ ///
			   public_hs \ age_sept_freshman \ maj_stem \ maj_soc_sci \ maj_lang \ maj_human \ maj_arts 
			   
frmttable using "Analysis/Output/StudentSum Stats", replace statmat(describe) tex ///landscape /// landscape
	title("Student Summary Statistics (Pre Characteristics)") coljust(l; r)  ///
	ctitles(Description ,Mean, Standard Deviation, Observations) varlabels /// basefont(fs10) titlfont(fs11 b) sdec(0, 2, 2, 2 ,2) 

preserve
	
foreach var in instructor_phd instructor_years instructor_spelyrs instructor_adwtimes instructor_full instructor_tenure ///
				 tch_mai_decl tch_mai_proc tch_mai_cond tch_mai_plan ///
				 tch_mai_info tch_mai_comp tch_mai_debg ///
				 tch_mai_eval tch_mai_overknow tch_mai_overreg{
			   
sum `var'
matrix `var' = r(mean), r(sd), r(N)
matrix rownames `var' = `var'
		}
matrix describe = tch_mai_overknow \ tch_mai_overreg \ instructor_phd \ instructor_full \ instructor_tenure \ instructor_adwtimes
				 
				 
frmttable using "Analysis/Output/InstructorSumStats", replace statmat(describe) fr tex ///landscape /// landscape
	title("Instructor Summary Statistics (Pre Characteristics)") coljust(l; r)  ///
	ctitles(Description ,Mean, Standard Deviation, Observations) varlabels /// basefont(fs10) titlfont(fs11 b) sdec(0, 2, 2, 2 ,2) 
	
restore	

foreach var in spring_reg spring_mai_overknow spring_mai_overreg spring_adw_grade fr_spring_gpa ///
				fr_spring_gpa_excl_adw persist_sophomore_fall {
			   
sum `var'
matrix `var' = r(mean), r(sd), r(N)
matrix rownames `var' = `var'			   
}

matrix describe = spring_reg \ spring_mai_overknow \ spring_mai_overreg \ spring_adw_grade \ fr_spring_gpa \ ///
				fr_spring_gpa_excl_adw \ persist_sophomore_fall	
			   
frmttable using "Analysis/Output/OutcomeSumStats.rtf", replace statmat(describe) ///landscape /// landscape
	title("Student Summary Statistics (Outcomes)") coljust(l; r)  ///
	ctitles(Description ,Mean, Standard Deviation, Observations) varlabels 


****************************************************
* RCT 1 - Balance Check (Student)                  *
****************************************************
preserve
keep if cohort == 1 | cohort == 2

foreach var in fall_pre_reg fall_pre_mai_overreg fall_pre_mai_overknow high_school_reported_gpa sat_act_combined pelleligible ///
			   parent_college maj_stem maj_soc_sci maj_lang maj_human maj_arts ///
			   west midwest northeast south two_parent racially_diverse_hs ///
			   public_hs age_sept_freshman {
			   
	mean `var'
	matrix n`var'=e(_N)	// number of observations 
	matrix m0`var'=e(b)	// overall mean 
	mean `var' if treatment == 0 
	matrix mc`var'=e(b)
	mean `var' if treatment == 1 
	matrix mt`var'=e(b)
	reg `var' treatment, cluster(rand_instructor)
	matrix d`var' = _b[treatment]
	matrix se`var' = _se[treatment]
	matrix t`var'= _b[treatment]/_se[treatment]
	matrix `var'=n`var', m0`var', mt`var', mc`var', d`var', se`var', t`var'
	matrix g`var' = d`var', d`var'-(1.96*se`var'),d`var'+(1.96*se`var')
	matrix rownames `var'=`var'
	matrix rownames g`var'=`var'
		}
		
matrix balance1 = fall_pre_reg \ fall_pre_mai_overreg \ fall_pre_mai_overknow \ high_school_reported_gpa \ sat_act_combined \ pelleligible \ ///
			   parent_college \ maj_stem \ maj_soc_sci \ maj_lang \ maj_human \ maj_arts \ ///
			   west \ midwest \ northeast \ south \ two_parent \ racially_diverse_hs \ ///
			   public_hs \ age_sept_freshman 
			   
matrix graph = 	gfall_pre_reg \ gfall_pre_mai_overreg \ gfall_pre_mai_overknow \ ghigh_school_reported_gpa \ gsat_act_combined \ gpelleligible \ ///
			   gparent_college \ ///
			   gwest \ gmidwest \ gnortheast \ gsouth \ gtwo_parent \ gracially_diverse_hs \ ///
			   gpublic_hs \ gage_sept_freshman \ gmaj_stem \ gmaj_soc_sci \ gmaj_lang \ gmaj_human \ gmaj_arts 	   
			   
frmttable using "Analysis/Output/RCT1StudentDescriptives.rtf", replace statmat(balance1) ///landscape /// landscape
	title("Balance of Student Pre-characteristics Across Condition (Control vs. Treatment)") coljust(l; r)  ///
	ctitles(Description ,N, Mean(Overall), Mean(Treatment), Mean(Control), Difference, Std. Error, T-stat) varlabels /// basefont(fs10) titlfont(fs11 b) sdec(0, 2, 2, 2 ,2) 
	note("Note: the table includes students from Cohort 1 and Cohort 2, the students arrived at Spelman in the falls of 2016 and 2017, respectively.")

	
*Present Visually	
coefplot (matrix(graph[,1]), ci((graph[,2] graph[,3]))), xline(0) ///
		headings(fall_pre_reg = "{bf:Metacognition Measures}" high_school_reported_gpa = "{bf:Academic Measures}" ///
		pelleligible = "{bf:Background Characteristics}" maj_stem = "{bf:Student Interests}", labcolor(orange)) scheme(s1mono) xtitle("Coefficient and Confidence Interval")

graph export "Analysis/Output/RCT1StudentBalance.png", replace width(800) height(600)	
restore
	
****************************************************
* RCT 1 - Balance Check Faculty                    *
****************************************************	

preserve

keep if cohort == 1 | cohort == 2
	
foreach var in instructor_phd instructor_years instructor_spelyrs instructor_adwtimes instructor_full instructor_tenure ///
				 tch_mai_decl tch_mai_proc tch_mai_cond tch_mai_plan ///
				 tch_mai_info tch_mai_comp tch_mai_debg ///
				 tch_mai_eval tch_mai_overknow tch_mai_overreg{
			   
	mean `var'
	matrix n`var'=e(_N)	// number of observations 
	matrix m0`var'=e(b)	// overall mean 
	mean `var' if treatment == 0 
	matrix mc`var'=e(b)
	mean `var' if treatment == 1 
	matrix mt`var'=e(b)
	reg `var' treatment, cluster(rand_instructor)
	matrix d`var' = _b[treatment]
	matrix se`var' = _se[treatment]
	matrix t`var'= _b[treatment]/_se[treatment]
	matrix `var'=n`var', m0`var', mt`var', mc`var', d`var', se`var', t`var'
	matrix g`var' = d`var', d`var'-(1.96*se`var'),d`var'+(1.96*se`var')
	matrix rownames `var'=`var'
	matrix rownames g`var'=`var'
		}
matrix balance1 = tch_mai_overknow \ tch_mai_overreg \ instructor_phd \ instructor_full \ instructor_tenure \ instructor_adwtimes

matrix graph = 	gtch_mai_overknow \ gtch_mai_overreg \ ginstructor_phd \ ginstructor_full \ ginstructor_tenure \ ginstructor_adwtimes 					 
				 
frmttable using "Analysis/Output/RCT1 - Instructor Descriptives.rtf", replace statmat(balance1) ///landscape /// landscape
	title("Balance of Student Pre-characteristics Across Condition (Control vs. Treatment)") coljust(l; r)  ///
	ctitles(Description ,N, Mean(Overall), Mean(Treatment), Mean(Control), Difference, Std. Error, T-stat) varlabels /// basefont(fs10) titlfont(fs11 b) sdec(0, 2, 2, 2 ,2) 
	note("This data is based on faculty who taught during the 2016/2017 and 2017/2018 academic years.")
	
coefplot (matrix(graph[,1]), ci((graph[,2] graph[,3]))), xline(0) ///
		headings(tch_mai_overknow = "{bf:Metacognition Measures}" instructor_phd = "{bf:Instructor Background}" ///
		, labcolor(orange)) scheme(s1mono) xtitle("Coefficient and Confidence Interval")

graph export "Analysis/Output/RCT1InstructorBalance.png", replace width(800) height(600)	
			
restore

****************************************************
* RCT 1 - Attrition Analysis                       *
****************************************************

preserve

keep if cohort == 1 | cohort == 2
mean id 
scalar obs = e(N)
mean id if treatment == 1
scalar obs_t = e(N)
mean id if treatment == 0
scalar obs_c = e(N)

foreach var in spring_reg spring_mai_overknow spring_mai_overreg spring_adw_grade fr_spring_gpa ///
				fr_spring_gpa_excl_adw persist_sophomore_fall{
				
	mean `var'
	scalar att = ((obs-e(N))/obs)*100
	mean `var' if treatment == 1
	scalar att_t = ((obs_t-e(N))/obs_t)*100
	mean `var' if treatment == 0
	scalar att_c = ((obs_c-e(N))/obs_c)*100
	scalar att_d = att_t - att_c
	matrix `var'=att, att_t, att_c, att_d
	matrix rownames `var'=`var'			
				}

matrix attrition = spring_reg \ spring_mai_overknow \ spring_mai_overreg \ spring_adw_grade \ fr_spring_gpa \ ///
				fr_spring_gpa_excl_adw \ persist_sophomore_fall 				
				
frmttable using "Analysis/Output/RCT1Attrition.rtf", replace statmat(attrition) ///landscape /// landscape
	title("Attrition Percentages by Outcome of Interest") coljust(l; r)  ///
	ctitles(Outcome Measure, Overall Attrition , Treatment Attrition, Control Attrition, Differential Attrition) varlabels /// basefont(fs10) titlfont(fs11 b) sdec(0, 2, 2, 2 ,2) 
	note("Note: the table includes students from Cohort 1 and Cohort 2, the students arrived at Spelman in the falls of 2016 and 2017, respectively.")

putexcel set "Analysis/Output/RCT1 - Attrition Data.xlsx", replace
putexcel A1=matrix(attrition), rownames	
import excel "Analysis/Output/RCT1 - Attrition Data.xlsx", sheet("Sheet1") clear	
rename A variable
drop C D
rename B overall
label var overall "Overall Attrition"
rename E differential
replace differential = abs(differential)
label var differential "Differential Attrition"
generate labelvar = "Regulation (MSLQ)" if var == "spring_reg"
replace labelvar = "Regulation (MAI)" if var == "spring_mai_overreg"
replace labelvar = "Knowledge (MAI)" if var == "spring_mai_overknow"
replace labelvar = "Spring GPA" if var == "fr_spring_gpa"
replace labelvar = "Spring ADW Grade" if var == "spring_adw_grade"
replace labelvar = "Spring GPA (excl. ADW)" if var == "fr_spring_gpa_excl_adw"
replace labelvar = "Persistence" if var == "persist_sophomore_fall"




twoway scatter differential overall if var == "spring_reg" | var == "spring_mai_overreg" | var == "spring_mai_overknow"   || ///
	   scatter differential overall if var == "fr_spring_gpa" | var == "spring_adw_grade" | var == "fr_spring_gpa_excl_adw" || ///
	   scatter differential overall if var == "persist_sophomore_fall", ///
	   legend(label(1 "Metacognitive Measures") label(2 "Academic Measures") label(3 "Persistence")) scheme(s1mono) xscale(range(0 67.5))  yscale(range(0 11.5)) ///
	   xlabel(0(5)67.5) ylabel(0(1)11.5)
graph export "Analysis/Output/RCT1Attrition.png", replace width(800) height(600)	
restore

****************************************************
* RCT 1 - Outcome Analysis                         *
****************************************************

preserve

keep if cohort == 1 | cohort == 2

foreach var in spring_reg spring_mai_overknow spring_mai_overreg spring_adw_grade fr_spring_gpa ///
				fr_spring_gpa_excl_adw persist_sophomore_fall{
reg `var' treatment i.cohort, cluster(rand_instructor)
eststo r`var'
matrix `var' = _b[treatment], _b[treatment]-(1.96*_se[treatment]),_b[treatment]+(1.96*_se[treatment])
matrix rownames `var' = `var'
			}

matrix graph = spring_reg \ spring_mai_overknow \ spring_mai_overreg \ spring_adw_grade \ fr_spring_gpa \ ///
				fr_spring_gpa_excl_adw \ persist_sophomore_fall			

esttab r* using "Analysis/Output/RCT1Regressions.rtf", se replace star (* 0.10 ** 0.05 *** 0.01) label
logit persist_sophomore_fall treatment i.cohort, cluster(rand_instructor)

coefplot (matrix(graph[,1]), ci((graph[,2] graph[,3]))), xline(0) ///
		headings(spring_reg = "{bf:Metacognition Measures}" spring_adw_grade = "{bf:Academic Measures}" ///
		persist_sophomore_fall = "{bf:Other Outcomes}", labcolor(black)) scheme(s1mono) xtitle("Coefficient and Confidence Interval")

graph export "Analysis/Output/RCT1Outcomes.png", replace width(800) height(600)	

restore				

****************************************************
* RCT 2 - Balance Check (Student)                  *
****************************************************
preserve
keep if cohort == 3

foreach var in fall_pre_reg fall_pre_mai_overreg fall_pre_mai_overknow high_school_reported_gpa sat_act_combined pelleligible ///
			   parent_college maj_stem maj_soc_sci maj_lang maj_human maj_arts ///
			   west midwest northeast south two_parent racially_diverse_hs ///
			   public_hs age_sept_freshman {
			   
	mean `var'
	matrix n`var'=e(_N)	// number of observations 
	matrix m0`var'=e(b)	// overall mean
	mean `var' if condition4 == 1
	matrix m1`var'=e(b)
	reg `var' i.condition, cluster(rand_instructor)
	matrix CTd`var' = _b[2.condition4]
	matrix TCd`var' = _b[3.condition4]
	matrix TTd`var' = _b[4.condition4]
	matrix seCT`var' = _se[2.condition4]
	matrix seTC`var' = _se[3.condition4]
	matrix seTT`var' = _se[4.condition4]
	matrix tCT`var'= _b[2.condition4]/_se[2.condition4]
	matrix tTC`var'= _b[3.condition4]/_se[3.condition4]
	matrix tTT`var'= _b[4.condition4]/_se[4.condition4]
	matrix `var'=n`var', m0`var', m1`var', CTd`var', TCd`var', TTd`var', tCT`var', tTC`var', tTT`var'
	matrix ct`var' = _b[2.condition4], _b[2.condition4]-(1.96*_se[2.condition4]), _b[2.condition4]+(1.96*_se[2.condition4])
	matrix tc`var' = _b[3.condition4], _b[3.condition4]-(1.96*_se[3.condition4]), _b[3.condition4]+(1.96*_se[3.condition4])
	matrix tt`var' = _b[4.condition4], _b[4.condition4]-(1.96*_se[4.condition4]), _b[4.condition4]+(1.96*_se[4.condition4])
	matrix rownames `var'=`var'
	matrix rownames ct`var'=`var'
	matrix rownames tc`var'=`var'
	matrix rownames tt`var'=`var'
		}
		
matrix balance1 = fall_pre_reg \ fall_pre_mai_overreg \ fall_pre_mai_overknow \ high_school_reported_gpa \ sat_act_combined \ pelleligible \ ///
			   parent_college \ maj_stem \ maj_soc_sci \ maj_lang \ maj_human \ maj_arts \ ///
			   west \ midwest \ northeast \ south \ two_parent \ racially_diverse_hs \ ///
			   public_hs \ age_sept_freshman 
			   
foreach var in ct tc tt{
matrix `var' = `var'fall_pre_reg \ `var'fall_pre_mai_overreg \ `var'fall_pre_mai_overknow \ `var'high_school_reported_gpa \ `var'sat_act_combined \ `var'pelleligible \ ///
			   `var'parent_college \ ///
			   `var'west \ `var'midwest \ `var'northeast \ `var'south \ `var'two_parent \ `var'racially_diverse_hs \ ///
			   `var'public_hs \ `var'age_sept_freshman \ `var'maj_stem \ `var'maj_soc_sci \ `var'maj_lang \ `var'maj_human \ `var'maj_arts 
}			   
			   
frmttable using "Analysis/Output/RCT2StudentDescriptives.rtf", replace statmat(balance1) ///landscape /// landscape
	title("Balance of Student Pre-characteristics Across Four Conditions") coljust(l; r)  ///
	ctitles(Description ,N, Mean(Overall), Mean(CC), Difference(CT-CC), Difference(TC-CC), Difference(TT-CC), T-Stat(CT-CC), T-Stat(TC-CC), T-Stat(TT-CC)) varlabels /// basefont(fs10) titlfont(fs11 b) sdec(0, 2, 2, 2 ,2) 
	note("Note: The table includes students from Cohort 3, who arrived at Spelman in the fall of 2018.")
	
*Present Visually	
coefplot (matrix(ct[,1]), ci((ct[,2] ct[,3]))), bylabel(CT) || (matrix(tc[,1]), ci((tc[,2] tc[,3]))), bylabel(TC)  || (matrix(tt[,1]), ci((tt[,2] tt[,3]))), bylabel(TT)  ||, xline(0) ///
		byopts(row(1)) headings(fall_pre_reg = "{bf:Metacognition Measures}" high_school_reported_gpa = "{bf:Academic Measures}" ///
		pelleligible = "{bf:Background Characteristics}" maj_stem = "{bf:Student Interests}", labcolor(orange)) scheme(s1mono) xtitle("Coefficient and Confidence Interval") ///
		xscale(range(-.2 .2))
		
graph export "Analysis/Output/RCT2StudentBalance.png", replace width(800) height(600)		
	
restore
	
****************************************************
* RCT 2 - Balance Check Faculty                    *
****************************************************	

preserve

keep if cohort == 3
	
foreach var in instructor_phd instructor_years instructor_spelyrs instructor_adwtimes instructor_full instructor_tenure ///
				 tch_mai_decl tch_mai_proc tch_mai_cond tch_mai_plan ///
				 tch_mai_info tch_mai_comp tch_mai_debg ///
				 tch_mai_eval tch_mai_overknow tch_mai_overreg{
			   
	mean `var'
	matrix n`var'=e(_N)	// number of observations 
	matrix m0`var'=e(b)	// overall mean
	mean `var' if condition4 == 1
	matrix m1`var'=e(b)
	reg `var' i.condition, cluster(rand_instructor)
	matrix CTd`var' = _b[2.condition4]
	matrix TCd`var' = _b[3.condition4]
	matrix TTd`var' = _b[4.condition4]
	matrix seCT`var' = _se[2.condition4]
	matrix seTC`var' = _se[3.condition4]
	matrix seTT`var' = _se[4.condition4]
	matrix tCT`var'= _b[2.condition4]/_se[2.condition4]
	matrix tTC`var'= _b[3.condition4]/_se[3.condition4]
	matrix tTT`var'= _b[4.condition4]/_se[4.condition4]
	matrix `var'=n`var', m0`var', m1`var', CTd`var', TCd`var', TTd`var', tCT`var', tTC`var', tTT`var'
	matrix ct`var' = _b[2.condition4], _b[2.condition4]-(1.96*_se[2.condition4]), _b[2.condition4]+(1.96*_se[2.condition4])
	matrix tc`var' = _b[3.condition4], _b[3.condition4]-(1.96*_se[3.condition4]), _b[3.condition4]+(1.96*_se[3.condition4])
	matrix tt`var' = _b[4.condition4], _b[4.condition4]-(1.96*_se[4.condition4]), _b[4.condition4]+(1.96*_se[4.condition4])
	matrix rownames `var'=`var'
	matrix rownames ct`var'=`var'
	matrix rownames tc`var'=`var'
	matrix rownames tt`var'=`var'
		}
matrix balance1 = tch_mai_overknow \ tch_mai_overreg \ instructor_phd \ instructor_full \ instructor_tenure \ instructor_adwtimes

foreach var in ct tc tt{
matrix `var' = `var'tch_mai_overknow \ `var'tch_mai_overreg \ `var'instructor_phd \ `var'instructor_full \ `var'instructor_tenure \ `var'instructor_adwtimes
}					 
				 
frmttable using "Analysis/Output/RCT2InstructorDescriptives", replace statmat(balance1) ///landscape /// landscape
	title("Balance of instructor Pre-characteristics Across Four Conditions") coljust(l; r)  ///
	ctitles(Description ,N, Mean(Overall), Mean(CC), Difference(CT-CC), Difference(TC-CC), Difference(TT-CC), T-Stat(CT-CC), T-Stat(TC-CC), T-Stat(TT-CC)) varlabels /// basefont(fs10) titlfont(fs11 b) sdec(0, 2, 2, 2 ,2) 
	note("This data is based on faculty who taught during the 2018/2019 academic year.")

*Present Visually	
coefplot (matrix(ct[,1]), ci((ct[,2] ct[,3]))), bylabel(CT) || (matrix(tc[,1]), ci((tc[,2] tc[,3]))), bylabel(TC)  || (matrix(tt[,1]), ci((tt[,2] tt[,3]))), bylabel(TT)  ||, xline(0) ///
		byopts(row(1)) headings(tch_mai_overknow = "{bf:Metacognition Measures}" instructor_phd = "{bf:Instructor Background}" ///
		, labcolor(orange)) scheme(s1mono) xtitle("Coefficient and Confidence Interval")
		
		
graph export "Analysis/Output/RCT2FacultyBalance.png", replace width(800) height(600)
	
restore

****************************************************
* RCT 2 - Attrition Analysis                       *
****************************************************

preserve

keep if cohort == 3
mean id 
scalar obs = e(N)
mean id if condition4 == 1
scalar obs_1 = e(N)
mean id if condition4 == 2
scalar obs_2 = e(N)
mean id if condition4 == 3
scalar obs_3 = e(N)
mean id if condition4 == 4
scalar obs_4 = e(N)

foreach var in spring_reg spring_mai_overknow spring_mai_overreg spring_adw_grade fr_spring_gpa ///
				fr_spring_gpa_excl_adw persist_sophomore_fall{
forvalues x = 2/4{
				
	mean `var' if condition == 1 | condition == `x' 
	scalar att`x' = ((obs_1+obs_`x'-e(N))/(obs_1+obs_`x'))*100
	mean `var' if condition == `x'
	scalar att_t = ((obs_`x'-e(N))/obs_`x')*100
	mean `var' if condition == 1
	scalar att_c = ((obs_1-e(N))/obs_1)*100
	scalar datt`x' = att_t - att_c

}
	matrix `var'=att2, datt2, att3, datt3, att4, datt4
	matrix rownames `var'=`var'	
	}

matrix attrition = spring_reg \ spring_mai_overknow \ spring_mai_overreg \ spring_adw_grade \ fr_spring_gpa \ ///
				fr_spring_gpa_excl_adw \ persist_sophomore_fall 

frmttable using "Analysis/Output/RCT2Attrition.rtf", replace statmat(attrition) ///landscape /// landscape
	title("Attrition Percentages by Outcome of Interest") coljust(l; r)  ///
	ctitles(Outcome Measure, CT Attrition , CT Differential Attrition, TC Attrition, TC Differential Attrition, TT Attrition, TT Differential Attrition) varlabels /// basefont(fs10) titlfont(fs11 b) sdec(0, 2, 2, 2 ,2) 
	note("Note: the table includes students from Cohort 1 and Cohort 2, the students arrived at Spelman in the falls of 2016 and 2017, respectively.")

putexcel set "Analysis/Output/RCT2 - Attrition Data.xlsx", replace
putexcel A1=matrix(attrition), rownames	
import excel "Analysis/Output/RCT2 - Attrition Data.xlsx", sheet("Sheet1") clear	
rename A variable

rename B overallct
rename C differentialct
rename D overalltc
rename E differentialtc
rename F overalltt
rename G differentialtt

reshape long overall differential, i(variable) j(condition) string

label var overall "Overall Attrition"
replace differential = abs(differential)
label var differential "Differential Attrition"
generate labelvar = "Regulation (MSLQ)" if var == "spring_reg"
replace labelvar = "Regulation (MAI)" if var == "spring_mai_overreg"
replace labelvar = "Knowledge (MAI)" if var == "spring_mai_overknow"
replace labelvar = "Spring GPA" if var == "fr_spring_gpa"
replace labelvar = "Spring ADW Grade" if var == "spring_adw_grade"
replace labelvar = "Spring GPA (excl. ADW)" if var == "fr_spring_gpa_excl_adw"
replace labelvar = "Persistence" if var == "persist_sophomore_fall"
replace condition = upper(condition)

twoway scatter differential overall if var == "spring_reg" | var == "spring_mai_overreg" | var == "spring_mai_overknow" , mlabel(condition)  || ///
	   scatter differential overall if var == "fr_spring_gpa" | var == "spring_adw_grade" | var == "fr_spring_gpa_excl_adw" || ///
	   scatter differential overall if var == "persist_sophomore_fall", ///
	   legend(label(1 "Metacognitive Measures") label(2 "Academic Measures") label(3 "Persistence")) scheme(s1mono) xscale(range(0 67.5))  yscale(range(0 11.5)) ///
	   xlabel(0(5)67.5) ylabel(0(1)11.5)
graph export "Analysis/Output/RCT2Attrition.png", replace width(800) height(600)
	
restore

****************************************************
* RCT 2 - Outcome Analysis                         *
****************************************************

preserve

keep if cohort == 3

foreach var in spring_reg spring_mai_overknow spring_mai_overreg spring_adw_grade fr_spring_gpa ///
				fr_spring_gpa_excl_adw persist_sophomore_fall{
reg `var' i.condition4, cluster(rand_instructor)
	matrix ct`var' = _b[2.condition4], _b[2.condition4]-(1.96*_se[2.condition4]), _b[2.condition4]+(1.96*_se[2.condition4])
	matrix tc`var' = _b[3.condition4], _b[3.condition4]-(1.96*_se[3.condition4]), _b[3.condition4]+(1.96*_se[3.condition4])
	matrix tt`var' = _b[4.condition4], _b[4.condition4]-(1.96*_se[4.condition4]), _b[4.condition4]+(1.96*_se[4.condition4])
	matrix rownames ct`var'=`var'
	matrix rownames tc`var'=`var'
	matrix rownames tt`var'=`var'
eststo r`var'	
	}

foreach var in ct tc tt{
matrix `var' = `var'spring_reg \ `var'spring_mai_overknow \ `var'spring_mai_overreg \ `var'spring_adw_grade \ `var'fr_spring_gpa \ `var'fr_spring_gpa_excl_adw \ `var'persist_sophomore_fall	
}		

coefplot (matrix(ct[,1]), ci((ct[,2] ct[,3]))), bylabel(CT) || (matrix(tc[,1]), ci((tc[,2] tc[,3]))), bylabel(TC)  || (matrix(tt[,1]), ci((tt[,2] tt[,3]))), bylabel(TT)  ||, xline(0) ///
		byopts(row(1))headings(spring_reg = "{bf:Metacognition Measures}" spring_adw_grade = "{bf:Academic Measures}" ///
		persist_sophomore_fall = "{bf:Other Outcomes}", labcolor(black)) scheme(s1mono) xtitle("Coefficient and Confidence Interval")

graph export "Analysis/Output/RCT2Outcomes.png", replace width(800) height(600)	

esttab r* using "Analysis/Output/RCT2RegressionsNoControls.rtf", se replace star (* 0.10 ** 0.05 *** 0.01) label
logit persist_sophomore_fall i.condition4, cluster(rand_instructor)

foreach var in spring_reg spring_mai_overknow spring_mai_overreg spring_adw_grade fr_spring_gpa ///
				fr_spring_gpa_excl_adw persist_sophomore_fall{
reg `var' i.condition4 instructor_phd instructor_years instructor_full instructor_tenure fall_pre_reg fall_pre_mai_overknow ///
			   parent_college maj_stem maj_soc_sci maj_lang, cluster(rand_instructor)
	matrix ct`var' = _b[2.condition4], _b[2.condition4]-(1.96*_se[2.condition4]), _b[2.condition4]+(1.96*_se[2.condition4])
	matrix tc`var' = _b[3.condition4], _b[3.condition4]-(1.96*_se[3.condition4]), _b[3.condition4]+(1.96*_se[3.condition4])
	matrix tt`var' = _b[4.condition4], _b[4.condition4]-(1.96*_se[4.condition4]), _b[4.condition4]+(1.96*_se[4.condition4])
	matrix rownames ct`var'=`var'
	matrix rownames tc`var'=`var'
	matrix rownames tt`var'=`var'
eststo r`var'	
	}

foreach var in ct tc tt{
matrix `var' = `var'spring_reg \ `var'spring_mai_overknow \ `var'spring_mai_overreg \ `var'spring_adw_grade \ `var'fr_spring_gpa \ `var'fr_spring_gpa_excl_adw \ `var'persist_sophomore_fall	
}		

coefplot (matrix(ct[,1]), ci((ct[,2] ct[,3]))), bylabel(CT) || (matrix(tc[,1]), ci((tc[,2] tc[,3]))), bylabel(TC)  || (matrix(tt[,1]), ci((tt[,2] tt[,3]))), bylabel(TT)  ||, xline(0) ///
		byopts(row(1))headings(spring_reg = "{bf:Metacognition Measures}" spring_adw_grade = "{bf:Academic Measures}" ///
		persist_sophomore_fall = "{bf:Other Outcomes}", labcolor(black)) scheme(s1mono) xtitle("Coefficient and Confidence Interval")

graph export "Analysis/Output/RCT2OutcomesUnbControls.png", replace width(800) height(600)
			
esttab r* using "Analysis/Output/RCT2RegressionsUnbalnacedControls.rtf", se replace star (* 0.10 ** 0.05 *** 0.01) label
logit persist_sophomore_fall i.condition4 instructor_phd instructor_years instructor_full instructor_tenure fall_pre_reg fall_pre_mai_overknow ///
			   parent_college maj_stem maj_soc_sci maj_lang, cluster(rand_instructor)

foreach var in spring_reg spring_mai_overknow spring_mai_overreg spring_adw_grade fr_spring_gpa ///
				fr_spring_gpa_excl_adw persist_sophomore_fall{
reg `var' i.condition4 instructor_phd instructor_years instructor_spelyrs instructor_adwtimes instructor_full instructor_tenure ///
				 tch_mai_decl tch_mai_proc tch_mai_cond tch_mai_plan ///
				 tch_mai_info tch_mai_comp tch_mai_debg ///
				 tch_mai_eval tch_mai_overknow tch_mai_overreg fall_pre_reg fall_pre_mai_overreg fall_pre_mai_overknow high_school_reported_gpa sat_act_combined pelleligible ///
			   parent_college maj_stem maj_soc_sci maj_lang maj_human maj_arts ///
			   west midwest northeast south two_parent racially_diverse_hs ///
			   public_hs age, cluster(rand_instructor)
eststo r`var'
			}
esttab r* using "Analysis/Output/RCT2RegressionsAllControls.rtf", se replace star (* 0.10 ** 0.05 *** 0.01) label

foreach var in spring_reg spring_mai_overknow spring_mai_overreg spring_adw_grade fr_spring_gpa ///
				fr_spring_gpa_excl_adw persist_sophomore_fall{
reg `var' i.condition4 instructor_phd instructor_years instructor_adwtimes instructor_full instructor_tenure fall_pre_reg fall_pre_mai_overknow ///
			   parent_college maj_stem maj_soc_sci maj_lang, cluster(rand_instructor)
	matrix ct`var' = _b[2.condition4], _b[2.condition4]-(1.96*_se[2.condition4]), _b[2.condition4]+(1.96*_se[2.condition4])
	matrix tc`var' = _b[3.condition4], _b[3.condition4]-(1.96*_se[3.condition4]), _b[3.condition4]+(1.96*_se[3.condition4])
	matrix tt`var' = _b[4.condition4], _b[4.condition4]-(1.96*_se[4.condition4]), _b[4.condition4]+(1.96*_se[4.condition4])
	matrix rownames ct`var'=`var'
	matrix rownames tc`var'=`var'
	matrix rownames tt`var'=`var'
eststo r`var'	
	}

foreach var in ct tc tt{
matrix `var' = `var'spring_reg \ `var'spring_mai_overknow \ `var'spring_mai_overreg \ `var'spring_adw_grade \ `var'fr_spring_gpa \ `var'fr_spring_gpa_excl_adw \ `var'persist_sophomore_fall	
}		

coefplot (matrix(ct[,1]), ci((ct[,2] ct[,3]))), bylabel(CT) || (matrix(tc[,1]), ci((tc[,2] tc[,3]))), bylabel(TC)  || (matrix(tt[,1]), ci((tt[,2] tt[,3]))), bylabel(TT)  ||, xline(0) ///
		byopts(row(1))headings(spring_reg = "{bf:Metacognition Measures}" spring_adw_grade = "{bf:Academic Measures}" ///
		persist_sophomore_fall = "{bf:Other Outcomes}", labcolor(black)) scheme(s1mono) xtitle("Coefficient and Confidence Interval")

graph export "Analysis/Output/RCT2OutcomesUnbControlsADW.png", replace width(800) height(600)

esttab r* using "Analysis/Output/RCT2RegressionsUnbControlsADW.rtf", se replace star (* 0.10 ** 0.05 *** 0.01) label

restore				

****************************************************
* RCT 1 and 2 - Control Group Means                *
****************************************************

sum fr_spring_gpa_excl_adw if condition4==1

sum fr_spring_gpa_excl_adw if (cohort==1 | cohort==2) & treatment==0

sum fr_spring_gpa_excl_adw if (cohort==3) & condition4==1

sum persist_sophomore_fall if (cohort==1 | cohort==2) & treatment==0

sum persist_sophomore_fall if (cohort==3) & condition4==1
