~ubuntu-branches/ubuntu/trusty/r-cran-vgam/trusty

« back to all changes in this revision

Viewing changes to man/Qvar.Rd

  • Committer: Package Import Robot
  • Author(s): Chris Lawrence
  • Date: 2011-11-04 13:13:06 UTC
  • mfrom: (1.1.12) (2.1.9 sid)
  • Revision ID: package-import@ubuntu.com-20111104131306-lrc3f24ev3xoev2q
Tags: 0.8-4-1
New upstream release.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
\name{Qvar}
 
2
\alias{Qvar}
 
3
%- Also NEED an '\alias' for EACH other topic documented here.
 
4
\title{
 
5
Quasi-variances Preprocessing Function
 
6
 
 
7
%%  ~~function to do ... ~~
 
8
}
 
9
\description{
 
10
  Takes a \code{\link{vglm}} fit or a variance-covariance matrix,
 
11
  and preprocesses it for \code{\link{rcam}} and
 
12
  \code{\link{normal1}} so that quasi-variances can be computed.
 
13
 
 
14
 
 
15
%%  ~~ A concise (1-5 lines) description of what the function does. ~~
 
16
}
 
17
\usage{
 
18
Qvar(object, factorname = NULL, coef.indices = NULL,
 
19
     labels = NULL, dispersion = NULL, reference.name = "(reference)",
 
20
     estimates = NULL)
 
21
}
 
22
%- maybe also 'usage' for other objects documented here.
 
23
\arguments{
 
24
  \item{object}{
 
25
    A \code{"\link[=vglmff-class]{vglm}"} object or a variance-covariance
 
26
    matrix, e.g., \code{vcov(vglm.object)}.
 
27
    The former is preferred since it contains all the information
 
28
    needed.
 
29
  If a matrix then \code{factorname} and/or \code{coef.indices}
 
30
  should be specified to identify the factor.
 
31
 
 
32
 
 
33
%%     ~~Describe \code{object} here~~
 
34
}
 
35
\item{factorname}{
 
36
  Character.
 
37
  If the \code{\link{vglm}} object contains more than one
 
38
  factor as explanatory variable then this argument should
 
39
  be the name of the factor of interest.
 
40
  If \code{object} is a variance-covariance matrix  then
 
41
  this argument should also be specified.
 
42
 
 
43
 
 
44
%%     ~~Describe \code{factor.name} here~~
 
45
}
 
46
\item{labels}{
 
47
  Character.
 
48
  Optional, for labelling the variance-covariance matrix.
 
49
 
 
50
%%     ~~Describe \code{level1.name} here~~
 
51
}
 
52
\item{dispersion}{
 
53
  Numeric.
 
54
  Optional, passed into \code{vcov()} with the same argument name.
 
55
 
 
56
%%     ~~Describe \code{level1.name} here~~
 
57
}
 
58
\item{reference.name}{
 
59
  Character.
 
60
  Label for for the reference level.
 
61
 
 
62
%%     ~~Describe \code{level1.name} here~~
 
63
}
 
64
\item{coef.indices}{
 
65
  Optional numeric vector of length at least 3 specifying
 
66
  the indices of the factor from the variance-covariance
 
67
  matrix.
 
68
 
 
69
 
 
70
}
 
71
\item{estimates}{
 
72
  an optional vector of estimated coefficients
 
73
  (redundant if \code{object} is a model).
 
74
 
 
75
}
 
76
}
 
77
\details{
 
78
 
 
79
 
 
80
  Suppose a factor with \eqn{L} levels is an explanatory variable in a
 
81
  regression model. By default, R treats the first level as baseline so
 
82
  that its coefficient is set to zero. It estimates the other \eqn{L-1}
 
83
  coefficients, and with its associated standard errors, this is the
 
84
  conventional output. From the complete variance-covariance matrix one
 
85
  can compute \eqn{L} quasi-variances based on all pairwise difference
 
86
  of the coefficients. They are based on an approximation, and can be
 
87
  treated as uncorrelated.  In minimizing the relative (not absolute)
 
88
  errors it is not hard to see that the estimation involves a RCAM
 
89
  (\code{\link{rcam}}) with an exponential link function
 
90
  (\code{\link{explink}}).
 
91
 
 
92
 
 
93
  If \code{object} is a model, then at least one of \code{factorname} or
 
94
  \code{coef.indices} must be non-\code{NULL}.  The value of
 
95
  \code{coef.indices}, if non-\code{NULL}, determines which rows and
 
96
  columns of the model's variance-covariance matrix to use.  If
 
97
  \code{coef.indices} contains a zero, an extra row and column are
 
98
  included at the indicated position, to represent the zero variances
 
99
  and covariances associated with a reference level.  If
 
100
  \code{coef.indices} is \code{NULL}, then \code{factorname} should be
 
101
  the name of a factor effect in the model, and is used in order to
 
102
  extract the necessary variance-covariance estimates.
 
103
 
 
104
 
 
105
  Quasi-variances were first implemented in R with \pkg{qvcalc}.
 
106
  This implementation draws heavily from that.
 
107
 
 
108
 
 
109
 
 
110
%%  ~~ If necessary, more details than the description above ~~
 
111
}
 
112
\value{
 
113
  A \eqn{L} by \eqn{L} matrix whose \eqn{i}-\eqn{j} element
 
114
  is the logarithm of the variance of the \eqn{i}th coefficient
 
115
  minus the \eqn{j}th coefficient, for all values of \eqn{i}
 
116
  and \eqn{j}. The diagonal elements are abitrary and are set
 
117
  to zero.
 
118
 
 
119
  
 
120
  The matrix has an attribute that corresponds to the prior
 
121
  weight matrix; it is accessed by \code{\link{normal1}}
 
122
  and replaces the usual \code{weights} argument.
 
123
  of \code{\link{vglm}}. This weight matrix has ones on
 
124
  the off-diagonals and some small positive number on
 
125
  the diagonals.
 
126
 
 
127
 
 
128
}
 
129
\references{
 
130
 
 
131
 
 
132
  Firth, D. (2003)
 
133
  Overcoming the reference category problem in the
 
134
  presentation of statistical models.
 
135
  \emph{Sociological Methodology} \bold{33}, 1--18.
 
136
 
 
137
 
 
138
  Firth, D. and Menezes, R. X. de (2004)
 
139
  Quasi-variances.
 
140
  \emph{Biometrika} \bold{91}, 65--80.
 
141
 
 
142
  
 
143
}
 
144
 
 
145
\author{
 
146
 
 
147
 
 
148
  T. W. Yee, based heavily on \code{qvcalc()} in \pkg{qvcalc}
 
149
  written by David Firth.
 
150
 
 
151
 
 
152
}
 
153
 
 
154
\note{
 
155
  This is an adaptation of \code{qvcalc()} in \pkg{qvcalc}.
 
156
  It should work for all \code{\link{vglm}}
 
157
  models with one linear predictor, i.e., \eqn{M = 1}.
 
158
  For \eqn{M > 1} the factor should appear only in one of the
 
159
  linear predictors.
 
160
 
 
161
 
 
162
  It is important to set \code{maxit} to be larger than usual for
 
163
  \code{\link{rcam}} since convergence is slow.  Upon successful
 
164
  convergence the \eqn{i}th row effect and the \eqn{i}th column effect
 
165
  should be equal.  A simple computation involving the fitted and
 
166
  predicted values allows the quasi-variances to be extracted (see
 
167
  example below).
 
168
 
 
169
 
 
170
  A function to plot \emph{comparison intervals} has not been
 
171
  written here.
 
172
 
 
173
}
 
174
 
 
175
\section{Warning }{
 
176
  Negative quasi-variances may occur (one of them and
 
177
  only one), though they are rare in practice. If
 
178
  so then numerical problems may occur.  See
 
179
  \code{qvcalc()} for more information.
 
180
 
 
181
 
 
182
 
 
183
}  
 
184
 
 
185
 
 
186
\seealso{
 
187
  \code{\link{rcam}},
 
188
  \code{\link{vglm}},
 
189
  \code{\link{normal1}},
 
190
  \code{\link{explink}},
 
191
  \code{qvcalc()} in \pkg{qvcalc},
 
192
  \code{\link[MASS]{ships}}.
 
193
 
 
194
 
 
195
%% ~~objects to See Also as \code{\link{help}}, ~~~
 
196
}
 
197
\examples{
 
198
library(MASS)  # Get the "ships" data frame
 
199
data(ships)
 
200
ships = ships
 
201
detach("package:MASS")
 
202
 
 
203
Shipmodel <- vglm(incidents ~ type + year + period,
 
204
                  quasipoissonff, offset = log(service),
 
205
#                 trace = TRUE, model = TRUE,
 
206
                  data = ships, subset = (service > 0))
 
207
 
 
208
# Easiest form of input
 
209
fit1 <- rcam(Qvar(Shipmodel, "type"), normal1("explink"), maxit = 99)
 
210
(quasiVar <- exp(diag(fitted(fit1))) / 2)                 # Version 1
 
211
(quasiVar <- diag(predict(fit1)[, c(TRUE, FALSE)]) / 2)   # Version 2
 
212
(quasiSE  <- sqrt(quasiVar))
 
213
 
 
214
# Another form of input
 
215
fit2 <- rcam(Qvar(Shipmodel, coef.ind = c(0,2:5), reference.name = "typeA"),
 
216
             normal1("explink"), maxit = 99)
 
217
\dontrun{ plotqvar(fit2, lcol = "blue", llwd = 2, las = 1) }
 
218
 
 
219
# The variance-covariance matrix is another form of input (not recommended)
 
220
fit3 <- rcam(Qvar(cbind(0, rbind(0, vcov(Shipmodel)[2:5, 2:5])),
 
221
                  labels = c("typeA", "typeB", "typeC", "typeD", "typeE"),
 
222
                  estimates = c(typeA = 0, coef(Shipmodel)[2:5])),
 
223
             normal1("explink"), maxit = 99)
 
224
(QuasiVar <- exp(diag(fitted(fit3))) / 2)                 # Version 1
 
225
(QuasiVar <- diag(predict(fit3)[, c(TRUE, FALSE)]) / 2)   # Version 2
 
226
(QuasiSE  <- sqrt(quasiVar))
 
227
\dontrun{ plotqvar(fit3) }
 
228
}
 
229
% Add one or more standard keywords, see file 'KEYWORDS' in the
 
230
% R documentation directory.
 
231
\keyword{models}
 
232
\keyword{regression}
 
233
% \code{\link[qvcalc:qvcalc]{qvcalc}} in \pkg{qvcalc}