-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBodyPerformance_DataPreprocessing.R
More file actions
294 lines (184 loc) · 5.02 KB
/
Copy pathBodyPerformance_DataPreprocessing.R
File metadata and controls
294 lines (184 loc) · 5.02 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
# Part 1: Preprocessing + EDA + Visualizations
# to visualize the correlation matrix
install.packages("corrplot")
# Load Dataset
data <- read.csv("C:/Users/Menna Thabet/Downloads/bodyPerformance.csv")
# Explore Dataset
head(data)
str(data)
summary(data)
dim(data)
names(data)
# Check Missing Values
sum(is.na(data))
colSums(is.na(data))
# Check Duplicate Rows
sum(duplicated(data))
# Remove Duplicates
data <- unique(data)
# Encoding categorical variables
data$gender <- as.factor(data$gender)
data$class <- as.factor(data$class)
# Check Dataset Structure Again
str(data)
summary(data)
# PREPROCESSING
# Outlier Detection
par(mfrow = c(2,2))
boxplot(data$age,
col = "lightblue",
main = "Age Boxplot",
ylab = "Age")
boxplot(data$weight_kg,
col = "lightgreen",
main = "Weight Boxplot",
ylab = "Weight")
boxplot(data$body.fat_.,
col = "pink",
main = "Body Fat Boxplot",
ylab = "Body Fat %")
boxplot(data$gripForce,
col = "orange",
main = "Grip Force Boxplot",
ylab = "Grip Force")
rug(data$gripForce, side = 2)
# Remove impossible blood pressure values
data <- subset(data, systolic > 0)
data <- subset(data, diastolic > 0)
# Remove impossible grip force values
data <- subset(data, gripForce > 0)
# Normalize Numerical Data
normalize <- function(x)
{
return((x - min(x)) / (max(x) - min(x)))
}
data$age_norm <- normalize(data$age)
data$weight_norm <- normalize(data$weight_kg)
data$bodyfat_norm <- normalize(data$body.fat_.)
data$grip_norm <- normalize(data$gripForce)
# EXPLORATORY DATA ANALYSIS (EDA)
# Class Distribution
counts <- table(data$class)
barplot(counts,
col = rainbow(4),
main = "Class Distribution",
xlab = "Class",
ylab = "Count")
# Gender Distribution
gender_count <- table(data$gender)
pie(gender_count,
labels = gender_count,
col = rainbow(length(gender_count)),
main = "Gender Distribution")
# Age Distribution
hist(data$age,
col = "lightblue",
breaks = 10,
main = "Age Distribution",
xlab = "Age")
# Weight Distribution
hist(data$weight_kg,
col = "lightgreen",
breaks = 10,
main = "Weight Distribution",
xlab = "Weight")
# Body Fat Distribution
hist(data$body.fat_.,
prob = TRUE,
col = "grey",
main = "Body Fat Distribution",
xlab = "Body Fat %")
lines(density(data$body.fat_.),
col = "blue",
lwd = 2)
# Grip Force Distribution
hist(data$gripForce,
col = "orange",
breaks = 10,
main = "Grip Force Distribution",
xlab = "Grip Force")
# Body Fat vs Class
boxplot(body.fat_. ~ class,
data = data,
col = rainbow(4),
main = "Body Fat Percentage by Class",
xlab = "Class",
ylab = "Body Fat %")
# Grip Force by Gender
boxplot(gripForce ~ gender,
data = data,
col = c("lightblue", "pink"),
main = "Grip Force by Gender",
xlab = "Gender",
ylab = "Grip Force")
# Sit-Ups by Class
boxplot(sit.ups.counts ~ class,
data = data,
col = rainbow(4),
main = "Sit-Ups Counts by Class",
xlab = "Class",
ylab = "Sit-Ups Counts")
# Broad Jump by Class
boxplot(broad.jump_cm ~ class,
data = data,
col = rainbow(4),
main = "Broad Jump by Class",
xlab = "Class",
ylab = "Broad Jump")
# Age vs Sit-Ups
plot(data$age,
data$sit.ups.counts,
col = "blue",
pch = 19,
main = "Age vs Sit-Ups Counts",
xlab = "Age",
ylab = "Sit-Ups Counts")
# Grip Force vs Broad Jump
plot(data$gripForce,
data$broad.jump_cm,
col = "red",
pch = 19,
main = "Grip Force vs Broad Jump",
xlab = "Grip Force",
ylab = "Broad Jump")
# Height vs Weight
plot(data$height_cm,
data$weight_kg,
col = "darkgreen",
pch = 19,
main = "Height vs Weight",
xlab = "Height (cm)",
ylab = "Weight (kg)")
# Systolic vs Diastolic
plot(data$systolic,
data$diastolic,
col = "purple",
pch = 19,
main = "Systolic vs Diastolic Pressure",
xlab = "Systolic",
ylab = "Diastolic")
# Dot Plot
dotchart(data$gripForce,
labels = row.names(data),
cex = 0.5,
main = "Grip Force Dot Plot",
xlab = "Grip Force")
# Correlation Analysis
numeric_data <- data[, sapply(data, is.numeric)]
cor_matrix <- cor(numeric_data)
cor_matrix
# Correlation Plot
library(corrplot)
corrplot(cor_matrix,
method = "color",
type = "upper",
tl.col = "black",
tl.cex = 0.7)
# Scatter Plot Matrix
dev.off()
plot(numeric_data)
# Save Clean Dataset
write.csv(data,
"cleaned_bodyPerformance.csv",
row.names = FALSE)
# END OF Part 1