Skip to contents

This function generates context data for each unit of analysis in your dataset, applying subsetting rules ("hoo rules") and optional splitting rules to organize the data for network accumulation.

Usage

contexts(x, hoo_rules, units_by = NULL, split_rules = NULL)

Arguments

x

A data.frame or TMA model object containing the raw input data.

hoo_rules

A list of logical expressions (see [rules()]) specifying how to subset the data for each context/unit.

units_by

Character vector of column names to use for defining units of analysis (e.g., c("userID", "condition")).

split_rules

Optional. Either a function or an expression specifying how to further split each context (e.g., by time period or other grouping variable).

Value

A TMA model object with updated contexts for each unit, where each context is a data.table containing only the relevant rows for that unit and context. The object includes attributes for unit labels and context row indices.

Details

This function is a core part of the TMA workflow. It first applies the specified `hoo_rules` to subset the data for each unit, then (optionally) applies `split_rules` to further divide each context. The resulting contexts are used in subsequent accumulation and network analysis steps.

Examples

data(test_mockdata, package = "tma")
mock_data <- test_mockdata[test_mockdata$chatGroup == "PAM",]
unit_cols <- c("userID", "condition")
codes <- c("A", "B", "C")
HOO_rules_model <- rules(
  modality %in% "chat" & chatGroup %in% UNIT$chatGroup & condition %in% UNIT$condition, 
  modality %in% "resource" & userID %in% UNIT$userID & condition %in% UNIT$condition
)
 
context_model <- contexts(
  x = mock_data,
  units = unit_cols,
  hoo_rules = HOO_rules_model
)
str(context_model$model$contexts)
#> List of 3
#>  $ User1::FirstHalf:Classes ‘data.table’ and 'data.frame':	10 obs. of  16 variables:
#>   ..$ V1               : int [1:10] 1 3 4 5 6 7 9 10 11 12
#>   ..$ userID           : chr [1:10] "User1" "User3" "User1" "User2" ...
#>   ..$ role             : chr [1:10] "Player" "Mentor" "Player" "Player" ...
#>   ..$ modality         : chr [1:10] "chat" "chat" "resource" "chat" ...
#>   ..$ timeStamp        : int [1:10] 1379410965 1379411300 1379411494 1379411667 1379411813 1379500979 1379501265 1379501471 1379501622 1379501815
#>   ..$ chatGroup        : chr [1:10] "PAM" "PAM" "PAM" "PAM" ...
#>   ..$ condition        : chr [1:10] "FirstHalf" "FirstHalf" "FirstHalf" "FirstHalf" ...
#>   ..$ language         : chr [1:10] "L1" "L1" "L1" "L2" ...
#>   ..$ handwriting      : chr [1:10] "poor" "poor" "poor" "good" ...
#>   ..$ A                : int [1:10] 1 1 0 0 1 1 1 0 0 1
#>   ..$ B                : int [1:10] 0 0 0 1 0 0 0 0 1 0
#>   ..$ C                : int [1:10] 0 0 1 0 1 0 0 1 0 1
#>   ..$ __CONTEXT_TBL__  : num [1:10] 1 1 1 1 1 1 1 1 1 1
#>   ..$ QEID             : int [1:10] 1 3 4 5 6 7 9 10 11 12
#>   ..$ QEUNIT           : chr [1:10] "User1::FirstHalf" "User3::FirstHalf" "User1::FirstHalf" "User2::FirstHalf" ...
#>   ..$ __CONTEXT_ROWID__: int [1:10] 1 2 3 4 5 6 7 8 9 10
#>   ..- attr(*, ".internal.selfref")=<externalptr> 
#>   ..- attr(*, "sorted")= chr "QEID"
#>   ..- attr(*, "tma.unit")=Classes ‘data.table’ and 'data.frame':	1 obs. of  2 variables:
#>   .. ..$ userID   : 'ena.metadata' chr "User1"
#>   .. ..$ condition: 'ena.metadata' chr "FirstHalf"
#>   .. ..- attr(*, ".internal.selfref")=<externalptr> 
#>   ..- attr(*, "tma.unit_rows")= int [1:4] 1 3 6 8
#>  $ User2::FirstHalf:Classes ‘data.table’ and 'data.frame':	9 obs. of  16 variables:
#>   ..$ V1               : int [1:9] 1 2 3 5 6 8 9 11 12
#>   ..$ userID           : chr [1:9] "User1" "User2" "User3" "User2" ...
#>   ..$ role             : chr [1:9] "Player" "Player" "Mentor" "Player" ...
#>   ..$ modality         : chr [1:9] "chat" "resource" "chat" "chat" ...
#>   ..$ timeStamp        : int [1:9] 1379410965 1379411071 1379411300 1379411667 1379411813 1379501110 1379501265 1379501622 1379501815
#>   ..$ chatGroup        : chr [1:9] "PAM" "PAM" "PAM" "PAM" ...
#>   ..$ condition        : chr [1:9] "FirstHalf" "FirstHalf" "FirstHalf" "FirstHalf" ...
#>   ..$ language         : chr [1:9] "L1" "L2" "L1" "L2" ...
#>   ..$ handwriting      : chr [1:9] "poor" "good" "poor" "good" ...
#>   ..$ A                : int [1:9] 1 0 1 0 1 0 1 0 1
#>   ..$ B                : int [1:9] 0 1 0 1 0 1 0 1 0
#>   ..$ C                : int [1:9] 0 1 0 0 1 1 0 0 1
#>   ..$ __CONTEXT_TBL__  : num [1:9] 1 1 1 1 1 1 1 1 1
#>   ..$ QEID             : int [1:9] 1 2 3 5 6 8 9 11 12
#>   ..$ QEUNIT           : chr [1:9] "User1::FirstHalf" "User2::FirstHalf" "User3::FirstHalf" "User2::FirstHalf" ...
#>   ..$ __CONTEXT_ROWID__: int [1:9] 1 2 3 4 5 6 7 8 9
#>   ..- attr(*, ".internal.selfref")=<externalptr> 
#>   ..- attr(*, "sorted")= chr "QEID"
#>   ..- attr(*, "tma.unit")=Classes ‘data.table’ and 'data.frame':	1 obs. of  2 variables:
#>   .. ..$ userID   : 'ena.metadata' chr "User2"
#>   .. ..$ condition: 'ena.metadata' chr "FirstHalf"
#>   .. ..- attr(*, ".internal.selfref")=<externalptr> 
#>   ..- attr(*, "tma.unit_rows")= int [1:4] 2 4 6 8
#>  $ User3::FirstHalf:Classes ‘data.table’ and 'data.frame':	7 obs. of  16 variables:
#>   ..$ V1               : int [1:7] 1 3 5 6 9 11 12
#>   ..$ userID           : chr [1:7] "User1" "User3" "User2" "User3" ...
#>   ..$ role             : chr [1:7] "Player" "Mentor" "Player" "Mentor" ...
#>   ..$ modality         : chr [1:7] "chat" "chat" "chat" "chat" ...
#>   ..$ timeStamp        : int [1:7] 1379410965 1379411300 1379411667 1379411813 1379501265 1379501622 1379501815
#>   ..$ chatGroup        : chr [1:7] "PAM" "PAM" "PAM" "PAM" ...
#>   ..$ condition        : chr [1:7] "FirstHalf" "FirstHalf" "FirstHalf" "FirstHalf" ...
#>   ..$ language         : chr [1:7] "L1" "L1" "L2" "L1" ...
#>   ..$ handwriting      : chr [1:7] "poor" "poor" "good" "poor" ...
#>   ..$ A                : int [1:7] 1 1 0 1 1 0 1
#>   ..$ B                : int [1:7] 0 0 1 0 0 1 0
#>   ..$ C                : int [1:7] 0 0 0 1 0 0 1
#>   ..$ __CONTEXT_TBL__  : num [1:7] 1 1 1 1 1 1 1
#>   ..$ QEID             : int [1:7] 1 3 5 6 9 11 12
#>   ..$ QEUNIT           : chr [1:7] "User1::FirstHalf" "User3::FirstHalf" "User2::FirstHalf" "User3::FirstHalf" ...
#>   ..$ __CONTEXT_ROWID__: int [1:7] 1 2 3 4 5 6 7
#>   ..- attr(*, ".internal.selfref")=<externalptr> 
#>   ..- attr(*, "sorted")= chr "QEID"
#>   ..- attr(*, "tma.unit")=Classes ‘data.table’ and 'data.frame':	1 obs. of  2 variables:
#>   .. ..$ userID   : 'ena.metadata' chr "User3"
#>   .. ..$ condition: 'ena.metadata' chr "FirstHalf"
#>   .. ..- attr(*, ".internal.selfref")=<externalptr> 
#>   ..- attr(*, "tma.unit_rows")= int [1:4] 2 4 5 7