Codebase list effects / b59f764
Import Upstream version 2.0.4 Dirk Eddelbuettel 5 years ago
15 changed file(s) with 206 addition(s) and 39 deletion(s). Raw diff Collapse all Expand all
0 Package: effects
1 Version: 2.0-3
2 Date: 2008/12/11
3 Title: Effect Displays for Linear, Generalized Linear, Multinomial-Logit, and Proportional-Odds Logit Models
4 Author: John Fox <jfox@mcmaster.ca> and Jangman Hong. We are grateful to Robert Andersen, David Firth, and
5 Michael Friendly, for various suggestions.
6 Maintainer: John Fox <jfox@mcmaster.ca>
7 Depends: R (>= 2.4.0), lattice, grid, MASS, nnet
8 LazyLoad: yes
9 LazyData: yes
10 Description:
11 Graphical and tabular effect displays, e.g., of interactions, for linear
12 generalized linear, multinomial-logit, and proportional-odds logit models.
13 License: GPL (>= 2)
14 URL: http://www.r-project.org, http://socserv.socsci.mcmaster.ca/jfox/
15 Revision: 22
16 Packaged: Fri Dec 12 05:12:00 2008; theussl
0 Package: effects
1 Version: 2.0-4
2 Date: 2009/03/26
3 Title: Effect Displays for Linear, Generalized Linear,
4 Multinomial-Logit, and Proportional-Odds Logit Models
5 Author: John Fox <jfox@mcmaster.ca> and Jangman Hong. We are grateful
6 to Robert Andersen, David Firth, and Michael Friendly, for
7 various suggestions.
8 Maintainer: John Fox <jfox@mcmaster.ca>
9 Depends: R (>= 2.4.0), lattice, grid, MASS, nnet, colorspace
10 LazyLoad: yes
11 LazyData: yes
12 Description: Graphical and tabular effect displays, e.g., of
13 interactions, for linear generalized linear, multinomial-logit,
14 and proportional-odds logit models.
15 License: GPL (>= 2)
16 URL: http://www.r-project.org, http://socserv.socsci.mcmaster.ca/jfox/
17 Repository: CRAN
18 Repository/R-Forge/Project: effects
19 Repository/R-Forge/Revision: 27
20 Publication/Date: 2009-03-27 02:25:06
21 Packaged: Wed Apr 1 05:14:22 2009; rforge
22 Date/Publication: 2009-04-01 17:01:40
00 # effect generic and methods; allEffects
11 # John Fox and Jangman Hong
2 # last modified 11 December 2008 by J. Fox
2 # last modified 6 March 2009 by J. Fox
33
44 effect <- function(term, mod, ...){
55 UseMethod("effect", mod)
194194 }
195195 result <- list(term=term, formula=formula(mod), response=response.name(mod),
196196 y.levels=mod$lev, variables=x, x=predict.data[,1:n.basic, drop=FALSE],
197 model.matrix=X0, data=X, discrepancy=discrepancy,
197 model.matrix=X0, data=X, discrepancy=discrepancy, model="multinom",
198198 prob=P, logit=Logit)
199199 if (se) result <- c(result, list(se.prob=SE.P, se.logit=SE.logit,
200200 lower.logit=Lower.logit, upper.logit=Upper.logit,
305305 result <- list(term=term, formula=formula(mod), response=response.name(mod),
306306 y.levels=mod$lev, variables=x,
307307 x=predict.data[,1:n.basic, drop=FALSE],
308 model.matrix=X0, data=X, discrepancy=discrepancy)
308 model.matrix=X0, data=X, discrepancy=discrepancy, model="polr")
309309 if (latent){
310310 res <- eff.latent(X0, b, vcov(mod)[1:p, 1:p])
311311 result$fit <- res$fit
00 # plot, summary, and print methods for effects package
11 # John Fox and Jangman Hong
2 # last modified 10 Decemeber 2008 by J. Fox
2 # last modified 19 March 2009 by J. Fox
33
44
55 summary.eff <- function(object, type=c("response", "link"), ...){
491491 xlab,
492492 ylab=paste(x$response, " (", type, ")", sep=""),
493493 main=paste(effect, "effect plot"),
494 colors=palette(), symbols=1:10, lines=1:10, cex=1.5,
494 colors, symbols=1:10, lines=1:10, cex=1.5,
495495 factor.names=TRUE, style=c("lines", "stacked"),
496496 confint=(style == "lines" && !is.null(x$confidence.level)),
497497 ylim, alternating=TRUE, layout, key.args=NULL,
510510 confint <- FALSE
511511 warning('confint set to FALSE for stacked plot')
512512 }
513 }
514 if (missing(colors)){
515 if (style == "stacked"){
516 colors <- if (x$model == "multinom") rainbow_hcl(length(x$y.levels))
517 else sequential_hcl(length(x$y.levels))
518 }
519 else colors <- palette()
513520 }
514521 effect <- paste(sapply(x$variables, "[[", "name"), collapse="*")
515522 split <- c(col, row, ncol, nrow)
556563 function(x) length(unique(x)))
557564 if (length(n.predictor.cats) == 0) n.predictor.cats <- 1
558565 if (!confint){ # plot without confidence bands
559 layout <- if (missing(layout)) c(prod(n.predictor.cats[-(n.predictors - 1)]),
566 layout <- if (missing(layout)){
567 lay <- c(prod(n.predictor.cats[-(n.predictors - 1)]),
560568 n.predictor.cats[(n.predictors - 1)], 1)
569 if (lay[1] > 1) lay else lay[c(2, 1, 3)]
570 }
561571 else layout
562572 if (style == "lines"){ # line plot
563573 if (n.y.lev > min(c(length(colors), length(lines), length(symbols))))
593603 main=main,
594604 key=c(key, key.args),
595605 layout=layout,
596 data=data),
606 data=data, ...),
597607 split=split, more=more)
598608 }
599609 else { # x-variable numeric
624634 main=main,
625635 key=c(key, key.args),
626636 layout=layout,
627 data=data),
637 data=data, ...),
628638 split=split, more=more)
629639 }
630640 }
683693 scales=list(alternating=alternating),
684694 main=main,
685695 key=c(key, key.args),
686 layout=layout),
696 layout=layout, ...),
687697 split=split, more=more)
688698 }
689699 }
729739 upper=upper,
730740 scales=list(x=list(at=1:length(levs), labels=levs), alternating=alternating),
731741 layout=layout,
732 data=data),
742 data=data, ...),
733743 split=split, more=more)
734744 }
735745 else { # x-variable numeric
763773 upper=upper,
764774 scales=list(alternating=alternating),
765775 layout=layout,
766 data=data),
776 data=data, ...),
767777 split=split, more=more)
768778 }
769779 }
00 # utilities and common functions for effects package
11 # John Fox and Jangman Hong
2 # last modified 19 October 2008 by J. Fox
2 # last modified 26 March 2009 by J. Fox
33
44
55 has.intercept <- function(model, ...) any(names(coefficients(model))=="(Intercept)")
234234 x=x, X.mod=X.mod, cnames=cnames, X=X)
235235 }
236236
237 #fixup.model.matrix <- function(mod, mod.matrix, mod.matrix.all, X.mod, mod.aug,
238 # factor.cols, cnames, term, typical, given.values){
239 # attr(mod.matrix, "assign") <- attr(mod.matrix.all, "assign")
240 # stranger.cols <- factor.cols &
241 # apply(outer(strangers(term, mod, mod.aug), attr(mod.matrix,'assign'), '=='), 2, any)
242 # if (has.intercept(mod)) stranger.cols[1] <- TRUE
243 # if (any(stranger.cols)) {
244 # mod.matrix[,stranger.cols] <-
245 # matrix(apply(as.matrix(X.mod[,stranger.cols]), 2, typical),
246 # nrow=nrow(mod.matrix), ncol=sum(stranger.cols), byrow=TRUE)
247 # if (!is.null(given.values)){
248 # stranger.names <- names(stranger.cols[stranger.cols])
249 # given <- stranger.names %in% names(given.values)
250 # if (any(given)) mod.matrix[,stranger.names[given]] <- given.values[stranger.names[given]]
251 # }
252 # }
253 # for (name in cnames){
254 # components <- unlist(strsplit(name, ':'))
255 # if (length(components) > 1)
256 # mod.matrix[,name] <- apply(mod.matrix[,components], 1, prod)
257 # }
258 # mod.matrix
259 #}
260
237261 fixup.model.matrix <- function(mod, mod.matrix, mod.matrix.all, X.mod, mod.aug,
238262 factor.cols, cnames, term, typical, given.values){
239263 attr(mod.matrix, "assign") <- attr(mod.matrix.all, "assign")
240 stranger.cols <- factor.cols &
264 stranger.cols <-
241265 apply(outer(strangers(term, mod, mod.aug), attr(mod.matrix,'assign'), '=='), 2, any)
242266 if (has.intercept(mod)) stranger.cols[1] <- TRUE
243267 if (any(stranger.cols)) {
244 mod.matrix[,stranger.cols] <-
245 matrix(apply(as.matrix(X.mod[,stranger.cols]), 2, typical),
246 nrow=nrow(mod.matrix), ncol=sum(stranger.cols), byrow=TRUE)
268 facs <- factor.cols & stranger.cols
269 covs <- (!factor.cols) & stranger.cols
270 if (any(facs)) mod.matrix[,facs] <-
271 matrix(apply(as.matrix(X.mod[,facs]), 2, mean),
272 nrow=nrow(mod.matrix), ncol=sum(facs), byrow=TRUE)
273 if (any(covs)) mod.matrix[,covs] <-
274 matrix(apply(as.matrix(X.mod[,covs]), 2, typical),
275 nrow=nrow(mod.matrix), ncol=sum(covs), byrow=TRUE)
247276 if (!is.null(given.values)){
248 stranger.names <- names(stranger.cols[stranger.cols])
277 stranger.names <- cnames[stranger.cols]
249278 given <- stranger.names %in% names(given.values)
250279 if (any(given)) mod.matrix[,stranger.names[given]] <- given.values[stranger.names[given]]
251280 }
Binary diff not shown
Binary diff not shown
8181
8282 o Fixed bug in all effect() methods that caused error when na.action="na.exclude" (reported by Tracy Lightcap and Rob Goedman).
8383
84 Version 2.0-4
85
86 o Palettes from the colorspace package are used by default for stacked plots.
87
88 o Fixed bug in handling of typical= argument to effect() (argument was effectively ignored).
89
90 o Added Titanic and Wells data sets.
91
92 o Small changes.
4242 Personal communication from Michael Friendly, York University.
4343 }
4444
45 \examples{
46 summary(Arrests)
47 }
48
4549 \keyword{datasets}
2929 \emph{Sociological Methodology} \bold{36}, 225--255.
3030 }
3131
32 \examples{
33 summary(BEPS)
34 }
35
3236 \keyword{datasets}
2323 \emph{British Journal of Social Psychology} \bold{26}, 97--102.
2424 }
2525
26 \examples{
27 summary(Cowles)
28 }
2629
2730 \keyword{datasets}
0 \name{Titanic}
1 \alias{Titanic}
2 \docType{data}
3 \title{Survival of Passengers on the Titanic}
4 \description{
5 Information on the survival status, sex, age, and passenger class of 1309
6 passengers in the Titanic disaster of 1912.
7 }
8
9 \usage{Titanic}
10
11 \format{
12 A data frame with 1309 observations on the following 4 variables.
13 \describe{
14 \item{\code{survived}}{\code{no} or \code{yes}.}
15 \item{\code{sex}}{\code{female} or \code{male}}
16 \item{\code{age}}{in years (and for some children, fractions of a year); age
17 is missing for 263 of the passengers.}
18 \item{\code{passengerClass}}{\code{1st}, \code{2nd}, or \code{3rd} class.}
19 }
20 }
21
22 \details{
23 This is part of a larger data set compiled by Thomas Cason.
24 Many additional details are given in the sources cited below.
25 }
26
27 \source{
28 Data set \code{titanic3} from
29 \url{http://biostat.mc.vanderbilt.edu/twiki/bin/view/Main/DataSets}.
30 }
31
32 \references{
33 \url{http://www.encyclopedia-titanica.org/}
34
35 F. E. Harrell, Jr. (2001)
36 \emph{Regression Modeling Strategies}
37 New York: Springer.
38 }
39 \examples{
40 summary(Titanic)
41 }
42 \keyword{datasets}
2525 \emph{Sociological Methodology} \bold{36}, 225--255.
2626 }
2727
28 \examples{
29 summary(WVS)
30 }
31
2832 \keyword{datasets}
0 \name{Wells}
1 \alias{Wells}
2 \docType{data}
3 \title{Well Switching in Bangladesh}
4 \description{
5 Data on whether or not households in Bangladesh changed the wells that
6 they were using.
7 }
8
9 \usage{Wells}
10 \format{
11 A data frame with 3020 observations on the following 5 variables.
12 \describe{
13 \item{\code{switch}}{whether or not the household switched to
14 another well from an unsafe well: \code{no} or \code{yes}.}
15 \item{\code{arsenic}}{the level of arsenic contamination in the
16 household's original well, in hundreds of micrograms per liter; all
17 are above 0.5, which was the level identified as ``safe''.}
18 \item{\code{distance}}{in meters to the closest known safe well.}
19 \item{\code{education}}{in years of the head of the household.}
20 \item{\code{association}}{whether or not any members of the household
21 participated in any community organizations: \code{no} or \code{yes}.}
22 }
23 }
24
25 \details{
26 The data are for an area of Arahazar upazila, Bangladesh.
27 The researchers labelled each well with its level of arsenic and an
28 indication of whether the well was ``safe'' or ``unsafe.'' Those using unsafe
29 wells were encouraged to switch. After several years, it was determined
30 whether each household using an unsafe well had changed its well.
31 These data are used by Gelman and Hill (2007) for a logistic-regression example.
32 }
33
34 \source{
35 \url{http://www.stat.columbia.edu/~gelman/arm/examples/arsenic/wells.dat}.
36 }
37
38 \references{
39 A. Gelman and J. Hill (2007)
40 \emph{Data Analysis Using Regression and Multilevel/Hierarchical Models.}
41 Cambridge: Cambridge University Press.
42 }
43
44 \examples{
45 summary(Wells)
46 }
47
48 \keyword{datasets}
129129 \item{model.matrix}{the model matrix from which the effect was calculated.}
130130 \item{data}{a data frame with the data on which the fitted model was based.}
131131 \item{discrepancy}{the percentage discrepancy for the `safe' predictions of the original fit; should be very close to 0.}
132 \item{model}{(for \code{"effpoly"} objects) \code{"multinom"} or \code{"polor"}, as appropriate.}
132133 \item{se}{(for \code{"eff"} objects) a vector of standard errors for the effect, on the scale of the linear predictor.}
133134 \item{se.prob, se.logit}{(for \code{"effpoly"} objects) matrices of standard errors for the effect, on the probability and logit scales.}
134135 \item{lower, upper}{(for \code{"eff"} objects) one-column matrices of confidence limits, on the
211212 data=WVS)
212213 plot(effect("country*poly(age, 3)", mod.wvs))
213214
214 plot(effect("country*poly(age, 3)", mod.wvs), style="stacked",
215 colors=c("gray75", "gray50", "gray25"))
215 plot(effect("country*poly(age, 3)", mod.wvs), style="stacked")
216216
217217 plot(effect("country*poly(age, 3)", latent=TRUE, mod.wvs))
218218
3939 x.var=which.max(levels), rug=TRUE, xlab,
4040 ylab=paste(x$response, " (", type, ")", sep=""),
4141 main=paste(effect, "effect plot"),
42 colors=palette(), symbols=1:10, lines=1:10, cex=1.5,
42 colors, symbols=1:10, lines=1:10, cex=1.5,
4343 factor.names=TRUE, style=c("lines", "stacked"),
4444 confint=(style == "lines" && !is.null(x$confidence.level)),
4545 ylim, alternating=TRUE, layout, key.args=NULL,
9191 to the levels of the \code{z.var} covariate or factor. In a stacked plot or a plot
9292 without confidence bands for a
9393 multinomial or proportional-odds logit model, the successive \code{colors}
94 correspond to the levels of the response factor. \bold{Warning:} This argument
94 correspond to the levels of the response factor. In all but stacked plots,
95 \code{colors} defaults to \code{palette()}; for stacked multinomial-logit plots,
96 \code{colors} defautls to \code{rainbow_hcl(levels)}, where \code{levels} is
97 the number of levels of the response variable; for stacked proportional-odds model
98 plots, \code{colors} defautls to \code{sequential_hcl(levels)}.
99 \bold{Warning:} This argument
95100 \emph{cannot} be abbreviated to \code{col}, which is used for a different
96101 purpose (see below).}
97102 \item{symbols, lines}{corresponding to the levels of the \code{z.var} covariate
177182
178183
179184 \seealso{\code{\link{effect}}, \code{\link{allEffects}}, \code{\link[lattice]{xyplot}},
180 \code{\link[lattice]{densityplot}}, \code{\link[lattice]{print.trellis}}}
185 \code{\link[lattice]{densityplot}}, \code{\link[lattice]{print.trellis}}
186 \code{\link[colorspace]{rainbow_hcl}}, \code{\link[colorspace]{sequential_hcl}}}
181187
182188 \examples{
183189 mod.cowles <- glm(volunteer ~ sex + neuroticism*extraversion,