ENMeval

Please find below the R code history from your *Wallace* v1.0.6.3
session.

You can reproduce your session results by running this R Markdown file
in RStudio.

Each code block is called a “chunk”, and you can run them either
one-by-one or all at once by choosing an option in the “Run” menu at the
top-right corner of the “Source” pane in RStudio.

For more detailed information see
<a href="http://rmarkdown.rstudio.com" class="uri">http://rmarkdown.rstudio.com</a>).

### Package installation

Wallace uses the following R packages that must be installed and loaded
before starting.

```{r}
library(spocc)
library(spThin)
library(dismo)
library(rgeos)
library(ENMeval)
library(dplyr)
```

Wallace also includes several functions developed to help integrate
different packages and some additional functionality. For this reason,
it is necessary to load the file `functions.R`, The function
`system.file()` finds this script, and `source()` loads it.

```{r}
source(system.file('shiny/funcs', 'functions.R', package = 'wallace'))
```

Record of analysis for \*\*.
----------------------------

  User CSV path with occurrence data. If the CSV file is not in the
current workspace, change to the correct file path
(e.g. “/Users/darwin/Documents/occs.csv”).

```{r}
# NOTE: provide the path to the folder that contains the CSV file
d.occs <- 'E:/worklist/ligengyun/f1_points/'
# create path to user occurrences csv file
userOccs.path <- file.path(d.occs, "aph_na.csv")
# read in csv
userOccs.csv <- read.csv(userOccs.path, header = TRUE)
# remove rows with duplicate coordinates
occs.dups <- duplicated(userOccs.csv[c('longitude', 'latitude')])
occs <- userOccs.csv[!occs.dups,]
# remove NAs
occs <- occs[complete.cases(occs$longitude, occs$latitude), ]
# give all records a unique ID
occs$occID <- row.names(occs)
```

### Obtain Environmental Data

dir("E:/环境数据/wc2.1_2.5m_bio/")
```{r}
# NOTE: provide the path to the folder that contains the rasters
d.envs <- 'E:/环境数据/wc2.5_bio/'
# create paths to the raster files
userRas.paths <- file.path(d.envs, c('wc2.1_2.5m_bio_5.tif', 'wc2.1_2.5m_bio_9.tif', 'wc2.1_2.5m_bio_11.tif', 'wc2.1_2.5m_bio_12.tif', 'wc2.1_2.5m_bio_13.tif', 'wc2.1_2.5m_bio_19.tif'))
# make a RasterStack out of the raster files
envs <- raster::stack(userRas.paths)
```

### Process Environmental Data

Background selection technique chosen as Minimum Convex Polygon.

```{r}
occs.xy <- occs[c('longitude', 'latitude')]
sp::coordinates(occs.xy) <- ~ longitude + latitude
bgExt <- mcp(occs.xy)
```

Buffer size of the study extent polygon defined as 0.5 degrees.

```{r}
bgExt <- rgeos::gBuffer(bgExt, width = 0.5)
```

Mask environmental variables by Minimum Convex Polygon, and take a
random sample of background values from the study extent. As the sample
is random, your results may be different than those in the session. If
there seems to be too much variability in these background samples, try
increasing the number from 10,000 to something higher (e.g. 50,000 or
                                                       100,000). The better your background sample, the less variability you’ll
have between runs.

```{r}
# crop the environmental rasters by the background extent shape
envsBgCrop <- raster::crop(envs, bgExt)
# mask the background extent shape from the cropped raster
envsBgMsk <- raster::mask(envsBgCrop, bgExt)
# sample random background points
bg.xy <- dismo::randomPoints(envsBgMsk, 5000)
# convert matrix output to data frame
bg.xy <- as.data.frame(bg.xy)  
```

### Partition Occurrence Data

Occurrence data is now partitioned for cross-validation, a method that
iteratively builds a model on all but one group and evaluates that model
on the left-out group.

For example, if the data is partitioned into 3 groups A, B, and C, a
model is first built with groups A and B and is evaluated on C. This is
repeated by building a model with B and C and evaluating on A, and so on
until all combinations are done.

Cross-validation operates under the assumption that the groups are
independent of each other, which may or may not be a safe assumption for
your dataset. Spatial partitioning is one way to ensure more
independence between groups.

You selected to partition your occurrence data by the method.

```{r}
occs.xy <- occs[c('longitude', 'latitude')]
group.data <- ENMeval::get.block(occ=occs.xy, bg.coords=bg.xy)
```

```{r}
# pull out the occurrence and background partition group numbers from the list
occs.grp <- group.data[[1]]
bg.grp <- group.data[[2]]
```

### Build and Evaluate Niche Model

You selected the maxent model.

```{r}
# define the vector of regularization multipliers to test
rms <- seq(0.5, 2, 0.5)
# iterate model building over all chosen parameter settings
e <- ENMeval::ENMevaluate(occs.xy, envsBgMsk, bg.coords = bg.xy, RMvalues = rms, fc = c('L', 'LQ', 'H', 'LQH'), 
                          method = 'user', occs.grp, bg.grp, clamp = TRUE, algorithm = "maxnet")

# unpack the results data frame, the list of models, and the RasterStack of raw predictions
evalTbl <- e@results
evalMods <- e@models
names(evalMods) <- e@results$settings
evalPreds <- e@predictions

```

```{r}
# view ENMeval results
ENMeval::eval.plot(evalTbl, value = "delta.AICc")
```

```{r}
# Select your model from the models list
mod <- evalMods[["L_0.5"]]
```

```{r}
# generate logistic prediction
pred <- ENMeval::maxnet.predictRaster(mod, envsBgMsk, type = 'logistic', clamp = TRUE)
```

```{r}
# plot the model prediction
plot(pred)
```

results matching ""

    No results matching ""