-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpattern.R
More file actions
643 lines (574 loc) · 24.6 KB
/
pattern.R
File metadata and controls
643 lines (574 loc) · 24.6 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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
patternUI <- function(id, label = 'pattern') {
ns <- NS(id)
h3('Pattern Recognition')
shinyjs::useShinyjs()
fluidPage(
# Help Box
fluidRow(
column(
width = 12,
div(
style = "margin-bottom: 20px; border: 1px solid #e5e7eb; border-radius: 4px; background-color: #ffffff; box-shadow: 0 1px 2px rgba(0,0,0,0.05);",
div(
style = "padding: 12px 20px; background: linear-gradient(135deg, #eef2ff 0%, #ffffff 100%); border-bottom: 1px solid #e5e7eb; cursor: pointer; border-radius: 4px 4px 0 0;",
onclick = "$(this).next().slideToggle(200);",
tags$span(
style = "font-size: 15px; font-weight: 500; color: #6366f1;",
icon('question-circle'), ' Need help? Click to expand'
)
),
div(
style = "display: none; padding: 20px;",
p(
style = "font-size: 14px; line-height: 1.6; color: #374151;",
"Pattern Recognition discovers recurring consumption patterns in your energy usage across days, weeks, and seasons. This helps you understand your typical behavior and optimize usage schedules."
),
tags$ul(
style = "font-size: 14px; line-height: 1.6; color: #4b5563;",
tags$li(tags$strong("How it works:"), " Select a pattern type to automatically analyze your consumption patterns. Results update instantly."),
tags$li(tags$strong("Daily Patterns:"), " Shows your average hourly consumption profile across all days."),
tags$li(tags$strong("Weekly Patterns:"), " Compares usage across different days of the week (Monday through Sunday)."),
tags$li(tags$strong("Day Type Comparison:"), " Reveals differences between weekday and weekend consumption habits."),
tags$li(tags$strong("Load Curve Clustering:"), " Groups similar consumption days together to identify different usage modes (e.g., work-from-home vs away days).")
)
)
)
)
),
# Control Panel
fluidRow(
shinydashboard::box(
width = 12,
title = 'Pattern Analysis Settings', status = 'primary', solidHeader = TRUE,
column(width = 4,
selectInput(
inputId = ns('pattern_type'),
label = 'Pattern Type',
choices = list(
'Daily Patterns' = 'daily',
'Weekly Patterns' = 'weekly',
'Day Type Comparison' = 'daytype',
'Load Curve Clustering' = 'clustering'
),
selected = 'daily'
)),
column(width = 4,
numericInput(
inputId = ns('num_clusters'),
label = 'Number of Clusters',
value = 3,
min = 2,
max = 7,
step = 1
))
)
),
# Summary Metrics
fluidRow(
column(width = 3,
shinydashboard::valueBoxOutput(ns('peak_hour'), width = 12)),
column(width = 3,
shinydashboard::valueBoxOutput(ns('avg_daily'), width = 12)),
column(width = 3,
shinydashboard::valueBoxOutput(ns('pattern_consistency'), width = 12)),
column(width = 3,
shinydashboard::valueBoxOutput(ns('weekend_vs_weekday'), width = 12))
),
# Main Pattern Visualization
fluidRow(
shinydashboard::box(
width = 12,
title = 'Pattern Visualization',
status = 'info',
solidHeader = TRUE,
shinycssloaders::withSpinner(
plotly::plotlyOutput(outputId = ns("pattern_main_plot"), height = '450px')
)
)
),
# Secondary Visualizations
fluidRow(
column(width = 6,
shinydashboard::box(
width = 12,
title = 'Hourly Consumption Heatmap',
status = 'info',
solidHeader = TRUE,
shinycssloaders::withSpinner(
plotly::plotlyOutput(outputId = ns("heatmap_plot"))
)
)),
column(width = 6,
shinydashboard::box(
width = 12,
title = 'Pattern Statistics',
status = 'info',
solidHeader = TRUE,
uiOutput(ns('pattern_stats'))
))
),
# Clustering Results (conditional - single box)
uiOutput(ns('clustering_box'))
)
}
patternServer <- function(id, dt) {
moduleServer(
id,
function(input, output, session) {
# Pattern Analysis Reactive ----
pattern_results <- reactive({
req(dt())
req(input$pattern_type)
logger::log_info("Running pattern analysis: {input$pattern_type}")
df <- copy(dt())
# Add derived columns
df[, start_date := as.Date(dttm_start)]
df[, weekday := weekdays(start_date)]
df[, is_weekend := weekday %in% c("Saturday", "Sunday")]
df[, day_type := ifelse(is_weekend, "Weekend", "Weekday")]
pattern_type <- input$pattern_type
results <- list()
results$type <- pattern_type
results$data <- df
# Common statistics
results$total_days <- length(unique(df$start_date))
results$avg_daily_consumption <- round(sum(df$value, na.rm = TRUE) / results$total_days, 2)
# Peak hour analysis
hourly_avg <- df[, .(avg_value = mean(value, na.rm = TRUE)), by = hour][order(-avg_value)]
results$peak_hour <- hourly_avg[1, hour]
results$peak_value <- round(hourly_avg[1, avg_value], 2)
# Pattern consistency (coefficient of variation)
daily_totals <- df[, .(daily_total = sum(value, na.rm = TRUE)), by = start_date]
mean_daily <- mean(daily_totals$daily_total, na.rm = TRUE)
sd_daily <- sd(daily_totals$daily_total, na.rm = TRUE)
# Use safe_divide to prevent division by zero
results$cv <- round(safe_divide(sd_daily, mean_daily) * 100, 1)
results$consistency_score <- max(0, 100 - results$cv)
# Weekend vs Weekday comparison
daytype_avg <- df[, .(avg_consumption = mean(value, na.rm = TRUE)), by = day_type]
if (nrow(daytype_avg) == 2) {
weekday_avg <- daytype_avg[day_type == "Weekday", avg_consumption]
weekend_avg <- daytype_avg[day_type == "Weekend", avg_consumption]
# Use safe_divide to prevent division by zero
results$weekend_diff_pct <- round(safe_divide(weekend_avg - weekday_avg, weekday_avg) * 100, 1)
} else {
results$weekend_diff_pct <- 0
}
# Pattern-specific analysis
if (pattern_type == 'daily') {
# Average hourly pattern across all days
results$hourly_pattern <- df[, .(
mean_value = mean(value, na.rm = TRUE),
sd_value = sd(value, na.rm = TRUE),
min_value = min(value, na.rm = TRUE),
max_value = max(value, na.rm = TRUE),
count = .N
), by = hour][order(hour)]
} else if (pattern_type == 'weekly') {
# Pattern by day of week
df[, weekday_ordered := factor(weekday,
levels = c("Monday", "Tuesday", "Wednesday",
"Thursday", "Friday", "Saturday", "Sunday"))]
results$weekly_pattern <- df[, .(
mean_value = mean(value, na.rm = TRUE),
total_consumption = sum(value, na.rm = TRUE),
count = .N
), by = .(weekday_ordered, hour)][order(weekday_ordered, hour)]
results$daily_totals <- df[, .(
total = sum(value, na.rm = TRUE),
avg = mean(value, na.rm = TRUE)
), by = weekday_ordered][order(weekday_ordered)]
} else if (pattern_type == 'daytype') {
# Weekday vs Weekend patterns
results$daytype_pattern <- df[, .(
mean_value = mean(value, na.rm = TRUE),
sd_value = sd(value, na.rm = TRUE),
count = .N
), by = .(day_type, hour)][order(day_type, hour)]
results$daytype_summary <- df[, .(
avg_hourly = mean(value, na.rm = TRUE),
total_daily = sum(value, na.rm = TRUE) / length(unique(start_date)),
peak_hour = hour[which.max(value)]
), by = day_type]
} else if (pattern_type == 'clustering') {
# K-means clustering of daily load curves
num_clusters <- input$num_clusters
# Pivot data to wide format (days x hours)
df_wide <- dcast(df, start_date ~ hour, value.var = "value", fun.aggregate = mean)
# Remove date column and handle NAs
cluster_data <- as.matrix(df_wide[, -1])
cluster_data[is.na(cluster_data)] <- 0
if (nrow(cluster_data) >= num_clusters) {
# Perform k-means clustering
set.seed(123)
km <- kmeans(cluster_data, centers = num_clusters, nstart = 25)
# Add cluster assignment back to data
df_wide[, cluster := km$cluster]
results$cluster_centers <- data.table(
cluster = 1:num_clusters,
as.data.table(km$centers)
)
# Melt centers for plotting
results$cluster_centers_long <- melt(
results$cluster_centers,
id.vars = "cluster",
variable.name = "hour",
value.name = "value"
)
results$cluster_centers_long[, hour := as.numeric(as.character(hour))]
# Cluster sizes and characteristics
results$cluster_info <- data.table(
cluster = 1:num_clusters,
size = as.numeric(table(km$cluster)),
avg_consumption = sapply(1:num_clusters, function(i) {
mean(rowSums(cluster_data[km$cluster == i, , drop = FALSE]))
}),
peak_hour = sapply(1:num_clusters, function(i) {
which.max(km$centers[i, ]) - 1
})
)
results$cluster_info[, percentage := round(size / sum(size) * 100, 1)]
# Add cluster info back to original data
df_with_clusters <- merge(df, df_wide[, .(start_date, cluster)], by = "start_date")
results$data_with_clusters <- df_with_clusters
} else {
logger::log_warn("Insufficient data for clustering: {nrow(cluster_data)} days < {num_clusters} clusters")
results$clustering_error <- "Insufficient data for clustering"
}
}
logger::log_info("Pattern analysis completed: {pattern_type}")
return(results)
})
# Value Boxes ----
output$peak_hour <- shinydashboard::renderValueBox({
results <- pattern_results()
shinydashboard::valueBox(
value = paste0(results$peak_hour, ":00"),
subtitle = paste0("Peak Hour (", results$peak_value, " kWh)"),
icon = icon("clock"),
color = "yellow"
)
})
output$avg_daily <- shinydashboard::renderValueBox({
results <- pattern_results()
shinydashboard::valueBox(
value = paste0(results$avg_daily_consumption, " kWh"),
subtitle = "Avg Daily Consumption",
icon = icon("chart-bar"),
color = "blue"
)
})
output$pattern_consistency <- shinydashboard::renderValueBox({
results <- pattern_results()
color_val <- if (results$consistency_score >= 70) "green" else if (results$consistency_score >= 50) "yellow" else "red"
shinydashboard::valueBox(
value = paste0(round(results$consistency_score, 0), "%"),
subtitle = "Pattern Consistency",
icon = icon("sync"),
color = color_val
)
})
output$weekend_vs_weekday <- shinydashboard::renderValueBox({
results <- pattern_results()
diff_pct <- results$weekend_diff_pct
color_val <- if (abs(diff_pct) < 10) "green" else if (abs(diff_pct) < 25) "yellow" else "red"
sign_char <- ifelse(diff_pct > 0, "+", "")
shinydashboard::valueBox(
value = paste0(sign_char, diff_pct, "%"),
subtitle = "Weekend vs Weekday",
icon = icon("calendar"),
color = color_val
)
})
# Main Pattern Plot ----
output$pattern_main_plot <- plotly::renderPlotly({
results <- pattern_results()
validate(
need(nrow(results$data) > 0, 'No data available for the selected date range. Please select a valid date range with data.')
)
pattern_type <- results$type
if (pattern_type == 'daily') {
# Daily average pattern with confidence band
data <- results$hourly_pattern
validate(
need(nrow(data) > 0, 'No data available for the selected date range. Please select a valid date range with data.')
)
plotly::plot_ly(data, x = ~hour) |>
plotly::add_ribbons(
ymin = ~pmax(0, mean_value - sd_value),
ymax = ~mean_value + sd_value,
name = "± 1 SD",
line = list(color = 'transparent'),
fillcolor = 'rgba(70, 130, 180, 0.3)',
hoverinfo = 'skip'
) |>
plotly::add_trace(
y = ~mean_value,
type = 'scatter',
mode = 'lines+markers',
name = 'Average Pattern',
line = list(color = '#4682B4', width = 3),
marker = list(size = 8, color = '#4682B4'),
text = ~paste0("Hour: ", hour, ":00<br>",
"Avg: ", round(mean_value, 2), " kWh<br>",
"Min: ", round(min_value, 2), " kWh<br>",
"Max: ", round(max_value, 2), " kWh"),
hoverinfo = 'text'
) |>
plotly::layout(
title = "Average Daily Consumption Pattern",
xaxis = list(title = "Hour of Day", dtick = 2),
yaxis = list(title = "Consumption (kWh)"),
hovermode = 'closest'
) |>
plotly::config(
modeBarButtonsToRemove = list(
'pan2d', 'select2d', 'lasso2d',
'toggleSpikelines', 'hoverClosestCartesian', 'hoverCompareCartesian'
),
doubleClick = 'reset',
displaylogo = FALSE
)
} else if (pattern_type == 'weekly') {
# Weekly pattern by day
data <- results$weekly_pattern
plotly::plot_ly(data, x = ~hour, y = ~mean_value,
color = ~weekday_ordered, colors = 'Set2',
type = 'scatter', mode = 'lines+markers',
line = list(width = 2),
marker = list(size = 4)) |>
plotly::layout(
title = "Weekly Consumption Pattern by Day of Week",
xaxis = list(title = "Hour of Day", dtick = 2),
yaxis = list(title = "Average Consumption (kWh)"),
hovermode = 'closest'
) |>
plotly::config(
modeBarButtonsToRemove = list(
'pan2d', 'select2d', 'lasso2d',
'toggleSpikelines', 'hoverClosestCartesian', 'hoverCompareCartesian'
),
doubleClick = 'reset',
displaylogo = FALSE
)
} else if (pattern_type == 'daytype') {
# Weekday vs Weekend comparison
data <- results$daytype_pattern
plotly::plot_ly(data, x = ~hour, y = ~mean_value,
color = ~day_type,
colors = c('Weekday' = '#2E86AB', 'Weekend' = '#A23B72'),
type = 'scatter', mode = 'lines+markers',
line = list(width = 3),
marker = list(size = 6)) |>
plotly::layout(
title = "Weekday vs Weekend Consumption Pattern",
xaxis = list(title = "Hour of Day", dtick = 2),
yaxis = list(title = "Average Consumption (kWh)"),
hovermode = 'closest'
) |>
plotly::config(
modeBarButtonsToRemove = list(
'pan2d', 'select2d', 'lasso2d',
'toggleSpikelines', 'hoverClosestCartesian', 'hoverCompareCartesian'
),
doubleClick = 'reset',
displaylogo = FALSE
)
} else if (pattern_type == 'clustering') {
if (!is.null(results$clustering_error)) {
plotly::plot_ly() |>
plotly::layout(
title = results$clustering_error,
xaxis = list(visible = FALSE),
yaxis = list(visible = FALSE)
)
} else {
# Plot cluster centers
data <- results$cluster_centers_long
plotly::plot_ly(data, x = ~hour, y = ~value,
color = ~factor(cluster),
colors = 'Set1',
type = 'scatter', mode = 'lines+markers',
line = list(width = 3),
marker = list(size = 6)) |>
plotly::layout(
title = "Load Curve Clusters (Typical Daily Patterns)",
xaxis = list(title = "Hour of Day", dtick = 2),
yaxis = list(title = "Average Consumption (kWh)"),
hovermode = 'closest',
legend = list(title = list(text = 'Cluster'))
) |>
plotly::config(
modeBarButtonsToRemove = list(
'pan2d', 'select2d', 'lasso2d',
'toggleSpikelines', 'hoverClosestCartesian', 'hoverCompareCartesian'
),
doubleClick = 'reset',
displaylogo = FALSE
)
}
}
})
# Heatmap Plot ----
output$heatmap_plot <- plotly::renderPlotly({
results <- pattern_results()
df <- results$data
validate(
need(nrow(df) > 0, 'No data available for the selected date range. Please select a valid date range with data.')
)
# Create hourly x day heatmap
df[, date_label := format(start_date, "%Y-%m-%d")]
heatmap_data <- df[, .(avg_value = mean(value, na.rm = TRUE)),
by = .(date_label, hour)]
# Convert to wide format for heatmap
heatmap_wide <- dcast(heatmap_data, date_label ~ hour, value.var = "avg_value")
# Convert to matrix (excluding date column)
z_matrix <- as.matrix(heatmap_wide[, -1])
plotly::plot_ly(
x = colnames(z_matrix),
y = heatmap_wide$date_label,
z = z_matrix,
type = "heatmap",
colorscale = "RdYlBu",
reversescale = TRUE,
colorbar = list(title = "kWh")
) |>
plotly::layout(
title = "Consumption Heatmap (Hour x Day)",
xaxis = list(title = "Hour of Day"),
yaxis = list(title = "Date")
) |>
plotly::config(displaylogo = FALSE)
})
# Pattern Statistics ----
output$pattern_stats <- renderUI({
results <- pattern_results()
df <- results$data
# Get actual date range from data
date_range_start <- min(as.Date(df$start_date))
date_range_end <- max(as.Date(df$start_date))
stats_list <- tagList(
tags$div(
style = "padding: 10px;",
tags$h4("Pattern Analysis Summary"),
tags$hr(),
tags$p(strong("Analysis Period: "),
paste(date_range_start, "to", date_range_end)),
tags$p(strong("Total Days Analyzed: "), results$total_days),
tags$p(strong("Average Daily Consumption: "),
paste(results$avg_daily_consumption, "kWh")),
tags$p(strong("Peak Hour: "),
paste0(results$peak_hour, ":00 (", results$peak_value, " kWh)")),
tags$p(strong("Consistency Score: "),
paste0(round(results$consistency_score, 0), "%")),
tags$p(strong("Coefficient of Variation: "),
paste0(results$cv, "%"))
)
)
# Add pattern-specific stats
if (results$type == 'daytype' && !is.null(results$daytype_summary)) {
stats_list <- tagList(
stats_list,
tags$hr(),
tags$h5("Day Type Comparison"),
tags$table(
class = "table table-condensed",
tags$thead(tags$tr(tags$th(""), tags$th("Weekday"), tags$th("Weekend"))),
tags$tbody(
tags$tr(
tags$td("Avg Hourly"),
tags$td(paste0(round(results$daytype_summary[day_type == "Weekday", avg_hourly], 2), " kWh")),
tags$td(paste0(round(results$daytype_summary[day_type == "Weekend", avg_hourly], 2), " kWh"))
),
tags$tr(
tags$td("Avg Daily"),
tags$td(paste0(round(results$daytype_summary[day_type == "Weekday", total_daily], 2), " kWh")),
tags$td(paste0(round(results$daytype_summary[day_type == "Weekend", total_daily], 2), " kWh"))
)
)
)
)
}
stats_list
})
# Clustering Box (conditional rendering) ----
output$clustering_box <- renderUI({
req(input$pattern_type)
if (input$pattern_type == 'clustering') {
fluidRow(
shinydashboard::box(
width = 12,
title = 'Clustering Analysis',
status = 'warning',
solidHeader = TRUE,
fluidRow(
column(width = 8,
h4('Cluster Profiles'),
shinycssloaders::withSpinner(
plotly::plotlyOutput(outputId = session$ns("cluster_profiles"))
)),
column(width = 4,
h4('Cluster Distribution'),
DT::dataTableOutput(session$ns('cluster_table'))
)
)
)
)
} else {
NULL
}
})
# Cluster Profiles Plot ----
output$cluster_profiles <- plotly::renderPlotly({
results <- pattern_results()
req(results$type == 'clustering')
req(!is.null(results$cluster_centers_long))
data <- results$cluster_centers_long
plotly::plot_ly(data, x = ~hour, y = ~value,
color = ~factor(cluster),
colors = 'Set1',
type = 'scatter', mode = 'lines+markers',
line = list(width = 2),
marker = list(size = 5),
fill = 'tozeroy',
fillcolor = 'rgba(0,0,0,0.1)') |>
plotly::layout(
title = "Cluster Profiles",
xaxis = list(title = "Hour of Day", dtick = 2),
yaxis = list(title = "Average Consumption (kWh)"),
legend = list(title = list(text = 'Cluster'))
) |>
plotly::config(displaylogo = FALSE)
})
# Cluster Table ----
output$cluster_table <- DT::renderDataTable({
results <- pattern_results()
req(results$type == 'clustering')
req(!is.null(results$cluster_info))
cluster_data <- results$cluster_info[, .(
Cluster = cluster,
Days = size,
Percentage = paste0(percentage, "%"),
Avg_Daily_kWh = round(avg_consumption, 2),
Peak_Hour = paste0(peak_hour, ":00")
)]
DT::datatable(
cluster_data,
options = list(
pageLength = 10,
dom = 't',
ordering = FALSE
),
rownames = FALSE
) |>
DT::formatStyle(
'Cluster',
backgroundColor = DT::styleEqual(
1:nrow(cluster_data),
RColorBrewer::brewer.pal(max(3, nrow(cluster_data)), "Set1")[1:nrow(cluster_data)]
)
)
})
}
)
}