1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468
|
\name{summaryM}
\alias{summaryM}
\alias{print.summaryM}
\alias{plot.summaryM}
\alias{latex.summaryM}
\title{Summarize Mixed Data Types vs. Groups}
\description{
\code{summaryM} summarizes the variables listed in an S formula,
computing descriptive statistics and optionally statistical tests for
group differences. This function is typically used when there are
multiple left-hand-side variables that are independently against by
groups marked by a single right-hand-side variable. The summary
statistics may be passed to \code{print} methods, \code{plot} methods
for making annotated dot charts and extended box plots, and
\code{latex} methods for typesetting tables using LaTeX.
The \code{print} methods use the \code{print.char.matrix} function to
print boxed tables.
Continuous variables are described by three quantiles (quartiles by
default) when printing, or by the following quantiles when plotting
expended box plots using the \code{\link{bpplt}} function:
0.05, 0.125, 0.25, 0.375, 0.5, 0.625, 0.75, 0.875, 0.95. The box
plots are scaled to the 0.025 and 0.975 quantiles of each continuous
left-hand-side variable. Categorical variables are
described by counts and percentages.
The left hand side of \code{formula} may contain \code{mChoice}
("multiple choice") variables. When \code{test=TRUE} each choice is
tested separately as a binary categorical response.
The \code{plot} method for \code{method="reverse"} creates a temporary
function \code{Key} as is done by the \code{xYplot} and
\code{Ecdf.formula} functions. After \code{plot}
runs, you can type \code{Key()} to put a legend in a default location, or
e.g. \code{Key(locator(1))} to draw a legend where you click the left
mouse button. This key is for categorical variables, so to have the
opportunity to put the key on the graph you will probably want to use
the command \code{plot(object, which="categorical")}. A second function
\code{Key2} is created if continuous variables are being plotted. It is
used the same as \code{Key}. If the \code{which} argument is not
specified to \code{plot}, two pages of plots will be produced. If you
don't define \code{par(mfrow=)} yourself,
\code{plot.summaryM} will try to lay out a multi-panel
graph to best fit all the individual charts for continuous
variables.
}
\usage{
summaryM(formula, groups=NULL, data=NULL, subset, na.action=na.retain,
overall=FALSE, continuous=10, na.include=FALSE,
quant=c(0.025, 0.05, 0.125, 0.25, 0.375, 0.5, 0.625,
0.75, 0.875, 0.95, 0.975),
nmin=100, test=FALSE,
conTest=conTestkw, catTest=catTestchisq,
ordTest=ordTestpo)
\method{print}{summaryM}(x, digits, prn = any(n != N),
what=c('proportion', '\%'), pctdig = if(what == '\%') 0 else 2,
npct = c('numerator', 'both', 'denominator', 'none'),
exclude1 = TRUE, vnames = c('labels', 'names'), prUnits = TRUE,
sep = '/', abbreviate.dimnames = FALSE,
prefix.width = max(nchar(lab)), min.colwidth, formatArgs=NULL, round=NULL,
prtest = c('P','stat','df','name'), prmsd = FALSE, long = FALSE,
pdig = 3, eps = 0.001, \dots)
\method{plot}{summaryM}(x, vnames = c('labels', 'names'),
what = c('proportion', '\%'),
which = c('both', 'categorical', 'continuous'),
xlim = if(what == 'proportion') c(0,1)
else c(0,100),
xlab = if(what=='proportion') 'Proportion'
else 'Percentage',
pch = c(16, 1, 2, 17, 15, 3, 4, 5, 0), exclude1 = TRUE,
main, subtitles = TRUE,
prtest = c('P', 'stat', 'df', 'name'), pdig = 3, eps = 0.001,
conType = c('bp', 'dot', 'raw'), cex.means = 0.5, cex=par('cex'), \dots)
\method{latex}{summaryM}(object, title =
first.word(deparse(substitute(object))),
file=paste(title, 'tex', sep='.'), append=FALSE, digits,
prn = any(n != N), what=c('proportion', '\%'),
pctdig = if(what == '\%') 0 else 2,
npct = c('numerator', 'both', 'denominator', 'none'),
npct.size = 'scriptsize', Nsize = 'scriptsize', exclude1 = TRUE,
vnames=c("labels", "names"), prUnits = TRUE, middle.bold = FALSE,
outer.size = "scriptsize", caption, rowlabel = "",
insert.bottom = TRUE, dcolumn = FALSE, formatArgs=NULL, round=NULL,
prtest = c('P', 'stat', 'df', 'name'), prmsd = FALSE,
msdsize = NULL, long = FALSE, pdig = 3, eps = 0.001,
auxCol = NULL, table.env=TRUE, \dots)
}
\arguments{
\item{formula}{
An S formula with additive effects. There may be several variables
on the right hand side separated by "+",
or the numeral \code{1}, indicating that
there is no grouping variable so that only margin summaries are
produced. The right hand side variable, if present, must be a
discrete variable producing a limited number of groups. On the
left hand side there may be any number of variables, separated by
"+", and these may be of mixed types. These variables are analyzed
separately by the grouping variable.
}
\item{groups}{if there is more than one right-hand variable, specify
\code{groups} as a character string containing the name of the
variable used to produce columns of the table. The remaining right
hand variables are combined to produce levels that cause separate
tables or plots to be produced.}
\item{x}{an object created by \code{summaryM}. For
\code{conTestkw} a numeric vector, and for \code{ordTestpo}, a numeric
or factor variable that can be considered ordered}
\item{data}{
name or number of a data frame. Default is the current frame.
}
\item{subset}{
a logical vector or integer vector of subscripts used to specify the
subset of data to use in the analysis. The default is to use all
observations in the data frame.
}
\item{na.action}{
function for handling missing data in the input data. The default is
a function defined here called \code{na.retain}, which keeps all
observations for processing, with missing variables or not.
}
\item{overall}{
Setting \code{overall=TRUE} makes a new column with
overall statistics for the whole sample. If \code{test=TRUE} these
marginal statistics are ignored in doing statistical tests.
}
\item{continuous}{
specifies the threshold for when a variable is considered to be
continuous (when there are at least \code{continuous} unique values).
\code{factor} variables are always considered to be categorical no matter
how many levels they have.
}
\item{na.include}{
Set \code{na.include=TRUE} to keep missing values of categorical
variables from being excluded from the table.
}
\item{nmin}{
For categories of the response variable in which there
are less than or equal to \code{nmin} non-missing observations, the raw
data are retained for later plotting in place of box plots.
}
\item{test}{
Set to \code{TRUE} to compute test
statistics using tests specified in \code{conTest} and \code{catTest}.
}
\item{conTest}{
a function of two arguments (grouping variable and a continuous
variable) that returns a list with components \code{P} (the computed
P-value), \code{stat} (the test statistic, either chi-square or F),
\code{df} (degrees of freedom), \code{testname} (test name), \code{statname}
(statistic name), an optional component \code{latexstat} (LaTeX
representation of \code{statname}), an optional component
\code{plotmathstat} (for R - the \code{plotmath} representation of
\code{statname}, as a character string), and an
optional component \code{note}
that contains a character string note about the test (e.g.,
\code{"test not done because n < 5"}). \code{conTest} is applied to
continuous variables
on the right-hand-side of the formula when \code{method="reverse"}. The
default uses the \code{spearman2} function to run the Wilcoxon or
Kruskal-Wallis test using the F distribution.
}
\item{catTest}{
a function of a frequency table (an integer matrix) that returns a
list with the same components as created by \code{conTest}. By default,
the Pearson chi-square test is done, without continuity correction
(the continuity correction would make the test conservative like the
Fisher exact test).
}
\item{ordTest}{
a function of a frequency table (an integer matrix) that returns a
list with the same components as created by \code{conTest}. By default,
the Proportional odds likelihood ratio test is done.
}
\item{...}{
For \code{Key} and \code{Key2} these arguments are passed to \code{key},
\code{text}, or \code{mtitle}. For \code{print} methods these are
optional arguments to \code{print.char.matrix}. For \code{latex} methods
these are passed to \code{latex.default}.
}
\item{object}{an object created by \code{summaryM}}
\item{quant}{
vector of quantiles to use for summarizing continuous variables.
These must be numbers between 0 and 1
inclusive and must include the numbers 0.5, 0.25, and 0.75 which are
used for printing and for plotting
quantile intervals. The outer quantiles are used for scaling the x-axes
for such plots. Specify outer quantiles as \code{0} and \code{1} to
scale the x-axes using the whole observed data ranges instead of the
default (a 0.95 quantile interval). Box-percentile plots are drawn
using all but the outer quantiles.
}
\item{vnames}{
By default, tables and plots are usually labeled with variable labels
(see the \code{label} and \code{sas.get} functions). To use the shorter
variable names, specify \code{vnames="name"}.
}
\item{pch}{
vector of plotting characters to represent different groups, in order
of group levels.
}
\item{abbreviate.dimnames}{see \code{print.char.matrix}}
\item{prefix.width}{see \code{print.char.matrix}}
\item{min.colwidth}{
minimum column width to use for boxes printed with \code{print.char.matrix}.
The default is the maximum of the minimum column label length and
the minimum length of entries in the data cells.
}
\item{formatArgs}{
a list containing other arguments to pass to \code{format.default} such as
\code{scientific}, e.g., \code{formatArgs=list(scientific=c(-5,5))}. For
\code{print.summary.formula.reverse} and
\code{format.summary.formula.reverse}, \code{formatArgs} applies only to
statistics computed on continuous variables, not to percents,
numerators, and denominators. The \code{round} argument may be preferred.
}
\item{digits}{
number of significant digits to print. Default is to use the current
value of the \code{digits} system option.
}
\item{what}{specifies whether proportions or percentages are to be
printed, plotted, or LaTeX'd}
\item{pctdig}{
number of digits to the right of the decimal place for printing
percentages or proportions. The default is zero if \code{what='\%'},
so percents will be rounded to the nearest percent. The default is
2 for proportions.
}
\item{prn}{
set to \code{TRUE} to print the number of non-missing observations on the
current (row) variable. The default is to print these only if any of
the counts of non-missing values differs from the total number of
non-missing values of the left-hand-side variable.
}
\item{npct}{
specifies which counts are to be printed to the right of percentages.
The default is to print the frequency (numerator of the percent) in
parentheses. You can specify \code{"both"} to print both numerator and
denominator, \code{"denominator"}, or \code{"none"}.
}
\item{npct.size}{
the size for typesetting \code{npct} information which appears after
percents. The default is \code{"scriptsize"}.
}
\item{Nsize}{
When a second row of column headings is added showing sample sizes,
\code{Nsize} specifies the LaTeX size for these subheadings. Default
is \code{"scriptsize"}.
}
\item{exclude1}{
By default, \code{summaryM} objects will be printed, plotted, or typeset by
removing redundant entries from percentage tables for categorical
variables. For example, if you print the percent of females, you
don't need to print the percent of males. To override this, set
\code{exclude1=FALSE}.
}
\item{prUnits}{
set to \code{FALSE} to suppress printing or latexing \code{units}
attributes of variables, when \code{method='reverse'} or \code{'response'}
}
\item{sep}{
character to use to separate quantiles when printing tables
}
\item{prtest}{
a vector of test statistic components to print if \code{test=TRUE} was in
effect when \code{summaryM} was called. Defaults to printing all
components. Specify \code{prtest=FALSE} or \code{prtest="none"} to not
print any tests. This applies to \code{print}, \code{latex}, and
\code{plot} methods.
}
\item{round}{
Specify \code{round} to round
the quantiles and optional mean and standard deviation to
\code{round} digits after the decimal point. Set \code{round='auto'}
to try an automatic choice.
}
\item{prmsd}{
set to \code{TRUE} to print mean and SD after the three quantiles, for
continuous variables
}
\item{msdsize}{
defaults to \code{NULL} to use the current font size for the mean and
standard deviation if \code{prmsd} is \code{TRUE}. Set to a character
string to specify an alternate LaTeX font size.
}
\item{long}{
set to \code{TRUE} to print the results for the first category on its own
line, not on the same line with the variable label
}
\item{pdig}{
number of digits to the right of the decimal place for printing
P-values. Default is \code{3}. This is passed to \code{format.pval}.
}
\item{eps}{
P-values less than \code{eps} will be printed as \code{< eps}. See
\code{format.pval}.
}
\item{auxCol}{
an optional auxiliary column of information, right justified, to add
in front of statistics typeset by
\code{latex.summaryM}. This argument is a list with a
single element that has a name specifying the column heading. If this
name includes a newline character, the portions of the string before
and after the newline form respectively the main heading and the
subheading (typically set in smaller font), respectively. See the
\code{extracolheads} argument to \code{latex.default}. \code{auxCol}
is filled with blanks when a variable being summarized takes up more
than one row in the output. This happens with categorical variables.
}
\item{table.env}{set to \code{FALSE} to use \code{tabular} environment
with no caption}
\item{which}{
Specifies whether to plot results
for categorical variables, continuous variables, or both (the default).
}
\item{conType}{
For drawing plots for continuous variables,
extended box plots (box-percentile-type plots) are drawn by default,
using all quantiles in \code{quant} except for the outermost ones
which are using for scaling the overall plot based on the
non-stratified marginal distribution of the current response variable.
Specify \code{conType='dot'} to draw dot plots showing the three
quartiles instead. For extended box plots, means are drawn
with a solid dot and vertical reference lines are placed at the three
quartiles. Specify \code{conType='raw'} to make a strip chart showing
the raw data. This can only be used if the sample size for each
right-hand-side group is less than or equal to \code{nmin}.}
\item{cex.means}{
character size for means in box-percentile plots; default is .5}
\item{cex}{character size for other plotted items}
\item{xlim}{
vector of length two specifying x-axis limits. This is only used
for plotting categorical variables. Limits for continuous
variables are determined by the outer quantiles specified in
\code{quant}.
}
\item{xlab}{x-axis label}
\item{main}{
a main title. This applies only to the plot
for categorical variables.
}
\item{subtitles}{
set to \code{FALSE} to suppress automatic subtitles
}
\item{caption}{
character string containing LaTeX table captions.
}
\item{title}{
name of resulting LaTeX file omitting the \code{.tex} suffix. Default
is the name of the \code{summary} object. If \code{caption} is specied,
\code{title} is also used for the table's symbolic reference label.
}
\item{file}{name of file to write LaTeX code to. Specifying
\code{file=""} will cause LaTeX code to just be printed to
standard output rather than be stored in a permanent file.
}
\item{append}{specify \code{TRUE} to add code to an existing file}
\item{rowlabel}{
see \code{latex.default} (under the help file \code{latex})
}
\item{middle.bold}{
set to \code{TRUE} to have LaTeX use bold face for the middle
quantile
}
\item{outer.size}{the font size for outer quantiles
}
\item{insert.bottom}{
set to \code{FALSE} to suppress inclusion of definitions placed at the
bottom of LaTeX tables. You can also specify a character string
containing other text that overrides the automatic text.
}
\item{dcolumn}{see \code{latex}}
}
\value{
a list. \code{plot.summaryM} returns the number
of pages of plots that were made.
}
\section{Side Effects}{
\code{plot.summaryM} creates a function \code{Key} and
\code{Key2} in frame 0 that will draw legends.
}
\author{
Frank Harrell
\cr
Department of Biostatistics
\cr
Vanderbilt University
\cr
\email{f.harrell@vanderbilt.edu}
}
\references{
Harrell FE (2004): Statistical tables and plots using S and LaTeX.
Document available from \url{http://biostat.mc.vanderbilt.edu/twiki/pub/Main/StatReport/summary.pdf}.
}
\seealso{
\code{\link{mChoice}}, \code{\link{label}}, \code{\link{dotchart3}},
\code{\link{print.char.matrix}}, \code{\link{update}},
\code{\link{formula}},
\code{\link{format.default}}, \code{\link{latex}},
\code{\link{latexTranslate}}, \code{\link{bpplt}},
\code{\link{tabulr}}, \code{\link{bpplotM}}, \code{\link{summaryP}}
}
\examples{
options(digits=3)
set.seed(173)
sex <- factor(sample(c("m","f"), 500, rep=TRUE))
country <- factor(sample(c('US', 'Canada'), 500, rep=TRUE))
age <- rnorm(500, 50, 5)
sbp <- rnorm(500, 120, 12)
label(sbp) <- 'Systolic BP'
units(sbp) <- 'mmHg'
treatment <- factor(sample(c("Drug","Placebo"), 500, rep=TRUE))
treatment[1]
sbp[1] <- NA
# Generate a 3-choice variable; each of 3 variables has 5 possible levels
symp <- c('Headache','Stomach Ache','Hangnail',
'Muscle Ache','Depressed')
symptom1 <- sample(symp, 500,TRUE)
symptom2 <- sample(symp, 500,TRUE)
symptom3 <- sample(symp, 500,TRUE)
Symptoms <- mChoice(symptom1, symptom2, symptom3, label='Primary Symptoms')
table(as.character(Symptoms))
# Note: In this example, some subjects have the same symptom checked
# multiple times; in practice these redundant selections would be NAs
# mChoice will ignore these redundant selections
f <- summaryM(age + sex + sbp + Symptoms ~ treatment, test=TRUE)
f
# trio of numbers represent 25th, 50th, 75th percentile
print(f, long=TRUE)
plot(f)
plot(f, conType='dot', prtest='P')
bpplt() # annotated example showing layout of bp plot
# Produce separate tables by country
f <- summaryM(age + sex + sbp + Symptoms ~ treatment + country,
groups='treatment', test=TRUE)
f
\dontrun{
getHdata(pbc)
s5 <- summaryM(bili + albumin + stage + protime + sex +
age + spiders ~ drug, data=pbc)
print(s5, npct='both')
# npct='both' : print both numerators and denominators
plot(s5, which='categorical')
Key(locator(1)) # draw legend at mouse click
par(oma=c(3,0,0,0)) # leave outer margin at bottom
plot(s5, which='continuous') # see also bpplotM
Key2() # draw legend at lower left corner of plot
# oma= above makes this default key fit the page better
options(digits=3)
w <- latex(s5, npct='both', here=TRUE, file='')
}
}
\keyword{category}
\keyword{interface}
\keyword{hplot}
\keyword{manip}
\concept{grouping}
\concept{stratification}
\concept{aggregation}
|