├── .Rbuildignore ├── .Rprofile ├── .github ├── .gitignore └── workflows │ └── R-CMD-check.yaml ├── .gitignore ├── DESCRIPTION ├── LICENSE ├── NAMESPACE ├── NEWS.md ├── R ├── buildPlot.r ├── buildPlotPlotly.r ├── buildPlottingFrame.r ├── coefPredMatching.r ├── coefpath.r ├── coefplot-package.r ├── coefplot.r ├── dodging.r ├── dyAnnotation.r ├── extractCoef.r ├── extractPath.r ├── multiplot.r ├── position.r └── transformations.r ├── README.Rmd ├── README.md ├── codecov.yml ├── coefplot.Rproj ├── cran-comments.md ├── hex └── hex_builder.r ├── inst ├── coefMatching.r └── deprecated │ ├── modelInfo.r │ └── utilities.r ├── man ├── annotateSeries.Rd ├── buildModelCI.Rd ├── buildModelCI.default.Rd ├── buildPlotting.default.Rd ├── buildPlottingPloty.default.Rd ├── coefpath.Rd ├── coefplot.Rd ├── coefplot.data.frame.Rd ├── doRegex.Rd ├── extract.coef.Rd ├── extract.coef.cv.glmnet.Rd ├── extract.coef.default.Rd ├── extract.coef.glm.Rd ├── extract.coef.glmnet.Rd ├── extract.coef.lm.Rd ├── extract.coef.maxLik.Rd ├── extract.coef.rxGlm.Rd ├── extract.coef.rxLinMod.Rd ├── extract.coef.rxLogit.Rd ├── extract.coef.xgb.Booster.Rd ├── extractPath.Rd ├── get.assign.Rd ├── get.assign.glm.Rd ├── get.assign.lm.Rd ├── getCoefsFromPredictors.Rd ├── getCoefsFromPredictors.default.Rd ├── getCoefsFromPredictors.rxGlm.Rd ├── getCoefsFromPredictors.rxLinMod.Rd ├── getCoefsFromPredictors.rxLogit.Rd ├── getCoefsFromPredictorsRevo.Rd ├── invlogit.Rd ├── matchCoefs.Rd ├── matchCoefs.default.Rd ├── multiplot.Rd └── position_dodgev.Rd ├── renv.lock ├── renv ├── .gitignore └── activate.R └── tests ├── testthat.R └── testthat ├── teardown-xyz.r ├── test-Transformations.R ├── test-buildModelCI.R ├── test-coefpath.R ├── test-extract-coef-maxLik.r ├── test-extract-coefs.r ├── test-extract.coef.glmnet.R ├── test-extract.coef.xgb.Booster.R ├── test-extractPath.R ├── test-frame-builder.r ├── test-interactive.R └── test-tidymodels.R /.Rbuildignore: -------------------------------------------------------------------------------- 1 | ^renv$ 2 | ^renv\.lock$ 3 | ^\.Rproj\.user$ 4 | ^.*\.Rproj$ 5 | .gitignore 6 | LISCENSE 7 | test 8 | utilities.r 9 | ^.git/ 10 | inst/ 11 | .Rhistory 12 | ^README\.Rmd$ 13 | ^README-.*\.png$ 14 | ^cran-comments\.md$ 15 | ^codecov\.yml$ 16 | hex/ 17 | ^\.github$ 18 | ^CRAN-RELEASE$ 19 | ^CRAN-SUBMISSION$ 20 | revdep/ 21 | -------------------------------------------------------------------------------- /.Rprofile: -------------------------------------------------------------------------------- 1 | source("renv/activate.R") 2 | -------------------------------------------------------------------------------- /.github/.gitignore: -------------------------------------------------------------------------------- 1 | *.html 2 | -------------------------------------------------------------------------------- /.github/workflows/R-CMD-check.yaml: -------------------------------------------------------------------------------- 1 | # For help debugging build failures open an issue on the RStudio community with the 'github-actions' tag. 2 | # https://community.rstudio.com/new-topic?category=Package%20development&tags=github-actions 3 | on: 4 | push: 5 | branches: 6 | - main 7 | - master 8 | pull_request: 9 | branches: 10 | - main 11 | - master 12 | 13 | name: R-CMD-check 14 | 15 | jobs: 16 | R-CMD-check: 17 | runs-on: ${{ matrix.config.os }} 18 | 19 | name: ${{ matrix.config.os }} (${{ matrix.config.r }}) 20 | 21 | strategy: 22 | fail-fast: false 23 | matrix: 24 | config: 25 | - {os: windows-latest, r: 'release'} 26 | - {os: macOS-latest, r: 'release'} 27 | - {os: ubuntu-20.04, r: 'release', rspm: "https://packagemanager.rstudio.com/cran/__linux__/focal/latest"} 28 | - {os: ubuntu-20.04, r: 'devel', rspm: "https://packagemanager.rstudio.com/cran/__linux__/focal/latest"} 29 | 30 | env: 31 | R_REMOTES_NO_ERRORS_FROM_WARNINGS: true 32 | RSPM: ${{ matrix.config.rspm }} 33 | GITHUB_PAT: ${{ secrets.GITHUB_TOKEN }} 34 | 35 | steps: 36 | - uses: actions/checkout@v2 37 | 38 | - uses: r-lib/actions/setup-r@v1 39 | with: 40 | r-version: ${{ matrix.config.r }} 41 | 42 | - uses: r-lib/actions/setup-pandoc@v1 43 | 44 | - name: Query dependencies 45 | run: | 46 | install.packages('remotes') 47 | saveRDS(remotes::dev_package_deps(dependencies = TRUE), ".github/depends.Rds", version = 2) 48 | writeLines(sprintf("R-%i.%i", getRversion()$major, getRversion()$minor), ".github/R-version") 49 | shell: Rscript {0} 50 | 51 | - name: Cache R packages 52 | if: runner.os != 'Windows' 53 | uses: actions/cache@v2 54 | with: 55 | path: ${{ env.R_LIBS_USER }} 56 | key: ${{ runner.os }}-${{ hashFiles('.github/R-version') }}-1-${{ hashFiles('.github/depends.Rds') }} 57 | restore-keys: ${{ runner.os }}-${{ hashFiles('.github/R-version') }}-1- 58 | 59 | - name: Install system dependencies 60 | if: runner.os == 'Linux' 61 | run: | 62 | while read -r cmd 63 | do 64 | eval sudo $cmd 65 | done < <(Rscript -e 'writeLines(remotes::system_requirements("ubuntu", "20.04"))') 66 | 67 | - name: Install dependencies 68 | run: | 69 | remotes::install_deps(dependencies = TRUE) 70 | remotes::install_cran("rcmdcheck") 71 | shell: Rscript {0} 72 | 73 | - name: Check 74 | env: 75 | _R_CHECK_CRAN_INCOMING_REMOTE_: false 76 | run: rcmdcheck::rcmdcheck(args = c("--no-manual", "--as-cran"), error_on = "warning", check_dir = "check") 77 | shell: Rscript {0} 78 | 79 | - name: Upload check results 80 | if: failure() 81 | uses: actions/upload-artifact@main 82 | with: 83 | name: ${{ runner.os }}-r${{ matrix.config.r }}-results 84 | path: check 85 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | .Rproj.user 2 | .Rhistory 3 | inst/doc 4 | -------------------------------------------------------------------------------- /DESCRIPTION: -------------------------------------------------------------------------------- 1 | Package: coefplot 2 | Type: Package 3 | Title: Plots Coefficients from Fitted Models 4 | Authors@R: 5 | person(given = "Jared", 6 | family = "Lander", 7 | role = c("aut", "cre"), 8 | email = "packages@jaredlander.com", 9 | comment = c(ORCID = "0000-0002-7291-726X")) 10 | Version: 1.2.8 11 | Date: 2022-01-12 12 | Maintainer: Jared P. Lander 13 | Description: Plots the coefficients from model objects. This very quickly shows the user the point estimates and confidence intervals for fitted models. 14 | License: BSD_3_clause + file LICENSE 15 | LazyLoad: yes 16 | Depends: 17 | ggplot2 (>= 2.0.0) 18 | Imports: 19 | plyr, 20 | reshape2, 21 | useful, 22 | stats, 23 | dplyr (>= 0.6.0), 24 | dygraphs, 25 | tibble, 26 | magrittr, 27 | purrr, 28 | plotly 29 | ByteCompile: TRUE 30 | Packaged: 2018-01-02 Jared 31 | Suggests: 32 | testthat (>= 2.0.0), 33 | covr, 34 | glmnet, 35 | maxLik, 36 | xgboost, 37 | workflows, 38 | parsnip, 39 | knitr, 40 | rmarkdown 41 | Encoding: UTF-8 42 | RoxygenNote: 7.1.2 43 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | YEAR: 2011-2022 2 | COPYRIGHT HOLDER: Jared Lander 3 | ORGANIZATION: Lander Analytics 4 | -------------------------------------------------------------------------------- /NAMESPACE: -------------------------------------------------------------------------------- 1 | # Generated by roxygen2: do not edit by hand 2 | 3 | S3method(buildModelCI,default) 4 | S3method(coefpath,cv.glmnet) 5 | S3method(coefpath,glmnet) 6 | S3method(coefplot,data.frame) 7 | S3method(coefplot,default) 8 | S3method(coefplot,glm) 9 | S3method(coefplot,lm) 10 | S3method(coefplot,rxGlm) 11 | S3method(coefplot,rxLinMod) 12 | S3method(coefplot,rxLogit) 13 | S3method(extract.coef,cv.glmnet) 14 | S3method(extract.coef,maxLik) 15 | S3method(extract.coef,xgb.Booster) 16 | S3method(extractPath,cv.glmnet) 17 | S3method(extractPath,glmnet) 18 | export(buildModelCI) 19 | export(buildModelCI.default) 20 | export(buildPlottingPloty.default) 21 | export(coefpath) 22 | export(coefplot) 23 | export(coefplot.data.frame) 24 | export(coefplot.default) 25 | export(coefplot.glm) 26 | export(coefplot.lm) 27 | export(coefplot.rxGlm) 28 | export(coefplot.rxLinMod) 29 | export(coefplot.rxLogit) 30 | export(extract.coef) 31 | export(extract.coef.maxLik) 32 | export(extractPath) 33 | export(invlogit) 34 | export(multiplot) 35 | export(plotcoef) 36 | export(position_dodgev) 37 | import(ggplot2) 38 | import(plyr) 39 | import(reshape2) 40 | import(useful) 41 | importFrom(magrittr,"%>%") 42 | -------------------------------------------------------------------------------- /NEWS.md: -------------------------------------------------------------------------------- 1 | # Version 1.2.8 2 | - Methods for plotting `{workflows}` and `{parsnip}` models. 3 | - Fixed warning due to deprecation of `guide` argument in `{ggplot2}`. 4 | 5 | # Version 1.2.7 6 | - Coefficients near zero in `{xgboost}` models are not returned and the user can set the tolerance level. 7 | - Changed the default `lwdOuter` to be 0 on Windows machines and 0.5 on non-Windows machines. Based on a pull request from [xfim](https://github.com/xfim). 8 | - Removed warnings when calling `coefplot()` on a `{glmnet}` model. 9 | - New `interactive` argument causes the plot to be interactive, using `{plotly}`. 10 | 11 | # Version 1.2.6 12 | - `coefpath()` has an argument for ID and assigns a random ID to the `{dygraph}` object if one is not supplied. 13 | - Zero-valued coefficients in `{xgboost}` models are not returned by default. 14 | 15 | # Version 1.2.5 16 | - Added `coefpath` to plot the coefficient path resulting from a `glmnet` object. 17 | - `coefplot` now displays the coefficients from a linear `xgboost` model. 18 | 19 | # Version 1.2.4 20 | - Patched to accommodate changes to `{ggplot2}`. 21 | 22 | # Version 1.2.3 23 | - Can run `coefplot()` on a `data.frame` that is properly setup like on resulting from `coefplot(..., plot=FALSE)`. 24 | 25 | # Version 1.2.2 26 | - Support for `{glmnet}` models. Added tests. 27 | 28 | # Version 1.2.1 29 | - In `mulitplot()` there is now an option to reverse the order of the legend so it matches the ordering in the plot. 30 | 31 | # Version 1.2.0 32 | - Major update. The codebase has been refactored to run faster and be easily updated for improvements and adding new models. 33 | - New arguments (predictors and coefficients) for specifying coefficients to be plotted both by indicating the specific coefficients (including specific factor levels) or by specifying the predictors that created coefficients. This is particularly important for factor variables and interactions. 34 | - New argument (newNames) for giving new names to coefficients. 35 | - Mulitplot can be used to plot Andy Gelman's secret weapon by setting secret.weapon to TRUE and selecting just one coefficient in coefficients. For a vertical secret weapon set by to "Model" and horizontal to FALSE. 36 | - Faceting a single model no longer works. 37 | 38 | 39 | #Version 1.1.9 40 | - Refactoring of code to make new models easier to add. 41 | - For now this means certain functionality will be lost, such as the shortening of coefficient names, plot a factor variable numerically. 42 | 43 | # Version 1.1.8 44 | - Minor changes to plotting to reflect change in `{gpplot2}` 0.9.2. 45 | 46 | # Version 1.1.7 47 | - Thanks to Felipe Carrillo I have fixed a bug in `multiplot()`. Previously, if multiple models with the same formula but different data.frames were inputed then they would all have the same name (even if specified with the `names` argument) and only one model would be plotted. This now works as expected, plotting all the models regardless of identical formulas. 48 | 49 | # Version 1.1.6 50 | - Made change to `reshape2::melt()` so that `variable.name` and `value.name` behave properly with new ggplot2 51 | 52 | # Version 1.1.5 53 | - Fixed glitch that didn't plot inner CI for single plots or multipane multiplots 54 | 55 | # Version 1.1.4 56 | - Added functionality for `rxLogit()` 57 | 58 | # Version 1.1.2 59 | - `multiplot()` can plot in a single pane or in facets 60 | - Changed sort argument to use `match.arg()` 61 | 62 | # Version 1.1.1 63 | - Adjusting the S3 dispatch for `coefplot.rxLinMod()` 64 | 65 | # Version 1.1 66 | - Added `multiplot()` to plot numerous models at once 67 | - Moved a lot of code in `coefplot.lm()` to their own functions 68 | 69 | # Version 1.0 70 | - First build 71 | - Methods for plotting coefficients from `lm()`, `glm()` and `rxLinMod()` 72 | -------------------------------------------------------------------------------- /R/buildPlot.r: -------------------------------------------------------------------------------- 1 | #' Coefplot plotting 2 | #' 3 | #' Build ggplot object for coefplot 4 | #' 5 | #' This function builds up the ggplot layer by layer for \code{\link{coefplot.lm}} 6 | #' 7 | #' @author Jared P. Lander www.jaredlander.com 8 | #' @seealso \code{\link{coefplot.default}} \code{\link{coefplot}} \code{\link{multiplot}} 9 | #' @aliases buildPlotting.default 10 | #' @param modelCI An object created by \code{\link{buildModelCI}} 11 | #' @param title The name of the plot, if NULL then no name is given 12 | #' @param xlab The x label 13 | #' @param ylab The y label 14 | #' @param innerCI How wide the inner confidence interval should be, normally 1 standard deviation. If 0, then there will be no inner confidence interval. 15 | #' @param outerCI How wide the outer confidence interval should be, normally 2 standard deviations. If 0, then there will be no outer confidence interval. 16 | #' @param multi logical; If this is for \code{\link{multiplot}} then leave the colors as determined by the legend, if FALSE then make all colors the same 17 | #' @param lwdInner The thickness of the inner confidence interval 18 | #' @param lwdOuter The thickness of the outer confidence interval 19 | #' @param pointSize Size of coefficient point 20 | #' @param color The color of the points and lines 21 | #' @param shape The shape of the points 22 | #' @param linetype The linetype of the error bars 23 | #' @param cex The text size multiplier, currently not used 24 | #' @param textAngle The angle for the coefficient labels, 0 is horizontal 25 | #' @param numberAngle The angle for the value labels, 0 is horizontal 26 | #' @param zeroColor The color of the line indicating 0 27 | #' @param zeroLWD The thickness of the 0 line 28 | #' @param zeroType The type of 0 line, 0 will mean no line 29 | #' @param facet logical; If the coefficients should be faceted by the variables, numeric coefficients (including the intercept) will be one facet 30 | #' @param scales The way the axes should be treated in a faceted plot. Can be c("fixed", "free", "free_x", "free_y") 31 | #' @param numeric logical; If true and factors has exactly one value, then it is displayed in a horizontal graph with continuous confidence bounds. 32 | #' @param fillColor The color of the confidence bounds for a numeric factor 33 | #' @param alpha The transparency level of the numeric factor's confidence bound 34 | #' @param horizontal logical; If the plot should be displayed horizontally 35 | #' @param value Name of variable for value metric 36 | #' @param coefficient Name of variable for coefficient names 37 | #' @param errorHeight Height of error bars 38 | #' @param dodgeHeight Amount of vertical dodging 39 | #' @param interactive If `TRUE` an interactive plot is generated instead of `[ggplot2]` 40 | #' @md 41 | #' @return a ggplot graph object 42 | #' @examples 43 | #' 44 | #' data(diamonds) 45 | #' model1 <- lm(price ~ carat + cut, data=diamonds) 46 | #' theCI <- coefplot:::buildModelCI(model1) 47 | #' coefplot:::buildPlotting.default(theCI) 48 | #' coefplot(model1) 49 | #' 50 | buildPlotting.default <- function( 51 | modelCI, 52 | title="Coefficient Plot", 53 | xlab="Value", ylab="Coefficient", 54 | lwdInner=1 + interactive*2, 55 | lwdOuter=if(interactive) 1 else unname((Sys.info()["sysname"] != 'Windows')*0.5), 56 | pointSize=3 + interactive*5, 57 | color="blue", cex=.8, textAngle=0, numberAngle=0, 58 | shape=16, linetype=1, 59 | outerCI=2, innerCI=1, multi=FALSE, 60 | zeroColor="grey", zeroLWD=1, zeroType=2, 61 | numeric=FALSE, fillColor="grey", alpha=1/2, 62 | horizontal=FALSE, facet=FALSE, scales="free", 63 | value="Value", coefficient="Coefficient", 64 | errorHeight=0, dodgeHeight=1, 65 | interactive=FALSE 66 | ) 67 | { 68 | if(interactive) 69 | { 70 | return( 71 | buildPlottingPloty.default( 72 | modelCI=modelCI, 73 | title=title, 74 | xlab=xlab, ylab=ylab, 75 | lwdInner=lwdInner, 76 | lwdOuter=lwdOuter, 77 | color=color, shape=shape, 78 | pointSize=pointSize 79 | ) 80 | ) 81 | } 82 | 83 | ## build the layer infos 84 | outerCIGeom <- geom_errorbarh( 85 | aes_string(xmin="LowOuter", xmax="HighOuter", color="Model", linetype="Model"), 86 | lwd=lwdOuter, height=errorHeight, position=position_dodgev(height=dodgeHeight), 87 | na.rm=TRUE 88 | ) 89 | 90 | innerCIGeom <- geom_errorbarh( 91 | aes_string(xmin="LowInner", xmax="HighInner", color="Model", linetype="Model"), 92 | lwd=lwdInner, height=errorHeight, position=position_dodgev(height=dodgeHeight), 93 | na.rm=TRUE 94 | ) 95 | 96 | # ribbon layer 97 | #ribbonGeom <- list(None=NULL, geom_ribbon(aes(ymin=LowOuter, ymax=HighOuter, group=Checkers), data=modelCI, fill=fillColor, alpha=alpha, lwd=lwdOuter)) 98 | 99 | # point layer 100 | pointGeom <- geom_point(aes_string(x=value, color="Model", shape="Model"), size=pointSize, position=position_dodgev(height=dodgeHeight)) 101 | 102 | #colorAes <- list(None=NULL, Single=aes(color=as.factor(Model))) 103 | colorScaleSingle <- scale_color_manual(values=rep(color, length(unique(modelCI$Model))), guide='none') 104 | shapeScaleSingle <- scale_shape_manual(values=rep(shape, length(unique(modelCI$Model))), guide='none') 105 | linetypeScaleSingle <- scale_linetype_manual(values=rep(linetype, length(unique(modelCI$Model))), guide='none') 106 | 107 | xScale <- list(None=NULL, Single=scale_x_discrete()) 108 | 109 | # faceting info 110 | faceting <- list(None=NULL, Display=facet_wrap(~Checkers, scales=scales)) 111 | 112 | # for a regular coefplot or a multiplot in seperate facets 113 | #p <- ggplot(data=modelCI, aes_string(x=value)) 114 | p <- ggplot(data=modelCI, aes_string(x=value, y=coefficient)) # the basics of the plot 115 | #p <- p + colorAes[[1 + multi]] # # in case model needs to be factorized, do it here 116 | p <- p + geom_vline(xintercept=0, colour=zeroColor, linetype=zeroType, lwd=zeroLWD) # the zero line 117 | p <- p + outerCIGeom + # the outer CI bars 118 | innerCIGeom # the inner CI bars 119 | p <- p + pointGeom # the points 120 | #p <- p + xScale[[1 + multi]] 121 | p <- p + theme(axis.text.y=element_text(angle=textAngle, hjust=1), axis.text.x=element_text(angle=numberAngle, vjust=.5)) + 122 | labs(title=title, x=xlab, y=ylab) # labeling and text info 123 | p <- p + if(!multi){ list(colorScaleSingle, shapeScaleSingle, linetypeScaleSingle) } 124 | p <- p + faceting[[facet + 1]] # faceting 125 | p <- p + if(horizontal) coord_flip() 126 | 127 | return(p) # return the ggplot object 128 | } 129 | -------------------------------------------------------------------------------- /R/buildPlotPlotly.r: -------------------------------------------------------------------------------- 1 | #' @title buildPlottingPloty.default 2 | #' @description Builds the plotting structure for interactive coefplots 3 | #' @details Uses plotly to make an interactive version of coefplot. Still uses modelCI. 4 | #' @author Jared P. Lander 5 | #' @export 6 | #' @seealso \code{\link{coefplot.default}} \code{\link{coefplot}} \code{\link{buildPlotting.default}} 7 | #' @param modelCI An object created by \code{\link{buildModelCI}} 8 | #' @param title The name of the plot, if NULL then no name is given 9 | #' @param xlab The x label 10 | #' @param ylab The y label 11 | #' @param lwdInner The thickness of the inner confidence interval 12 | #' @param lwdOuter The thickness of the outer confidence interval 13 | #' @param color The color of the points and lines 14 | #' @param shape The shape of the points 15 | #' @param pointSize Size of coefficient point 16 | #' @return a ggplot graph object 17 | #' @examples 18 | #' 19 | #' data(diamonds) 20 | #' mod1 <- lm(price ~ carat + cut, data=diamonds) 21 | #' theCI1 <- coefplot:::buildModelCI(mod1) 22 | #' coefplot:::buildPlottingPloty.default(theCI1) 23 | #' coefplot(mod1, interactive=TRUE) 24 | #' mod2 <- lm(mpg ~ cyl + qsec - 1, data=mtcars) 25 | #' mod3 <- lm(mpg ~ cyl + qsec + disp - 1, data=mtcars) 26 | #' theCI2 <- coefplot:::buildModelCI(mod2) 27 | #' theCI3 <- coefplot:::buildModelCI(mod3) 28 | #' coefplot::buildPlottingPloty.default(theCI2) 29 | #' coefplot::buildPlottingPloty.default(theCI3) 30 | #' coefplot(mod2, interactive=TRUE) 31 | #' coefplot(mod3, interactive=TRUE) 32 | #' 33 | #' mod4 <- glmnet::glmnet( 34 | #' x=as.matrix(diamonds[, c('carat', 'x', 'y', 'z')]), 35 | #' y=diamonds$price 36 | #' ) 37 | #' coefplot(mod4, interactive=TRUE, lambda=0.65) 38 | #' 39 | buildPlottingPloty.default <- function( 40 | modelCI, 41 | title="Coefficient Plot", 42 | xlab="Value", ylab="Coefficient", 43 | lwdInner=3, 44 | lwdOuter=1, 45 | color="blue", shape='circle', 46 | pointSize=8 47 | ) 48 | { 49 | # if there is only one model, use the hard coded version of color 50 | if(length(unique(modelCI$Model)) == 1) 51 | { 52 | outer_line_list <- list(color=color, width=lwdOuter) 53 | inner_line_list <- list(color=color, width=lwdInner) 54 | point_list <- list(symbol=shape, size=pointSize, color=color) 55 | } else 56 | { 57 | outer_line_list <- list(width=lwdOuter) 58 | inner_line_list <- list(width=lwdInner) 59 | point_list <- list(symbol=shape, size=pointSize) 60 | } 61 | 62 | p <- plotly::plot_ly(data=modelCI, y= ~ Coefficient) 63 | 64 | # if the entire column for a CI is not missing, add the segments 65 | # otherwise don't add the segments 66 | # because plotly does not fail gracefully when an entire column is NA 67 | if(!all(is.na(modelCI$LowInner)) && !all(is.na(modelCI$HighInner))) 68 | { 69 | p <- p %>% 70 | # outer confidence interval 71 | plotly::add_segments( 72 | x=~LowOuter, 73 | xend=~HighOuter, 74 | y=~Coefficient, 75 | yend=~Coefficient, 76 | text=~Coefficient, 77 | # color=~Model, 78 | line=outer_line_list, 79 | # only showing legend for dots 80 | showlegend=FALSE, 81 | # but all elements are connected 82 | # so clicking on a dot for a model, removes the bars too 83 | legendgroup=~Model, 84 | hoverinfo='text' 85 | ) %>% 86 | # inner confidence interval 87 | plotly::add_segments( 88 | x=~LowInner, 89 | xend=~HighInner, 90 | y=~Coefficient, 91 | yend=~Coefficient, 92 | text=~Coefficient, 93 | # color=~Model, 94 | line=inner_line_list, 95 | # only showing legend for dots 96 | showlegend=FALSE, 97 | # but all elements are connected 98 | # so clicking on a dot for a model, removes the bars too 99 | legendgroup=~Model, 100 | hoverinfo='text' 101 | ) 102 | } 103 | 104 | p %>% 105 | # point estimates 106 | plotly::add_markers( 107 | x=~Value, 108 | text=~Coefficient, 109 | # color=~Model, 110 | marker=point_list, 111 | # only showing legend for dots 112 | # this will not show up if there is only one model 113 | showlegend=TRUE, 114 | # but all elements are connected 115 | # so clicking on a dot for a model, removes the bars too 116 | legendgroup=~Model, 117 | hoverinfo='text+x' 118 | ) %>% 119 | plotly::layout( 120 | title=title, 121 | xaxis=list(title=xlab), 122 | yaxis=list(title=ylab) 123 | ) 124 | } 125 | -------------------------------------------------------------------------------- /R/buildPlottingFrame.r: -------------------------------------------------------------------------------- 1 | # make data plotable 2 | 3 | #' @title buildModelCI 4 | #' 5 | #' @description Construct Confidence Interval Values 6 | #' 7 | #' @details Takes a model and builds a data.frame holding the coefficient value and the confidence interval values. 8 | #' 9 | #' @author Jared P. Lander 10 | #' @aliases buildModelCI 11 | #' @export buildModelCI 12 | #' @import plyr 13 | #' @param model A Fitted model such as from lm, glm 14 | #' @param \dots Arguments passed on onto other methods 15 | #' @return A \code{\link{data.frame}} listing coefficients and confidence bands. 16 | #' @seealso \code{\link{coefplot}} \code{\link{multiplot}} 17 | #' @examples 18 | #' 19 | #' data(diamonds) 20 | #' model1 <- lm(price ~ carat + cut, data=diamonds) 21 | #' coefplot:::buildModelCI(model1) 22 | #' coefplot(model1) 23 | #' 24 | buildModelCI <- function(model, ...) 25 | { 26 | UseMethod(generic="buildModelCI") 27 | } 28 | 29 | #' @title buildModelCI.default 30 | #' 31 | #' @description Construct Confidence Interval Values 32 | #' 33 | #' @details Takes a model and builds a data.frame holding the coefficient value and the confidence interval values. 34 | #' 35 | #' @author Jared P. Lander 36 | #' @aliases buildModelCI.default 37 | #' @export buildModelCI.default 38 | #' @export 39 | #' @method buildModelCI default 40 | #' @import plyr 41 | #' @param model A Fitted model such as from lm, glm 42 | #' @param innerCI How wide the inner confidence interval should be, normally 1 standard deviation. If 0, then there will be no inner confidence interval. 43 | #' @param outerCI How wide the outer confidence interval should be, normally 2 standard deviations. If 0, then there will be no outer confidence interval. 44 | #' @param sort Determines the sort order of the coefficients. Possible values are c("natural", "magnitude", "alphabetical") 45 | #' @param decreasing logical; Whether the coefficients should be ascending or descending 46 | #' @param predictors A character vector specifying which variables to keep. Each individual variable has to be specified, so individual levels of factors must be specified. We are working on making this easier to implement, but this is the only option for now. 47 | #' @param coefficients A character vector specifying which factor variables to keep. It will keep all levels and any interactions, even if those are not listed. 48 | #' @param strict If TRUE then predictors will only be matched to its own coefficients, not its interactions 49 | #' @param newNames Named character vector of new names for coefficients 50 | #' @param trans A transformation function to apply to the values and confidence intervals. \code{identity} by default. Use \code{invlogit} for binary regression. 51 | #' @param numeric logical; If true and factors has exactly one value, then it is displayed in a horizontal graph with continuous confidence bounds.; not used for now. 52 | #' @param intercept logical; Whether the Intercept coefficient should be plotted 53 | #' @param interceptName Specifies name of intercept it case it is not the default of "(Intercept"). 54 | #' @param \dots See Details for information on \code{factors}, \code{only} and \code{shorten} 55 | #' @param name A name for the model, if NULL the call will be used 56 | #' @return A \code{\link{data.frame}} listing coefficients and confidence bands. 57 | #' @seealso \code{\link{coefplot}} \code{\link{multiplot}} 58 | #' @examples 59 | #' 60 | #' data(diamonds, package='ggplot2') 61 | #' model1 <- lm(price ~ carat + cut, data=diamonds) 62 | #' coefplot:::buildModelCI(model1) 63 | #' coefplot(model1) 64 | #' 65 | buildModelCI.default <- function(model, outerCI=2, innerCI=1, intercept=TRUE, numeric=FALSE, 66 | sort=c("natural", "magnitude", "alphabetical"), predictors=NULL, strict=FALSE, coefficients=NULL, 67 | newNames=NULL, 68 | trans=identity, 69 | decreasing=TRUE, name=NULL, interceptName="(Intercept)", ...) 70 | { 71 | sort <- match.arg(sort) 72 | 73 | #print(structure(as.list(match.call()[-1]), class = "uneval")$model) 74 | # get model information 75 | modelCI <- extract.coef(model, ...) 76 | 77 | # if the user has specified predictors calculate which coefficient they go with 78 | keptCoefsFromPredictors <- getCoefsFromPredictors(model=model, predictors=predictors, strict=strict) 79 | 80 | # if individual coefficients were specified use them, if not it will be null 81 | keptCoefsFromCoefficients <- coefficients 82 | 83 | if(!is.null(predictors) || !is.null(coefficients)) 84 | { 85 | modelCI <- modelCI[modelCI$Coefficient %in% unique(c(keptCoefsFromPredictors, keptCoefsFromCoefficients)), ] 86 | } 87 | 88 | if(NROW(modelCI) == 0) 89 | { 90 | return(NULL) 91 | } 92 | 93 | if(!is.null(newNames)) 94 | { 95 | modelCI$Coefficient <- revalue(x=modelCI$Coefficient, replace=newNames, warn_missing=FALSE) 96 | } 97 | 98 | # build confidence bounds columns 99 | # modelCI <- within(modelCI, {LowOuter <- Value - outerCI*SE; 100 | # HighOuter <- Value + outerCI*SE; 101 | # LowInner <- Value - innerCI*SE; 102 | # HighInner <- Value + innerCI*SE}) 103 | 104 | modelCI[, c("HighInner", "LowInner", "HighOuter", "LowOuter")] <- modelCI$Value + modelCI$SE %*% matrix(c(innerCI, -innerCI, outerCI, -outerCI), nrow=1) 105 | 106 | # get rid of SE column 107 | modelCI$SE <- NULL 108 | 109 | # if no intercept is desired, remove it 110 | if(!intercept) 111 | { 112 | modelCI <- modelCI[rownames(modelCI) != interceptName, ] 113 | } 114 | 115 | # make column for model name 116 | # if a name for the model is provided, use it, otherwise use the call 117 | if(is.null(name)) 118 | { 119 | #modelCI$Model <- as.character(paste(model$call, collapse="_")) 120 | modelCI$Model <- paste(as.character(structure(as.list(match.call()[-1]), class = "uneval")$model), collapse="") 121 | }else 122 | { 123 | modelCI$Model <- name 124 | } 125 | 126 | # perform a transformation on the numbers if it's not the identity 127 | if(!identical(trans, identity)) 128 | { 129 | modelCI <- dplyr::mutate_at(.tbl=modelCI, .funs=dplyr::funs(trans), 130 | .vars=c('Value', 131 | 'HighInner', 'HighOuter', 132 | 'LowInner', 'LowOuter')) 133 | } 134 | 135 | ## possible orderings of the coefficients 136 | ordering <- switch(sort, 137 | natural=order(1:nrow(modelCI), decreasing=decreasing), # the way the data came in 138 | magnitude=order(modelCI$Value, decreasing=decreasing), # size order 139 | alphabetical=order(modelCI$Coefficient, decreasing=decreasing), # alphabetical order 140 | order(1:nrow(modelCI)) # default, the way it came in 141 | ) 142 | 143 | 144 | # implement the ordering 145 | modelCI <- modelCI[ordering, ] 146 | modelCI$Coefficient <- factor(modelCI$Coefficient, levels=modelCI$Coefficient) 147 | 148 | return(modelCI) 149 | } 150 | 151 | 152 | # #' Melt the modelCI 153 | # #' 154 | # #' Melt a modelCI into a form suitable for plotting 155 | # #' 156 | # #' \code{\link{buildModelCI}} builds a data.frame for plotting. This function melts it into plottable form and seperates the coefficient data from the SE data into seprate data.frames 157 | # #' 158 | # #' @author Jared P. Lander www.jaredlander.com 159 | # #' @aliases meltModelCI 160 | # #' @seealso \code{\link{coefplot}} \code{\link{buildModelCI}} 161 | # #' @param modelCI A \code{\link{data.frame}} as built by \code{\link{buildModelCI}} 162 | # #' @param keepCols The columns in modelCI that should be kept as there can be extras 163 | # #' @param id.vars The columns to use as ID variables in \code{\link{melt}} 164 | # #' @param variable.name Used in \code{\link{melt}} for naming the column that stores the melted variables 165 | # #' @param value.name Used in \code{\link{melt}} for naming the column that stores the melted values 166 | # #' @param innerCols The columns to be included in the \code{\link{data.frame}} of inner standard errors 167 | # #' @param outerCols The columns to be included in the \code{\link{data.frame}} of outer standard errors 168 | # #' @return A list consisting of 169 | # #' \item{modelMelt}{Melted modelCI with all values} 170 | # #' \item{modelMeltOuter}{modelMelt with only values associated with the outer standard errors} 171 | # #' \item{modelMeltInner}{modelMelt with only values associated with the inner standard errors} 172 | # #' @examples 173 | # #' 174 | # #' data(diamonds) 175 | # #' model1 <- lm(price ~ carat + cut, data=diamonds) 176 | # #' \dontrun{modeled <- coefplot:::buildModelCI(model1) 177 | # #' coefplot:::meltModelCI(modeled)} 178 | # #' coefplot(model1) 179 | # #' 180 | # meltModelCI <- function(modelCI, 181 | # keepCols=c("LowOuter", "HighOuter", "LowInner", "HighInner", "Coefficient", "Value", "Model"), 182 | # id.vars=c("Coefficient", "Model"), variable.name="Type", 183 | # value.name="Value", outerCols=c("LowOuter", "HighOuter"), 184 | # innerCols=c("LowInner", "HighInner")) 185 | # { 186 | # # melt the data frame so it is suitable for ggplot 187 | # modelMelt <- melt(data=modelCI[, keepCols], id.vars=id.vars, variable.name=variable.name, value.name=value.name) 188 | # 189 | # # just the outerCI info 190 | # modelMeltOuter <- modelMelt[modelMelt$Type %in% outerCols, ] # pull out the outer (95% default) CI 191 | # 192 | # # just the innerCI info 193 | # modelMeltInner <- modelMelt[modelMelt$Type %in% innerCols, ] # pull out the inner (68% default) CI 194 | # 195 | # # return the data.frames 196 | # return(list(modelMelt=modelMelt, modelMeltOuter=modelMeltOuter, modelMeltInner=modelMeltInner)) 197 | # } 198 | -------------------------------------------------------------------------------- /R/coefPredMatching.r: -------------------------------------------------------------------------------- 1 | # 2 | # baseball <- data.frame(Bat=sample(1:100, 20, replace=T), Batter=sample(c("David", "Batley", "Bob", "Ace"), 20, replace=T), Hits=sample(1:20, 20, replace=T)) 3 | # baseball <- data.frame(Batter=sample(1:100, 10000, replace=T), Bat=sample(c("David", "Batley", "Bob", "Ace"), 10000, replace=T), Hits=sample(1:100, 10000, replace=T), Team=sample(c("Braves", "Red Sox", "Yankees", "Phillies"), size=10000, replace=T)) 4 | # bMod <- lm(Hits ~ Bat*Batter, baseball) 5 | # bLinMod <- rxLinMod(Hits ~ Bat*Batter + Bat*Team, baseball) 6 | # matchCoefs(bMod) 7 | 8 | #' @title get.assign 9 | #' @description The assignment vector for a model 10 | #' @details Gets relative positions of predictors 11 | #' @aliases get.assign 12 | #' @author Jared P. Lander 13 | #' @param model Fitted model 14 | #' @param \dots Further arguments 15 | #' @return The assignment vector 16 | get.assign <- function(model, ...) 17 | { 18 | UseMethod("get.assign") 19 | } 20 | 21 | #' @title get.assign.lm 22 | #' @description The assignment vector for an lm model 23 | #' @details Gets relative positions of predictors 24 | #' @aliases get.assign.lm 25 | #' @author Jared P. Lander 26 | #' @param model Fitted model 27 | #' @param \dots Further arguments 28 | #' @return The assignment vector 29 | get.assign.lm <- function(model, ...) 30 | { 31 | model$assign 32 | } 33 | 34 | #' @title get.assign.glm 35 | #' @description The assignment vector for a glm model 36 | #' @details Gets relative positions of predictors 37 | #' @aliases get.assign.glm 38 | #' @author Jared P. Lander 39 | #' @param model Fitted model 40 | #' @param \dots Further arguments 41 | #' @return The assignment vector 42 | get.assign.glm <- function(model, ...) 43 | { 44 | # build model.matrix 45 | theMat <- stats::model.matrix(object=model$formula, data=model$data) 46 | # get assignment 47 | attr(theMat, "assign") 48 | } 49 | 50 | #' @title matchCoefs 51 | #' @description Match coefficients to predictors 52 | #' @details Matches coefficients to predictors using information from model matrices 53 | #' @author Jared P. Lander 54 | #' @aliases matchCoefs 55 | #' @param model Fitted model 56 | #' @param \dots Further arguments 57 | #' @return a data.frame matching predictors to coefficients 58 | #' @examples 59 | #' \dontrun{ 60 | #' require(reshape2) 61 | #' require(plyr) 62 | #' data("tips", package="reshape2") 63 | #' mod1 <- lm(tip ~ total_bill * sex + day, tips) 64 | #' mod2 <- lm(tip ~ total_bill * sex + day - 1, tips) 65 | #' mod3 <- glm(tip ~ total_bill * sex + day, tips, family=gaussian(link="identity")) 66 | #' mod4 <- lm(tip ~ (total_bill + sex + day)^3, tips) 67 | #' mod5 <- lm(tip ~ total_bill * sex + day + I(total_bill^2), tips) 68 | #' coefplot:::matchCoefs(mod1) 69 | #' coefplot:::matchCoefs(mod2) 70 | #' coefplot:::matchCoefs(mod3) 71 | #' coefplot:::matchCoefs(mod4) 72 | #' coefplot:::matchCoefs(mod5) 73 | #' } 74 | # 75 | matchCoefs <- function(model, ...) 76 | { 77 | UseMethod(generic="matchCoefs") 78 | } 79 | 80 | #' @title matchCoefs.default 81 | #' @description Match coefficients to predictors 82 | #' @details Matches coefficients to predictors using information from model matrices 83 | #' @author Jared P. Lander 84 | #' @aliases matchCoefs.default 85 | #' @import reshape2 86 | #' @param model Fitted model 87 | #' @param \dots Further arguments 88 | #' @return a data.frame matching predictors to coefficients 89 | matchCoefs.default <- function(model, ...) 90 | { 91 | # get the terms 92 | theTerms <- model$terms 93 | # get the assignment position 94 | #thePos <- model$assign 95 | thePos <- get.assign(model) 96 | # get intercept indicator 97 | inter <- attr(theTerms, "intercept") 98 | # get coef names 99 | coefNames <- names(stats::coef(model)) 100 | # get pred names 101 | predNames <- attr(theTerms, "term.labels") 102 | # expand out pred names to match coefficient names 103 | predNames <- predNames[thePos] 104 | # if there's an intercept term add it to the pred names 105 | if(inter == 1) 106 | { 107 | predNames <- c("(Intercept)", predNames) 108 | } 109 | 110 | # build data.frame linking term to coefficient name 111 | matching <- data.frame(Term=predNames, Coefficient=coefNames, stringsAsFactors=FALSE) 112 | 113 | ## now match individual predictor to term 114 | # get matrix as data.frame 115 | factorMat <- as.data.frame(attr(theTerms, "factor")) 116 | # add column from rownames as identifier 117 | factorMat$.Pred <- rownames(factorMat) 118 | factorMat$.Type <- attr(theTerms, "dataClasses") 119 | 120 | # melt it down for comparison 121 | factorMelt <- melt(factorMat, id.vars=c(".Pred", ".Type"), variable.name="Term") 122 | factorMelt$Term <- as.character(factorMelt$Term) 123 | 124 | # only keep rows where there's a match 125 | factorMelt <- factorMelt[factorMelt$value == 1, ] 126 | 127 | # again, bring in coefficient if needed 128 | if(inter == 1) 129 | { 130 | factorMelt <- rbind(data.frame(.Pred="(Intercept)", .Type="(Intercept)", Term="(Intercept)", value=1, stringsAsFactors=FALSE), factorMelt) 131 | } 132 | 133 | # join into the matching data.frame 134 | matching <- join(matching, factorMelt, by="Term") 135 | 136 | return(matching) 137 | } 138 | 139 | # theTerms <- mod1$terms 140 | # thePos <- mod1$assign 141 | # inter <- attr(theTerms, "intercept") 142 | # coefNames <- names(coef(mod1)) 143 | # coefNames 144 | # predNames <- attr(theTerms, "term.labels") 145 | # predNames <- predNames[thePos] 146 | # factorMat <- as.data.frame(attr(theTerms, "factor")) 147 | # factorMat$.Pred <- rownames(factorMat) 148 | # factorMelt <- melt(factorMat, id.vars=".Pred") 149 | # factorMelt <- factorMelt[factorMelt$value == 1, ] 150 | # 151 | # 152 | 153 | #' @title getCoefsFromPredictors 154 | #' 155 | #' @description Generic function for finding which coefficients go with which predictors 156 | #' 157 | #' @details The user specifies predictors whose coefficients should be included in the coefplot. 158 | #' @aliases getCoefsFromPredictors 159 | #' @author Jared P. Lander 160 | #' @return A character vector of coefficients listing the coefficients that match the predictor 161 | #' @param model A fitted model 162 | #' @param predictors A character vector of predictors to match against 163 | #' @param \dots further arguments 164 | getCoefsFromPredictors <- function(model, predictors, ...) 165 | { 166 | UseMethod(generic="getCoefsFromPredictors", object=model) 167 | } 168 | 169 | 170 | #' @title getCoefsFromPredictors.default 171 | #' 172 | #' @description Default function (lm, glm) for matching coefficients with predictors 173 | #' 174 | #' @details The user specifies predictors whose coefficients should be included in the coefplot. 175 | #' @aliases getCoefsFromPredictors.default 176 | #' @author Jared P. Lander 177 | #' @return A character vector of coefficients listing the coefficients that match the predictor 178 | #' @param model A fitted model 179 | #' @param predictors A character vector of predictors to match against. Interactions can be explicitly specified by VariableA:VariableB. 180 | #' @param strict Logical specifying if interactions terms should be included (\code{FALSE}) or just the main terms (\code{TRUE}). 181 | #' @param \dots further arguments 182 | getCoefsFromPredictors.default <- function(model, predictors=NULL, strict=FALSE, ...) 183 | { 184 | # if no predictors indicated just return NULL 185 | if(is.null(predictors)) 186 | { 187 | return(NULL) 188 | } 189 | 190 | # build data.frame matching predictors with coefficients 191 | matchPredsCoefs <- matchCoefs(model) 192 | 193 | # find out which coefficients we'll be keeping 194 | # if strict, it will only match the singleton term 195 | # if not strict it will match interactions 196 | if(!strict) 197 | { 198 | toKeepNotStrict <- which(matchPredsCoefs$.Pred %in% predictors) 199 | }else 200 | { 201 | toKeepNotStrict <- NULL 202 | } 203 | toKeepStrict <- which(matchPredsCoefs$Term %in% predictors) 204 | 205 | keptCoefsFromPredictors <- unique(matchPredsCoefs$Coefficient[unique(c(toKeepNotStrict, toKeepStrict))]) 206 | 207 | return(keptCoefsFromPredictors) 208 | } 209 | 210 | 211 | #' @title getCoefsFromPredictorsRevo 212 | #' 213 | #' @description Function that does the work for Revo models for matching coefficients with predictors 214 | #' 215 | #' @details The user specifies predictors whose coefficients should be included in the coefplot. 216 | #' @aliases getCoefsFromPredictorsRevo 217 | #' @author Jared P. Lander 218 | #' @return A character vector of coefficients listing the coefficients that match the predictor. As of now interactions cannot be explicitly specified. 219 | #' @param model A fitted model 220 | #' @param predictors A character vector of predictors to match against 221 | #' @param strict Logical specifying if interactions terms should be included (\code{FALSE}) or just the main terms (\code{TRUE}). 222 | #' @param \dots further arguments 223 | getCoefsFromPredictorsRevo <- function(model, predictors=NULL, strict=FALSE, ...) 224 | { 225 | # if no predictors indicated just return NULL 226 | if(is.null(predictors)) 227 | { 228 | return(NULL) 229 | } 230 | 231 | predMatcher <- subSpecials(predictors)[[1]] 232 | names(predMatcher) <- predictors 233 | 234 | # get coefficient names 235 | coefNames <- names(stats::coef(model)) 236 | 237 | # if strict do one search 238 | if(strict) 239 | { 240 | # strict 241 | toKeep <- lapply(predMatcher, FUN=doRegex, matchAgainst=coefNames, pattern="^%s(=+[^,]*)*$") 242 | }else 243 | { 244 | # not strict 245 | toKeep <- lapply(predMatcher, FUN=doRegex, matchAgainst=coefNames, pattern="(^| )%s($|,|=| for)") 246 | } 247 | 248 | keepFrame <- ldply(toKeep, function(x) data.frame(Target=x)) 249 | 250 | keptCoefsFromPredictors <- coefNames[keepFrame$Target] 251 | 252 | return(keptCoefsFromPredictors) 253 | } 254 | 255 | 256 | #' @title doRegex 257 | #' 258 | #' @description Helper function for matching coefficients 259 | #' 260 | #' @details Only used by \code{\link{getCoefsFromPredictorsRevo}} for finding matches between predictors and coefficients 261 | #' 262 | #' @aliases doRegex 263 | #' @author Jared P. Lander 264 | #' @param x Root pattern to search for 265 | #' @param matchAgainst Text to search through 266 | #' @param pattern Regex pattern to build x into 267 | #' @return A list of indices of matchAgainst that is matched 268 | doRegex <- function(x, matchAgainst, pattern="(^| )%s($|,|=)") 269 | { 270 | grep(pattern=sprintf(pattern, x), x=matchAgainst) 271 | } 272 | 273 | 274 | #' @title getCoefsFromPredictors.rxLinMod 275 | #' 276 | #' @description Function for matching coefficients with predictors for rxLinMod 277 | #' 278 | #' @details The user specifies predictors whose coefficients should be included in the coefplot. 279 | #' @aliases getCoefsFromPredictors.rxLinMod 280 | #' @author Jared P. Lander 281 | #' @return A character vector of coefficients listing the coefficients that match the predictor 282 | #' @param model A fitted model 283 | #' @param predictors A character vector of predictors to match against 284 | #' @param strict Logical specifying if interactions terms should be included (\code{FALSE}) or just the main terms (\code{TRUE}). 285 | #' @param \dots further arguments 286 | getCoefsFromPredictors.rxLinMod <- function(model, predictors=NULL, strict=FALSE, ...) 287 | { 288 | getCoefsFromPredictorsRevo(model, predictors, strict) 289 | } 290 | 291 | 292 | #' @title getCoefsFromPredictors.rxLogit 293 | #' 294 | #' @description Function for matching coefficients with predictors for rxLogit 295 | #' 296 | #' @details The user specifies predictors whose coefficients should be included in the coefplot. 297 | #' @aliases getCoefsFromPredictors.rxLogit 298 | #' @author Jared P. Lander 299 | #' @return A character vector of coefficients listing the coefficients that match the predictor 300 | #' @param model A fitted model 301 | #' @param predictors A character vector of predictors to match against 302 | #' @param strict Logical specifying if interactions terms should be included (\code{FALSE}) or just the main terms (\code{TRUE}). 303 | #' @param \dots further arguments 304 | getCoefsFromPredictors.rxLogit <- function(model, predictors=NULL, strict=FALSE, ...) 305 | { 306 | getCoefsFromPredictorsRevo(model, predictors, strict) 307 | } 308 | 309 | 310 | #' @title getCoefsFromPredictors.rxGlm 311 | #' 312 | #' @description Function for matching coefficients with predictors for rxGlm 313 | #' 314 | #' @details The user specifies predictors whose coefficients should be included in the coefplot. 315 | #' @aliases getCoefsFromPredictors.rxGlm 316 | #' @author Jared P. Lander 317 | #' @return A character vector of coefficients listing the coefficients that match the predictor 318 | #' @param model A fitted model 319 | #' @param predictors A character vector of predictors to match against 320 | #' @param strict Logical specifying if interactions terms should be included (\code{FALSE}) or just the main terms (\code{TRUE}). 321 | #' @param \dots further arguments 322 | getCoefsFromPredictors.rxGlm <- function(model, predictors=NULL, strict=FALSE, ...) 323 | { 324 | getCoefsFromPredictorsRevo(model, predictors, strict) 325 | } 326 | -------------------------------------------------------------------------------- /R/coefpath.r: -------------------------------------------------------------------------------- 1 | #' @title coefpath 2 | #' @description Visualize the coefficient path resulting from the elastic net 3 | #' @details This is a replacement plot for visualizing the coefficient path resulting from the elastic net. This allows for interactively inspecting the plot so it is easier to disambiguate the coefficients. 4 | #' @author Jared P. Lander 5 | #' @export 6 | #' @param model A \code{\link[glmnet]{glmnet}} model 7 | #' @param \dots Arguments passed on to \code{\link{extractPath}} 8 | #' @return A dygraphs object 9 | #' @examples 10 | #' 11 | #' \dontshow{if(requireNamespace('glmnet', quietly=TRUE))\{} 12 | #' library(glmnet) 13 | #' library(ggplot2) 14 | #' library(useful) 15 | #' data(diamonds) 16 | #' diaX <- useful::build.x(price ~ carat + cut + x - 1, data=diamonds, contrasts = TRUE) 17 | #' diaY <- useful::build.y(price ~ carat + cut + x - 1, data=diamonds) 18 | #' modG1 <- glmnet(x=diaX, y=diaY) 19 | #' coefpath(modG1) 20 | #' 21 | #' modG2 <- cv.glmnet(x=diaX, y=diaY, nfolds=5) 22 | #' coefpath(modG2) 23 | #' 24 | #' x <- matrix(rnorm(100*20),100,20) 25 | #' y <- rnorm(100) 26 | #' fit1 <- glmnet(x, y) 27 | #' coefpath(fit1) 28 | #' \dontshow{\}} 29 | #' 30 | coefpath <- function(model, ...) 31 | { 32 | UseMethod('coefpath') 33 | } 34 | 35 | #' @rdname coefpath 36 | #' @export 37 | #' @importFrom magrittr "%>%" 38 | #' @param xlab x-axis label 39 | #' @param ylab y-axis label 40 | #' @param showLegend When to display the legend. Specify "always" to always show the legend. Specify "onmouseover" to only display it when a user mouses over the chart. Specify "follow" to have the legend show as overlay to the chart which follows the mouse. The default behavior is "auto", which results in "always" when more than one series is plotted and "onmouseover" when only a single series is plotted. 41 | #' @param annotate If \code{TRUE} (default) plot the name of the series 42 | #' @param elementID Unique identified for dygraph, if \code{NULL} it will be randomly generated 43 | #' 44 | coefpath.glmnet <- function(model, 45 | xlab='Log Lambda', 46 | ylab='Coefficients', 47 | showLegend=c('onmouseover', 'auto', 'always', 48 | 'follow' ,'never'), 49 | annotate=TRUE, 50 | elementID=NULL, 51 | ...) 52 | { 53 | # figure out how to show the legend 54 | showLegend <- match.arg(showLegend) 55 | 56 | # get the coefficients in a nice data.frame 57 | pathDF <- extractPath(model, ...) 58 | 59 | # if ID is NULL, make a random ID 60 | if(is.null(elementID)) 61 | { 62 | elementID <- sprintf('coefpath_%s', 63 | paste( 64 | sample(x=c(letters, LETTERS, 0:9), 65 | size=12, 66 | replace=TRUE), 67 | collapse='' 68 | ) 69 | ) 70 | } 71 | 72 | # build the graph 73 | g <- dygraphs::dygraph(pathDF, elementId=elementID) %>% 74 | # nice axis labels 75 | dygraphs::dyAxis(name='x', label=xlab) %>% 76 | dygraphs::dyAxis(name='y', label=ylab) %>% 77 | # control the legend 78 | dygraphs::dyLegend(show='onmouseover') %>% 79 | # allow zooming 80 | dygraphs::dyRangeSelector() %>% 81 | # allow unzooming 82 | dygraphs::dyUnzoom() %>% 83 | dygraphs::dyHighlight(highlightCircleSize=3, 84 | highlightSeriesBackgroundAlpha=0.5, 85 | highlightSeriesOpts=list(strokeWidth=3)) 86 | 87 | if(annotate) 88 | { 89 | g <- purrr::reduce(.x=names(pathDF)[-1], 90 | .f=annotateSeries, 91 | .init=g, 92 | x=min(pathDF$lambda)) 93 | } 94 | 95 | return(g) 96 | } 97 | 98 | #' @rdname coefpath 99 | #' @export 100 | #' @importFrom magrittr "%>%" 101 | #' @param colorMin Color for line showing lambda.min 102 | #' @param strokePatternMin Stroke pattern for line showing lambda.min 103 | #' @param labelMin Label for line showing lambda.min 104 | #' @param locMin Location for line showing lambda.min, can be 'bottom' or 'top' 105 | #' @param color1se Color for line showing lambda.1se 106 | #' @param strokePattern1se Stroke pattern for line showing lambda.1se 107 | #' @param label1se Label for line showing lambda.1se 108 | #' @param loc1se Location for line showing lambda.1se, can be 'bottom' or 'top' 109 | #' 110 | coefpath.cv.glmnet <- function(model, 111 | xlab='Log Lambda', 112 | ylab='Coefficients', 113 | showLegend=c('onmouseover', 'auto', 'always', 114 | 'follow' ,'never'), 115 | annotate=TRUE, 116 | colorMin='black', strokePatternMin='dotted', 117 | labelMin='lambda.min', locMin=c('bottom', 'top'), 118 | color1se='black', strokePattern1se='dotted', 119 | label1se='lambda.1se', loc1se=c('bottom', 'top'), 120 | ...) 121 | { 122 | # figure out how to show the legend 123 | showLegend <- match.arg(showLegend) 124 | locMin <- match.arg(locMin) 125 | loc1se <- match.arg(loc1se) 126 | 127 | g <- coefpath(model$glmnet.fit, ...) 128 | 129 | g %>% 130 | dygraphs::dyEvent(x=log(model$lambda.min), label=labelMin, 131 | color=colorMin, 132 | labelLoc=locMin, strokePattern=strokePatternMin) %>% 133 | dygraphs::dyEvent(x=log(model$lambda.1se), label=label1se, 134 | color=color1se, 135 | labelLoc=loc1se, strokePattern=strokePattern1se) 136 | } 137 | -------------------------------------------------------------------------------- /R/coefplot-package.r: -------------------------------------------------------------------------------- 1 | #' Plotting Model Coefficients 2 | #' 3 | #' Provides an S3 generic method for plotting coefficients from a model so it can be extended to other model types. 4 | #' 5 | #' Currently, methods are available for lm, glm and rxLinMod objects. 6 | #' 7 | # @import plyr ggplot2 reshape2 8 | #' @docType package 9 | #' @name coefplot 10 | #' @aliases coefplot-package 11 | NULL 12 | 13 | 14 | ## quiets concerns of R CMD check re: the .data's that appear in pipelines 15 | ## because dplyr doesn't offer a way to use arrange with Standard evaluation 16 | 17 | if(getRversion() >= "2.15.1") utils::globalVariables(c(".data")) 18 | -------------------------------------------------------------------------------- /R/dodging.r: -------------------------------------------------------------------------------- 1 | # #' collidev 2 | # #' 3 | # #' Vertical collision checking 4 | # #' 5 | # #' A hacky adaptation of ggplot's collide function to be used for vertical collisions. No warranties on this working except that it looks good for now. 6 | # #' 7 | # #' The adaptation switch x's to y's and y's to x's and width to height 8 | # #' 9 | # #' @author Jared P. Lander 10 | # #' @aliases collidev 11 | # #' @import ggplot2 12 | # #' @export collidev 13 | # #' @return Not sure 14 | # #' @param data data 15 | # #' @param height How much to dodge the items 16 | # #' @param name Refer to ggplot2 17 | # #' @param strategy Refer to ggplot2 18 | # #' @param check.height Refer to ggplot2 19 | # #' @examples 20 | # #' # none here 21 | # collidev <- function(data, height = NULL, name, strategy, check.height = TRUE) 22 | # { 23 | # if (!is.null(height)) { 24 | # if (!(all(c("ymin", "ymax") %in% names(data)))) { 25 | # # data <- within(data, { 26 | # # ymin <- y - height/2 27 | # # ymax <- y + height/2 28 | # # }) 29 | # data[, c("ymax", "ymin")] <- data$y + matrix(c(1, -1), ncol=2, nrow=NROW(data), byrow=TRUE)*height/2 30 | # } 31 | # } 32 | # else { 33 | # if (!(all(c("ymin", "ymax") %in% names(data)))) { 34 | # data$ymin <- data$y 35 | # data$ymax <- data$y 36 | # } 37 | # heights <- unique(with(data, ymax - ymin)) 38 | # heights <- heights[!is.na(heights)] 39 | # if (!zero_range(range(heights))) { 40 | # warning(name, " requires constant height: output may be incorrect", 41 | # call. = FALSE) 42 | # } 43 | # height <- heights[1] 44 | # } 45 | # data <- data[order(data$ymin), ] 46 | # intervals <- as.numeric(t(unique(data[c("ymin", "ymax")]))) 47 | # intervals <- intervals[!is.na(intervals)] 48 | # if (length(unique(intervals)) > 1 & any(diff(scale(intervals)) < 49 | # -1e-06)) { 50 | # warning(name, " requires non-overlapping y intervals", 51 | # call. = FALSE) 52 | # } 53 | # if (!is.null(data$xmax)) { 54 | # # this line is commented out and replaced with the one below to deal with CRANs abhorence of non-visible bindings 55 | # #ddply(data, .(ymin), strategy, height = height) 56 | # ddply(data, "ymin", strategy, height = height) 57 | # } 58 | # else if (!is.null(data$x)) { 59 | # message("xmax not defined: adjusting position using x instead") 60 | # # this line is commented out and replaced with the ones below to deal with CRANs abhorence of non-visible bindings 61 | # # transform(ddply(transform(data, xmax = x), .(ymin), strategy, 62 | # # height = height), x = xmax) 63 | # data$xmax <- data$x 64 | # data <- ddply(data, "ymin", strategy, height = height) 65 | # data$x <- data$xmax 66 | # data 67 | # } 68 | # else { 69 | # stop("Neither x nor xmax defined") 70 | # } 71 | # } 72 | # 73 | # #' pos_dodgev 74 | # #' 75 | # #' Vertical dodging internal 76 | # #' 77 | # #' A hacky adaptation of ggplot's pos_dodge function to be used for vertical collisions. No warranties on this working except that it looks good for now. 78 | # #' 79 | # #' The adaptation switch x's to y's and y's to x's and width to height 80 | # #' 81 | # #' @author Jared P. Lander 82 | # #' @aliases pos_dodgev 83 | # #' @export pos_dodgev 84 | # #' @return Not sure 85 | # #' @param df Refer to ggplot2 86 | # #' @param height Refer to ggplot2 87 | # #' @examples 88 | # #' # none here 89 | # pos_dodgev <- function (df, height) 90 | # { 91 | # n <- length(unique(df$group)) 92 | # if (n == 1) 93 | # return(df) 94 | # if (!all(c("ymin", "ymax") %in% names(df))) { 95 | # df$ymin <- df$y 96 | # df$ymax <- df$y 97 | # } 98 | # d_width <- max(df$ymax - df$ymin) 99 | # diff <- height - d_width 100 | # groupidx <- match(df$group, sort(unique(df$group))) 101 | # df$y <- df$y + height * ((groupidx - 0.5)/n - 0.5) 102 | # df$ymin <- df$y - d_width/n/2 103 | # df$ymax <- df$y + d_width/n/2 104 | # df 105 | # } 106 | # 107 | # #' position_dodgev 108 | # #' 109 | # #' Vertical dodging internal 110 | # #' 111 | # #' A hacky adaptation of ggplot's position_dodge function to be used for vertical collisions. No warranties on this working except that it looks good for now. 112 | # #' 113 | # #' No changes were necessary but this is a new geom with a v on the end 114 | # #' 115 | # #' @author Jared P. Lander 116 | # #' @aliases position_dodgev 117 | # #' @return Not sure 118 | # #' @param width Refer to ggplot2 119 | # #' @param height Refer to ggplot2 120 | # #' @export position_dodgev 121 | # #' @examples 122 | # #' # none here 123 | # position_dodgev <- function (width = NULL, height = NULL) { 124 | # #PositionDodgev$new(width = width, height = height) 125 | # ggplot2::ggproto(NULL, PositionDodgev, width=width, height=height) 126 | # } 127 | # 128 | # PositionDodgev <- ggplot2::ggproto("dodgev", ggplot2::Position, 129 | # required_aes = "y", 130 | # height = NULL, 131 | # setup_params = function(self, data) { 132 | # if (is.null(data$ymin) && is.null(data$ymax) && is.null(self$height)) { 133 | # warning("Height not defined. Set with `position_dodgev(height = ?)`", 134 | # call. = FALSE) 135 | # } 136 | # list(height = self$height) 137 | # }, 138 | # 139 | # compute_panel = function(data, params, scales) { 140 | # ggplot2:::collide(data, params$height, "position_dodgev", pos_dodgev, check.width = FALSE) 141 | # } 142 | # ) 143 | -------------------------------------------------------------------------------- /R/dyAnnotation.r: -------------------------------------------------------------------------------- 1 | #' @title annotateSeries 2 | #' @description Annotate a series 3 | #' @details A helper function that changes the order of some options for \code{link[dygraphs]{dyAnnotation}} so it is easier to use with \code{\link[purrr]{reduce}}. 4 | #' @author Jared P. Lander 5 | #' @inheritParams dygraphs::dyAnnotation 6 | #' @param \dots Further arguments passed to \code{link[dygraphs]{dyAnnotation}} 7 | annotateSeries <- function(dygraph, series, x=0, 8 | text=series, tooltip=series, 9 | width=50, 10 | ...) 11 | { 12 | dygraphs::dyAnnotation(dygraph=dygraph, 13 | x=x, 14 | text=text, 15 | series=series, 16 | width=width, 17 | tooltip=tooltip, 18 | ...) 19 | } 20 | -------------------------------------------------------------------------------- /R/extractCoef.r: -------------------------------------------------------------------------------- 1 | # this file extracts coefficients from varying models 2 | #' extract.coef.default 3 | #' 4 | #' Extract Coefficient Information from Models 5 | #' 6 | #' Gets the coefficient values and standard errors, and variable names from a model. 7 | #' 8 | #' @author Jared P. Lander 9 | #' @method extract.coef default 10 | #' @aliases extract.coef.default 11 | #' @param model Model object to extract information from. 12 | #' @param \dots Further arguments 13 | #' @return A \code{\link{data.frame}} containing the coefficient, the standard 14 | #' error and the variable name. 15 | #' @examples 16 | #' \dontrun{ 17 | #' library(ggplot2) 18 | #' library(coefplot) 19 | #' data(diamonds) 20 | #' mod1 <- lm(price ~ carat + cut + x, data=diamonds) 21 | #' extract.coef(mod1) 22 | #' } 23 | #' 24 | extract.coef.default <- function(model, ...) 25 | { 26 | # get summary of model 27 | theSumm <- summary(model) 28 | # get coef and standard error 29 | # print(theSumm) 30 | # print(head(model)) 31 | info <- as.data.frame(theSumm$coefficients[, 1:2]) 32 | names(info) <- c("Value", "SE") 33 | # make a variable tracking the name 34 | info$Coefficient <- rownames(info) 35 | 36 | return(info) 37 | } 38 | 39 | #' extract.coef.lm 40 | #' 41 | #' Extract Coefficient Information from lm Models 42 | #' 43 | #' Gets the coefficient values and standard errors, and variable names from an lm model. 44 | #' 45 | #' @author Jared P. Lander 46 | #' @aliases extract.coef.lm 47 | #' @method extract.coef lm 48 | #' @inheritParams extract.coef.default 49 | #' @return A \code{\link{data.frame}} containing the coefficient, the standard 50 | #' error and the variable name. 51 | #' @examples 52 | #' \dontrun{ 53 | #' library(ggplot2) 54 | #' data(diamonds) 55 | #' library(coefplot) 56 | #' mod1 <- lm(price ~ carat + cut + x, data=diamonds) 57 | #' extract.coef(mod1) 58 | #' } 59 | #' 60 | extract.coef.lm <- function(model, ...) 61 | { 62 | extract.coef.default(model=model, ...) 63 | } 64 | 65 | 66 | #' extract.coef.glm 67 | #' 68 | #' Extract Coefficient Information from glm Models 69 | #' 70 | #' Gets the coefficient values and standard errors, and variable names from a glm model. 71 | #' 72 | #' @author Jared P. Lander 73 | #' @aliases extract.coef.glm 74 | #' @inheritParams extract.coef.default 75 | #' @method extract.coef glm 76 | #' @return A \code{\link{data.frame}} containing the coefficient, the standard 77 | #' error and the variable name. 78 | #' @examples 79 | #' \dontrun{ 80 | #' library(ggplot2) 81 | #' data(diamonds) 82 | #' library(coefplot) 83 | #' mod2 <- glm(price > 10000 ~ carat + cut + x, data=diamonds, family=binomial(link="logit")) 84 | #' extract.coef(mod2) 85 | #' } 86 | #' 87 | extract.coef.glm <- function(model, ...) 88 | { 89 | extract.coef.default(model=model, ...) 90 | } 91 | 92 | 93 | #' extract.coef 94 | #' 95 | #' Extract Coefficient Information from glm Models 96 | #' 97 | #' Gets the coefficient values and standard errors, and variable names from a 98 | #' glm model. 99 | #' 100 | #' @author Jared P. Lander 101 | #' @aliases extract.coef 102 | #' @export extract.coef 103 | #' @param model Model object to extract information from. 104 | #' @param \dots Further arguments 105 | #' @return A \code{\link{data.frame}} containing the coefficient, the standard 106 | #' error and the variable name. 107 | #' @examples 108 | #' \dontrun{ 109 | #' library(ggplot2) 110 | #' data(diamonds) 111 | #' library(coefplot) 112 | #' mod1 <- lm(price ~ carat + cut + x, data=diamonds) 113 | #' mod2 <- glm(price > 10000 ~ carat + cut + x, data=diamonds, family=binomial(link="logit")) 114 | #' mod3 <- lm(price ~ carat*cut + x, data=diamonds) 115 | #' extract.coef(mod1) 116 | #' extract.coef(mod2) 117 | #' extract.coef(mod3) 118 | #' 119 | #' mod4 <- rxLinMod(price ~ carat*cut + x, diamonds) 120 | #' } 121 | #' 122 | extract.coef <- function(model, ...) 123 | { 124 | UseMethod(generic="extract.coef", object=model) 125 | } 126 | 127 | 128 | #' extract.coef.rxLinMod 129 | #' 130 | #' Extract Coefficient Information from rxLinMod Models 131 | #' 132 | #' Gets the coefficient values and standard errors, and variable names from an 133 | #' rxLinMod model. 134 | #' 135 | #' @author Jared P. Lander 136 | #' @aliases extract.coef.rxLinMod 137 | #' @inheritParams extract.coef.default 138 | #' @method extract.coef rxLinMod 139 | #' @return A \code{\link{data.frame}} containing the coefficient, the standard 140 | #' error and the variable name. 141 | #' @examples 142 | #' \dontrun{ 143 | #' library(ggplot2) 144 | #' data(diamonds) 145 | #' mod3 <- rxLinMod(price ~ carat + cut + x, data=diamonds) 146 | #' extract.coef(mod3) 147 | #' } 148 | #' 149 | extract.coef.rxLinMod <- function(model, ...) 150 | { 151 | # get summary 152 | theSumm <- summary(model)[[1]] 153 | # get coefs 154 | info <- as.data.frame(theSumm$coefficients[, 1:2]) 155 | # give good names 156 | names(info) <- c("Value", "SE") 157 | # get variable names 158 | info$Coefficient <- rownames(info) 159 | 160 | return(info) 161 | } 162 | 163 | 164 | #' extract.coef.rxGlm 165 | #' 166 | #' Extract Coefficient Information from rxGlm Models 167 | #' 168 | #' Gets the coefficient values and standard errors, and variable names from an 169 | #' rxGlm model. 170 | #' 171 | #' @author Jared P. Lander 172 | #' @aliases extract.coef.rxGlm 173 | #' @inheritParams extract.coef.default 174 | #' @method extract.coef rxGlm 175 | #' @return A \code{\link{data.frame}} containing the coefficient, the standard 176 | #' error and the variable name. 177 | #' @examples 178 | #' \dontrun{ 179 | #' library(ggplot2) 180 | #' data(diamonds) 181 | #' mod4 <- rxGlm(price ~ carat + cut + x, data=diamonds) 182 | #' mod5 <- rxGlm(price > 10000 ~ carat + cut + x, data=diamonds, fmaily="binomial") 183 | #' extract.coef(mod4) 184 | #' extract.coef(mod5) 185 | #' } 186 | #' 187 | extract.coef.rxGlm <- function(model, ...) 188 | { 189 | extract.coef.default(model=model, ...) 190 | } 191 | 192 | 193 | #' extract.coef.rxLogit 194 | #' 195 | #' Extract Coefficient Information from rxLogit Models 196 | #' 197 | #' Gets the coefficient values and standard errors, and variable names from an 198 | #' rxLogit model. 199 | #' 200 | #' @author Jared P. Lander 201 | #' @aliases extract.coef.rxLogit 202 | #' @inheritParams extract.coef.default 203 | #' @method extract.coef rxLogit 204 | #' @return A \code{\link{data.frame}} containing the coefficient, the standard 205 | #' error and the variable name. 206 | #' @examples 207 | #' \dontrun{ 208 | #' library(ggplot2) 209 | #' data(diamonds) 210 | #' mod6 <- rxLogit(price > 10000 ~ carat + cut + x, data=diamonds) 211 | #' extract.coef(mod6) 212 | #' } 213 | #' 214 | extract.coef.rxLogit <- function(model, ...) 215 | { 216 | extract.coef.default(model=model, ...) 217 | } 218 | 219 | #' @title extract.coef.glmnet 220 | #' @description Extract Coefficient Information from Models 221 | #' @details Gets the coefficient values and variable names from a model. Since 222 | #' glmnet does not have standard errors, those will just be NA. 223 | #' @author Jared P. Lander 224 | #' @method extract.coef glmnet 225 | #' @aliases extract.coef.glmnet 226 | #' @param model Model object from which to extract information. 227 | #' @param lambda Value of penalty parameter 228 | #' @param \dots Further arguments 229 | #' @return A \code{\link{data.frame}} containing the coefficient, the standard 230 | #' error and the variable name. 231 | #' @examples 232 | #' \dontshow{if(requireNamespace('glmnet', quietly=TRUE))\{} 233 | #' library(glmnet) 234 | #' library(ggplot2) 235 | #' library(useful) 236 | #' data(diamonds) 237 | #' diaX <- build.x(price ~ carat + cut + x - 1, data=diamonds, contrasts = TRUE) 238 | #' diaY <- build.y(price ~ carat + cut + x - 1, data=diamonds) 239 | #' modG1 <- glmnet(x=diaX, y=diaY) 240 | #' extract.coef(modG1) 241 | #' } 242 | #' \dontshow{\}} 243 | #' 244 | extract.coef.glmnet <- function(model, lambda=stats::median(model$lambda), ...) 245 | { 246 | # get coefs at given s 247 | theCoef <- as.matrix(stats::coef(model, s=lambda)) 248 | # supressing warning because sometimes there will be two intercepts 249 | # that would generate a rowname warning which really isn't needed 250 | coefDF <- suppressWarnings( 251 | data.frame(Value=theCoef, SE=NA_real_, Coefficient=rownames(theCoef)) 252 | ) 253 | coefDF <- coefDF[theCoef != 0, ] 254 | names(coefDF)[1] <- "Value" 255 | 256 | return(coefDF) 257 | } 258 | 259 | #' @title extract.coef.cv.glmnet 260 | #' @description Extract Coefficient Information from Models 261 | #' @details Gets the coefficient values and variable names from a model. Since 262 | #' glmnet does not have standard errors, those will just be NA. 263 | #' @author Jared P. Lander 264 | #' @method extract.coef cv.glmnet 265 | #' @aliases extract.coef.cv.glmnet 266 | #' @export 267 | #' @param model Model object from which to extract information. 268 | #' @param lambda Value of penalty parameter. Can be either a numeric value or 269 | #' one of "lambda.min" or "lambda.1se" 270 | #' @param \dots Further arguments 271 | #' @return A \code{\link{data.frame}} containing the coefficient, the standard 272 | #' error and the variable name. 273 | #' @examples 274 | #' \dontshow{if(requireNamespace('glmnet', quietly=TRUE))\{} 275 | #' library(glmnet) 276 | #' library(ggplot2) 277 | #' library(useful) 278 | #' data(diamonds) 279 | #' diaX <- useful::build.x(price ~ carat + cut + x - 1, data=diamonds, 280 | #' contrasts=FALSE) 281 | #' diaY <- useful::build.y(price ~ carat + cut + x - 1, data=diamonds) 282 | #' modG1 <- cv.glmnet(x=diaX, y=diaY, k=5) 283 | #' extract.coef(modG1) 284 | #' \dontshow{\}} 285 | #' 286 | extract.coef.cv.glmnet <- function(model, lambda="lambda.min", ...) 287 | { 288 | extract.coef.glmnet(model, lambda=lambda, ...) 289 | } 290 | 291 | 292 | #' @title extract.coef.maxLik 293 | #' @description Extract Coefficient Information from Models 294 | #' @details Gets the coefficient values and variable names from a model. 295 | #' @author Jared P. Lander 296 | #' @method extract.coef maxLik 297 | #' @export extract.coef.maxLik 298 | #' @export 299 | #' @aliases extract.coef.maxLik 300 | #' @param model Model object from which to extract information. 301 | #' @param \dots Further arguments 302 | #' @return A \code{\link{data.frame}} containing the coefficient, the standard 303 | #' error and the variable name. 304 | #' @examples 305 | #' \dontshow{ 306 | #' if(requireNamespace('maxLik', quietly=TRUE)) 307 | #' \{ 308 | #' } 309 | #' library(maxLik) 310 | #' loglik <- function(param) { 311 | #' mu <- param[1] 312 | #' sigma <- param[2] 313 | #' ll <- -0.5*N*log(2*pi) - N*log(sigma) - sum(0.5*(x - mu)^2/sigma^2) 314 | #' ll 315 | #' } 316 | #' x <- rnorm(1000, 1, 2) # use mean=1, stdd=2 317 | #' N <- length(x) 318 | #' res <- maxLik(loglik, start=c(0,1)) # use 'wrong' start values 319 | #' extract.coef(res) 320 | #' } 321 | #' \dontshow{ 322 | #' \} 323 | #' } 324 | #' 325 | extract.coef.maxLik <- function(model, ...) 326 | { 327 | # get coefficients 328 | theCoef <- stats::coef(model) 329 | # get coef names 330 | coefNames <- names(theCoef) 331 | 332 | ## if names do not exist make the names the number of the coef 333 | if(is.null(coefNames)) 334 | { 335 | coefNames <- seq(along=theCoef) 336 | } 337 | 338 | data.frame(Value=theCoef, SE=summary(model)$estimate[, 'Std. error'], Coefficient=coefNames) 339 | } 340 | 341 | 342 | #' @title extract.coef.xgb.Booster 343 | #' @description Extract Coefficient Information from Models 344 | #' @details Gets the coefficient values and variable names from a model. Since 345 | #' xgboost does not have standard errors, those will just be NA. 346 | #' @author Jared P. Lander 347 | #' @method extract.coef xgb.Booster 348 | #' @aliases extract.coef.xgb.Booster 349 | #' @export 350 | #' @param model Model object from which to extract information. 351 | #' @param feature_names Names of coefficients 352 | #' @param removeNonSelected If \code{TRUE} (default) do not return the non-selected (0) coefficients 353 | #' @param zero_threshold Since \code{coefficients} from 354 | #' \code{\link[xgboost]{xgboost}} are not exactly zero, 355 | #' this is the threshold under which a coefficient is considered zero 356 | #' @param \dots Further arguments 357 | #' @return A \code{\link{data.frame}} containing the coefficient, the standard 358 | #' error and the variable name. 359 | #' @examples 360 | #' \dontshow{ 361 | #' if(requireNamespace('xgboost', quietly=TRUE))\{} 362 | #' library(xgboost) 363 | #' data(diamonds, package='ggplot2') 364 | #' diaX <- useful::build.x(price ~ carat + cut + x, data=diamonds, contrasts=FALSE) 365 | #' diaY <- useful::build.y(price ~ carat + cut + x, data=diamonds) 366 | #' xg1 <- xgb.train(data=xgb.DMatrix(data=diaX, label=diaY), 367 | #' booster='gblinear', 368 | #' objective='reg:squarederror', eval_metric='rmse', 369 | #' nrounds=50 370 | #' ) 371 | #' extract.coef(xg1) 372 | #' extract.coef(xg1, zero_threshold=0) 373 | #' extract.coef(xg1, feature_names=colnames(diaX)) 374 | #' \dontshow{\}} 375 | #' 376 | extract.coef.xgb.Booster <- function(model, feature_names=NULL, 377 | removeNonSelected=TRUE, 378 | zero_threshold=1E-3, 379 | ...) 380 | { 381 | # get coefs for the boosted tree 382 | theCoef <- xgboost::xgb.importance(model=model, 383 | feature_names=feature_names) %>% 384 | # convert to a tibble 385 | tibble::as_tibble() %>% 386 | # return NAs for SE 387 | dplyr::mutate(SE=NA_real_) %>% 388 | # rename some columns 389 | dplyr::select('Value'='Weight', 'SE'='SE', 'Coefficient'='Feature') %>% 390 | dplyr::mutate_at( 391 | .vars='Coefficient', 392 | .funs=~factor(., 393 | levels=model$feature_names, 394 | labels=model$feature_names 395 | ) 396 | ) %>% 397 | dplyr::arrange_at('Coefficient') %>% 398 | dplyr::mutate_at( 399 | .vars='Coefficient', 400 | .funs=as.character 401 | ) 402 | 403 | if(removeNonSelected) 404 | { 405 | # remove (close to) 0 Values 406 | theCoef <- dplyr::filter_at( 407 | theCoef, 408 | .vars='Value', 409 | .vars_predicate=dplyr::all_vars(abs(.) > zero_threshold)) 410 | } 411 | 412 | return(theCoef) 413 | } 414 | -------------------------------------------------------------------------------- /R/extractPath.r: -------------------------------------------------------------------------------- 1 | #' @title extractPath 2 | #' @description Extracts the coefficient path of the elastic net 3 | #' @details This is a replacement plot for visualizing the coefficient path resulting from the elastic net. 4 | #' @author Jared P. Lander 5 | #' @export 6 | #' @param model A \code{\link[glmnet]{glmnet}} model 7 | #' @param \dots Further arguments 8 | #' @return A \code{link[tibble]{tibble}} holding the coefficients for various lambdas 9 | #' @examples 10 | #' 11 | #' library(glmnet) 12 | #' data(diamonds, package='ggplot2') 13 | #' diaX <- useful::build.x(price ~ carat + cut + x - 1, data=diamonds, contrasts = TRUE) 14 | #' diaY <- useful::build.y(price ~ carat + cut + x - 1, data=diamonds) 15 | #' modG1 <- glmnet(x=diaX, y=diaY) 16 | #' extractPath(modG1) 17 | #' 18 | #' modG2 <- cv.glmnet(x=diaX, y=diaY, nfolds=5) 19 | #' extractPath(modG2) 20 | #' 21 | extractPath <- function(model, ...) 22 | { 23 | UseMethod('extractPath') 24 | } 25 | 26 | #' @rdname extractPath 27 | #' @export 28 | #' @importFrom magrittr "%>%" 29 | #' @param intercept If \code{FALSE} (the default), no intercept will be provided 30 | extractPath.glmnet <- function(model, intercept=FALSE, ...) 31 | { 32 | thePath <- dplyr::bind_cols( 33 | tibble::tibble(lambda=log(model$lambda)), 34 | model %>% 35 | glmnet::coef.glmnet() %>% 36 | as.matrix() %>% t() %>% 37 | tibble::as_tibble() 38 | ) %>% 39 | dplyr::arrange(.data$lambda) 40 | 41 | if(!intercept) 42 | { 43 | thePath <- thePath %>% 44 | dplyr::select(-dplyr::matches('(Intercept)')) 45 | } 46 | 47 | return(thePath) 48 | } 49 | 50 | 51 | #' @rdname extractPath 52 | #' @export 53 | #' @importFrom magrittr "%>%" 54 | extractPath.cv.glmnet <- function(model, ...) 55 | { 56 | extractPath(model$glmnet.fit, ...) 57 | } 58 | -------------------------------------------------------------------------------- /R/multiplot.r: -------------------------------------------------------------------------------- 1 | ### Functions for plotting multiple coefplots at once 2 | #' Plot multiple coefplots 3 | #' 4 | #' Plot the coefficients from multiple models 5 | #' 6 | #' Plots a graph similar to \code{\link{coefplot}} but for multiple plots at once. 7 | #' 8 | #' For now, if \code{names} is provided the plots will appear in alphabetical order of the names. This will be adjusted in future iterations. When setting \code{by} to "Model" and specifying exactly one variable in \code{variables} that one coefficient will be plotted repeatedly with the axis labeled by model. This is Andy Gelman's secret weapon. 9 | #' 10 | #' @export multiplot 11 | #' @seealso \code{link{coefplot}} 12 | #' @param \dots Models to be plotted 13 | #' @param title The name of the plot, if NULL then no name is given 14 | #' @param xlab The x label 15 | #' @param ylab The y label 16 | #' @param innerCI How wide the inner confidence interval should be, normally 1 standard deviation. If 0, then there will be no inner confidence interval. 17 | #' @param outerCI How wide the outer confidence interval should be, normally 2 standard deviations. If 0, then there will be no outer confidence interval. 18 | #' @param lwdInner The thickness of the inner confidence interval 19 | #' @param lwdOuter The thickness of the outer confidence interval 20 | #' @param pointSize Size of coefficient point 21 | #' @param dodgeHeight Amount of vertical dodging 22 | #' @param color The color of the points and lines 23 | #' @param shape The shape of the points 24 | #' @param linetype The type of line drawn for the standard errors 25 | #' @param cex The text size multiplier, currently not used 26 | #' @param textAngle The angle for the coefficient labels, 0 is horizontal 27 | #' @param numberAngle The angle for the value labels, 0 is horizontal 28 | #' @param zeroColor The color of the line indicating 0 29 | #' @param zeroLWD The thickness of the 0 line 30 | #' @param zeroType The type of 0 line, 0 will mean no line 31 | ## @param facet logical; If the coefficients should be faceted by the variables, numeric coefficients (including the intercept) will be one facet 32 | #' @param single logical; If TRUE there will be one plot with the points and bars stacked, otherwise the models will be displayed in separate facets 33 | #' @param scales The way the axes should be treated in a faceted plot. Can be c("fixed", "free", "free_x", "free_y") 34 | #' @param ncol The number of columns that the models should be plotted in 35 | #' @param sort Determines the sort order of the coefficients. Possible values are c("natural", "magnitude", "alphabetical") 36 | #' @param decreasing logical; Whether the coefficients should be ascending or descending 37 | #' @param names Names for models, if NULL then they will be named after their inputs 38 | #' @param numeric logical; If true and factors has exactly one value, then it is displayed in a horizontal graph with continuous confidence bounds. 39 | #' @param fillColor The color of the confidence bounds for a numeric factor 40 | #' @param alpha The transparency level of the numeric factor's confidence bound 41 | #' @param horizontal logical; If the plot should be displayed horizontally 42 | #' @param intercept logical; Whether the Intercept coefficient should be plotted 43 | #' @param interceptName Specifies name of intercept it case it is not the default of "(Intercept"). 44 | #' @param predictors A character vector specifying which coefficients to keep. Each individual coefficient can be specified. Use predictors to specify entire factors 45 | #' @param coefficients A character vector specifying which factor coefficients to keep. It will keep all levels and any interactions, even if those are not listed. 46 | #' @param strict If TRUE then predictors will only be matched to its own coefficients, not its interactions 47 | #' @param newNames Named character vector of new names for coefficients 48 | #' @param trans A transformation function to apply to the values and confidence intervals. \code{identity} by default. Use \code{invlogit} for binary regression. 49 | #' @param plot logical; If the plot should be drawn, if false then a data.frame of the values will be returned 50 | #' @param factors Vector of factor variables that will be the only ones shown 51 | #' @param only logical; If factors has a value this determines how interactions are treated. True means just that variable will be shown and not its interactions. False means interactions will be included. 52 | #' @param shorten logical or character; If \code{FALSE} then coefficients for factor levels will include their variable name. If \code{TRUE} coefficients for factor levels will be stripped of their variable names. If a character vector of variables only coefficients for factor levels associated with those variables will the variable names stripped. 53 | #' @param drop logical; if TRUE then models without valid coefficients to show will not be plotted 54 | #' @param by If "Coefficient" then a normal multiplot is plotted, if "Model" then the coefficients are plotted along the axis with one for each model. If plotting by model only one coefficient at a time can be selected. This is called the secret weapon by Andy Gelman. 55 | #' @param plot.shapes If \code{TRUE} points will have different shapes for different models 56 | #' @param plot.linetypes If \code{TRUE} lines will have different shapes for different models 57 | #' @param legend.position position of legend, one of "left", "right", "bottom", "top", "none" 58 | #' @param secret.weapon If this is \code{TRUE} and exactly one coefficient is listed in coefficients then Andy Gelman's secret weapon is plotted. 59 | #' @param legend.reverse Setting to reverse the legend in a multiplot so that it matches the order they are drawn in the plot 60 | #' @return A ggplot object 61 | #' @examples 62 | #' 63 | #' data(diamonds) 64 | #' model1 <- lm(price ~ carat + cut, data=diamonds) 65 | #' model2 <- lm(price ~ carat + cut + color, data=diamonds) 66 | #' model3 <- lm(price ~ carat + color, data=diamonds) 67 | #' multiplot(model1, model2, model3) 68 | #' multiplot(model1, model2, model3, single=FALSE) 69 | #' multiplot(model1, model2, model3, plot=FALSE) 70 | #' require(reshape2) 71 | #' data(tips, package="reshape2") 72 | #' mod1 <- lm(tip ~ total_bill + sex, data=tips) 73 | #' mod2 <- lm(tip ~ total_bill * sex, data=tips) 74 | #' mod3 <- lm(tip ~ total_bill * sex * day, data=tips) 75 | #' mod7 <- lm(tip ~ total_bill + day + time, data=tips) 76 | #' multiplot(mod1, mod2, mod3, mod7, single=FALSE, scales="free_x") 77 | #' multiplot(mod1, mod2, mod3, mod7, single=FALSE, scales="free_x") 78 | #' multiplot(mod1, mod2, mod3, mod7, single=FALSE, scales="free_x", plot.shapes=TRUE) 79 | #' multiplot(mod1, mod2, mod3, mod7, single=TRUE, scales="free_x", 80 | #' plot.shapes=TRUE, plot.linetypes=TRUE) 81 | #' multiplot(mod1, mod2, mod3, mod7, single=TRUE, scales="free_x", 82 | #' plot.shapes=FALSE, plot.linetypes=TRUE, legend.position="bottom") 83 | #' # the secret weapon 84 | #' multiplot(mod1, mod2, mod3, mod7, coefficients="total_bill", secret.weapon=TRUE) 85 | #' # horizontal secret weapon 86 | #' multiplot(mod1, mod2, mod3, mod7, coefficients="total_bill", by="Model", horizontal=FALSE) 87 | #' 88 | #' 89 | multiplot <- function(..., 90 | title="Coefficient Plot", xlab="Value", ylab="Coefficient", 91 | innerCI=1, outerCI=2, 92 | lwdInner=1, 93 | lwdOuter=(Sys.info()["sysname"] != 'Windows')*0.5, 94 | pointSize=3, dodgeHeight=1, 95 | color="blue", shape=16, linetype=1, 96 | cex=.8, textAngle=0, numberAngle=90, 97 | zeroColor="grey", zeroLWD=1, zeroType=2, 98 | #facet=FALSE, 99 | single=TRUE, 100 | scales="fixed", ncol=length(unique(modelCI$Model)), 101 | sort=c("natural", "normal", "magnitude", "size", "alphabetical"), decreasing=FALSE, names=NULL, 102 | numeric=FALSE, fillColor="grey", alpha=1/2, 103 | horizontal=FALSE, factors=NULL, only=NULL, shorten=TRUE, 104 | intercept=TRUE, interceptName="(Intercept)", 105 | coefficients=NULL, predictors=NULL, strict=FALSE, newNames=NULL, plot=TRUE, drop=FALSE, 106 | by=c("Coefficient", "Model"), plot.shapes=FALSE, plot.linetypes=FALSE, 107 | legend.position=c("bottom", "right", "left", "top", "none"), 108 | secret.weapon=FALSE, legend.reverse=FALSE, trans=identity 109 | ) 110 | { 111 | ## if ... is already a list just grab the dots, otherwise force it into a list 112 | if(tryCatch(is.list(...), error = function(e) FALSE)) 113 | { 114 | # grab the models 115 | theDots <- list(...)[[1]] 116 | # since theDots came in as a list it might have names, if so, leave them, if not, assign them names 117 | if(is.null(names(theDots))) 118 | { 119 | names(theDots) <- sprintf("Model%s", 1:length(theDots)) 120 | } 121 | }else 122 | { 123 | # grab the models 124 | theDots <- list(...) 125 | } 126 | 127 | # get the inputs, anything in the dots is blank or "" 128 | theArgs <- unlist(structure(as.list(match.call()[-1]), class = "uneval")) 129 | # if names(theArgs) is null, only dots were passed, treat them all as model 130 | # otherwise find args that are "" and treat them as model 131 | #print(theArgs[names(theArgs) == ""]) 132 | if(is.null(names(theArgs))) 133 | { 134 | # if names(theArgs) is null, only dots were passed, treat them all as model 135 | theNames <- theArgs 136 | }else 137 | { 138 | theNames <- theArgs[names(theArgs) == ""] 139 | } 140 | 141 | # if theDots doesn't already have names apply what we just created 142 | if(is.null(names(theDots))) 143 | { 144 | names(theDots) <- theNames 145 | } 146 | 147 | # get variables that have multiple options 148 | sort <- match.arg(sort) 149 | by <- match.arg(by) 150 | 151 | legend.position <- match.arg(legend.position) 152 | 153 | if(secret.weapon) 154 | { 155 | by <- "Model" 156 | horizontal <- TRUE 157 | } 158 | 159 | if(by == "Model" & length(coefficients) != 1) 160 | { 161 | stop("If plotting the model along the axis then exactly one coefficient must be specified for plotting") 162 | } 163 | 164 | # return(theDots) 165 | # need to change getModelInfo and buildModelCI and coefplot.lm so that shorten, factors and only are normal arguments and not part of ..., that way it will work better for this 166 | # get the modelCI for each model and make one big data.frame 167 | modelCI <- ldply(theDots, .fun=buildModelCI, outerCI=outerCI, innerCI=innerCI, intercept=intercept, numeric=numeric, 168 | sort=sort, decreasing=decreasing, factors=factors, shorten=shorten, coefficients=coefficients, 169 | predictors=predictors, strict=strict, newNames=newNames, trans=trans) 170 | 171 | # Turn the Call into a unique identifier for each model 172 | #modelCI$Model <- as.factor(as.numeric(factor(modelCI$Model, levels=unique(modelCI$Model)))) 173 | modelCI$Model <- modelCI$.id 174 | modelCI$.id <- NULL 175 | 176 | # if names are provided use those instead of the numbers 177 | if(!is.null(names)) 178 | { 179 | names(names) <- theNames 180 | modelCI$Model <- names[modelCI$Model] 181 | #modNames <- structure(as.list(match.call()[-1]), class = "uneval") 182 | } 183 | 184 | # ## if we are not plotting return modelCI right away 185 | # if(!plot) 186 | # { 187 | # return(modelCI) 188 | # } 189 | 190 | ## if drop is true get rid of models without valid coefficients 191 | if(drop) 192 | { 193 | notNA <- daply(modelCI, .variables="Model", function(x) { !all(is.na(x$Coef)) }) 194 | #return(which(notNA == TRUE)) 195 | modelCI <- modelCI[modelCI$Model %in% names(which(notNA == TRUE)), ] 196 | } 197 | 198 | if(!plot) 199 | { 200 | return(modelCI) 201 | } 202 | 203 | legendLabels <- if(legend.reverse){ rev(unique(modelCI$Model)) } else{ unique(modelCI$Model) } 204 | 205 | p <- buildPlotting.default(modelCI=modelCI, 206 | #modelMeltInner=modelMeltInner, modelMeltOuter=modelMeltOuter, 207 | title=title, xlab=xlab, ylab=ylab, 208 | lwdInner=lwdInner, lwdOuter=lwdOuter, pointSize=pointSize, dodgeHeight=dodgeHeight, 209 | color=color, shape=shape, linetype=linetype, cex=cex, textAngle=textAngle, 210 | numberAngle=numberAngle, zeroColor=zeroColor, zeroLWD=zeroLWD, 211 | outerCI=outerCI, innerCI=innerCI,# single=single, 212 | zeroType=zeroType, numeric=numeric, fillColor=fillColor, alpha=alpha, multi=TRUE, 213 | value="Value", coefficient=by, 214 | horizontal=horizontal, facet=FALSE, scales="fixed") 215 | 216 | theColorScale <- list("Coefficient"=scale_colour_discrete("Model", breaks=legendLabels), 217 | "Model"=scale_color_manual(values=rep(color, length(unique(modelCI$Model))), guide='none')) 218 | 219 | theShapeScale <- list("NoShapes"=scale_shape_manual(values=rep(shape, length(unique(modelCI$Model))), guide='none'), 220 | "Shapes"=scale_shape_manual(values=1:length(unique(modelCI$Model))) 221 | ) 222 | 223 | theLinetypeScale <- list("NoShapes"=scale_linetype_manual(values=rep(linetype, length(unique(modelCI$Model))), guide='none'), 224 | "Shapes"=scale_linetype_manual(values=1:length(unique(modelCI$Model))) 225 | ) 226 | # print(rep(linetype, length(unique(modelCI$Model)))) 227 | p + theColorScale[[by]] + 228 | theShapeScale[[plot.shapes+1]] + 229 | theLinetypeScale[[plot.linetypes+1]] + 230 | theme(legend.position=legend.position) + 231 | if(!single) facet_wrap(~Model, scales=scales, ncol=ncol) 232 | } 233 | -------------------------------------------------------------------------------- /R/position.r: -------------------------------------------------------------------------------- 1 | # Detect and prevent collisions. 2 | # Powers dodging, stacking and filling. 3 | collidev <- function(data, height = NULL, name, strategy, check.height = TRUE) { 4 | # Determine height 5 | if (!is.null(height)) { 6 | # height set manually 7 | if (!(all(c("ymin", "ymax") %in% names(data)))) { 8 | data$ymin <- data$y - height / 2 9 | data$ymax <- data$y + height / 2 10 | } 11 | } else { 12 | if (!(all(c("ymin", "ymax") %in% names(data)))) { 13 | data$ymin <- data$y 14 | data$ymax <- data$y 15 | } 16 | 17 | # height determined from data, must be floating point constant 18 | heights <- unique(data$ymax - data$ymin) 19 | heights <- heights[!is.na(heights)] 20 | 21 | # # Suppress warning message since it's not reliable 22 | # if (!zero_range(range(heights))) { 23 | # warning(name, " requires constant height: output may be incorrect", 24 | # call. = FALSE) 25 | # } 26 | height <- heights[1] 27 | } 28 | 29 | # Reorder by x position, relying on stable sort to preserve existing 30 | # ordering, which may be by group or order. 31 | data <- data[order(data$ymin), ] 32 | 33 | # Check for overlap 34 | intervals <- as.numeric(t(unique(data[c("ymin", "ymax")]))) 35 | intervals <- intervals[!is.na(intervals)] 36 | 37 | if (length(unique(intervals)) > 1 & any(diff(scale(intervals)) < -1e-6)) { 38 | warning(name, " requires non-overlapping y intervals", call. = FALSE) 39 | # This is where the algorithm from [L. Wilkinson. Dot plots. 40 | # The American Statistician, 1999.] should be used 41 | } 42 | 43 | if (!is.null(data$xmax)) { 44 | plyr::ddply(data, "ymin", strategy, height = height) 45 | } else if (!is.null(data$x)) { 46 | data$xmax <- data$x 47 | data <- plyr::ddply(data, "ymin", strategy, height = height) 48 | data$x <- data$xmax 49 | data 50 | } else { 51 | stop("Neither x nor xmax defined") 52 | } 53 | } 54 | 55 | # Stack overlapping intervals. 56 | # Assumes that each set has the same horizontal position 57 | pos_stackv <- function(df, height) { 58 | if (nrow(df) == 1) return(df) 59 | 60 | n <- nrow(df) + 1 61 | x <- ifelse(is.na(df$x), 0, df$x) 62 | if (all(is.na(df$y))) { 63 | heights <- rep(NA, n) 64 | } else { 65 | heights <- c(0, cumsum(x)) 66 | } 67 | 68 | df$xmin <- heights[-n] 69 | df$xmax <- heights[-1] 70 | df$x <- df$xmax 71 | df 72 | } 73 | 74 | # Stack overlapping intervals and set height to 1. 75 | # Assumes that each set has the same horizontal position. 76 | pos_fillv <- function(df, height) { 77 | stacked <- pos_stackv(df, height) 78 | stacked$xmin <- stacked$xmin / max(stacked$xmax) 79 | stacked$xmax <- stacked$xmax / max(stacked$xmax) 80 | stacked$x <- stacked$xmax 81 | stacked 82 | } 83 | 84 | # Dodge overlapping interval. 85 | # Assumes that each set has the same horizontal position. 86 | pos_dodgev <- function(df, height) { 87 | n <- length(unique(df$group)) 88 | if (n == 1) return(df) 89 | 90 | if (!all(c("ymin", "ymax") %in% names(df))) { 91 | df$ymin <- df$y 92 | df$ymax <- df$y 93 | } 94 | 95 | d_height <- max(df$ymax - df$ymin) 96 | 97 | # df <- data.frame(n = c(2:5, 10, 26), div = c(4, 3, 2.666666, 2.5, 2.2, 2.1)) 98 | # ggplot(df, aes(n, div)) + geom_point() 99 | 100 | # Have a new group index from 1 to number of groups. 101 | # This might be needed if the group numbers in this set don't include all of 1:n 102 | groupidy <- match(df$group, sort(unique(df$group))) 103 | 104 | # Find the center for each group, then use that to calculate xmin and xmax 105 | df$y <- df$y + height * ((groupidy - 0.5) / n - .5) 106 | df$ymin <- df$y - d_height / n / 2 107 | df$ymax <- df$y + d_height / n / 2 108 | 109 | df 110 | } 111 | 112 | 113 | #' Adjust position by dodging overlaps to the side. 114 | #' 115 | #' @inheritParams ggplot2::position_identity 116 | #' @param height Dodging height, when different to the height of the individual 117 | #' elements. This is useful when you want to align narrow geoms with wider 118 | #' geoms. See the examples for a use case. 119 | #' @family position adjustments 120 | #' @export 121 | #' @examples 122 | #' ggplot(mtcars, aes(factor(cyl), fill = factor(vs))) + 123 | #' geom_bar(position = "dodge") 124 | #' 125 | #' ggplot(diamonds, aes(price, fill = cut)) + 126 | #' geom_histogram(position="dodge") 127 | #' # see ?geom_boxplot and ?geom_bar for more examples 128 | #' 129 | #' # To dodge items with different heights, you need to be explicit 130 | #' df <- data.frame(x=c("a","a","b","b"), y=2:5, g = rep(1:2, 2)) 131 | #' p <- ggplot(df, aes(x, y, group = g)) + 132 | #' geom_bar( 133 | #' stat = "identity", position = "dodge", 134 | #' fill = "grey50", colour = "black" 135 | #' ) 136 | #' p 137 | #' 138 | #' # A line range has no height: 139 | #' p + geom_linerange(aes(ymin = y-1, ymax = y+1), position = "dodge") 140 | #' # You need to explicitly specify the height for dodging 141 | #' p + geom_linerange(aes(ymin = y-1, ymax = y+1), 142 | #' position = position_dodge(width = 0.9)) 143 | #' 144 | #' # Similarly with error bars: 145 | #' p + geom_errorbar(aes(ymin = y-1, ymax = y+1), width = 0.2, 146 | #' position = "dodge") 147 | #' p + geom_errorbar(aes(ymin = y-1, ymax = y+1, height = 0.2), 148 | #' position = position_dodge(width = 0.90)) 149 | #' 150 | position_dodgev <- function(height = NULL) { 151 | ggproto(NULL, PositionDodgeV, height = height) 152 | } 153 | 154 | 155 | 156 | PositionDodgeV <- ggproto("PositionDodgeV", Position, 157 | required_aes = "y", 158 | height = NULL, 159 | setup_params = function(self, data) { 160 | if (is.null(data$ymin) && is.null(data$ymax) && is.null(self$height)) { 161 | warning("height not defined. Set with `position_dodgev(height = ?)`", 162 | call. = FALSE) 163 | } 164 | list(height = self$height) 165 | }, 166 | 167 | compute_panel = function(data, params, scales) { 168 | collidev(data, params$height, "position_dodgev", pos_dodgev, check.height = FALSE) 169 | } 170 | ) 171 | -------------------------------------------------------------------------------- /R/transformations.r: -------------------------------------------------------------------------------- 1 | #' @title invlogit 2 | #' @description Calculates the inverse logit 3 | #' @details Maps the real line to [0, 1] 4 | #' @author Jared P. Lander 5 | #' @export invlogit 6 | #' @rdname invlogit 7 | #' @param x Vector of numbers 8 | #' @return \code{x} mapped to [0, 1] 9 | #' @examples 10 | #' invlogit(3) 11 | #' invlogit(-6:6) 12 | #' invlogit(c(-1, 1, 2)) 13 | #' 14 | invlogit <- function(x) 15 | { 16 | 1/(1 + exp(-x)) 17 | } 18 | -------------------------------------------------------------------------------- /README.Rmd: -------------------------------------------------------------------------------- 1 | --- 2 | output: 3 | md_document: 4 | variant: markdown_github 5 | --- 6 | 7 | 8 | [![R-CMD-check](https://github.com/jaredlander/coefplot/workflows/R-CMD-check/badge.svg)](https://github.com/jaredlander/coefplot/actions) 9 | [![CRAN_Status_Badge](https://www.r-pkg.org/badges/version/coefplot)](https://CRAN.R-project.org/package=coefplot) 10 | [![Downloads from the RStudio CRAN mirror](https://cranlogs.r-pkg.org/badges/coefplot)](https://CRAN.R-project.org/package=coefplot) 11 | [![Coverage Status](https://img.shields.io/codecov/c/github/jaredlander/coefplot/master.svg)](https://codecov.io/github/jaredlander/coefplot?branch=master) 12 | 13 | 14 | 15 | 16 | ```{r, echo = FALSE} 17 | knitr::opts_chunk$set( 18 | collapse = TRUE, 19 | comment = "#>", 20 | fig.path = "README-" 21 | ) 22 | ``` 23 | 24 | Coefplot is a package for plotting the coefficients and standard errors from a variety of models. Currently lm, glm, glmnet, maxLik, rxLinMod, rxGLM and rxLogit are supported. 25 | 26 | The package is designed for S3 dispatch from the functions coefplot and getModelInfo to make for easy additions of new models. 27 | 28 | If interested in helping please contact the package author. 29 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | 2 | 3 | [![R-CMD-check](https://github.com/jaredlander/coefplot/workflows/R-CMD-check/badge.svg)](https://github.com/jaredlander/coefplot/actions) 4 | [![CRAN_Status_Badge](https://www.r-pkg.org/badges/version/coefplot)](https://CRAN.R-project.org/package=coefplot) 5 | [![Downloads from the RStudio CRAN 6 | mirror](https://cranlogs.r-pkg.org/badges/coefplot)](https://CRAN.R-project.org/package=coefplot) 7 | [![Coverage 8 | Status](https://img.shields.io/codecov/c/github/jaredlander/coefplot/master.svg)](https://codecov.io/github/jaredlander/coefplot?branch=master) 9 | 10 | 11 | 12 | 13 | Coefplot is a package for plotting the coefficients and standard errors 14 | from a variety of models. Currently lm, glm, glmnet, maxLik, rxLinMod, 15 | rxGLM and rxLogit are supported. 16 | 17 | The package is designed for S3 dispatch from the functions coefplot and 18 | getModelInfo to make for easy additions of new models. 19 | 20 | If interested in helping please contact the package author. 21 | -------------------------------------------------------------------------------- /codecov.yml: -------------------------------------------------------------------------------- 1 | comment: false 2 | -------------------------------------------------------------------------------- /coefplot.Rproj: -------------------------------------------------------------------------------- 1 | Version: 1.0 2 | 3 | RestoreWorkspace: Default 4 | SaveWorkspace: Default 5 | AlwaysSaveHistory: Default 6 | 7 | EnableCodeIndexing: Yes 8 | UseSpacesForTab: Yes 9 | NumSpacesForTab: 4 10 | Encoding: UTF-8 11 | 12 | RnwWeave: knitr 13 | LaTeX: pdfLaTeX 14 | 15 | BuildType: Package 16 | PackageUseDevtools: Yes 17 | PackageInstallArgs: --no-multiarch --with-keep.source 18 | -------------------------------------------------------------------------------- /cran-comments.md: -------------------------------------------------------------------------------- 1 | ## Version Number 2 | 3 | 1.2.8 4 | 5 | ## Test environments 6 | 7 | - Github Actions 8 | - windows-latest (release) 9 | - macOS-latest (release) 10 | - ubuntu-20.04 (release) 11 | - ubuntu-20.04 (devel) 12 | - Windows 10, R 4.1.0 13 | - Ubuntu 18.04, R 4.1.2 14 | - Win-Builder 15 | - R-Hub 16 | 17 | ## R CMD check results 18 | 19 | There were no ERRORs, WARNINGs, NOTEs when checked locally on Windows and Ubuntu or the GitHub Actions for Windows or Ubuntu-release. 20 | 21 | GitHub Actions for Mac and Ubuntu-devel had a notes of an example taking more than 5 seconds, though I imagine that has more to do with the machine doing the testing. 22 | 23 | WinBuild had a note of an example taking more than 10 seconds, though I imagine that has more to do with the machine doing the testing. 24 | 25 | R-Hub had a note of an example taking more than 5 seconds, though I imagine that has more to do with the machine doing the testing. 26 | 27 | ## Tests 28 | 29 | All pass. 30 | -------------------------------------------------------------------------------- /hex/hex_builder.r: -------------------------------------------------------------------------------- 1 | library(coefplot) 2 | library(dplyr) 3 | library(recipes) 4 | library(hexSticker) 5 | 6 | land <- readr::read_csv('https://www.jaredlander.com/data/manhattan_Train.csv') 7 | land 8 | 9 | train <- land %>% 10 | mutate(Class=stringr::str_replace(Class, ' Apartment', '')) %>% 11 | mutate(LandUse=stringr::str_replace(LandUse, ' Buildings', '')) %>% 12 | filter(LotArea > 0) %>% 13 | recipe(TotalValue ~ Class + LotArea + HistoricDistrict + NumFloors + BldgArea, data=.) %>% 14 | step_other(all_nominal()) %>% 15 | step_normalize(NumFloors, LotArea) %>% 16 | prep() %>% 17 | juice() 18 | mod2 <- lm(TotalValue ~ ., data=train) 19 | interMult <- -1/5 20 | the_coefs <- coefplot(mod2, sort='magnitude', innerCI=3, outerCI=6, plot=FALSE) %>% 21 | mutate_at(.vars=c('Value', 'HighInner', 'LowInner', 'HighOuter', 'LowOuter'), .funs=~.x/100) %>% 22 | mutate(Coefficient=stringr::str_replace(Coefficient, 'Class', '')) %>% 23 | mutate(Coefficient=stringr::str_replace(Coefficient, 'Yes', '')) %>% 24 | mutate_at(.vars=c('Value', 'HighInner', 'LowInner', 'HighOuter', 'LowOuter'), 25 | .funs=~if_else(Coefficient=='(Intercept)', .x*interMult, .x)) %>% 26 | mutate_at(.vars=c('Value', 'HighInner', 'LowInner', 'HighOuter', 'LowOuter'), 27 | .funs=~if_else(Coefficient=='Single Family', .x*-1, .x)) %>% 28 | mutate_at(.vars=c('Value', 'HighInner', 'LowInner', 'HighOuter', 'LowOuter'), 29 | .funs=~if_else(Coefficient=='LotArea', .x-5000, .x)) %>% 30 | arrange(Value) %>% 31 | mutate(Coefficient=factor(Coefficient, levels=Coefficient)) 32 | 33 | coefColor <- 'blue' 34 | the_plot <- ggplot(the_coefs, aes(x=Value, y=Coefficient)) + 35 | geom_errorbarh(aes(xmin=LowOuter, xmax=HighOuter), height=0, color=coefColor, size=0.1) + 36 | # geom_errorbarh(aes(xmin=LowInner, xmax=HighInner), height=0, color=coefColor, size=2) + 37 | geom_vline(xintercept=0, linetype=2, color='grey') + 38 | geom_point(color=coefColor, size=1) + 39 | # scale_x_continuous(labels=scales::dollar) + 40 | # theme_classic() + 41 | labs(y=NULL, x=NULL) + 42 | theme(panel.grid=element_blank()) + 43 | theme_transparent() 44 | the_plot 45 | sticker(the_plot, package="coefplot", 46 | p_size=20, p_color=coefColor, p_y=1.52, 47 | s_x=0.93, s_y=.82, s_width=1.3, s_height=1, 48 | h_fill="white", white_around_sticker=FALSE, h_color='grey', 49 | filename="inst/figures/coefplot_v2.png", spotlight=FALSE) 50 | 51 | # sticker(the_plot, package="coefplot", 52 | # p_size=20, p_color=coefColor, 53 | # s_x=0.89, s_y=.75, s_width=1.3, s_height=1, 54 | # filename="inst/figures/coefplot_v2.png", spotlight=FALSE, h_fill="#40a0db") 55 | 56 | -------------------------------------------------------------------------------- /inst/coefMatching.r: -------------------------------------------------------------------------------- 1 | # 2 | # baseball <- data.frame(Bat=sample(1:100, 20, replace=T), Batter=sample(c("David", "Batley", "Bob", "Ace"), 20, replace=T), Hits=sample(1:20, 20, replace=T)) 3 | # bMod <- lm(Hits ~ Bat*Batter, baseball) 4 | # matchCoefs(bMod) 5 | 6 | #' @title get.assign 7 | #' @description The assignment vector for a model 8 | #' @details Gets relative positions of predictors 9 | #' @aliases get.assign 10 | #' @author Jared P. Lander 11 | #' @param model Fitted model 12 | #' @param \dots Further arguments 13 | #' @return The assignment vector 14 | get.assign <- function(model, ...) 15 | { 16 | UseMethod("get.assign") 17 | } 18 | 19 | #' @title get.assign.lm 20 | #' @description The assignment vector for an lm model 21 | #' @details Gets relative positions of predictors 22 | #' @aliases get.assign.lm 23 | #' @S3method get.assign lm 24 | #' @author Jared P. Lander 25 | #' @param model Fitted model 26 | #' @param \dots Further arguments 27 | #' @return The assignment vector 28 | get.assign.lm <- function(model, ...) 29 | { 30 | model$assign 31 | } 32 | 33 | #' @title get.assign.glm 34 | #' @description The assignment vector for a glm model 35 | #' @details Gets relative positions of predictors 36 | #' @aliases get.assign.glm 37 | #' @S3method get.assign glm 38 | #' @author Jared P. Lander 39 | #' @param model Fitted model 40 | #' @param \dots Further arguments 41 | #' @return The assignment vector 42 | get.assign.glm <- function(model, ...) 43 | { 44 | # build model.matrix 45 | theMat <- model.matrix(object=model$formula, data=model$data) 46 | # get assignment 47 | attr(theMat, "assign") 48 | } 49 | 50 | #' @title matchCoefs 51 | #' @description Match coefficients to predictors 52 | #' @details Matches coefficients to predictors using information from model matrices 53 | #' @author Jared P. Lander 54 | #' @aliases matchCoefs 55 | #' @param model Fitted model 56 | #' @param \dots Further arguments 57 | #' @return a data.frame matching predictors to coefficients 58 | #' @examples 59 | #' require(reshape2) 60 | #' require(plyr) 61 | #' data("tips", package="reshape2") 62 | #' mod1 <- lm(tip ~ total_bill * sex + day, tips) 63 | #' mod2 <- lm(tip ~ total_bill * sex + day - 1, tips) 64 | #' mod3 <- glm(tip ~ total_bill * sex + day, tips, family=gaussian(link="identity")) 65 | #' mod4 <- lm(tip ~ (total_bill + sex + day)^3, tips) 66 | #' mod5 <- lm(tip ~ total_bill * sex + day + I(total_bill^2), tips) 67 | #' matchCoefs(mod1) 68 | #' matchCoefs(mod2) 69 | #' matchCoefs(mod3) 70 | #' matchCoefs(mod4) 71 | #' matchCoefs(mod5) 72 | # 73 | matchCoefs <- function(model, ...) 74 | { 75 | UseMethod(generic="matchCoefs") 76 | } 77 | 78 | #' @title matchCoefs.default 79 | #' @description Match coefficients to predictors 80 | #' @details Matches coefficients to predictors using information from model matrices 81 | #' @author Jared P. Lander 82 | #' @aliases matchCoefs.default 83 | #' @S3method matchCoefs default 84 | #' @param model Fitted model 85 | #' @param \dots Further arguments 86 | #' @return a data.frame matching predictors to coefficients 87 | matchCoefs.default <- function(model, ...) 88 | { 89 | # get the terms 90 | theTerms <- model$terms 91 | # get the assignment position 92 | #thePos <- model$assign 93 | thePos <- get.assign(model) 94 | # get intercept indicator 95 | inter <- attr(theTerms, "intercept") 96 | # get coef names 97 | coefNames <- names(coef(model)) 98 | # get pred names 99 | predNames <- attr(theTerms, "term.labels") 100 | # expand out pred names to match coefficient names 101 | predNames <- predNames[thePos] 102 | # if there's an intercept term add it to the pred names 103 | if(inter == 1) 104 | { 105 | predNames <- c("(Intercept)", predNames) 106 | } 107 | 108 | # build data.frame linking term to coefficient name 109 | matching <- data.frame(Term=predNames, Coefficient=coefNames, stringsAsFactors=FALSE) 110 | 111 | ## now match individual predictor to term 112 | # get matrix as data.frame 113 | factorMat <- as.data.frame(attr(theTerms, "factor")) 114 | # add column from rownames as identifier 115 | factorMat$.Pred <- rownames(factorMat) 116 | factorMat$.Type <- attr(theTerms, "dataClasses") 117 | 118 | # melt it down for comparison 119 | factorMelt <- melt(factorMat, id.vars=c(".Pred", ".Type"), variable.name="Term") 120 | factorMelt$Term <- as.character(factorMelt$Term) 121 | 122 | # only keep rows where there's a match 123 | factorMelt <- factorMelt[factorMelt$value == 1, ] 124 | 125 | # again, bring in coefficient if needed 126 | if(inter == 1) 127 | { 128 | factorMelt <- rbind(data.frame(.Pred="(Intercept)", .Type="(Intercept)", Term="(Intercept)", value=1, stringsAsFactors=FALSE), factorMelt) 129 | } 130 | 131 | # join into the matching data.frame 132 | matching <- join(matching, factorMelt, by="Term") 133 | 134 | return(matching) 135 | } 136 | 137 | # theTerms <- mod1$terms 138 | # thePos <- mod1$assign 139 | # inter <- attr(theTerms, "intercept") 140 | # coefNames <- names(coef(mod1)) 141 | # coefNames 142 | # predNames <- attr(theTerms, "term.labels") 143 | # predNames <- predNames[thePos] 144 | # factorMat <- as.data.frame(attr(theTerms, "factor")) 145 | # factorMat$.Pred <- rownames(factorMat) 146 | # factorMelt <- melt(factorMat, id.vars=".Pred") 147 | # factorMelt <- factorMelt[factorMelt$value == 1, ] 148 | # 149 | # 150 | -------------------------------------------------------------------------------- /inst/deprecated/modelInfo.r: -------------------------------------------------------------------------------- 1 | ## information on models 2 | #' Model Information 3 | #' 4 | #' Extracts and builds extensive information from models 5 | #' 6 | #' Helper function for \code{\link{coefplot}} 7 | #' @author Jared P. Lander 8 | #' @seealso \code{\link{coefplot.lm}} 9 | #' @param model The fitted model with coefficients to be plotted 10 | #' @param \dots Further arguments such as shorten, only and factors 11 | #' @import stringr 12 | #' @rdname getModelInfo 13 | #' @return Information on the model 14 | #' @examples 15 | #' 16 | #' data(diamonds) 17 | #' model1 <- lm(price ~ carat + cut*color, data=diamonds) 18 | #' coefplot(model1) 19 | #' 20 | getModelInfo <- function(model, ...) 21 | { 22 | UseMethod("getModelInfo", model) 23 | } 24 | 25 | 26 | ## Builds all of the info necessary for building the graph 27 | # param model (lm object) the model to be plotted 28 | # param shorten (logical or character vector) logical if all or none of the factors should be shortened, if character then only the variables listed will be shortened 29 | # param factors (character vector) a list of factors to include, if NULL all of them will be included 30 | # param only (logical) if TRUE then only the specified factors will be computed, otherwise the included factors and their interactions will be computed 31 | # param \dots other options 32 | # return Information on the model 33 | #' Model Information 34 | #' 35 | #' Extracts and builds extensive information from lm and glm models 36 | #' 37 | #' Helper function for \code{\link{coefplot}} 38 | #' @author Jared P. Lander 39 | #' @seealso \code{\link{coefplot.lm}} 40 | #' @param model The fitted model with coefficients to be plotted 41 | #' @param factors Vector of factor variables that will be the only ones shown 42 | #' @param only logical; If factors has a value this determines how interactions are treated. True means just that variable will be shown and not its interactions. False means interactions will be included. 43 | #' @param shorten logical or character; If \code{FALSE} then coefficients for factor levels will include their variable name. If \code{TRUE} coefficients for factor levels will be stripped of their variable names. If a character vector of variables only coefficients for factor levels associated with those variables will the variable names stripped. 44 | #' @param \dots Further arguments 45 | #' @import stringr 46 | #' @rdname getModelInfo.lm 47 | ## @method getModelInfo lm 48 | #' @S3method getModelInfo lm 49 | #' @return Information on the model 50 | #' @examples 51 | #' 52 | #' data(diamonds) 53 | #' model1 <- lm(price ~ carat + cut*color, data=diamonds) 54 | #' coefplot(model1) 55 | #' 56 | getModelInfo.lm <- function(model, shorten=TRUE, factors=NULL, only=NULL, ...) 57 | { 58 | # get the model summary to easily get info out of it 59 | modelSummary <- summary(model) 60 | 61 | ## extract coefficients and standard errors 62 | coef <- modelSummary$coefficients[, 1] 63 | SE <- modelSummary$coefficients[, 2] # gets standard error from summary 64 | 65 | varTypes <- attr(model$terms, "dataClasses") ## These are the types of the different variables 66 | factorVars <- names(varTypes[varTypes %in% c("factor", "other")]) ## The variables that are factor 67 | 68 | # store the names of the coefficients 69 | newList <- names(coef) ## names of the coefficients 70 | 71 | ## if there are some factor variables 72 | if(length(factorVars) > 0) 73 | { 74 | # figure out which variable belongs to each coefficient 75 | # passing just one row doesn't work 76 | matchedVars <- buildFactorDF(modelFactorVars=factorVars, modelModel=model$model, modelCoefs=newList, shorten=shorten, factors=factors, only=only) 77 | 78 | if(!is.null(factors)) 79 | { 80 | ## since some factors are not included they must also be removed from coef's and SE's 81 | remainingFactors <- which(names(coef) %in% matchedVars$Coef) 82 | coef <- coef[remainingFactors] 83 | SE <- SE[remainingFactors] 84 | newList <- newList[remainingFactors] 85 | rm(remainingFactors); gc() 86 | } 87 | }else 88 | { 89 | newList <- NA 90 | matchedVars <- data.frame(Var=NA, Checkers=NA, Coef=NA, CoefShort=NA) 91 | } 92 | 93 | rm(varTypes); gc() # do some memory cleanup 94 | 95 | list(coef=coef, SE=SE, factorVars=factorVars, factorVarsHuman=factorVars, factorCoefs=newList, matchedVars=matchedVars) ## return the coefs and SEs as a named list 96 | } 97 | 98 | 99 | ## can deal with this in a very simliar way to lm 100 | #' Model Information for rxLinMod 101 | #' 102 | #' Extracts and builds extensive information from rxLinMod models 103 | #' 104 | #' Helper function for \code{\link{coefplot}} 105 | #' @author Jared P. Lander 106 | #' @seealso \code{\link{coefplot.lm}} \code{\link{coefplot}} \code{\link{getModelInfo.rxLinMod}} 107 | #' @param model The fitted model with coefficients to be plotted 108 | #' @param factors Vector of factor variables that will be the only ones shown 109 | #' @param only logical; If factors has a value this determines how interactions are treated. True means just that variable will be shown and not its interactions. False means interactions will be included. 110 | #' @param shorten logical or character; If \code{FALSE} then coefficients for factor levels will include their variable name. If \code{TRUE} coefficients for factor levels will be stripped of their variable names. If a character vector of variables only coefficients for factor levels associated with those variables will the variable names stripped. 111 | #' @param \dots Further arguments 112 | #' @import stringr 113 | #' @rdname getModelInfo.rxLinMod 114 | ## @method getModelInfo rxLinMod 115 | #' @S3method getModelInfo rxLinMod 116 | #' @return Information on the model 117 | #' @examples 118 | #' 119 | #' data(diamonds) 120 | #' model1 <- lm(price ~ carat + cut*color, data=diamonds) 121 | #' coefplot(model1) 122 | #' 123 | getModelInfo.rxLinMod <- function(model, shorten=TRUE, factors=NULL, only=NULL, ...) 124 | { 125 | # get the model summary to easily get info out of it 126 | #modelSummary <- summary(model)[[1]] 127 | 128 | ## extract coefficients and standard errors 129 | coef <- model$coefficients#modelSummary$coefficients[, 1, drop=F] #model$coefficients # coefficients 130 | SE <- model$coef.std.error#modelSummary$coefficients[, 2, drop=F] #model$coef.std.error # gets standard error 131 | 132 | ## the special characters and their escaped equivalents 133 | specials <- c("!", "(", ")", "-", "=", ".") 134 | 135 | ## if they are reducing variables, get rid of them now 136 | if(!is.null(factors)) 137 | { 138 | # take care of special characters 139 | factors <- subSpecials(factors, specialChars=specials)[[1]] 140 | 141 | # make a pipe seperated character of factors to keep 142 | toKeep <- paste(factors, collapse="|") 143 | 144 | ## if they are only keeping the factor itself, and not interactions 145 | if(identical(only, TRUE)) 146 | { 147 | # just check for when that variable is the only one 148 | theString <- paste("^(", toKeep, ")=[^, ]*?$", sep="") 149 | }else 150 | { 151 | # as long as that variable is in there 152 | theString <- paste("(^|, | for )(", toKeep, ")=", sep="") 153 | } 154 | 155 | # the ones we are going to keep 156 | theKeepers <- grep(theString, rownames(coef)) 157 | 158 | # take just the coefs and SEs that were specified 159 | coef <- coef[theKeepers, , drop=FALSE] 160 | SE <- SE[theKeepers, , drop=FALSE] 161 | } 162 | 163 | ## figure out which variables are factors 164 | # get coefficient names 165 | coefNames <- rownames(coef) 166 | 167 | # get the variables put into the formula and clean them up 168 | theTerms <- paste(model$formula)[[3]] 169 | theTerms <- gsub("\\*|\\+", "|", theTerms) 170 | theTerms <- gsub(" ", "", theTerms) 171 | theTerms <- gsub("F\\(([A-Za-z0-9]+)\\)", "F_\\1", theTerms) 172 | 173 | # get those variables out of the coefficient names 174 | # these are the factor variables thanks to the equal sign in the regular expression 175 | factorVars <- unlist(str_extract_all(string=coefNames, pattern=paste("(^|, | for )(", theTerms, ")=", sep=""))) 176 | # the function below depends on R 2.14 so it cannot be used for now to preserve backward compatability 177 | #theReg <- gregexpr(pattern=paste("(^|, | for )(", theTerms, ")=", sep=""), text=coefNames) 178 | #factorVars <- unlist(regmatches(x=coefNames, m=theReg)) 179 | 180 | # strip out spaces, for, commas and equals and get unique values 181 | factorVars <- unique(gsub("(^ for )|(^, )|(=$)", "", factorVars)) 182 | 183 | # build matched df 184 | 185 | if(length(factorVars) > 0) 186 | { 187 | matchedVars <- rxVarMatcher(modelFactorVars=factorVars, modelCoefNames=coefNames, shorten=shorten) 188 | }else 189 | { 190 | newList <- NA 191 | matchedVars <- data.frame(Var=NA, Checkers=NA, Coef=NA, CoefShort=NA) 192 | } 193 | 194 | 195 | return(list(coef=coef, SE=SE, factorVars=factorVars, factorCoefs=coefNames, matchedVars=matchedVars)) ## return the coefs and SEs as a named list 196 | } 197 | 198 | 199 | ## can deal with this in a very simliar way to lm 200 | #' Model Information for rxLogit 201 | #' 202 | #' Extracts and builds extensive information from rxLogit models 203 | #' 204 | #' Helper function for \code{\link{coefplot}} 205 | #' 206 | #' See \code{\link{getModelInfo.rxLinMod}} for more information on this function's arguments as it is simply a wrapper function. 207 | #' @author Jared P. Lander 208 | #' @seealso \code{\link{coefplot.lm}} \code{\link{coefplot}} 209 | ## @param model The fitted model with coefficients to be plotted 210 | ## @param factors Vector of factor variables that will be the only ones shown 211 | ## @param only logical; If factors has a value this determines how interactions are treated. True means just that variable will be shown and not its interactions. False means interactions will be included. 212 | ## @param shorten logical or character; If \code{FALSE} then coefficients for factor levels will include their variable name. If \code{TRUE} coefficients for factor levels will be stripped of their variable names. If a character vector of variables only coefficients for factor levels associated with those variables will the variable names stripped. 213 | #' @param \dots Further arguments. See \code{\link{getModelInfo.rxLinMod}} for more information on this function's arguments. 214 | #' @import stringr 215 | #' @rdname getModelInfo.rxLogit 216 | ## @method getModelInfo rxLogit 217 | #' @S3method getModelInfo rxLogit 218 | #' @return Information on the model 219 | #' @examples 220 | #' 221 | #' data(diamonds) 222 | #' model1 <- lm(price ~ carat + cut*color, data=diamonds) 223 | #' coefplot(model1) 224 | #' 225 | getModelInfo.rxLogit <- function(...) 226 | { 227 | getModelInfo.rxLinMod(...) 228 | } -------------------------------------------------------------------------------- /man/annotateSeries.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/dyAnnotation.r 3 | \name{annotateSeries} 4 | \alias{annotateSeries} 5 | \title{annotateSeries} 6 | \usage{ 7 | annotateSeries( 8 | dygraph, 9 | series, 10 | x = 0, 11 | text = series, 12 | tooltip = series, 13 | width = 50, 14 | ... 15 | ) 16 | } 17 | \arguments{ 18 | \item{dygraph}{Dygraph to add an annotation to} 19 | 20 | \item{series}{Series to attach the annotation to. By default, the last series 21 | defined using \code{\link[dygraphs]{dySeries}}.} 22 | 23 | \item{x}{Either numeric or date value indicating where to place the 24 | annotation. For date value, this should be of class \code{POSIXct} or 25 | convertible to \code{POSIXct}.} 26 | 27 | \item{text}{Text to overlay on the chart at the location of x} 28 | 29 | \item{tooltip}{Additional tooltip text to display on mouse hover} 30 | 31 | \item{width}{Width (in pixels) of the annotation flag.} 32 | 33 | \item{\dots}{Further arguments passed to \code{link[dygraphs]{dyAnnotation}}} 34 | } 35 | \description{ 36 | Annotate a series 37 | } 38 | \details{ 39 | A helper function that changes the order of some options for \code{link[dygraphs]{dyAnnotation}} so it is easier to use with \code{\link[purrr]{reduce}}. 40 | } 41 | \author{ 42 | Jared P. Lander 43 | } 44 | -------------------------------------------------------------------------------- /man/buildModelCI.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/buildPlottingFrame.r 3 | \name{buildModelCI} 4 | \alias{buildModelCI} 5 | \title{buildModelCI} 6 | \usage{ 7 | buildModelCI(model, ...) 8 | } 9 | \arguments{ 10 | \item{model}{A Fitted model such as from lm, glm} 11 | 12 | \item{\dots}{Arguments passed on onto other methods} 13 | } 14 | \value{ 15 | A \code{\link{data.frame}} listing coefficients and confidence bands. 16 | } 17 | \description{ 18 | Construct Confidence Interval Values 19 | } 20 | \details{ 21 | Takes a model and builds a data.frame holding the coefficient value and the confidence interval values. 22 | } 23 | \examples{ 24 | 25 | data(diamonds) 26 | model1 <- lm(price ~ carat + cut, data=diamonds) 27 | coefplot:::buildModelCI(model1) 28 | coefplot(model1) 29 | 30 | } 31 | \seealso{ 32 | \code{\link{coefplot}} \code{\link{multiplot}} 33 | } 34 | \author{ 35 | Jared P. Lander 36 | } 37 | -------------------------------------------------------------------------------- /man/buildModelCI.default.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/buildPlottingFrame.r 3 | \name{buildModelCI.default} 4 | \alias{buildModelCI.default} 5 | \title{buildModelCI.default} 6 | \usage{ 7 | \method{buildModelCI}{default}( 8 | model, 9 | outerCI = 2, 10 | innerCI = 1, 11 | intercept = TRUE, 12 | numeric = FALSE, 13 | sort = c("natural", "magnitude", "alphabetical"), 14 | predictors = NULL, 15 | strict = FALSE, 16 | coefficients = NULL, 17 | newNames = NULL, 18 | trans = identity, 19 | decreasing = TRUE, 20 | name = NULL, 21 | interceptName = "(Intercept)", 22 | ... 23 | ) 24 | } 25 | \arguments{ 26 | \item{model}{A Fitted model such as from lm, glm} 27 | 28 | \item{outerCI}{How wide the outer confidence interval should be, normally 2 standard deviations. If 0, then there will be no outer confidence interval.} 29 | 30 | \item{innerCI}{How wide the inner confidence interval should be, normally 1 standard deviation. If 0, then there will be no inner confidence interval.} 31 | 32 | \item{intercept}{logical; Whether the Intercept coefficient should be plotted} 33 | 34 | \item{numeric}{logical; If true and factors has exactly one value, then it is displayed in a horizontal graph with continuous confidence bounds.; not used for now.} 35 | 36 | \item{sort}{Determines the sort order of the coefficients. Possible values are c("natural", "magnitude", "alphabetical")} 37 | 38 | \item{predictors}{A character vector specifying which variables to keep. Each individual variable has to be specified, so individual levels of factors must be specified. We are working on making this easier to implement, but this is the only option for now.} 39 | 40 | \item{strict}{If TRUE then predictors will only be matched to its own coefficients, not its interactions} 41 | 42 | \item{coefficients}{A character vector specifying which factor variables to keep. It will keep all levels and any interactions, even if those are not listed.} 43 | 44 | \item{newNames}{Named character vector of new names for coefficients} 45 | 46 | \item{trans}{A transformation function to apply to the values and confidence intervals. \code{identity} by default. Use \code{invlogit} for binary regression.} 47 | 48 | \item{decreasing}{logical; Whether the coefficients should be ascending or descending} 49 | 50 | \item{name}{A name for the model, if NULL the call will be used} 51 | 52 | \item{interceptName}{Specifies name of intercept it case it is not the default of "(Intercept").} 53 | 54 | \item{\dots}{See Details for information on \code{factors}, \code{only} and \code{shorten}} 55 | } 56 | \value{ 57 | A \code{\link{data.frame}} listing coefficients and confidence bands. 58 | } 59 | \description{ 60 | Construct Confidence Interval Values 61 | } 62 | \details{ 63 | Takes a model and builds a data.frame holding the coefficient value and the confidence interval values. 64 | } 65 | \examples{ 66 | 67 | data(diamonds, package='ggplot2') 68 | model1 <- lm(price ~ carat + cut, data=diamonds) 69 | coefplot:::buildModelCI(model1) 70 | coefplot(model1) 71 | 72 | } 73 | \seealso{ 74 | \code{\link{coefplot}} \code{\link{multiplot}} 75 | } 76 | \author{ 77 | Jared P. Lander 78 | } 79 | -------------------------------------------------------------------------------- /man/buildPlotting.default.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/buildPlot.r 3 | \name{buildPlotting.default} 4 | \alias{buildPlotting.default} 5 | \title{Coefplot plotting} 6 | \usage{ 7 | buildPlotting.default( 8 | modelCI, 9 | title = "Coefficient Plot", 10 | xlab = "Value", 11 | ylab = "Coefficient", 12 | lwdInner = 1 + interactive * 2, 13 | lwdOuter = if (interactive) 1 else unname((Sys.info()["sysname"] != "Windows") * 0.5), 14 | pointSize = 3 + interactive * 5, 15 | color = "blue", 16 | cex = 0.8, 17 | textAngle = 0, 18 | numberAngle = 0, 19 | shape = 16, 20 | linetype = 1, 21 | outerCI = 2, 22 | innerCI = 1, 23 | multi = FALSE, 24 | zeroColor = "grey", 25 | zeroLWD = 1, 26 | zeroType = 2, 27 | numeric = FALSE, 28 | fillColor = "grey", 29 | alpha = 1/2, 30 | horizontal = FALSE, 31 | facet = FALSE, 32 | scales = "free", 33 | value = "Value", 34 | coefficient = "Coefficient", 35 | errorHeight = 0, 36 | dodgeHeight = 1, 37 | interactive = FALSE 38 | ) 39 | } 40 | \arguments{ 41 | \item{modelCI}{An object created by \code{\link{buildModelCI}}} 42 | 43 | \item{title}{The name of the plot, if NULL then no name is given} 44 | 45 | \item{xlab}{The x label} 46 | 47 | \item{ylab}{The y label} 48 | 49 | \item{lwdInner}{The thickness of the inner confidence interval} 50 | 51 | \item{lwdOuter}{The thickness of the outer confidence interval} 52 | 53 | \item{pointSize}{Size of coefficient point} 54 | 55 | \item{color}{The color of the points and lines} 56 | 57 | \item{cex}{The text size multiplier, currently not used} 58 | 59 | \item{textAngle}{The angle for the coefficient labels, 0 is horizontal} 60 | 61 | \item{numberAngle}{The angle for the value labels, 0 is horizontal} 62 | 63 | \item{shape}{The shape of the points} 64 | 65 | \item{linetype}{The linetype of the error bars} 66 | 67 | \item{outerCI}{How wide the outer confidence interval should be, normally 2 standard deviations. If 0, then there will be no outer confidence interval.} 68 | 69 | \item{innerCI}{How wide the inner confidence interval should be, normally 1 standard deviation. If 0, then there will be no inner confidence interval.} 70 | 71 | \item{multi}{logical; If this is for \code{\link{multiplot}} then leave the colors as determined by the legend, if FALSE then make all colors the same} 72 | 73 | \item{zeroColor}{The color of the line indicating 0} 74 | 75 | \item{zeroLWD}{The thickness of the 0 line} 76 | 77 | \item{zeroType}{The type of 0 line, 0 will mean no line} 78 | 79 | \item{numeric}{logical; If true and factors has exactly one value, then it is displayed in a horizontal graph with continuous confidence bounds.} 80 | 81 | \item{fillColor}{The color of the confidence bounds for a numeric factor} 82 | 83 | \item{alpha}{The transparency level of the numeric factor's confidence bound} 84 | 85 | \item{horizontal}{logical; If the plot should be displayed horizontally} 86 | 87 | \item{facet}{logical; If the coefficients should be faceted by the variables, numeric coefficients (including the intercept) will be one facet} 88 | 89 | \item{scales}{The way the axes should be treated in a faceted plot. Can be c("fixed", "free", "free_x", "free_y")} 90 | 91 | \item{value}{Name of variable for value metric} 92 | 93 | \item{coefficient}{Name of variable for coefficient names} 94 | 95 | \item{errorHeight}{Height of error bars} 96 | 97 | \item{dodgeHeight}{Amount of vertical dodging} 98 | 99 | \item{interactive}{If \code{TRUE} an interactive plot is generated instead of \verb{[ggplot2]}} 100 | } 101 | \value{ 102 | a ggplot graph object 103 | } 104 | \description{ 105 | Build ggplot object for coefplot 106 | } 107 | \details{ 108 | This function builds up the ggplot layer by layer for \code{\link{coefplot.lm}} 109 | } 110 | \examples{ 111 | 112 | data(diamonds) 113 | model1 <- lm(price ~ carat + cut, data=diamonds) 114 | theCI <- coefplot:::buildModelCI(model1) 115 | coefplot:::buildPlotting.default(theCI) 116 | coefplot(model1) 117 | 118 | } 119 | \seealso{ 120 | \code{\link{coefplot.default}} \code{\link{coefplot}} \code{\link{multiplot}} 121 | } 122 | \author{ 123 | Jared P. Lander www.jaredlander.com 124 | } 125 | -------------------------------------------------------------------------------- /man/buildPlottingPloty.default.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/buildPlotPlotly.r 3 | \name{buildPlottingPloty.default} 4 | \alias{buildPlottingPloty.default} 5 | \title{buildPlottingPloty.default} 6 | \usage{ 7 | buildPlottingPloty.default( 8 | modelCI, 9 | title = "Coefficient Plot", 10 | xlab = "Value", 11 | ylab = "Coefficient", 12 | lwdInner = 3, 13 | lwdOuter = 1, 14 | color = "blue", 15 | shape = "circle", 16 | pointSize = 8 17 | ) 18 | } 19 | \arguments{ 20 | \item{modelCI}{An object created by \code{\link{buildModelCI}}} 21 | 22 | \item{title}{The name of the plot, if NULL then no name is given} 23 | 24 | \item{xlab}{The x label} 25 | 26 | \item{ylab}{The y label} 27 | 28 | \item{lwdInner}{The thickness of the inner confidence interval} 29 | 30 | \item{lwdOuter}{The thickness of the outer confidence interval} 31 | 32 | \item{color}{The color of the points and lines} 33 | 34 | \item{shape}{The shape of the points} 35 | 36 | \item{pointSize}{Size of coefficient point} 37 | } 38 | \value{ 39 | a ggplot graph object 40 | } 41 | \description{ 42 | Builds the plotting structure for interactive coefplots 43 | } 44 | \details{ 45 | Uses plotly to make an interactive version of coefplot. Still uses modelCI. 46 | } 47 | \examples{ 48 | 49 | data(diamonds) 50 | mod1 <- lm(price ~ carat + cut, data=diamonds) 51 | theCI1 <- coefplot:::buildModelCI(mod1) 52 | coefplot:::buildPlottingPloty.default(theCI1) 53 | coefplot(mod1, interactive=TRUE) 54 | mod2 <- lm(mpg ~ cyl + qsec - 1, data=mtcars) 55 | mod3 <- lm(mpg ~ cyl + qsec + disp - 1, data=mtcars) 56 | theCI2 <- coefplot:::buildModelCI(mod2) 57 | theCI3 <- coefplot:::buildModelCI(mod3) 58 | coefplot::buildPlottingPloty.default(theCI2) 59 | coefplot::buildPlottingPloty.default(theCI3) 60 | coefplot(mod2, interactive=TRUE) 61 | coefplot(mod3, interactive=TRUE) 62 | 63 | mod4 <- glmnet::glmnet( 64 | x=as.matrix(diamonds[, c('carat', 'x', 'y', 'z')]), 65 | y=diamonds$price 66 | ) 67 | coefplot(mod4, interactive=TRUE, lambda=0.65) 68 | 69 | } 70 | \seealso{ 71 | \code{\link{coefplot.default}} \code{\link{coefplot}} \code{\link{buildPlotting.default}} 72 | } 73 | \author{ 74 | Jared P. Lander 75 | } 76 | -------------------------------------------------------------------------------- /man/coefpath.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/coefpath.r 3 | \name{coefpath} 4 | \alias{coefpath} 5 | \alias{coefpath.glmnet} 6 | \alias{coefpath.cv.glmnet} 7 | \title{coefpath} 8 | \usage{ 9 | coefpath(model, ...) 10 | 11 | \method{coefpath}{glmnet}( 12 | model, 13 | xlab = "Log Lambda", 14 | ylab = "Coefficients", 15 | showLegend = c("onmouseover", "auto", "always", "follow", "never"), 16 | annotate = TRUE, 17 | elementID = NULL, 18 | ... 19 | ) 20 | 21 | \method{coefpath}{cv.glmnet}( 22 | model, 23 | xlab = "Log Lambda", 24 | ylab = "Coefficients", 25 | showLegend = c("onmouseover", "auto", "always", "follow", "never"), 26 | annotate = TRUE, 27 | colorMin = "black", 28 | strokePatternMin = "dotted", 29 | labelMin = "lambda.min", 30 | locMin = c("bottom", "top"), 31 | color1se = "black", 32 | strokePattern1se = "dotted", 33 | label1se = "lambda.1se", 34 | loc1se = c("bottom", "top"), 35 | ... 36 | ) 37 | } 38 | \arguments{ 39 | \item{model}{A \code{\link[glmnet]{glmnet}} model} 40 | 41 | \item{\dots}{Arguments passed on to \code{\link{extractPath}}} 42 | 43 | \item{xlab}{x-axis label} 44 | 45 | \item{ylab}{y-axis label} 46 | 47 | \item{showLegend}{When to display the legend. Specify "always" to always show the legend. Specify "onmouseover" to only display it when a user mouses over the chart. Specify "follow" to have the legend show as overlay to the chart which follows the mouse. The default behavior is "auto", which results in "always" when more than one series is plotted and "onmouseover" when only a single series is plotted.} 48 | 49 | \item{annotate}{If \code{TRUE} (default) plot the name of the series} 50 | 51 | \item{elementID}{Unique identified for dygraph, if \code{NULL} it will be randomly generated} 52 | 53 | \item{colorMin}{Color for line showing lambda.min} 54 | 55 | \item{strokePatternMin}{Stroke pattern for line showing lambda.min} 56 | 57 | \item{labelMin}{Label for line showing lambda.min} 58 | 59 | \item{locMin}{Location for line showing lambda.min, can be 'bottom' or 'top'} 60 | 61 | \item{color1se}{Color for line showing lambda.1se} 62 | 63 | \item{strokePattern1se}{Stroke pattern for line showing lambda.1se} 64 | 65 | \item{label1se}{Label for line showing lambda.1se} 66 | 67 | \item{loc1se}{Location for line showing lambda.1se, can be 'bottom' or 'top'} 68 | } 69 | \value{ 70 | A dygraphs object 71 | } 72 | \description{ 73 | Visualize the coefficient path resulting from the elastic net 74 | } 75 | \details{ 76 | This is a replacement plot for visualizing the coefficient path resulting from the elastic net. This allows for interactively inspecting the plot so it is easier to disambiguate the coefficients. 77 | } 78 | \examples{ 79 | 80 | \dontshow{if(requireNamespace('glmnet', quietly=TRUE))\{} 81 | library(glmnet) 82 | library(ggplot2) 83 | library(useful) 84 | data(diamonds) 85 | diaX <- useful::build.x(price ~ carat + cut + x - 1, data=diamonds, contrasts = TRUE) 86 | diaY <- useful::build.y(price ~ carat + cut + x - 1, data=diamonds) 87 | modG1 <- glmnet(x=diaX, y=diaY) 88 | coefpath(modG1) 89 | 90 | modG2 <- cv.glmnet(x=diaX, y=diaY, nfolds=5) 91 | coefpath(modG2) 92 | 93 | x <- matrix(rnorm(100*20),100,20) 94 | y <- rnorm(100) 95 | fit1 <- glmnet(x, y) 96 | coefpath(fit1) 97 | \dontshow{\}} 98 | 99 | } 100 | \author{ 101 | Jared P. Lander 102 | } 103 | -------------------------------------------------------------------------------- /man/coefplot.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/coefplot-package.r, R/coefplot.r 3 | \docType{package} 4 | \name{coefplot} 5 | \alias{coefplot} 6 | \alias{coefplot-package} 7 | \alias{plotcoef} 8 | \alias{coefplot.default} 9 | \alias{coefplot.lm} 10 | \alias{coefplot.glm} 11 | \alias{coefplot.workflow} 12 | \alias{coefplot.model_fit} 13 | \alias{coefplot.rxGlm} 14 | \alias{coefplot.rxLinMod} 15 | \alias{coefplot.rxLogit} 16 | \title{Plotting Model Coefficients} 17 | \usage{ 18 | coefplot(model, ...) 19 | 20 | \method{coefplot}{default}( 21 | model, 22 | title = "Coefficient Plot", 23 | xlab = "Value", 24 | ylab = "Coefficient", 25 | innerCI = 1, 26 | outerCI = 2, 27 | lwdInner = 1 + interactive * 2, 28 | lwdOuter = if (interactive) 1 else unname((Sys.info()["sysname"] != "Windows") * 0.5), 29 | pointSize = 3 + interactive * 5, 30 | color = "blue", 31 | shape = 16, 32 | cex = 0.8, 33 | textAngle = 0, 34 | numberAngle = 0, 35 | zeroColor = "grey", 36 | zeroLWD = 1, 37 | zeroType = 2, 38 | facet = FALSE, 39 | scales = "free", 40 | sort = c("natural", "magnitude", "alphabetical"), 41 | decreasing = FALSE, 42 | numeric = FALSE, 43 | fillColor = "grey", 44 | alpha = 1/2, 45 | horizontal = FALSE, 46 | factors = NULL, 47 | only = NULL, 48 | shorten = TRUE, 49 | intercept = TRUE, 50 | interceptName = "(Intercept)", 51 | coefficients = NULL, 52 | predictors = NULL, 53 | strict = FALSE, 54 | trans = identity, 55 | interactive = FALSE, 56 | newNames = NULL, 57 | plot = TRUE, 58 | ... 59 | ) 60 | 61 | \method{coefplot}{lm}(...) 62 | 63 | \method{coefplot}{glm}(...) 64 | 65 | \method{coefplot}{workflow}(model, ...) 66 | 67 | \method{coefplot}{model_fit}(model, ...) 68 | 69 | \method{coefplot}{rxGlm}(...) 70 | 71 | \method{coefplot}{rxLinMod}(...) 72 | 73 | \method{coefplot}{rxLogit}(...) 74 | } 75 | \arguments{ 76 | \item{model}{A parsnip object} 77 | 78 | \item{\dots}{All arguments are passed on to \code{\link{coefplot.lm}}. Please see that function for argument information.} 79 | 80 | \item{title}{The name of the plot, if NULL then no name is given} 81 | 82 | \item{xlab}{The x label} 83 | 84 | \item{ylab}{The y label} 85 | 86 | \item{innerCI}{How wide the inner confidence interval should be, normally 1 standard deviation. If 0, then there will be no inner confidence interval.} 87 | 88 | \item{outerCI}{How wide the outer confidence interval should be, normally 2 standard deviations. If 0, then there will be no outer confidence interval.} 89 | 90 | \item{lwdInner}{The thickness of the inner confidence interval} 91 | 92 | \item{lwdOuter}{The thickness of the outer confidence interval} 93 | 94 | \item{pointSize}{Size of coefficient point} 95 | 96 | \item{color}{The color of the points and lines} 97 | 98 | \item{shape}{The shape of the points} 99 | 100 | \item{cex}{The text size multiplier, currently not used} 101 | 102 | \item{textAngle}{The angle for the coefficient labels, 0 is horizontal} 103 | 104 | \item{numberAngle}{The angle for the value labels, 0 is horizontal} 105 | 106 | \item{zeroColor}{The color of the line indicating 0} 107 | 108 | \item{zeroLWD}{The thickness of the 0 line} 109 | 110 | \item{zeroType}{The type of 0 line, 0 will mean no line} 111 | 112 | \item{facet}{logical; If the coefficients should be faceted by the variables, numeric coefficients (including the intercept) will be one facet. Currently not available.} 113 | 114 | \item{scales}{The way the axes should be treated in a faceted plot. Can be c("fixed", "free", "free_x", "free_y"). Currently not available.} 115 | 116 | \item{sort}{Determines the sort order of the coefficients. Possible values are c("natural", "magnitude", "alphabetical")} 117 | 118 | \item{decreasing}{logical; Whether the coefficients should be ascending or descending} 119 | 120 | \item{numeric}{logical; If true and factors has exactly one value, then it is displayed in a horizontal graph with continuous confidence bounds. Currently not available.} 121 | 122 | \item{fillColor}{The color of the confidence bounds for a numeric factor. Currently not available.} 123 | 124 | \item{alpha}{The transparency level of the numeric factor's confidence bound. Currently not available.} 125 | 126 | \item{horizontal}{logical; If the plot should be displayed horizontally. Currently not available.} 127 | 128 | \item{factors}{Vector of factor variables that will be the only ones shown} 129 | 130 | \item{only}{logical; If factors has a value this determines how interactions are treated. True means just that variable will be shown and not its interactions. False means interactions will be included.} 131 | 132 | \item{shorten}{logical or character; If \code{FALSE} then coefficients for factor levels will include their variable name. If \code{TRUE} coefficients for factor levels will be stripped of their variable names. If a character vector of variables only coefficients for factor levels associated with those variables will the variable names stripped. Currently not available.} 133 | 134 | \item{intercept}{logical; Whether the Intercept coefficient should be plotted} 135 | 136 | \item{interceptName}{Specifies name of intercept it case it is not the default of "(Intercept").} 137 | 138 | \item{coefficients}{A character vector specifying which factor coefficients to keep. It will keep all levels and any interactions, even if those are not listed.} 139 | 140 | \item{predictors}{A character vector specifying which coefficients to keep. Each individual coefficient can be specified. Use predictors to specify entire factors.} 141 | 142 | \item{strict}{If TRUE then predictors will only be matched to its own coefficients, not its interactions} 143 | 144 | \item{trans}{A transformation function to apply to the values and confidence intervals. \code{identity} by default. Use \code{invlogit} for binary regression.} 145 | 146 | \item{interactive}{If \code{TRUE} an interactive plot is generated instead of \code{ggplot2}} 147 | 148 | \item{newNames}{Named character vector of new names for coefficients} 149 | 150 | \item{plot}{logical; If the plot should be drawn, if false then a data.frame of the values will be returned} 151 | } 152 | \value{ 153 | A ggplot2 object or data.frame. See details in \code{\link{coefplot.lm}} for more information 154 | 155 | If \code{plot} is \code{TRUE} then a \code{\link{ggplot}} object is returned. Otherwise a \code{\link{data.frame}} listing coefficients and confidence bands is returned. 156 | 157 | A ggplot object. See \code{\link{coefplot.lm}} for more information. 158 | 159 | A ggplot object. See \code{\link{coefplot.lm}} for more information. 160 | 161 | A ggplot object. See \code{\link{coefplot.lm}} for more information. 162 | } 163 | \description{ 164 | Provides an S3 generic method for plotting coefficients from a model so it can be extended to other model types. 165 | 166 | A graphical display of the coefficients and standard errors from a fitted model 167 | 168 | \code{coefplot} is the S3 generic method for plotting the coefficients from a fitted model. 169 | 170 | This can be extended with new methods for other types of models not currently available. 171 | 172 | Coefplot method for workflow objects 173 | 174 | Coefplot method for parsnip objects 175 | } 176 | \details{ 177 | Currently, methods are available for lm, glm and rxLinMod objects. 178 | 179 | For more information on this function and it's arguments see \code{\link{coefplot.default}} 180 | 181 | Pulls model element out of workflow object then calls \code{coefplot}. 182 | 183 | Pulls model element out of parsnip object then calls \code{coefplot}. 184 | } 185 | \section{Methods (by class)}{ 186 | \itemize{ 187 | \item \code{default}: Default method 188 | 189 | \item \code{lm}: \code{lm} 190 | 191 | \item \code{glm}: \code{glm} 192 | 193 | \item \code{workflow}: \code{tidymodels workflows} 194 | 195 | \item \code{model_fit}: \code{parsnip} 196 | 197 | \item \code{rxGlm}: \code{rxGlm} 198 | 199 | \item \code{rxLinMod}: \code{rxLinMod} 200 | 201 | \item \code{rxLogit}: \code{rxLogit} 202 | }} 203 | 204 | \examples{ 205 | 206 | data(diamonds) 207 | head(diamonds) 208 | model1 <- lm(price ~ carat + cut*color, data=diamonds) 209 | model2 <- lm(price ~ carat*color, data=diamonds) 210 | model3 <- glm(price > 10000 ~ carat*color, data=diamonds) 211 | coefplot(model1) 212 | coefplot(model2) 213 | coefplot(model3) 214 | coefplot(model1, predictors="color") 215 | coefplot(model1, predictors="color", strict=TRUE) 216 | coefplot(model1, coefficients=c("(Intercept)", "color.Q")) 217 | coefplot(model1, predictors="cut", coefficients=c("(Intercept)", "color.Q"), strict=TRUE) 218 | coefplot(model1, predictors="cut", coefficients=c("(Intercept)", "color.Q"), strict=FALSE) 219 | coefplot(model1, predictors="cut", coefficients=c("(Intercept)", "color.Q"), 220 | strict=TRUE, newNames=c(color.Q="Color", "cut^4"="Fourth")) 221 | coefplot(model1, predictors=c("(Intercept)", "carat"), newNames=c(carat="Size")) 222 | coefplot(model1, predictors=c("(Intercept)", "carat"), 223 | newNames=c(carat="Size", "(Intercept)"="Constant")) 224 | 225 | 226 | data(diamonds) 227 | head(diamonds) 228 | model1 <- lm(price ~ carat + cut*color, data=diamonds) 229 | model2 <- lm(price ~ carat*color, data=diamonds) 230 | coefplot(model1) 231 | coefplot(model2) 232 | coefplot(model1, predictors="color") 233 | coefplot(model1, predictors="color", strict=TRUE) 234 | coefplot(model1, coefficients=c("(Intercept)", "color.Q")) 235 | 236 | 237 | model1 <- lm(price ~ carat + cut*color, data=diamonds) 238 | coefplot(model1) 239 | 240 | model2 <- glm(price > 10000 ~ carat + cut*color, data=diamonds, family=binomial(link="logit")) 241 | coefplot(model2) 242 | coefplot(model2, trans=invlogit) 243 | 244 | \dontrun{ 245 | mod4 <- rxGlm(price ~ carat + cut + x, data=diamonds) 246 | mod5 <- rxGlm(price > 10000 ~ carat + cut + x, data=diamonds, family="binomial") 247 | coefplot(mod4) 248 | coefplot(mod5) 249 | } 250 | 251 | 252 | \dontrun{ 253 | data(diamonds) 254 | mod3 <- rxLinMod(price ~ carat + cut + x, data=diamonds) 255 | coefplot(mod3) 256 | } 257 | 258 | \dontrun{ 259 | data(diamonds) 260 | mod6 <- rxLogit(price > 10000 ~ carat + cut + x, data=diamonds) 261 | coefplot(mod6) 262 | } 263 | } 264 | \seealso{ 265 | \code{\link{coefplot.lm}} \code{\link{coefplot.data.frame}} 266 | 267 | \code{\link{lm}} \code{\link{glm}} \code{\link{ggplot}} \code{\link{coefplot}} \code{\link{plotcoef}} 268 | } 269 | \author{ 270 | Jared P. Lander 271 | } 272 | \keyword{coefficient} 273 | \keyword{coefficients} 274 | \keyword{coefplot} 275 | \keyword{dotplot} 276 | \keyword{glm} 277 | \keyword{linear} 278 | \keyword{lm} 279 | \keyword{model} 280 | \keyword{rxLinMod} 281 | -------------------------------------------------------------------------------- /man/coefplot.data.frame.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/coefplot.r 3 | \name{coefplot.data.frame} 4 | \alias{coefplot.data.frame} 5 | \title{coefplot.data.frame} 6 | \usage{ 7 | \method{coefplot}{data.frame}( 8 | model, 9 | title = "Coefficient Plot", 10 | xlab = "Value", 11 | ylab = "Coefficient", 12 | interactive = FALSE, 13 | lwdInner = 1 + interactive * 2, 14 | lwdOuter = if (interactive) 1 else unname((Sys.info()["sysname"] != "Windows") * 0.5), 15 | pointSize = 3 + interactive * 5, 16 | color = "blue", 17 | cex = 0.8, 18 | textAngle = 0, 19 | numberAngle = 0, 20 | shape = 16, 21 | linetype = 1, 22 | outerCI = 2, 23 | innerCI = 1, 24 | multi = FALSE, 25 | zeroColor = "grey", 26 | zeroLWD = 1, 27 | zeroType = 2, 28 | numeric = FALSE, 29 | fillColor = "grey", 30 | alpha = 1/2, 31 | horizontal = FALSE, 32 | facet = FALSE, 33 | scales = "free", 34 | value = "Value", 35 | coefficient = "Coefficient", 36 | errorHeight = 0, 37 | dodgeHeight = 1, 38 | ... 39 | ) 40 | } 41 | \arguments{ 42 | \item{model}{A data.frame like that built from coefplot(..., plot=FALSE)} 43 | 44 | \item{title}{The name of the plot, if NULL then no name is given} 45 | 46 | \item{xlab}{The x label} 47 | 48 | \item{ylab}{The y label} 49 | 50 | \item{interactive}{If `TRUE` an interactive plot is generated instead of `[ggplot2]`} 51 | 52 | \item{lwdInner}{The thickness of the inner confidence interval} 53 | 54 | \item{lwdOuter}{The thickness of the outer confidence interval} 55 | 56 | \item{pointSize}{Size of coefficient point} 57 | 58 | \item{color}{The color of the points and lines} 59 | 60 | \item{cex}{The text size multiplier, currently not used} 61 | 62 | \item{textAngle}{The angle for the coefficient labels, 0 is horizontal} 63 | 64 | \item{numberAngle}{The angle for the value labels, 0 is horizontal} 65 | 66 | \item{shape}{The shape of the points} 67 | 68 | \item{linetype}{The linetype of the error bars} 69 | 70 | \item{outerCI}{How wide the outer confidence interval should be, normally 2 standard deviations. If 0, then there will be no outer confidence interval.} 71 | 72 | \item{innerCI}{How wide the inner confidence interval should be, normally 1 standard deviation. If 0, then there will be no inner confidence interval.} 73 | 74 | \item{multi}{logical; If this is for \code{\link{multiplot}} then leave the colors as determined by the legend, if FALSE then make all colors the same} 75 | 76 | \item{zeroColor}{The color of the line indicating 0} 77 | 78 | \item{zeroLWD}{The thickness of the 0 line} 79 | 80 | \item{zeroType}{The type of 0 line, 0 will mean no line} 81 | 82 | \item{numeric}{logical; If true and factors has exactly one value, then it is displayed in a horizontal graph with continuous confidence bounds.} 83 | 84 | \item{fillColor}{The color of the confidence bounds for a numeric factor} 85 | 86 | \item{alpha}{The transparency level of the numeric factor's confidence bound} 87 | 88 | \item{horizontal}{logical; If the plot should be displayed horizontally} 89 | 90 | \item{facet}{logical; If the coefficients should be faceted by the variables, numeric coefficients (including the intercept) will be one facet} 91 | 92 | \item{scales}{The way the axes should be treated in a faceted plot. Can be c("fixed", "free", "free_x", "free_y")} 93 | 94 | \item{value}{Name of variable for value metric} 95 | 96 | \item{coefficient}{Name of variable for coefficient names} 97 | 98 | \item{errorHeight}{Height of error bars} 99 | 100 | \item{dodgeHeight}{Amount of vertical dodging} 101 | 102 | \item{\dots}{Further Arguments} 103 | } 104 | \value{ 105 | a ggplot graph object 106 | } 107 | \description{ 108 | Dotplot for coefficients 109 | } 110 | \details{ 111 | A graphical display of the coefficients and standard errors from a fitted model, this function uses a data.frame as the input. 112 | } 113 | \examples{ 114 | data(diamonds) 115 | head(diamonds) 116 | model1 <- lm(price ~ carat + cut*color, data=diamonds) 117 | model2 <- lm(price ~ carat*color, data=diamonds) 118 | df1 <- coefplot(model1, plot=FALSE) 119 | df2 <- coefplot(model2, plot=FALSE) 120 | coefplot(df1) 121 | coefplot(df2) 122 | 123 | } 124 | \seealso{ 125 | \code{\link{coefplot}} 126 | } 127 | \author{ 128 | Jared P. Lander 129 | } 130 | -------------------------------------------------------------------------------- /man/doRegex.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/coefPredMatching.r 3 | \name{doRegex} 4 | \alias{doRegex} 5 | \title{doRegex} 6 | \usage{ 7 | doRegex(x, matchAgainst, pattern = "(^| )\%s($|,|=)") 8 | } 9 | \arguments{ 10 | \item{x}{Root pattern to search for} 11 | 12 | \item{matchAgainst}{Text to search through} 13 | 14 | \item{pattern}{Regex pattern to build x into} 15 | } 16 | \value{ 17 | A list of indices of matchAgainst that is matched 18 | } 19 | \description{ 20 | Helper function for matching coefficients 21 | } 22 | \details{ 23 | Only used by \code{\link{getCoefsFromPredictorsRevo}} for finding matches between predictors and coefficients 24 | } 25 | \author{ 26 | Jared P. Lander 27 | } 28 | -------------------------------------------------------------------------------- /man/extract.coef.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/extractCoef.r 3 | \name{extract.coef} 4 | \alias{extract.coef} 5 | \title{extract.coef} 6 | \usage{ 7 | extract.coef(model, ...) 8 | } 9 | \arguments{ 10 | \item{model}{Model object to extract information from.} 11 | 12 | \item{\dots}{Further arguments} 13 | } 14 | \value{ 15 | A \code{\link{data.frame}} containing the coefficient, the standard 16 | error and the variable name. 17 | } 18 | \description{ 19 | Extract Coefficient Information from glm Models 20 | } 21 | \details{ 22 | Gets the coefficient values and standard errors, and variable names from a 23 | glm model. 24 | } 25 | \examples{ 26 | \dontrun{ 27 | library(ggplot2) 28 | data(diamonds) 29 | library(coefplot) 30 | mod1 <- lm(price ~ carat + cut + x, data=diamonds) 31 | mod2 <- glm(price > 10000 ~ carat + cut + x, data=diamonds, family=binomial(link="logit")) 32 | mod3 <- lm(price ~ carat*cut + x, data=diamonds) 33 | extract.coef(mod1) 34 | extract.coef(mod2) 35 | extract.coef(mod3) 36 | 37 | mod4 <- rxLinMod(price ~ carat*cut + x, diamonds) 38 | } 39 | 40 | } 41 | \author{ 42 | Jared P. Lander 43 | } 44 | -------------------------------------------------------------------------------- /man/extract.coef.cv.glmnet.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/extractCoef.r 3 | \name{extract.coef.cv.glmnet} 4 | \alias{extract.coef.cv.glmnet} 5 | \title{extract.coef.cv.glmnet} 6 | \usage{ 7 | \method{extract.coef}{cv.glmnet}(model, lambda = "lambda.min", ...) 8 | } 9 | \arguments{ 10 | \item{model}{Model object from which to extract information.} 11 | 12 | \item{lambda}{Value of penalty parameter. Can be either a numeric value or 13 | one of "lambda.min" or "lambda.1se"} 14 | 15 | \item{\dots}{Further arguments} 16 | } 17 | \value{ 18 | A \code{\link{data.frame}} containing the coefficient, the standard 19 | error and the variable name. 20 | } 21 | \description{ 22 | Extract Coefficient Information from Models 23 | } 24 | \details{ 25 | Gets the coefficient values and variable names from a model. Since 26 | glmnet does not have standard errors, those will just be NA. 27 | } 28 | \examples{ 29 | \dontshow{if(requireNamespace('glmnet', quietly=TRUE))\{} 30 | library(glmnet) 31 | library(ggplot2) 32 | library(useful) 33 | data(diamonds) 34 | diaX <- useful::build.x(price ~ carat + cut + x - 1, data=diamonds, 35 | contrasts=FALSE) 36 | diaY <- useful::build.y(price ~ carat + cut + x - 1, data=diamonds) 37 | modG1 <- cv.glmnet(x=diaX, y=diaY, k=5) 38 | extract.coef(modG1) 39 | \dontshow{\}} 40 | 41 | } 42 | \author{ 43 | Jared P. Lander 44 | } 45 | -------------------------------------------------------------------------------- /man/extract.coef.default.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/extractCoef.r 3 | \name{extract.coef.default} 4 | \alias{extract.coef.default} 5 | \title{extract.coef.default} 6 | \usage{ 7 | \method{extract.coef}{default}(model, ...) 8 | } 9 | \arguments{ 10 | \item{model}{Model object to extract information from.} 11 | 12 | \item{\dots}{Further arguments} 13 | } 14 | \value{ 15 | A \code{\link{data.frame}} containing the coefficient, the standard 16 | error and the variable name. 17 | } 18 | \description{ 19 | Extract Coefficient Information from Models 20 | } 21 | \details{ 22 | Gets the coefficient values and standard errors, and variable names from a model. 23 | } 24 | \examples{ 25 | \dontrun{ 26 | library(ggplot2) 27 | library(coefplot) 28 | data(diamonds) 29 | mod1 <- lm(price ~ carat + cut + x, data=diamonds) 30 | extract.coef(mod1) 31 | } 32 | 33 | } 34 | \author{ 35 | Jared P. Lander 36 | } 37 | -------------------------------------------------------------------------------- /man/extract.coef.glm.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/extractCoef.r 3 | \name{extract.coef.glm} 4 | \alias{extract.coef.glm} 5 | \title{extract.coef.glm} 6 | \usage{ 7 | \method{extract.coef}{glm}(model, ...) 8 | } 9 | \arguments{ 10 | \item{model}{Model object to extract information from.} 11 | 12 | \item{...}{Further arguments} 13 | } 14 | \value{ 15 | A \code{\link{data.frame}} containing the coefficient, the standard 16 | error and the variable name. 17 | } 18 | \description{ 19 | Extract Coefficient Information from glm Models 20 | } 21 | \details{ 22 | Gets the coefficient values and standard errors, and variable names from a glm model. 23 | } 24 | \examples{ 25 | \dontrun{ 26 | library(ggplot2) 27 | data(diamonds) 28 | library(coefplot) 29 | mod2 <- glm(price > 10000 ~ carat + cut + x, data=diamonds, family=binomial(link="logit")) 30 | extract.coef(mod2) 31 | } 32 | 33 | } 34 | \author{ 35 | Jared P. Lander 36 | } 37 | -------------------------------------------------------------------------------- /man/extract.coef.glmnet.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/extractCoef.r 3 | \name{extract.coef.glmnet} 4 | \alias{extract.coef.glmnet} 5 | \title{extract.coef.glmnet} 6 | \usage{ 7 | \method{extract.coef}{glmnet}(model, lambda = stats::median(model$lambda), ...) 8 | } 9 | \arguments{ 10 | \item{model}{Model object from which to extract information.} 11 | 12 | \item{lambda}{Value of penalty parameter} 13 | 14 | \item{\dots}{Further arguments} 15 | } 16 | \value{ 17 | A \code{\link{data.frame}} containing the coefficient, the standard 18 | error and the variable name. 19 | } 20 | \description{ 21 | Extract Coefficient Information from Models 22 | } 23 | \details{ 24 | Gets the coefficient values and variable names from a model. Since 25 | glmnet does not have standard errors, those will just be NA. 26 | } 27 | \author{ 28 | Jared P. Lander 29 | } 30 | -------------------------------------------------------------------------------- /man/extract.coef.lm.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/extractCoef.r 3 | \name{extract.coef.lm} 4 | \alias{extract.coef.lm} 5 | \title{extract.coef.lm} 6 | \usage{ 7 | \method{extract.coef}{lm}(model, ...) 8 | } 9 | \arguments{ 10 | \item{model}{Model object to extract information from.} 11 | 12 | \item{...}{Further arguments} 13 | } 14 | \value{ 15 | A \code{\link{data.frame}} containing the coefficient, the standard 16 | error and the variable name. 17 | } 18 | \description{ 19 | Extract Coefficient Information from lm Models 20 | } 21 | \details{ 22 | Gets the coefficient values and standard errors, and variable names from an lm model. 23 | } 24 | \examples{ 25 | \dontrun{ 26 | library(ggplot2) 27 | data(diamonds) 28 | library(coefplot) 29 | mod1 <- lm(price ~ carat + cut + x, data=diamonds) 30 | extract.coef(mod1) 31 | } 32 | 33 | } 34 | \author{ 35 | Jared P. Lander 36 | } 37 | -------------------------------------------------------------------------------- /man/extract.coef.maxLik.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/extractCoef.r 3 | \name{extract.coef.maxLik} 4 | \alias{extract.coef.maxLik} 5 | \title{extract.coef.maxLik} 6 | \usage{ 7 | \method{extract.coef}{maxLik}(model, ...) 8 | } 9 | \arguments{ 10 | \item{model}{Model object from which to extract information.} 11 | 12 | \item{\dots}{Further arguments} 13 | } 14 | \value{ 15 | A \code{\link{data.frame}} containing the coefficient, the standard 16 | error and the variable name. 17 | } 18 | \description{ 19 | Extract Coefficient Information from Models 20 | } 21 | \details{ 22 | Gets the coefficient values and variable names from a model. 23 | } 24 | \author{ 25 | Jared P. Lander 26 | } 27 | -------------------------------------------------------------------------------- /man/extract.coef.rxGlm.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/extractCoef.r 3 | \name{extract.coef.rxGlm} 4 | \alias{extract.coef.rxGlm} 5 | \title{extract.coef.rxGlm} 6 | \usage{ 7 | \method{extract.coef}{rxGlm}(model, ...) 8 | } 9 | \arguments{ 10 | \item{model}{Model object to extract information from.} 11 | 12 | \item{...}{Further arguments} 13 | } 14 | \value{ 15 | A \code{\link{data.frame}} containing the coefficient, the standard 16 | error and the variable name. 17 | } 18 | \description{ 19 | Extract Coefficient Information from rxGlm Models 20 | } 21 | \details{ 22 | Gets the coefficient values and standard errors, and variable names from an 23 | rxGlm model. 24 | } 25 | \examples{ 26 | \dontrun{ 27 | library(ggplot2) 28 | data(diamonds) 29 | mod4 <- rxGlm(price ~ carat + cut + x, data=diamonds) 30 | mod5 <- rxGlm(price > 10000 ~ carat + cut + x, data=diamonds, fmaily="binomial") 31 | extract.coef(mod4) 32 | extract.coef(mod5) 33 | } 34 | 35 | } 36 | \author{ 37 | Jared P. Lander 38 | } 39 | -------------------------------------------------------------------------------- /man/extract.coef.rxLinMod.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/extractCoef.r 3 | \name{extract.coef.rxLinMod} 4 | \alias{extract.coef.rxLinMod} 5 | \title{extract.coef.rxLinMod} 6 | \usage{ 7 | \method{extract.coef}{rxLinMod}(model, ...) 8 | } 9 | \arguments{ 10 | \item{model}{Model object to extract information from.} 11 | 12 | \item{...}{Further arguments} 13 | } 14 | \value{ 15 | A \code{\link{data.frame}} containing the coefficient, the standard 16 | error and the variable name. 17 | } 18 | \description{ 19 | Extract Coefficient Information from rxLinMod Models 20 | } 21 | \details{ 22 | Gets the coefficient values and standard errors, and variable names from an 23 | rxLinMod model. 24 | } 25 | \examples{ 26 | \dontrun{ 27 | library(ggplot2) 28 | data(diamonds) 29 | mod3 <- rxLinMod(price ~ carat + cut + x, data=diamonds) 30 | extract.coef(mod3) 31 | } 32 | 33 | } 34 | \author{ 35 | Jared P. Lander 36 | } 37 | -------------------------------------------------------------------------------- /man/extract.coef.rxLogit.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/extractCoef.r 3 | \name{extract.coef.rxLogit} 4 | \alias{extract.coef.rxLogit} 5 | \title{extract.coef.rxLogit} 6 | \usage{ 7 | \method{extract.coef}{rxLogit}(model, ...) 8 | } 9 | \arguments{ 10 | \item{model}{Model object to extract information from.} 11 | 12 | \item{...}{Further arguments} 13 | } 14 | \value{ 15 | A \code{\link{data.frame}} containing the coefficient, the standard 16 | error and the variable name. 17 | } 18 | \description{ 19 | Extract Coefficient Information from rxLogit Models 20 | } 21 | \details{ 22 | Gets the coefficient values and standard errors, and variable names from an 23 | rxLogit model. 24 | } 25 | \examples{ 26 | \dontrun{ 27 | library(ggplot2) 28 | data(diamonds) 29 | mod6 <- rxLogit(price > 10000 ~ carat + cut + x, data=diamonds) 30 | extract.coef(mod6) 31 | } 32 | 33 | } 34 | \author{ 35 | Jared P. Lander 36 | } 37 | -------------------------------------------------------------------------------- /man/extract.coef.xgb.Booster.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/extractCoef.r 3 | \name{extract.coef.xgb.Booster} 4 | \alias{extract.coef.xgb.Booster} 5 | \title{extract.coef.xgb.Booster} 6 | \usage{ 7 | \method{extract.coef}{xgb.Booster}( 8 | model, 9 | feature_names = NULL, 10 | removeNonSelected = TRUE, 11 | zero_threshold = 0.001, 12 | ... 13 | ) 14 | } 15 | \arguments{ 16 | \item{model}{Model object from which to extract information.} 17 | 18 | \item{feature_names}{Names of coefficients} 19 | 20 | \item{removeNonSelected}{If \code{TRUE} (default) do not return the non-selected (0) coefficients} 21 | 22 | \item{zero_threshold}{Since \code{coefficients} from 23 | \code{\link[xgboost]{xgboost}} are not exactly zero, 24 | this is the threshold under which a coefficient is considered zero} 25 | 26 | \item{\dots}{Further arguments} 27 | } 28 | \value{ 29 | A \code{\link{data.frame}} containing the coefficient, the standard 30 | error and the variable name. 31 | } 32 | \description{ 33 | Extract Coefficient Information from Models 34 | } 35 | \details{ 36 | Gets the coefficient values and variable names from a model. Since 37 | xgboost does not have standard errors, those will just be NA. 38 | } 39 | \examples{ 40 | \dontshow{ 41 | if(requireNamespace('xgboost', quietly=TRUE))\{} 42 | library(xgboost) 43 | data(diamonds, package='ggplot2') 44 | diaX <- useful::build.x(price ~ carat + cut + x, data=diamonds, contrasts=FALSE) 45 | diaY <- useful::build.y(price ~ carat + cut + x, data=diamonds) 46 | xg1 <- xgb.train(data=xgb.DMatrix(data=diaX, label=diaY), 47 | booster='gblinear', 48 | objective='reg:squarederror', eval_metric='rmse', 49 | nrounds=50 50 | ) 51 | extract.coef(xg1) 52 | extract.coef(xg1, zero_threshold=0) 53 | extract.coef(xg1, feature_names=colnames(diaX)) 54 | \dontshow{\}} 55 | 56 | } 57 | \author{ 58 | Jared P. Lander 59 | } 60 | -------------------------------------------------------------------------------- /man/extractPath.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/extractPath.r 3 | \name{extractPath} 4 | \alias{extractPath} 5 | \alias{extractPath.glmnet} 6 | \alias{extractPath.cv.glmnet} 7 | \title{extractPath} 8 | \usage{ 9 | extractPath(model, ...) 10 | 11 | \method{extractPath}{glmnet}(model, intercept = FALSE, ...) 12 | 13 | \method{extractPath}{cv.glmnet}(model, ...) 14 | } 15 | \arguments{ 16 | \item{model}{A \code{\link[glmnet]{glmnet}} model} 17 | 18 | \item{\dots}{Further arguments} 19 | 20 | \item{intercept}{If \code{FALSE} (the default), no intercept will be provided} 21 | } 22 | \value{ 23 | A \code{link[tibble]{tibble}} holding the coefficients for various lambdas 24 | } 25 | \description{ 26 | Extracts the coefficient path of the elastic net 27 | } 28 | \details{ 29 | This is a replacement plot for visualizing the coefficient path resulting from the elastic net. 30 | } 31 | \examples{ 32 | 33 | library(glmnet) 34 | data(diamonds, package='ggplot2') 35 | diaX <- useful::build.x(price ~ carat + cut + x - 1, data=diamonds, contrasts = TRUE) 36 | diaY <- useful::build.y(price ~ carat + cut + x - 1, data=diamonds) 37 | modG1 <- glmnet(x=diaX, y=diaY) 38 | extractPath(modG1) 39 | 40 | modG2 <- cv.glmnet(x=diaX, y=diaY, nfolds=5) 41 | extractPath(modG2) 42 | 43 | } 44 | \author{ 45 | Jared P. Lander 46 | } 47 | -------------------------------------------------------------------------------- /man/get.assign.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/coefPredMatching.r 3 | \name{get.assign} 4 | \alias{get.assign} 5 | \title{get.assign} 6 | \usage{ 7 | get.assign(model, ...) 8 | } 9 | \arguments{ 10 | \item{model}{Fitted model} 11 | 12 | \item{\dots}{Further arguments} 13 | } 14 | \value{ 15 | The assignment vector 16 | } 17 | \description{ 18 | The assignment vector for a model 19 | } 20 | \details{ 21 | Gets relative positions of predictors 22 | } 23 | \author{ 24 | Jared P. Lander 25 | } 26 | -------------------------------------------------------------------------------- /man/get.assign.glm.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/coefPredMatching.r 3 | \name{get.assign.glm} 4 | \alias{get.assign.glm} 5 | \title{get.assign.glm} 6 | \usage{ 7 | \method{get.assign}{glm}(model, ...) 8 | } 9 | \arguments{ 10 | \item{model}{Fitted model} 11 | 12 | \item{\dots}{Further arguments} 13 | } 14 | \value{ 15 | The assignment vector 16 | } 17 | \description{ 18 | The assignment vector for a glm model 19 | } 20 | \details{ 21 | Gets relative positions of predictors 22 | } 23 | \author{ 24 | Jared P. Lander 25 | } 26 | -------------------------------------------------------------------------------- /man/get.assign.lm.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/coefPredMatching.r 3 | \name{get.assign.lm} 4 | \alias{get.assign.lm} 5 | \title{get.assign.lm} 6 | \usage{ 7 | \method{get.assign}{lm}(model, ...) 8 | } 9 | \arguments{ 10 | \item{model}{Fitted model} 11 | 12 | \item{\dots}{Further arguments} 13 | } 14 | \value{ 15 | The assignment vector 16 | } 17 | \description{ 18 | The assignment vector for an lm model 19 | } 20 | \details{ 21 | Gets relative positions of predictors 22 | } 23 | \author{ 24 | Jared P. Lander 25 | } 26 | -------------------------------------------------------------------------------- /man/getCoefsFromPredictors.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/coefPredMatching.r 3 | \name{getCoefsFromPredictors} 4 | \alias{getCoefsFromPredictors} 5 | \title{getCoefsFromPredictors} 6 | \usage{ 7 | getCoefsFromPredictors(model, predictors, ...) 8 | } 9 | \arguments{ 10 | \item{model}{A fitted model} 11 | 12 | \item{predictors}{A character vector of predictors to match against} 13 | 14 | \item{\dots}{further arguments} 15 | } 16 | \value{ 17 | A character vector of coefficients listing the coefficients that match the predictor 18 | } 19 | \description{ 20 | Generic function for finding which coefficients go with which predictors 21 | } 22 | \details{ 23 | The user specifies predictors whose coefficients should be included in the coefplot. 24 | } 25 | \author{ 26 | Jared P. Lander 27 | } 28 | -------------------------------------------------------------------------------- /man/getCoefsFromPredictors.default.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/coefPredMatching.r 3 | \name{getCoefsFromPredictors.default} 4 | \alias{getCoefsFromPredictors.default} 5 | \title{getCoefsFromPredictors.default} 6 | \usage{ 7 | \method{getCoefsFromPredictors}{default}(model, predictors = NULL, strict = FALSE, ...) 8 | } 9 | \arguments{ 10 | \item{model}{A fitted model} 11 | 12 | \item{predictors}{A character vector of predictors to match against. Interactions can be explicitly specified by VariableA:VariableB.} 13 | 14 | \item{strict}{Logical specifying if interactions terms should be included (\code{FALSE}) or just the main terms (\code{TRUE}).} 15 | 16 | \item{\dots}{further arguments} 17 | } 18 | \value{ 19 | A character vector of coefficients listing the coefficients that match the predictor 20 | } 21 | \description{ 22 | Default function (lm, glm) for matching coefficients with predictors 23 | } 24 | \details{ 25 | The user specifies predictors whose coefficients should be included in the coefplot. 26 | } 27 | \author{ 28 | Jared P. Lander 29 | } 30 | -------------------------------------------------------------------------------- /man/getCoefsFromPredictors.rxGlm.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/coefPredMatching.r 3 | \name{getCoefsFromPredictors.rxGlm} 4 | \alias{getCoefsFromPredictors.rxGlm} 5 | \title{getCoefsFromPredictors.rxGlm} 6 | \usage{ 7 | \method{getCoefsFromPredictors}{rxGlm}(model, predictors = NULL, strict = FALSE, ...) 8 | } 9 | \arguments{ 10 | \item{model}{A fitted model} 11 | 12 | \item{predictors}{A character vector of predictors to match against} 13 | 14 | \item{strict}{Logical specifying if interactions terms should be included (\code{FALSE}) or just the main terms (\code{TRUE}).} 15 | 16 | \item{\dots}{further arguments} 17 | } 18 | \value{ 19 | A character vector of coefficients listing the coefficients that match the predictor 20 | } 21 | \description{ 22 | Function for matching coefficients with predictors for rxGlm 23 | } 24 | \details{ 25 | The user specifies predictors whose coefficients should be included in the coefplot. 26 | } 27 | \author{ 28 | Jared P. Lander 29 | } 30 | -------------------------------------------------------------------------------- /man/getCoefsFromPredictors.rxLinMod.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/coefPredMatching.r 3 | \name{getCoefsFromPredictors.rxLinMod} 4 | \alias{getCoefsFromPredictors.rxLinMod} 5 | \title{getCoefsFromPredictors.rxLinMod} 6 | \usage{ 7 | \method{getCoefsFromPredictors}{rxLinMod}(model, predictors = NULL, strict = FALSE, ...) 8 | } 9 | \arguments{ 10 | \item{model}{A fitted model} 11 | 12 | \item{predictors}{A character vector of predictors to match against} 13 | 14 | \item{strict}{Logical specifying if interactions terms should be included (\code{FALSE}) or just the main terms (\code{TRUE}).} 15 | 16 | \item{\dots}{further arguments} 17 | } 18 | \value{ 19 | A character vector of coefficients listing the coefficients that match the predictor 20 | } 21 | \description{ 22 | Function for matching coefficients with predictors for rxLinMod 23 | } 24 | \details{ 25 | The user specifies predictors whose coefficients should be included in the coefplot. 26 | } 27 | \author{ 28 | Jared P. Lander 29 | } 30 | -------------------------------------------------------------------------------- /man/getCoefsFromPredictors.rxLogit.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/coefPredMatching.r 3 | \name{getCoefsFromPredictors.rxLogit} 4 | \alias{getCoefsFromPredictors.rxLogit} 5 | \title{getCoefsFromPredictors.rxLogit} 6 | \usage{ 7 | \method{getCoefsFromPredictors}{rxLogit}(model, predictors = NULL, strict = FALSE, ...) 8 | } 9 | \arguments{ 10 | \item{model}{A fitted model} 11 | 12 | \item{predictors}{A character vector of predictors to match against} 13 | 14 | \item{strict}{Logical specifying if interactions terms should be included (\code{FALSE}) or just the main terms (\code{TRUE}).} 15 | 16 | \item{\dots}{further arguments} 17 | } 18 | \value{ 19 | A character vector of coefficients listing the coefficients that match the predictor 20 | } 21 | \description{ 22 | Function for matching coefficients with predictors for rxLogit 23 | } 24 | \details{ 25 | The user specifies predictors whose coefficients should be included in the coefplot. 26 | } 27 | \author{ 28 | Jared P. Lander 29 | } 30 | -------------------------------------------------------------------------------- /man/getCoefsFromPredictorsRevo.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/coefPredMatching.r 3 | \name{getCoefsFromPredictorsRevo} 4 | \alias{getCoefsFromPredictorsRevo} 5 | \title{getCoefsFromPredictorsRevo} 6 | \usage{ 7 | getCoefsFromPredictorsRevo(model, predictors = NULL, strict = FALSE, ...) 8 | } 9 | \arguments{ 10 | \item{model}{A fitted model} 11 | 12 | \item{predictors}{A character vector of predictors to match against} 13 | 14 | \item{strict}{Logical specifying if interactions terms should be included (\code{FALSE}) or just the main terms (\code{TRUE}).} 15 | 16 | \item{\dots}{further arguments} 17 | } 18 | \value{ 19 | A character vector of coefficients listing the coefficients that match the predictor. As of now interactions cannot be explicitly specified. 20 | } 21 | \description{ 22 | Function that does the work for Revo models for matching coefficients with predictors 23 | } 24 | \details{ 25 | The user specifies predictors whose coefficients should be included in the coefplot. 26 | } 27 | \author{ 28 | Jared P. Lander 29 | } 30 | -------------------------------------------------------------------------------- /man/invlogit.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/transformations.r 3 | \name{invlogit} 4 | \alias{invlogit} 5 | \title{invlogit} 6 | \usage{ 7 | invlogit(x) 8 | } 9 | \arguments{ 10 | \item{x}{Vector of numbers} 11 | } 12 | \value{ 13 | \code{x} mapped to [0, 1] 14 | } 15 | \description{ 16 | Calculates the inverse logit 17 | } 18 | \details{ 19 | Maps the real line to [0, 1] 20 | } 21 | \examples{ 22 | invlogit(3) 23 | invlogit(-6:6) 24 | invlogit(c(-1, 1, 2)) 25 | 26 | } 27 | \author{ 28 | Jared P. Lander 29 | } 30 | -------------------------------------------------------------------------------- /man/matchCoefs.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/coefPredMatching.r 3 | \name{matchCoefs} 4 | \alias{matchCoefs} 5 | \title{matchCoefs} 6 | \usage{ 7 | matchCoefs(model, ...) 8 | } 9 | \arguments{ 10 | \item{model}{Fitted model} 11 | 12 | \item{\dots}{Further arguments} 13 | } 14 | \value{ 15 | a data.frame matching predictors to coefficients 16 | } 17 | \description{ 18 | Match coefficients to predictors 19 | } 20 | \details{ 21 | Matches coefficients to predictors using information from model matrices 22 | } 23 | \examples{ 24 | \dontrun{ 25 | require(reshape2) 26 | require(plyr) 27 | data("tips", package="reshape2") 28 | mod1 <- lm(tip ~ total_bill * sex + day, tips) 29 | mod2 <- lm(tip ~ total_bill * sex + day - 1, tips) 30 | mod3 <- glm(tip ~ total_bill * sex + day, tips, family=gaussian(link="identity")) 31 | mod4 <- lm(tip ~ (total_bill + sex + day)^3, tips) 32 | mod5 <- lm(tip ~ total_bill * sex + day + I(total_bill^2), tips) 33 | coefplot:::matchCoefs(mod1) 34 | coefplot:::matchCoefs(mod2) 35 | coefplot:::matchCoefs(mod3) 36 | coefplot:::matchCoefs(mod4) 37 | coefplot:::matchCoefs(mod5) 38 | } 39 | } 40 | \author{ 41 | Jared P. Lander 42 | } 43 | -------------------------------------------------------------------------------- /man/matchCoefs.default.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/coefPredMatching.r 3 | \name{matchCoefs.default} 4 | \alias{matchCoefs.default} 5 | \title{matchCoefs.default} 6 | \usage{ 7 | \method{matchCoefs}{default}(model, ...) 8 | } 9 | \arguments{ 10 | \item{model}{Fitted model} 11 | 12 | \item{\dots}{Further arguments} 13 | } 14 | \value{ 15 | a data.frame matching predictors to coefficients 16 | } 17 | \description{ 18 | Match coefficients to predictors 19 | } 20 | \details{ 21 | Matches coefficients to predictors using information from model matrices 22 | } 23 | \author{ 24 | Jared P. Lander 25 | } 26 | -------------------------------------------------------------------------------- /man/multiplot.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/multiplot.r 3 | \name{multiplot} 4 | \alias{multiplot} 5 | \title{Plot multiple coefplots} 6 | \usage{ 7 | multiplot( 8 | ..., 9 | title = "Coefficient Plot", 10 | xlab = "Value", 11 | ylab = "Coefficient", 12 | innerCI = 1, 13 | outerCI = 2, 14 | lwdInner = 1, 15 | lwdOuter = (Sys.info()["sysname"] != "Windows") * 0.5, 16 | pointSize = 3, 17 | dodgeHeight = 1, 18 | color = "blue", 19 | shape = 16, 20 | linetype = 1, 21 | cex = 0.8, 22 | textAngle = 0, 23 | numberAngle = 90, 24 | zeroColor = "grey", 25 | zeroLWD = 1, 26 | zeroType = 2, 27 | single = TRUE, 28 | scales = "fixed", 29 | ncol = length(unique(modelCI$Model)), 30 | sort = c("natural", "normal", "magnitude", "size", "alphabetical"), 31 | decreasing = FALSE, 32 | names = NULL, 33 | numeric = FALSE, 34 | fillColor = "grey", 35 | alpha = 1/2, 36 | horizontal = FALSE, 37 | factors = NULL, 38 | only = NULL, 39 | shorten = TRUE, 40 | intercept = TRUE, 41 | interceptName = "(Intercept)", 42 | coefficients = NULL, 43 | predictors = NULL, 44 | strict = FALSE, 45 | newNames = NULL, 46 | plot = TRUE, 47 | drop = FALSE, 48 | by = c("Coefficient", "Model"), 49 | plot.shapes = FALSE, 50 | plot.linetypes = FALSE, 51 | legend.position = c("bottom", "right", "left", "top", "none"), 52 | secret.weapon = FALSE, 53 | legend.reverse = FALSE, 54 | trans = identity 55 | ) 56 | } 57 | \arguments{ 58 | \item{\dots}{Models to be plotted} 59 | 60 | \item{title}{The name of the plot, if NULL then no name is given} 61 | 62 | \item{xlab}{The x label} 63 | 64 | \item{ylab}{The y label} 65 | 66 | \item{innerCI}{How wide the inner confidence interval should be, normally 1 standard deviation. If 0, then there will be no inner confidence interval.} 67 | 68 | \item{outerCI}{How wide the outer confidence interval should be, normally 2 standard deviations. If 0, then there will be no outer confidence interval.} 69 | 70 | \item{lwdInner}{The thickness of the inner confidence interval} 71 | 72 | \item{lwdOuter}{The thickness of the outer confidence interval} 73 | 74 | \item{pointSize}{Size of coefficient point} 75 | 76 | \item{dodgeHeight}{Amount of vertical dodging} 77 | 78 | \item{color}{The color of the points and lines} 79 | 80 | \item{shape}{The shape of the points} 81 | 82 | \item{linetype}{The type of line drawn for the standard errors} 83 | 84 | \item{cex}{The text size multiplier, currently not used} 85 | 86 | \item{textAngle}{The angle for the coefficient labels, 0 is horizontal} 87 | 88 | \item{numberAngle}{The angle for the value labels, 0 is horizontal} 89 | 90 | \item{zeroColor}{The color of the line indicating 0} 91 | 92 | \item{zeroLWD}{The thickness of the 0 line} 93 | 94 | \item{zeroType}{The type of 0 line, 0 will mean no line} 95 | 96 | \item{single}{logical; If TRUE there will be one plot with the points and bars stacked, otherwise the models will be displayed in separate facets} 97 | 98 | \item{scales}{The way the axes should be treated in a faceted plot. Can be c("fixed", "free", "free_x", "free_y")} 99 | 100 | \item{ncol}{The number of columns that the models should be plotted in} 101 | 102 | \item{sort}{Determines the sort order of the coefficients. Possible values are c("natural", "magnitude", "alphabetical")} 103 | 104 | \item{decreasing}{logical; Whether the coefficients should be ascending or descending} 105 | 106 | \item{names}{Names for models, if NULL then they will be named after their inputs} 107 | 108 | \item{numeric}{logical; If true and factors has exactly one value, then it is displayed in a horizontal graph with continuous confidence bounds.} 109 | 110 | \item{fillColor}{The color of the confidence bounds for a numeric factor} 111 | 112 | \item{alpha}{The transparency level of the numeric factor's confidence bound} 113 | 114 | \item{horizontal}{logical; If the plot should be displayed horizontally} 115 | 116 | \item{factors}{Vector of factor variables that will be the only ones shown} 117 | 118 | \item{only}{logical; If factors has a value this determines how interactions are treated. True means just that variable will be shown and not its interactions. False means interactions will be included.} 119 | 120 | \item{shorten}{logical or character; If \code{FALSE} then coefficients for factor levels will include their variable name. If \code{TRUE} coefficients for factor levels will be stripped of their variable names. If a character vector of variables only coefficients for factor levels associated with those variables will the variable names stripped.} 121 | 122 | \item{intercept}{logical; Whether the Intercept coefficient should be plotted} 123 | 124 | \item{interceptName}{Specifies name of intercept it case it is not the default of "(Intercept").} 125 | 126 | \item{coefficients}{A character vector specifying which factor coefficients to keep. It will keep all levels and any interactions, even if those are not listed.} 127 | 128 | \item{predictors}{A character vector specifying which coefficients to keep. Each individual coefficient can be specified. Use predictors to specify entire factors} 129 | 130 | \item{strict}{If TRUE then predictors will only be matched to its own coefficients, not its interactions} 131 | 132 | \item{newNames}{Named character vector of new names for coefficients} 133 | 134 | \item{plot}{logical; If the plot should be drawn, if false then a data.frame of the values will be returned} 135 | 136 | \item{drop}{logical; if TRUE then models without valid coefficients to show will not be plotted} 137 | 138 | \item{by}{If "Coefficient" then a normal multiplot is plotted, if "Model" then the coefficients are plotted along the axis with one for each model. If plotting by model only one coefficient at a time can be selected. This is called the secret weapon by Andy Gelman.} 139 | 140 | \item{plot.shapes}{If \code{TRUE} points will have different shapes for different models} 141 | 142 | \item{plot.linetypes}{If \code{TRUE} lines will have different shapes for different models} 143 | 144 | \item{legend.position}{position of legend, one of "left", "right", "bottom", "top", "none"} 145 | 146 | \item{secret.weapon}{If this is \code{TRUE} and exactly one coefficient is listed in coefficients then Andy Gelman's secret weapon is plotted.} 147 | 148 | \item{legend.reverse}{Setting to reverse the legend in a multiplot so that it matches the order they are drawn in the plot} 149 | 150 | \item{trans}{A transformation function to apply to the values and confidence intervals. \code{identity} by default. Use \code{invlogit} for binary regression.} 151 | } 152 | \value{ 153 | A ggplot object 154 | } 155 | \description{ 156 | Plot the coefficients from multiple models 157 | } 158 | \details{ 159 | Plots a graph similar to \code{\link{coefplot}} but for multiple plots at once. 160 | 161 | For now, if \code{names} is provided the plots will appear in alphabetical order of the names. This will be adjusted in future iterations. When setting \code{by} to "Model" and specifying exactly one variable in \code{variables} that one coefficient will be plotted repeatedly with the axis labeled by model. This is Andy Gelman's secret weapon. 162 | } 163 | \examples{ 164 | 165 | data(diamonds) 166 | model1 <- lm(price ~ carat + cut, data=diamonds) 167 | model2 <- lm(price ~ carat + cut + color, data=diamonds) 168 | model3 <- lm(price ~ carat + color, data=diamonds) 169 | multiplot(model1, model2, model3) 170 | multiplot(model1, model2, model3, single=FALSE) 171 | multiplot(model1, model2, model3, plot=FALSE) 172 | require(reshape2) 173 | data(tips, package="reshape2") 174 | mod1 <- lm(tip ~ total_bill + sex, data=tips) 175 | mod2 <- lm(tip ~ total_bill * sex, data=tips) 176 | mod3 <- lm(tip ~ total_bill * sex * day, data=tips) 177 | mod7 <- lm(tip ~ total_bill + day + time, data=tips) 178 | multiplot(mod1, mod2, mod3, mod7, single=FALSE, scales="free_x") 179 | multiplot(mod1, mod2, mod3, mod7, single=FALSE, scales="free_x") 180 | multiplot(mod1, mod2, mod3, mod7, single=FALSE, scales="free_x", plot.shapes=TRUE) 181 | multiplot(mod1, mod2, mod3, mod7, single=TRUE, scales="free_x", 182 | plot.shapes=TRUE, plot.linetypes=TRUE) 183 | multiplot(mod1, mod2, mod3, mod7, single=TRUE, scales="free_x", 184 | plot.shapes=FALSE, plot.linetypes=TRUE, legend.position="bottom") 185 | # the secret weapon 186 | multiplot(mod1, mod2, mod3, mod7, coefficients="total_bill", secret.weapon=TRUE) 187 | # horizontal secret weapon 188 | multiplot(mod1, mod2, mod3, mod7, coefficients="total_bill", by="Model", horizontal=FALSE) 189 | 190 | 191 | } 192 | \seealso{ 193 | \code{link{coefplot}} 194 | } 195 | -------------------------------------------------------------------------------- /man/position_dodgev.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/position.r 3 | \name{position_dodgev} 4 | \alias{position_dodgev} 5 | \title{Adjust position by dodging overlaps to the side.} 6 | \usage{ 7 | position_dodgev(height = NULL) 8 | } 9 | \arguments{ 10 | \item{height}{Dodging height, when different to the height of the individual 11 | elements. This is useful when you want to align narrow geoms with wider 12 | geoms. See the examples for a use case.} 13 | } 14 | \description{ 15 | Adjust position by dodging overlaps to the side. 16 | } 17 | \examples{ 18 | ggplot(mtcars, aes(factor(cyl), fill = factor(vs))) + 19 | geom_bar(position = "dodge") 20 | 21 | ggplot(diamonds, aes(price, fill = cut)) + 22 | geom_histogram(position="dodge") 23 | # see ?geom_boxplot and ?geom_bar for more examples 24 | 25 | # To dodge items with different heights, you need to be explicit 26 | df <- data.frame(x=c("a","a","b","b"), y=2:5, g = rep(1:2, 2)) 27 | p <- ggplot(df, aes(x, y, group = g)) + 28 | geom_bar( 29 | stat = "identity", position = "dodge", 30 | fill = "grey50", colour = "black" 31 | ) 32 | p 33 | 34 | # A line range has no height: 35 | p + geom_linerange(aes(ymin = y-1, ymax = y+1), position = "dodge") 36 | # You need to explicitly specify the height for dodging 37 | p + geom_linerange(aes(ymin = y-1, ymax = y+1), 38 | position = position_dodge(width = 0.9)) 39 | 40 | # Similarly with error bars: 41 | p + geom_errorbar(aes(ymin = y-1, ymax = y+1), width = 0.2, 42 | position = "dodge") 43 | p + geom_errorbar(aes(ymin = y-1, ymax = y+1, height = 0.2), 44 | position = position_dodge(width = 0.90)) 45 | 46 | } 47 | \concept{position adjustments} 48 | -------------------------------------------------------------------------------- /renv/.gitignore: -------------------------------------------------------------------------------- 1 | library/ 2 | local/ 3 | lock/ 4 | python/ 5 | staging/ 6 | -------------------------------------------------------------------------------- /tests/testthat.R: -------------------------------------------------------------------------------- 1 | library(testthat) 2 | library(coefplot) 3 | 4 | test_check("coefplot") 5 | -------------------------------------------------------------------------------- /tests/testthat/teardown-xyz.r: -------------------------------------------------------------------------------- 1 | unlink('TestModelXG.model') 2 | -------------------------------------------------------------------------------- /tests/testthat/test-Transformations.R: -------------------------------------------------------------------------------- 1 | context("Transformations return correct reults") 2 | 3 | test_that("invlogit returns numeric", { 4 | expect_is(invlogit(3), 'numeric') 5 | expect_is(invlogit(-6:6), 'numeric') 6 | }) 7 | 8 | test_that('invlogit generates correct numbers', { 9 | expect_equal(round(invlogit(3), 3), 0.953) 10 | expect_equal(invlogit(-6:6), 11 | c(0.002472623, 0.006692851, 0.017986210, 0.047425873, 0.119202922, 0.268941421, 12 | 0.500000000, 0.731058579, 0.880797078, 0.952574127, 0.982013790, 0.993307149, 13 | 0.997527377)) 14 | }) -------------------------------------------------------------------------------- /tests/testthat/test-buildModelCI.R: -------------------------------------------------------------------------------- 1 | context("buildModelCI builds the appropriate model") 2 | 3 | library(glmnet) 4 | library(xgboost) 5 | 6 | data(diamonds, package='ggplot2') 7 | 8 | model1 <- lm(price ~ carat + cut, data=diamonds) 9 | ci1 <- coefplot:::buildModelCI(model1) 10 | 11 | diaX <- useful::build.x(price ~ carat + cut + x, data=diamonds, contrasts=FALSE) 12 | diaY <- useful::build.y(price ~ carat + cut + x, data=diamonds) 13 | 14 | xg1 <- xgboost(data=diaX, label=diaY, 15 | booster='gblinear', 16 | objective='reg:squarederror', eval_metric='rmse', 17 | nrounds=50, 18 | verbose=FALSE, 19 | save_name='TestModelXG.model' 20 | ) 21 | 22 | xgCI <- coefplot:::buildModelCI(xg1) 23 | xgCI_named <- coefplot:::buildModelCI(xg1, feature_names=colnames(diaX)) 24 | 25 | modG1 <- glmnet(x=diaX, y=diaY) 26 | coefG1 <- coefplot:::buildModelCI(modG1) 27 | 28 | correctNames <- c('Value', 'Coefficient', 'HighInner', 'LowInner', 'HighOuter', 'LowOuter', 'Model') 29 | 30 | test_that('buildModelCI returns a data.frame', { 31 | expect_is(ci1, 'data.frame') 32 | expect_is(xgCI, 'tbl') 33 | expect_is(xgCI_named, 'tbl') 34 | expect_is(coefG1, 'data.frame') 35 | }) 36 | 37 | test_that('buildModelCI returns correct number and names of columns', { 38 | expect_length(ci1, 7) 39 | expect_length(xgCI, 7) 40 | expect_length(xgCI_named, 7) 41 | expect_length(coefG1, 7) 42 | 43 | expect_named(ci1) 44 | expect_named(xgCI) 45 | expect_named(xgCI_named) 46 | expect_named(coefG1) 47 | 48 | expect_equal(names(ci1), correctNames) 49 | expect_equal(names(xgCI), correctNames) 50 | expect_equal(names(xgCI_named), correctNames) 51 | expect_equal(names(coefG1), correctNames) 52 | }) 53 | 54 | 55 | test_that('buildModelCI returns correct number of rows', { 56 | expect_equal(nrow(ci1), 6) 57 | expect_equal(nrow(xgCI), 8) 58 | expect_equal(nrow(xgCI_named), 8) 59 | expect_equal(nrow(coefG1), 5) 60 | }) 61 | 62 | -------------------------------------------------------------------------------- /tests/testthat/test-coefpath.R: -------------------------------------------------------------------------------- 1 | context("Coefficient path plotting") 2 | 3 | library(glmnet) 4 | library(ggplot2) 5 | library(useful) 6 | data(diamonds) 7 | diaX <- useful::build.x(price ~ carat + cut + x - 1, data=diamonds, contrasts = TRUE) 8 | diaY <- useful::build.y(price ~ carat + cut + x - 1, data=diamonds) 9 | modG1 <- glmnet(x=diaX, y=diaY) 10 | coefpath(modG1) 11 | 12 | modG2 <- cv.glmnet(x=diaX, y=diaY, nfolds=5) 13 | coefpath(modG2) 14 | 15 | x <- matrix(rnorm(100*20),100,20) 16 | y <- rnorm(100) 17 | fit1 <- glmnet(x, y) 18 | coefpath(fit1) 19 | 20 | test_that('dygraph objects are returned', { 21 | expect_is(coefpath(modG1), 'dygraphs') 22 | expect_is(coefpath(modG2), 'dygraphs') 23 | expect_is(coefpath(fit1), 'dygraphs') 24 | 25 | expect_is(coefpath(modG1, annotate=FALSE), 'dygraphs') 26 | expect_is(coefpath(modG2, annotate=FALSE), 'dygraphs') 27 | expect_is(coefpath(fit1, annotate=FALSE), 'dygraphs') 28 | }) 29 | -------------------------------------------------------------------------------- /tests/testthat/test-extract-coef-maxLik.r: -------------------------------------------------------------------------------- 1 | context("Extracting Coefficients From maxLik Models") 2 | 3 | runThis <- suppressWarnings(suppressPackageStartupMessages(require(maxLik))) 4 | 5 | if(runThis) 6 | { 7 | ## ML estimation of exponential duration model: 8 | t <- rexp(100, 2) 9 | 10 | loglik <- function(theta) log(theta) - theta*t 11 | gradlik <- function(theta) 1/theta - t 12 | hesslik <- function(theta) -100/theta^2 13 | 14 | ## Estimate with numeric gradient and hessian 15 | a <- maxLik(loglik, start=1) 16 | 17 | ## Estimate with analytic gradient and hessian 18 | b <- maxLik(loglik, gradlik, hesslik, start=1) 19 | 20 | ## Next, we give an example with vector argument: Estimate the mean and 21 | ## variance of a random normal sample by maximum likelihood 22 | ## 23 | loglik <- function(param) 24 | { 25 | mu <- param[1] 26 | sigma <- param[2] 27 | ll <- -0.5*N*log(2*pi) - N*log(sigma) - sum(0.5*(x - mu)^2/sigma^2) 28 | ll 29 | } 30 | 31 | x <- rnorm(1000, 1, 2) # use mean=1, stdd=2 32 | N <- length(x) 33 | 34 | res <- maxLik(loglik, start=c(0,1)) # use 'wrong' start values 35 | 36 | maxLikA <- extract.coef.maxLik(a) 37 | maxLikB <- extract.coef.maxLik(b) 38 | maxLikRes <- extract.coef.maxLik(res) 39 | 40 | maxLikAGeneric <- extract.coef(a) 41 | maxLikBGeneric <- extract.coef(b) 42 | maxLikResGeneric <- extract.coef(res) 43 | } 44 | 45 | test_that("extract.coef.maxLik has correct dimensions", { 46 | skip_if_not_installed('maxLik') 47 | 48 | expect_equal(dim(maxLikA), c(1, 3)) 49 | expect_equal(dim(maxLikB), c(1, 3)) 50 | expect_equal(dim(maxLikRes), c(2, 3)) 51 | }) 52 | 53 | test_that("extract.coef has correct dimensions", { 54 | skip_if_not_installed('maxLik') 55 | 56 | expect_equal(dim(maxLikAGeneric), c(1, 3)) 57 | expect_equal(dim(maxLikBGeneric), c(1, 3)) 58 | expect_equal(dim(maxLikResGeneric), c(2, 3)) 59 | }) 60 | 61 | test_that("extract.coef.maxLik has proper types", { 62 | skip_if_not_installed('maxLik') 63 | 64 | expect_equal(sapply(maxLikA, class), c(Value="numeric", SE="numeric", Coefficient="integer")) 65 | expect_equal(sapply(maxLikB, class), c(Value="numeric", SE="numeric", Coefficient="integer")) 66 | expect_equal(sapply(maxLikRes, class), c(Value="numeric", SE="numeric", Coefficient="integer")) 67 | }) 68 | 69 | test_that("extract.coef has proper types", { 70 | skip_if_not_installed('maxLik') 71 | 72 | expect_equal(sapply(maxLikAGeneric, class), c(Value="numeric", SE="numeric", Coefficient="integer")) 73 | expect_equal(sapply(maxLikBGeneric, class), c(Value="numeric", SE="numeric", Coefficient="integer")) 74 | expect_equal(sapply(maxLikResGeneric, class), c(Value="numeric", SE="numeric", Coefficient="integer")) 75 | }) -------------------------------------------------------------------------------- /tests/testthat/test-extract-coefs.r: -------------------------------------------------------------------------------- 1 | context("Extracting Coefficients") 2 | 3 | # we need data 4 | data("tips", package="reshape2") 5 | mod1 <- lm(tip ~ total_bill, data=tips) 6 | mod2 <- lm(tip ~ total_bill + sex, data=tips) 7 | mod3 <- lm(tip ~ total_bill + sex + smoker, data=tips) 8 | mod4 <- lm(tip ~ total_bill + sex*smoker, data=tips) 9 | mod5 <- lm(tip ~ total_bill + sex:smoker, data=tips) 10 | mod6 <- lm(tip ~ total_bill*sex, data=tips) 11 | mod7 <- lm(tip ~ sex*smoker, data=tips) 12 | 13 | coef1 <- extract.coef(mod1) 14 | coef2 <- extract.coef(mod2) 15 | coef3 <- extract.coef(mod3) 16 | coef4 <- extract.coef(mod4) 17 | coef5 <- extract.coef(mod5) 18 | coef6 <- extract.coef(mod6) 19 | coef7 <- extract.coef(mod7) 20 | 21 | tips$Threshold <- tips$tip >= 4.5 22 | modG1 <- glm(Threshold ~ total_bill, data=tips, family=binomial(link="logit")) 23 | modG2 <- glm(Threshold ~ total_bill + sex, data=tips, family=binomial(link="logit")) 24 | modG3 <- glm(Threshold ~ total_bill + sex + smoker, data=tips, family=binomial(link="logit")) 25 | modG4 <- glm(Threshold ~ total_bill + sex*smoker, data=tips, family=binomial(link="logit")) 26 | modG5 <- glm(Threshold ~ total_bill + sex:smoker, data=tips, family=binomial(link="logit")) 27 | modG6 <- glm(Threshold ~ total_bill*sex, data=tips, family=binomial(link="logit")) 28 | modG7 <- glm(Threshold ~ sex*smoker, data=tips, family=binomial(link="logit")) 29 | 30 | coefG1 <- extract.coef(modG1) 31 | coefG2 <- extract.coef(modG2) 32 | coefG3 <- extract.coef(modG3) 33 | coefG4 <- extract.coef(modG4) 34 | coefG5 <- extract.coef(modG5) 35 | coefG6 <- extract.coef(modG6) 36 | coefG7 <- extract.coef(modG7) 37 | 38 | test_that("Coefficients come as data.frames", { 39 | expect_is(coef1, "data.frame") 40 | expect_is(coef2, "data.frame") 41 | expect_is(coef3, "data.frame") 42 | expect_is(coef4, "data.frame") 43 | expect_is(coef5, "data.frame") 44 | expect_is(coef6, "data.frame") 45 | expect_is(coef7, "data.frame") 46 | 47 | expect_is(coefG1, "data.frame") 48 | expect_is(coefG2, "data.frame") 49 | expect_is(coefG3, "data.frame") 50 | expect_is(coefG4, "data.frame") 51 | expect_is(coefG5, "data.frame") 52 | expect_is(coefG6, "data.frame") 53 | expect_is(coefG7, "data.frame") 54 | }) 55 | 56 | test_that("Coefficients have proper dimenions", { 57 | expect_equal(dim(coef1), c(2, 3)) 58 | expect_equal(dim(coef2), c(3, 3)) 59 | expect_equal(dim(coef3), c(4, 3)) 60 | expect_equal(dim(coef4), c(5, 3)) 61 | expect_equal(dim(coef5), c(5, 3)) 62 | expect_equal(dim(coef6), c(4, 3)) 63 | expect_equal(dim(coef7), c(4, 3)) 64 | 65 | expect_equal(dim(coefG1), c(2, 3)) 66 | expect_equal(dim(coefG2), c(3, 3)) 67 | expect_equal(dim(coefG3), c(4, 3)) 68 | expect_equal(dim(coefG4), c(5, 3)) 69 | expect_equal(dim(coefG5), c(5, 3)) 70 | expect_equal(dim(coefG6), c(4, 3)) 71 | expect_equal(dim(coefG7), c(4, 3)) 72 | }) 73 | 74 | test_that("Coefficients have proper types", { 75 | expect_equal(sapply(coef1, class), c(Value="numeric", SE="numeric", Coefficient="character")) 76 | expect_equal(sapply(coef2, class), c(Value="numeric", SE="numeric", Coefficient="character")) 77 | expect_equal(sapply(coef3, class), c(Value="numeric", SE="numeric", Coefficient="character")) 78 | expect_equal(sapply(coef4, class), c(Value="numeric", SE="numeric", Coefficient="character")) 79 | expect_equal(sapply(coef5, class), c(Value="numeric", SE="numeric", Coefficient="character")) 80 | expect_equal(sapply(coef6, class), c(Value="numeric", SE="numeric", Coefficient="character")) 81 | expect_equal(sapply(coef7, class), c(Value="numeric", SE="numeric", Coefficient="character")) 82 | 83 | expect_equal(sapply(coefG1, class), c(Value="numeric", SE="numeric", Coefficient="character")) 84 | expect_equal(sapply(coefG2, class), c(Value="numeric", SE="numeric", Coefficient="character")) 85 | expect_equal(sapply(coefG3, class), c(Value="numeric", SE="numeric", Coefficient="character")) 86 | expect_equal(sapply(coefG4, class), c(Value="numeric", SE="numeric", Coefficient="character")) 87 | expect_equal(sapply(coefG5, class), c(Value="numeric", SE="numeric", Coefficient="character")) 88 | expect_equal(sapply(coefG6, class), c(Value="numeric", SE="numeric", Coefficient="character")) 89 | expect_equal(sapply(coefG7, class), c(Value="numeric", SE="numeric", Coefficient="character")) 90 | }) 91 | 92 | expectedCoef1 <- structure(list(Value = c(0.920269613554671, 0.105024517384353), 93 | SE = c(0.159734746376432, 0.0073647898487626), 94 | Coefficient = c("(Intercept)", "total_bill")), 95 | .Names = c("Value", "SE", "Coefficient"), 96 | row.names = c("(Intercept)", "total_bill"), 97 | class = "data.frame") 98 | 99 | expectedCoef2 <- structure(list(Value = c(0.933278494035796, 0.105232356866155, -0.0266087137098722), 100 | SE = c(0.173755748089091, 0.00745817378218747, 0.138333951666825), 101 | Coefficient = c("(Intercept)", "total_bill", "sexMale")), 102 | .Names = c("Value", "SE", "Coefficient"), 103 | row.names = c("(Intercept)", "total_bill", "sexMale"), 104 | class = "data.frame") 105 | 106 | expectedCoef3 <- structure(list(Value = c(0.97703525070222, 0.105943079358779, -0.0280925699231267, -0.14919234332629), 107 | SE = c(0.178163334196653, 0.00748273981231774, 0.13827928730744, 0.135435290301022), 108 | Coefficient = c("(Intercept)", "total_bill", "sexMale", "smokerYes")), 109 | .Names = c("Value", "SE", "Coefficient"), 110 | row.names = c("(Intercept)", "total_bill", "sexMale", "smokerYes"), 111 | class = "data.frame") 112 | 113 | expectedCoef4 <- structure(list(Value = c(0.838665697721798, 0.106867331154389, 0.159699673580903, 0.171601527915647, -0.50028527366166), 114 | SE = c(0.193672618884891, 0.00746732438703733, 0.173479347933277, 0.225167097638273, 0.28123965022277), 115 | Coefficient = c("(Intercept)", "total_bill", "sexMale", "smokerYes", "sexMale:smokerYes")), 116 | .Names = c("Value", "SE", "Coefficient"), 117 | row.names = c("(Intercept)", "total_bill", "sexMale", "smokerYes", "sexMale:smokerYes"), 118 | class = "data.frame") 119 | 120 | expectedCoef5 <- structure(list(Value = c(0.669681625556689, 0.106867331154389, 0.168984072165109, 0.328683745746012, 0.340585600080757), 121 | SE = c(0.212128542982023, 0.00746732438703733, 0.193681264106416, 0.168404634825236, 0.223182521333619), 122 | Coefficient = c("(Intercept)", "total_bill", "sexFemale:smokerNo", "sexMale:smokerNo", "sexFemale:smokerYes")), 123 | .Names = c("Value", "SE", "Coefficient"), 124 | row.names = c("(Intercept)", "total_bill", "sexFemale:smokerNo", "sexMale:smokerNo", "sexFemale:smokerYes"), 125 | class = "data.frame") 126 | 127 | expectedCoef6 <- structure(list(Value = c(1.0480199036793, 0.0988779199719281, -0.195872209747046, 0.0089827576377142), 128 | SE = c(0.272497600665525, 0.0138079864596611, 0.338953639877964, 0.0164171081078769), 129 | Coefficient = c("(Intercept)", "total_bill", "sexMale", "total_bill:sexMale")), 130 | .Names = c("Value", "SE", "Coefficient"), 131 | row.names = c("(Intercept)", "total_bill", "sexMale", "total_bill:sexMale"), 132 | class = "data.frame") 133 | 134 | expectedCoef7 <- structure(list(Value = c(2.77351851851852, 0.339883543337153, 0.157996632996634, -0.220232028185638), 135 | SE = c(0.188579004667271, 0.235285923694171, 0.306193520066471, 0.381520292408129), 136 | Coefficient = c("(Intercept)", "sexMale", "smokerYes", "sexMale:smokerYes")), 137 | .Names = c("Value", "SE", "Coefficient"), 138 | row.names = c("(Intercept)", "sexMale", "smokerYes", "sexMale:smokerYes"), 139 | class = "data.frame") 140 | 141 | expectedCoefG1 <- structure(list(Value = c(-6.20405198369694, 0.178746135713105), 142 | SE = c(0.787846856153379, 0.0280564850923222), 143 | Coefficient = c("(Intercept)", "total_bill")), 144 | .Names = c("Value", "SE", "Coefficient"), 145 | row.names = c("(Intercept)", "total_bill"), 146 | class = "data.frame") 147 | 148 | expectedCoefG2 <- structure(list(Value = c(-6.16250676539459, 0.179197631542195, -0.0778168380625508), 149 | SE = c(0.830682665606069, 0.0282263860677389, 0.510653903330164), 150 | Coefficient = c("(Intercept)", "total_bill", "sexMale")), 151 | .Names = c("Value", "SE", "Coefficient"), 152 | row.names = c("(Intercept)", "total_bill", "sexMale"), 153 | class = "data.frame") 154 | 155 | expectedCoefG3 <- structure(list(Value = c(-6.28879908755451, 0.19791038354303, 0.0425603970575011, -1.26080742323577), 156 | SE = c(0.887343464175678, 0.0313202875695088, 0.525802585221906, 0.568477363122317), 157 | Coefficient = c("(Intercept)", "total_bill", "sexMale", "smokerYes")), 158 | .Names = c("Value", "SE", "Coefficient"), 159 | row.names = c("(Intercept)", "total_bill", "sexMale", "smokerYes"), 160 | class = "data.frame") 161 | 162 | expectedCoefG4 <- structure(list(Value = c(-6.50722534287237, 0.200398977461892, 0.277416562038954, -0.641536473204469, -0.871196107806703), 163 | SE = c(0.957583876450863, 0.0319532718817944, 0.622811036359676, 0.980442295268331, 1.17344558101382), 164 | Coefficient = c("(Intercept)", "total_bill", "sexMale", "smokerYes", "sexMale:smokerYes")), 165 | .Names = c("Value", "SE", "Coefficient"), 166 | row.names = c("(Intercept)", "total_bill", "sexMale", "smokerYes", "sexMale:smokerYes"), 167 | class = "data.frame") 168 | 169 | expectedCoefG5 <- structure(list(Value = c(-7.74254136184459, 0.200398977461892, 1.23531601897222, 1.51273258101117, 0.593779545767748), 170 | SE = c(1.15628637706478, 0.0319532718817944, 0.759703572779889, 0.673162985532241, 0.989243572422861), 171 | Coefficient = c("(Intercept)", "total_bill", "sexFemale:smokerNo", "sexMale:smokerNo", "sexFemale:smokerYes")), 172 | .Names = c("Value", "SE", "Coefficient"), 173 | row.names = c("(Intercept)", "total_bill", "sexFemale:smokerNo", "sexMale:smokerNo", "sexFemale:smokerYes"), 174 | class = "data.frame") 175 | 176 | expectedCoefG6 <- structure(list(Value = c(-9.02683319336376, 0.288225436284044, 3.54244163292209, -0.136784802449348), 177 | SE = c(2.23820089584356, 0.0808437446613046, 2.39386567363978, 0.086162375938673), 178 | Coefficient = c("(Intercept)", "total_bill", "sexMale", "total_bill:sexMale")), 179 | .Names = c("Value", "SE", "Coefficient"), 180 | row.names = c("(Intercept)", "total_bill", "sexMale", "total_bill:sexMale"), 181 | class = "data.frame") 182 | 183 | expectedCoefG7 <- structure(list(Value = c(-2.07944154018219, 0.457581107749538, -0.223143450341104, -0.179377881333291), 184 | SE = c(0.433002251934947, 0.512188079894551, 0.744316797288887, 0.889142445029405), 185 | Coefficient = c("(Intercept)", "sexMale", "smokerYes", "sexMale:smokerYes")), 186 | .Names = c("Value", "SE", "Coefficient"), 187 | row.names = c("(Intercept)", "sexMale", "smokerYes", "sexMale:smokerYes"), 188 | class = "data.frame") 189 | 190 | test_that("Data comes out as expected", { 191 | expect_equivalent(coef1, expectedCoef1) 192 | expect_equivalent(coef2, expectedCoef2) 193 | expect_equivalent(coef3, expectedCoef3) 194 | expect_equivalent(coef4, expectedCoef4) 195 | expect_equivalent(coef5, expectedCoef5) 196 | expect_equivalent(coef6, expectedCoef6) 197 | expect_equivalent(coef7, expectedCoef7) 198 | 199 | expect_equivalent(coefG1, expectedCoefG1) 200 | expect_equivalent(coefG2, expectedCoefG2) 201 | expect_equivalent(coefG3, expectedCoefG3) 202 | expect_equivalent(coefG4, expectedCoefG4) 203 | expect_equivalent(coefG5, expectedCoefG5) 204 | expect_equivalent(coefG6, expectedCoefG6) 205 | expect_equivalent(coefG7, expectedCoefG7) 206 | }) 207 | -------------------------------------------------------------------------------- /tests/testthat/test-extract.coef.glmnet.R: -------------------------------------------------------------------------------- 1 | context("Extracting coefs from glmnet models") 2 | 3 | library(xgboost) 4 | diaX <- useful::build.x(price ~ carat + cut + x - 1, data=diamonds, contrasts=FALSE) 5 | diaY <- useful::build.y(price ~ carat + cut + x - 1, data=diamonds) 6 | 7 | modG1 <- glmnet(x=diaX, y=diaY) 8 | coef1 <- extract.coef(modG1) 9 | 10 | test_that("The correct class is returned", { 11 | expect_is(coef1, 'data.frame') 12 | }) 13 | 14 | test_that("The correct number and names of columns are returned", { 15 | expect_named(coef1) 16 | 17 | expect_length(coef1, 3) 18 | 19 | expect_equal(names(coef1), c('Value', 'SE', 'Coefficient')) 20 | }) 21 | 22 | # this depends on the selected variables so need a different way to test 23 | # test_that("The correct number of rows are returned", { 24 | # expect_equal(nrow(coef1), ncol(diaX)) 25 | # }) 26 | 27 | -------------------------------------------------------------------------------- /tests/testthat/test-extract.coef.xgb.Booster.R: -------------------------------------------------------------------------------- 1 | context("Extracting coefs from xgboost models") 2 | 3 | library(xgboost) 4 | diaX <- useful::build.x(price ~ carat + cut + x, data=diamonds, contrasts=FALSE) 5 | diaY <- useful::build.y(price ~ carat + cut + x, data=diamonds) 6 | 7 | xg1 <- xgboost(data=diaX, label=diaY, 8 | booster='gblinear', 9 | objective='reg:squarederror', eval_metric='rmse', 10 | nrounds=50, 11 | verbose=FALSE, 12 | save_name='TestModelXG.model' 13 | ) 14 | 15 | xgCoef <- extract.coef(xg1) 16 | xgCoef_named <- extract.coef(xg1, feature_names=colnames(diaX)) 17 | xgCoef_named_all <- extract.coef(xg1, feature_names=colnames(diaX), removeNonSelected=FALSE) 18 | 19 | xg2 <- xgb.train(data=xgb.DMatrix(data=diaX, label=diaY), 20 | booster='gblinear', 21 | objective='reg:squarederror', eval_metric='rmse', 22 | alpha=65.65, 23 | nrounds=50, 24 | verbose=FALSE, 25 | save_name='TestModelXG.model' 26 | ) 27 | 28 | xgCoef2.1 <- extract.coef(xg2) 29 | xgCoef_named2.1 <- extract.coef(xg2, feature_names=colnames(diaX)) 30 | xgCoef_named_all2.1 <- extract.coef(xg2, feature_names=colnames(diaX), 31 | removeNonSelected=FALSE) 32 | 33 | xgCoef2.2 <- extract.coef(xg2, zero_threshold=0) 34 | xgCoef_named2.2 <- extract.coef(xg2, feature_names=colnames(diaX), zero_threshold=0) 35 | xgCoef_named_all2.2 <- extract.coef(xg2, feature_names=colnames(diaX), 36 | removeNonSelected=FALSE, zero_threshold=0) 37 | 38 | test_that("The correct class is returned", { 39 | expect_is(xgCoef, 'tbl') 40 | expect_is(xgCoef_named, 'tbl') 41 | expect_is(xgCoef_named_all, 'tbl') 42 | 43 | expect_is(xgCoef2.1, 'tbl') 44 | expect_is(xgCoef_named2.1, 'tbl') 45 | expect_is(xgCoef_named_all2.1, 'tbl') 46 | 47 | expect_is(xgCoef2.2, 'tbl') 48 | expect_is(xgCoef_named2.2, 'tbl') 49 | expect_is(xgCoef_named_all2.2, 'tbl') 50 | }) 51 | 52 | test_that("The correct number and names of columns are returned", { 53 | expect_named(xgCoef) 54 | expect_named(xgCoef_named) 55 | expect_named(xgCoef_named_all) 56 | 57 | expect_length(xgCoef, 3) 58 | expect_length(xgCoef_named, 3) 59 | expect_length(xgCoef_named_all, 3) 60 | 61 | expect_equal(names(xgCoef), c('Value', 'SE', 'Coefficient')) 62 | expect_equal(names(xgCoef_named), c('Value', 'SE', 'Coefficient')) 63 | expect_equal(names(xgCoef_named_all), c('Value', 'SE', 'Coefficient')) 64 | 65 | #################### 66 | # 2.1 67 | expect_named(xgCoef2.1) 68 | expect_named(xgCoef_named2.1) 69 | expect_named(xgCoef_named_all2.1) 70 | 71 | expect_length(xgCoef2.1, 3) 72 | expect_length(xgCoef_named2.1, 3) 73 | expect_length(xgCoef_named_all2.1, 3) 74 | 75 | expect_equal(names(xgCoef2.1), c('Value', 'SE', 'Coefficient')) 76 | expect_equal(names(xgCoef_named2.1), c('Value', 'SE', 'Coefficient')) 77 | expect_equal(names(xgCoef_named_all2.1), c('Value', 'SE', 'Coefficient')) 78 | 79 | ################### 80 | # 2.2 81 | expect_named(xgCoef2.2) 82 | expect_named(xgCoef_named2.2) 83 | expect_named(xgCoef_named_all2.2) 84 | 85 | expect_length(xgCoef2.2, 3) 86 | expect_length(xgCoef_named2.2, 3) 87 | expect_length(xgCoef_named_all2.2, 3) 88 | 89 | expect_equal(names(xgCoef2.2), c('Value', 'SE', 'Coefficient')) 90 | expect_equal(names(xgCoef_named2.2), c('Value', 'SE', 'Coefficient')) 91 | expect_equal(names(xgCoef_named_all2.2), c('Value', 'SE', 'Coefficient')) 92 | }) 93 | 94 | test_that("The correct number of rows are returned", { 95 | expect_equal(nrow(xgCoef), ncol(diaX)) 96 | expect_equal(nrow(xgCoef_named), ncol(diaX)) 97 | expect_equal(nrow(xgCoef_named_all), ncol(diaX)) 98 | 99 | ################ 100 | # 2.1 101 | expect_equal(nrow(xgCoef2.1), 4) 102 | expect_equal(nrow(xgCoef_named2.1), 4) 103 | expect_equal(nrow(xgCoef_named_all2.1), ncol(diaX)) 104 | 105 | ################ 106 | # 2.2 107 | expect_equal(nrow(xgCoef2.2), ncol(diaX)) 108 | expect_equal(nrow(xgCoef_named2.2), ncol(diaX)) 109 | expect_equal(nrow(xgCoef_named_all2.2), ncol(diaX)) 110 | }) 111 | 112 | test_that("The correct coefficient names are returned", { 113 | expect_equal(xgCoef$Coefficient, xg1$feature_names) 114 | expect_equal(xgCoef_named$Coefficient, colnames(diaX)) 115 | expect_equal(xgCoef_named_all$Coefficient, colnames(diaX)) 116 | 117 | expect_equal(xgCoef2.1$Coefficient, c("(Intercept)", "carat", "cutIdeal", "x")) 118 | expect_equal(xgCoef_named2.1$Coefficient, c("(Intercept)", "carat", "cutIdeal", "x")) 119 | expect_equal(xgCoef_named_all2.1$Coefficient, colnames(diaX)) 120 | 121 | expect_equal(xgCoef2.2$Coefficient, xg2$feature_names) 122 | expect_equal(xgCoef_named2.2$Coefficient, colnames(diaX)) 123 | expect_equal(xgCoef_named_all2.2$Coefficient, colnames(diaX)) 124 | }) 125 | 126 | unlink('TestModelXG.model') 127 | -------------------------------------------------------------------------------- /tests/testthat/test-extractPath.R: -------------------------------------------------------------------------------- 1 | context("Coefficient path extraction") 2 | 3 | library(glmnet) 4 | data(diamonds, package='ggplot2') 5 | diaX <- useful::build.x(price ~ carat + cut + x - 1, data=diamonds, contrasts = TRUE) 6 | diaY <- useful::build.y(price ~ carat + cut + x - 1, data=diamonds) 7 | modG1 <- glmnet(x=diaX, y=diaY) 8 | modG2 <- cv.glmnet(x=diaX, y=diaY, nfolds=5) 9 | 10 | extracted <- extractPath(modG1) 11 | extracted2 <- extractPath(modG2) 12 | 13 | test_that('extractPath returns the correct type and number of columns', { 14 | expect_length(extracted, c(8)) 15 | expect_is(extracted, 'tbl_df') 16 | 17 | expect_length(extracted2, c(8)) 18 | expect_is(extracted2, 'tbl_df') 19 | }) 20 | 21 | test_that('extractPath returns the correct columns', { 22 | expect_named(extracted) 23 | expect_named(extracted2) 24 | 25 | expect_equal(names(extracted), c("lambda", "carat", "cutFair", "cutGood", 26 | "cutVery Good", "cutPremium", "cutIdeal", 27 | "x" )) 28 | expect_equal(names(extracted2), c("lambda", "carat", "cutFair", "cutGood", 29 | "cutVery Good", "cutPremium", "cutIdeal", 30 | "x" )) 31 | }) 32 | -------------------------------------------------------------------------------- /tests/testthat/test-frame-builder.r: -------------------------------------------------------------------------------- 1 | context("ModelFrame") 2 | 3 | # we need data 4 | data("tips", package="reshape2") 5 | mod1 <- lm(tip ~ total_bill, data=tips) 6 | mod2 <- lm(tip ~ total_bill + sex, data=tips) 7 | mod3 <- lm(tip ~ total_bill + sex + smoker, data=tips) 8 | mod4 <- lm(tip ~ total_bill + sex*smoker, data=tips) 9 | mod5 <- lm(tip ~ total_bill + sex:smoker, data=tips) 10 | mod6 <- lm(tip ~ total_bill*sex, data=tips) 11 | mod7 <- lm(tip ~ sex*smoker, data=tips) 12 | 13 | coefDF1 <- buildModelCI(mod1) 14 | coefDF2 <- buildModelCI(mod2) 15 | coefDF3 <- buildModelCI(mod3) 16 | coefDF4 <- buildModelCI(mod4) 17 | coefDF5 <- buildModelCI(mod5) 18 | coefDF6 <- buildModelCI(mod6) 19 | coefDF7 <- buildModelCI(mod7) 20 | 21 | tips$Threshold <- tips$tip >= 4.5 22 | modG1 <- glm(Threshold ~ total_bill, data=tips, family=binomial(link="logit")) 23 | modG2 <- glm(Threshold ~ total_bill + sex, data=tips, family=binomial(link="logit")) 24 | modG3 <- glm(Threshold ~ total_bill + sex + smoker, data=tips, family=binomial(link="logit")) 25 | modG4 <- glm(Threshold ~ total_bill + sex*smoker, data=tips, family=binomial(link="logit")) 26 | modG5 <- glm(Threshold ~ total_bill + sex:smoker, data=tips, family=binomial(link="logit")) 27 | modG6 <- glm(Threshold ~ total_bill*sex, data=tips, family=binomial(link="logit")) 28 | modG7 <- glm(Threshold ~ sex*smoker, data=tips, family=binomial(link="logit")) 29 | 30 | coefDFG1 <- buildModelCI(modG1) 31 | coefDFG2 <- buildModelCI(modG2) 32 | coefDFG3 <- buildModelCI(modG3) 33 | coefDFG4 <- buildModelCI(modG4) 34 | coefDFG5 <- buildModelCI(modG5) 35 | coefDFG6 <- buildModelCI(modG6) 36 | coefDFG7 <- buildModelCI(modG7) 37 | 38 | test_that("Models come as data.frames", { 39 | expect_is(coefDF1, "data.frame") 40 | expect_is(coefDF2, "data.frame") 41 | expect_is(coefDF3, "data.frame") 42 | expect_is(coefDF4, "data.frame") 43 | expect_is(coefDF5, "data.frame") 44 | expect_is(coefDF6, "data.frame") 45 | expect_is(coefDF7, "data.frame") 46 | 47 | expect_is(coefDFG1, "data.frame") 48 | expect_is(coefDFG2, "data.frame") 49 | expect_is(coefDFG3, "data.frame") 50 | expect_is(coefDFG4, "data.frame") 51 | expect_is(coefDFG5, "data.frame") 52 | expect_is(coefDFG6, "data.frame") 53 | expect_is(coefDFG7, "data.frame") 54 | }) 55 | 56 | test_that("Coefficients have proper dimenions", { 57 | expect_equal(dim(coefDF1), c(2, 7)) 58 | expect_equal(dim(coefDF2), c(3, 7)) 59 | expect_equal(dim(coefDF3), c(4, 7)) 60 | expect_equal(dim(coefDF4), c(5, 7)) 61 | expect_equal(dim(coefDF5), c(5, 7)) 62 | expect_equal(dim(coefDF6), c(4, 7)) 63 | expect_equal(dim(coefDF7), c(4, 7)) 64 | 65 | expect_equal(dim(coefDFG1), c(2, 7)) 66 | expect_equal(dim(coefDFG2), c(3, 7)) 67 | expect_equal(dim(coefDFG3), c(4, 7)) 68 | expect_equal(dim(coefDFG4), c(5, 7)) 69 | expect_equal(dim(coefDFG5), c(5, 7)) 70 | expect_equal(dim(coefDFG6), c(4, 7)) 71 | expect_equal(dim(coefDFG7), c(4, 7)) 72 | }) 73 | 74 | test_that("Coefficients have proper types", { 75 | expect_equal(sapply(coefDF1, class), c(Value="numeric", Coefficient="factor", HighInner="numeric", LowInner="numeric", HighOuter="numeric", LowOuter="numeric", Model="character")) 76 | expect_equal(sapply(coefDF2, class), c(Value="numeric", Coefficient="factor", HighInner="numeric", LowInner="numeric", HighOuter="numeric", LowOuter="numeric", Model="character")) 77 | expect_equal(sapply(coefDF3, class), c(Value="numeric", Coefficient="factor", HighInner="numeric", LowInner="numeric", HighOuter="numeric", LowOuter="numeric", Model="character")) 78 | expect_equal(sapply(coefDF4, class), c(Value="numeric", Coefficient="factor", HighInner="numeric", LowInner="numeric", HighOuter="numeric", LowOuter="numeric", Model="character")) 79 | expect_equal(sapply(coefDF5, class), c(Value="numeric", Coefficient="factor", HighInner="numeric", LowInner="numeric", HighOuter="numeric", LowOuter="numeric", Model="character")) 80 | expect_equal(sapply(coefDF6, class), c(Value="numeric", Coefficient="factor", HighInner="numeric", LowInner="numeric", HighOuter="numeric", LowOuter="numeric", Model="character")) 81 | expect_equal(sapply(coefDF7, class), c(Value="numeric", Coefficient="factor", HighInner="numeric", LowInner="numeric", HighOuter="numeric", LowOuter="numeric", Model="character")) 82 | 83 | expect_equal(sapply(coefDFG1, class), c(Value="numeric", Coefficient="factor", HighInner="numeric", LowInner="numeric", HighOuter="numeric", LowOuter="numeric", Model="character")) 84 | expect_equal(sapply(coefDFG2, class), c(Value="numeric", Coefficient="factor", HighInner="numeric", LowInner="numeric", HighOuter="numeric", LowOuter="numeric", Model="character")) 85 | expect_equal(sapply(coefDFG3, class), c(Value="numeric", Coefficient="factor", HighInner="numeric", LowInner="numeric", HighOuter="numeric", LowOuter="numeric", Model="character")) 86 | expect_equal(sapply(coefDFG4, class), c(Value="numeric", Coefficient="factor", HighInner="numeric", LowInner="numeric", HighOuter="numeric", LowOuter="numeric", Model="character")) 87 | expect_equal(sapply(coefDFG5, class), c(Value="numeric", Coefficient="factor", HighInner="numeric", LowInner="numeric", HighOuter="numeric", LowOuter="numeric", Model="character")) 88 | expect_equal(sapply(coefDFG6, class), c(Value="numeric", Coefficient="factor", HighInner="numeric", LowInner="numeric", HighOuter="numeric", LowOuter="numeric", Model="character")) 89 | expect_equal(sapply(coefDFG7, class), c(Value="numeric", Coefficient="factor", HighInner="numeric", LowInner="numeric", HighOuter="numeric", LowOuter="numeric", Model="character")) 90 | }) 91 | -------------------------------------------------------------------------------- /tests/testthat/test-interactive.R: -------------------------------------------------------------------------------- 1 | mod2 <- lm(mpg ~ cyl + qsec - 1, data=mtcars) 2 | mod3 <- lm(mpg ~ cyl + qsec + disp - 1, data=mtcars) 3 | mod4 <- glmnet::glmnet( 4 | x=as.matrix(ggplot2::diamonds[, c('carat', 'x', 'y', 'z')]), 5 | y=ggplot2::diamonds$price 6 | ) 7 | 8 | # make static plots for each 9 | coef2_static_default <- coefplot(mod2) 10 | coef2_static_false <- coefplot(mod2, interactive=FALSE) 11 | coef2_static_default_sorted <- coefplot(mod2, interactive=FALSE, sort='magnitude') 12 | coef2_static_false_sorted <- coefplot(mod2, interactive=FALSE, sort='magnitude') 13 | coef3_static_default <- coefplot(mod3) 14 | coef3_static_false <- coefplot(mod3, interactive=FALSE) 15 | coef4_static_default <- coefplot(mod4, lambda=0.65) 16 | coef4_static_false <- coefplot(mod4, lambda=0.65, interactive=FALSE) 17 | 18 | # make interactive pots for each 19 | coef2_interactive <- coefplot(mod2, interactive=TRUE) 20 | coef3_interactive <- coefplot(mod3, interactive=TRUE) 21 | coef3_interactive_sorted <- coefplot(mod3, interactive=TRUE, sorted=TRUE) 22 | coef4_interactive <- coefplot(mod4, lambda=0.65, interactive=TRUE) 23 | 24 | test_that("Static plots return ggplot objects", { 25 | expect_s3_class(coef2_static_default, 'ggplot') 26 | expect_s3_class(coef2_static_false, 'ggplot') 27 | expect_s3_class(coef3_static_default, 'ggplot') 28 | expect_s3_class(coef3_static_false, 'ggplot') 29 | expect_s3_class(coef3_static_default, 'ggplot') 30 | expect_s3_class(coef3_static_false, 'ggplot') 31 | expect_s3_class(coef2_static_false_sorted, 'ggplot') 32 | expect_s3_class(coef2_static_default_sorted, 'ggplot') 33 | }) 34 | 35 | test_that("Interactive plots return plotly objects", { 36 | expect_s3_class(coef2_interactive, 'plotly') 37 | expect_s3_class(coef3_interactive, 'plotly') 38 | expect_s3_class(coef4_interactive, 'plotly') 39 | expect_s3_class(coef3_interactive_sorted, 'plotly') 40 | }) 41 | -------------------------------------------------------------------------------- /tests/testthat/test-tidymodels.R: -------------------------------------------------------------------------------- 1 | library(workflows) 2 | library(magrittr) 3 | 4 | lm_spec <- parsnip::linear_reg() %>% parsnip::set_engine('lm') 5 | flow <- workflow() %>% 6 | add_model(lm_spec) %>% 7 | add_formula(mpg ~ cyl + qsec + disp) 8 | mod5 <- parsnip::fit(flow, data=mtcars) 9 | mod6 <- parsnip::fit(lm_spec, mpg ~ cyl + qsec + disp, data=mtcars) 10 | 11 | coef5_static_default <- coefplot(mod5) 12 | coef5_static_false <- coefplot(mod5, interactive=FALSE) 13 | coef5_interactive <- coefplot(mod5, interactive=TRUE) 14 | coef5_interactive_sorted <- coefplot(mod5, interactive=TRUE, sort='magnitude') 15 | 16 | coef6_static_default <- coefplot(mod6) 17 | coef6_static_false <- coefplot(mod6, interactive=FALSE) 18 | coef6_interactive <- coefplot(mod6, interactive=TRUE) 19 | coef6_interactive_sorted <- coefplot(mod6, interactive=TRUE, sort='magnitude') 20 | 21 | test_that("Static plots return ggplot objects", { 22 | expect_s3_class(coef5_static_default, 'ggplot') 23 | expect_s3_class(coef5_static_false, 'ggplot') 24 | 25 | expect_s3_class(coef6_static_default, 'ggplot') 26 | expect_s3_class(coef6_static_false, 'ggplot') 27 | }) 28 | 29 | test_that("Interactive plots return plotly objects", { 30 | expect_s3_class(coef5_interactive, 'plotly') 31 | expect_s3_class(coef5_interactive_sorted, 'plotly') 32 | 33 | expect_s3_class(coef6_interactive, 'plotly') 34 | expect_s3_class(coef6_interactive_sorted, 'plotly') 35 | }) 36 | --------------------------------------------------------------------------------