# NOT RUN {
set.seed(1)
d <- expand.grid(major=c('Alabama', 'Alaska', 'Arkansas'),
minor=c('East', 'West'),
group=c('Female', 'Male'),
city=0:2)
n <- nrow(d)
d$num <- round(100*runif(n))
d$denom <- d$num + round(100*runif(n))
d$x <- d$num / d$denom
d$lower <- d$x - runif(n)
d$upper <- d$x + runif(n)
with(d,
dotchartpl(x, major, minor, group, city, lower=lower, upper=upper,
big=city==0, num=num, denom=denom, xlab='x'))
# Show half-width confidence intervals for Female - Male differences
# after subsetting the data to have only one record per
# state/region/group
d <- subset(d, city == 0)
with(d,
dotchartpl(x, major, minor, group, num=num, denom=denom,
lower=lower, upper=upper, refgroup='Male')
)
n <- 500
set.seed(1)
d <- data.frame(
race = sample(c('Asian', 'Black/AA', 'White'), n, TRUE),
sex = sample(c('Female', 'Male'), n, TRUE),
treat = sample(c('A', 'B'), n, TRUE),
smoking = sample(c('Smoker', 'Non-smoker'), n, TRUE),
hypertension = sample(c('Hypertensive', 'Non-Hypertensive'), n, TRUE),
region = sample(c('North America','Europe','South America',
'Europe', 'Asia', 'Central America'), n, TRUE))
d <- upData(d, labels=c(race='Race', sex='Sex'))
dm <- addMarginal(d, region)
s <- summaryP(race + sex + smoking + hypertension ~
region + treat, data=dm)
s$region <- ifelse(s$region == 'All', 'All Regions', as.character(s$region))
with(s,
dotchartpl(freq / denom, major=var, minor=val, group=treat, mult=region,
big=region == 'All Regions', num=freq, denom=denom)
)
s2 <- s[- attr(s, 'rows.to.exclude1'), ]
with(s2,
dotchartpl(freq / denom, major=var, minor=val, group=treat, mult=region,
big=region == 'All Regions', num=freq, denom=denom)
)
# Note these plots can be created by plot.summaryP when options(grType='plotly')
# Plot hazard rates and ratios with confidence limits, on log scale
d <- data.frame(tx=c('a', 'a', 'b', 'b'),
event=c('MI', 'stroke', 'MI', 'stroke'),
count=c(10, 5, 5, 2),
exposure=c(1000, 1000, 900, 900))
# There were no zero event counts in this dataset. In general we
# want to handle that, hence the 0.5 below
d <- upData(d, hazard = pmax(0.5, count) / exposure,
selog = sqrt(1. / pmax(0.5, count)),
lower = log(hazard) - 1.96 * selog,
upper = log(hazard) + 1.96 * selog)
with(d,
dotchartpl(log(hazard), minor=event, group=tx, num=count, denom=exposure,
lower=lower, upper=upper,
fun=exp, ifun=log, op='/',
numlabel='events', denomlabel='years',
refgroup='a', xlab='Events Per Person-Year')
)
# }
Run the code above in your browser using DataLab