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

## ----setup--------------------------------------------------------------------
Sys.setenv(OMP_THREAD_LIMIT = 1) # Reducing core use, to avoid accidental use of too many cores
library(Colossus)
library(data.table)

## ----eval=FALSE---------------------------------------------------------------
# Strat_Col <- "s0"
# model <- Cox_Strata(time1, time2, event, s0) ~ loglinear(dose)
# e <- CoxRun(model, df,
#   a_n = a_n, control = control
# )
# Strat_Cols <- c("s0", "s1", "s2")
# model <- Cox_Strata(time1, time2, event, c(s0, s1, s2)) ~ loglinear(dose)
# e <- CoxRun(model, df,
#   a_n = a_n, control = control
# )

## ----eval=FALSE---------------------------------------------------------------
# Strat_Col <- c("s0")
# e <- PoisRun(Poisson_Strata(pyr, event, s0) ~ loglinear(dose), df,
#   a_n = a_n, control = control
# )
# 
# Strat_Col <- c("s0", "s1", "s2")
# e <- PoisRun(Poisson_Strata(pyr, event, s0, s1, s2) ~ loglinear(dose), df,
#   a_n = a_n, control = control
# )

## ----eval=FALSE---------------------------------------------------------------
# e <- CoxRun(Cox(time1, time2, event) ~ loglinear(dose), df,
#   a_n = a_n, control = control, single = TRUE
# )

## ----eval=FALSE---------------------------------------------------------------
# pdata <- finegray(Surv(time2, event) ~ ., data = df)
# 
# e <- CoxRun(FineGray(fgstart, fgstop, fgstatus, fgwt) ~ loglinear(dose), pdata,
#   a_n = a_n, control = control
# )

## ----eval=TRUE----------------------------------------------------------------
a <- c(0, 0, 0, 1, 1, 1)
b <- c(1, 1, 1, 2, 2, 2)
c <- c(0, 1, 2, 2, 1, 0)
d <- c(1, 1, 0, 0, 1, 1)
e <- c(0, 1, 1, 1, 0, 0)
df <- data.table(t0 = a, t1 = b, e0 = c, e1 = d, fac = e)
time1 <- "t0"
time2 <- "t1"
df$pyr <- df$t1 - df$t0
pyr <- "pyr"
events <- c("e0", "e1")

## ----eval=TRUE----------------------------------------------------------------
model_1 <- Pois(pyr, e0) ~ loglin(fac, 0)
model_2 <- Pois(pyr, e1) ~ loglin(fac, 0)
model_s <- Pois(pyr) ~ plinear(t0, 0)
formula_list <- list(model_1, model_2, "shared" = model_s)

## ----eval=TRUE----------------------------------------------------------------
res <- get_form_joint(formula_list, df, nthreads = 1)
model <- res$model
df_combined <- res$data
df_combined

## ----eval=TRUE----------------------------------------------------------------
control <- list(
  ncores = 1, maxiter = 10, halfmax = 5, verbose = 2
)
e <- PoisRunJoint(formula_list, df, control = control)
print(e)

## ----eval=TRUE----------------------------------------------------------------
a <- c(0, 0, 0, 1, 1, 1)
b <- c(1, 1, 1, 2, 2, 2)
c <- c(0, 0, 1, 0, 1, 0)
d <- c(1, 1, 0, 0, 1, 1)
e <- c(0, 1, 1, 1, 0, 0)
df <- data.table(t0 = a, t1 = b, e = c, fac0 = d, fac1 = e)

model <- Cox(t0, t1, e) ~ loglinear(fac0, fac1)

# suppose we want the factors to be a value of 0.5 apart
# 1*fac0 + -1*fac1 = 0.5
cons_mat <- c(1, -1)
cons_vec <- c(0.5)

e <- CoxRun(model, df, cons_mat = cons_mat, cons_vec = cons_vec)
print(e)

# instead we could make the values opposite
# 1*fac0 + 1*fac1 = 0.0
cons_mat <- c(1, 1)
cons_vec <- c(0.0)

e <- CoxRun(model, df, cons_mat = cons_mat, cons_vec = cons_vec)
print(e)

## ----eval=TRUE----------------------------------------------------------------
a <- c(0, 0, 0, 1, 1, 1)
b <- c(1, 1, 1, 2, 2, 2)
c <- c(0, 0, 1, 0, 1, 0)
d <- c(1, 1, 0, 0, 1, 1)
e <- c(0, 1, 1, 1, 0, 0)
df <- data.table(t0 = a, t1 = b, e = c, fac0 = d, fac1 = e)

model <- Cox(t0, t1, e) ~ loglinear(fac0, fac1)

# suppose we have several parameter sets we think could be the best
# we can put each parameter set into a list
a_ns <- list(c(0.1, 0.1), c(-0.1, 0.1), c(-0.1, -0.1), c(0.1, -0.1))

# The default
maxiters <- c(1, 1, 1, 20)
# Running each guess for 5 iterations
maxiters <- c(5, 5, 5, 20)
maxiters <- c(5, 20)

e <- CoxRun(model, df, a_n = a_ns, maxiters = maxiters)
print(e)

# We can also print out the intermediate results
print(e$Guess_Results)

