-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMortality_Data_Analysis.r
More file actions
369 lines (269 loc) · 12.6 KB
/
Mortality_Data_Analysis.r
File metadata and controls
369 lines (269 loc) · 12.6 KB
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
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
# Import the data
data <- read.delim("Mortality_Data_1.txt")
View(data)
# Let's figure out what kind of variables are contained in this dataset
names(data)
# Check what kind of levels are contained in the following key variables.
unique(data$Ten.Year.Age.Groups)
unique(data$Gender)
unique(data$Census.Region)
unique(data$Year)
# Store the desired levels for each variables
ages=unique(data$Ten.Year.Age.Groups)[1:11]
genders=unique(data$Gender)[1:2]
regions=unique(data$Census.Region)[1:4]
years=unique(data$Year)[1:22]
subdata=subset(data,Year==years[1] & Census.Region==regions[1])
deathrates=matrix(NA,length(ages),length(genders))
colnames(deathrates)=genders
row.names(deathrates)=ages
for (i in 1:length(genders)){
subdatai=(subset(subdata,Gender==genders[i]))
for (j in 1:length(ages)){
subdataij=subset(subdatai,Ten.Year.Age.Groups==ages[j])
deathrates[j,i]=subdataij$Deaths/as.numeric(subdataij$Population)*100
}
}
deathrates
# Plot Death Rates by Age and Gender
library(reshape2)
deathrates_long <- melt(deathrates, varnames = c("Age", "Gender"), value.name = "DeathRate")
library(ggplot2)
ggplot(deathrates_long, aes(x = Age, y = DeathRate, color = Gender, group = Gender)) +
geom_line(size = 1.2) +
labs(title = paste("Death Rates (diseases of the respiratory system) \n by Age and Gender \n on Year ",years[1],", ",regions[1],sep=""), x = "Age Groups", y = "Death Rates") +
theme(plot.title = element_text(hjust = 0.5))+
theme(axis.text.x = element_text(angle = 45, hjust = 1))
# Second option could be, we can draw death rates vs year with different lines for genders.
# Set the other two predictor variables (region and age) at some specific levels.
# death rates presented in %.
subdata=subset(data,Ten.Year.Age.Groups==ages[5] & Census.Region==regions[2])
deathrates=matrix(NA,length(years),length(genders))
colnames(deathrates)=genders
row.names(deathrates)=years
for (i in 1:length(genders)){
subdatai=(subset(subdata,Gender==genders[i]))
for (j in 1:length(years)){
subdataij=subset(subdatai,Year==years[j])
deathrates[j,i]=subdataij$Deaths/as.numeric(subdataij$Population)*100
}
}
deathrates
# Plot Death Rates by Year and Gender
library(reshape2)
deathrates_long <- melt(deathrates, varnames = c("Year", "Gender"), value.name = "DeathRate")
library(ggplot2)
ggplot(deathrates_long, aes(x = Year, y = DeathRate, color = Gender, group = Gender)) +
geom_line(size = 1.2) +
labs(title = paste("Death Rates (diseases of the respiratory system) \n by Year and Gender \n for Age ",ages[5],", ",regions[2],sep=""), x = "Year", y = "Death Rates") +
theme(plot.title = element_text(hjust = 0.5))+
theme(axis.text.x = element_text(angle = 45, hjust = 1))
# Subset data for a specific year and region
subdata = subset(data, Year == years[1] & Census.Region == regions[1])
# Create matrix for storing death rates
deathrates = matrix(NA, length(ages), length(genders))
colnames(deathrates) = genders
row.names(deathrates) = ages
# Calculate death rates by age and gender
for (i in 1:length(genders)) {
subdatai = subset(subdata, Gender == genders[i])
for (j in 1:length(ages)) {
subdataij = subset(subdatai, Ten.Year.Age.Groups == ages[j])
deathrates[j, i] = subdataij$Deaths / as.numeric(subdataij$Population) * 100
}
}
# Convert to long format for ggplot
library(reshape2)
deathrates_long = melt(deathrates, varnames = c("Age", "Gender"), value.name = "DeathRate")
# Plot Death Rates by Age and Gender
library(ggplot2)
ggplot(deathrates_long, aes(x = Age, y = DeathRate, color = Gender, group = Gender)) +
geom_line(size = 1.2) +
labs(title = paste("Death Rates by Age and Gender\nYear:", years[1], ", Region:", regions[1]),
x = "Age Groups", y = "Death Rates (%)") +
theme_minimal() +
theme(axis.text.x = element_text(angle = 45, hjust = 1))
# Subset data for a specific gender and age group
subdata = subset(data, Gender == genders[1] & Ten.Year.Age.Groups == ages[5])
# Create matrix for storing death rates by year and region
deathrates = matrix(NA, length(years), length(regions))
colnames(deathrates) = regions
row.names(deathrates) = years
# Calculate death rates by year and region
for (i in 1:length(regions)) {
subdatai = subset(subdata, Census.Region == regions[i])
for (j in 1:length(years)) {
subdataij = subset(subdatai, Year == years[j])
deathrates[j, i] = subdataij$Deaths / as.numeric(subdataij$Population) * 100
}
}
# Convert to long format for ggplot
deathrates_long = melt(deathrates, varnames = c("Year", "Region"), value.name = "DeathRate")
# Line Plot: Death Rates by Year and Region
ggplot(deathrates_long, aes(x = Year, y = DeathRate, color = Region, group = Region)) +
geom_line(size = 1.2) +
labs(title = paste("Death Rates by Region and Year\nGender:", genders[1], ", Age Group:", ages[5]),
x = "Year", y = "Death Rates (%)") +
theme_minimal() +
theme(axis.text.x = element_text(angle = 45, hjust = 1))
# Subset data for a specific gender and age group
subdata = subset(data, Gender == genders[2] & Ten.Year.Age.Groups == ages[5])
# Create matrix for storing death rates by year and region
deathrates = matrix(NA, length(years), length(regions))
colnames(deathrates) = regions
row.names(deathrates) = years
# Calculate death rates by year and region
for (i in 1:length(regions)) {
subdatai = subset(subdata, Census.Region == regions[i])
for (j in 1:length(years)) {
subdataij = subset(subdatai, Year == years[j])
deathrates[j, i] = subdataij$Deaths / as.numeric(subdataij$Population) * 100
}
}
# Convert to long format for ggplot
deathrates_long = melt(deathrates, varnames = c("Year", "Region"), value.name = "DeathRate")
# Line Plot: Death Rates by Year and Region
ggplot(deathrates_long, aes(x = Year, y = DeathRate, color = Region, group = Region)) +
geom_line(size = 1.2) +
labs(title = paste("Death Rates by Region and Year\nGender:", genders[2], ", Age Group:", ages[5]),
x = "Year", y = "Death Rates (%)") +
theme_minimal() +
theme(axis.text.x = element_text(angle = 45, hjust = 1))
# Subset data for a specific region and age group
subdata = subset(data, Census.Region == regions[2] & Ten.Year.Age.Groups == ages[6])
# Create matrix for storing death rates by gender and year
deathrates = matrix(NA, length(years), length(genders))
colnames(deathrates) = genders
row.names(deathrates) = years
# Calculate death rates by gender and year
for (i in 1:length(genders)) {
subdatai = subset(subdata, Gender == genders[i])
for (j in 1:length(years)) {
subdataij = subset(subdatai, Year == years[j])
deathrates[j, i] = subdataij$Deaths / as.numeric(subdataij$Population) * 100
}
}
# Convert to long format for ggplot
deathrates_long = melt(deathrates, varnames = c("Year", "Gender"), value.name = "DeathRate")
# Line Plot: Death Rates by Year and Gender
ggplot(deathrates_long, aes(x = Year, y = DeathRate, color = Gender, group = Gender)) +
geom_line(size = 1.2) +
labs(title = paste("Death Rates by Gender and Year\nRegion:", regions[2], ", Age Group:", ages[6]),
x = "Year", y = "Death Rates (%)") +
theme_minimal() +
theme(axis.text.x = element_text(angle = 45, hjust = 1))
# Subset data for a specific gender and region
subdata = subset(data, Gender == genders[2] & Census.Region == regions[3])
# Create matrix for storing death rates by age and year
deathrates = matrix(NA, length(ages), length(years))
colnames(deathrates) = years
row.names(deathrates) = ages
# Calculate death rates by age and year
for (i in 1:length(years)) {
subdatai = subset(subdata, Year == years[i])
for (j in 1:length(ages)) {
subdataij = subset(subdatai, Ten.Year.Age.Groups == ages[j])
deathrates[j, i] = subdataij$Deaths / as.numeric(subdataij$Population) * 100
}
}
# Convert to long format for ggplot
deathrates_long = melt(deathrates, varnames = c("Age", "Year"), value.name = "DeathRate")
# Line Plot: Death Rates by Age and Year
ggplot(deathrates_long, aes(x = Age, y = DeathRate, color = Year, group = Year)) +
geom_line(size = 1.2) +
labs(title = paste("Death Rates by Age and Year\nGender:", genders[2], ", Region:", regions[3]),
x = "Age Groups", y = "Death Rates (%)") +
theme_minimal() +
theme(axis.text.x = element_text(angle = 45, hjust = 1))
# Subset data for a specific year and gender
subdata = subset(data, Year == years[10] & Gender == genders[1])
# Create matrix for storing death rates by age group and region
deathrates = matrix(NA, length(ages), length(regions))
colnames(deathrates) = regions
row.names(deathrates) = ages
# Calculate death rates by age group and region
for (i in 1:length(regions)) {
subdatai = subset(subdata, Census.Region == regions[i])
for (j in 1:length(ages)) {
subdataij = subset(subdatai, Ten.Year.Age.Groups == ages[j])
deathrates[j, i] = subdataij$Deaths / as.numeric(subdataij$Population) * 100
}
}
# Convert to long format for ggplot
deathrates_long = melt(deathrates, varnames = c("Age", "Region"), value.name = "DeathRate")
# Line Plot: Death Rates by Age Group and Region
ggplot(deathrates_long, aes(x = Age, y = DeathRate, color = Region, group = Region)) +
geom_line(size = 1.2) +
labs(title = paste("Death Rates by Age Group and Region\nYear:", years[10], ", Gender:", genders[1]),
x = "Age Groups", y = "Death Rates (%)") +
theme_minimal() +
theme(axis.text.x = element_text(angle = 45, hjust = 1))
# Subset data for a specific year and gender
subdata = subset(data, Year == years[10] & Gender == genders[2])
# Create matrix for storing death rates by age group and region
deathrates = matrix(NA, length(ages), length(regions))
colnames(deathrates) = regions
row.names(deathrates) = ages
# Calculate death rates by age group and region
for (i in 1:length(regions)) {
subdatai = subset(subdata, Census.Region == regions[i])
for (j in 1:length(ages)) {
subdataij = subset(subdatai, Ten.Year.Age.Groups == ages[j])
deathrates[j, i] = subdataij$Deaths / as.numeric(subdataij$Population) * 100
}
}
# Convert to long format for ggplot
deathrates_long = melt(deathrates, varnames = c("Age", "Region"), value.name = "DeathRate")
# Line Plot: Death Rates by Age Group and Region
ggplot(deathrates_long, aes(x = Age, y = DeathRate, color = Region, group = Region)) +
geom_line(size = 1.2) +
labs(title = paste("Death Rates by Age Group and Region\nYear:", years[10], ", Gender:", genders[2]),
x = "Age Groups", y = "Death Rates (%)") +
theme_minimal() +
theme(axis.text.x = element_text(angle = 45, hjust = 1))
# Subset data for a specific region and gender
subdata = subset(data, Census.Region == regions[3] & Gender == genders[2])
# Create matrix for storing death rates by age group and year
deathrates = matrix(NA, length(ages), length(years))
colnames(deathrates) = years
row.names(deathrates) = ages
# Calculate death rates by age group and year
for (i in 1:length(years)) {
subdatai = subset(subdata, Year == years[i])
for (j in 1:length(ages)) {
subdataij = subset(subdatai, Ten.Year.Age.Groups == ages[j])
deathrates[j, i] = subdataij$Deaths / as.numeric(subdataij$Population) * 100
}
}
# Convert to long format for ggplot
deathrates_long = melt(deathrates, varnames = c("Age", "Year"), value.name = "DeathRate")
# Line Plot: Death Rates by Age Group and Year
ggplot(deathrates_long, aes(x = Age, y = DeathRate, color = Year, group = Year)) +
geom_line(size = 1.2) +
labs(title = paste("Death Rates by Age Group and Year\nRegion:", regions[3], ", Gender:", genders[2]),
x = "Age Groups", y = "Death Rates (%)") +
theme_minimal() +
theme(axis.text.x = element_text(angle = 45, hjust = 1))
# Subset data for a specific year and age group
subdata = subset(data, Year == years[15] & Ten.Year.Age.Groups == ages[8])
# Create matrix for storing death rates by region and gender
deathrates = matrix(NA, length(regions), length(genders))
colnames(deathrates) = genders
row.names(deathrates) = regions
# Calculate death rates by region and gender
for (i in 1:length(genders)) {
subdatai = subset(subdata, Gender == genders[i])
for (j in 1:length(regions)) {
subdataij = subset(subdatai, Census.Region == regions[j])
deathrates[j, i] = subdataij$Deaths / as.numeric(subdataij$Population) * 100
}
}
# Convert to long format for ggplot
deathrates_long = melt(deathrates, varnames = c("Region", "Gender"), value.name = "DeathRate")
# Line Plot: Death Rates by Region and Gender
ggplot(deathrates_long, aes(x = Region, y = DeathRate, color = Gender, group = Gender)) +
geom_line(size = 1.2) +
labs(title = paste("Death Rates by Region and Gender\nYear:", years[15], ", Age Group:", ages[8]),
x = "Census Region", y = "Death Rates (%)") +
theme_minimal() +
theme(axis.text.x = element_text(angle = 45, hjust = 1))