-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhdi_religion.R
117 lines (94 loc) · 3.05 KB
/
hdi_religion.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
# Start
# title: "Examining the Human Development Index and Non-religious Groups"
# author: "by Raquel Baeta"
# date: "2021-11-10"
# load packages
library(tidyverse)
library(readr)
library(ggplot2)
library(car)
# import data and assign a new data frame
xn <- read_csv("~/Desktop/xn.csv",
col_names = TRUE,
na = "NA")
# data insepction
view(xn) # view the data frame
problems(xn) # check failures
str(xn) # check structure
# data selection
my_data <- select(xn, country, pop, hdi, nonrelig) # selecting variables
view(my_data) # check new data set
# data transformation
xn$pop <- log(xn$pop) # log population
summary(xn$pop) # check
# missing data
colSums(is.na(my_data)) # checking missing values
missingdata <- my_data[!complete.cases(my_data), ]
missingdata # check
# remove missing data
my_data <- na.omit(my_data)
sum(is.na(my_data)) # check again for missing data
# plot overview
plot(my_data, col = "blue")
# hdi and non-religious
ggplot(data = xn) +
geom_point(mapping = aes(x = hdi, y = nonrelig))
# population and non-religious
ggplot(data = xn) +
geom_point(mapping = aes(x = pop, y = nonrelig))
# hdi and population
ggplot(data = xn) +
geom_point(mapping = aes(x = hdi, y = pop))
# hdi and violent_decol
ggplot(data = xn, mapping = aes(hdi, violent_decol)) +
geom_point() +
geom_smooth(method = "lm", col = "black") +
facet_wrap(vars(continent)) +
xlab("Human Development Index") +
ylab("Violent Conflict with Western Colonial Power") +
scale_x_log10(labels=scales::comma) +
theme_bw()
# population and christianity
ggplot(data = xn, mapping = aes(pop, christian)) +
geom_point() +
geom_smooth(method = "lm", col = "black") +
facet_wrap(vars(continent)) +
xlab("Population") +
ylab("Christianity") +
scale_x_log10(labels = scales::comma) +
theme_bw()
# population and non-religious
ggplot(data = xn, mapping = aes(pop, nonrelig)) +
geom_point() +
geom_smooth(method = "lm", col = "black") +
facet_wrap(vars(continent)) +
xlab("Population") +
ylab("Non-religious") +
scale_x_log10(labels = scales::comma) +
theme_bw()
# linear regression of hdi and non-religion
my_reg <- lm(hdi ~ pop + nonrelig, data = my_data)
summary(my_reg)
# plot
plot(hdi ~ pop + nonrelig, data = my_data)
abline(my_reg)
# regression plotted in ggplot2
ggplot(my_data, aes(x = hdi, y = nonrelig)) +
geom_point() +
stat_smooth(method = "lm", col = "red")
# regression plotted in ggplot2
ggplotRegression <- function (my_data)
{
require(ggplot2)
ggplot(my_data$model, aes_string(x = names(my_data$model)[2], y = names(my_data$model)[1])) +
geom_point() +
stat_smooth(method = "lm", col = "red") +
labs(title = paste("Adj R2 = ",signif(summary(my_data)$adj.r.squared, 5),
"Intercept =",signif(my_data$coef[[1]],5 ),
" Slope =",signif(my_data$coef[[2]], 5),
" P =",signif(summary(my_data)$coef[2,4], 5)))
}
# final regression plot
my_regression <- lm(nonrelig ~ hdi, data = my_data)
ggplotRegression(my_regression)
# end