|
4 | 4 | #'
|
5 | 5 | #' @param hM a fitted \code{Hmsc} model object
|
6 | 6 | #' @param focalVariable focal variable over which the gradient is constructed
|
7 |
| -#' @param non.focalVariables list giving assumptions on how non-focal variables co-vary with the focal variable |
| 7 | +#' @param non.focalVariables list giving assumptions on how non-focal variables co-vary with the focal variable or a single number given the default type for all non-focal variables |
8 | 8 | #' @param ngrid number of points along the gradient (for continuous focal variables)
|
9 | 9 | #'
|
10 | 10 | #' @return a named list with slots \code{XDataNew}, \code{studyDesignNew} and \code{rLNew}
|
11 | 11 | #'
|
12 | 12 | #' @details
|
13 |
| -#' \code{non.focalVariables} is a list, of which each element is on the form variable=list(type,value), |
14 |
| -#' where variable is one of the non-focal variables, and the value is needed only if type = 3 |
15 |
| -#' type = 1 sets the values of the non-focal variable |
16 |
| -#' to the most likely value (defined as expected value for covariates, mode for factors) |
17 |
| -#' type = 2 sets the values of the non-focal variable to most likely value, given the value of focal variable, |
18 |
| -#' based on a linear relationship |
19 |
| -#' type = 3 fixes to the value given. |
20 |
| -#' if a non.focalVariable is not listed, type=2 is used as default |
21 |
| -#' note that if the focal variable is continuous, selecting type 2 for a non-focal categorical variable can cause abrupt changes in response |
| 13 | +#' In basic form, \code{non.focalVariables} is a list, where each element is on the form variable=list(type,value), |
| 14 | +#' where \code{variable} is one of the non-focal variables, and the \code{value} is needed only if \code{type = 3}. Alternatives |
| 15 | +#' \code{type = 1} sets the values of the non-focal variable |
| 16 | +#' to the most likely value (defined as expected value for covariates, mode for factors), |
| 17 | +#' \code{type = 2} sets the values of the non-focal variable to most likely value, given the value of focal variable, |
| 18 | +#' based on a linear relationship, and |
| 19 | +#' \code{type = 3} fixes to the value given. |
| 20 | +#' If all non-focal variables are of the identical \code{type} \code{1} or \code{2}, |
| 21 | +#' a single number (\code{1} or \code{2}) can be given as an argument to |
| 22 | +#' \code{non.focalVariables}. |
| 23 | +#' If a \code{non.focalVariable} is not listed, \code{type=2} is used as default. |
| 24 | +#' Note that if the focal variable is continuous, selecting type 2 for a non-focal categorical variable can cause abrupt changes in response. |
22 | 25 | #'
|
23 | 26 | #' @seealso
|
24 | 27 | #' \code{\link{plotGradient}}, \code{\link{predict}}
|
|
38 | 41 | #'
|
39 | 42 | #' @export
|
40 | 43 |
|
41 |
| -constructGradient = function(hM, focalVariable, non.focalVariables=list(), ngrid=20){ |
| 44 | +constructGradient = |
| 45 | + function(hM, focalVariable, non.focalVariables=list(), ngrid=20) |
| 46 | +{ |
| 47 | + ## default type 2 unless a single number is given as a non-focal variable |
| 48 | + if (is.numeric(non.focalVariables) && length(non.focalVariables) == 1) { |
| 49 | + defType <- non.focalVariables |
| 50 | + if (!(defType %in% 1:2)) { |
| 51 | + warning("only type 1 and 2 are allowed as a single number: setting to 2") |
| 52 | + defType <- 2 |
| 53 | + } |
| 54 | + non.focalVariables <- NULL |
| 55 | + } else { |
| 56 | + defType <- 2 |
| 57 | + } |
42 | 58 |
|
43 | 59 | non.focalNames = names(non.focalVariables)
|
| 60 | + |
44 | 61 | Mode <- function(x, na.rm=FALSE) {
|
45 | 62 | if(na.rm){
|
46 | 63 | x = x[!is.na(x)]
|
@@ -75,7 +92,7 @@ constructGradient = function(hM, focalVariable, non.focalVariables=list(), ngrid
|
75 | 92 | }
|
76 | 93 | }
|
77 | 94 | if (!found) {
|
78 |
| - types = c(types,2) |
| 95 | + types = c(types, defType) |
79 | 96 | vals[[length(vals)+1]] = NA
|
80 | 97 | }
|
81 | 98 | }
|
|
0 commit comments