Datasets and software

Software: Stata and R

In this module, instructions for fitting the models will be provided for two statistical software packages, Stata and R. We assume that you are reasonably familiar with the use of at least one of these packages in general.

For R, you will need to install an additional package called lavaan. Please click here for more information on installing this package

Datasets

The Stata data file was obtained by downloading the full ESS5 Stata data file from the ESS website. It was then reduced to the file used here by keeping only the 15 variables listed in the previous section, plus the variables idno (respondent ID) and cntry (country of the respondent).

The Stata file was then imported into R using the read.dta function from the foreign package in R, and saved from R as ESS5Police.RData.

If you started with a dataset in SPSS, you could save it as a Stata data file directly from SPSS. To import the SPSS data file into R, you could use the read.spss function from the foreign package in R.

Begin by downloading the data file ESS5Police.dta (for Stata) or ESS5Police.RData (for R) by clicking on the links below:

In Stata, you then open the data file using the File → Open menu, and in R using the File → Load Workspace menu.

Command files

Stata and R commands for data preparation and data analysis exercises for these data are given throughout this module. All of the commands for these different steps are also included in single files which you can download by clicking on the links below:

Preliminary analysis: Missing values and recoding some items

Before you can start analysing the data, you should carry out the following two steps of data processing:

See below for the commands for carrying out these steps:

Stata commands: To run this and any other commands included in this module, you can copy and paste them into the do-file editor in Stata, and run the commands from there. These commands assume that you have first opened the data file ESS5Police.dta in Stata, as described above. After you run the commands here, save the data file again to save the changes.

Show Stata commands

// Open the data file ESS5Police.dta in Stata
// Preliminary data processing:
* Recoding some values of the variables as missing:
mvdecode plcrspc plcfrdc plcexdc ///
plcrgwr plcipvl gsupplc ///
caplcst widprsn wevdct, mv(7 8 9)
mvdecode plcpvcr plccbrg plcarcr ///
bplcdc doplcsy dpcstrb, mv(77 88 99)
*** Two additional special cases:
mvdecode plcexdc, mv(5)
mvdecode plcarcr, mv(55)
* Reversing the coding of three items:
replace plcrgwr=6-plcrgwr
replace plcipvl=6-plcipvl
replace gsupplc=6-gsupplc
label define plcrgwr 1 "Disagree strongly" 2 "Disagree" ///
3 "Neither agree nor disagree" 4 "Agree" 5 "Agree strongly", replace
label copy plcrgwr plcipvl, replace
label copy plcrgwr gsupplc, replace
// Save the data file ESS5Police.dta, to save these changes

R commands: : To run this and any other commands included in this module, you can copy and paste them into the script editor in R, and run the commands from there. These commands assume that you have first loaded the data into R, as described above. After you run these commands, the data will be in the data frame ESS5Police, ready for the analyses.

Show R commands

# Load the file ESS5Police.Rdata to R
# if you have not done so previously. This loads the data frame ESS5Police.
# Preliminary data processing:
# Recoding some values of the variables as missing:
for(v.tmp in c("plcrspc", "plcfrdc", "plcexdc",
"plcrgwr", "plcipvl", "gsupplc",
"caplcst", "widprsn", "wevdct")){
tmp <- ESS5Police[,v.tmp]
ESS5Police[(tmp==7 | tmp==8 | tmp==9)&!is.na(tmp),v.tmp] <- NA
}
for(v.tmp in c("plcpvcr","plccbrg","plcarcr",
"bplcdc","doplcsy", "dpcstrb")){
tmp <- ESS5Police[,v.tmp]
ESS5Police[(tmp==77 | tmp==88 | tmp==99)&!is.na(tmp),v.tmp] <- NA
}
## Two additional special cases:
ESS5Police[ESS5Police$plcexdc==5 & !is.na(ESS5Police$plcexdc),"plcexdc"] <- NA
ESS5Police[ESS5Police$plcarcr==55 & !is.na(ESS5Police$plcarcr),"plcarcr"] <- NA
# Reversing the coding of three items:
ESS5Police$plcrgwr=6-ESS5Police$plcrgwr
ESS5Police$plcipvl=6-ESS5Police$plcipvl
ESS5Police$gsupplc=6-ESS5Police$gsupplc
# The data frame ESS5Police is now ready for the analyses

Go to next chapter >>