-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStatistical_Analysis.R
254 lines (196 loc) · 6.82 KB
/
Statistical_Analysis.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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
install.packages("ggstatsplot")
install.packages("qqplotr")
install.packages("RVAideMemoire")
install.packages("fBasics")
install.packages("car")
install.packages("corrplot")
install.packages("caret")
install.packages("doBy")
install.packages("TTR")
install.packages("forecast")
library(car)
library(corrplot)
library(caret)
library(tidyverse)
library(fBasics)
library(RVAideMemoire)
library(datarium)
library(qqplotr)
library(ggplot2)
library(car)
library(doBy)
library(ggstatsplot)
library(dplyr)
library(TTR)
library(forecast)
dataset <- read.csv('R_HIV_dataset.csv', header = TRUE)
### Descriptive analysis ###
head(dataset)
dataset$HDI = as.factor(dataset$HDI)
summary(dataset)
str(dataset)
hist(dataset$Deaths.by.AIDS)
#select unique values in team and points columns
dataset %>% distinct(Country, HDI)
numeric_data = dataset[ ,c("Deaths.by.AIDS","TOTAL.PLWH","TOTAL.PNIWH")]
head(numeric_data)
boxplot(numeric_data)
#basic stats
basicStats(numeric_data)
options(scipen = 999)
basicStats(numeric_data)
plot(numeric_data)
boxplot(numeric_data,horizontal = TRUE)
#normality test visual before transformation
qqnorm(dataset$Deaths.by.AIDS, col=6)
qqline(dataset$Deaths.by.AIDS, col=3)
#Transformation
norm_deaths = as.data.frame(log10(dataset$Deaths.by.AIDS))
norm_PLWH = as.data.frame(log10(dataset$TOTAL.PLWH))
norm_PNIWH = as.data.frame(log10(dataset$TOTAL.PNIWH))
#Renaming column name
names(norm_deaths)[names(norm_deaths)=="log10(dataset$Deaths.by.AIDS)"] = "Deaths"
names(norm_PLWH)[names(norm_PLWH)=="log10(dataset$TOTAL.PLWH)"] = "PLWH"
names(norm_PNIWH)[names(norm_PNIWH)=="log10(dataset$TOTAL.PNIWH)"] = "PNIWH"
norms_d = data.frame(norm_deaths,norm_PLWH,norm_PNIWH)
numeric_data = norms_d
#Merging with dataset
new_dataset = data.frame(dataset,numeric_data)
numeric_data = new_dataset[ ,c("Deaths", "ART.PLWH", "ART.PMTCT.LWH", "Undernourished",
"PLWH", "PNIWH")]
#normality test visual after transformation
qqnorm(new_dataset$Deaths, col=6)
qqline(new_dataset$Deaths, col=3)
#outlier
boxplot(Deaths.by.AIDS ~ HDI, data = dataset, names=c("High","Low"),
xlab="HDI", ylab="# of Deaths",
main="Deaths by HDI")
#resolved outlier
boxplot(Deaths ~ HDI, data = new_dataset, names=c("High","Low"),
xlab="HDI", ylab="# of Deaths",
main="Deaths by HDI")
### correlation ###
corrplot(cor(numeric_data),method = "number", type = "upper")
### Regression ###
#MLR
#splitting my data set
least_dev = new_dataset[new_dataset$HDI=='Low',]
least_dev
developed = new_dataset[new_dataset$HDI=='High',]
developed
#numerical var for least dev
least_dev_num_var = least_dev[ ,c("Deaths", "ART.PLWH", "ART.PMTCT.LWH", "Undernourished",
"PLWH", "PNIWH")]
least_dev_num_var
corrplot(cor(least_dev_num_var),method = "number", type = "lower")
#model trials
model_1 = lm(Deaths ~ PNIWH, least_dev_num_var)
summary.lm(model_1)
model_2 = lm(Deaths ~ PNIWH + PLWH, least_dev_num_var)
summary.lm(model_2)
model_3 = lm(Deaths ~ PNIWH + PLWH + Undernourished, least_dev_num_var)
summary.lm(model_3)
pairs(least_dev_num_var[,c(1,6,5,4)], lower.panel = NULL, pch = 19, cex = 0.2)
model_3a = lm(Deaths ~ PNIWH + Undernourished, least_dev_num_var)
summary.lm(model_3a)
pairs(least_dev_num_var[,c(1,6,5,4)], lower.panel = NULL, pch = 19, cex = 0.2)
vif(model_3a)
model_4 = lm(Deaths ~ PNIWH + PLWH + ART.PMTCT.LWH + Undernourished, least_dev_num_var)
summary.lm(model_4)
data.frame(colnames(least_dev_num_var))
pairs(least_dev_num_var[,c(1,6,5,3,4)], lower.panel = NULL, pch = 19, cex = 0.2)
#final MLR model
model_4a = lm(Deaths ~ PNIWH + ART.PMTCT.LWH + Undernourished, least_dev_num_var)
summary.lm(model_4a)
pairs(least_dev_num_var[,c(1,6,3,4)], lower.panel = NULL, pch = 19, cex = 0.2)
plot(model_4a,1)
plot(model_4a,2)
plot(model_4a,3)
vif(model_4a)
### Time series ###
#Case study of Ghana
case_study = new_dataset[new_dataset$Country == 'Ghana', ]
case_study
ghana = case_study[ ,c("Deaths.by.AIDS")]
ghanatimeseries <- ts(ghana, frequency=1, start=c(2010,1))
#plotting
plot.ts(ghanatimeseries)
#decomposing/smoothing
ghanaSMA3 <- SMA(ghanatimeseries,n=3)
plot.ts(ghanaSMA3)
#in-sample forecast
ghanaforecasts <- HoltWinters(ghanatimeseries, gamma=FALSE)
ghanaforecasts
ghanaforecasts$SSE
plot(ghanaforecasts)
#forecasting 10 years using Holt
ghanaforecasts_2 <- forecast(ghanaforecasts, h=10)
plot(ghanaforecasts_2)
acf(ghanaforecasts_2$residuals, lag.max=10, na.action = na.pass)
Box.test(ghanaforecasts_2$residuals, lag=8, type="Ljung-Box")
# make time series plot for errors
plot.ts(ghanaforecasts_2$residuals)
# make a histogram
ghanaforecasts_2$residuals <- ghanaforecasts_2$residuals[!is.na(ghanaforecasts_2$residuals)]
#forecast function
plotForecastErrors <- function(forecasterrors)
{
# make a histogram of the forecast errors:
mybinsize <- IQR(forecasterrors)/4
mysd <- sd(forecasterrors)
mymin <- min(forecasterrors) - mysd*5
mymax <- max(forecasterrors) + mysd*3
# generate normally distributed data with mean 0 and standard deviation mysd
mynorm <- rnorm(10000, mean=0, sd=mysd)
mymin2 <- min(mynorm)
mymax2 <- max(mynorm)
if (mymin2 < mymin) { mymin <- mymin2 }
if (mymax2 > mymax) { mymax <- mymax2 }
# make a red histogram of the forecast errors, with the normally distributed data overlaid:
mybins <- seq(mymin, mymax, mybinsize)
hist(forecasterrors, col="red", freq=FALSE, breaks=mybins)
# freq=FALSE ensures the area under the histogram = 1
# generate normally distributed data with mean 0 and standard deviation mysd
myhist <- hist(mynorm, plot=FALSE, breaks=mybins)
# plot the normal curve as a blue line on top of the histogram of forecast errors:
points(myhist$mids, myhist$density, type="l", col="blue", lwd=2)
}
plotForecastErrors(ghanaforecasts_2$residuals)
### Comparative Analysis ###
#Anova assumptions failed
one_anova = new_dataset %>%
select(-Deaths.by.AIDS,-ART.PLWH,-ART.PMTCT.LWH,-Undernourished,-TOTAL.PLWH,-TOTAL.PNIWH,-PLWH,-PNIWH)
#Assumptions 4-6
boxplot(Deaths ~ HDI, data = one_anova, names=c("High","Low"),
xlab="HDI", ylab="# of Deaths",
main="Deaths by HDI")
#5
byf.shapiro(Deaths ~ HDI, data = one_anova)
bartlett.test(Deaths ~ HDI, data = one_anova)
#Wilcoxon on raw data
WilTestR = new_dataset %>%
select(HDI,Deaths.by.AIDS)
ggplot(WilTestR) +
aes(x = HDI, y = Deaths.by.AIDS, fill = HDI) +
geom_boxplot() +
theme(legend.position = "none")
testR = wilcox.test(WilTestR$Deaths.by.AIDS ~ WilTestR$HDI)
testR
#Wilcoxon on transformed data
WilTestT = new_dataset %>%
select(HDI,Deaths)
ggplot(WilTestT) +
aes(x = HDI, y = Deaths, fill = HDI) +
geom_boxplot() +
theme(legend.position = "none")
testT = wilcox.test(WilTestT$Deaths ~ WilTestR$HDI)
testT
#plot with statistical results
ggbetweenstats(
data = WilTestT,
x = HDI,
y = Deaths,
plot.type = "box",
type = "nonparametric",
centrality.plotting = FALSE
)