lab4
variables | types | missing_count | missing_percent | unique_count | unique_rate |
|---|---|---|---|---|---|
age | character | 0 | 0 | 74 | 0.074 |
workclass | character | 50 | 5 | 9 | 0.009 |
education | character | 0 | 0 | 17 | 0.017 |
education_num | integer | 0 | 0 | 16 | 0.016 |
marital_status | character | 50 | 5 | 8 | 0.008 |
occupation | character | 0 | 0 | 15 | 0.015 |
relationship | character | 50 | 5 | 7 | 0.007 |
race | character | 0 | 0 | 6 | 0.006 |
sex | character | 50 | 5 | 3 | 0.003 |
capital_gain | numeric | 0 | 0 | 947 | 0.947 |
capital_loss | numeric | 0 | 0 | 948 | 0.948 |
hours_per_week | integer | 50 | 5 | 100 | 0.100 |
native_country | character | 50 | 5 | 42 | 0.042 |
income | character | 0 | 0 | 2 | 0.002 |
#setting age as integer and it will automatically change invalidAge as N/A
sample_dirty_dataset$age <- as.integer(sample_dirty_dataset$age)# columns that contains N/A: workclass, martial_status, reationship, sex, hours_per_week, native_country,
# age -> invalidAge
# education -> InvalidEducation
# occupation -> InvalidOccupation
# race -> invalidRace
# capital gain/loss -> negative numbers
#Changing all invaild data to NA
sample_dirty_dataset$age[sample_dirty_dataset$age == "invalidAge"] <- NA
sample_dirty_dataset$education[sample_dirty_dataset$education == "InvalidEducation"] <- NA
sample_dirty_dataset$occupation[sample_dirty_dataset$occupation == "InvalidOccupation"] <- NA
sample_dirty_dataset$race[sample_dirty_dataset$race == "InvalidRace"] <- NA
sample_dirty_dataset$capital_gain[sample_dirty_dataset$capital_gain < 0] <- NA
sample_dirty_dataset$capital_loss[sample_dirty_dataset$capital_loss < 0] <- NAvariables | types | missing_count | missing_percent | unique_count | unique_rate |
|---|---|---|---|---|---|
age | integer | 50 | 5 | 74 | 0.074 |
workclass | character | 50 | 5 | 9 | 0.009 |
education | character | 50 | 5 | 17 | 0.017 |
education_num | integer | 0 | 0 | 16 | 0.016 |
marital_status | character | 50 | 5 | 8 | 0.008 |
occupation | character | 50 | 5 | 15 | 0.015 |
relationship | character | 50 | 5 | 7 | 0.007 |
race | character | 50 | 5 | 6 | 0.006 |
sex | character | 50 | 5 | 3 | 0.003 |
capital_gain | numeric | 50 | 5 | 947 | 0.947 |
capital_loss | numeric | 50 | 5 | 948 | 0.948 |
hours_per_week | integer | 50 | 5 | 100 | 0.100 |
native_country | character | 50 | 5 | 42 | 0.042 |
income | character | 0 | 0 | 2 | 0.002 |
# changing character data to factors
sample_dirty_dataset <- sample_dirty_dataset %>%
mutate_if(is.character, as.factor)
new_data <- complete(mice(sample_dirty_dataset, m = 1, method ="cart"))##
## iter imp variable
## 1 1 age workclass education marital_status occupation relationship race sex capital_gain capital_loss hours_per_week native_country
## 2 1 age workclass education marital_status occupation relationship race sex capital_gain capital_loss hours_per_week native_country
## 3 1 age workclass education marital_status occupation relationship race sex capital_gain capital_loss hours_per_week native_country
## 4 1 age workclass education marital_status occupation relationship race sex capital_gain capital_loss hours_per_week native_country
## 5 1 age workclass education marital_status occupation relationship race sex capital_gain capital_loss hours_per_week native_country
variables | types | missing_count | missing_percent | unique_count | unique_rate |
|---|---|---|---|---|---|
age | integer | 50 | 5 | 74 | 0.074 |
workclass | factor | 50 | 5 | 9 | 0.009 |
education | factor | 50 | 5 | 17 | 0.017 |
education_num | integer | 0 | 0 | 16 | 0.016 |
marital_status | factor | 50 | 5 | 8 | 0.008 |
occupation | factor | 50 | 5 | 15 | 0.015 |
relationship | factor | 50 | 5 | 7 | 0.007 |
race | factor | 50 | 5 | 6 | 0.006 |
sex | factor | 50 | 5 | 3 | 0.003 |
capital_gain | numeric | 50 | 5 | 947 | 0.947 |
capital_loss | numeric | 50 | 5 | 948 | 0.948 |
hours_per_week | integer | 50 | 5 | 100 | 0.100 |
native_country | factor | 50 | 5 | 42 | 0.042 |
income | factor | 0 | 0 | 2 | 0.002 |
variables | types | missing_count | missing_percent | unique_count | unique_rate |
|---|---|---|---|---|---|
age | numeric | 0 | 0 | 73 | 0.073 |
workclass | factor | 0 | 0 | 8 | 0.008 |
education | factor | 0 | 0 | 16 | 0.016 |
education_num | integer | 0 | 0 | 16 | 0.016 |
marital_status | factor | 0 | 0 | 7 | 0.007 |
occupation | factor | 0 | 0 | 14 | 0.014 |
relationship | factor | 0 | 0 | 6 | 0.006 |
race | factor | 0 | 0 | 5 | 0.005 |
sex | factor | 0 | 0 | 2 | 0.002 |
capital_gain | numeric | 0 | 0 | 946 | 0.946 |
capital_loss | numeric | 0 | 0 | 947 | 0.947 |
hours_per_week | numeric | 0 | 0 | 99 | 0.099 |
native_country | factor | 0 | 0 | 41 | 0.041 |
income | factor | 0 | 0 | 2 | 0.002 |
## variables outliers_cnt outliers_ratio outliers_mean with_mean
## 1 age 0 0 NaN 53.886
## 2 education_num 0 0 NaN 8.345
## 3 capital_gain 0 0 NaN 49733.000
## 4 capital_loss 0 0 NaN 48718.277
## 5 hours_per_week 0 0 NaN 48.087
## without_mean
## 1 53.886
## 2 8.345
## 3 49733.000
## 4 48718.277
## 5 48.087
#3 Method
lower_bound <- quantile(new_data$capital_gain, 0.025)
upper_bound <- quantile(new_data$capital_gain, 0.975)
outlier_ind <- which(new_data$capital_gain < lower_bound | new_data$capital_gain > upper_bound)
new_data[outlier_ind, "capital_gain"]## [1] 2553 97981 990 99180 97595 1097 98599 98953 2076 98168 98978 2603
## [13] 2557 550 1536 99134 581 98754 99960 1256 97315 872 1277 98687
## [25] 98302 2271 99342 127 97555 1524 419 99257 99411 232 98660 98314
## [37] 2136 97283 97939 97315 2207 97555 99839 403 530 1569 1524 1536
## [49] 98540 20
#Transformation may not be needed it all depends what we need our data for.
#Lets assume we need to transform age. Dont know why we would like to do it but whatever
head(new_data)%>% flextable() #dataframe before transformatiomage | workclass | education | education_num | marital_status | occupation | relationship | race | sex | capital_gain | capital_loss | hours_per_week | native_country | income |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
48 | Local-gov | 7th-8th | 1 | Separated | Prof-specialty | Other-relative | Black | Male | 7,826 | 77,249 | 60 | Poland | >50K |
68 | Self-emp-inc | Prof-school | 1 | Divorced | Priv-house-serv | Unmarried | Asian-Pac-Islander | Female | 84,532 | 60,981 | 73 | Columbia | <=50K |
31 | State-gov | Bachelors | 2 | Married-spouse-absent | Protective-serv | Wife | Black | Male | 35,297 | 15,093 | 98 | Hungary | <=50K |
84 | Never-worked | 7th-8th | 10 | Separated | Handlers-cleaners | Other-relative | Asian-Pac-Islander | Male | 27,074 | 40,203 | 68 | South | <=50K |
59 | Private | 12th | 16 | Married-civ-spouse | Machine-op-inspct | Other-relative | White | Female | 73,785 | 2,056 | 4 | Haiti | <=50K |
67 | Self-emp-not-inc | Preschool | 12 | Married-spouse-absent | Adm-clerical | Husband | Black | Male | 2,553 | 17,031 | 70 | Scotland | <=50K |
opt_lambda <- bc_out$x[which.max(bc_out$y)] #Choosing optimal lambda
df$age_transformed <- (df$age^opt_lambda - 1) / opt_lambda #Transformation
head(df) %>% flextable() #Dataframe after transformation of age tableage | workclass | education | education_num | marital_status | occupation | relationship | race | sex | capital_gain | capital_loss | hours_per_week | native_country | income | age_transformed |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
48 | Local-gov | 7th-8th | 1 | Separated | Prof-specialty | Other-relative | Black | Male | 7,826 | 77,249 | 60 | Poland | >50K | 18.10897 |
68 | Self-emp-inc | Prof-school | 1 | Divorced | Priv-house-serv | Unmarried | Asian-Pac-Islander | Female | 84,532 | 60,981 | 73 | Columbia | <=50K | 23.20109 |
31 | State-gov | Bachelors | 2 | Married-spouse-absent | Protective-serv | Wife | Black | Male | 35,297 | 15,093 | 98 | Hungary | <=50K | 13.17523 |
84 | Never-worked | 7th-8th | 10 | Separated | Handlers-cleaners | Other-relative | Asian-Pac-Islander | Male | 27,074 | 40,203 | 68 | South | <=50K | 26.91436 |
59 | Private | 12th | 16 | Married-civ-spouse | Machine-op-inspct | Other-relative | White | Female | 73,785 | 2,056 | 4 | Haiti | <=50K | 20.98281 |
67 | Self-emp-not-inc | Preschool | 12 | Married-spouse-absent | Adm-clerical | Husband | Black | Male | 2,553 | 17,031 | 70 | Scotland | <=50K | 22.95974 |
Functions used: diagnose() - Diagnoses data quality with a focus on missing values, outliers, and invalid values.
flextable() - creation of tables.
as.integer() - Converts data to integer format; non-integer -> NA.
mutate_if() - Modifies columns that meet a condition.
complete() - imputation on missing data.
plot_na_hclust() - Visualizes the patterns of missing data.
plot_na_intersect() - Visualizes the intersections of missing data.
boxplot() - Creates a box plot
diagnose_outlier() (dlookr package) - Diagnoses outliers
quantile() - Computes the quantiles of data.
which() - Used to select only data which matches condition.
boxcox() (MASS package) - Performs Box-cox, plots a chart so we can choose optimal lamda for futher transformation.
Steps:
Issue Identification - Use diagnose() to highlight data quality issues.
Handling Invalid Data - Replace error entries with NA.
Data Type Corrections - Cast age to integers, throw out non-integer values.
Missing Values Analysis - Visualize missing data patterns and correlations.
Data Imputation - Use mice for missing data imputation.
Data Transformation - Use Box-Cox transformation for normality.
Outlier Detection - Identify extremes using box plots and quantiles.