Import Upstream version 2.0.4
Dirk Eddelbuettel
5 years ago
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 |
0 | 0 | # effect generic and methods; allEffects |
1 | 1 | # John Fox and Jangman Hong |
2 | # last modified 11 December 2008 by J. Fox | |
2 | # last modified 6 March 2009 by J. Fox | |
3 | 3 | |
4 | 4 | effect <- function(term, mod, ...){ |
5 | 5 | UseMethod("effect", mod) |
194 | 194 | } |
195 | 195 | result <- list(term=term, formula=formula(mod), response=response.name(mod), |
196 | 196 | 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", | |
198 | 198 | prob=P, logit=Logit) |
199 | 199 | if (se) result <- c(result, list(se.prob=SE.P, se.logit=SE.logit, |
200 | 200 | lower.logit=Lower.logit, upper.logit=Upper.logit, |
305 | 305 | result <- list(term=term, formula=formula(mod), response=response.name(mod), |
306 | 306 | y.levels=mod$lev, variables=x, |
307 | 307 | 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") | |
309 | 309 | if (latent){ |
310 | 310 | res <- eff.latent(X0, b, vcov(mod)[1:p, 1:p]) |
311 | 311 | result$fit <- res$fit |
0 | 0 | # plot, summary, and print methods for effects package |
1 | 1 | # John Fox and Jangman Hong |
2 | # last modified 10 Decemeber 2008 by J. Fox | |
2 | # last modified 19 March 2009 by J. Fox | |
3 | 3 | |
4 | 4 | |
5 | 5 | summary.eff <- function(object, type=c("response", "link"), ...){ |
491 | 491 | xlab, |
492 | 492 | ylab=paste(x$response, " (", type, ")", sep=""), |
493 | 493 | 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, | |
495 | 495 | factor.names=TRUE, style=c("lines", "stacked"), |
496 | 496 | confint=(style == "lines" && !is.null(x$confidence.level)), |
497 | 497 | ylim, alternating=TRUE, layout, key.args=NULL, |
510 | 510 | confint <- FALSE |
511 | 511 | warning('confint set to FALSE for stacked plot') |
512 | 512 | } |
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() | |
513 | 520 | } |
514 | 521 | effect <- paste(sapply(x$variables, "[[", "name"), collapse="*") |
515 | 522 | split <- c(col, row, ncol, nrow) |
556 | 563 | function(x) length(unique(x))) |
557 | 564 | if (length(n.predictor.cats) == 0) n.predictor.cats <- 1 |
558 | 565 | 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)]), | |
560 | 568 | n.predictor.cats[(n.predictors - 1)], 1) |
569 | if (lay[1] > 1) lay else lay[c(2, 1, 3)] | |
570 | } | |
561 | 571 | else layout |
562 | 572 | if (style == "lines"){ # line plot |
563 | 573 | if (n.y.lev > min(c(length(colors), length(lines), length(symbols)))) |
593 | 603 | main=main, |
594 | 604 | key=c(key, key.args), |
595 | 605 | layout=layout, |
596 | data=data), | |
606 | data=data, ...), | |
597 | 607 | split=split, more=more) |
598 | 608 | } |
599 | 609 | else { # x-variable numeric |
624 | 634 | main=main, |
625 | 635 | key=c(key, key.args), |
626 | 636 | layout=layout, |
627 | data=data), | |
637 | data=data, ...), | |
628 | 638 | split=split, more=more) |
629 | 639 | } |
630 | 640 | } |
683 | 693 | scales=list(alternating=alternating), |
684 | 694 | main=main, |
685 | 695 | key=c(key, key.args), |
686 | layout=layout), | |
696 | layout=layout, ...), | |
687 | 697 | split=split, more=more) |
688 | 698 | } |
689 | 699 | } |
729 | 739 | upper=upper, |
730 | 740 | scales=list(x=list(at=1:length(levs), labels=levs), alternating=alternating), |
731 | 741 | layout=layout, |
732 | data=data), | |
742 | data=data, ...), | |
733 | 743 | split=split, more=more) |
734 | 744 | } |
735 | 745 | else { # x-variable numeric |
763 | 773 | upper=upper, |
764 | 774 | scales=list(alternating=alternating), |
765 | 775 | layout=layout, |
766 | data=data), | |
776 | data=data, ...), | |
767 | 777 | split=split, more=more) |
768 | 778 | } |
769 | 779 | } |
0 | 0 | # utilities and common functions for effects package |
1 | 1 | # John Fox and Jangman Hong |
2 | # last modified 19 October 2008 by J. Fox | |
2 | # last modified 26 March 2009 by J. Fox | |
3 | 3 | |
4 | 4 | |
5 | 5 | has.intercept <- function(model, ...) any(names(coefficients(model))=="(Intercept)") |
234 | 234 | x=x, X.mod=X.mod, cnames=cnames, X=X) |
235 | 235 | } |
236 | 236 | |
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 | ||
237 | 261 | fixup.model.matrix <- function(mod, mod.matrix, mod.matrix.all, X.mod, mod.aug, |
238 | 262 | factor.cols, cnames, term, typical, given.values){ |
239 | 263 | attr(mod.matrix, "assign") <- attr(mod.matrix.all, "assign") |
240 | stranger.cols <- factor.cols & | |
264 | stranger.cols <- | |
241 | 265 | apply(outer(strangers(term, mod, mod.aug), attr(mod.matrix,'assign'), '=='), 2, any) |
242 | 266 | if (has.intercept(mod)) stranger.cols[1] <- TRUE |
243 | 267 | 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) | |
247 | 276 | if (!is.null(given.values)){ |
248 | stranger.names <- names(stranger.cols[stranger.cols]) | |
277 | stranger.names <- cnames[stranger.cols] | |
249 | 278 | given <- stranger.names %in% names(given.values) |
250 | 279 | if (any(given)) mod.matrix[,stranger.names[given]] <- given.values[stranger.names[given]] |
251 | 280 | } |
Binary diff not shown
Binary diff not shown
81 | 81 | |
82 | 82 | o Fixed bug in all effect() methods that caused error when na.action="na.exclude" (reported by Tracy Lightcap and Rob Goedman). |
83 | 83 | |
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. |
42 | 42 | Personal communication from Michael Friendly, York University. |
43 | 43 | } |
44 | 44 | |
45 | \examples{ | |
46 | summary(Arrests) | |
47 | } | |
48 | ||
45 | 49 | \keyword{datasets} |
29 | 29 | \emph{Sociological Methodology} \bold{36}, 225--255. |
30 | 30 | } |
31 | 31 | |
32 | \examples{ | |
33 | summary(BEPS) | |
34 | } | |
35 | ||
32 | 36 | \keyword{datasets} |
23 | 23 | \emph{British Journal of Social Psychology} \bold{26}, 97--102. |
24 | 24 | } |
25 | 25 | |
26 | \examples{ | |
27 | summary(Cowles) | |
28 | } | |
26 | 29 | |
27 | 30 | \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} |
25 | 25 | \emph{Sociological Methodology} \bold{36}, 225--255. |
26 | 26 | } |
27 | 27 | |
28 | \examples{ | |
29 | summary(WVS) | |
30 | } | |
31 | ||
28 | 32 | \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} |
129 | 129 | \item{model.matrix}{the model matrix from which the effect was calculated.} |
130 | 130 | \item{data}{a data frame with the data on which the fitted model was based.} |
131 | 131 | \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.} | |
132 | 133 | \item{se}{(for \code{"eff"} objects) a vector of standard errors for the effect, on the scale of the linear predictor.} |
133 | 134 | \item{se.prob, se.logit}{(for \code{"effpoly"} objects) matrices of standard errors for the effect, on the probability and logit scales.} |
134 | 135 | \item{lower, upper}{(for \code{"eff"} objects) one-column matrices of confidence limits, on the |
211 | 212 | data=WVS) |
212 | 213 | plot(effect("country*poly(age, 3)", mod.wvs)) |
213 | 214 | |
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") | |
216 | 216 | |
217 | 217 | plot(effect("country*poly(age, 3)", latent=TRUE, mod.wvs)) |
218 | 218 |
39 | 39 | x.var=which.max(levels), rug=TRUE, xlab, |
40 | 40 | ylab=paste(x$response, " (", type, ")", sep=""), |
41 | 41 | 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, | |
43 | 43 | factor.names=TRUE, style=c("lines", "stacked"), |
44 | 44 | confint=(style == "lines" && !is.null(x$confidence.level)), |
45 | 45 | ylim, alternating=TRUE, layout, key.args=NULL, |
91 | 91 | to the levels of the \code{z.var} covariate or factor. In a stacked plot or a plot |
92 | 92 | without confidence bands for a |
93 | 93 | 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 | |
95 | 100 | \emph{cannot} be abbreviated to \code{col}, which is used for a different |
96 | 101 | purpose (see below).} |
97 | 102 | \item{symbols, lines}{corresponding to the levels of the \code{z.var} covariate |
177 | 182 | |
178 | 183 | |
179 | 184 | \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}}} | |
181 | 187 | |
182 | 188 | \examples{ |
183 | 189 | mod.cowles <- glm(volunteer ~ sex + neuroticism*extraversion, |