## ----setup, include=FALSE-----------------------------------------------------
knitr::opts_chunk$set(dpi = 72, collapse = TRUE, comment = "#>")

## ----load-data----------------------------------------------------------------
library(remverse)   # loads remify, remstats, remstimate; remdata is a dependency

data(randomREH3)
data(info3)         # actor attributes (age, sex)
head(randomREH3)

## ----data-summary-------------------------------------------------------------
cat(sprintf(
    "%d events: %d complete, %d right-censored\n",
    nrow(randomREH3),
    sum(!is.na(randomREH3$end)),
    sum( is.na(randomREH3$end))
))

## ----remify-------------------------------------------------------------------
reh <- remify(
    edgelist  = randomREH3,
    duration  = TRUE,
    model     = "tie",
    directed  = TRUE        # actor1 initiates; the end process is undirected by default
)

## ----print-reh----------------------------------------------------------------
reh

## ----summary-reh--------------------------------------------------------------
summary(reh)

## ----plot-reh, out.width="70%", dev=c("jpeg"), dev.args = list(bg = "white")----
plot(reh)

## ----durem-slot---------------------------------------------------------------
reh$durem

## ----edgelist-dual------------------------------------------------------------
head(reh$edgelist_dual, 12)

## ----remstats, message=FALSE--------------------------------------------------
# Start model (directed): history-weighted effects + active out-degree of sender
start_fx <- ~ inertia(scaling = "std") +
              reciprocity(scaling = "std") +
              activeOutdegreeSender(scaling = "std")

# End model (undirected): history-weighted effects
end_fx   <- ~ totaldegreeDyad(scaling = "std") +
              difference("age", attr_actors = info3, scaling = "std")

dstats <- remstats(
    reh              = reh,
    start_effects    = start_fx,
    end_effects      = end_fx,
    memory           = "decay",
    memory_value     = 2000,
    display_progress = FALSE
)

## ----print-dstats-------------------------------------------------------------
dstats

## ----dstats-dims--------------------------------------------------------------
head(dstats$stacked$remstats_stack)

## ----consider-type-demo, eval=FALSE-------------------------------------------
# # Rename `setting` to `type` and build a typed duration history
# history_typed <- randomREH3
# names(history_typed)[names(history_typed) == "setting"] <- "type"
# reh_typed <- remify(history_typed, duration = TRUE, model = "tie", directed = TRUE)
# 
# # "separate": one activeTie statistic per setting
# dstats_sep <- remstats(
#     reh_typed,
#     start_effects = ~ activeTie(consider_type = "separate")
# )
# dimnames(dstats_sep$start_stats)[[3]]

## ----remstimate---------------------------------------------------------------
fit <- remstimate(reh = reh, stats = dstats)

## ----print-fit----------------------------------------------------------------
fit

## ----coef---------------------------------------------------------------------
coef(fit)

## ----summary-fit--------------------------------------------------------------
summary(fit)

## ----durem-diag, out.width="60%", dev=c("jpeg"), dev.args = list(bg = "white")----
diag <- diagnostics(fit, reh, dstats)
plot(diag)

## ----model-comparison, message=FALSE------------------------------------------
# A simpler model: baseline dynamics only
dstats_simple <- remstats(
    reh,
    start_effects = ~ inertia(scaling = "std"),
    end_effects   = ~ totaldegreeDyad(scaling = "std"),
    memory        = "decay",
    memory_value  = 2000
)
fit_simple <- remstimate(reh = reh, stats = dstats_simple)

logLik(fit)
logLik(fit_simple)

AIC(fit)
AIC(fit_simple)

## ----censored-----------------------------------------------------------------
reh$durem$n_censored
reh$durem$n_complete

## ----simultaneous-------------------------------------------------------------
# Time points in the dual edgelist shared by more than one row
tab <- table(reh$edgelist_dual$time)
head(tab[tab > 1], 6)

## ----directed-end, eval=FALSE-------------------------------------------------
# reh_de <- remify(randomREH3, duration = TRUE, model = "tie",
#                  directed = TRUE, dur_directed_end = TRUE)
# 
# dstats_de <- remstats(
#     reh_de,
#     start_effects = ~ inertia(scaling = "std") + activeOutdegreeSender(scaling = "std"),
#     end_effects   = ~ activeOutdegreeSender(scaling = "std")
# )
# fit_de <- remstimate(reh = reh_de, stats = dstats_de)
# coef(fit_de)

