Learn R Programming

unmarked (version 1.4.3)

plotEffects: Plot marginal effects of covariates in unmarked models

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 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 to the matching predict method. For example, unmarkedFitOccuMulti models require the species argument to be included in the function call in order to work properly.

If you want to customize a plot, the easiest approach is to get data formatted for plotting using plotEffectsData, and use that. If you want to see and/or modify the code used by plotEffects to generate the default plots, run getMethod("plotEffects", "unmarkedFit") in the R console.

Usage

# S4 method for unmarkedFit
plotEffects(object, type, covariate, level=0.95, ...)
# S4 method for unmarkedFit
plotEffectsData(object, type, covariate, level=0.95, ...)

Value

A plot (plotEffects or a data frame (plotEffectsData) containing values to be used in a plot.

Arguments

object

A fitted model inheriting class unmarkedFit

type

Submodel in which the covariate of interest can be found, for example "state" or "det". This will depend on the fitted model

covariate

The name of the covariate to be plotted, as a character string

level

Confidence level for the error ribbons or bars

...

Other arguments passed to the predict function, required for some unmarkedFit types such as unmarkedFitOccuMulti

Author

Ken Kellner contact@kenkellner.com

Examples

Run this code

if (FALSE) {

# Simulate data and build an unmarked frame
set.seed(123)
dat_occ <- data.frame(x1=rnorm(500))
dat_p <- data.frame(x2=rnorm(500*5))

y <- matrix(NA, 500, 5)
z <- rep(NA, 500)

b <- c(0.4, -0.5, 0.3, 0.5)

re_fac <- factor(sample(letters[1:5], 500, replace=T))
dat_occ$group <- re_fac
re <- rnorm(5, 0, 1.2)
re_idx <- as.numeric(re_fac)

idx <- 1
for (i in 1:500){
  z[i] <- rbinom(1,1, plogis(b[1] + b[2]*dat_occ$x1[i] + re[re_idx[i]]))
  for (j in 1:5){
    y[i,j] <- z[i]*rbinom(1,1,
                    plogis(b[3] + b[4]*dat_p$x2[idx]))
    idx <- idx + 1
  }
}

umf <- unmarkedFrameOccu(y=y, siteCovs=dat_occ, obsCovs=dat_p)

# Fit model
(fm <- occu(~x2 ~x1 + group, umf))

# Plot marginal effects of various covariates
plotEffects(fm, "state", "x1")
plotEffects(fm, "state", "group")
plotEffects(fm, "det", "x2")

# Get raw data used for a plot
plotEffectsData(fm, "state", "group")

# See code used by plotEffects so you can edit it yourself and customize the plot
methods::getMethod("plotEffects", "unmarkedFit")
}

Run the code above in your browser using DataLab