## ----eval=FALSE---------------------------------------------------------------
# install.packages("tsdf")

## -----------------------------------------------------------------------------
library(tsdf)

## -----------------------------------------------------------------------------
# type I errors
alpha1 <- 0.15
alpha2 <- 0.10

# type II error
beta <- 0.15

# response rates
pc <- 0.25
pe <- pc + 0.20

# two-stage design
out <- opt.design(alpha1, alpha2, beta, pc, pe, stage = 2)

## -----------------------------------------------------------------------------
print(out)

## ----eval=FALSE---------------------------------------------------------------
# opt.design(alpha1, alpha2, beta, pc, pe, stage = 2, sf.param = 1)

## -----------------------------------------------------------------------------
# cohort sizes at each stage
n <- rep(3, 3)

# type I errors
alpha.l <- 0.6
alpha.r <- 0.4
alpha.u <- 0.2

# target toxicity
pt <- 0.3

# generate the decision table
out <- dec.table(alpha.l, alpha.r, alpha.u, pt, n)

## -----------------------------------------------------------------------------
print(out)

## -----------------------------------------------------------------------------
plot(out)

## -----------------------------------------------------------------------------
# true toxicity probabilities
truep <- c(0.3, 0.45, 0.5, 0.6)

# generate a decision table
dt <- dec.table(0.6, 0.4, 0.2, 0.3, c(3, 3, 3))

# convert the table object to a plain matrix for simulation
sim_table <- matrix(
  as.character(dt$table),
  nrow = nrow(dt$table),
  dimnames = dimnames(dt$table)
)

# run one simulation scenario
out1 <- dec.sim(truep, sim_table, start.level = 2, nsim = 1000)

## -----------------------------------------------------------------------------
test.file <- system.file("extdata", "testS.csv", package = "tsdf")

## -----------------------------------------------------------------------------
out2 <- sl.sim(sim_table, test.file)

## -----------------------------------------------------------------------------
# target toxicity for each scenario
pt <- c(0.3, 0.4)
summary(out2, pt)

## -----------------------------------------------------------------------------
# scenario information (true toxicity)
plot(out2, s = 2, pt = c(0.3, 0.4), type = "s")

# probability of selecting each dose as the MTD
plot(out2, s = 2, pt = c(0.3, 0.4), type = "prob")

# average number of patients treated at each dose
plot(out2, s = 2, pt = c(0.3, 0.4), type = "np")

# average number of DLTs at each dose
plot(out2, s = 2, pt = c(0.3, 0.4), type = "dlt")

## ----fig.height = 8-----------------------------------------------------------
plot(out2, pt = c(0.3, 0.4), type = "all", cex = 0.7)

## ----echo=FALSE, results='asis'-----------------------------------------------
sl <- system.file("extdata", "testS.csv", package = "tsdf")
knitr::kable(read.table(sl, header = TRUE, sep = ","))

## ----warning=FALSE------------------------------------------------------------
table.file <- system.file("extdata", "decTable.csv", package = "tsdf")
dec <- read.csv(table.file, row.names = 1, check.names = FALSE)
colnames(dec)

## ----echo=FALSE---------------------------------------------------------------
knitr::kable(dec)

