This analysis uses the Smart City Digital Twin Ecosystem
Dataset (Abdellatif, n.d.), a simulated dataset on Kaggle. Two
of its files are used: city_events.csv, with one row per
event, and traffic.csv, with congestion readings by
district and time.
# The CSV files must be in the same folder as this .Rmd file
events_raw <- read_csv("city_events.csv") %>% clean_names()
traffic_raw <- read_csv("traffic.csv") %>% clean_names()
glimpse(events_raw)
## Rows: 300
## Columns: 9
## $ event_id <chr> "EVT_0001", "EVT_0002", "EVT_0003", "EVT_0004", "E…
## $ event_name <chr> "Conference 2025", "Marathon 2023", "Marathon 2024…
## $ event_type <chr> "Conference", "Marathon", "Marathon", "Conference"…
## $ district_id <chr> "D08", "D18", "D01", "D15", "D08", "D11", "D12", "…
## $ start_timestamp <dttm> 2025-09-21 16:00:00, 2023-04-12 18:00:00, 2024-05…
## $ end_timestamp <dttm> 2025-09-21 19:00:00, 2023-04-12 22:00:00, 2024-05…
## $ expected_attendance <dbl> 6468, 78082, 29996, 3032, 2771, 20816, 3033, 5494,…
## $ actual_attendance <dbl> 6488, 58674, 29136, 2743, 2270, 18316, 2932, 5831,…
## $ event_impact_level <chr> "Medium", "Extreme", "High", "Low", "Low", "High",…
glimpse(traffic_raw)
## Rows: 527,132
## Columns: 13
## $ timestamp <dttm> 2024-09-18 17:00:00, 2023-07-08 17:00:00, 2023-0…
## $ district_id <chr> "D13", "D08", "D17", "D11", "D20", "D08", "D17", …
## $ traffic_volume <dbl> 645, 1321, 1948, 416, 771, 374, 327, NA, 1459, 13…
## $ average_speed_kmh <dbl> 49.14628, 39.82861, 45.80805, 54.32691, 53.69650,…
## $ congestion_index <dbl> 25.226010, 25.148830, 27.286539, 11.527077, 14.91…
## $ accident_count <dbl> 0, 0, 0, 0, 0, 0, 0, NA, 0, 0, 0, 0, 0, 0, 0, 0, …
## $ accident_flag <dbl> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0…
## $ road_incident_count <dbl> 0, 0, 0, 0, 0, 0, 0, NA, 0, 0, 0, 0, 0, 0, 0, 0, …
## $ rush_hour_flag <dbl> 1, 0, 0, 0, 0, 0, 0, NA, 0, 0, 0, 0, 0, 0, 0, 0, …
## $ weekend_flag <dbl> 0, 1, 0, 0, 1, 0, 0, NA, 0, 1, 0, 0, 0, 1, 1, 1, …
## $ weather_impact_score <dbl> 1.0, 1.4, 1.0, 1.0, 1.0, 1.0, 1.0, NA, 1.0, 1.0, …
## $ special_event_impact <dbl> 0, 0, 0, 0, 0, 0, 0, NA, 0, 0, 0, 0, 0, 0, 0, 0, …
## $ sensor_status <chr> "Normal", "Normal", "Normal", "Normal", "Normal",…
congestion, the
average traffic congestion index in the event’s district while the event
is running.attendance: the number of people who actually attended.
More people arriving and leaving should mean more cars, parking demand
and transit use.event_hour: the hour of day the event starts. Events
near rush hour may add to traffic that is already heavy.Each event is matched to the traffic readings in its district from the hour it starts until it ends. Those readings are averaged to give one congestion value per event.
to_datetime <- function(x) {
if (inherits(x, "POSIXt")) return(x)
parse_date_time(as.character(x), orders = c("ymd HMS", "ymd HM", "mdy HMS", "mdy HM"))
}
traffic <- traffic_raw %>%
transmute(district_id,
traffic_time = to_datetime(timestamp),
congestion = as.numeric(congestion_index)) %>%
drop_na()
events <- events_raw %>%
transmute(event_id, district_id,
start = to_datetime(start_timestamp),
end = to_datetime(end_timestamp),
attendance = as.numeric(actual_attendance)) %>%
drop_na() %>%
distinct(event_id, .keep_all = TRUE) %>%
filter(attendance > 0, end >= start) %>%
mutate(window_start = floor_date(start, "hour"),
event_hour = hour(start))
# Named event_data (not "dt", which is a built-in R function)
event_data <- traffic %>%
inner_join(events, by = join_by(district_id, between(traffic_time, window_start, end))) %>%
group_by(event_id, event_hour, attendance) %>%
summarise(congestion = mean(congestion), .groups = "drop") %>%
select(congestion, attendance, event_hour)
nrow(event_data)
## [1] 300
summary(event_data)
## congestion attendance event_hour
## Min. : 9.00 Min. : 859 Min. : 9.00
## 1st Qu.: 32.92 1st Qu.: 5480 1st Qu.:11.00
## Median : 52.37 Median :14753 Median :13.00
## Mean : 56.63 Mean :20032 Mean :13.52
## 3rd Qu.: 78.48 3rd Qu.:29182 3rd Qu.:16.00
## Max. :120.00 Max. :77608 Max. :18.00
congestion_model <- lm(congestion ~ attendance + event_hour, data = event_data)
summary(congestion_model)
##
## Call:
## lm(formula = congestion ~ attendance + event_hour, data = event_data)
##
## Residuals:
## Min 1Q Median 3Q Max
## -59.665 -23.635 -2.427 19.353 71.433
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 5.763e+01 7.981e+00 7.221 4.36e-12 ***
## attendance 5.791e-04 8.484e-05 6.825 4.93e-11 ***
## event_hour -9.319e-01 5.608e-01 -1.662 0.0976 .
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 26.98 on 297 degrees of freedom
## Multiple R-squared: 0.1447, Adjusted R-squared: 0.1389
## F-statistic: 25.11 on 2 and 297 DF, p-value: 8.38e-11
R-squared. The model has an R² of 0.1447 and an adjusted R² of 0.1389. This means actual attendance and event start time together explain about 14.5% of the variation in congestion. The other 85.5% comes from things not in the model, such as weather, road capacity, the day of the week or other events happening at the same time. This is a modest amount. The model captures a real pattern, but it leaves out most of what drives congestion.
Overall p-value. The F-test gives F(2, 297) = 25.11, p < .001. The overall model is statistically significant, so it predicts congestion better than a model with no predictors.
Individual p-values (significant if p < .05):
Significant variables: actual attendance. Insignificant variables: event start hour.
Actual attendance (b = 5.79^{-4}). Holding start time constant, each additional attendee changes the congestion index by 5.79^{-4} points. One person makes very little difference, so it is easier to read at a larger scale:
Attendance in this dataset ranges from about 859 to 77,608 people. Going from the smallest to the largest event therefore predicts a difference of about 44.4 points in congestion. Bigger crowds clearly mean more traffic congestion.
Event start hour (b = -0.932). Holding attendance constant, each hour later that an event starts is associated with a congestion change of -0.932 points. This variable is not significant, so the estimate cannot be read as a real effect. Once attendance is accounted for, start time does not reliably change congestion.
Intercept. The intercept (57.63) is the predicted congestion for an event with zero attendees starting at hour 0. That situation does not exist in the data, so the intercept is only a starting point for the equation and has no real-world meaning.
The residuals-versus-fitted plot checks linearity. If the relationship is linear, the points scatter randomly around the horizontal line at 0, and the red line stays roughly flat, with no clear curve or U-shape.
plot(congestion_model, which = 1)
# Extra check: the straight-line fit (blue) vs a flexible curve (red) for each predictor
event_data %>%
pivot_longer(c(attendance, event_hour), names_to = "predictor") %>%
ggplot(aes(value, congestion)) +
geom_point(alpha = 0.4) +
geom_smooth(method = "lm", colour = "steelblue", se = FALSE) +
geom_smooth(method = "loess", colour = "firebrick", se = FALSE) +
facet_wrap(~ predictor, scales = "free_x") +
labs(x = NULL, y = "Congestion index") +
theme_minimal()
Conclusion. In the residuals-versus-fitted plot, the
residuals scatter evenly above and below zero across the fitted values,
and the red line stays close to the zero line without a strong curve.
The model therefore meets the linearity assumption well
enough. The scatter plots support this: for attendance, the
straight line and the flexible curve mostly agree. For start hour, both
lines are fairly flat, which fits with start hour not being significant.
If the red line in your plot shows a clear bend, especially at the
largest fitted values where the biggest events are, that would suggest a
mild violation. One fix is to log-transform attendance:
lm(congestion ~ log(attendance) + event_hour).
Abdellatif. (n.d.). Smart City Digital Twin Ecosystem Dataset [Data set]. Kaggle.