Repository navigation
Expand file tree
/
Copy pathcode.R
More file actions
253 lines (201 loc) · 8.86 KB
/
Copy pathcode.R
File metadata and controls
253 lines (201 loc) · 8.86 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
#Question 1
setwd("C:/Users/USER/OneDrive/Desktop/SP/Assignment")
dataset <- read.csv("filtered_final_dataset.csv")
#Question 2
head(dataset)
summary(dataset)
library(DataExplorer)
create_report(dataset)
#Question 3
#a
#Check for duplicate rows
duplicate_rows <- dataset[duplicated(dataset), ]
num_duplicates <- nrow(duplicate_rows)
#Report the number of duplicate rows
cat("Total number of duplicate rows:", num_duplicates, "\n")
#View the duplicate rows ******* 22 duplictaes
print(duplicate_rows)
#Identify and remove actual duplicates for the specific case of the 21-year-old male
dataset_clean <- dataset[!(duplicated(dataset) &
dataset$Age == 21 &
dataset$Height == 1.62 &
dataset$Weight == 70 &
dataset$Gender == "Male" &
dataset$Family_history == "no" &
dataset$Obesity_level == "Overweight_Level_I"), ]
#Verify that specific duplicates have been removed
cat("Number of rows before removing duplicates:", nrow(dataset), "\n")
cat("Number of rows after removing duplicates:", nrow(dataset_clean), "\n")
#b
#Check for missing values in the dataset
missing_values <- colSums(is.na(dataset_clean))
#Display the number of missing values for each column
cat("Missing values in each column:\n")
print(missing_values) #******* no missing values
#c
# Convert categorical variables to factors
dataset_clean$Gender <- as.factor(dataset_clean$Gender)
dataset_clean$Family_history <- as.factor(dataset_clean$Family_history)
dataset_clean$Obesity_level <- as.factor(dataset_clean$Obesity_level)
# Apply label encoding
dataset_clean$Gender <- as.numeric(dataset_clean$Gender)
dataset_clean$Family_history <- as.numeric(dataset_clean$Family_history)
dataset_clean$Obesity_level <- as.numeric(dataset_clean$Obesity_level)
# View the first few rows of the label-encoded dataset
head(dataset_clean)
dim(dataset_clean)
#d
#Generate boxplot for detecting outliers for Age, Height, and Weight together
boxplot(dataset_clean[, c("Age", "Height", "Weight")],
main="Boxplot for Outlier Detection (Age, Height, Weight)",
col=c("lightblue", "lightgreen", "lightcoral"),
names=c("Age", "Height", "Weight"))
# Identify outliers for a specific attribute, e.g., Age
outliers_age <- boxplot.stats(dataset_clean$Age)$out
num_outliers_age <- length(outliers_age)
print(paste("Number of outliers in Age:", num_outliers_age))
# Identify outliers for Weight
outliers_weight <- boxplot.stats(dataset_clean$Weight)$out
num_outliers_weight <- length(outliers_weight)
print(paste("Number of outliers in Weight:", num_outliers_weight))
# Define the function to identify valid (non-outliers) using the IQR method
remove_outliers <- function(x) {
Q1 <- quantile(x, 0.25)
Q3 <- quantile(x, 0.75)
IQR <- Q3 - Q1
lower_bound <- Q1 - 1.5 * IQR
upper_bound <- Q3 + 1.5 * IQR
return(x >= lower_bound & x <= upper_bound)
}
# Apply the function to the Age and Weight columns
valid_age <- remove_outliers(dataset_clean$Age)
valid_weight <- remove_outliers(dataset_clean$Weight)
# Combine valid indices to ensure consistency (both columns should have valid values)
valid_data <- valid_age & valid_weight
# Filter the dataset to remove outliers from Age and Weight
dataset_no_outliers <- dataset_clean[valid_data, ]
# Check the dimensions of the cleaned data frame (no outliers in Age and Weight)
print(dim(dataset_no_outliers))
# Generate a boxplot again after removing outliers
boxplot(dataset_no_outliers[, c("Age", "Height", "Weight")],
main="Boxplot After Removing Outliers (Age, Height, Weight)",
col=c("lightblue", "lightgreen", "lightcoral"),
names=c("Age", "Height", "Weight"))
#density plot
library(ggplot2)
library(patchwork)
# Density for Age
p5 <- ggplot(dataset_clean, aes(x=Age)) +
geom_density(fill="lightblue") +
ggtitle("Age Density (Before Outlier Removal)") + theme_minimal()
p6 <- ggplot(dataset_no_outliers, aes(x=Age)) +
geom_density(fill="lightblue") +
ggtitle("Age Density (After Outlier Removal)") + theme_minimal()
# Density for Weight
p7 <- ggplot(dataset_clean, aes(x=Weight)) +
geom_density(fill="lightcoral") +
ggtitle("Weight Density (Before Outlier Removal)") + theme_minimal()
p8 <- ggplot(dataset_no_outliers, aes(x=Weight)) +
geom_density(fill="lightcoral") +
ggtitle("Weight Density (After Outlier Removal)") + theme_minimal()
# Side-by-side display
(p5 + p6) / (p7 + p8)
# QQ plot for Age
p15 <- ggplot(dataset_clean, aes(sample=Age)) +
stat_qq() + stat_qq_line() +
ggtitle("QQ Plot of Age (Before Outlier Removal)") + theme_minimal()
p16 <- ggplot(dataset_no_outliers, aes(sample=Age)) +
stat_qq() + stat_qq_line() +
ggtitle("QQ Plot of Age (After Outlier Removal)") + theme_minimal()
# QQ plot for Weight
p17 <- ggplot(dataset_clean, aes(sample=Weight)) +
stat_qq() + stat_qq_line() +
ggtitle("QQ Plot of Weight (Before Outlier Removal)") + theme_minimal()
p18 <- ggplot(dataset_no_outliers, aes(sample=Weight)) +
stat_qq() + stat_qq_line() +
ggtitle("QQ Plot of Weight (After Outlier Removal)") + theme_minimal()
# Side-by-side QQ plots
(p15 + p16) / (p17 + p18)
# Q4
#Descriptive statistics by Obesity Level for numerical attributes
library(dplyr)
age_stats <- dataset_no_outliers %>%
group_by(Obesity_level) %>%
summarise(
Age_Mean = mean(Age, na.rm = TRUE),
Age_Median = median(Age, na.rm = TRUE),
Age_Mode = as.numeric(names(sort(table(Age), decreasing=TRUE))[1]),
Age_SD = sd(Age, na.rm = TRUE),
Age_Variance = var(Age, na.rm = TRUE)
)
print(age_stats) # Descriptive statistics for Height by Obesity Level
height_stats <- dataset_no_outliers %>%
group_by(Obesity_level) %>%
summarise(
Height_Mean = mean(Height, na.rm = TRUE),
Height_Median = median(Height, na.rm = TRUE),
Height_Mode = as.numeric(names(sort(table(Height), decreasing=TRUE))[1]),
Height_SD = sd(Height, na.rm = TRUE),
Height_Variance = var(Height, na.rm = TRUE)
)
print(height_stats) # Descriptive statistics for Weight by Obesity Level
weight_stats <- dataset_no_outliers %>%
group_by(Obesity_level) %>%
summarise(
Weight_Mean = mean(Weight, na.rm = TRUE),
Weight_Median = median(Weight, na.rm = TRUE),
Weight_Mode = as.numeric(names(sort(table(Weight), decreasing=TRUE))[1]),
Weight_SD = sd(Weight, na.rm = TRUE),
Weight_Variance = var(Weight, na.rm = TRUE)
)
print(weight_stats)
#Q5
#age
library(ggplot2)
# Ensure Obesity_level is a factor with proper labels
dataset_no_outliers$Obesity_level <- factor(dataset_no_outliers$Obesity_level,
levels = c("Normal_Weight", "Overweight_Level_I", "Obesity_Type_I"),
labels = c("Normal Weight", "Overweight Level I", "Obesity Type I"))
# Boxplot for Age by Obesity Level
ggplot(dataset, aes(x=Obesity_level, y=Age, fill=Obesity_level)) +
geom_boxplot()
labs(title="Boxplot of Age by Obesity Level", x="Obesity Level", y="Age") +
theme_minimal()
#weight
ggplot(dataset, aes(x=Obesity_level, y=Weight, fill=Obesity_level)) +
geom_boxplot() +
labs(title="Boxplot of Weight by Obesity Level", x="Obesity Level", y="Weight") +
theme_minimal()
#height
ggplot(dataset, aes(x=Height, fill=Obesity_level)) +
geom_histogram(binwidth=0.05, position="dodge") +
facet_wrap(~Obesity_level, scales="free_y") +
labs(title="Histogram of Height by Obesity Level", x="Height", y="Count") +
theme_minimal()
#gender
library(ggplot2)
ggplot(dataset, aes(x = Gender, fill = Gender)) +
geom_bar(position = "dodge") +
facet_wrap(~Obesity_level, scales = "free_y") +
labs(title = "Bar Plot of Gender by Obesity Level", x = "Gender", y = "Count") +
scale_fill_manual(values = c("Female" = "lightblue", "Male" = "lightcoral")) +
theme_minimal()
#family history
library(ggplot2)
# Grouped bar plot for Family History
ggplot(dataset, aes(x = Obesity_level, fill = Family_history)) +
geom_bar(position = "dodge") +
labs(title = "Grouped Bar Plot of Family History by Obesity Level", x = "Obesity Level", y = "Count") +
scale_fill_manual(values = c("yes" = "lightblue", "no" = "lightcoral")) +
theme_minimal()
#Correlation
library(corrplot)
library(colorspace)
numeric_vars <- dataset %>% select_if(is.numeric) # Select only numeric variables
cor_matrix <- cor(numeric_vars, use = "complete.obs") # Compute correlation matrix
# Plot correlation matrix
corrplot(cor_matrix, method="color",
col=colorspace::diverge_hcl(200),
addCoef.col = "black",
tl.col = "black",
title = "Correlation Matrix of Numerical Variables")