## Replication file for "How Robust are Compliance Findings? A study synthesis", JEPP 2012

## Replicate results in table 4 : Number of studies analyzing (robust) explanatory factors by country of country groups (EU12, EU15, EU27, NMS12)
## + extended Version of table 4 for all theoretical arguments: online APPENDIX


## clean working space
ls()
rm(list=ls())


## set working directory to folder with compliance dataset
	## set working directory (MAC) e.g.
	#setwd('~/Dropbox/Compliance')

	## set working directory (WINDOWS) e.g.
	#setwd('~\\Dropbox\\Compliance')

## Load Data
	## load data (MAC)
	load('./compliance2012onlineFinal.RData')
	## load data (WINDOWS)
	#load('.\\compliance2012onlineFinal.RData')

## keep only case studies and quantitative studies
data<-data[data$type.label!='Descriptive',]


	## make factor variables
	data$studyid<-as.factor(data$studyid.label)
	data$ivtheoryneu<-as.factor(data$ivtheoryneu.label)
	data$dvcat<-as.factor(data$dvcat.label)
	data$type<-as.factor(data$type.label)




	## create variable which indicates whether authors analyse all EU countries (EU12, EU15, EU27)

	
	### General Info
	## European Union of 12 member states (EU-12): Belgium, Denmark, France, Germany, Greece, Ireland, Italy, Luxembourg, the Netherlands, Portugal, Spain, United Kingdom
	##  European Union of 15 member states (EU-15): EU-12 plus Austria, Finland, Sweden
	##  European Union of 25 member states (EU-25): EU-15 plus Czech Republic, Cyprus, Estonia, Hungary, Latvia, Lithuania, Malta, Poland, Slovakia, Slovenia
	##  European Union of 27 member states (EU-27): EU-25 plus Bulgaria, Romania
	
	
	## EU 12
	eu12<-which(
	## EU 12
	data$belgium==1 & # 1
	data$denmark==1 & #2
	data$france==1 & #3
	data$germany==1 & #4
	data$greece==1 & #5
	data$ireland==1 & #6
	data$italy==1 & #7
	data$luxembourg==1 & #8
	data$netherlands==1 & #9
	data$portugal==1 & #10
	data$spain==1 & #11
	data$uk==1 & #12
	## EU15	
	data$austria==0 &#13
	data$finland==0 &#14
	data$sweden==0 &#15
	## EU25
	data$czech_republic==0 &#16
	data$cyprus==0 &#17
	data$estonia==0 &#18
	data$hungary==0 &#19
	data$latvia==0 &#20
	data$lithuania==0 &#21
	data$malta==0 &#22
	data$poland==0 &#23
	data$slovakia==0 &#24
	data$slovenia==0 &#25
	## EU27
	data$bulgaria==0 &#26
	data$romania==0 #27
	)		
	data$eu12<-0
	data$eu12[eu12]<-1
	unique(data$studyid[data$eu12==1])	
	
	
	## EU 15
	eu15<-which(
	## EU 12
	data$belgium==1 & # 1
	data$denmark==1 & #2
	data$france==1 & #3
	data$germany==1 & #4
	data$greece==1 & #5
	data$ireland==1 & #6
	data$italy==1 & #7
	data$luxembourg==1 & #8
	data$netherlands==1 & #9
	data$portugal==1 & #10
	data$spain==1 & #11
	data$uk==1 & #12
	## EU15	
	data$austria==1 &#13
	data$finland==1 &#14
	data$sweden==1 &#15
	## EU25
	data$czech_republic==0 &#16
	data$cyprus==0 &#17
	data$estonia==0 &#18
	data$hungary==0 &#19
	data$latvia==0 &#20
	data$lithuania==0 &#21
	data$malta==0 &#22
	data$poland==0 &#23
	data$slovakia==0 &#24
	data$slovenia==0 &#25
	## EU27
	data$bulgaria==0 &#26
	data$romania==0 #27
	)		
	data$eu15<-0
	data$eu15[eu15]<-1
	unique(data$studyid[data$eu15==1])		
	
	## EU 25
	eu25<-which(
	## EU 12
	data$belgium==1 & # 1
	data$denmark==1 & #2
	data$france==1 & #3
	data$germany==1 & #4
	data$greece==1 & #5
	data$ireland==1 & #6
	data$italy==1 & #7
	data$luxembourg==1 & #8
	data$netherlands==1 & #9
	data$portugal==1 & #10
	data$spain==1 & #11
	data$uk==1 & #12
	## EU15	
	data$austria==1 &#13
	data$finland==1 &#14
	data$sweden==1 &#15
	## EU25
	data$czech_republic==1 &#16
	data$cyprus==1 &#17
	data$estonia==1 &#18
	data$hungary==1 &#19
	data$latvia==1 &#20
	data$lithuania==1 &#21
	data$malta==1 &#22
	data$poland==1 &#23
	data$slovakia==1 &#24
	data$slovenia==1 &#25
	## EU27
	data$bulgaria==0 &#26
	data$romania==0 #27
	)		
	data$eu25<-0
	data$eu25[eu25]<-1
	unique(data$studyid[data$eu25==1])	
	

	## EU27
	eu27<-which(
	## EU 12
	data$belgium==1 & # 1
	data$denmark==1 & #2
	data$france==1 & #3
	data$germany==1 & #4
	data$greece==1 & #5
	data$ireland==1 & #6
	data$italy==1 & #7
	data$luxembourg==1 & #8
	data$netherlands==1 & #9
	data$portugal==1 & #10
	data$spain==1 & #11
	data$uk==1 & #12
	## EU15	
	data$austria==1 &#13
	data$finland==1 &#14
	data$sweden==1 &#15
	## EU25
	data$czech_republic==1 &#16
	data$cyprus==1 &#17
	data$estonia==1 &#18
	data$hungary==1 &#19
	data$latvia==1 &#20
	data$lithuania==1 &#21
	data$malta==1 &#22
	data$poland==1 &#23
	data$slovakia==1 &#24
	data$slovenia==1 &#25
	## EU27
	data$bulgaria==1 &#26
	data$romania==1 #27
	)		
	data$eu27<-0
	data$eu27[eu27]<-1
	unique(data$studyid[data$eu27==1])	
	
	
	## New Member states which joined the EU in 2004 and 2007
	nms12<-which(
	## EU 12
	data$belgium==0 & # 1
	data$denmark==0 & #2
	data$france==0 & #3
	data$germany==0 & #4
	data$greece==0 & #5
	data$ireland==0 & #6
	data$italy==0 & #7
	data$luxembourg==0 & #8
	data$netherlands==0 & #9
	data$portugal==0 & #10
	data$spain==0 & #11
	data$uk==0 & #12
	## EU15	
	data$austria==0 &#13
	data$finland==0 &#14
	data$sweden==0 &#15
	## EU25
	data$czech_republic==1 &#16
	data$cyprus==1 &#17
	data$estonia==1 &#18
	data$hungary==1 &#19
	data$latvia==1 &#20
	data$lithuania==1 &#21
	data$malta==1 &#22
	data$poland==1 &#23
	data$slovakia==1 &#24
	data$slovenia==1 &#25
	## EU27
	data$bulgaria==1 &#26
	data$romania==1 #27
	)		
	data$nms12<-0
	data$nms12[nms12]<-1
	unique(data$studyid[data$nms12==1])	

	## determine the cases for which all EU countries (EU12, EU15, EU25, EU27,NMS12) have been studied

	EU<-which(
	data$eu12==1 |
	data$eu15==1 |
	data$eu25==1 |
	data$eu27==1 |	
	data$nms12==1 
	)
	data$eu<-0
	data$eu[EU]<-1
	
	## show created variables
	data$eu12
	data$eu15
	data$eu25
	data$eu27
	data$nms12
	
	## retreive info for the number of studies per country and theoretical argument. Note that this shows only the cases where not all EU countries (EU12, EU15, EU25, EU27 or NMS12 were studied at once (data$eu==0))
	length(unique(data$studyid[data$greece==1 & data$ivtheoryneu=="Institutional decision-making capacity" & data$type=="Qualitative" & data$eu==0] ))
	length(unique(data$studyid[data$greece==1 & data$ivtheoryneu=="Institutional decision-making capacity" & data$type=="Quantitative" & data$eu==0]))
	
	### Retreive info for the number of studies which analyse all EU countries alltogether (data$eu==1)
	## retrieve the number of studies per theoretical argument for EU12
	length(unique(data$studyid[data$ivtheoryneu=="Institutional decision-making capacity" & data$type=="Qualitative" & data$eu12==1] ))
	length(unique(data$studyid[data$ivtheoryneu=="Institutional decision-making capacity" & data$type=="Quantitative" & data$eu12==1] ))
	## retrieve the number of studies per theoretical argument for EU15
	length(unique(data$studyid[data$ivtheoryneu=="Institutional decision-making capacity" & data$type=="Qualitative" & data$eu15==1] ))
		length(unique(data$studyid[data$ivtheoryneu=="Institutional decision-making capacity" & data$type=="Quantitative" & data$eu15==1] ))
	## retrieve the number of studies per theoretical argument for EU25
	length(unique(data$studyid[data$ivtheoryneu=="Institutional decision-making capacity" & data$type=="Qualitative" & data$eu25==1] ))
		length(unique(data$studyid[data$ivtheoryneu=="Institutional decision-making capacity" & data$type=="Quantitative" & data$eu25==1] ))
	## retrieve the number of studies per theoretical argument gfor EU27
		length(unique(data$studyid[data$ivtheoryneu=="Institutional decision-making capacity" & data$type=="Qualitative" & data$eu27==1] ))
		length(unique(data$studyid[data$ivtheoryneu=="Institutional decision-making capacity" & data$type=="Quantitative" & data$eu27==1] ))
	## retrieve the number of studies per theoretical argument gfor NMS12
		length(unique(data$studyid[data$ivtheoryneu=="Institutional decision-making capacity" & data$type=="Qualitative" & data$nms12==1] ))
		length(unique(data$studyid[data$ivtheoryneu=="Institutional decision-making capacity" & data$type=="Quantitative" & data$nms12==1] ))

	##... for the reamining theoretical arguments paste one of the theoretical arguments in the code from above in the place of "Institutional decision-making capacity": (Good fit,Preferences,Administrative efficiency,Low complexity of EU laws,Favourable culture,Interministerial coordination problems,National monitoring and enforcement,Supranational monitoring and enforcement,Power (EU),Positive public support, Learning,Control variable,No appropriate category)
	## repeat the same approach for each country which are saved as dummy variables and can be seen by entering: names(data)




### CREATE TABLE 4

	
	## THEORETICAL ARGUMENTS
		##create lists with theoretical arguments
		listTheory<-sort(as.character(unique(data$ivtheoryneu)))
		##reorganize list with Theories to the occurence in table2 of the paper 
		listTheory<-c(listTheory[5], listTheory[4],listTheory[13],listTheory[1],listTheory[8],listTheory[3],listTheory[6],listTheory[9],listTheory[14],listTheory[12],listTheory[11],listTheory[7],listTheory[2],listTheory[10])		## create theory abbreviations for later
		listTheoryAbb<-c("IDMC","GoF","PREF",'AE',"COMPL","CULT",'COORD','NME','SNME','POW','PS','LEARN','CV','NAC','ALL','Total','Total Coverage')
	## POLICY FIELDS	
		## create a list with countries
		listCountry<-c('belgium',"denmark","france","germany","greece","ireland","italy","luxembourg","netherlands","portugal","spain","uk","austria","finland","sweden",'eu12','eu15','eu27','nms12')
		
		
		## reorganize list with countries following the order in table 4 in the paper
		listCountry<-c(listCountry[5], listCountry[7], listCountry[10],listCountry[8],listCountry[1],listCountry[3],listCountry[4],listCountry[6],listCountry[13],listCountry[12],listCountry[11],listCountry[9],listCountry[14],listCountry[15],listCountry[2],listCountry[16],listCountry[17],listCountry[18],listCountry[19])	
			
		## create empty matrix for table 4
		ncol<-length(listTheory)+3 # number columns in the table
		nrow<-length(listCountry)+1 # number of rows in the table
		table4 <- matrix(NA, nrow=nrow, ncol=ncol)
		
		## assign column and row names to the matrix
		colnames(table4)<-c(listTheory,'All',"Total","Total Coverage")
		rownames(table4)<-c(listCountry, "Total")
		## reformat the matrix into a dataframe
		table4<-as.data.frame(table4)
		## show empty table4
		table4
		## save empty table for quantitative studies
		empty_matrix<-table4
		## create a variable for policy fields
		table4$country<-rownames(table4)
	
		
##### QUALITATIVE STUDIES		
		
		## make backup for data
		data_full<-data
		## create a new object with cases which analyse all EU countries (EU12,EU15, EU27, NMS12)
		data.eu<-data[data$eu==1,]
		## create an object with cases which analyse countries or country groups whithout considering all EU countries
		## save the object in place of data
		data<-data[data$eu==0,]
		
		## assign number of studies per theory and country (QUALITATIVE STUDIES)
	
		## for 15 countries
		for(i in 1: length(listCountry[1:15])) {
			for (j in 1: length(listTheory)) {
			## subsample of qualitative studies analyzing policy filed i (see lis "listPolicy" by inserting e.g listPolicy[1], listPolicy[2]...listPolicy[i]]))
			info<-data[data$type=="Qualitative" & data[, listCountry[i]]==1,]			## count numbrt of studies for  country i and theory j
			res<-length(unique(info$studyid[info$ivtheoryneu==listTheory[j]]))
			## determine the place where the results (res) should be inserted
				## given policy filed i
				country<-which(table4$country== listCountry[i])
				## given theory j
				theory<-which(names(table4)==listTheory[j])
			## insert results in the table
			table4[country,theory]<-res
			## count total number of qualitative studies for country i
			all<-length(unique(info$studyid))
			## insert total number of studies for policy field i in table 3
			table4[country,"All"]<-all
			
			## count total number of studies given theory i (all studies!, therefore data_full is used here)
			total<-length(unique(data_full$studyid[data_full$ivtheoryneu==listTheory[j] & data_full$type=="Qualitative"]))
			## insert total number of studies given theory i
			table4[table4$country=="Total",theory]<-total
			## count total number of (qualitative studies)
			totalAll<-length(unique(data_full$studyid[data_full$type=="Qualitative"]))
			## insert total number of qualitative studies
			table4[table4$country=="Total","All"]<-totalAll
			
		} # close j loop which stands for theory
	} # close i loop which stands for policy fields
	
		
		
		### for EU12, EU15, EU27, NMS12 (listCountry[16:19])
				for(i in 16: 19) {
			for (j in 1: length(listTheory)) {
			## subsample of qualitative studies analyzing policy filed i (see lis "listPolicy" by inserting e.g listPolicy[1], listPolicy[2]...listPolicy[i]]))
			info<-data.eu[data.eu$type=="Qualitative" & data.eu[, listCountry[i]]==1,]			## count numbrt of studies for  country i and theory j
			res<-length(unique(info$studyid[info$ivtheoryneu==listTheory[j]]))
			## determine the place where the results (res) should be inserted
				## given policy filed i
				country<-which(table4$country== listCountry[i])
				## given theory j
				theory<-which(names(table4)==listTheory[j])
			## insert results in the table
			table4[country,theory]<-res
			## count total number of qualitative studies for country i
			all<-length(unique(info$studyid))
			## insert total number of studies for policy field i in table 3
			table4[country,"All"]<-all
		
		} # close j loop which stands for theory
	} # close i loop which stands for policy fields
	
		
		
		
		
		## save results in a new object
		table4_Qulitative<-table4
	
	
		## SHOW RESULTS for table 4 for QULITATIVE STUDIES
		table4_Qulitative	

	
### QUANTITATIVE STUDIES
	
	## assign empty matrix to table4
	table4<-empty_matrix
	table4$country<-rownames(table4)



		## assign number of studies per theory and country (QUNATITATIVE STUDIES)
	
		## for 15 countries
		for(i in 1: length(listCountry[1:15])) {
			for (j in 1: length(listTheory)) {
			## subsample of qualitative studies analyzing policy filed i (see lis "listPolicy" by inserting e.g listPolicy[1], listPolicy[2]...listPolicy[i]]))
			info<-data[data$type=="Quantitative" & data[, listCountry[i]]==1,]			## count numbrt of studies for  country i and theory j
			res<-length(unique(info$studyid[info$ivtheoryneu==listTheory[j]]))
			## determine the place where the results (res) should be inserted
				## given policy filed i
				country<-which(table4$country== listCountry[i])
				## given theory j
				theory<-which(names(table4)==listTheory[j])
			## insert results in the table
			table4[country,theory]<-res
			## count total number of qualitative studies for country i
			all<-length(unique(info$studyid))
			## insert total number of studies for policy field i in table 3
			table4[country,"All"]<-all
			
			## count total number of studies given theory i (all studies!, therefore data_full is used here)
			total<-length(unique(data_full$studyid[data_full$ivtheoryneu==listTheory[j] & data_full$type=="Quantitative"]))
			## insert total number of studies given theory i
			table4[table4$country=="Total",theory]<-total
			## count total number of (qualitative studies)
			totalAll<-length(unique(data_full$studyid[data_full$type=="Quantitative"]))
			## insert total number of qualitative studies
			table4[table4$country=="Total","All"]<-totalAll
			
			## count total number of all studies (only separate countries) for countr i
			total<-length(unique(data$studyid[data[, listCountry[i]]==1]))
			## insert tottal number of all studies per policy field i
			table4[country,"Total"]<-total			
			## count total number of all studies (separate countries and all countries (EU12 etc.)) for country i
			total.coverage<-length(unique(data_full$studyid[data_full[, listCountry[i]]==1]))
			## insert tottal number of all studies per policy field i
			table4[country,"Total Coverage"]<-total.coverage	
					
		} # close j loop which stands for theory
	} # close i loop which stands for policy fields
	
		
		
		### for EU12, EU15, EU27, NMS12 (listCountry[16:19])
				for(i in 16: 19) {
			for (j in 1: length(listTheory)) {
			## subsample of qualitative studies analyzing policy filed i (see lis "listPolicy" by inserting e.g listPolicy[1], listPolicy[2]...listPolicy[i]]))
			info<-data.eu[data.eu$type=="Quantitative" & data.eu[, listCountry[i]]==1,]			## count numbrt of studies for  country i and theory j
			res<-length(unique(info$studyid[info$ivtheoryneu==listTheory[j]]))
			## determine the place where the results (res) should be inserted
				## given policy filed i
				country<-which(table4$country== listCountry[i])
				## given theory j
				theory<-which(names(table4)==listTheory[j])
			## insert results in the table
			table4[country,theory]<-res
			## count total number of qualitative studies for country i
			all<-length(unique(info$studyid))
			## insert total number of studies for policy field i in table 3
			table4[country,"All"]<-all
			
			## count total number of all studies (only separate countries, not EU15, EU25 etc.!) for country i
			total<-length(unique(data$studyid[data[, listCountry[i]]==1]))
			## insert tottal number of all studies per country i
			table4[country,"Total"]<-total			
			## count total number of all studies (separate countries and all countries (EU12 etc.)) for country i
			total.coverage<-length(unique(data_full$studyid[data_full[, listCountry[i]]==1]))
			## insert tottal number of all studies per policy field i
			table4[country,"Total Coverage"]<-total.coverage
			table4["Total","Total"]<-length(unique(data_full$studyid))

		} # close j loop which stands for theory
	} # close i loop which stands for policy fields
	
		# insert Total Coverage for EU12, EU15, EU27 and NMS 12 also to Total (these are identical)
		rownames(table4)
		eu.var<-which(rownames(table4)=="eu12" | rownames(table4)=="eu15" | rownames(table4)=="eu27" | rownames(table4)=="nms12")
		table4[eu.var,"Total"]<-table4[eu.var,"Total Coverage"]
		
	
	## save results for Quantitative studies in a separate object
	
	table4_Quantitative<-table4
	
	## combine results: APPENDIX
	table4_Quantitative
	table4_Qulitative
	appendix<-cbind(table4_Qulitative, table4_Quantitative)
	## delete redundant policy variables
	del.variables<-which(names(appendix)=="country")
	appendix<-appendix[,-del.variables]
	## rename theoretical arguments with their corresponding abbreviations and add _C for Qualitataive(Case) Studies and _Q for Quantitative Studies
	qual<-paste(listTheoryAbb,"_C",sep="")
	quant<-paste(listTheoryAbb,"_Q",sep="")
	## paste theory abbreviations
	omit<-which(quant=="Total_Q" | quant=="Total Coverage_Q") 
	names(appendix)<-c(qual,quant[-omit],"Total","Total_Coverage")
	## delete redundant variables
	del.var<-which(names(appendix)=="Total_C" | names(appendix)=="Total Coverage_C")
	appendix<-appendix[,-del.var]
	## show APPENDIX TABLE
	appendix
	## short VERSION: Table 4
	## choose Theoretical Arguments: IDMC, GOF, AE, NME, ALL
	rel.theories<-which(names(appendix)=="IDMC_C"| names(appendix)=="GoF_C"| names(appendix)=="AE_C" | names(appendix)=="NME_C"| names(appendix)=="ALL_C" | names(appendix)=="IDMC_Q"| names(appendix)=="GoF_Q" | names(appendix)=="AE_Q" | names(appendix)=="NME_Q"| names(appendix)=="ALL_Q" | names(appendix)=="Total" | names(appendix)=="Total_Coverage")
	## assign results for these theoretical arguements
	table4.paper<-appendix[, rel.theories]	
	## show table 4 from the paper
	table4.paper
	
	
