-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathreplication.R
78 lines (63 loc) · 3.02 KB
/
replication.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# All files required for full replication are available at https://github.com/andrewheiss/Attitudes-in-the-Arab-World
#--------------------------
# Load data and libraries
#--------------------------
library(car)
library(ggplot2)
library(reshape2)
library(plyr)
library(xtable)
setwd("~/Documents/Duke 2012-2013/Spring 2013/Polsci 733/Attitudes in the Arab World Replication")
load("Barometer.RData")
set.seed(1234)
#---------------
# Build models
#---------------
# Save the formulas so they can be reused more easily
form.logit <- formula(autocracy.bad ~ quran + prime.minister +
citizen.influence + maintain.order +
education + age + family.econ)
form.ologit <- formula(autocracy.bad.ordinal ~ quran + prime.minister +
citizen.influence + maintain.order +
education + age + family.econ)
# Corresponding human-readable names for nicer output
nice.names <- c("Read Qur'an", "Prime minister", "Citizen influence",
"Maintain order", "Education", "Age", "Family economic status")
# logit models for all countries combined
all.countries.logit <- glm(form.logit, data=barometer,
subset=(dem.best==1), family=binomial(link="logit"))
original4.logit <- glm(form.logit, data=barometer.original4,
subset=(dem.best==1), family=binomial(link="logit"))
# ologit models for all countries combined
all.countries <- polr(form.ologit, data=barometer,
subset=(dem.best==1), method="logistic", Hess=TRUE)
original4 <- polr(form.ologit, data=barometer.original4,
subset=(dem.best==1), method="logistic", Hess=TRUE)
# Function to create a list of individual country models
# bquote() + .() passes the actual names of the arguments into the model call; eval() actually runs the model
run.country.models.logit <- function(country, form) {
model <- bquote(glm(.(form), data=barometer,
subset=(dem.best==1 & country.name==.(country)),
family=binomial(link="logit")))
eval(model)
}
run.country.models <- function(country, form) {
model <- bquote(polr(.(form), data=barometer,
subset=(dem.best==1 & country.name==.(country)),
method="logistic", Hess=TRUE))
eval(model)
}
# Create a list of models for all countries, given a model formula
country.models.logit <- lapply(levels(barometer$country.name), FUN=run.country.models.logit, form=form.logit)
names(country.models.logit) <- levels(barometer$country.name) # Name the list for convenience
country.models <- lapply(levels(barometer$country.name), FUN=run.country.models, form=form.ologit)
names(country.models) <- levels(barometer$country.name) # Name the list for convenience
#-----------------------
# Summarize all models
#-----------------------
summary(all.countries.logit)
summary(original4.logit)
summary(all.countries)
summary(original4)
lapply(country.models.logit, summary)
lapply(country.models, summary)