# R code for inference from two-stage adaptive design

• Koyama T and Chen H. ``Proper inference from Simon's two-stage designs'' Statistics in Medicine 27(16), 2008. PMID: 17960777. PMCID: PMC6047527.
• The main page
• twostage2020Web.R: R code: Inference from two-stage designs

# R code for the Length of the Beatles' Songs plot

kmplot()

tplot()

jmplot()

dsplot()

showcolors()

## kmplot()

• Kaplan-Meier survival plot – with at-risk table
• plot Kaplan-Meier curves with 'numbers at risk' at bottom margin.
• can specify at which time n at-risk is displayed
• can specify line type / line width / line color for each line
• can change the order of appearance in the n at-risk table
• n.at.risk numbers are right-justified!
• simple=TRUE suppresses the 'at risk' table.

#### Example

```require(survival)

kma <- survfit( Surv(time, status) ~ rx + adhere, data=colon )

kmplot(kma, mark='', simple=FALSE,
xaxis.at=c(0,.5,1:9)*365, xaxis.lab=c(0,.5,1:9), # n.risk.at
lty.surv=c(1,2), lwd.surv=1, col.surv=c(1,1,2,2,4,4), # survival.curves
col.ci=0, # confidence intervals not plotted
group.order=c(5,3,1,6,4,2), # order of appearance in the n.risk.at table and legend.
extra.left.margin=6, label.n.at.risk=FALSE, draw.lines=TRUE,
cex.axis=.8, xlab='Years', ylab='Survival Probability', # labels
grid=TRUE, lty.grid=1, lwd.grid=1, col.grid=grey(.9),
legend=TRUE, loc.legend='bottomleft',
cex.lab=.8, xaxs='r', bty='L', las=1, tcl=-.2  # other parameters passed to plot()
)
title(main='Chemotherapy for stage B/C colon cancer', adj=.1, font.main=1, line=0.5, cex.main=1)
```

## tplot()

tplot() is an alternative to boxplot(). The individual data can be shown (either in the foreground or background) with jittering if necessary.

#### Example

This example shows what options are available with tplot(). This may not be a good way to show these data (unnecessary color varieties...)
col and pch may be specified at the individual level or group level. (In this example, col is specified at the individual level, and pch is specified for groups.)
colors of boxes and box borders are specified by boxcol and boxborder.
boxplot.pars is passed to boxplot().

```set.seed(100)
age <- rnorm(80, rep(c(26,36), c(70,10)), 4)
sex <- factor( sample(c('Female','Male'), 80, TRUE) )
group <- paste('Group ', sample(1:4, 40, prob=c(2,5,4,1), replace=TRUE), sep='')
d <- data.frame(age, sex, group)

tplot( age ~ group, data=d,
las=1, cex=1, cex.axis=1, bty='L',
show.n=TRUE, dist=.25, jit=.05, type=c('db','db','db','d'),
group.pch=TRUE, pch=c(15,17,19,8),
col=c('darkred','darkblue')[c(sex)],
boxcol=c('lightsteelblue1','lightyellow1',grey(.9), 0), boxborder=grey(.8),
boxplot.pars=list(notch=TRUE, boxwex=.5)
)
```

## jmplot()

• Joint distribution and marginal distributions on its margins.
• requires tplot() [See above.]

#### Example

``` set.seed(274)
x <- rnorm(100, 20, 5)
y <- rexp(100)
levels <- as.factor(sample(c("Male","Female"), 100, TRUE))
jmplot(x, y, levels, col=levels, las=1, type='db', main='Some scores', jit=.02)
```

## dsplot()

• Discrete Scatter Plot for Integer-valued variables
• This creates a scatter plot (sort of) for discrete bivariate data. An alternative to sunflower plots.

#### Example

```set.seed(6)
x <- round(rnorm(400, 100, 4))
y <- round(rnorm(400, 200, 4))
sex <- sample(c('Female','Male'), 400, T)
dsplot(y ~ x, pch=19, col=1+(sex=='Female'), cex=.6, bkgr=T,
xlab='measurement 1', ylab='measurement 2', bty='L')
legend( 'bottomright', pch=19, col=1:2, legend=c('Male','Female'), cex=.8 )
```

## Show colors

In R, there are 657 named colors. The following funciton, show.colors(), shows these colors and corresponding numbers.
Find the color you like in the plot. You can find the name of the color by running >colors()[number].

```show.colors <- function(){
par(mfrow=c(1,1))
#   par(mai=c(.4,.4,.4,.4), oma=c(.2,0,0,.2))
x <- 22 ; y <- 30
plot(c(-1,x),c(-1,y), xlab='', ylab='', type='n', xaxt='n', yaxt='n', bty='n')
for(i in 1:x){for(j in 1:y){
k <- y*(i-1)+j ; co <- colors()[k]
rect(i-1, j-1, i, j, col=co, border=grey(.5) )}}

text(rep(-.5,y),(1:y)-.5, 1:y, cex=1.2-.016*y)
text((1:x)-.5, rep(-.5,x), y*(0:(x-1)), cex=1.2-.022*x)
title('col=colors()[n]')
}

> colors()[465]
[1] "mediumorchid3"
```

Topic attachments
I Attachment Action Size Date Who Comment
csv Beatlesongs072108.csv manage 10.3 K 10 Oct 2008 - 17:18 TatsukiKoyama
pdf BeatlesongsPoster.pdf manage 66.8 K 10 Oct 2008 - 17:11 TatsukiKoyama
pdf BeatlesongsPoster2.pdf manage 66.9 K 10 Oct 2008 - 18:05 TatsukiKoyama
pdf Poster3.pdf manage 69.2 K 22 Nov 2016 - 16:31 TatsukiKoyama
R RFunctions0.R manage 27.0 K 19 May 2017 - 12:09 TatsukiKoyama
R RFunctions071817.R manage 11.1 K 18 Jul 2017 - 03:07 TatsukiKoyama
R RFunctions1.R manage 28.5 K 29 Oct 2020 - 15:45 TatsukiKoyama
R RFunctions2.R manage 16.3 K 29 Oct 2020 - 15:44 TatsukiKoyama
R RFunctions3.R manage 12.8 K 29 Oct 2020 - 15:45 TatsukiKoyama
R TatsukiRcodeFormatSum.R manage 3.4 K 11 May 2017 - 00:05 TatsukiKoyama
R TatsukiRcodeMisc.R manage 5.7 K 11 May 2017 - 00:05 TatsukiKoyama
R TextBox.R manage 13.2 K 11 Apr 2014 - 11:49 TatsukiKoyama
r TextBoxExample.r manage 2.2 K 10 Oct 2008 - 16:33 TatsukiKoyama
R Three3.R manage 2.7 K 11 Feb 2020 - 11:32 TatsukiKoyama
R USEFUL.R manage 32.7 K 11 May 2017 - 00:05 TatsukiKoyama
r USEFUL.r manage 32.7 K 11 Nov 2008 - 09:20 TatsukiKoyama
csv ap21.csv manage 10.9 K 03 Aug 2011 - 12:11 TatsukiKoyama needed in computation on 3+3 designs: http://data.vanderbilt.edu/~koyamat/brew/t33.html
csv ap31.csv manage 60.9 K 03 Aug 2011 - 12:11 TatsukiKoyama
csv ap32.csv manage 90.8 K 03 Aug 2011 - 12:12 TatsukiKoyama
csv ap41.csv manage 264.1 K 03 Aug 2011 - 12:13 TatsukiKoyama
csv ap42.csv manage 325.0 K 03 Aug 2011 - 12:13 TatsukiKoyama
csv ap51.csv manage 1017.9 K 03 Aug 2011 - 12:13 TatsukiKoyama
csv ap52.csv manage 1122.6 K 03 Aug 2011 - 12:13 TatsukiKoyama
csv ap53.csv manage 1996.2 K 03 Aug 2011 - 12:13 TatsukiKoyama
csv ap61.csv manage 3622.9 K 03 Aug 2011 - 12:13 TatsukiKoyama
csv ap62.csv manage 3713.3 K 03 Aug 2011 - 12:14 TatsukiKoyama
csv ap63.csv manage 5637.9 K 03 Aug 2011 - 12:14 TatsukiKoyama
R beatlesongs2012.R manage 14.2 K 14 May 2020 - 09:58 TatsukiKoyama
csv beatlesongs2012.csv manage 15.4 K 03 Mar 2012 - 20:48 TatsukiKoyama
png dspExample.png manage 7.1 K 06 Jan 2011 - 14:56 TatsukiKoyama
R f1Scores.R manage 3.4 K 01 Aug 2022 - 15:13 TatsukiKoyama
R f1score.R manage 3.1 K 01 Aug 2022 - 14:49 TatsukiKoyama
png jmplotExample1.png manage 8.9 K 07 Dec 2011 - 16:48 TatsukiKoyama
png kmplotExample.png manage 11.6 K 17 Dec 2011 - 16:41 TatsukiKoyama
css koyama.css manage 0.3 K 03 Jan 2012 - 13:26 TatsukiKoyama
png tplotExample.png manage 6.1 K 07 Dec 2011 - 13:04 TatsukiKoyama
R twostage2018Web.R manage 0.1 K 16 Jun 2020 - 13:17 TatsukiKoyama
R twostage2020Web.R manage 5.3 K 05 Aug 2021 - 12:04 TatsukiKoyama
Topic revision: r166 - 01 Aug 2022, TatsukiKoyama

• Biostatistics Webs

Copyright © 2013-2022 by the contributing authors. All material on this collaboration platform is the property of the contributing authors.
Ideas, requests, problems regarding Vanderbilt Biostatistics Wiki? Send feedback