-
Notifications
You must be signed in to change notification settings - Fork 0
/
R Script - Regression Model.R
340 lines (235 loc) · 10.6 KB
/
R Script - Regression Model.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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
#FINAL PROJECT
#installing packages
install.packages("car")
#importing libraries
library(FSA)
library(FSAdata)
library(magrittr)
library(dplyr)
library(tidyr)
library(plyr)
library(ggplot2)
library(scales)
library(ggboxplot)
library(MASS)
library(ggpubr)
library(broom)
library(corrplot)
library(gtsummary)
library(car)
#reading csv file
deathrate_dataset <- read.csv("number-of-deaths-by-risk-factor.csv")
deathrate_dataset
#describing the data
colnames(deathrate_dataset)
start_records <- head(deathrate_dataset,10)
start_records
end_records <- tail(deathrate_dataset,10)
end_records
dim(deathrate_dataset)
str(deathrate_dataset)
summary_dataset <- summary(deathrate_dataset)
summary_dataset
classtype_variable <- sapply(deathrate_dataset, class)
classtype_variable
#descriptive analysis
#alcohol use
min(deathrate_dataset$Alcohol.use)
max(deathrate_dataset$Alcohol.use)
mean(deathrate_dataset$Alcohol.use)
median(deathrate_dataset$Alcohol.use)
sd(deathrate_dataset$Alcohol.use)
range(deathrate_dataset$Alcohol.use)
summary(deathrate_dataset$Alcohol.use)
#air pollution
min(deathrate_dataset$Air.pollution)
max(deathrate_dataset$Air.pollution)
mean(deathrate_dataset$Air.pollution)
median(deathrate_dataset$Air.pollution)
sd(deathrate_dataset$Air.pollution)
range(deathrate_dataset$Air.pollution)
summary(deathrate_dataset$Air.pollution)
#smoking
min(deathrate_dataset$Smoking)
max(deathrate_dataset$Smoking)
mean(deathrate_dataset$Smoking)
median(deathrate_dataset$Smoking)
sd(deathrate_dataset$Smoking)
range(deathrate_dataset$Smoking)
summary(deathrate_dataset$Smoking)
#drug use
min(deathrate_dataset$Drug.use)
max(deathrate_dataset$Drug.use)
mean(deathrate_dataset$Drug.use)
median(deathrate_dataset$Drug.use)
sd(deathrate_dataset$Drug.use)
range(deathrate_dataset$Drug.use)
summary(deathrate_dataset$Drug.use)
#hand washing facility
min(deathrate_dataset$No.access.to.handwashing.facility)
max(deathrate_dataset$No.access.to.handwashing.facility)
mean(deathrate_dataset$No.access.to.handwashing.facility)
median(deathrate_dataset$No.access.to.handwashing.facility)
sd(deathrate_dataset$No.access.to.handwashing.facility)
range(deathrate_dataset$No.access.to.handwashing.facility)
summary(deathrate_dataset$No.access.to.handwashing.facility)
#frequency table
unique_entity <- unique(deathrate_dataset$Entity)
unique_entity
table_counrty <- table(deathrate_dataset$Entity)
table_counrty
count_entity <- count(deathrate_dataset$Entity)
count_entity
#creating new subset of dataset
#Entity = India
india_entity <- subset(deathrate_dataset, subset=(deathrate_dataset$Entity=="India"))
india_entity
#Entity = North America
northamerica_entity <- subset(deathrate_dataset, subset = (deathrate_dataset$Entity=="North America"))
northamerica_entity
#Entity = Maldives
maldives_entity <- subset(deathrate_dataset, subset = (deathrate_dataset$Entity=="Maldives"))
maldives_entity
#visualizations
#graph 1 : Boxplot of the attributes for Entity India
par(mfrow=c(2,2))
boxplot(india_entity$Alcohol.use, col = "green", main = "Alcohol Use in India")
boxplot(india_entity$Drug.use, col = "yellow", main = "Drug Use in India")
boxplot(india_entity$Smoking, col = "orange", main = "Smoking in India")
boxplot(india_entity$Air.pollution, col = "blue", main = "Air Pollution in India")
#graph 2 : Boxplot of the attributes for Entity North America
par(mfrow=c(2,2))
boxplot(northamerica_entity$Alcohol.use, col = "blue", main = "Alcohol Use in North America")
boxplot(northamerica_entity$Drug.use, col = "orange", main = "Drug Use in North America")
boxplot(northamerica_entity$Smoking, col = "green", main = "Smoking in North America")
boxplot(northamerica_entity$Air.pollution, col = "yellow", main = "Air Pollution in North America")
#graph 3 : Boxplot of the attributes for Entity Maldives
par(mfrow=c(2,2))
boxplot(maldives_entity$Alcohol.use, col = "yellow", main = "Alcohol Use in Maldives")
boxplot(maldives_entity$Drug.use, col = "green", main = "Drug Use in Maldives")
boxplot(maldives_entity$Smoking, col = "blue", main = "Smoking in Maldives")
boxplot(maldives_entity$Air.pollution, col = "orange", main = "Air Pollution in Maldives")
#graph 4 : Density Plot of the attributes for Entity India
par(mfrow=c(2, 2))
plot(density(india_entity$Alcohol.use), main = "Density Plot, Entity-India : Risk Factor, Alcohol Use", ylab = "Frequency")
polygon(density(india_entity$Alcohol.use), col = "cadetblue")
plot(density(india_entity$Drug.use), main = "Density Plot, Entity-India : Risk Factor, Drug Use", ylab = "Frequency")
polygon(density(india_entity$Drug.use), col = "blue")
plot(density(india_entity$Smoking), main = "Density Plot, Entity-India : Risk Factor, Smoking", ylab = "Frequency")
polygon(density(india_entity$Smoking), col = "lightblue")
plot(density(india_entity$Air.pollution), main = "Density Plot, Entity-India : Risk Factor, Air Pollution", ylab = "Frequency")
polygon(density(india_entity$Air.pollution), col = "darkblue")
#graph 5 : Density Plot of the attributes for Entity North America
par(mfrow=c(2, 2))
plot(density(northamerica_entity$Alcohol.use), main = "Density Plot, Entity-NorthAmerica : Risk Factor, Alcohol Use", ylab = "Frequency")
polygon(density(northamerica_entity$Alcohol.use), col = "gray")
plot(density(northamerica_entity$Drug.use), main = "Density Plot, Entity-NorthAmerica : Risk Factor, Drug Use", ylab = "Frequency")
polygon(density(northamerica_entity$Drug.use), col = "cadetblue")
plot(density(northamerica_entity$Smoking), main = "Density Plot, Entity-NorthAmerica : Risk Factor, Smoking", ylab = "Frequency")
polygon(density(northamerica_entity$Smoking), col = "yellow")
plot(density(northamerica_entity$Air.pollution), main = "Density Plot, Entity-NorthAmerica : Risk Factor, Air Pollution", ylab = "Frequency")
polygon(density(northamerica_entity$Air.pollution), col = "pink")
#graph 6 : Density Plot of the attributes for Entity Maldives
par(mfrow=c(2, 2))
plot(density(maldives_entity$Alcohol.use), main = "Density Plot, Entity-Maldives : Risk Factor, Alcohol Use", ylab = "Frequency")
polygon(density(maldives_entity$Alcohol.use), col = "pink")
plot(density(maldives_entity$Drug.use), main = "Density Plot, Entity-Maldives : Risk Factor, Drug Use", ylab = "Frequency")
polygon(density(maldives_entity$Drug.use), col = "orange")
plot(density(maldives_entity$Smoking), main = "Density Plot, Entity-Maldives : Risk Factor, Smoking", ylab = "Frequency")
polygon(density(maldives_entity$Smoking), col = "yellow")
plot(density(maldives_entity$Air.pollution), main = "Density Plot, Entity-Maldives : Risk Factor, Air Pollution", ylab = "Frequency")
polygon(density(maldives_entity$Air.pollution), col = "lightblue")
#1.relationship between year and alcohol use in entity India
#correlation
correlation_value_india_alcoholuse <- cor(india_entity$Alcohol.use, india_entity$Year)
correlation_value_india_alcoholuse
#linear regression model
plot(india_entity$Year, india_entity$Alcohol.use, col = "blue")
linearregression_model1 <- lm(Alcohol.use ~ Year, data = india_entity)
linearregression_model1
abline(linearregression_model1, col = "red")
summary(linearregression_model1)
#regression table 1
tbl_regression(linearregression_model1)
#2.relationship between year and drug use in entity India
#correlation
correlation_value_india_druguse <- cor(india_entity$Drug.use, india_entity$Year)
correlation_value_india_druguse
#linear regression model
plot(india_entity$Year, india_entity$Drug.use, col = "blue")
linearregression_model2 <- lm(Drug.use ~ Year, data = india_entity)
linearregression_model2
abline(linearregression_model2, col = "red")
summary(linearregression_model2)
#regression table 2
tbl_regression(linearregression_model2)
#3.relationship between year and drug use in entity North America
#correlation
correlation_value_northamerica_druguse <- cor(northamerica_entity$Drug.use, northamerica_entity$Year)
correlation_value_northamerica_druguse
#linear regression model
plot(northamerica_entity$Year, northamerica_entity$Drug.use, col = "blue")
linearregression_model3 <- lm(Drug.use ~ Year, data = northamerica_entity)
linearregression_model3
abline(linearregression_model3, col = "red")
summary(linearregression_model3)
#regression table 3
tbl_regression(linearregression_model3)
#4.relationship between year and smoking in entity Maldives
#correlation
correlation_value_maldives_smoking <- cor(maldives_entity$Smoking, maldives_entity$Year)
correlation_value_maldives_smoking
#linear regression model
plot(maldives_entity$Year, maldives_entity$Smoking, col = "blue")
linearregression_model4 <- lm(Smoking ~ Year, data = maldives_entity)
linearregression_model4
abline(linearregression_model4, col = "red")
summary(linearregression_model4)
#regression table 4
tbl_regression(linearregression_model4)
#subset of the dataset
new_india_entity <- data.frame(india_entity$Year, india_entity$Alcohol.use, india_entity$Drug.use,
india_entity$Smoking, india_entity$Air.pollution,
india_entity$No.access.to.handwashing.facility)
new_india_entity
new_northamerica_entity <- data.frame(northamerica_entity$Year, northamerica_entity$Alcohol.use, northamerica_entity$Drug.use,
northamerica_entity$Smoking, northamerica_entity$Air.pollution,
northamerica_entity$No.access.to.handwashing.facility)
new_northamerica_entity
new_maldives_entity <- data.frame(maldives_entity$Year, maldives_entity$Alcohol.use, maldives_entity$Drug.use,
maldives_entity$Smoking, maldives_entity$Air.pollution,
maldives_entity$No.access.to.handwashing.facility)
new_maldives_entity
#correlation table & chart
#india entity
new_india_entity.cor = cor(new_india_entity)
new_india_entity.cor
corrplot(new_india_entity.cor)
#north america entity
new_northamerica_entity.cor = cor(new_northamerica_entity)
new_northamerica_entity.cor
corrplot(new_northamerica_entity.cor)
#MV Regression model for entity India
mvregression1 <- lm(cbind(Smoking, Drug.use) ~ Year, data = india_entity)
mvregression1
summary(mvregression1)
coef(mvregression1)
sigma(mvregression1)
vcov(mvregression1)
Anova(mvregression1) #Analysis of Variance Table
#MV Regression model for entity North America
mvregression2 <- lm(cbind(Alcohol.use, Air.pollution) ~ Year, data = northamerica_entity)
mvregression2
summary(mvregression2)
coef(mvregression2)
sigma(mvregression2)
vcov(mvregression2)
Anova(mvregression2)
#MV Regression model for entity Maldives
mvregression3 <- lm(cbind(Drug.use, Alcohol.use) ~ Year, data = maldives_entity)
mvregression3
summary(mvregression3)
coef(mvregression3)
sigma(mvregression3)
vcov(mvregression3)
Anova(mvregression3)