Skip to content

Commit e8458c8

Browse files
committed
Merge branch 'course-todo'
2 parents 72c479e + 1446c91 commit e8458c8

File tree

4 files changed

+62
-27
lines changed

4 files changed

+62
-27
lines changed

R/constructGradient.R

+29-12
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,24 @@
44
#'
55
#' @param hM a fitted \code{Hmsc} model object
66
#' @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
88
#' @param ngrid number of points along the gradient (for continuous focal variables)
99
#'
1010
#' @return a named list with slots \code{XDataNew}, \code{studyDesignNew} and \code{rLNew}
1111
#'
1212
#' @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.
2225
#'
2326
#' @seealso
2427
#' \code{\link{plotGradient}}, \code{\link{predict}}
@@ -38,9 +41,23 @@
3841
#'
3942
#' @export
4043

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+
}
4258

4359
non.focalNames = names(non.focalVariables)
60+
4461
Mode <- function(x, na.rm=FALSE) {
4562
if(na.rm){
4663
x = x[!is.na(x)]
@@ -75,7 +92,7 @@ constructGradient = function(hM, focalVariable, non.focalVariables=list(), ngrid
7592
}
7693
}
7794
if (!found) {
78-
types = c(types,2)
95+
types = c(types, defType)
7996
vals[[length(vals)+1]] = NA
8097
}
8198
}

R/plotGradient.R

+15-5
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,11 @@
1717
#' @param showData whether raw data are plotted as well
1818
#' @param jigger the amount by which the raw data are to be jiggered in x-direction (for factors) or
1919
#' y-direction (for continuous covariates)
20+
#'
21+
#' @param yshow scale y-axis so that these values are also
22+
#' visible. This can used to scale y-axis so that it includes 0
23+
#' and the expected maximum values.
24+
#'
2025
#' @param ... additional arguments for plot
2126
#'
2227
#' @return For the case of a continuous covariate, returns the posterior probability that the plotted
@@ -59,7 +64,12 @@
5964
#'
6065
#' @export
6166

62-
plotGradient=function (hM, Gradient, predY, measure, xlabel = NULL, ylabel = NULL, index = 1, q = c(0.025, 0.5, 0.975), cicol = rgb(0,0,1,alpha=.5), pointcol = "lightgrey", pointsize = 1, showData = FALSE, jigger = 0, ...){
67+
plotGradient =
68+
function (hM, Gradient, predY, measure, xlabel = NULL, ylabel = NULL,
69+
index = 1, q = c(0.025, 0.5, 0.975), cicol = rgb(0,0,1,alpha=.5),
70+
pointcol = "lightgrey", pointsize = 1, showData = FALSE,
71+
jigger = 0, yshow = NA, ...)
72+
{
6373

6474
Pr = NA
6575

@@ -136,8 +146,8 @@ plotGradient=function (hM, Gradient, predY, measure, xlabel = NULL, ylabel = NUL
136146
hi = qpred[3, ]
137147
me = qpred[2, ]
138148

139-
lo1 = min(lo)
140-
hi1 = max(hi)
149+
lo1 = min(lo, yshow, na.rm = TRUE)
150+
hi1 = max(hi, yshow, na.rm = TRUE)
141151

142152
if(showData){
143153
switch(class(hM$X)[1L],
@@ -149,7 +159,7 @@ plotGradient=function (hM, Gradient, predY, measure, xlabel = NULL, ylabel = NUL
149159
}
150160
)
151161
if (measure == "S") {
152-
pY = rowSums(hM$Y)
162+
pY = rowSums(hM$Y, na.rm = TRUE)
153163
}
154164
if (measure == "Y") {
155165
pY = hM$Y[,index]
@@ -215,5 +225,5 @@ plotGradient=function (hM, Gradient, predY, measure, xlabel = NULL, ylabel = NUL
215225
lines(xx, qpred[2, ], lwd = 2)
216226

217227
}
218-
return(if(is.factor(xx)){pl} else {Pr})
228+
return(if(is.factor(xx)) pl else Pr)
219229
}

man/constructGradient.Rd

+13-10
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/plotGradient.Rd

+5
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)