## -----------------------------------------------------------------------------
library(trajeR)

data("data_CNORM")

matplot(
  t(data_CNORM[, 12:21]),
  t(data_CNORM[, 2:11]),
  pch = 1,
  type = 'b',
  col = "black",
  lty = 1,
  xlab = "Times",
  ylab = "Values",
  main = "Plot of the individual's trajectories"
)

## ----message = FALSE----------------------------------------------------------
# Likelihood different sigma
solL <- trajeR(
  Y = data_CNORM[, 2:11],
  A = data_CNORM[, 12:21],
  ng = 3,
  degre = c(0, 3, 4),
  Model = "CNORM",
  Method = "L",
  hessian = TRUE,
  ssigma = FALSE,
  control = list(stepmax = 1e-2)
)
solL

## ----message = FALSE----------------------------------------------------------
# Likelihood same sigma
solLs <- trajeR(
  Y = data_CNORM[, 2:11],
  A = data_CNORM[, 12:21],
  ng = 3,
  degre = c(0, 3, 4),
  Model = "CNORM",
  Method = "L",
  ssigma = TRUE,
  hessian = TRUE,
  control = list(stepmax = 1e-2)
)
solLs

## ----message = FALSE----------------------------------------------------------
# EM
solEM <- trajeR(
  Y = data_CNORM[, 2:11],
  A = data_CNORM[, 12:21],
  ng = 3,
  degre = c(0, 3, 4),
  Model = "CNORM",
  Method = "EM",
  ssigma = FALSE,
  hessian = TRUE
)
solEMs <- trajeR(
  Y = data_CNORM[, 2:11],
  A = data_CNORM[, 12:21],
  ng = 3,
  degre = c(0, 3, 4),
  Model = "CNORM",
  Method = "EM",
  ssigma = TRUE,
  hessian = TRUE
)
solEM
solEMs

## ----echo = FALSE-------------------------------------------------------------
t1 <- solL$tab[, 1]
t1[14:16] <- exp(t1[14:16]) / sum(exp(t1[14:16]))
t2 <- solLs$tab[, 1]
t2[14:16] <- exp(t2[14:16]) / sum(exp(t2[14:16]))
tab <- round(cbind(t1, solEM$tab[, 1], t2, solEMs$tab[, 1]), 5)
colnames(tab) <- c("SolL", "SolEM", "SolLs", "SolEMs")

library(kableExtra)
kable(tab, "html", booktabs = T, escape = FALSE, align = "r") %>%
  kable_styling() %>%
  add_header_above(c("Different sigma" = 2, "Same sigma" = 2)) %>%
  pack_rows("Beta 1", 1, 1) %>%
  pack_rows("Beta 2", 2, 5) %>%
  pack_rows("Beta 3", 6, 10) %>%
  pack_rows("Sigma", 11, 13) %>%
  pack_rows("Pi", 14, 16)

## -----------------------------------------------------------------------------
plotrajeR(solL)

## -----------------------------------------------------------------------------
# colour's defintion
trans <- "70"
col1 <- "#034569"
col1.1 <- paste0("#64AAD0", trans)
col2 <- "#750062"
col2.1 <- paste0("#D962C7", trans)
col3 <- "#A68900"
col3.1 <- paste0("#FFE773", trans)
cols1 <- c(col1.1, col2.1, col3.1)
cols2 <- c(col1, col2, col3)
vcol <- c(cols1, cols2)

plotrajeR(solEM, Y = data_CNORM[, 2:11], A = data_CNORM[, 12:21], col = vcol)

## ----message = FALSE----------------------------------------------------------
solLRisk <- trajeR(
  Y = data_CNORM[, 2:11],
  A = data_CNORM[, 12:21],
  Risk = data_CNORM[, 42:43],
  ng = 3,
  degre = c(0, 3, 4),
  Model = "CNORM",
  Method = "L",
  ssigma = FALSE,
  hessian = TRUE,
  control = list(stepmax = 1e-2)
)
solLRisks <- trajeR(
  Y = data_CNORM[, 2:11],
  A = data_CNORM[, 12:21],
  Risk = data_CNORM[, 42:43],
  ng = 3,
  degre = c(0, 3, 4),
  Model = "CNORM",
  Method = "L",
  ssigma = TRUE,
  hessian = TRUE,
  control = list(stepmax = 1e-2)
)
solEMRisk <- trajeR(
  Y = data_CNORM[, 2:11],
  A = data_CNORM[, 12:21],
  Risk = data_CNORM[, 42:43],
  ng = 3,
  degre = c(0, 3, 4),
  Model = "CNORM",
  Method = "EM",
  ssigma = FALSE,
  hessian = TRUE
)
solEMRisks <- trajeR(
  Y = data_CNORM[, 2:11],
  A = data_CNORM[, 12:21],
  Risk = data_CNORM[, 42:43],
  ng = 3,
  degre = c(0, 3, 4),
  Model = "CNORM",
  Method = "EM",
  ssigma = TRUE,
  hessian = TRUE
)

## ----echo = FALSE-------------------------------------------------------------
tab <- cbind(
  solLRisk$tab[, 1],
  solEMRisk$tab[, 1],
  solLRisks$tab[, 1],
  solEMRisks$tab[, 1]
)
for (i in 1:4) {
  tab[14:22, i] <- tab[14:22, i] - tab[14:16, i]
}
tab <- tab[-c(14:16), ]
tab <- round(tab, 5)
colnames(tab) <- c("SolLRisk", "SolEMRisk", "SolLRisks", "SolEMRisks")
kable(tab, "html", booktabs = T, escape = FALSE, align = "r") %>%
  kable_styling() %>%
  add_header_above(c("Different sigma" = 2, "Same sigma" = 2)) %>%
  pack_rows("Beta 1", 1, 1) %>%
  pack_rows("Beta 2", 2, 5) %>%
  pack_rows("Beta 3", 6, 10) %>%
  pack_rows("Sigma", 11, 13) %>%
  pack_rows("Theta - First group 0", 14, 19)

## ----message = FALSE----------------------------------------------------------
solLTCOV2 <- trajeR(
  Y = data_CNORM[, 2:11],
  A = data_CNORM[, 12:21],
  TCOV = data_CNORM[, 22:41],
  ng = 3,
  degre = c(0, 3, 4),
  Model = "CNORM",
  Method = "L",
  ssigma = FALSE,
  hessian = TRUE,
  control = list(stepmax = 1e-2)
)
solLTCOV2

## ----message = FALSE----------------------------------------------------------
solLTCOV2s <- trajeR(
  data_CNORM[, 2:11],
  data_CNORM[, 12:21],
  TCOV = data_CNORM[, 22:41],
  ng = 3,
  degre = c(0, 3, 4),
  Model = "CNORM",
  Method = "L",
  ssigma = TRUE,
  hessian = TRUE,
  control = list(stepmax = 1e-2)
)
solEMTCOV2 <- trajeR(
  data_CNORM[, 2:11],
  data_CNORM[, 12:21],
  TCOV = data_CNORM[, 22:41],
  ng = 3,
  degre = c(0, 3, 4),
  Model = "CNORM",
  Method = "EM",
  ssigma = FALSE,
  hessian = TRUE
)
solEMTCOV2s <- trajeR(
  data_CNORM[, 2:11],
  data_CNORM[, 12:21],
  TCOV = data_CNORM[, 22:41],
  ng = 3,
  degre = c(0, 3, 4),
  Model = "CNORM",
  Method = "EM",
  ssigma = TRUE,
  hessian = TRUE
)

## ----echo = FALSE-------------------------------------------------------------
t1 <- solLTCOV2$tab[, 1]
t1[20:22] <- exp(t1[20:22]) / sum(exp(t1[20:22]))
t2 <- solLTCOV2s$tab[, 1]
t2[20:22] <- exp(t2[20:22]) / sum(exp(t2[20:22]))
tab <- round(cbind(t1, solEMTCOV2$tab[, 1], t2, solEMTCOV2s$tab[, 1]), 5)
colnames(tab) <- c("SolLTCOV2", "SolEMCOV2", "SolLTCOV2s", "SolEMCOV2s")

kable(tab, "html", booktabs = T, escape = FALSE, align = "r") %>%
  kable_styling() %>%
  add_header_above(c("Different sigma" = 2, "Same sigma" = 2)) %>%
  pack_rows("Beta 1", 1, 1) %>%
  pack_rows("Beta 2", 2, 5) %>%
  pack_rows("Beta 3", 6, 10) %>%
  pack_rows("Sigma", 11, 13) %>%
  pack_rows("Delta 1", 14, 15) %>%
  pack_rows("Delta 2", 16, 17) %>%
  pack_rows("Delta 3", 18, 19) %>%
  pack_rows("Pi", 20, 22)

## -----------------------------------------------------------------------------
library(trajeR)

data("data_CNORM_Censored")

matplot(
  t(data_CNORM_Censored[, 12:21]),
  t(data_CNORM_Censored[, 2:11]),
  pch = 1,
  type = 'b',
  col = "black",
  lty = 1,
  xlab = "Times",
  ylab = "Values",
  main = "Plot of the individual's trajectories"
)

## ----message = FALSE----------------------------------------------------------
# Likelihood different sigma
solLC <- trajeR(
  Y = data_CNORM_Censored[, 2:11],
  A = data_CNORM_Censored[, 12:21],
  ng = 3,
  degre = c(0, 3, 4),
  Model = "CNORM",
  Method = "L",
  hessian = TRUE,
  ssigma = FALSE,
  ymin = 2,
  ymax = 23,
  control = list(stepmax = 1e-2)
)
solLC

# colour's defintion
trans <- "70"
col1 <- "#034569"
col1.1 <- paste0("#64AAD0", trans)
col2 <- "#750062"
col2.1 <- paste0("#D962C7", trans)
col3 <- "#A68900"
col3.1 <- paste0("#FFE773", trans)
cols1 <- c(col1.1, col2.1, col3.1)
cols2 <- c(col1, col2, col3)
vcol <- c(cols1, cols2)

plotrajeR(
  solLC,
  Y = data_CNORM_Censored[, 2:11],
  A = data_CNORM_Censored[, 12:21],
  col = vcol
)

## ----echo = FALSE, message = FALSE--------------------------------------------
solLnC <- trajeR(
  Y = data_CNORM_Censored[, 2:11],
  A = data_CNORM_Censored[, 12:21],
  ng = 3,
  degre = c(0, 3, 4),
  Model = "CNORM",
  Method = "L",
  hessian = TRUE,
  ssigma = FALSE,
  control = list(stepmax = 1e-2)
)

## ----echo = FALSE-------------------------------------------------------------
tab <- round(cbind(solLC$tab[, 1], solLnC$tab[, 1]), 5)
tab[14:16, 1] <- exp(tab[14:16, 1]) / sum(exp(tab[14:16, 1]))
tab[14:16, 2] <- exp(tab[14:16, 2]) / sum(exp(tab[14:16, 2]))
colnames(tab) <- c("Censored", "Not Censored")

kable(tab, "html", booktabs = T, escape = FALSE, align = "r") %>%
  kable_styling() %>%
  pack_rows("Beta 1", 1, 1) %>%
  pack_rows("Beta 2", 2, 5) %>%
  pack_rows("Beta 3", 6, 10) %>%
  pack_rows("Sigma", 11, 13) %>%
  pack_rows("Pi", 14, 16)

par(mfrow = c(1, 2))
plotrajeR(
  solLC,
  Y = data_CNORM_Censored[, 2:11],
  A = data_CNORM_Censored[, 12:21],
  col = vcol,
  main = "Censored"
)
plotrajeR(
  solLnC,
  Y = data_CNORM_Censored[, 2:11],
  A = data_CNORM_Censored[, 12:21],
  col = vcol,
  main = "Not Censored"
)

## ----message = FALSE----------------------------------------------------------
solLCs <- trajeR(
  Y = data_CNORM_Censored[, 2:11],
  A = data_CNORM_Censored[, 12:21],
  ng = 3,
  degre = c(0, 3, 4),
  Model = "CNORM",
  Method = "L",
  ssigma = TRUE,
  hessian = TRUE,
  ymin = 2,
  ymax = 23,
  control = list(stepmax = 1e-2)
)
solEMC <- trajeR(
  Y = data_CNORM_Censored[, 2:11],
  A = data_CNORM_Censored[, 12:21],
  ng = 3,
  degre = c(0, 3, 4),
  Model = "CNORM",
  Method = "EM",
  ssigma = FALSE,
  hessian = TRUE,
  ymin = 2,
  ymax = 23,
)
solEMCs <- trajeR(
  Y = data_CNORM_Censored[, 2:11],
  A = data_CNORM_Censored[, 12:21],
  ng = 3,
  degre = c(0, 3, 4),
  Model = "CNORM",
  Method = "EM",
  ssigma = TRUE,
  hessian = TRUE,
  ymin = 2,
  ymax = 23,
)

## ----echo = FALSE-------------------------------------------------------------
tab <- cbind(solLC$tab[, 1], solEMC$tab[, 1], solLCs$tab[, 1], solEMCs$tab[, 1])
tab[14:16, 1] <- exp(tab[14:16, 1]) / sum(exp(tab[14:16, 1]))
tab[14:16, 3] <- exp(tab[14:16, 3]) / sum(exp(tab[14:16, 3]))
tab <- round(tab, 5)
colnames(tab) <- c("SolLC", "SolEMC", "SolLCs", "SolEMCs")

kable(tab, "html", booktabs = T, escape = FALSE, align = "r") %>%
  kable_styling() %>%
  add_header_above(c("Different sigma" = 2, "Same sigma" = 2)) %>%
  pack_rows("Beta 1", 1, 1) %>%
  pack_rows("Beta 2", 2, 5) %>%
  pack_rows("Beta 3", 6, 10) %>%
  pack_rows("Sigma", 11, 13) %>%
  pack_rows("Pi", 14, 16)

## ----message = FALSE----------------------------------------------------------
solLCRisk <- trajeR(
  Y = data_CNORM_Censored[, 2:11],
  A = data_CNORM_Censored[, 12:21],
  Risk = data_CNORM_Censored[, 42:43],
  ng = 3,
  degre = c(0, 3, 4),
  Model = "CNORM",
  Method = "L",
  ssigma = TRUE,
  hessian = TRUE,
  ymin = 2,
  ymax = 23,
  control = list(stepmax = 1e-2)
)
solLCRisk

## ----message = FALSE----------------------------------------------------------
solLCRisks <- trajeR(
  Y = data_CNORM_Censored[, 2:11],
  A = data_CNORM_Censored[, 12:21],
  Risk = data_CNORM_Censored[, 42:43],
  ng = 3,
  degre = c(0, 3, 4),
  Model = "CNORM",
  Method = "L",
  ssigma = TRUE,
  hessian = TRUE,
    ymin = 2,
  ymax = 23,
  control = list(stepmax = 1e-2)
)
solEMCRisk <- trajeR(
  Y = data_CNORM_Censored[, 2:11],
  A = data_CNORM_Censored[, 12:21],
  Risk = data_CNORM_Censored[, 42:43],
  ng = 3,
  degre = c(0, 3, 4),
  Model = "CNORM",
  Method = "EM",
  ssigma = TRUE,
  hessian = TRUE,
    ymin = 2,
  ymax = 23,
)
solEMCRisks <- trajeR(
  Y = data_CNORM_Censored[, 2:11],
  A = data_CNORM_Censored[, 12:21],
  Risk = data_CNORM_Censored[, 42:43],
  ng = 3,
  degre = c(0, 3, 4),
  Model = "CNORM",
  Method = "EM",
  ssigma = TRUE,
  hessian = TRUE,
    ymin = 2,
  ymax = 23,
)

## ----echo = FALSE-------------------------------------------------------------
library(kableExtra)

tab <- cbind(
  solLCRisk$tab[, 1],
  solEMCRisk$tab[, 1],
  solLCRisks$tab[, 1],
  solEMCRisks$tab[, 1]
)
for (i in 1:4) {
  tab[14:22, i] <- tab[14:22, i] - tab[14:16, i]
}
tab <- tab[-c(14:16), ]
tab <- round(tab, 5)
colnames(tab) <- c("SolLRisk", "SolEMRisk", "SolLRisks", "SolEMRisks")

kable(tab, "html", booktabs = T, escape = FALSE, align = "r") %>%
  kable_styling() %>%
  add_header_above(c("Different sigma" = 2, "Same sigma" = 2)) %>%
  pack_rows("Beta 1", 1, 1) %>%
  pack_rows("Beta 2", 2, 5) %>%
  pack_rows("Beta 3", 6, 10) %>%
  pack_rows("Sigma", 11, 13) %>%
  pack_rows("Theta - First group 0", 14, 19)

## ----message = FALSE----------------------------------------------------------
solLCTCOV2 <- trajeR(
  Y = data_CNORM_Censored[, 2:11],
  A = data_CNORM_Censored[, 12:21],
  TCOV = data_CNORM_Censored[, 22:41],
  ng = 3,
  degre = c(0, 3, 4),
  Model = "CNORM",
  Method = "L",
  ssigma = FALSE,
  hessian = TRUE,
    ymin = 2,
  ymax = 23,
  control = list(stepmax = 1e-2)
)
solLTCOV2

## ----message = FALSE----------------------------------------------------------
solLCTCOV2s <- trajeR(
  data_CNORM_Censored[, 2:11],
  data_CNORM_Censored[, 12:21],
  TCOV = data_CNORM_Censored[, 22:41],
  ng = 3,
  degre = c(0, 3, 4),
  Model = "CNORM",
  Method = "L",
  ssigma = TRUE,
  hessian = TRUE,
    ymin = 2,
  ymax = 23,
  control = list(stepmax = 1e-2)
)
solEMCTCOV2 <- trajeR(
  data_CNORM_Censored[, 2:11],
  data_CNORM_Censored[, 12:21],
  TCOV = data_CNORM_Censored[, 22:41],
  ng = 3,
  degre = c(0, 3, 4),
  Model = "CNORM",
  Method = "EM",
  ssigma = FALSE,
  hessian = TRUE,
    ymin = 2,
  ymax = 23,
)
solEMCTCOV2s <- trajeR(
  data_CNORM_Censored[, 2:11],
  data_CNORM_Censored[, 12:21],
  TCOV = data_CNORM_Censored[, 22:41],
  ng = 3,
  degre = c(0, 3, 4),
  Model = "CNORM",
  Method = "EM",
  ssigma = TRUE,
  hessian = TRUE,
    ymin = 2,
  ymax = 23,
)

## ----echo = FALSE-------------------------------------------------------------
t1 <- solLCTCOV2$tab[, 1]
t1[20:22] <- exp(t1[20:22]) / sum(exp(t1[20:22]))
t2 <- solLCTCOV2s$tab[, 1]
t2[20:22] <- exp(t2[20:22]) / sum(exp(t2[20:22]))
tab <- round(cbind(t1, solEMCTCOV2$tab[, 1], t2, solEMCTCOV2s$tab[, 1]), 5)
colnames(tab) <- c("SolLTCOV2", "SolEMCOV2", "SolLTCOV2s", "SolEMCOV2s")

kable(tab, "html", booktabs = T, escape = FALSE, align = "r") %>%
  kable_styling() %>%
  add_header_above(c("Different sigma" = 2, "Same sigma" = 2)) %>%
  pack_rows("Beta 1", 1, 1) %>%
  pack_rows("Beta 2", 2, 5) %>%
  pack_rows("Beta 3", 6, 10) %>%
  pack_rows("Sigma", 11, 13) %>%
  pack_rows("Delta 1", 14, 15) %>%
  pack_rows("Delta 2", 16, 17) %>%
  pack_rows("Delta 3", 18, 19) %>%
  pack_rows("Pi", 20, 22)

