Nothing
###################################################################################
# modification of print's method for class trajectory
####################################################################################
####################################################################################
####################################################################################
# general functions
####################################################################################
Row <- function(x, n, pad = 1) {
foo <- function(i, x, n) {
fmt <- paste0("%", n[i], "s")
sprintf(fmt, as.character(x[i]))
}
rowc <- sapply(seq_along(x), foo, x = x, n = n)
paste0(
" ",
paste(paste0(rep(" ", pad), rowc, rep(" ", pad)), collapse = " "),
" "
)
}
SepLine1 <- function(n, pad = 1) {
tmp <- lapply(
n,
function(x, pad) {
paste(rep("-", x + (2 * pad)), collapse = "")
},
pad = pad
)
paste0("-", paste(tmp, collapse = "-"), "-")
}
SepLine2 <- function(n, pad = 1) {
tmp <- lapply(
n,
function(x, pad) {
paste(rep(" ", x + (2 * pad)), collapse = "")
},
pad = pad
)
paste0(" ", paste(tmp, collapse = " "), " ")
}
###################################################################################
# modification of print's method for class trajectory.CNORM
####################################################################################
#' @title Print Method for a `Trajectory.CNORM` Object
#'
#' @description Displays a summary of a censored normal trajectory model fitted by `trajeR`.
#'
#' @param x An object of class `Trajectory.CNORM`.
#' @param ... Further arguments passed to or from other methods (not currently used).
#'
#' @return Invisibly returns the original object `x`.
#' @export
#'
#' @examples
#' data <- read.csv(system.file("extdata", "CNORM2gr.csv", package = "trajeR"))
#' data <- as.matrix(data)
#' sol <- trajeR(Y = data[, 2:6], A = data[, 7:11], degre = c(2, 2), Model = "CNORM", Method = "EM")
#' sol
print.Trajectory.CNORM <- function(x, ...) {
Obj <- x
n <- Obj$Size
ng <- Obj$groups
nbeta <- Obj$degre + 1
ntheta <- length(Obj$theta) / ng
if (any(is.na(Obj$delta))) {
ndelta <- 0
} else {
ndelta <- length(Obj$delta) / ng
}
nbetatmp <- c(0, nbeta)
nbetacum <- cumsum(c(0, nbeta))
ndeltacum <- cumsum(c(0, rep(ndelta, ng)))
indbeta <- 1:sum(nbeta)
indsigma <- (sum(nbeta) + 1):(sum(nbeta) + ng)
if (any(is.na(Obj$delta))) {
inddelta <- 0
} else {
inddelta <- (sum(nbeta) + ng + 1):(sum(nbeta) + ng + ndelta * ng)
}
indtheta <- (sum(nbeta) + ng + ndelta * ng + 1):(sum(nbeta) +
ng +
ndelta * ng +
ntheta * ng)
indcum <- c(0, nbetacum[-1] + (1:ng) * ndelta)
group <- rep("", sum(nbeta) + ng + ndelta * ng + ntheta * ng)
for (i in 1:ng) {
group[nbetacum[i] + 1] <- as.character(i)
}
for (i in 1:ng) {
group[nbetacum[ng + 1] + i] <- as.character(i)
}
for (i in 1:(ng)) {
group[indcum[ng + 1] + ng + 1 + (i - 1) * ntheta] <- as.character(i)
}
dfprint <- cbind(group, Parameter = Obj$Names, Obj$tab)
dfprint[, 2] <- as.character(dfprint[, 2])
dfprint[, 1] <- as.character(dfprint[, 1])
if (ntheta == 1) {
if (Obj$Method == "L") {
dfprint[indtheta, 3] <- exp(dfprint[indtheta, 3]) /
sum(exp(dfprint[indtheta, 3]))
}
dfprint[indtheta, 2] <- paste0("pi", 1:ng)
} else {
dfprint[indtheta, 3] <- dfprint[indtheta, 3] -
dfprint[indtheta[1:ntheta], 3]
dfprint <- dfprint[-indtheta[2:ntheta], ]
dfprint[indcum[ng + 1] + ng + 1, 2] <- "Baseline"
}
tmp <- c()
for (i in 1:ng) {
tmp <- rbind(tmp, dfprint[(nbetacum[i] + 1):(nbetacum[i + 1]), ])
if (ndelta != 0) {
tmp <- rbind(
tmp,
dfprint[
(sum(nbeta) + ng + ndeltacum[i] + 1):(sum(nbeta) +
ng +
ndeltacum[i + 1]),
]
)
}
}
for (i in 1:ng) {
tmp <- rbind(tmp, dfprint[sum(nbeta) + i, ])
}
if (ntheta == 1) {
tmp <- rbind(tmp, dfprint[indtheta, ])
} else {
tmp <- rbind(tmp, dfprint[indtheta[1:(length(indtheta) - ntheta + 1)], ])
}
dfprint <- tmp
dfprint[, 3] <- round(as.numeric(dfprint[, 3]), 5)
dfprint[, 5] <- round(as.numeric(dfprint[, 5]), 5)
dfprint[, 6] <- round(as.numeric(dfprint[, 6]), 5)
dfprint[, 4] <- round(as.numeric(dfprint[, 4]), 5)
fObj <- format(dfprint)
strings <- apply(dfprint, 2, function(x) unlist(dfprint))[1, ]
widths <- nchar(strings)
names <- c(
"Group",
"Parameter",
"Estimate",
"Std. Error",
"T for H0:",
"Prob>|T|"
)
widths <- pmax(nchar(strings), nchar(names))
csum <- sum(widths + 1) - 1
cat(
paste("Call TrajeR with"),
Obj$groups,
"groups and a",
paste(Obj$degre, collapse = ","),
"degrees of polynomial shape of trajectory.\n"
)
cat("Model : Censored Normal\n")
if (Obj$Method == "L") {
cat("Method : Likelihood \n")
} else if (Obj$Method == "EM") {
cat("Method : Expectation-maximization \n")
} else {
cat("Method : Expectation-maximization with IWRLS\n")
}
conv_msg <- if (Obj$convergence %in% c(0, 1, 2, 3, 4)) {
sprintf("Successful (Code %d)", Obj$convergence)
} else if (Obj$convergence < 0) {
sprintf("Warning (Code %d)", Obj$convergence)
} else {
sprintf("Warning (Code %d)", Obj$convergence)
}
cat(sprintf("Convergence : %s\n\n", conv_msg))
esp <- 1
sep1 <- SepLine1(widths, pad = esp)
sep2 <- SepLine2(widths, pad = esp)
namestmp <- colnames(dfprint)
namestmp[5] <- "T for H0:"
writeLines(Row(namestmp, widths, esp))
writeLines(Row(c("", "", "", "", "param.=0", ""), widths, esp))
writeLines(sep1)
for (i in 1:(ng - 1)) {
for (j in 1:(nbeta[i] + ndelta)) {
writeLines(Row(dfprint[indcum[i] + j, ], widths, esp))
}
writeLines(sep2)
}
for (j in 1:(nbeta[ng] + ndelta)) {
writeLines(Row(dfprint[indcum[ng] + j, ], widths, esp))
}
writeLines(sep1)
for (i in 1:(ng - 1)) {
writeLines(Row(dfprint[sum(nbeta) + ndelta * ng + i, ], widths, esp))
}
writeLines(Row(dfprint[sum(nbeta) + ndelta * ng + ng, ], widths, esp))
writeLines(sep1)
if (ntheta == 1) {
for (i in 1:ng) {
if (ndelta != 0) {
writeLines(Row(
dfprint[sum(nbeta) + ng + ndelta * ng + i, ],
widths,
esp
))
} else {
writeLines(Row(
dfprint[sum(nbeta) + ng + ndelta * ng + i, ],
widths,
esp
))
}
}
} else {
writeLines(Row(dfprint[indcum[ng + 1] + ng + 1, ], widths, esp))
writeLines(sep2)
if (ng > 2) {
for (j in 1:((ng - 2) * ntheta)) {
writeLines(Row(dfprint[indcum[ng + 1] + ng + 1 + j, ], widths, esp))
}
writeLines(sep2)
for (j in 1:ntheta) {
writeLines(Row(
dfprint[indcum[ng + 1] + ng + (ng - 2) * ntheta + 1 + j, ],
widths,
esp
))
}
} else {
for (j in 1:ntheta) {
writeLines(Row(dfprint[indcum[ng + 1] + ng + 1 + j, ], widths, esp))
}
}
}
writeLines(sep1)
cat("Likelihood :", Obj$Likelihood)
}
###################################################################################
# modification of print's method for class trajectory.ZIP
####################################################################################
#' Print ZIP
#'
#' Print method for an object of class "\code{Trajectory.ZIP}".
#'
#' @param x Trajectory's object. An object of class "\code{Trajectory.ZIP}".
#' @param ... optional parameters
#'
#' @export
#'
#' @return The print of Obj.
#'
#' @examples
#' data <- read.csv(system.file("extdata", "ZIP2gr.csv", package = "trajeR"))
#' data <- as.matrix(data)
#' sol <- trajeR(
#' Y = data[, 2:6], A = data[, 7:11],
#' degre = c(1, 2), degre.nu = c(1, 1), Model = "ZIP", Method = "L"
#' )
#' sol
print.Trajectory.ZIP <- function(x, ...) {
Obj <- x
n <- Obj$Size
ng <- Obj$groups
nbeta <- Obj$degre + 1
ntheta <- length(Obj$theta) / ng
nnu <- Obj$degre.nu + 1
if (any(is.na(Obj$delta))) {
ndelta <- 0
} else {
ndelta <- length(Obj$delta) / ng
}
nbetatmp <- c(0, nbeta)
nbetacum <- cumsum(c(0, nbeta))
nnucum <- cumsum(c(0, nnu))
ndeltacum <- cumsum(c(0, rep(ndelta, ng)))
indbeta <- 1:sum(nbeta)
indnu <- (sum(nbeta) + 1):(sum(nbeta) + sum(nnu))
if (any(is.na(Obj$delta))) {
inddelta <- 0
} else {
inddelta <- (sum(nbeta) + sum(nnu) + 1):(sum(nbeta) +
sum(nnu) +
ndelta * ng)
}
indtheta <- (sum(nbeta) + sum(nnu) + ndelta * ng + 1):(sum(nbeta) +
sum(nnu) +
ndelta * ng +
ntheta * ng)
indcum <- c(0, nbetacum[-1] + nnucum[-1] + (1:ng) * ndelta)
group <- rep("", sum(nbeta) + sum(nnu) + ndelta * ng + ntheta * ng)
for (i in 1:ng) {
group[nbetacum[i] + 1] <- as.character(i)
group[indcum[ng + 1] + 1 + (i - 1) * ntheta] <- as.character(i)
}
dfprint <- cbind(group, Parameter = Obj$Names, Obj$tab)
dfprint[, 2] <- as.character(dfprint[, 2])
dfprint[, 1] <- as.character(dfprint[, 1])
if (ntheta == 1) {
if (Obj$Method == "L") {
dfprint[indtheta, 3] <- exp(dfprint[indtheta, 3]) /
sum(exp(dfprint[indtheta, 3]))
}
dfprint[indtheta, 2] <- paste0("pi", 1:ng)
} else {
dfprint[indtheta, 3] <- dfprint[indtheta, 3] -
dfprint[indtheta[1:ntheta], 3]
dfprint <- dfprint[-indtheta[2:ntheta], ]
dfprint[indcum[ng + 1] + 1, 2] <- "Baseline"
}
tmp <- c()
for (i in 1:ng) {
tmp <- rbind(tmp, dfprint[(nbetacum[i] + 1):(nbetacum[i + 1]), ])
tmp <- rbind(
tmp,
dfprint[
(nbetacum[ng + 1] + nnucum[i] + 1):(nbetacum[ng + 1] + nnucum[i + 1]),
]
)
if (ndelta != 0) {
tmp <- rbind(
tmp,
dfprint[
(sum(nbeta) + sum(nnu) + ndeltacum[i] + 1):(sum(nbeta) +
sum(nnu) +
ndeltacum[i + 1]),
]
)
}
}
if (ntheta == 1) {
tmp <- rbind(tmp, dfprint[indtheta, ])
} else {
tmp <- rbind(tmp, dfprint[indtheta[1:(length(indtheta) - ntheta + 1)], ])
}
dfprint <- tmp
dfprint[, 3] <- round(as.numeric(dfprint[, 3]), 5)
dfprint[, 5] <- round(as.numeric(dfprint[, 5]), 5)
dfprint[, 6] <- round(as.numeric(dfprint[, 6]), 5)
dfprint[, 4] <- round(as.numeric(dfprint[, 4]), 5)
fObj <- format(dfprint)
strings <- apply(dfprint, 2, function(x) unlist(dfprint))[1, ]
widths <- nchar(strings)
names <- c(
"Group",
"Parameter",
"Estimate",
"Std. Error",
"T for H0:",
"Prob>|T|"
)
widths <- pmax(nchar(strings), nchar(names))
csum <- sum(widths + 1) - 1
cat(
paste("Call TrajeR with"),
Obj$groups,
"groups and a",
paste(Obj$degre, collapse = ","),
"degrees of polynomial shape of trajectory.\n"
)
cat("Model : Zero Inflated Poisson\n")
if (Obj$Method == "L") {
cat("Method : Likelihood \n")
} else if (Obj$Method == "EM") {
cat("Method : Expectation-maximization \n")
} else {
cat("Method : Expectation-maximization with IWRLS\n")
}
conv_msg <- if (Obj$convergence %in% c(0, 1, 2, 3, 4)) {
sprintf("Successful (Code %d)", Obj$convergence)
} else if (Obj$convergence < 0) {
sprintf("Warning (Code %d)", Obj$convergence)
} else {
sprintf("Warning (Code %d)", Obj$convergence)
}
cat(sprintf("Convergence : %s\n\n", conv_msg))
esp <- 1
sep1 <- SepLine1(widths, pad = esp)
sep2 <- SepLine2(widths, pad = esp)
namestmp <- colnames(dfprint)
namestmp[5] <- "T for H0:"
writeLines(Row(namestmp, widths, esp))
writeLines(Row(c("", "", "", "", "param.=0", ""), widths, esp))
writeLines(sep1)
for (i in 1:(ng - 1)) {
for (j in 1:(nbeta[i] + nnu[i] + ndelta)) {
writeLines(Row(dfprint[indcum[i] + j, ], widths, esp))
}
writeLines(sep2)
}
for (j in 1:(nbeta[ng] + nnu[ng] + ndelta)) {
writeLines(Row(dfprint[indcum[ng] + j, ], widths, esp))
}
writeLines(sep1)
if (ntheta == 1) {
for (i in 1:ng) {
if (ndelta != 0) {
writeLines(Row(
dfprint[sum(nbeta) + sum(nnu) + ndelta * ng + i, ],
widths,
esp
))
} else {
writeLines(Row(
dfprint[sum(nbeta) + sum(nnu) + ndelta * ng + i, ],
widths,
esp
))
}
}
} else {
writeLines(Row(dfprint[indcum[ng + 1] + 1, ], widths, esp))
writeLines(sep2)
if (ng > 2) {
for (i in 2:(ng - 1)) {
for (j in 1:((ng - 2) * ntheta)) {
writeLines(Row(dfprint[indcum[ng + 1] + 1 + j, ], widths, esp))
}
writeLines(sep2)
}
for (j in 1:ntheta) {
writeLines(Row(
dfprint[indcum[ng + 1] + (ng - 2) * ntheta + 1 + j, ],
widths,
esp
))
}
} else {
for (j in 1:ntheta) {
writeLines(Row(dfprint[indcum[ng + 1] + 1 + j, ], widths, esp))
}
}
}
writeLines(sep1)
cat("Likelihood :", Obj$Likelihood)
}
###################################################################################
# modification of print's method for class trajectory.LOGIT
####################################################################################
#' @title Print Method for a `Trajectory.LOGIT` Object
#'
#' @description Displays a summary of a logistic trajectory model fitted by `trajeR`.
#'
#' @param x An object of class `Trajectory.LOGIT`.
#' @param ... Further arguments passed to or from other methods (not currently used).
#'
#' @return Invisibly returns the original object `x`.
#' @export
#'
#' @examples
#' data <- read.csv(system.file("extdata", "LOGIT2gr.csv", package = "trajeR"))
#' data <- as.matrix(data)
#' sol <- trajeR(Y = data[, 2:6], A = data[, 7:11], degre = c(1, 2), Model = "LOGIT", Method = "L")
#' sol
print.Trajectory.LOGIT <- function(x, ...) {
Obj <- x
n <- Obj$Size
ng <- Obj$groups
nbeta <- Obj$degre + 1
ntheta <- length(Obj$theta) / ng
if (any(is.na(Obj$delta))) {
ndelta <- 0
} else {
ndelta <- length(Obj$delta) / ng
}
nbetatmp <- c(0, nbeta)
nbetacum <- cumsum(c(0, nbeta))
ndeltacum <- cumsum(c(0, rep(ndelta, ng)))
indbeta <- 1:sum(nbeta)
if (any(is.na(Obj$delta))) {
inddelta <- 0
} else {
inddelta <- (sum(nbeta) + 1):(sum(nbeta) + ndelta * ng)
}
indtheta <- (sum(nbeta) + ndelta * ng + 1):(sum(nbeta) +
ndelta * ng +
ntheta * ng)
indcum <- c(0, nbetacum[-1] + (1:ng) * ndelta)
group <- rep("", sum(nbeta) + ndelta * ng + ntheta * ng)
for (i in 1:ng) {
group[nbetacum[i] + 1] <- as.character(i)
}
for (i in 1:(ng)) {
group[indcum[ng + 1] + 1 + (i - 1) * ntheta] <- as.character(i)
}
dfprint <- cbind(group, Parameter = Obj$Names, Obj$tab)
dfprint[, 2] <- as.character(dfprint[, 2])
dfprint[, 1] <- as.character(dfprint[, 1])
if (ntheta == 1) {
if (Obj$Method == "L") {
dfprint[indtheta, 3] <- exp(dfprint[indtheta, 3]) /
sum(exp(dfprint[indtheta, 3]))
}
dfprint[indtheta, 2] <- paste0("pi", 1:ng)
} else {
dfprint[indtheta, 3] <- dfprint[indtheta, 3] -
dfprint[indtheta[1:ntheta], 3]
dfprint <- dfprint[-indtheta[2:ntheta], ]
dfprint[indcum[ng + 1] + 1, 2] <- "Baseline"
}
tmp <- c()
for (i in 1:ng) {
tmp <- rbind(tmp, dfprint[(nbetacum[i] + 1):(nbetacum[i + 1]), ])
if (ndelta != 0) {
tmp <- rbind(
tmp,
dfprint[
(sum(nbeta) + ndeltacum[i] + 1):(sum(nbeta) + ndeltacum[i + 1]),
]
)
}
}
if (ntheta == 1) {
tmp <- rbind(tmp, dfprint[indtheta, ])
} else {
tmp <- rbind(tmp, dfprint[indtheta[1:(length(indtheta) - ntheta + 1)], ])
}
dfprint <- tmp
dfprint[, 3] <- round(as.numeric(dfprint[, 3]), 5)
dfprint[, 5] <- round(as.numeric(dfprint[, 5]), 5)
dfprint[, 6] <- round(as.numeric(dfprint[, 6]), 5)
dfprint[, 4] <- round(as.numeric(dfprint[, 4]), 5)
fObj <- format(dfprint)
strings <- apply(dfprint, 2, function(x) unlist(dfprint))[1, ]
widths <- nchar(strings)
names <- c(
"Group",
"Parameter",
"Estimate",
"Std. Error",
"T for H0:",
"Prob>|T|"
)
widths <- pmax(nchar(strings), nchar(names))
csum <- sum(widths + 1) - 1
cat(
paste("Call TrajeR with"),
Obj$groups,
"groups and a",
paste(Obj$degre, collapse = ","),
"degrees of polynomial shape of trajectory.\n"
)
cat("Model : Logit\n")
if (Obj$Method == "L") {
cat("Method : Likelihood \n")
} else if (Obj$Method == "EM") {
cat("Method : Expectation-maximization \n")
} else {
cat("Method : Expectation-maximization with IWRLS\n")
}
conv_msg <- if (Obj$convergence %in% c(0, 1, 2, 3, 4)) {
sprintf("Successful (Code %d)", Obj$convergence)
} else if (Obj$convergence < 0) {
sprintf("Warning (Code %d)", Obj$convergence)
} else {
sprintf("Warning (Code %d)", Obj$convergence)
}
cat(sprintf("Convergence : %s\n\n", conv_msg))
esp <- 1
sep1 <- SepLine1(widths, pad = esp)
sep2 <- SepLine2(widths, pad = esp)
namestmp <- colnames(dfprint)
namestmp[5] <- "T for H0:"
writeLines(Row(namestmp, widths, esp))
writeLines(Row(c("", "", "", "", "param.=0", ""), widths, esp))
writeLines(sep1)
for (i in 1:(ng - 1)) {
for (j in 1:(nbeta[i] + ndelta)) {
writeLines(Row(dfprint[indcum[i] + j, ], widths, esp))
}
writeLines(sep2)
}
for (j in 1:(nbeta[ng] + ndelta)) {
writeLines(Row(dfprint[indcum[ng] + j, ], widths, esp))
}
writeLines(sep1)
if (ntheta == 1) {
for (i in 1:ng) {
if (ndelta != 0) {
writeLines(Row(dfprint[sum(nbeta) + ndelta * ng + i, ], widths, esp))
} else {
writeLines(Row(dfprint[sum(nbeta) + ndelta * ng + i, ], widths, esp))
}
}
} else {
writeLines(Row(dfprint[indcum[ng + 1] + 1, ], widths, esp))
writeLines(sep2)
if (ng > 2) {
for (i in 2:(ng - 1)) {
for (j in 1:((ng - 2) * ntheta)) {
writeLines(Row(dfprint[indcum[ng + 1] + 1 + j, ], widths, esp))
}
writeLines(sep2)
}
for (j in 1:ntheta) {
writeLines(Row(
dfprint[indcum[ng + 1] + (ng - 2) * ntheta + 1 + j, ],
widths,
esp
))
}
} else {
for (j in 1:ntheta) {
writeLines(Row(dfprint[indcum[ng + 1] + 1 + j, ], widths, esp))
}
}
}
writeLines(sep1)
cat("Likelihood :", Obj$Likelihood)
}
###################################################################################
# modification of print's method for class trajectory.POIS
####################################################################################
#' @title Print Method for a `Trajectory.ZIP` Object
#'
#' @description Displays a summary of a zero-inflated Poisson trajectory model fitted by `trajeR`.
#'
#' @param x An object of class `Trajectory.ZIP`.
#' @param ... Further arguments passed to or from other methods (not currently used).
#'
#' @return Invisibly returns the original object `x`.
#' @export
#'
#' @examples
#' data <- read.csv(system.file("extdata", "POIS2gr.csv", package = "trajeR"))
#' data <- as.matrix(data)
#' sol <- trajeR(
#' Y = data[, 2:6], A = data[, 7:11],
#' degre = c(2, 2), Model = "POIS", Method = "L", hessian = FALSE
#' )
#' sol
print.Trajectory.POIS <- function(x, ...) {
Obj <- x
n <- Obj$Size
ng <- Obj$groups
nbeta <- Obj$degre + 1
ntheta <- length(Obj$theta) / ng
if (any(is.na(Obj$delta))) {
ndelta <- 0
} else {
ndelta <- length(Obj$delta) / ng
}
nbetatmp <- c(0, nbeta)
nbetacum <- cumsum(c(0, nbeta))
ndeltacum <- cumsum(c(0, rep(ndelta, ng)))
indbeta <- 1:sum(nbeta)
if (any(is.na(Obj$delta))) {
inddelta <- 0
} else {
inddelta <- (sum(nbeta) + 1):(sum(nbeta) + ndelta * ng)
}
indtheta <- (sum(nbeta) + ndelta * ng + 1):(sum(nbeta) +
ndelta * ng +
ntheta * ng)
indcum <- c(0, nbetacum[-1] + (1:ng) * ndelta)
group <- rep("", sum(nbeta) + ndelta * ng + ntheta * ng)
for (i in 1:ng) {
group[nbetacum[i] + 1] <- as.character(i)
}
for (i in 1:(ng)) {
group[indcum[ng + 1] + 1 + (i - 1) * ntheta] <- as.character(i)
}
dfprint <- cbind(group, Parameter = Obj$Names, Obj$tab)
dfprint[, 2] <- as.character(dfprint[, 2])
dfprint[, 1] <- as.character(dfprint[, 1])
if (ntheta == 1) {
if (Obj$Method == "L") {
dfprint[indtheta, 3] <- exp(dfprint[indtheta, 3]) /
sum(exp(dfprint[indtheta, 3]))
}
dfprint[indtheta, 2] <- paste0("pi", 1:ng)
} else {
dfprint[indtheta, 3] <- dfprint[indtheta, 3] -
dfprint[indtheta[1:ntheta], 3]
dfprint <- dfprint[-indtheta[2:ntheta], ]
dfprint[indcum[ng + 1] + 1, 2] <- "Baseline"
}
tmp <- c()
for (i in 1:ng) {
tmp <- rbind(tmp, dfprint[(nbetacum[i] + 1):(nbetacum[i + 1]), ])
if (ndelta != 0) {
tmp <- rbind(
tmp,
dfprint[
(sum(nbeta) + ndeltacum[i] + 1):(sum(nbeta) + ndeltacum[i + 1]),
]
)
}
}
if (ntheta == 1) {
tmp <- rbind(tmp, dfprint[indtheta, ])
} else {
tmp <- rbind(tmp, dfprint[indtheta[1:(length(indtheta) - ntheta + 1)], ])
}
dfprint <- tmp
dfprint[, 3] <- round(as.numeric(dfprint[, 3]), 5)
dfprint[, 5] <- round(as.numeric(dfprint[, 5]), 5)
dfprint[, 6] <- round(as.numeric(dfprint[, 6]), 5)
dfprint[, 4] <- round(as.numeric(dfprint[, 4]), 5)
fObj <- format(dfprint)
strings <- apply(dfprint, 2, function(x) unlist(dfprint))[1, ]
widths <- nchar(strings)
names <- c(
"Group",
"Parameter",
"Estimate",
"Std. Error",
"T for H0:",
"Prob>|T|"
)
widths <- pmax(nchar(strings), nchar(names))
csum <- sum(widths + 1) - 1
cat(
paste("Call TrajeR with"),
Obj$groups,
"groups and a",
paste(Obj$degre, collapse = ","),
"degrees of polynomial shape of trajectory.\n"
)
cat("Model : Poisson\n")
if (Obj$Method == "L") {
cat("Method : Likelihood \n")
} else if (Obj$Method == "EM") {
cat("Method : Expectation-maximization \n")
} else {
cat("Method : Expectation-maximization with IWRLS\n")
}
conv_msg <- if (Obj$convergence %in% c(0, 1, 2, 3, 4)) {
sprintf("Successful (Code %d)", Obj$convergence)
} else if (Obj$convergence < 0) {
sprintf("Warning (Code %d)", Obj$convergence)
} else {
sprintf("Warning (Code %d)", Obj$convergence)
}
cat(sprintf("Convergence : %s\n\n", conv_msg))
esp <- 1
sep1 <- SepLine1(widths, pad = esp)
sep2 <- SepLine2(widths, pad = esp)
namestmp <- colnames(dfprint)
namestmp[5] <- "T for H0:"
writeLines(Row(namestmp, widths, esp))
writeLines(Row(c("", "", "", "", "param.=0", ""), widths, esp))
writeLines(sep1)
for (i in 1:(ng - 1)) {
for (j in 1:(nbeta[i] + ndelta)) {
writeLines(Row(dfprint[indcum[i] + j, ], widths, esp))
}
writeLines(sep2)
}
for (j in 1:(nbeta[ng] + ndelta)) {
writeLines(Row(dfprint[indcum[ng] + j, ], widths, esp))
}
writeLines(sep1)
if (ntheta == 1) {
for (i in 1:ng) {
if (ndelta != 0) {
writeLines(Row(dfprint[sum(nbeta) + ndelta * ng + i, ], widths, esp))
} else {
writeLines(Row(dfprint[sum(nbeta) + ndelta * ng + i, ], widths, esp))
}
}
} else {
writeLines(Row(dfprint[indcum[ng + 1] + 1, ], widths, esp))
writeLines(sep2)
if (ng > 2) {
for (i in 2:(ng - 1)) {
for (j in 1:((ng - 2) * ntheta)) {
writeLines(Row(dfprint[indcum[ng + 1] + 1 + j, ], widths, esp))
}
writeLines(sep2)
}
for (j in 1:ntheta) {
writeLines(Row(
dfprint[indcum[ng + 1] + (ng - 2) * ntheta + 1 + j, ],
widths,
esp
))
}
} else {
for (j in 1:ntheta) {
writeLines(Row(dfprint[indcum[ng + 1] + 1 + j, ], widths, esp))
}
}
}
writeLines(sep1)
cat("Likelihood :", Obj$Likelihood)
}
###################################################################################
# modification of print's method for class trajectory.NL
####################################################################################
#' @title Print Method for a `Trajectory.NL` Object
#'
#' @description Displays a summary of a non-linear trajectory model fitted by `trajeR`.
#'
#' @param x An object of class `Trajectory.NL`.
#' @param ... Further arguments passed to or from other methods (not currently used).
#'
#' @return Invisibly returns the original object `x`.
#' @export
#'
print.Trajectory.NL <- function(x, ...) {
Obj <- x
n <- Obj$Size
ng <- Obj$groups
nbeta <- Obj$degre + 1
ntheta <- length(Obj$theta) / ng
if (any(is.na(Obj$delta))) {
ndelta <- 0
} else {
ndelta <- length(Obj$delta) / ng
}
nbetatmp <- c(0, nbeta)
nbetacum <- cumsum(c(0, nbeta))
ndeltacum <- cumsum(c(0, rep(ndelta, ng)))
indbeta <- 1:sum(nbeta)
indsigma <- (sum(nbeta) + 1):(sum(nbeta) + ng)
if (any(is.na(Obj$delta))) {
inddelta <- 0
} else {
inddelta <- (sum(nbeta) + ng + 1):(sum(nbeta) + ng + ndelta * ng)
}
indtheta <- (sum(nbeta) + ng + ndelta * ng + 1):(sum(nbeta) +
ng +
ndelta * ng +
ntheta * ng)
indcum <- c(0, nbetacum[-1] + (1:ng) * ndelta)
group <- rep("", sum(nbeta) + ng + ndelta * ng + ntheta * ng)
for (i in 1:ng) {
group[nbetacum[i] + 1] <- as.character(i)
}
for (i in 1:ng) {
group[nbetacum[ng + 1] + i] <- as.character(i)
}
for (i in 1:(ng)) {
group[indcum[ng + 1] + ng + 1 + (i - 1) * ntheta] <- as.character(i)
}
dfprint <- cbind(group, Parameter = Obj$Names, Obj$tab)
dfprint[, 2] <- as.character(dfprint[, 2])
dfprint[, 1] <- as.character(dfprint[, 1])
if (ntheta == 1) {
if (Obj$Method == "L") {
dfprint[indtheta, 3] <- exp(dfprint[indtheta, 3]) /
sum(exp(dfprint[indtheta, 3]))
}
dfprint[indtheta, 2] <- paste0("pi", 1:ng)
} else {
dfprint[indtheta, 3] <- dfprint[indtheta, 3] -
dfprint[indtheta[1:ntheta], 3]
dfprint <- dfprint[-indtheta[2:ntheta], ]
dfprint[indcum[ng + 1] + ng + 1, 2] <- "Baseline"
}
tmp <- c()
for (i in 1:ng) {
tmp <- rbind(tmp, dfprint[(nbetacum[i] + 1):(nbetacum[i + 1]), ])
if (ndelta != 0) {
tmp <- rbind(
tmp,
dfprint[
(sum(nbeta) + ng + ndeltacum[i] + 1):(sum(nbeta) +
ng +
ndeltacum[i + 1]),
]
)
}
}
for (i in 1:ng) {
tmp <- rbind(tmp, dfprint[sum(nbeta) + i, ])
}
if (ntheta == 1) {
tmp <- rbind(tmp, dfprint[indtheta, ])
} else {
tmp <- rbind(tmp, dfprint[indtheta[1:(length(indtheta) - ntheta + 1)], ])
}
dfprint <- tmp
dfprint[, 3] <- round(as.numeric(dfprint[, 3]), 5)
dfprint[, 5] <- round(as.numeric(dfprint[, 5]), 5)
dfprint[, 6] <- round(as.numeric(dfprint[, 6]), 5)
dfprint[, 4] <- round(as.numeric(dfprint[, 4]), 5)
fObj <- format(dfprint)
strings <- apply(dfprint, 2, function(x) unlist(dfprint))[1, ]
widths <- nchar(strings)
names <- c(
"Group",
"Parameter",
"Estimate",
"Std. Error",
"T for H0:",
"Prob>|T|"
)
widths <- pmax(nchar(strings), nchar(names))
csum <- sum(widths + 1) - 1
cat(
paste("Call TrajeR with"),
Obj$groups,
"groups and a",
paste(Obj$degre, collapse = ","),
"degrees of polynomial shape of trajectory.\n"
)
cat("Model : Non Linear\n")
if (Obj$Method == "L") {
cat("Method : Likelihood \n \n")
} else if (Obj$Method == "EM") {
cat("Method : Expectation-maximization \n \n")
} else {
cat("Method : Expectation-maximization with IWRLS\n \n")
}
esp <- 1
sep1 <- SepLine1(widths, pad = esp)
sep2 <- SepLine2(widths, pad = esp)
namestmp <- colnames(dfprint)
namestmp[5] <- "T for H0:"
writeLines(Row(namestmp, widths, esp))
writeLines(Row(c("", "", "", "", "param.=0", ""), widths, esp))
writeLines(sep1)
for (i in 1:(ng - 1)) {
for (j in 1:(nbeta[i] + ndelta)) {
writeLines(Row(dfprint[indcum[i] + j, ], widths, esp))
}
writeLines(sep2)
}
for (j in 1:(nbeta[ng] + ndelta)) {
writeLines(Row(dfprint[indcum[ng] + j, ], widths, esp))
}
writeLines(sep1)
for (i in 1:(ng - 1)) {
writeLines(Row(dfprint[sum(nbeta) + ndelta * ng + i, ], widths, esp))
}
writeLines(Row(dfprint[sum(nbeta) + ndelta * ng + ng, ], widths, esp))
writeLines(sep1)
if (ntheta == 1) {
for (i in 1:ng) {
if (ndelta != 0) {
writeLines(Row(
dfprint[sum(nbeta) + ng + ndelta * ng + i, ],
widths,
esp
))
} else {
writeLines(Row(
dfprint[sum(nbeta) + ng + ndelta * ng + i, ],
widths,
esp
))
}
}
} else {
writeLines(Row(dfprint[indcum[ng + 1] + ng + 1, ], widths, esp))
writeLines(sep2)
if (ng > 2) {
for (i in 2:(ng - 1)) {
for (j in 1:((ng - 2) * ntheta)) {
writeLines(Row(dfprint[indcum[ng + 1] + ng + 1 + j, ], widths, esp))
}
writeLines(sep2)
}
for (j in 1:ntheta) {
writeLines(Row(
dfprint[indcum[ng + 1] + ng + (ng - 2) * ntheta + 1 + j, ],
widths,
esp
))
}
} else {
for (j in 1:ntheta) {
writeLines(Row(dfprint[indcum[ng + 1] + ng + 1 + j, ], widths, esp))
}
}
}
writeLines(sep1)
cat("Likelihood :", Obj$Likelihood)
}
###################################################################################
# modification of print's method for class trajectory.BETA
####################################################################################
#' Print BETA
#'
#' Print method for an object of class "\code{Trajectory.BETA}".
#'
#' @param x Trajectory's object. An object of class "\code{Trajectory.BETA}".
#' @param ... optional parameters
#'
#' @export
#'
#' @return The print of Obj.
#' @examples
#' data <- read.csv(system.file("extdata", "BETA2gr.csv", package = "trajeR"))
#' data <- as.matrix(data)
#' data[, 2:6] <- data[, 2:6] * (nrow(data[, 2:6]) - 1 + 0.5) / nrow(data[, 2:6])
#' sol <- trajeR(
#' Y = data[, 2:6], A = data[, 7:11], itermax = 50,
#' degre = c(2, 2), degre.phi = c(1, 1), Model = "BETA", Method = "L"
#' )
#' sol
print.Trajectory.BETA <- function(x, ...) {
Obj <- x
n <- Obj$Size
ng <- Obj$groups
nbeta <- Obj$degre + 1
ntheta <- length(Obj$theta) / ng
nphi <- Obj$degre.phi + 1
if (any(is.na(Obj$delta))) {
ndelta <- 0
} else {
ndelta <- length(Obj$delta) / ng
}
nbetatmp <- c(0, nbeta)
nbetacum <- cumsum(c(0, nbeta))
nphicum <- cumsum(c(0, nphi))
ndeltacum <- cumsum(c(0, rep(ndelta, ng)))
indbeta <- 1:sum(nbeta)
indphi <- (sum(nbeta) + 1):(sum(nbeta) + sum(nphi))
if (any(is.na(Obj$delta))) {
inddelta <- 0
} else {
inddelta <- (sum(nbeta) + sum(nphi) + 1):(sum(nbeta) +
sum(nphi) +
ndelta * ng)
}
indtheta <- (sum(nbeta) + sum(nphi) + ndelta * ng + 1):(sum(nbeta) +
sum(nphi) +
ndelta * ng +
ntheta * ng)
indcum <- c(0, nbetacum[-1] + nphicum[-1] + (1:ng) * ndelta)
group <- rep("", sum(nbeta) + sum(nphi) + ndelta * ng + ntheta * ng)
for (i in 1:ng) {
group[nbetacum[i] + 1] <- as.character(i)
group[nbetacum[ng + 1] + nphicum[i] + 1] <- as.character(i)
group[indcum[ng + 1] + 1 + (i - 1) * ntheta] <- as.character(i)
}
dfprint <- cbind(group, Parameter = Obj$Names, Obj$tab)
dfprint[, 2] <- as.character(dfprint[, 2])
dfprint[, 1] <- as.character(dfprint[, 1])
if (ntheta == 1) {
if (Obj$Method == "L") {
dfprint[indtheta, 3] <- exp(dfprint[indtheta, 3]) /
sum(exp(dfprint[indtheta, 3]))
}
dfprint[indtheta, 2] <- paste0("pi", 1:ng)
} else {
dfprint[indtheta, 3] <- dfprint[indtheta, 3] -
dfprint[indtheta[1:ntheta], 3]
dfprint <- dfprint[-indtheta[2:ntheta], ]
dfprint[indcum[ng + 1] + 1, 2] <- "Baseline"
}
tmp <- c()
for (i in 1:ng) {
tmp <- rbind(tmp, dfprint[(nbetacum[i] + 1):(nbetacum[i + 1]), ])
tmp <- rbind(
tmp,
dfprint[
(nbetacum[ng + 1] + nphicum[i] + 1):(nbetacum[ng + 1] + nphicum[i + 1]),
]
)
if (ndelta != 0) {
tmp <- rbind(
tmp,
dfprint[
(sum(nbeta) + sum(nphi) + ndeltacum[i] + 1):(sum(nbeta) +
sum(nphi) +
ndeltacum[i + 1]),
]
)
}
}
if (ntheta == 1) {
tmp <- rbind(tmp, dfprint[indtheta, ])
} else {
tmp <- rbind(tmp, dfprint[indtheta[1:(length(indtheta) - ntheta + 1)], ])
}
dfprint <- tmp
dfprint[, 3] <- round(as.numeric(dfprint[, 3]), 5)
dfprint[, 5] <- round(as.numeric(dfprint[, 5]), 5)
dfprint[, 6] <- round(as.numeric(dfprint[, 6]), 5)
dfprint[, 4] <- round(as.numeric(dfprint[, 4]), 5)
fObj <- format(dfprint)
strings <- apply(dfprint, 2, function(x) unlist(dfprint))[1, ]
widths <- nchar(strings)
names <- c(
"Group",
"Parameter",
"Estimate",
"Std. Error",
"T for H0:",
"Prob>|T|"
)
widths <- pmax(nchar(strings), nchar(names))
csum <- sum(widths + 1) - 1
cat(
paste("Call TrajeR with"),
Obj$groups,
"groups and a",
paste(Obj$degre, collapse = ","),
"degrees of polynomial shape of trajectory.\n"
)
cat("Model : Beta\n")
if (Obj$Method == "L") {
cat("Method : Likelihood \n")
} else if (Obj$Method == "EM") {
cat("Method : Expectation-maximization \n")
} else {
cat("Method : Expectation-maximization with IWRLS\n")
}
conv_msg <- if (Obj$convergence %in% c(0, 1, 2, 3, 4)) {
sprintf("Successful (Code %d)", Obj$convergence)
} else if (Obj$convergence < 0) {
sprintf("Warning (Code %d)", Obj$convergence)
} else {
sprintf("Warning (Code %d)", Obj$convergence)
}
cat(sprintf("Convergence : %s\n\n", conv_msg))
esp <- 1
sep1 <- SepLine1(widths, pad = esp)
sep2 <- SepLine2(widths, pad = esp)
namestmp <- colnames(dfprint)
namestmp[5] <- "T for H0:"
writeLines(Row(namestmp, widths, esp))
writeLines(Row(c("", "", "", "", "param.=0", ""), widths, esp))
writeLines(sep1)
for (i in 1:(ng - 1)) {
writeLines(Row(c("mean", "", "", "", "", ""), widths, esp))
for (j in (indcum[i] + 1):(indcum[i] + nbeta[i])) {
writeLines(Row(dfprint[j, ], widths, esp))
}
if (ndelta != 0) {
for (j in (indcum[i] + nbeta[i] + nphi[i] + 1):(indcum[i] +
nbeta[i] +
nphi[i] +
ndelta)) {
writeLines(Row(dfprint[j, ], widths, esp))
}
}
writeLines(Row(c("var.", "", "", "", "", ""), widths, esp))
for (j in (indcum[i] + nbeta[i] + 1):(indcum[i] + nbeta[i] + nphi[i])) {
writeLines(Row(dfprint[j, ], widths, esp))
}
writeLines(sep2)
}
writeLines(Row(c("mean", "", "", "", "", ""), widths, esp))
for (j in 1:(nbeta[ng])) {
writeLines(Row(dfprint[indcum[ng] + j, ], widths, esp))
}
if (ndelta != 0) {
for (j in (nbeta[ng] + nphi[ng] + 1):(nbeta[ng] + nphi[ng] + ndelta)) {
writeLines(Row(dfprint[indcum[ng] + j, ], widths, esp))
}
}
writeLines(Row(c("var.", "", "", "", "", ""), widths, esp))
for (j in (indcum[ng] + nbeta[ng] + 1):(indcum[ng] + nbeta[ng] + nphi[ng])) {
writeLines(Row(dfprint[j, ], widths, esp))
}
writeLines(sep1)
if (ntheta == 1) {
for (i in 1:ng) {
if (ndelta != 0) {
writeLines(Row(
dfprint[sum(nbeta) + sum(nphi) + ndelta * ng + i, ],
widths,
esp
))
} else {
writeLines(Row(
dfprint[sum(nbeta) + sum(nphi) + ndelta * ng + i, ],
widths,
esp
))
}
}
} else {
writeLines(Row(dfprint[indcum[ng + 1] + 1, ], widths, esp))
writeLines(sep2)
if (ng > 2) {
for (i in 2:(ng - 1)) {
for (j in ((i - 2) * ntheta + 1):((i - 1) * ntheta)) {
writeLines(Row(dfprint[indcum[ng + 1] + 1 + j, ], widths, esp))
}
writeLines(sep2)
}
for (j in 1:ntheta) {
writeLines(Row(
dfprint[indcum[ng + 1] + (ng - 2) * ntheta + 1 + j, ],
widths,
esp
))
}
} else {
for (j in 1:ntheta) {
writeLines(Row(dfprint[indcum[ng + 1] + 1 + j, ], widths, esp))
}
}
}
writeLines(sep1)
cat("Likelihood :", Obj$Likelihood)
}
#' @title Print Method for a `trajectory.mult` Object
#'
#' @description Displays a summary of a multivariate (joint) trajectory model fitted by `trajeR_mult`.
#'
#' @param x An object of class `trajectory.mult`.
#' @param ... Further arguments passed to or from other methods.
#' @return Invisibly returns the original object `x`.
#' @export
print.trajectory.mult <- function(x, ...) {
cat(sprintf("Call Multi-TrajeR with %d joint models.\n", length(x$models)))
cat(sprintf("Joint Likelihood : %.3f\n", x$likelihood))
conv_msg <- if (x$convergence %in% c(0, 1, 2, 3, 4)) {
sprintf("Successful (Code %d)", x$convergence)
} else if (x$convergence < 0) {
sprintf("Warning (Code %d)", x$convergence)
} else {
sprintf("Warning (Code %d)", x$convergence)
}
cat(sprintf("Convergence : %s\n\n", conv_msg))
deg_names <- c(
"Intercept",
"Linear",
"Quadratic",
"Cubic",
"Quartic",
"Quintic",
"Sextic",
"Septic"
)
print_row <- function(grp, param_name, est, se) {
if (is.null(se) || is.na(se)) {
cat(sprintf(
"%7s %11s %10.5f NA NA NA \n",
grp,
param_name,
est
))
} else {
t_val <- est / se
p_val <- 2 * (1 - stats::pnorm(abs(t_val)))
cat(sprintf(
"%7s %11s %10.5f %10.5f %10.5f %10.5f \n",
grp,
param_name,
est,
se,
t_val,
p_val
))
}
}
U <- sum(unlist(x$ng))
all_marginal_probs <- rep(NA, U)
se_marginal_probs <- rep(NA, U)
prob_vector_idx_map <- list()
u_idx <- 1
for (m_idx in 1:length(x$ng)) {
prob_vector_idx_map[[m_idx]] <- u_idx:(u_idx + x$ng[[m_idx]] - 1)
u_idx <- u_idx + x$ng[[m_idx]]
}
current_risk_estimates <- c(
unlist(lapply(x$models, function(m) m$theta)),
x$psi
)
P <- length(current_risk_estimates)
if (P > 0) {
mk_1 <- as.matrix(expand.grid(lapply(x$ng, function(g) 1:g)))
L <- nrow(mk_1)
thetas_list <- list()
idx_curr <- 1
for (m_idx in 1:length(x$ng)) {
n_t <- x$ng[[m_idx]] - 1
if (n_t > 0) {
thetas_list[[m_idx]] <- current_risk_estimates[
idx_curr:(idx_curr + n_t - 1)
]
idx_curr <- idx_curr + n_t
} else {
thetas_list[[m_idx]] <- numeric(0)
}
}
psis <- if (P >= idx_curr) {
current_risk_estimates[idx_curr:P]
} else {
numeric(0)
}
psi_grid <- as.matrix(expand.grid(lapply(x$ng, function(g) {
if (g > 1) 2:g else integer(0)
})))
scores <- numeric(L)
for (l in 1:L) {
t_score <- 0
for (m_idx in 1:length(x$ng)) {
g_m <- mk_1[l, m_idx]
if (g_m > 1) t_score <- t_score + thetas_list[[m_idx]][g_m - 1]
}
p_score <- 0
if (length(psis) > 0 && all(mk_1[l, ] > 1)) {
match_idx <- which(apply(psi_grid, 1, function(row) {
all(row == mk_1[l, ])
}))
if (length(match_idx) > 0) p_score <- psis[match_idx]
}
scores[l] <- t_score + p_score
}
exp_scores <- exp(scores)
joint_probs <- exp_scores / sum(exp_scores)
u_idx <- 1
for (m_idx in 1:length(x$ng)) {
for (g in 1:x$ng[[m_idx]]) {
matching_rows <- which(mk_1[, m_idx] == g)
all_marginal_probs[u_idx] <- sum(joint_probs[matching_rows])
u_idx <- u_idx + 1
}
}
V_full <- x$varcov
if (is.null(V_full)) {
V_full <- x$varcov_mat
}
if (is.null(V_full) && !is.null(x$hessian)) {
V_full <- tryCatch(
{
solve(-x$hessian)
},
error = function(e) {
tryCatch(MASS::ginv(-x$hessian), error = function(e2) NULL)
}
)
}
target_ses <- c(unlist(lapply(x$models, function(m) m$se_theta)), x$se_psi)
if (!is.null(V_full) && length(target_ses) == P) {
raw_SE <- sqrt(diag(V_full))
risk_indices <- rep(NA, P)
used_indices <- c()
for (i in 1:P) {
tgt <- target_ses[i]
if (!is.na(tgt) && !is.null(tgt)) {
diffs <- abs(raw_SE - tgt)
diffs[used_indices] <- Inf
best_idx <- which.min(diffs)
if (diffs[best_idx] < 1e-3) {
risk_indices[i] <- best_idx
used_indices <- c(used_indices, best_idx)
}
}
}
if (!any(is.na(risk_indices))) {
V_risk <- V_full[risk_indices, risk_indices, drop = FALSE]
D <- matrix(0, nrow = L, ncol = L)
for (l in 1:L) {
for (lp in 1:L) {
if (l == lp) {
D[l, l] <- joint_probs[l] * (1 - joint_probs[l])
} else {
D[l, lp] <- -joint_probs[l] * joint_probs[lp]
}
}
}
M <- matrix(0, nrow = L, ncol = P)
for (l in 1:L) {
j <- 1
for (m_idx in 1:length(x$ng)) {
n_t <- x$ng[[m_idx]] - 1
if (n_t > 0) {
g_m <- mk_1[l, m_idx]
for (g_theta in 1:n_t) {
if (g_m == (g_theta + 1)) {
M[l, j] <- 1
}
j <- j + 1
}
}
}
if (length(psis) > 0 && all(mk_1[l, ] > 1)) {
match_idx <- which(apply(psi_grid, 1, function(row) {
all(row == mk_1[l, ])
}))
if (length(match_idx) > 0) M[l, j + match_idx - 1] <- 1
}
}
J_joint <- D %*% M
A_mat <- matrix(0, nrow = U, ncol = L)
u_idx <- 1
for (m_idx in 1:length(x$ng)) {
for (g in 1:x$ng[[m_idx]]) {
A_mat[u_idx, which(mk_1[, m_idx] == g)] <- 1
u_idx <- u_idx + 1
}
}
J_marginal <- A_mat %*% J_joint
V_prop <- J_marginal %*% V_risk %*% t(J_marginal)
diag_V <- diag(V_prop)
diag_V[diag_V < 0] <- NA
se_marginal_probs <- sqrt(diag_V)
}
}
}
for (m in 1:length(x$models)) {
mod <- x$models[[paste0("Model_", m)]]
ng_m <- x$ng[[m]]
model_type <- x$model_types[m]
cat(sprintf(">>> MODEL %d : %s (%d groups) <<<\n", m, model_type, ng_m))
cat(
" group Parameter Estimate Std. Error T for H0: Prob>|T| \n"
)
cat(
" param.=0 \n"
)
cat(
"--------------------------------------------------------------------\n"
)
has_risk_covariates <- length(mod$theta) > (ng_m - 1)
if (!has_risk_covariates && !any(is.na(all_marginal_probs))) {
for (g in 1:ng_m) {
p_idx <- prob_vector_idx_map[[m]][g]
print_row(
sprintf(" %d", g),
sprintf("Prob(G%d)", g),
all_marginal_probs[p_idx],
se_marginal_probs[p_idx]
)
}
} else {
print_row(" 1", "theta1", 0.0, NA)
if (ng_m > 1) {
for (idx_t in 1:length(mod$theta)) {
se_t <- if (!is.null(mod$se_theta)) mod$se_theta[idx_t] else NA
print_row(
sprintf(" %d", idx_t + 1),
sprintf("theta%d", idx_t + 1),
mod$theta[idx_t],
se_t
)
}
}
}
cat(
" \n"
)
beta_idx <- 1
nu_idx <- 1
sigma_idx <- 1
phi_idx <- 1
for (g in 1:ng_m) {
n_b <- x$lnbeta[[m]][g]
for (b_i in 1:n_b) {
p_name <- deg_names[b_i]
se_b <- if (!is.null(mod$se_beta)) mod$se_beta[beta_idx] else NA
print_row(sprintf(" %d", g), p_name, mod$beta[beta_idx], se_b)
beta_idx <- beta_idx + 1
}
if (model_type == "ZIP" && length(mod$nu) > 1) {
n_nu_g <- if (!is.null(x$lnnu[[m]])) {
x$lnnu[[m]][g]
} else {
(length(mod$nu) / ng_m)
}
if (n_nu_g > 0 && (n_nu_g == floor(n_nu_g))) {
for (i in 1:n_nu_g) {
se_n <- if (!is.null(mod$se_nu)) mod$se_nu[nu_idx] else NA
print_row(
sprintf(" %d", g),
sprintf("nu%d", nu_idx),
mod$nu[nu_idx],
se_n
)
nu_idx <- nu_idx + 1
}
}
}
if (model_type == "CNORM" && length(mod$sigma) > 1) {
n_sig_g <- if (!is.null(x$lnsigma[[m]])) {
x$lnsigma[[m]][g]
} else {
(length(mod$sigma) / ng_m)
}
if (n_sig_g > 0 && (n_sig_g == floor(n_sig_g))) {
for (i in 1:n_sig_g) {
se_s <- if (!is.null(mod$se_sigma)) mod$se_sigma[sigma_idx] else NA
print_row(
sprintf(" %d", g),
sprintf("sigma%d", sigma_idx),
mod$sigma[sigma_idx],
se_s
)
sigma_idx <- sigma_idx + 1
}
}
}
if (model_type == "BETA" && length(mod$phi) > 1) {
n_phi_g <- if (!is.null(x$lnphi[[m]])) {
x$lnphi[[m]][g]
} else {
(length(mod$phi) / ng_m)
}
if (n_phi_g > 0 && (n_phi_g == floor(n_phi_g))) {
for (i in 1:n_phi_g) {
se_p <- if (!is.null(mod$se_phi)) mod$se_phi[phi_idx] else NA
print_row(
sprintf(" %d", g),
sprintf("phi%d", phi_idx),
mod$phi[phi_idx],
se_p
)
phi_idx <- phi_idx + 1
}
}
}
cat(
" \n"
)
}
cat(
"--------------------------------------------------------------------\n"
)
has_global_param <- FALSE
if (model_type == "ZIP") {
if (length(mod$nu) == 1) {
se_n <- if (!is.null(mod$se_nu)) mod$se_nu[1] else NA
print_row(" All", "nu1", mod$nu[1], se_n)
has_global_param <- TRUE
} else if (nu_idx <= length(mod$nu)) {
for (i in nu_idx:length(mod$nu)) {
se_n <- if (!is.null(mod$se_nu)) mod$se_nu[i] else NA
print_row(sprintf(" %d", i), sprintf("nu%d", i), mod$nu[i], se_n)
has_global_param <- TRUE
}
}
}
if (model_type == "CNORM") {
if (length(mod$sigma) == 1) {
se_s <- if (!is.null(mod$se_sigma)) mod$se_sigma[1] else NA
print_row(" All", "sigma1", mod$sigma[1], se_s)
has_global_param <- TRUE
} else if (sigma_idx <= length(mod$sigma)) {
for (i in sigma_idx:length(mod$sigma)) {
se_s <- if (!is.null(mod$se_sigma)) mod$se_sigma[i] else NA
print_row(
sprintf(" %d", i),
sprintf("sigma%d", i),
mod$sigma[i],
se_s
)
has_global_param <- TRUE
}
}
}
if (model_type == "BETA") {
if (length(mod$phi) == 1) {
se_p <- if (!is.null(mod$se_phi)) mod$se_phi[1] else NA
print_row(" All", "phi1", mod$phi[1], se_p)
has_global_param <- TRUE
} else if (phi_idx <= length(mod$phi)) {
for (i in phi_idx:length(mod$phi)) {
se_p <- if (!is.null(mod$se_phi)) mod$se_phi[i] else NA
print_row(
sprintf(" %d", i),
sprintf("phi%d", i),
mod$phi[i],
se_p
)
has_global_param <- TRUE
}
}
}
if (has_global_param) {
cat(
"--------------------------------------------------------------------\n\n"
)
} else {
cat("\n")
}
}
if (!is.null(x$psi) && length(x$psi) > 0) {
cat(">>> JOINT INTERACTION PARAMETERS (Psi) <<<\n")
cat(
" Parameter Estimate Std. Error T for H0: Prob>|T| \n"
)
cat(
"--------------------------------------------------------------------\n"
)
psi_labels <- c()
n_mod <- length(x$models)
if (n_mod > 1) {
for (m1 in 1:(n_mod - 1)) {
for (m2 in (m1 + 1):n_mod) {
if (x$ng[[m1]] > 1 && x$ng[[m2]] > 1) {
for (g1 in 2:x$ng[[m1]]) {
for (g2 in 2:x$ng[[m2]]) {
psi_labels <- c(
psi_labels,
sprintf("psi^%d%d_%d%d", m1, m2, g1, g2)
)
}
}
}
}
}
}
if (length(psi_labels) != length(x$psi)) {
psi_labels <- paste0("psi", 1:length(x$psi))
}
for (p_i in 1:length(x$psi)) {
se_p <- if (!is.null(x$se_psi)) x$se_psi[p_i] else NA
label <- psi_labels[p_i]
if (is.null(se_p) || is.na(se_p)) {
cat(sprintf(
"%19s %10.5f NA NA NA \n",
label,
x$psi[p_i]
))
} else {
t_val <- x$psi[p_i] / se_p
p_val <- 2 * (1 - stats::pnorm(abs(t_val)))
cat(sprintf(
"%19s %10.5f %10.5f %10.5f %10.5f \n",
label,
x$psi[p_i],
se_p,
t_val,
p_val
))
}
}
cat(
"--------------------------------------------------------------------\n"
)
}
invisible(x)
}
Any scripts or data that you put into this service are public.
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.