Skip to content

Commit 53a56bf

Browse files
Merge pull request #19 from finnlindgren/main
fmesher/inlabru compatibility updates
2 parents ea42ed2 + 788a885 commit 53a56bf

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

45 files changed

+517
-183
lines changed

.github/workflows/R-CMD-check.yaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ jobs:
1818
fail-fast: false
1919
matrix:
2020
config:
21-
- {os: macOS-12, r: 'release'}
21+
- {os: macOS-latest, r: 'release'}
2222
- {os: windows-latest, r: 'release'}
2323
- {os: ubuntu-latest, r: 'devel', http-user-agent: 'release'}
2424
- {os: ubuntu-latest, r: 'release'}

DESCRIPTION

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
Package: PointedSDMs
22
Type: Package
33
Title: Fit Models Derived from Point Processes to Species Distributions using 'inlabru'
4-
Version: 2.1.2
4+
Version: 2.1.2.9000
55
Authors@R: c(
66
person(given = "Philip", family = "Mostert", email = "philip.s.mostert@ntnu.no",
77
role = c("aut", "cre")),
@@ -19,12 +19,13 @@ Depends:
1919
R (>= 4.1),
2020
stats,
2121
sf,
22-
inlabru (>= 2.8.0),
22+
inlabru (>= 2.12.0),
2323
R6 (>= 2.5),
2424
methods,
2525
Imports:
2626
terra,
2727
ggplot2,
28+
fmesher,
2829
raster,
2930
sp (>= 1.4-5),
3031
R.devices,
@@ -36,7 +37,6 @@ Suggests:
3637
INLA (>= 21.08.31),
3738
rasterVis,
3839
ggmap,
39-
ggpolypath,
4040
RColorBrewer,
4141
cowplot,
4242
knitr,
@@ -45,7 +45,7 @@ Suggests:
4545
spocc,
4646
covr
4747
Additional_repositories: https://inla.r-inla-download.org/R/testing
48-
RoxygenNote: 7.3.1
48+
RoxygenNote: 7.3.2
4949
License: GPL (>= 3)
5050
VignetteBuilder: knitr
5151
Config/testthat/edition: 3

R/blockedCV.R

+3-3
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ blockedCV <- function(data, options = list(),
5353

5454
if (!inherits(data, 'dataSDM') && !inherits(data, 'specifySpecies') && !inherits(data, 'specifyISDM')) stop('data needs to be a dataSDM object.')
5555

56-
if (is.null(data$.__enclos_env__$private$INLAmesh)) stop('An inla.mesh object is required before any model is run.')
56+
if (is.null(data$.__enclos_env__$private$INLAmesh)) stop('An fm_mesh_2d object is required before any model is run.')
5757

5858
if (!data$.__enclos_env__$private$blockedCV) stop('Please use ".$spatialBlock" before using this function.')
5959

@@ -226,12 +226,12 @@ blockedCV <- function(data, options = list(),
226226

227227
if (!is.null(data$.__enclos_env__$private$speciesSpatial)) {
228228

229-
if (data$.__enclos_env__$private$speciesSpatial == 'replicate') ips <- fm_cprod(ips, data.frame(speciesSpatialGroup = 1:max(data$.__enclos_env__$private$speciesTable$index)))
229+
if (data$.__enclos_env__$private$speciesSpatial == 'replicate') ips <- fmesher::fm_cprod(ips, data.frame(speciesSpatialGroup = 1:max(data$.__enclos_env__$private$speciesTable$index)))
230230

231231
}
232232
if (!is.null(data$.__enclos_env__$private$Intercepts)) {
233233

234-
if (data$.__enclos_env__$private$speciesIntercepts) ips <- fm_cprod(ips, data.frame(specIntTermRem = 1:max(data$.__enclos_env__$private$speciesTable$index)))
234+
if (data$.__enclos_env__$private$speciesIntercepts) ips <- fmesher::fm_cprod(ips, data.frame(specIntTermRem = 1:max(data$.__enclos_env__$private$speciesTable$index)))
235235
names(ips)[names(ips) == 'specIntTermRem'] <- data$.__enclos_env__$private$speciesName
236236
}
237237
}

R/bruSDM_predict.R

+7-7
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ setClass('bruSDM_predict')
1111
#' @param object A \code{bru_sdm} objects.
1212
#' @param data Data containing points of the map with which to predict on. May be \code{NULL} if one of \code{mesh} or \code{mask} is \code{NULL}.
1313
#' @param formula Formula to predict. May be \code{NULL} if other arguments: \code{covariates}, \code{spatial}, \code{intercepts} are not \code{NULL}.
14-
#' @param mesh An \code{inla.mesh} object.
14+
#' @param mesh An \code{fm_mesh_2d} object.
1515
#' @param mask A mask of the study background. Defaults to \code{NULL}.
1616
#' @param covariates Name of covariates to predict.
1717
#' @param temporal Make predictions for the temporal component of the model.
@@ -65,7 +65,7 @@ predict.bruSDM <- function(object, data = NULL, formula = NULL, mesh = NULL,
6565
marks = NULL, biasfield = FALSE, biasnames = NULL, predictor = FALSE,
6666
fun = 'linear', format = 'sf', ...) {
6767

68-
if (is.null(data) & is.null(mesh)) stop("Either data covering the entire study region or an inla.mesh object is required.")
68+
if (is.null(data) & is.null(mesh)) stop("Either data covering the entire study region or an fm_mesh_2d object is required.")
6969

7070
## if datasets !is.null but at least one not in model stop
7171
# else datasets <- all datasets in the model.
@@ -142,24 +142,24 @@ predict.bruSDM <- function(object, data = NULL, formula = NULL, mesh = NULL,
142142

143143
if (!is.null(mask)) {
144144

145-
data <- inlabru::fm_pixels(mesh, mask = mask, format = format)
145+
data <- fmesher::fm_pixels(mesh, mask = mask, format = format)
146146

147147
}
148-
else data <- inlabru::fm_int(mesh, format = format)
148+
else data <- fmesher::fm_int(mesh, format = format)
149149
}
150150

151151
if (speciespreds) {
152152

153153
if (object[['species']][['speciesEffects']][['Intercepts']]) {
154154

155-
data <- fm_cprod(data, data.frame(speciesIndexREMOVE = 1:length(unique(unlist(object$species$speciesIn)))))
155+
data <- fmesher::fm_cprod(data, data.frame(speciesIndexREMOVE = 1:length(unique(unlist(object$species$speciesIn)))))
156156
names(data)[names(data) == 'speciesIndexREMOVE'] <- object[['species']][['speciesVar']]
157157

158158
}
159159

160160
if (object$spatial$species == 'replicate') {
161161

162-
if (!object[['species']][['speciesVar']] %in% names(data)) data <- fm_cprod(data, data.frame(speciesSpatialGroup = 1:length(unique(unlist(object$species$speciesIn)))))
162+
if (!object[['species']][['speciesVar']] %in% names(data)) data <- fmesher::fm_cprod(data, data.frame(speciesSpatialGroup = 1:length(unique(unlist(object$species$speciesIn)))))
163163
else data$speciesSpatialGroup <- data[[object[['species']][['speciesVar']]]]
164164

165165
}
@@ -213,7 +213,7 @@ predict.bruSDM <- function(object, data = NULL, formula = NULL, mesh = NULL,
213213
time_data <- data.frame(seq_len(max(numeric_time)))
214214
names(time_data) <- time_variable
215215

216-
timeData <- inlabru::fm_cprod(data, data.frame(time_data))
216+
timeData <- fmesher::fm_cprod(data, data.frame(time_data))
217217
names(timeData)[!names(timeData) %in% c('geometry', '.block')] <- c(time_variable, 'weight')
218218

219219
#bias

R/dataOrganize.R

+10-7
Original file line numberDiff line numberDiff line change
@@ -896,7 +896,7 @@ dataOrganize$set('public', 'makeComponents', function(spatial, intercepts,
896896
})
897897

898898
#' @description Function to make the datasets into likelihoods.
899-
#' @param mesh An inla.mesh object.
899+
#' @param mesh An \code{fm_mesh_2d} object.
900900
#' @param ips Integration points used.
901901
#' @param paresp The response variable name for the presence absence datasets.
902902
#' @param ntrialsvar The trials variable name for the presence absence datasets.
@@ -968,19 +968,22 @@ dataOrganize$set('public', 'makeLhoods', function(mesh, ips,
968968
}
969969
else IPS <- ips
970970
##rm formulas for now
971+
972+
# bru_like_list will use the like-tag, not the list names.
973+
if (is.null(names(self$Data[[dataset]])[species])) nameGive <- names(self$Data)[[dataset]]
974+
else nameGive <- names(self$Data[[dataset]])[species]
975+
976+
like_name <- paste0(nameGive, '_', as.character(self$Formulas[[dataset]][[species]][[process]][['LHS']])[2])
971977

972978
Likelihoods[[Likindex]] <- inlabru::like(formula = self$Formulas[[dataset]][[species]][[process]][['LHS']],
973979
include = self$Formulas[[dataset]][[species]][[process]][['RHS']],
974980
data = self$Data[[dataset]][[species]],
975981
Ntrials = Ntrials,
976-
mesh = mesh,
977982
ips = IPS,
978-
family = self$Family[[dataset]][process])
979-
980-
if (is.null(names(self$Data[[dataset]])[species])) nameGive <- names(self$Data)[[dataset]]
981-
else nameGive <- names(self$Data[[dataset]])[species]
983+
family = self$Family[[dataset]][process],
984+
tag = like_name)
982985

983-
names(Likelihoods)[[Likindex]] <- paste0(nameGive, '_', as.character(self$Formulas[[dataset]][[species]][[process]][['LHS']])[2])
986+
names(Likelihoods)[[Likindex]] <- like_name
984987

985988
}
986989

R/datasetOut.R

+6-8
Original file line numberDiff line numberDiff line change
@@ -125,14 +125,8 @@ datasetOut <- function(model, dataset,
125125

126126
}
127127

128-
129-
reduced_terms <- unique(unlist(lapply(model$bru_info$lhoods[index], function(x) {
130-
131-
if (!identical(unlist(x$used), character(0))) unlist(x$used)
132-
else labels(terms(x$formula))
133-
134-
})))
135-
128+
reduced_terms <- unlist(inlabru::bru_used(model$bru_info$lhoods[index]))
129+
136130
reduced_components <- reduceComps(componentsOld = model$componentsJoint,
137131
pointsCopy = ifelse(model$spatial$points == 'copy',
138132
TRUE, FALSE),
@@ -178,6 +172,10 @@ datasetOut <- function(model, dataset,
178172

179173
if (predictions) {
180174

175+
# Seems you require names, but haven't used tags, so they're all NA
176+
if (all(is.na(names(model$bru_info$lhoods)))) {
177+
names(model$bru_info$lhoods) <- model[["source"]]
178+
}
181179
reduced_lik <- model$bru_info$lhoods
182180

183181
for (data in names(model$bru_info$lhoods)[!index]) {

R/fitISDM.R

+3-3
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ fitISDM <- function(data, options = list()) {
4444
if (!inherits(data, 'dataSDM') && !inherits(data, 'specifySpecies') && !inherits(data, 'specifyISDM') &&
4545
!inherits(data, 'specifyMarks')) stop('data needs to be either a specifySpecies, specifyISDM or specifyMarks object.')
4646

47-
if (is.null(data$.__enclos_env__$private$INLAmesh)) stop('An inla.mesh object is required before any model is run.')
47+
if (is.null(data$.__enclos_env__$private$INLAmesh)) stop('An fm_mesh_2d object is required before any model is run.')
4848

4949
data2ENV(data = data, env = environment())
5050

@@ -135,8 +135,8 @@ fitISDM <- function(data, options = list()) {
135135
samplers = data$.__enclos_env__$private$biasData[[bias]],
136136
ips = data$.__enclos_env__$private$IPS,
137137
domain = list(coordinates = data$.__enclos_env__$private$INLAmesh),
138-
include = c(paste0(bias, '_samplers_field'), paste0(bias,'_samplers'), data$.__enclos_env__$private$spatcovsNames))
139-
138+
include = c(paste0(bias, '_samplers_field'), paste0(bias,'_samplers'), data$.__enclos_env__$private$spatcovsNames),
139+
tag = paste0(bias, '_samplers'))
140140

141141
}
142142

R/intModel.R

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66
#' @param spatialCovariates The spatial covariates used in the model. These covariates must be measured at every location (pixel) in the study area, and must be a \code{Raster*}, \code{SpatialPixelsDataFrame} or \code{SpatialRaster} object. Can be either \code{numeric}, \code{factor} or \code{character} data.
77
#' @param Coordinates A vector of length 2 containing the names (class \code{character}) of the coordinate variables used in the model.
88
#' @param Projection The coordinate reference system used by both the spatial points and spatial covariates. Must be of class \code{character}.
9-
#' @param Mesh An \code{inla.mesh} object required for the spatial random fields and the integration points in the model (see \code{\link[INLA]{inla.mesh.2d}} from the \pkg{INLA} package for more details).
10-
#' @param IPS The integration points to be used in the model (that is, the points on the map where the intensity of the model is calculated). See \code{\link[inlabru]{fm_int}} from the \pkg{inlabru} package for more details regarding these points; however defaults to \code{NULL} which will create integration points from the \code{Mesh} object.
9+
#' @param Mesh An \code{fm_mesh_2d} object required for the spatial random fields and the integration points in the model (see \code{\link[fmesher]{fm_mesh_2d_inla}} from the \pkg{fmesher} package for more details).
10+
#' @param IPS The integration points to be used in the model (that is, the points on the map where the intensity of the model is calculated). See \code{\link[fmesher]{fm_int}} from the \pkg{fmesher} package for more details regarding these points; however defaults to \code{NULL} which will create integration points from the \code{Mesh} object.
1111
#' @param Boundary A \code{sf} object of the study area. If not missing, this object is used to help create the integration points.
1212
#' @param speciesSpatial Argument to specify if each species should have their own spatial effect with different hyperparameters to be estimated using \pkg{INLA}'s "replicate" feature, of if a the field's should be estimated per species copied across datasets using \pkg{INLA}'s "copy" feature. Possible values include: \code{'replicate'}, \code{'copy'}, \code{'shared'} or \code{NULL} if no species-specific spatial effects should be estimated.
1313
#' @param speciesIndependent Logical argument: Should species effects be made independent of one another. Defaults to \code{FALSE} which creates effects for each species independently.

R/makeLhoods.R

+11-8
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
#' @param data A list of sf objects containing the datasets for which likelihoods need to be constructed.
44
#' @param formula A list of formulas to add to the likelihoods.
55
#' @param family A list of vectors containing the families within each dataset.
6-
#' @param mesh An inla.mesh object.
6+
#' @param mesh An \code{fm_mesh_2d} object.
77
#' @param ips Integration points used.
88
#' @param paresp The response variable name for the presence absence datasets.
99
#' @param ntrialsvar The trials variable name for the presence absence datasets.
@@ -99,21 +99,24 @@ makeLhoods <- function(data, formula, family, mesh, ips,
9999

100100
}
101101
}
102-
102+
103+
# bru_like_list will use the like-tag for list names; inlabru >= 2.12.0
104+
if (is.null(names(data[[dataset]])[species])) nameGive <- names(data)[[dataset]]
105+
else nameGive <- names(data[[dataset]])[species]
106+
107+
like_name <- paste0(nameGive, '_', sub(' .*', '', as.character(formula[[dataset]][[species]][[process]][['LHS']])[2]))
108+
103109
Likelihoods[[Likindex]] <- inlabru::like(formula = formula[[dataset]][[species]][[process]][['LHS']], ## but obs change these in function call
104110
include = formula[[dataset]][[species]][[process]][['RHS']],
105111
data = data[[dataset]][[species]],
106112
Ntrials = Ntrials,
107-
mesh = mesh,
108113
ips = IPS,
109114
domain = list(geometry = mesh),
110115
samplers = samplers[[names(data)[[dataset]]]],
111-
family = family[[dataset]][process])
112-
113-
if (is.null(names(data[[dataset]])[species])) nameGive <- names(data)[[dataset]]
114-
else nameGive <- names(data[[dataset]])[species]
116+
family = family[[dataset]][process],
117+
tag = like_name)
115118

116-
names(Likelihoods)[[Likindex]] <- paste0(nameGive, '_', sub(' .*', '', as.character(formula[[dataset]][[species]][[process]][['LHS']])[2]))
119+
names(Likelihoods)[[Likindex]] <- like_name
117120

118121
}
119122

R/modISDM_predict.R

+5-5
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ setClass('modISDM_predict')
1010
#' @param object A \code{modISDM} object.
1111
#' @param data Data containing points of the map with which to predict on. May be \code{NULL} if one of \code{mesh} or \code{mask} is \code{NULL}.
1212
#' @param formula Formula to predict. May be \code{NULL} if other arguments: \code{covariates}, \code{spatial}, \code{intercepts} are not \code{NULL}.
13-
#' @param mesh An \code{inla.mesh} object.
13+
#' @param mesh An \code{fm_mesh_2d} object.
1414
#' @param mask A mask of the study background. Defaults to \code{NULL}.
1515
#' @param covariates Name of covariates to predict.
1616
#' @param spatial Logical: include spatial effects in prediction. Defaults to \code{FALSE}.
@@ -65,7 +65,7 @@ predict.modISDM <- function(object, data = NULL, formula = NULL, mesh = NULL,
6565

6666
##If pointsSpatial == correlate, then need to chose a dataset for which to use as the spatial effect. Could use either the first PA dataset or specify using dataset
6767

68-
if (is.null(data) & is.null(mesh)) stop("Either data covering the entire study region or an inla.mesh object is required.")
68+
if (is.null(data) & is.null(mesh)) stop("Either data covering the entire study region or an fm_mesh_2d object is required.")
6969

7070
#Why can't you do both here?
7171
if (bias && spatial) stop('Please choose one of bias and spatial.')
@@ -110,10 +110,10 @@ predict.modISDM <- function(object, data = NULL, formula = NULL, mesh = NULL,
110110

111111
if (!is.null(mask)) {
112112

113-
data <- inlabru::fm_pixels(mesh, mask = mask)
113+
data <- fmesher::fm_pixels(mesh, mask = mask)
114114

115115
}
116-
else data <- inlabru::fm_int(mesh)
116+
else data <- fmesher::fm_int(mesh)
117117
}
118118

119119
if (!is.null(object$temporal$temporalVar)) temporal <- TRUE
@@ -164,7 +164,7 @@ predict.modISDM <- function(object, data = NULL, formula = NULL, mesh = NULL,
164164
time_data <- data.frame(seq_len(max(numeric_time)))
165165
names(time_data) <- time_variable
166166

167-
data <- inlabru::fm_cprod(data, data.frame(time_data))
167+
data <- fmesher::fm_cprod(data, data.frame(time_data))
168168
data$.__plot__index__ <- data[[time_variable]]
169169

170170
}

R/modMarks_predict.R

+6-6
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ setClass('modMarks_predict')
1111
#' @param object A \code{modMarks} object.
1212
#' @param data Data containing points of the map with which to predict on. May be \code{NULL} if one of \code{mesh} or \code{mask} is \code{NULL}.
1313
#' @param formula Formula to predict. May be \code{NULL} if other arguments: \code{covariates}, \code{spatial}, \code{intercepts} are not \code{NULL}.
14-
#' @param mesh An \code{inla.mesh} object.
14+
#' @param mesh An \code{fm_mesh_2d} object.
1515
#' @param mask A mask of the study background. Defaults to \code{NULL}.
1616
#' @param covariates Name of covariates to predict.
1717
#' @param spatial Logical: include spatial effects in prediction. Defaults to \code{FALSE}.
@@ -65,7 +65,7 @@ predict.modMarks <- function(object, data = NULL, formula = NULL, mesh = NULL,
6565
marks = NULL, bias = FALSE, biasnames = NULL, predictor = FALSE,
6666
fun = 'linear', ...) {
6767

68-
if (is.null(data) & is.null(mesh)) stop("Either data covering the entire study region or an inla.mesh object is required.")
68+
if (is.null(data) & is.null(mesh)) stop("Either data covering the entire study region or an fm_mesh_2d object is required.")
6969

7070
## if non-null biasfields ## if no bias fields in stop: if biasnames not in biasfields stop
7171
if (bias && spatial) stop('Please choose one of bias and spatial.')
@@ -140,10 +140,10 @@ predict.modMarks <- function(object, data = NULL, formula = NULL, mesh = NULL,
140140

141141
if (!is.null(mask)) {
142142

143-
data <- inlabru::fm_pixels(mesh, mask = mask)
143+
data <- fmesher::fm_pixels(mesh, mask = mask)
144144

145145
}
146-
else data <- inlabru::fm_int(mesh)
146+
else data <- fmesher::fm_int(mesh)
147147
}
148148

149149
if (!any(names(data) %in% object$spatCovs$name)) {
@@ -193,7 +193,7 @@ predict.modMarks <- function(object, data = NULL, formula = NULL, mesh = NULL,
193193
time_data <- data.frame(seq_len(max(numeric_time)))
194194
names(time_data) <- time_variable
195195

196-
data <- inlabru::fm_cprod(data, data.frame(time_data))
196+
data <- fmesher::fm_cprod(data, data.frame(time_data))
197197
data$.__plot__index__ <- data[[time_variable]]
198198

199199
}
@@ -428,4 +428,4 @@ plot.modMarks_predict <- function(x,
428428
}
429429

430430

431-
}
431+
}

0 commit comments

Comments
 (0)