Skip to content

Commit

Permalink
Merge branch 'main' into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
auzaheta committed Sep 2, 2024
2 parents 38c5a21 + b83a7ef commit 7009ca7
Show file tree
Hide file tree
Showing 11 changed files with 180 additions and 172 deletions.
50 changes: 25 additions & 25 deletions vignettes/dynami-example.R
Original file line number Diff line number Diff line change
@@ -1,67 +1,67 @@
## ----setup, include=FALSE-----------------------------------------------------------------------------------
## ----setup, include=FALSE-----------------------------------------------------
knitr::opts_chunk$set(echo = TRUE)


## ----load---------------------------------------------------------------------------------------------------
## ----load---------------------------------------------------------------------
library(goldfish)
data("RFID_Validity_Study")
#?RFID_Validity_Study


## ----headParticipants---------------------------------------------------------------------------------------
## ----headParticipants---------------------------------------------------------
head(participants)


## ----headRfid-----------------------------------------------------------------------------------------------
## ----headRfid-----------------------------------------------------------------
head(rfid)


## ----headVideo----------------------------------------------------------------------------------------------
## ----headVideo----------------------------------------------------------------
head(video)


## ----defGroups----------------------------------------------------------------------------------------------
## ----defGroups----------------------------------------------------------------
#?defineGroups_interaction
prepdata <- defineGroups_interaction(video, participants,
seed.randomization = 1)


## ----assGroups----------------------------------------------------------------------------------------------
## ----assGroups----------------------------------------------------------------
groups <- prepdata$groups
head(groups)


## ----headDependent------------------------------------------------------------------------------------------
## ----headDependent------------------------------------------------------------
dependent.events <- prepdata$dependent.events
head(dependent.events)


## ----headExogenous------------------------------------------------------------------------------------------
## ----headExogenous------------------------------------------------------------
exogenous.events <- prepdata$exogenous.events
head(exogenous.events)


## ----headInteraction----------------------------------------------------------------------------------------
## ----headInteraction----------------------------------------------------------
interaction.updates <- prepdata$interaction.updates
head(interaction.updates)


## ----headOpportunities--------------------------------------------------------------------------------------
## ----headOpportunities--------------------------------------------------------
opportunities <- prepdata$opportunities
head(opportunities)


## ----defNodes-----------------------------------------------------------------------------------------------
## ----defNodes-----------------------------------------------------------------
# goldfish requires character names
participants$label <- as.character(participants$label)
actors <- defineNodes(participants)


## ----groups-------------------------------------------------------------------------------------------------
## ----groups-------------------------------------------------------------------
groups <- defineNodes(groups)


## ----defNet-------------------------------------------------------------------------------------------------
## ----defNet-------------------------------------------------------------------
init.network <- diag(x = 1, nrow(actors), nrow(groups))
# goldfish check that row/column names agree with the nodes data frame labels
dimnames(init.network) <- list(actors$label, groups$label)
Expand All @@ -78,21 +78,21 @@ network.interactions <- linkEvents(
)


## ----defNetPast, warning=FALSE------------------------------------------------------------------------------
## ----defNetPast, warning=FALSE------------------------------------------------
network.past <- defineNetwork(nodes = actors, directed = FALSE)
network.past <- linkEvents(
x = network.past, changeEvents = interaction.updates, nodes = actors
) # don't worry about the warnings


## ----defEvents----------------------------------------------------------------------------------------------
## ----defEvents----------------------------------------------------------------
dependent.events <- defineDependentEvents(
events = dependent.events, nodes = actors,
nodes2 = groups, defaultNetwork = network.interactions
)


## ----modeRateM1---------------------------------------------------------------------------------------------
## ----modeRateM1---------------------------------------------------------------
formula.rate.M1 <- dependent.events ~ 1 +
intercept(network.interactions, joining = 1) +
ego(actors$age, joining = 1, subType = "centered") +
Expand All @@ -104,7 +104,7 @@ formula.rate.M1 <- dependent.events ~ 1 +
tie(known.before, joining = -1, subType = "proportion")


## ----modeChoiceM1-------------------------------------------------------------------------------------------
## ----modeChoiceM1-------------------------------------------------------------
formula.choice.M1 <- dependent.events ~
diff(actors$age, subType = "averaged_sum") +
diff(actors$level, subType = "averaged_sum") +
Expand All @@ -113,12 +113,12 @@ formula.choice.M1 <- dependent.events ~
tie(known.before, subType = "proportion")


## ----modRateM1Est-------------------------------------------------------------------------------------------
## ----modRateM1Est-------------------------------------------------------------
est.rate.M1 <- estimate(formula.rate.M1, model = "DyNAMi", subModel = "rate")
summary(est.rate.M1)


## ----modChoiceM1Est-----------------------------------------------------------------------------------------
## ----modChoiceM1Est-----------------------------------------------------------
est.choice.M1 <- estimate(
formula.choice.M1,
model = "DyNAMi", subModel = "choice",
Expand All @@ -127,7 +127,7 @@ est.choice.M1 <- estimate(
summary(est.choice.M1)


## ----modeRateM2---------------------------------------------------------------------------------------------
## ----modeRateM2---------------------------------------------------------------
formula.rate.M2 <- dependent.events ~ 1 +
intercept(network.interactions, joining = 1) +
ego(actors$age, joining = 1, subType = "centered") +
Expand All @@ -142,7 +142,7 @@ formula.rate.M2 <- dependent.events ~ 1 +
egopop(network.past, joining = -1, subType = "normalized")


## ----modeChoiceM2-------------------------------------------------------------------------------------------
## ----modeChoiceM2-------------------------------------------------------------
formula.choice.M2 <- dependent.events ~
diff(actors$age, subType = "averaged_sum") +
diff(actors$level, subType = "averaged_sum") +
Expand All @@ -156,12 +156,12 @@ formula.choice.M2 <- dependent.events ~
inertia(network.past, window = 300, subType = "mean")


## ----modRateM2Est-------------------------------------------------------------------------------------------
## ----modRateM2Est-------------------------------------------------------------
est.rate.M2 <- estimate(formula.rate.M2, model = "DyNAMi", subModel = "rate")
summary(est.rate.M2)


## ----modChoiceM2Est-----------------------------------------------------------------------------------------
## ----modChoiceM2Est-----------------------------------------------------------
est.choice.M2 <- estimate(
formula.choice.M2,
model = "DyNAMi", subModel = "choice",
Expand All @@ -170,7 +170,7 @@ est.choice.M2 <- estimate(
summary(est.choice.M2)


## ----interceptJoining---------------------------------------------------------------------------------------
## ----interceptJoining---------------------------------------------------------
cov.matrix <- vcov(est.rate.M2)

est.interceptjoining <- coef(est.rate.M2)[1] + coef(est.rate.M2)[2]
Expand Down
48 changes: 24 additions & 24 deletions vignettes/dynami-example.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ You can find out more about this dataset and its format by callings
its documentation.


```r
``` r
library(goldfish)
data("RFID_Validity_Study")
#?RFID_Validity_Study
Expand All @@ -43,7 +43,7 @@ The `participants` object contains the nodeset of actors interacting together.
The available attributes are their age, their gender, their organizational
unit (group), and their seniority level:

```r
``` r
head(participants)
```

Expand All @@ -62,7 +62,7 @@ RFID badges.
Each interaction is characterized by two actors (`NodeA` and `NodeB`) and
two time points (`Start` and `End`):

```r
``` r
head(rfid)
```

Expand All @@ -81,7 +81,7 @@ via video recordings and has the same format as the `rfid` object.
We know that these measures should be more reliable, so we will work with
those data in this script!

```r
``` r
head(video)
```

Expand Down Expand Up @@ -109,7 +109,7 @@ leavin interaction groups. We can use this function to create these group
events (please note we need to use the right labels in the dataframe:
`NodeA`, `NodeB`,`Start`, and `End`):

```r
``` r
#?defineGroups_interaction
prepdata <- defineGroups_interaction(video, participants,
seed.randomization = 1)
Expand All @@ -120,7 +120,7 @@ This functions to creates 5 objects:
(initially there are as many groups as actors and they are all present,
meaning available)

```r
``` r
groups <- prepdata$groups
head(groups)
```
Expand All @@ -140,7 +140,7 @@ to model, when an actor (in the dataframe, the `sender` column) joins or
leaves (`increment` = 1 or -1) a group (`receiver`) at a particular point
in time (`time`)

```r
``` r
dependent.events <- prepdata$dependent.events
head(dependent.events)
```
Expand All @@ -160,7 +160,7 @@ happen but are not modeled (for example, when an actor leaves a group, a
dependent event is created for this leaving but an exogenous event is also
created because the actor "joins" a new group, its own isolated group)

```r
``` r
exogenous.events <- prepdata$exogenous.events
head(exogenous.events)
```
Expand All @@ -178,7 +178,7 @@ head(exogenous.events)
4. `interaction.updates`: goldfish events that are used to update the number
of past interactions between participants:

```r
``` r
interaction.updates <- prepdata$interaction.updates
head(interaction.updates)
```
Expand All @@ -196,7 +196,7 @@ head(interaction.updates)
5. `opportunities`: list containing the interaction groups available at each
decision time (this will vary when groups are being joined or left)

```r
``` r
opportunities <- prepdata$opportunities
head(opportunities)
```
Expand Down Expand Up @@ -228,23 +228,23 @@ objects in a way that goldfish understands what is going on.

First, we define the first mode nodeset, the actors:

```r
``` r
# goldfish requires character names
participants$label <- as.character(participants$label)
actors <- defineNodes(participants)
```

Then we define the second mode nodeset, the groups:

```r
``` r
groups <- defineNodes(groups)
```

Then we create the dynamic interaction network between the actors and
the groups, updated by the previously created events in `dependent.events`
and `exogenous.events`

```r
``` r
init.network <- diag(x = 1, nrow(actors), nrow(groups))
# goldfish check that row/column names agree with the nodes data frame labels
dimnames(init.network) <- list(actors$label, groups$label)
Expand All @@ -265,15 +265,15 @@ Then we create the dynamic network between the actors that records
past interactions, updated by the previously created events in
`interaction.updates`

```r
``` r
network.past <- defineNetwork(nodes = actors, directed = FALSE)
network.past <- linkEvents(
x = network.past, changeEvents = interaction.updates, nodes = actors
) # don't worry about the warnings
```
Finally, we define the events that we want to model: `dependent.events`:

```r
``` r
dependent.events <- defineDependentEvents(
events = dependent.events, nodes = actors,
nodes2 = groups, defaultNetwork = network.interactions
Expand Down Expand Up @@ -305,7 +305,7 @@ For the rate, we have the following effects:
if the proportion of previous friends in the group is high


```r
``` r
formula.rate.M1 <- dependent.events ~ 1 +
intercept(network.interactions, joining = 1) +
ego(actors$age, joining = 1, subType = "centered") +
Expand All @@ -331,7 +331,7 @@ and for the choice model:
if the proportion of previous friends in the group is high


```r
``` r
formula.choice.M1 <- dependent.events ~
diff(actors$age, subType = "averaged_sum") +
diff(actors$level, subType = "averaged_sum") +
Expand All @@ -346,7 +346,7 @@ the second explains who people want to leave (or stay with).

Now let us run goldfish estimation!

```r
``` r
est.rate.M1 <- estimate(formula.rate.M1, model = "DyNAMi", subModel = "rate")
summary(est.rate.M1)
```
Expand Down Expand Up @@ -400,7 +400,7 @@ summary(est.rate.M1)
```


```r
``` r
est.choice.M1 <- estimate(
formula.choice.M1,
model = "DyNAMi", subModel = "choice",
Expand Down Expand Up @@ -456,7 +456,7 @@ We add to the rate model the effects of:
who had more interactions in the past to leave groups faster


```r
``` r
formula.rate.M2 <- dependent.events ~ 1 +
intercept(network.interactions, joining = 1) +
ego(actors$age, joining = 1, subType = "centered") +
Expand All @@ -482,7 +482,7 @@ We add to the choice model:
3. inertia, window = 300s: same as above, for 5 minutes


```r
``` r
formula.choice.M2 <- dependent.events ~
diff(actors$age, subType = "averaged_sum") +
diff(actors$level, subType = "averaged_sum") +
Expand All @@ -506,7 +506,7 @@ please look at the goldfish documentation to learn more.

Now we can run again

```r
``` r
est.rate.M2 <- estimate(formula.rate.M2, model = "DyNAMi", subModel = "rate")
summary(est.rate.M2)
```
Expand Down Expand Up @@ -569,7 +569,7 @@ summary(est.rate.M2)
```
and:

```r
``` r
est.choice.M2 <- estimate(
formula.choice.M2,
model = "DyNAMi", subModel = "choice",
Expand Down Expand Up @@ -638,7 +638,7 @@ a dummy interaction between the intercept and the joining events.
If we want to report the actual intercept for joining,
we can calculate the following (for M2 for example):

```r
``` r
cov.matrix <- vcov(est.rate.M2)

est.interceptjoining <- coef(est.rate.M2)[1] + coef(est.rate.M2)[2]
Expand Down
Binary file modified vignettes/teaching/plot-examine-1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified vignettes/teaching/plot-teaching1-1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified vignettes/teaching/plot-teaching1-2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified vignettes/teaching/plot-teaching1-3.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified vignettes/teaching/plot-teaching2-1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

0 comments on commit 7009ca7

Please sign in to comment.