-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfinal_project.R
217 lines (176 loc) · 7.34 KB
/
final_project.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
rm(list=ls())
graphics.off()
library(ggplot2)
library(gridExtra)
library(ggpmisc)
# library(GGally)
# library(PerformanceAnalytics)
###############################################################
# A conflict-year dataset with information on armed conflict
# where at least one party is the government of a state in
# the time period 1946-2019.
load("data/ucdp-prio-acd-201.RData")
# Count by year
statedata <- ucdp_prio_acd_201
stateyears <- statedata$year
stateyears_unique <- sort(unique(statedata$year))
stateconflicts <- data.frame(matrix(999,ncol=2,nrow=length(stateyears_unique)))
stateconflicts[,1] <- stateyears_unique
names(stateconflicts) <- c("year","count")
for (n in 1:length(stateyears_unique)){
stateconflicts[n,2] <- sum(stateyears==stateyears_unique[n])
}
# # plot
# plot(x=stateconflicts$year,y=stateconflicts$count,
# ylab = "global armed conflicts involving a state government",
# xlab = "year")
# ggplot
stateplot <- ggplot(data=stateconflicts, aes(x=year, y=count)) +
geom_point() +
# theme(panel.grid.major = element_blank(),
# panel.grid.minor = element_blank()) +
ylab("Global armed conflicts (state)") + xlab("Year") +
geom_smooth()
# geom_smooth(aes(colour="LOESS"))+
# scale_colour_manual(name="legend", values=c("red"))
print(stateplot)
###############################################################
# A conflict-year dataset containing information on communal
# and organized armed conflict where none of the parties is
# the government of a state.
load("data/ucdp-nonstate-201.RData")
# Count by year
nonstatedata <- ucdp_nonstate_201
nonstateyears <- nonstatedata$year
nonstateyears_unique <- sort(unique(nonstatedata$year))
nonstateconflicts <- data.frame(matrix(999,ncol=2,nrow=length(nonstateyears_unique)))
nonstateconflicts[,1] <- nonstateyears_unique
names(nonstateconflicts) <- c("year","count")
for (n in 1:length(nonstateyears_unique)){
nonstateconflicts[n,2] <- sum(nonstateyears==nonstateyears_unique[n])
}
# plot
# plot(x=nonstateconflicts[,1],y=nonstateconflicts[,2],
# ylab = "global armed conflicts involving no state government",
# xlab = "year")
# ggplot
nonstateplot <- ggplot(data=nonstateconflicts, aes(x=year, y=count)) +
geom_point() +
# theme(panel.grid.major = element_blank(),
# panel.grid.minor = element_blank()) +
ylab("Global armed conflicts (non-state)") + xlab("Year") +
geom_smooth()
# geom_smooth(aes(colour="LOESS"))+
# scale_colour_manual(name="legend", values=c("red"))
print(nonstateplot)
##############################################################
# Global temperature anomaly from NASA
tempdata.in <- read.delim("data/temperature_anomaly.txt",sep="")
temps <- data.frame(matrix(999,ncol=2,nrow=length(tempdata.in$X1880)))
names(temps) <- c("year","temp")
temps[,1] <- tempdata.in$X1880
temps[,2] <- tempdata.in$X.0.16
# plot
# plot(x=temps[1,],y=temps[2,],
# xlab = "year",
# ylab = "mean temperature anomaly in deg C")
anomalyplot <- ggplot(data=temps, aes(x=year, y=temp)) +
geom_point() +
# theme(panel.grid.major = element_blank(),
# panel.grid.minor = element_blank()) +
ylab("Mean global temperature anomaly [deg C]") + xlab("Year") +
geom_smooth()
# geom_smooth(aes(colour="LOESS"))+
# scale_colour_manual(name="legend", values=c("red"))
print(anomalyplot)
##############################################################
# Correlation between temperature anomaly and state conflicts
# line up the years
begyear <- stateconflicts$year[1]
endyear <- stateconflicts$year[length(stateconflicts$year)]
statetemps = subset(temps, year>=begyear&year<=endyear) # trim to desired years
statecor <- cor.test(x=stateconflicts$count,y=statetemps$temp
,method="pearson", exact=FALSE)
# pearson's correlation
stateplotdata <- stateconflicts
stateplotdata$temps <- statetemps$temp
stateplotdata <- subset (stateplotdata, select = -year)
##############################################################
# Correlation between temperature anomaly and state conflicts
# line up the years
begyear <- nonstateconflicts$year[1]
endyear <- nonstateconflicts$year[length(nonstateconflicts$year)]
nonstatetemps = subset(temps, year>=begyear&year<=endyear) # trim to desired years
# pearson's correlation
nonstatecor <- cor.test(x=nonstateconflicts$count,y=nonstatetemps$temp
,method="pearson", exact=FALSE)
nonstateplotdata <- nonstateconflicts
nonstateplotdata$temps <- nonstatetemps$temp
nonstateplotdata <- subset (nonstateplotdata, select = -year)
##############################################################
# linear regressions
# state
statecordata <- stateconflicts
statecordata[,3] <- statetemps$temp
names(statecordata) <- c("year","conflicts","temps")
linear_state <- lm(conflicts~temps,data=statecordata) # linear regression
summary(linear_state)
anova(linear_state)
my.formula <- y ~ x
p_linear <- ggplot(data = statecordata, aes(x = temps, y = conflicts)) +
geom_smooth(method = "lm", se=FALSE, color="red", formula = my.formula) +
stat_poly_eq(formula = my.formula,
aes(label = paste(..eq.label.., ..rr.label.., sep = "~~~")),
parse = TRUE) +
ylab("Global armed conflict (state)") +
xlab("Mean Temperature Anomaly [deg C]") +
geom_point()
print(p_linear)
# nonstate
nonstatecordata <- nonstateconflicts
nonstatecordata[,3] <- nonstatetemps$temp
names(nonstatecordata) <- c("year","conflicts","temps")
linear_nonstate <- lm(conflicts~temps,data=nonstatecordata) # linear regression
summary(linear_nonstate)
anova(linear_nonstate)
my.formula <- y ~ x
p_linear_nonstate <- ggplot(data = nonstatecordata, aes(x = temps, y = conflicts)) +
geom_smooth(method = "lm", se=FALSE, color="red", formula = my.formula) +
stat_poly_eq(formula = my.formula,
aes(label = paste(..eq.label.., ..rr.label.., sep = "~~~")),
parse = TRUE) +
ylab("Global armed conflict (non-state)") +
xlab("Mean Temperature Anomaly [deg C]") +
geom_point()
print(p_linear_nonstate)
##############################################################
# plots
# state
statecorplot <- ggplot(data=stateplotdata, aes(x=temps,y=count)) +
geom_point() +
ylab("Global armed conflict (state)") +
xlab("Mean Temperature Anomaly [deg C]") +
# geom_smooth()
geom_abline(intercept = 26.400, slope = 25.249, color="red")
# geom_smooth(aes(colour="LOESS"))+
# scale_colour_manual(name="legend", values=c("red"))
print(statecorplot)
# non-state
nonstatecorplot <- ggplot(data=nonstateplotdata, aes(x=temps,y=count)) +
geom_point() +
ylab("Global armed conflict (non-state)") +
xlab("Mean Temperature Anomaly [deg C]") +
geom_abline(intercept = 1.464, slope = 67.564, color="red")
# geom_smooth()
# geom_smooth(aes(colour="LOESS"))+
# scale_colour_manual(name="legend", values=c("red"))
print(nonstatecorplot)
##############################################################
# grid arrange for plots
# save plot to file without using ggsave
png("!grid1.png")
grid.arrange(stateplot,nonstateplot,anomalyplot,ncol=2,nrow=2)
dev.off()
png("!grid2.png")
grid.arrange(p_linear,p_linear_nonstate,ncol=2)
dev.off()