#Install Amelia package to create missing data maps

install.packages(“Amelia”)

#Install VIM package install.packages(“VIM”) library(VIM)

#Load train.csv data file

file_path <- “C:\Users\fardm\OneDrive\Desktop\PhD\train.csv” train_data <- read.csv(“C:\Users\fardm\OneDrive\Desktop\PhD\train.csv”)

Generate missing data visualization

aggr_plot <- aggr(train_data, col = c(‘blue’, ‘yellow’), numbers = TRUE, sortVars = TRUE, labels = names(train_data), cex.axis = 0.7, gap = 3)

#Replacing the missing values in the categorical variables with null factor level

data\(PassengerId <- factor(data\)PassengerId, levels = c(““, levels(data\(PassengerId))) data\)HomePlanet <- factor(data\(HomePlanet, levels = c("", levels(data\)HomePlanet))) data\(CryoSleep <- factor(data\)CryoSleep, levels = c(”“, levels(data\(CryoSleep))) data\)Cabin <- factor(data\(Cabin, levels = c("", levels(data\)Cabin))) data\(Destination <- factor(data\)Destination, levels = c(”“, levels(data\(Destination))) data\)VIP <- factor(data\(VIP, levels = c("", levels(data\)VIP))) data\(Name <- factor(data\)Name, levels = c(”“, levels(data\(Name))) data\)Transported <- factor(data\(Transported, levels = c("", levels(data\)Transported)))

Identify rows with 20% or more missing values

rows_with_missing <- train_data[!complete.cases(train_data) | rowMeans(is.na(train_data)) >= 0.2, ]

Identify columns with 20% or more missing values

columns_with_missing <- colnames(train_data)[colMeans(is.na(train_data)) >= 0.2]

Display the identified rows and columns

print(“Rows with 20% or more missing values:”) print(rows_with_missing)

print(“Columns with 20% or more missing values:”) print(columns_with_missing)

Remove rows with 20% or more missing values

cleaned_data_rows <- train_data[complete.cases(train_data) & rowMeans(is.na(train_data)) < 0.2, ]

Remove columns with 20% or more missing values

cleaned_data_columns <- train_data[, !(colnames(train_data) %in% columns_with_missing)]

Display the cleaned dataset after removing rows and columns

print(“Cleaned Data without rows with 20% or more missing values:”) print(cleaned_data_rows)

print(“Cleaned Data without columns with 20% or more missing values:”) print(cleaned_data_columns)

Replace missing values with the mean for numeric columns

cleaned_data_imputed <- apply(train_data, 2, function(x) { if (is.numeric(x)) { ifelse(is.na(x), mean(x, na.rm = TRUE), x) } else { x } })

Replace non-numeric values with a specific value

cleaned_data_imputed <- apply(train_data, 2, function(x) { if (all(is.numeric(x))) { ifelse(is.na(x), mean(x, na.rm = TRUE), x) } else { replacement_value <- 0 # Replace non-numeric values with 0, you can change this to your desired value ifelse(is.na(x) | !is.numeric(x), replacement_value, x) } })

Create a missing data map for the cleaned and imputed dataset

missing_data_map <- aggr(cleaned_data_imputed, col = c(‘blue’, ‘yellow’), numbers = TRUE, sortVars = TRUE, labels = names(cleaned_data_imputed), cex.axis = 0.7, gap = 3, plotMatrix = TRUE)