Skip to contents
library(actsims)
#> Loading required package: actdata

Introduction

These functions power the “Analyze Events” pane in interactShiny.

The idea is that each agent in a two-person interaction is an independent InteRactModel.

situation <- define_situation(
  agent1 = interact(), ## the idea is that each agent can be set up with
  agent2 = interact()  ## different dictionaries and equations.
)
#>  dictionary = list(dataset = "usfullsurveyor2015", group = "all")
#>  equations = list(key = "us2010", group = "all")
#>  dictionary = list(dataset = "usfullsurveyor2015", group = "all")
#>  equations = list(key = "us2010", group = "all")

The newly created situation object contains various fields and functions.

Most of these will be empty until the $start() function is used.

For example:

situation$active  ## one of the two agents will be active at any given time
#> [1] "agent1"
situation$time
#> NULL
situation$history
#> NULL

And here is how they look after $start():

situation$start(list(A = "boyfriend", B = "hurt", O = "firefighter"))
opt_beh <- situation$optimal_behavior(who = "agent2")
opt_beh
#> # A tibble: 1 × 3
#>      Be    Bp     Ba
#>   <dbl> <dbl>  <dbl>
#> 1  1.40  4.30 0.0535
situation$agent2$closest_terms(opt_beh, component = "behavior")[1:5]
#> [1] NA NA NA NA NA

situation$activate("agent2")
situation$new(list(A = "firefighter", B = "babble_to", O = "boyfriend"))

situation$history
#> $deflection
#> # A tibble: 2 × 7
#>    time agent1 agent2 A           B         O           deflection
#>   <int> <chr>  <chr>  <chr>       <chr>     <chr>            <dbl>
#> 1     0 A      O      boyfriend   hurt      firefighter       98.7
#> 2     1 O      A      firefighter babble_to boyfriend         72.4
#> 
#> $fundamentals
#> # A tibble: 2 × 15
#>    time agent1 agent2 A          B     O        Ae    Ap    Aa    Be    Bp    Ba
#>   <int> <chr>  <chr>  <chr>      <chr> <chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
#> 1     0 A      O      boyfriend  hurt  fire…  1.86  1.68  0.89 -3.17  1.06  0.92
#> 2     1 O      A      firefight… babb… boyf…  3.27  2.85  2.29 -0.89 -0.66  1.88
#> # ℹ 3 more variables: Oe <dbl>, Op <dbl>, Oa <dbl>
#> 
#> $transients
#> # A tibble: 2 × 15
#>    time agent1 agent2 A       B     O         Ae    Ap    Aa     Be     Bp    Ba
#>   <int> <chr>  <chr>  <chr>   <chr> <chr>  <dbl> <dbl> <dbl>  <dbl>  <dbl> <dbl>
#> 1     0 A      O      boyfri… hurt  fire… -3.58   1.90 1.67  -5.13   2.32   1.77
#> 2     1 O      A      firefi… babb… boyf… -0.423 -1.25 0.794 -0.652 -0.755  1.69
#> # ℹ 3 more variables: Oe <dbl>, Op <dbl>, Oa <dbl>
#> 
#> $element_wise_deflection
#> # A tibble: 2 × 15
#>    time agent1 agent2 A           B     O        Ae      Ap    Aa     Be      Bp
#>   <int> <chr>  <chr>  <chr>       <chr> <chr> <dbl>   <dbl> <dbl>  <dbl>   <dbl>
#> 1     0 A      O      boyfriend   hurt  fire…  29.6  0.0502 0.609 3.84   1.60   
#> 2     1 O      A      firefighter babb… boyf…  13.6 16.8    2.24  0.0566 0.00894
#> # ℹ 4 more variables: Ba <dbl>, Oe <dbl>, Op <dbl>, Oa <dbl>

This situation allows for “method chaining” using the $ operator. This type of code is not very R-like.

situation$
  activate("agent1")$
  start(list(A = "employer", B = "hurt", O = "employee"))$ 
  activate("agent2")$
  new(list(A = "employee", B = "pull_away_from", O = "employer"))$
  activate("agent1")$
  new(list(A = "employer", B = "confront", O = "employee"))$
  history
#> $deflection
#> # A tibble: 3 × 7
#>    time agent1 agent2 A        B              O        deflection
#>   <int> <chr>  <chr>  <chr>    <chr>          <chr>         <dbl>
#> 1     0 A      O      employer hurt           employee       43.5
#> 2     1 O      A      employee pull_away_from employer       19.2
#> 3     2 A      O      employer confront       employee       13.3
#> 
#> $fundamentals
#> # A tibble: 3 × 15
#>    time agent1 agent2 A        B       O        Ae    Ap    Aa    Be    Bp    Ba
#>   <int> <chr>  <chr>  <chr>    <chr>   <chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
#> 1     0 A      O      employer hurt    empl…  1.57  2.56   0.7 -3.17  1.06  0.92
#> 2     1 O      A      employee pull_a… empl…  1.04 -0.37   0.2 -0.81  0.93 -0.6 
#> 3     2 A      O      employer confro… empl…  1.57  2.56   0.7  0.39  1.99  1.28
#> # ℹ 3 more variables: Oe <dbl>, Op <dbl>, Oa <dbl>
#> 
#> $transients
#> # A tibble: 3 × 15
#>    time agent1 agent2 A      B     O         Ae    Ap    Aa     Be     Bp     Ba
#>   <int> <chr>  <chr>  <chr>  <chr> <chr>  <dbl> <dbl> <dbl>  <dbl>  <dbl>  <dbl>
#> 1     0 A      O      emplo… hurt  empl… -1.79   3.05  1.62 -2.38   2.79   1.55 
#> 2     1 O      A      emplo… pull… empl… -0.486 -1.26 -1.33 -0.472 -0.449 -0.680
#> 3     2 A      O      emplo… conf… empl… -0.662  1.83  1.35 -0.607  1.76   1.28 
#> # ℹ 3 more variables: Oe <dbl>, Op <dbl>, Oa <dbl>
#> 
#> $element_wise_deflection
#> # A tibble: 3 × 15
#>    time agent1 agent2 A       B     O        Ae    Ap    Aa    Be     Bp      Ba
#>   <int> <chr>  <chr>  <chr>   <chr> <chr> <dbl> <dbl> <dbl> <dbl>  <dbl>   <dbl>
#> 1     0 A      O      employ… hurt  empl… 11.3  0.240 0.848 0.622 3.00   3.98e-1
#> 2     1 O      A      employ… pull… empl…  2.33 0.795 2.33  0.114 1.90   6.43e-3
#> 3     2 A      O      employ… conf… empl…  4.98 0.531 0.424 0.995 0.0511 1.94e-5
#> # ℹ 3 more variables: Oe <dbl>, Op <dbl>, Oa <dbl>
  
situation$history$deflection
#> # A tibble: 3 × 7
#>    time agent1 agent2 A        B              O        deflection
#>   <int> <chr>  <chr>  <chr>    <chr>          <chr>         <dbl>
#> 1     0 A      O      employer hurt           employee       43.5
#> 2     1 O      A      employee pull_away_from employer       19.2
#> 3     2 A      O      employer confront       employee       13.3

Instead, you can use R’s pipe operator in tandem with the sttn_* functions.

df <- situation |> 
  sttn_activate("agent1") |> 
  sttn_start(list(A = "employer", B = "hurt", O = "employee")) |> 
  sttn_activate("agent2") |> 
  sttn_new(list(A = "employee", B = "pull_away_from", O = "employer")) |> 
  sttn_activate("agent1") |> 
  sttn_new(list(A = "employer", B = "confront", O = "employee")) |> 
  sttn_extract()
  
df$deflection
#> # A tibble: 3 × 7
#>    time agent1 agent2 A        B              O        deflection
#>   <int> <chr>  <chr>  <chr>    <chr>          <chr>         <dbl>
#> 1     0 A      O      employer hurt           employee       43.5
#> 2     1 O      A      employee pull_away_from employer       19.2
#> 3     2 A      O      employer confront       employee       13.3
df$transients
#> # A tibble: 3 × 15
#>    time agent1 agent2 A      B     O         Ae    Ap    Aa     Be     Bp     Ba
#>   <int> <chr>  <chr>  <chr>  <chr> <chr>  <dbl> <dbl> <dbl>  <dbl>  <dbl>  <dbl>
#> 1     0 A      O      emplo… hurt  empl… -1.79   3.05  1.62 -2.38   2.79   1.55 
#> 2     1 O      A      emplo… pull… empl… -0.486 -1.26 -1.33 -0.472 -0.449 -0.680
#> 3     2 A      O      emplo… conf… empl… -0.662  1.83  1.35 -0.607  1.76   1.28 
#> # ℹ 3 more variables: Oe <dbl>, Op <dbl>, Oa <dbl>