diff options
author | Ken Kellner <ken@kenkellner.com> | 2023-03-07 10:09:09 -0500 |
---|---|---|
committer | Ken Kellner <ken@kenkellner.com> | 2023-03-07 10:09:09 -0500 |
commit | 40a45fbe202625c93dd1be893891ecd1d8f3bbd3 (patch) | |
tree | 85b055396544a21370adb48da3dafb17344086c4 | |
parent | af3942cfdc289b8dedc3df176549b13c36dab23d (diff) |
Change name to plotEffects
-rw-r--r-- | DESCRIPTION | 6 | ||||
-rw-r--r-- | NAMESPACE | 2 | ||||
-rw-r--r-- | R/plotEffects.R (renamed from R/plotMarginal.R) | 18 | ||||
-rw-r--r-- | man/plotEffects.Rd (renamed from man/plotMarginal.Rd) | 44 | ||||
-rw-r--r-- | tests/testthat/test_gdistremoval.R | 6 | ||||
-rw-r--r-- | tests/testthat/test_occuTTD.R | 4 | ||||
-rw-r--r-- | tests/testthat/test_plotEffects.R (renamed from tests/testthat/test_plotMarginal.R) | 18 |
7 files changed, 56 insertions, 42 deletions
diff --git a/DESCRIPTION b/DESCRIPTION index cabf60b..5a04732 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,6 +1,6 @@ Package: unmarked -Version: 1.2.5.9010 -Date: 2022-12-29 +Version: 1.2.5.9011 +Date: 2023-03-07 Type: Package Title: Models for Data from Unmarked Animals Authors@R: c( @@ -46,7 +46,7 @@ Collate: 'classes.R' 'unmarkedEstimate.R' 'mapInfo.R' 'unmarkedFrame.R' 'unmarkedCrossVal.R' 'piFun.R' 'vif.R' 'makePiFun.R' 'posteriorSamples.R' 'nmixTTD.R' 'gdistremoval.R' - 'plotMarginal.R' + 'plotEffects.R' 'mixedModelTools.R' 'power.R' 'simulate.R' @@ -51,7 +51,7 @@ exportMethods(backTransform, coef, confint, coordinates, fitted, getData, "siteCovs<-", summary, update, vcov, yearlySiteCovs, "yearlySiteCovs<-", "[", smoothed, projected, nonparboot, logLik, LRT, ranef, bup, crossVal, posteriorSamples, sigma, randomTerms, - optimizePenalty, unmarkedPowerList, plotMarginalData, plotMarginal) + optimizePenalty, unmarkedPowerList, plotEffectsData, plotEffects) S3method("print", "unmarkedPostSamples") diff --git a/R/plotMarginal.R b/R/plotEffects.R index 14bbd74..f5be1d8 100644 --- a/R/plotMarginal.R +++ b/R/plotEffects.R @@ -1,8 +1,10 @@ type_to_covs <- function(umf, type){ - if(type %in% c("state")){ + if(type %in% c("state","psi","lam","lambda","sigma","dist")){ return(methods::slot(umf, "siteCovs")) - } else if(type %in% c("det")){ + } else if(type %in% c("det","rem","fp","b")){ return(methods::slot(umf, "obsCovs")) + } else if(type %in% c("phi","transition","col","ext","gamma","omega","iota")){ + return(methods::slot(umf, "yearlySiteCovs")) } return(NULL) } @@ -11,7 +13,7 @@ get_base_newdata <- function(umf, type){ covs <- type_to_covs(umf, type) out <- lapply(covs, function(x){ if(is.numeric(x)){ - return(mean(x, na.rm=TRUE)) + return(median(x, na.rm=TRUE)) } else if(is.factor(x)){ return(factor(levels(x)[1], levels=levels(x))) } else { @@ -33,9 +35,9 @@ get_cov_seq <- function(covariate, umf, type){ } } -setGeneric("plotMarginalData", function(object, ...) standardGeneric("plotMarginalData")) +setGeneric("plotEffectsData", function(object, ...) standardGeneric("plotEffectsData")) -setMethod("plotMarginalData", "unmarkedFit", +setMethod("plotEffectsData", "unmarkedFit", function(object, type, covariate, level=0.95, ...){ umf <- umf_to_factor(object@data) @@ -53,13 +55,13 @@ setMethod("plotMarginalData", "unmarkedFit", pr }) -setGeneric("plotMarginal", function(object, ...) standardGeneric("plotMarginal")) +setGeneric("plotEffects", function(object, ...) standardGeneric("plotEffects")) -setMethod("plotMarginal", "unmarkedFit", +setMethod("plotEffects", "unmarkedFit", function(object, type, covariate, level=0.95, ...){ # Get data for plot - plot_data <- plotMarginalData(object, type, covariate, level, ...) + plot_data <- plotEffectsData(object, type, covariate, level, ...) # Is covariate a factor? is_factor <- is.factor(plot_data$covariateValue) diff --git a/man/plotMarginal.Rd b/man/plotEffects.Rd index 0be5bbc..d176d43 100644 --- a/man/plotMarginal.Rd +++ b/man/plotEffects.Rd @@ -1,22 +1,22 @@ -\name{plotMarginal} -\alias{plotMarginal} -\alias{plotMarginal-methods} -\alias{plotMarginal,unmarkedFit-method} -\alias{plotMarginalData} -\alias{plotMarginalData-methods} -\alias{plotMarginalData,unmarkedFit-method} +\name{plotEffects} +\alias{plotEffects} +\alias{plotEffects-methods} +\alias{plotEffects,unmarkedFit-method} +\alias{plotEffectsData} +\alias{plotEffectsData-methods} +\alias{plotEffectsData,unmarkedFit-method} \title{Plot marginal effects of covariates in unmarked models} -\description{This function generates a plot visualizing the marginal effects - of a given covariate on a parameter (e.g. occupancy, abundance) in an unmarked +\description{This function generates a plot visualizing the effects + of a single covariate on a parameter (e.g. occupancy, abundance) in an unmarked model. If the covariate is numeric, the result is a line plot with an error ribbon where the x-axis is the range of the covariate and the y-axis is the predicted parameter value. If the covariate is an R factor (i.e., categorical), the x-axis instead contains each unique value of the covariate. All covariates in the model besides the one being plotted are held either at - their mean value (if they are numeric) or at their reference level (if they + their median value (if they are numeric) or at their reference level (if they are factors). Some types of unmarked models may require additional arguments, which are passed @@ -25,15 +25,15 @@ call in order to work properly. If you want to customize a plot, the easiest approach is to get data - formatted for plotting using \code{plotMarginalData}, and use that. If - you want to see and/or modify the code used by \code{plotMarginal} to generate - the default plots, run \code{getMethod("plotMarginal", "unmarkedFit")} in + formatted for plotting using \code{plotEffectsData}, and use that. If + you want to see and/or modify the code used by \code{plotEffects} to generate + the default plots, run \code{getMethod("plotEffects", "unmarkedFit")} in the R console. } \usage{ -\S4method{plotMarginal}{unmarkedFit}(object, type, covariate, level=0.95, ...) -\S4method{plotMarginalData}{unmarkedFit}(object, type, covariate, level=0.95, ...) +\S4method{plotEffects}{unmarkedFit}(object, type, covariate, level=0.95, ...) +\S4method{plotEffectsData}{unmarkedFit}(object, type, covariate, level=0.95, ...) } \arguments{ @@ -46,7 +46,7 @@ for some \code{unmarkedFit} types such as \code{unmarkedFitOccuMulti}} } -\value{A plot (\code{plotMarginal} or a data frame (\code{plotMarginalData}) +\value{A plot (\code{plotEffects} or a data frame (\code{plotEffectsData}) containing values to be used in a plot. } @@ -87,14 +87,14 @@ umf <- unmarkedFrameOccu(y=y, siteCovs=dat_occ, obsCovs=dat_p) (fm <- occu(~x2 ~x1 + group, umf)) # Plot marginal effects of various covariates -plotMarginal(fm, "state", "x1") -plotMarginal(fm, "state", "group") -plotMarginal(fm, "det", "x2") +plotEffects(fm, "state", "x1") +plotEffects(fm, "state", "group") +plotEffects(fm, "det", "x2") # Get raw data used for a plot -plotMarginalData(fm, "state", "group") +plotEffectsData(fm, "state", "group") -# See code used by plotMarginal so you can edit it yourself and customize the plot -methods::getMethod("plotMarginal", "unmarkedFit") +# See code used by plotEffects so you can edit it yourself and customize the plot +methods::getMethod("plotEffects", "unmarkedFit") } } diff --git a/tests/testthat/test_gdistremoval.R b/tests/testthat/test_gdistremoval.R index 8f453a5..3e205fb 100644 --- a/tests/testthat/test_gdistremoval.R +++ b/tests/testthat/test_gdistremoval.R @@ -343,6 +343,12 @@ test_that("gdistremoval can fit models",{ expect_is(fl, "unmarkedFitList") ms <- modSel(fl) expect_is(ms, "unmarkedModSel") + + #plotEffects + pdf(NULL) + plotEffects(fit, 'lambda', "sc1") + plotEffects(fit, 'rem', 'oc1') + dev.off() }) test_that("gdistremoval predict method works",{ diff --git a/tests/testthat/test_occuTTD.R b/tests/testthat/test_occuTTD.R index f152cde..0af27a8 100644 --- a/tests/testthat/test_occuTTD.R +++ b/tests/testthat/test_occuTTD.R @@ -382,6 +382,10 @@ test_that("occuTTD can fit a dynamic model",{ expect_equivalent(dim(s[[1]]), c(100,4)) r <- residuals(fit) expect_equivalent(dim(r), c(100,4)) + pdf(NULL) + plotEffects(fit, 'psi', 'elev') + plotEffects(fit, 'col', 'forest') + dev.off() #Check ranef r <- ranef(fit) diff --git a/tests/testthat/test_plotMarginal.R b/tests/testthat/test_plotEffects.R index fcbfe2a..3aea3ec 100644 --- a/tests/testthat/test_plotMarginal.R +++ b/tests/testthat/test_plotEffects.R @@ -1,4 +1,4 @@ -context("plotMarginal") +context("plotEffects") skip_on_cran() @@ -30,15 +30,17 @@ umf <- unmarkedFrameOccu(y=y, siteCovs=dat_occ, obsCovs=dat_p) fm <- occu(~x2 ~x1 + group, umf) -test_that("plotMarginal works", { +test_that("plotEffects works", { + + pdf(NULL) + plotEffects(fm, "state", "x1") + plotEffects(fm, "state", "group") + plotEffects(fm, "det", "x2") + dev.off() - plotMarginal(fm, "state", "x1") - plotMarginal(fm, "state", "group") - plotMarginal(fm, "det", "x2") + expect_error(plotEffects(fm, "state", "x2")) - expect_error(plotMarginal(fm, "state", "x2")) - - dat <- plotMarginalData(fm, "state", "group") + dat <- plotEffectsData(fm, "state", "group") expect_true(inherits(dat, "data.frame")) expect_equal(nrow(dat), 5) |