-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgoverment_expenditure_un_compliance_2017.R
151 lines (115 loc) · 4.54 KB
/
goverment_expenditure_un_compliance_2017.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
# Start
# title: "Multiple Regression and Causal Analysis"
# author: "Raquel Baeta"
# date: '2022-06-12'
# load packages
library(readr)
library(ggplot2)
library(corrplot)
library(stargazer)
# load data
data <- read_csv("data.csv", show_col_types = FALSE)
# pull put the year 2017 and make a new data frame
df_2017 <- data[which(data$year == "2017"),]
# remove nas
df_2017 <- na.omit(df_2017)
# peak at data
str(df_2017) # structure
dim(df_2017) # dimension
head(df_2017) # first few rows
summary(df_2017) # summary
###
# Univariate analysis
###
# Figure 1: A bar plot of the raw variable of ‘government expenditure’ from the World Bank.
barplot(df_2017$exp,
xlab = "Countries",
ylab = "Government Expenditure",
main = "Government Expenditure: A brief overview")
# Figure 2: A bar plot of the raw variable ‘gdp’ from the World Bank.
barplot(df_2017$gdp,
xlab = "Countries",
ylab = "Gross Domestic Product (GDP)",
main = "Gross Domestic Product (GDP): A Brief overview")
# Figure 3: A bar plot of ‘all_drugs’ that includes cannabis, cocaine and opioid seizures from the UNODC.
barplot(df_2017$all_drugs,
xlab = "Countries",
ylab = "All drugs: number of seziures",
main = "All drugs: A Brief overview")
# Figure 4: A bar plot of the variable ‘un1’ of number of countries that have signed (0) or haven’t signed (1).
hist(df_2017$un1,
xlab = "Countries that have signed the 1961 treaty",
ylab = "Countries",
main = "Single Convention on Narcotic Drugs (1961): A Brief overview")
# overviews
summary(df_2017$exp) # government expenditure
summary(df_2017$gdp) # gross domestic product
summary(df_2017$all_drugs) # cannabis, cocaine and opioids
# filtering all countries that have signed a convention
signed <- df_2017[which(df_2017$un1 == "1"),]
# filtering all countries that haven't signed a convention
not_signed <- df_2017[which(df_2017$un1 == "0"),]
###
# Transfromed variables
###
# Figure 5: A bar plot of the transformed variable of ‘government expenditure’ from the World Bank.
barplot(df_2017$exp_ln,
xlab = "Countries",
ylab = "Government Expenditure",
main = "Government Expenditure (ln): A brief overview")
# Figure 6: A bar plot of the transformed variable of ‘gdp’ from the World Bank.
barplot(df_2017$gdp_ln,
xlab = "Countries",
ylab = "Gross Domestic Product (GDP)",
main = "Gross Domestic Product (GDP) (ln): A Brief overview")
# overviews
summary(df_2017$exp_ln) # government expenditure
summary(df_2017$gdp_ln) # gross domestic product
###
# Bivariate analysis
###
# correlation matrix
drugsdf <- subset(df_2017, select = c(exp_ln, all_drugs, gdp_ln, un1))
# correlation
drugs.cor = cor(drugsdf)
# Figure 7: A correlation matrix of variables: ‘government expenditure’, ‘all drugs’, ‘gdp’, and ‘un1’.
# correlation plot
corrplot(drugs.cor)
# correlation matrix
orrelation.matrix <- cor(drugsdf[,c("all_drugs","exp_ln","gdp_ln", "un1")])
stargazer(correlation.matrix, title = "Correlation Matrix", type = "html")
# Figure 8: A scatterplot matrix of variables: ‘government expenditure’, ‘all drugs’, ‘gdp’, and ‘un1’.
# scatterplot matrix
plot(drugsdf, main = "Scatterplot matrix of all variables")
###
# Effect decomposition
###
# A bivariate regression: 'all drugs' and 'government expenditure'
# bivariate regression
lm.out <- summary(lm(all_drugs ~ exp_ln, data = drugsdf))
lm.out
# multiple regression: 'all drugs', 'government expenditure' and 'GDP'
lm.out2 <- lm(all_drugs ~ exp_ln + gdp_ln, data =drugsdf)
summary(lm.out)
# multiple regression: 'all drugs', 'government expenditure' and 'United Nations Drug Control Treaty'
lm.out3 <- lm(all_drugs ~ exp_ln + un1, data = drugsdf)
summary(lm.out3)
# multiple regression: 'all drugs', 'government expenditure', 'GDP' and 'United Nations Drug Control Treaty'
lm.out4 <- lm(all_drugs ~ exp_ln + gdp_ln + un1, data = drugsdf)
summary(lm.out4)
# regression table
stargazer(lm.out2, lm.out3, lm.out4,
title = "Regression Results",
dep.var.labels = c("Drug seizures"),
covariate.labels = c("Government Eexpenditure",
"Gross Domestic Product",
"United Nation Drug Control Treaty"),
omit.stat = c("LL", "ser", "f"),
ci = TRUE,
ci.level = 0.90,
single.row = TRUE)
# plot regression
par(mfrow = c(2,2)) # splits window
plot(lm.out4, cex.caption = 0.6, cex.id = 0.6,
main = "Regression Plot")
# end