*** Replication syntax ***

* set the directory
* cd ""
use ZA7500_v4-0-0.dta 

* select the Italian sample
keep if country==380

* frequency distributions of the item of the battery of social distance
fre  v22 v23 v25 v26 v28 v29 v30

* recoding items: missing cases and not mentioned equal to zero
recode v22 v23 v25 v26 v28 v29 v30 (2 = 0) (-1 -2 =0), gen (razza alcool tossico omosex musulmani ebrei zingari)
tab1 razza-zingari
tab1 razza-zingari, miss

* recoding manipulated item: missing cases equal to zero
recode v24 v24a_IT v24b_IT (2 = 0) , gen (immlav lavstra immigrati)

tab1 immlav lavstra immigrati

tab1 razza-zingari immlav

* generating new variables: missing cases and not mentioned equal to zero
gen immlav0 = immlav
replace immlav0=0 if f24_IT==1 & (immlav==-1 | immlav==-2)
gen lavstra0 = lavstra
replace lavstra0=0 if f24_IT==2 & (lavstra==-1 | lavstra==-2)
gen immigrati0 = immigrati
replace immigrati0=0 if f24_IT==3 & (immigrati==-1 | immigrati==-2)

* variabile di servizio 
recode immlav0 (-4=0), gen (immlav1)
recode lavstra0 (-4=0), gen (lavstra1)
recode immigrati0 (-4=0), gen (immigrati1)

* generating a single item
gen var = immlav1 + lavstra1 + immigrati1

* Comparing the frequency distribution of the manipulated item by experimental condition
tab f24_IT var, row chi /* contingency table and chi-test of indipendence */
prtest var if f24_IT!=2, by(f24_IT) /* z-test between two proportions: "immigrants/foreign workers" vs "immigrants" */
prtest var if f24_IT!=3, by(f24_IT) /* z-test between two proportions: "immigrants/foreign workers" vs "foreign workers" */
prtest var if f24_IT!=1, by(f24_IT) /* z-test between two proportions: "immigrants" vs "foreign workers" */

* recode the variable indicating the experimental condition for a better graphical representation of the 
* percentage of "mentioned" answers to the manipulated item by experimental condition
recode f24_IT (1=2) (2=1) (3=3), gen(exp)
label define exp 2 "immigrants/foreign workers" 1 "foreign workers" 3 "immigrants"
label values exp exp
fre exp
logit var i.exp
margins, at (exp=(1 2 3)) 
marginsplot /* Figure A1: later edited with Graph Editor */

* RANDOMIZATION CHECKS: Table A1
* gender
tab v225 f24_IT , col chi nof
* education
* recode missing
recode v243_r (-2=.), gen(edu3)
label values edu3 v243_r
tab edu3 f24_IT if edu3>0, col chi nof
* age
recode age_r3 (1 2 =1) (3 4= 2)(5 6 7=3), gen(age3)
label define age3 1 "18-34" 2 "35/54" 3 "55 and more"
label values age3 age3
tab age3 f24_IT, col chi nof
* sx/dx
recode v102 (1/3= 1 "sx")(4 5 = 2 "csx")(6 7 = 3 "cdx")(8 9 10 = 4 "dx") (-2 -1 = 5 "dk/da"), gen (sxdx)
tab sxdx f24_IT, col chi nof
* area
recode v275c_N1 (38012 38017= 1 "Nord") (38018=2 "Centro")(38016 38015=3 "Sud e Isole"), gen (zona3)
tab zona3 f24_IT, col chi nof

******** Comparing IRT Anlysis with 2 parameters and IRT Anlysis with 1 parameters

* Immigrants/foreign workers
irt 1pl razza alcool tossico omosex musulmani ebrei zingari var if f24_IT==1
estimates store rasch

irt 2pl razza alcool tossico omosex musulmani ebrei zingari var if f24_IT==1
estimates store duepl
irtgraph icc, blocation

lrtest rasch duepl /* 2pl is preferrable */

* Foreign workers
irt 1pl razza alcool tossico omosex musulmani ebrei zingari var if f24_IT==2
estimates store rasch

irt 2pl razza alcool tossico omosex musulmani ebrei zingari var if f24_IT==2
estimates store duepl
irtgraph icc, blocation

lrtest rasch duepl /* 2pl is largely preferrable */

* Immigrants
irt 1pl razza alcool tossico omosex musulmani ebrei zingari var if f24_IT==3
estimates store rasch

irt 2pl razza alcool tossico omosex musulmani ebrei zingari var if f24_IT==3
estimates store duepl
irtgraph icc, blocation

******** Multi-group IRT Analysis

* Immigrants/foreign workers vs Immigrants: Table 1
irt 2pl razza alcool tossico omosex musulmani ebrei zingari var if f24_IT!=3, group(f24_IT)
irtgraph icc, blocation
estimates store nodif
estat greport

irt (2pl razza alcool tossico omosex musulmani ebrei zingari) (1: 2pl var) (2: 2pl var) if f24_IT!=3, group(f24_IT)
estimates store difq4
estat greport

lrtest nodif difq4

* Immigrants/foreign workers vs foreugn workers: Table 2
irt 2pl razza alcool tossico omosex musulmani ebrei zingari var if f24_IT!=2, group(f24_IT)
estimates store nodif
estat greport

irt (2pl razza alcool tossico omosex musulmani ebrei zingari) (1: 2pl var) (3: 2pl var) if f24_IT!=2, group(f24_IT)
estimates store difq4
estat greport

lrtest nodif difq4

********* Analysing the heterogeneity of the effects of the experimental condition on the item distribution by socio-demographics and ideology *****
* Five different logistic regression models in which the dependent variable is the dichotomous answer 
* to the item of social distance and the main independent variable is the interaction term between 
* the experimental condition and each of those variables (gender, age, level of education, geographical area and left-right orientation)
* by controlling for the remaining four variables

* Multivariate logistic regressions for Table A2 and A3
logit var i.f24_IT##i.v225 i.age3 i.edu3 i.zona3 i.sxdx
outreg2 using socdist1.xls, excel dec(2) replace
margins, dydx (f24_IT) at (v225=(1 2))
logit var i.f24_IT##i.age3 i.v225 i.edu3 i.zona3 i.sxdx
outreg2 using socdist1.xls, excel dec(2)
margins, dydx (f24_IT) at (age3=(1 2 3))
logit var i.f24_IT##i.edu3 i.v225 i.age3 i.zona3 i.sxdx
outreg2 using socdist1.xls, excel dec(2)
margins, dydx (f24_IT) at (edu3=(1 2 3))
logit var i.f24_IT##i.zona3 i.v225 i.age3 i.edu3 i.sxdx
outreg2 using socdist1.xls, excel dec(2)
margins, dydx (f24_IT) at (zona3=(1 2 3))
logit var i.f24_IT##i.sxdx i.v225 i.edu3 i.age3 i.zona3
outreg2 using socdist1.xls, excel dec(2)
margins, dydx (f24_IT) at (sxdx=(1 2 3 4 5))
