-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdescriptive.R
More file actions
168 lines (130 loc) · 4.62 KB
/
descriptive.R
File metadata and controls
168 lines (130 loc) · 4.62 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
# Fausford Descriptive functions
library(patchwork)
handle_numeric <- function(data) {
# Verify input is a data frame
if (!is.data.frame(data)) {
stop("Input must be a data frame")
}
# Identify numeric columns
numeric_cols <- sapply(data, is.numeric)
# If no numeric columns found
if (sum(numeric_cols) == 0) {
message("No numeric columns found in the dataset")
return(NULL)
}
numeric_data <- data[, numeric_cols, drop = FALSE]
# Calculate statistics for each numeric column
stats_list <- lapply(numeric_data, function(x) {
data.frame(
mean = mean(x, na.rm = TRUE),
sd = sd(x, na.rm = TRUE),
min = min(x, na.rm = TRUE),
max = max(x, na.rm = TRUE),
range = diff(range(x, na.rm = TRUE)),
na_count = sum(is.na(x)),
stringsAsFactors = FALSE
)
})
# Combine all statistics
stats_df <- do.call(rbind, stats_list)
stats_df$variable <- rownames(stats_df)
rownames(stats_df) <- NULL
# Reorder columns
stats_df <- stats_df[, c("variable", "mean", "sd", "min", "max", "range", "na_count")]
return(stats_df)
}
plot_numeric <- function(data, pairs_to_plot = NULL) {
# Verify input is a data frame
if (!is.data.frame(data)) {
stop("Input must be a data frame")
}
# Check for required packages
if (!requireNamespace("ggplot2", quietly = TRUE)) {
install.packages("ggplot2")
}
if (!requireNamespace("patchwork", quietly = TRUE)) {
install.packages("patchwork")
}
library(ggplot2)
library(patchwork)
# Identify numeric columns
numeric_cols <- sapply(data, is.numeric)
if (sum(numeric_cols) == 0) {
message("No numeric columns found to plot")
return(NULL)
}
numeric_data <- data[, numeric_cols, drop = FALSE]
num_vars <- names(numeric_data)
# Create histograms
hist_plots <- lapply(num_vars, function(var) {
ggplot(data, aes(x = .data[[var]])) +
geom_histogram(fill = "skyblue", color = "black", bins = 30) +
labs(title = paste("Distribution of", var),
x = var, y = "Frequency") +
theme_minimal()
})
# Arrange histograms
hist_grid <- wrap_plots(hist_plots) +
plot_annotation(title = "Numeric Variables Distribution")
print(hist_grid)
# Create scatterplots if requested
if (!is.null(pairs_to_plot)) {
valid_pairs <- pairs_to_plot[pairs_to_plot %in% num_vars]
if (length(valid_pairs) >= 2) {
pairs <- combn(valid_pairs, 2, simplify = FALSE)
scatter_plots <- lapply(pairs, function(pair) {
ggplot(data, aes(x = .data[[pair[1]]], y = .data[[pair[2]]])) +
geom_point(alpha = 0.6, color = "steelblue") +
geom_smooth(method = "lm", color = "red", se = FALSE) +
labs(x = pair[1], y = pair[2]) +
theme_minimal()
})
scatter_grid <- wrap_plots(scatter_plots) +
plot_annotation(title = "Scatterplot Matrix of Selected Variables")
print(scatter_grid)
}
}
invisible(list(histograms = hist_plots))
}
# plot numeric
plot_numeric <- function(data, pairs_to_plot = NULL) {
numeric_cols <- sapply(data, is.numeric)
if (sum(numeric_cols) == 0) {
message("No numeric columns found to plot")
return(NULL)
}
numeric_data <- data[, numeric_cols, drop = FALSE]
num_vars <- names(numeric_data)
hist_plots <- lapply(num_vars, function(var) {
ggplot(data, aes(x = .data[[var]])) +
geom_histogram(fill = "skyblue", color = "black", bins = 30) +
labs(title = paste("Distribution of", var),
x = var, y = "Frequency") +
theme_minimal()
})
hist_grid <- wrap_plots(hist_plots) +
plot_annotation(title = "Numeric Variables Distribution")
print(hist_grid)
if (!is.null(pairs_to_plot)) {
if (length(pairs_to_plot) >= 2) {
pairs_to_plot <- pairs_to_plot[pairs_to_plot %in% num_vars]
if (length(pairs_to_plot) >= 2) {
# Create all possible pairs
pairs <- combn(pairs_to_plot, 2, simplify = FALSE)
scatter_plots <- lapply(pairs, function(pair) {
ggplot(data, aes(x = .data[[pair[1]]], y = .data[[pair[2]]])) +
geom_point(alpha = 0.6, color = "steelblue") +
geom_smooth(method = "lm", color = "red", se = FALSE) +
labs(x = pair[1], y = pair[2]) +
theme_minimal()
})
scatter_grid <- wrap_plots(scatter_plots) +
plot_annotation(title = "Scatterplot Matrix of Selected Variables")
print(scatter_grid)
} else {
message("Insufficient valid variables for scatterplot matrix")
}
}
}
invisible(list(histograms = hist_plots))
}