The diagnose_numeric() produces information for diagnosing the quality of the numerical data.
diagnose_numeric(.data, ...)
# S3 method for data.frame
diagnose_numeric(.data, ...)
# S3 method for grouped_df
diagnose_numeric(.data, ...)
a data.frame or a tbl_df
or a grouped_df
.
one or more unquoted expressions separated by commas. You can treat variable names like they are positions. Positive values select variables; negative values to drop variables. If the first expression is negative, diagnose_numeric() will automatically start with all variables. These arguments are automatically quoted and evaluated in a context where column names represent column positions. They support unquoting and splicing.
an object of tbl_df.
The scope of the diagnosis is the calculate a statistic that can be used to understand the distribution of numerical data. min, Q1, mean, median, Q3, max can be used to estimate the distribution of data. If the number of zero or minus is large, it is necessary to suspect the error of the data. If the number of outliers is large, a strategy of eliminating or replacing outliers is needed.
The information derived from the numerical data diagnosis is as follows.
variables : variable names
min : minimum
Q1 : 25 percentile
mean : arithmetic average
median : median. 50 percentile
Q3 : 75 percentile
max : maximum
zero : count of zero values
minus : count of minus values
outlier : count of outliers
See vignette("diagonosis") for an introduction to these concepts.
# \donttest{
# Diagnosis of numerical variables
diagnose_numeric(heartfailure)
#> variables min Q1 mean median Q3 max
#> 1 age 40.0 51.0 60.82943 60.0 70.0 95.0
#> 2 cpk_enzyme 23.0 116.5 581.83946 250.0 582.0 7861.0
#> 3 ejection_fraction 14.0 30.0 38.08361 38.0 45.0 80.0
#> 4 platelets 25100.0 212500.0 263358.02926 262000.0 303500.0 850000.0
#> 5 creatinine 0.5 0.9 1.39388 1.1 1.4 9.4
#> 6 sodium 113.0 134.0 136.62542 137.0 140.0 148.0
#> 7 time 4.0 73.0 130.26087 115.0 203.0 285.0
#> zero minus outlier
#> 1 0 0 0
#> 2 0 0 29
#> 3 0 0 2
#> 4 0 0 21
#> 5 0 0 29
#> 6 0 0 4
#> 7 0 0 0
# Select the variable to diagnose
diagnose_numeric(heartfailure, cpk_enzyme, sodium)
#> variables min Q1 mean median Q3 max zero minus outlier
#> 1 cpk_enzyme 23 116.5 581.8395 250 582 7861 0 0 29
#> 2 sodium 113 134.0 136.6254 137 140 148 0 0 4
diagnose_numeric(heartfailure, -cpk_enzyme, -sodium)
#> variables min Q1 mean median Q3 max
#> 1 age 40.0 51.0 60.82943 60.0 70.0 95.0
#> 2 ejection_fraction 14.0 30.0 38.08361 38.0 45.0 80.0
#> 3 platelets 25100.0 212500.0 263358.02926 262000.0 303500.0 850000.0
#> 4 creatinine 0.5 0.9 1.39388 1.1 1.4 9.4
#> 5 time 4.0 73.0 130.26087 115.0 203.0 285.0
#> zero minus outlier
#> 1 0 0 0
#> 2 0 0 2
#> 3 0 0 21
#> 4 0 0 29
#> 5 0 0 0
diagnose_numeric(heartfailure, "cpk_enzyme", "sodium")
#> variables min Q1 mean median Q3 max zero minus outlier
#> 1 cpk_enzyme 23 116.5 581.8395 250 582 7861 0 0 29
#> 2 sodium 113 134.0 136.6254 137 140 148 0 0 4
diagnose_numeric(heartfailure, 5)
#> # A tibble: 1 × 10
#> variables min Q1 mean median Q3 max zero minus outlier
#> <chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <int> <int> <int>
#> 1 ejection_fraction 14 30 38.1 38 45 80 0 0 2
# Using pipes ---------------------------------
library(dplyr)
# Diagnosis of all numerical variables
heartfailure %>%
diagnose_numeric()
#> variables min Q1 mean median Q3 max
#> 1 age 40.0 51.0 60.82943 60.0 70.0 95.0
#> 2 cpk_enzyme 23.0 116.5 581.83946 250.0 582.0 7861.0
#> 3 ejection_fraction 14.0 30.0 38.08361 38.0 45.0 80.0
#> 4 platelets 25100.0 212500.0 263358.02926 262000.0 303500.0 850000.0
#> 5 creatinine 0.5 0.9 1.39388 1.1 1.4 9.4
#> 6 sodium 113.0 134.0 136.62542 137.0 140.0 148.0
#> 7 time 4.0 73.0 130.26087 115.0 203.0 285.0
#> zero minus outlier
#> 1 0 0 0
#> 2 0 0 29
#> 3 0 0 2
#> 4 0 0 21
#> 5 0 0 29
#> 6 0 0 4
#> 7 0 0 0
# Positive values select variables
heartfailure %>%
diagnose_numeric(cpk_enzyme, sodium)
#> variables min Q1 mean median Q3 max zero minus outlier
#> 1 cpk_enzyme 23 116.5 581.8395 250 582 7861 0 0 29
#> 2 sodium 113 134.0 136.6254 137 140 148 0 0 4
# Negative values to drop variables
heartfailure %>%
diagnose_numeric(-cpk_enzyme, -sodium)
#> variables min Q1 mean median Q3 max
#> 1 age 40.0 51.0 60.82943 60.0 70.0 95.0
#> 2 ejection_fraction 14.0 30.0 38.08361 38.0 45.0 80.0
#> 3 platelets 25100.0 212500.0 263358.02926 262000.0 303500.0 850000.0
#> 4 creatinine 0.5 0.9 1.39388 1.1 1.4 9.4
#> 5 time 4.0 73.0 130.26087 115.0 203.0 285.0
#> zero minus outlier
#> 1 0 0 0
#> 2 0 0 2
#> 3 0 0 21
#> 4 0 0 29
#> 5 0 0 0
# Positions values select variables
heartfailure %>%
diagnose_numeric(5)
#> # A tibble: 1 × 10
#> variables min Q1 mean median Q3 max zero minus outlier
#> <chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <int> <int> <int>
#> 1 ejection_fraction 14 30 38.1 38 45 80 0 0 2
# Negative values to drop variables
heartfailure %>%
diagnose_numeric(-1, -5)
#> variables min Q1 mean median Q3 max zero
#> 1 cpk_enzyme 23.0 116.5 581.83946 250.0 582.0 7861.0 0
#> 2 platelets 25100.0 212500.0 263358.02926 262000.0 303500.0 850000.0 0
#> 3 creatinine 0.5 0.9 1.39388 1.1 1.4 9.4 0
#> 4 sodium 113.0 134.0 136.62542 137.0 140.0 148.0 0
#> 5 time 4.0 73.0 130.26087 115.0 203.0 285.0 0
#> minus outlier
#> 1 0 29
#> 2 0 21
#> 3 0 29
#> 4 0 4
#> 5 0 0
# Using pipes & dplyr -------------------------
# List of variables containing outliers
heartfailure %>%
diagnose_numeric() %>%
filter(outlier > 0)
#> variables min Q1 mean median Q3 max
#> 1 cpk_enzyme 23.0 116.5 581.83946 250.0 582.0 7861.0
#> 2 ejection_fraction 14.0 30.0 38.08361 38.0 45.0 80.0
#> 3 platelets 25100.0 212500.0 263358.02926 262000.0 303500.0 850000.0
#> 4 creatinine 0.5 0.9 1.39388 1.1 1.4 9.4
#> 5 sodium 113.0 134.0 136.62542 137.0 140.0 148.0
#> zero minus outlier
#> 1 0 0 29
#> 2 0 0 2
#> 3 0 0 21
#> 4 0 0 29
#> 5 0 0 4
# Using group_by ------------------------------
# Calculate the diagnosis of all variables by 'death_event' using group_by()
heartfailure %>%
group_by(death_event) %>%
diagnose_numeric()
#> # A tibble: 14 × 11
#> variables death_event min Q1 mean median Q3 max zero minus
#> <chr> <fct> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <int> <int>
#> 1 age No 40 5 e+1 5.88e1 6 e1 6.5 e1 9 e1 0 0
#> 2 age Yes 42 5.5 e+1 6.52e1 6.5 e1 7.5 e1 9.5 e1 0 0
#> 3 cpk_enzy… No 30 1.09e+2 5.40e2 2.45e2 5.82e2 5.21e3 0 0
#> 4 cpk_enzy… Yes 23 1.29e+2 6.70e2 2.59e2 5.82e2 7.86e3 0 0
#> 5 ejection… No 17 3.5 e+1 4.03e1 3.8 e1 4.5 e1 8 e1 0 0
#> 6 ejection… Yes 14 2.5 e+1 3.35e1 3 e1 3.8 e1 7 e1 0 0
#> 7 platelets No 25100 2.20e+5 2.67e5 2.63e5 3.02e5 8.5 e5 0 0
#> 8 platelets Yes 47000 1.98e+5 2.56e5 2.58e5 3.11e5 6.21e5 0 0
#> 9 creatini… No 0.5 9 e-1 1.18e0 1 e0 1.2 e0 6.1 e0 0 0
#> 10 creatini… Yes 0.6 1.08e+0 1.84e0 1.3 e0 1.9 e0 9.4 e0 0 0
#> 11 sodium No 113 1.36e+2 1.37e2 1.37e2 1.4 e2 1.48e2 0 0
#> 12 sodium Yes 116 1.33e+2 1.35e2 1.36e2 1.38e2 1.46e2 0 0
#> 13 time No 12 9.5 e+1 1.58e2 1.72e2 2.13e2 2.85e2 0 0
#> 14 time Yes 4 2.55e+1 7.09e1 4.45e1 1.02e2 2.41e2 0 0
#> # ℹ 1 more variable: outlier <int>
# }