|
| 1 | +#' Collapse Timeframes in a Longitudinal Edgelist |
| 2 | +#' |
| 3 | +#' @description |
| 4 | +#' Allows users to take a high-resolution or continuous-time longitudinal |
| 5 | +#' edgelist and dynamically collapse or discretize it into larger time windows. |
| 6 | +#' The output is a shorter, aggregated edgelist ready to be passed into |
| 7 | +#' \code{[edgelist_to_adjmat]} or \code{[as_diffnet]}. |
| 8 | +#' |
| 9 | +#' @param edgelist A \code{data.frame} representing the longitudinal edgelist. |
| 10 | +#' @param ego Character scalar. Name of the column representing the ego (sender). |
| 11 | +#' @param alter Character scalar. Name of the column representing the alter (receiver). |
| 12 | +#' @param timevar Character scalar. Name of the column representing the time variable. |
| 13 | +#' @param weightvar Character scalar or \code{NULL}. Name of the column representing |
| 14 | +#' the edge weight. If \code{NULL}, the function tallies the number of interactions |
| 15 | +#' within the time window as the weight. |
| 16 | +#' @param window_size Numeric scalar. The size of the time window to collapse into. |
| 17 | +#' @param time_format Character scalar or \code{NULL}. If the time variable is a |
| 18 | +#' character or factor, the format passed to \code{as.POSIXct}. |
| 19 | +#' For example, \code{"\%d-\%m-\%Y \%H:\%M"}. |
| 20 | +#' @param relative_time Logical scalar. If \code{TRUE}, normalizes the binned |
| 21 | +#' times into a strict integer sequence starting at 1 (1, 2, 3...). |
| 22 | +#' @param binarize Logical scalar. If \code{TRUE}, sets all resulting edge weights to 1. |
| 23 | +#' @param cumulative Logical scalar. If \code{TRUE}, edges from previous time windows |
| 24 | +#' are carried over to subsequent windows. |
| 25 | +#' @param symmetric Logical scalar. If \code{TRUE}, the resulting graph will be |
| 26 | +#' symmetrized (i.e., if an edge A->B exists, an edge B->A is added). |
| 27 | +#' |
| 28 | +#' @return A \code{data.frame} with 4 columns: the ego, the alter, the new collapsed |
| 29 | +#' discrete time, and the aggregated weight. |
| 30 | +#' |
| 31 | +#' @export |
| 32 | +#' @examples |
| 33 | +#' \dontrun{ |
| 34 | +#' # Load the package's hourly dataset |
| 35 | +#' load(system.file("data/epigames_raw.rda", package = "netdiffuseR")) |
| 36 | +#' |
| 37 | +#' # Collapse the hourly edgelist into a daily edgelist (window_size = 24) |
| 38 | +#' daily_edgelist <- collapse_timeframes( |
| 39 | +#' edgelist = epigames_raw$edgelist, |
| 40 | +#' timevar = "time", |
| 41 | +#' weightvar = "weight", |
| 42 | +#' window_size = 24 |
| 43 | +#' ) |
| 44 | +#' head(daily_edgelist) |
| 45 | +#' } |
| 46 | +collapse_timeframes <- function( |
| 47 | + edgelist, |
| 48 | + ego = "sender", |
| 49 | + alter = "receiver", |
| 50 | + timevar = "time", |
| 51 | + weightvar = NULL, |
| 52 | + window_size = 1, |
| 53 | + time_format = NULL, |
| 54 | + relative_time = TRUE, |
| 55 | + binarize = FALSE, |
| 56 | + cumulative = FALSE, |
| 57 | + symmetric = FALSE) { |
| 58 | + # Step 1: Time Column Parsing |
| 59 | + time_raw <- edgelist[[timevar]] |
| 60 | + |
| 61 | + if (is.character(time_raw) || is.factor(time_raw)) { |
| 62 | + if (!is.null(time_format)) { |
| 63 | + time_raw <- as.numeric(as.POSIXct(as.character(time_raw), format = time_format)) |
| 64 | + } else { |
| 65 | + time_raw <- as.numeric(as.POSIXct(as.character(time_raw))) |
| 66 | + } |
| 67 | + } else if (!is.numeric(time_raw) && !is.integer(time_raw)) { |
| 68 | + # e.g., Date or POSIXct already |
| 69 | + time_raw <- as.numeric(time_raw) |
| 70 | + } |
| 71 | + |
| 72 | + # Check for NAs after conversion |
| 73 | + if (any(is.na(time_raw))) { |
| 74 | + warning("There are NA values in the parsed time variable.") |
| 75 | + } |
| 76 | + |
| 77 | + # Step 2: Binning / Window Creation |
| 78 | + t_min <- min(time_raw, na.rm = TRUE) |
| 79 | + # Adding a tiny offset so min time doesn't fall out of bounds or shift unnecessarily |
| 80 | + discrete_time <- ceiling((time_raw - t_min + 1e-9) / window_size) |
| 81 | + # Ensure the minimum index is 1 at this stage |
| 82 | + min_dt <- min(discrete_time, na.rm = TRUE) |
| 83 | + if (min_dt < 1) { |
| 84 | + discrete_time <- discrete_time - min_dt + 1 |
| 85 | + } |
| 86 | + |
| 87 | + # Step 3: Handling relative_time |
| 88 | + if (relative_time) { # e.g. strict sequence 1, 2, 3 |
| 89 | + sorted_unique_times <- sort(unique(discrete_time[!is.na(discrete_time)])) |
| 90 | + time_map <- stats::setNames(seq_along(sorted_unique_times), sorted_unique_times) |
| 91 | + discrete_time <- unname(time_map[as.character(discrete_time)]) |
| 92 | + } |
| 93 | + |
| 94 | + # Create a working data frame to hold things |
| 95 | + work_df <- data.frame( |
| 96 | + ego_col = edgelist[[ego]], |
| 97 | + alter_col = edgelist[[alter]], |
| 98 | + time_col = discrete_time |
| 99 | + ) |
| 100 | + |
| 101 | + # Step 4: Aggregation |
| 102 | + if (is.null(weightvar)) { |
| 103 | + work_df$weight_col <- 1 |
| 104 | + } else { |
| 105 | + work_df$weight_col <- edgelist[[weightvar]] |
| 106 | + } |
| 107 | + |
| 108 | + # Remove rows with NAs in essential grouping variables |
| 109 | + work_df <- work_df[!is.na(work_df$ego_col) & !is.na(work_df$alter_col) & !is.na(work_df$time_col), ] |
| 110 | + |
| 111 | + agg_df <- stats::aggregate( |
| 112 | + weight_col ~ ego_col + alter_col + time_col, |
| 113 | + data = work_df, |
| 114 | + FUN = sum, |
| 115 | + na.rm = TRUE |
| 116 | + ) |
| 117 | + |
| 118 | + # Step 5: Output with 4 clean columns |
| 119 | + weight_col_name <- if (is.null(weightvar)) "weight" else weightvar |
| 120 | + colnames(agg_df) <- c(ego, alter, timevar, weight_col_name) |
| 121 | + |
| 122 | + # Step 6: Post-aggregation processing |
| 123 | + |
| 124 | + # 6.1 Binarize |
| 125 | + if (binarize) { |
| 126 | + agg_df[[weight_col_name]] <- 1 |
| 127 | + } |
| 128 | + |
| 129 | + # 6.2 Symmetrize |
| 130 | + if (symmetric) { |
| 131 | + rev_df <- agg_df |
| 132 | + rev_df[[ego]] <- agg_df[[alter]] |
| 133 | + rev_df[[alter]] <- agg_df[[ego]] |
| 134 | + |
| 135 | + # Combine and de-duplicate (in case they already existed symmetrically) |
| 136 | + agg_df <- unique(rbind(agg_df, rev_df)) |
| 137 | + } |
| 138 | + |
| 139 | + # 6.3 Cumulative |
| 140 | + if (cumulative) { |
| 141 | + all_periods <- sort(unique(agg_df[[timevar]])) |
| 142 | + if (length(all_periods) > 1) { |
| 143 | + cumulative_el <- agg_df[agg_df[[timevar]] == all_periods[1], ] |
| 144 | + for (t_idx in 2:length(all_periods)) { |
| 145 | + t <- all_periods[t_idx] |
| 146 | + current <- agg_df[agg_df[[timevar]] == t, ] |
| 147 | + prev <- cumulative_el[cumulative_el[[timevar]] == all_periods[t_idx - 1], ] |
| 148 | + if (nrow(prev) > 0) { |
| 149 | + prev[[timevar]] <- t |
| 150 | + } |
| 151 | + # Combine current window with previous accumulated edges and de-duplicate |
| 152 | + combined <- unique(rbind(current, prev)) |
| 153 | + cumulative_el <- rbind(cumulative_el, combined) |
| 154 | + } |
| 155 | + agg_df <- cumulative_el |
| 156 | + } |
| 157 | + } |
| 158 | + |
| 159 | + # Apply standard sort for consistent outputs: time, ego, alter |
| 160 | + order_idx <- order(agg_df[[timevar]], agg_df[[ego]], agg_df[[alter]]) |
| 161 | + agg_df <- agg_df[order_idx, ] |
| 162 | + rownames(agg_df) <- NULL |
| 163 | + |
| 164 | + return(agg_df) |
| 165 | +} |
0 commit comments