lodds.RdComputes (log) odds and their asymptotic variance covariance matrix for R (by strata) tables. Odds are calculated for pairs of levels of one array dimension (typically a response or focal variable) separately for each level of all stratifying dimensions. See Friendly et al. (2011) for a sketch of a general theory.
lodds(x, ...)
# Default S3 method
lodds(x, response = NULL, strata = NULL, log = TRUE,
ref = NULL, correct = any(x == 0), ...)
# S3 method for class 'formula'
lodds(formula, data = NULL, ...,
subset = NULL, na.action = NULL)
odds(x, log = FALSE, ...)
# S3 method for class 'lodds'
coef(object, log = object$log, ...)
# S3 method for class 'lodds'
vcov(object, log = object$log, ...)
# S3 method for class 'lodds'
print(x, log = x$log, ...)
# S3 method for class 'lodds'
confint(object, parm, level = 0.95, log = object$log, ...)
# S3 method for class 'lodds'
dim(x, ...)
# S3 method for class 'lodds'
dimnames(x, ...)
<!-- %as.array(x, \dots) -->
# S3 method for class 'lodds'
as.array(x, log=x$log, ...)
# S3 method for class 'lodds'
t(x)
# S3 method for class 'lodds'
aperm(a, perm, ...)an object. For the default method a k-way matrix/table/array of frequencies. The number of margins has to be at least 2.
Numeric or character indicating the margin of a
$k$-way table x (with $k$ greater than 2) that should be employed
as the response variable. By default the first dimension is used.
Numeric or character indicating the margins of a
$k$-way table x (with $k$ greater than 2) that should be employed
as strata. Ignored if response is specified.
By default all dimensions except the first are used as strata.
numeric or character. Reference categories for the (non-stratum) row and column dimensions that should be employed for computing the odds. By default, odds for profile contrasts (or sequential contrasts, i.e., successive differences of adjacent categories) are used. See details below.
a formula specifying the variables used to create a
contingency table from data. A conditioning
formula can be specified; the conditioning variables will then be
used as strata variables.
either a data frame, or an object of class "table"
or "ftable".
an optional vector specifying a subset of observations to be used.
a function which indicates what should happen when
the data contain NAs. Ignored if data is a contingency table.
logical. Should the results be displayed on a log scale
or not? All internal computations are always on the log-scale but the
results are transformed by default if log = TRUE.
logical or numeric. Should a continuity correction
be applied before computing odds?
If TRUE, 0.5 is added to all cells; if numeric (or an
array conforming to the data) that value is added to all cells. By default,
this not employed unless there are any zero cells in the table, but this
correction is often recommended to reduce bias when some frequencies are small
(Fleiss, 1981).
an object of class lodds as computed by
lodds.
numeric or character vector specifying a permutation of strata.
arguments passed to methods.
a specification of which parameters are to be given confidence intervals, either a vector of numbers or a vector of names. If missing, all parameters are considered.
the confidence level required for the confint method.
For an n-way table with the response variable containing R levels,
(log) odds are formed (by default) for the set of (R-1)
contrasts among the response levels.
The ref argument allows these to be specified in a general
way.
ref = NULL (default) corresponds to “profile contrasts”
(or sequential contrasts or successive differences) for ordered categories,
i.e., R1–R2, R2–R3, R3–R4, etc., and similarly for the column categories.
These are sometimes called “local odds” or “adjacent odds”.
ref = 1 gives contrasts with the first category; ref = dim(x)
gives contrasts with the last category.
Note that all such parameterizations are equivalent, in that one can derive all other possible odds from any non-redundant set, but the interpretation of these values depends on the parameterization.
See the help page of plot.loddsratio for related visualization methods.
There is as yet no plot method for lodds objects.
An object of class lodds, with the following components:
A named vector, of length (R-1) x (C-1) x prod(dim(x)[strata])
containing the log odds. Use the coef method to
extract these from the object, and the confint method for confidence intervals.
For a two-way table, the names for the log oddsare constructed in the form
Ri:Rj using the table names for rows and columns. For a stratified table,
the names are constructed in the form Ri:Rj|Lk.
Variance covariance matrix of the log odds.
Dimension names for the log odds, considered as a table of
size (R-1, C-1, dim(x)[strata]). Use the dim and dimnames methods
to extract these and manipulate the log odds in relation to the original table.
Corresponding dimension vector.
A matrix C, such that C %*% as.vector(log(x)) gives the log odds
ratios. Each row corresponds to one log odds, and is all zero, except for 2 elements
of c(1, -1) for a given 2 x 1 subtable.
A logical, indicating the value of log in the original call.
A. Agresti (2013), Categorical Data Analysis, 3rd Ed. New York: Wiley.
Fleiss, J. L. (1981). Statistical Methods for Rates and Proportions. 2nd Edition. New York: Wiley.
M. Friendly (2000), Visualizing Categorical Data. SAS Institute, Cary, NC.
Friendly, M., Turner, H,, Firth, D., Zeileis, A. (2011). Advances in Visualizing Categorical Data Using the vcd, gnm and vcdExtra Packages in R. Correspondence Analysis and Related Methods (CARME 2011). http://www.datavis.ca/papers/adv-vcd-4up.pdf
The method of calculation is an example of the use of the delta method described by Agresti (2013), Section 16.1.6, giving estimates of log odds ratios and their asymptotic covariance matrix.
The coef method returns the coefficients component as a vector
of length (R-1) x prod(dim(x)[strata]).
The dim and dimnames methods provide the proper attributes for
treating the coefficients vector as an (R-1) x strata array.
as.matrix and as.array methods are also provided for this purpose.
The confint method computes confidence intervals for the log odds
(or for odds, with log = FALSE).
The coeftest method (summary is an alias)
prints the asymptotic standard errors, z tests (standardized log odds), and the corresponding p values.
Structural zeros: In addition to the options for zero cells provided by correct,
the function allows for structural zeros to be represented as NA in the data argument.
NA in the data yields NA as the LOR estimate, but does not affect other
cells.
odds is just an alias to lodds with the default log=FALSE for
convenience.
loddsratio for log odds ratios;
confint for confidence intervals;
coeftest for z-tests of significance
## artificial example
set.seed(1)
x <- matrix(rpois(5 * 3, 7), ncol = 5, nrow = 3)
dimnames(x) <- list(Row = head(letters, 3), Col = tail(letters, 5))
x_lodds <- lodds(x)
coef(x_lodds)
#> a:b|v b:c|v a:b|w b:c|w a:b|x b:c|x a:b|y
#> -0.1823216 -0.1541507 0.7884574 -0.6931472 0.3184537 0.0000000 -0.5108256
#> b:c|y a:b|z b:c|z
#> 0.0000000 0.2876821 -0.4054651
x_lodds
#> log odds for Row by Col
#>
#> Col
#> Row v w x y z
#> a:b -0.1823216 0.7884574 0.3184537 -0.5108256 0.2876821
#> b:c -0.1541507 -0.6931472 0.0000000 0.0000000 -0.4054651
confint(x_lodds)
#> 2.5 % 97.5 %
#> a:b|v -1.3691387 1.0044956
#> b:c|v -1.2445740 0.9362726
#> a:b|w -0.2686686 1.8455833
#> b:c|w -1.7666637 0.3803693
#> a:b|x -0.5922635 1.2291710
#> b:c|x -0.9799820 0.9799820
#> a:b|y -1.9421809 0.9205297
#> b:c|y -1.2395901 1.2395901
#> a:b|z -0.7708195 1.3461836
#> b:c|z -1.4384568 0.6275266
summary(x_lodds)
#>
#> z test of coefficients:
#>
#> Estimate Std. Error z value Pr(>|z|)
#> a:b|v -0.18232 0.60553 -0.3011 0.7633
#> b:c|v -0.15415 0.55635 -0.2771 0.7817
#> a:b|w 0.78846 0.53936 1.4618 0.1438
#> b:c|w -0.69315 0.54772 -1.2655 0.2057
#> a:b|x 0.31845 0.46466 0.6853 0.4931
#> b:c|x 0.00000 0.50000 0.0000 1.0000
#> a:b|y -0.51083 0.73030 -0.6995 0.4843
#> b:c|y 0.00000 0.63246 0.0000 1.0000
#> a:b|z 0.28768 0.54006 0.5327 0.5943
#> b:c|z -0.40547 0.52705 -0.7693 0.4417
#>
### 2 x 2 x k cases
##data(CoalMiners, package = "vcd")
#lor_CM <- loddsratio(CoalMiners)
#lor_CM
#coef(lor_CM)
#confint(lor_CM)
#confint(lor_CM, log = FALSE)
#
### 2 x k x 2
#lor_Emp <-loddsratio(Employment)
#lor_Emp
#confint(lor_Emp)
#
### 4 way tables
#data(Punishment, package = "vcd")
#lor_pun <- loddsratio(Freq ~ memory + attitude | age + education, data = Punishment)
#lor_pun
#confint(lor_pun)
#summary(lor_pun)
#
## fit linear model using WLS
#lor_pun_df <- as.data.frame(lor_pun)
#pun_mod1 <- lm(LOR ~ as.numeric(age) * as.numeric(education),
# data = lor_pun_df, weights = 1 / ASE^2)
#anova(pun_mod1)
#
### illustrate ref levels
#VA.fem <- xtabs(Freq ~ left + right, subset=gender=="female", data=VisualAcuity)
#VA.fem
#loddsratio(VA.fem) # profile contrasts
#loddsratio(VA.fem, ref=1) # contrasts against level 1
#loddsratio(VA.fem, ref=dim(VA.fem)) # contrasts against level 4
#