-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMortality_Data_Analysis_1.r
More file actions
179 lines (144 loc) · 6.48 KB
/
Mortality_Data_Analysis_1.r
File metadata and controls
179 lines (144 loc) · 6.48 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
library(reshape2)
library(ggplot2)
# 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
# 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 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))