Title: | Modeling and Plotting for Ecologist |
Version: | 0.1.0 |
Description: | It provides multiple functions that are useful for ecological research and teaching statistics to ecologists. It is based on data analysis courses offered at the Instituto de Ecología AC (INECOL). For references and published evidence see, Manrique-Ascencio, et al (2024) <doi:10.1111/gcb.17282>, Manrique-Ascencio et al (2024) <doi:10.1111/plb.13683>, Ruiz-Guerra et al(2017) <doi:10.17129/botsci.812>, Juarez-Fragoso et al (2024) <doi:10.1007/s10980-024-01809-z>, Papaqui-Bello et al (2024) <doi:10.13102/sociobiology.v71i2.10503>. |
Date: | 2025-05-10 |
Depends: | R (≥ 3.5.0) |
Imports: | ape, betapart, boot, cli, gmodels, gtools, multcomp, stringr, terra, tree, vegan |
License: | GPL-3 |
Encoding: | UTF-8 |
NeedsCompilation: | no |
Packaged: | 2025-05-15 01:34:21 UTC; rogerguevara |
Author: | Roger Guevara [aut, cre] |
Maintainer: | Roger Guevara <roger.guevara@inecol.mx> |
Repository: | CRAN |
Date/Publication: | 2025-05-19 13:20:06 UTC |
A presence/absebce matrux
Description
Sites (cells) are in the orws and columns are species. Species names must match those in PHYLO
Usage
data("PAM")
Format
A data frame with 1000 observations on the following 43 variables.
Specis_1
a numeric vector
Specis_2
a numeric vector
Specis_3
a numeric vector
Specis_4
a numeric vector
Specis_5
a numeric vector
Specis_6
a numeric vector
Specis_7
a numeric vector
Specis_8
a numeric vector
Specis_9
a numeric vector
Specis_10
a numeric vector
Specis_11
a numeric vector
Specis_12
a numeric vector
Specis_13
a numeric vector
Specis_14
a numeric vector
Specis_15
a numeric vector
Specis_16
a numeric vector
Specis_17
a numeric vector
Specis_18
a numeric vector
Specis_19
a numeric vector
Specis_20
a numeric vector
Specis_21
a numeric vector
Specis_22
a numeric vector
Specis_23
a numeric vector
Specis_24
a numeric vector
Specis_25
a numeric vector
Specis_26
a numeric vector
Specis_27
a numeric vector
Specis_28
a numeric vector
Specis_29
a numeric vector
Specis_30
a numeric vector
Specis_31
a numeric vector
Specis_32
a numeric vector
Specis_33
a numeric vector
Specis_34
a numeric vector
Specis_35
a numeric vector
Specis_36
a numeric vector
Specis_37
a numeric vector
Specis_38
a numeric vector
Specis_39
a numeric vector
Specis_40
a numeric vector
Specis_41
a numeric vector
Specis_42
a numeric vector
Specis_43
a numeric vector
Examples
data(PAM)
Phylogenetic tree
Description
A rooted phylogenetic tree with branch lengths. Tip names must math those in PAM's columns
Usage
data("PHYLO")
Format
The format is: List of 4 $ edge : int [1:84, 1:2] 44 45 46 47 48 49 50 50 51 52 ... $ edge.length: num [1:84] 26.2 81.9 32.6 26.7 100.1 ... $ Nnode : int 42 $ tip.label : chr [1:43] "Specis_43" "Specis_28" "Specis_26" "Specis_27" ... - attr(*, "class")= chr "phylo" - attr(*, "order")= chr "cladewise"
Examples
data(PHYLO)
## maybe str(PHYLO) ; plot(PHYLO) ...
Functional traits of species
Description
Matrix of five functional traits (columns) of species (rows). Species names must match those in PAM
Usage
data("TRAITS")
Format
A data frame with 43 observations on the following 5 variables.
FT1
a numeric vector
FT2
a numeric vector
FT3
a numeric vector
FT4
a numeric vector
FT5
a numeric vector
Examples
data(TRAITS)
Beta Base on Complementarity of Ecological Communities
Description
Estimate beta diversity based on the Bray-Curtis index estiumates of complementarity (an assymetric meassures) betweb pairs of ecological communities.
Usage
betacomp(M)
Arguments
M |
A numeric matrix with at least two columns (sites) and r rows (species). |
Value
Return a list of matrixes of the complementarities between pairs of communities and the beta diversity between them.
Author(s)
Guevara, R., roger.guevara@inecol.mx
References
Aguirre, A., Guevara, R., García, M. and López, J.C. (2010), Fate of epiphytes on phorophytes with different architectural characteristics along the perturbation gradient of Sabal mexicana forests in Veracruz, Mexico. Journal of Vegetation Science, 21: 6-15. https://doi.org/10.1111/j.1654-1103.2009.01131.x
Examples
library(vegan)
data(dune)
betacomp(t(dune[1:4,]))
Functional Beta Biversity for MacroEcology
Description
It estimates the phylogenetic beta diversity of focal cells within a SpatVector object by considering neighboring cells located within a specified buffer.
Usage
fbeta4me(PAM, TRAITS, SHP, width = 10000, index.family="sorensen", verbose=TRUE)
Arguments
PAM |
A presence/absence matrix where sites are organized in rows and species in columns. |
TRAITS |
A species x Traits matrix or a PCoA scores of functional traits. The row labels must exactly match the species names in the presence/absence matrix (PAM). |
SHP |
A SpatVector object representing cells distributed over a geographic space |
width |
Refers to the radius of the buffer. If the SpatVector object uses a latitude/longitude projection, the width is specified in meters. |
index.family |
Choose either the 'sorensen' or 'jaccard' dissimilarity index. |
verbose |
Logical, indicating whether messages and the progress bar are shown. The default is TRUE. |
Value
The function returns a data frame that includes turnover, nestedness, total beta diversity, and the number of cells contributing to the mean diversity of the focal cell. The IDs of the cells are used as the row names of the data frame.
Author(s)
Guevara, R., roger.guevara@inecol.mx
References
Yang, J., Swenson, N. G., Zhang, G., Ci, X., Cao, M., Sha, L., ... & Lin, L. (2015). Local-scale partitioning of functional and phylogenetic beta diversity in a tropical tree assemblage. Scientific Reports, 5(1), 12731.
Examples
data(TRAITS)
data(PAM)
SHP <- terra::vect(paste(system.file("extdata", package = "inecolr"),
"/test.shp", sep=""))
SHP <- SHP[1:10]
f_beta <- fbeta4me(PAM, TRAITS, SHP, width = 10000, index.family="sorensen")
head(f_beta)
data(TRAITS)
data(PAM)
SHP <- terra::vect(paste(system.file("extdata", package = "inecolr"),
"/test.shp", sep=""))
SHP <- SHP[1:10]
f_beta <- fbeta4me(PAM, TRAITS, SHP, width = 10000, index.family="sorensen", verbose=FALSE)
head(f_beta)
Homocedasticity test
Description
Test of homocedasticity for lm glm and glmer models based on the trend of the squared residuals as a function of the fitted values
Usage
hcsrt(M)
Arguments
M |
A lm, glm or glmer model. |
Value
Returns the ANOVA table for the test. Nonsignificant effects mean there is no evidence to reject the null hypothesis that variability is the same across the fitted values range.
Author(s)
Guevara, R., roger.guevara@inecol.mx
References
Rohlf, F. (1981). Biometry the principles and practice of statistics in biological research. Zar, J. H. (1999). Biostatistical analysis. Pearson Education India.
Examples
y <- sort(rnorm(20, 5, 2))
x <- c(1:20)
M <- lm(y~x)
hcsrt(M)
Kolmogrov-Smirnov model's Residuals Normality Test
Description
Performed a normality test of the model residuals agains a randon normal sample with as many data as the length of the residuals, mean zero and standar deviation equals to that of the residuals
Usage
ks.rnorte(M, quantiles=TRUE)
Arguments
M |
M, a linear or glm model. |
quantiles |
quantiles, logical weather the test is based on quentiles rathern that raw data, defualt is TRUE. |
Value
Returs the ks.test. No significant effect means that residuals behave as a normal variable
Author(s)
Guevara, R., roger.guevara@inecol.mx
References
Rohlf, F. (1981). Biometry the principles and practice of statistics in biological research. Zar, J. H. (1999). Biostatistical analysis. Pearson Education India.
Examples
x <- c(1,3,5,7,9, 11)
y<- c(3.3, 5.8, 10, 12, 16, 17.8)
M <- lm(y~x)
ks.rnorte(M)
Mean and error intervals
Description
Mean and error intervals, either represent the mean values as a character (e.g., dot, solid square, etc.) or the height of bars. Error intervals are back-transformed from regular link functions included in <<glm>>, but the user can customize the back transformation by introducing a function in the form of, <<function(x)log1p(x)>>
Usage
meanerrba(means, error, link=NA, backtrans=NULL, bars= FALSE,
legend.text=FALSE, ylab="Mean and error", xlab="Groups", main="", pch=19,
cex = 1, cex.pt=1, cex.axis = 1, cex.lab = 1, cex.main = 2, col= "grey",
border= "grey30", density=NULL, angle=NULL, lwd=1, axis.lty=0,
names.arg = colnames(means), las = 1, ylim = FALSE, axes = TRUE, xpd = FALSE,
add = FALSE)
Arguments
means |
A vector or matrix with mean values. If a matrix, each column of values is grouped in the figure |
error |
A vector or matrix with error values. If a matrix, each column of values is grouped in the figure |
link |
Specified the scale of the linear predictor used to calculate the mean and error values and apply the corresponding back transformation in the plot. |
backtrans |
Define the customized back transformation if needed. |
bars |
Logical values to whether bars should be represented in the plot. The default option is FALSE, and mean values are represented with character, e.g., dot, open square, etc., as defined with the <<pch>> argument. |
legend.text |
Optional when bars=TRUE: A character vector to ID bars in the groups. The length of the vector must match the number of rows in the means matrix or the length of the means vector. |
ylab |
Label for the y-axis. |
xlab |
Label for the x-axis. |
main |
Label for the heading of the figure. |
pch |
Character to represent the mean values when teh bars argumen is FALSE. |
cex |
Character expansion factor for the names of the groups. |
cex.pt |
Expansion factor for the character representing the mean value. |
cex.axis |
Expansion factor for the axis values. |
cex.lab |
Expansion factor for the axis labels. |
cex.main |
Expansion factor for the heading of the figure. |
col |
A vector for the color of the elements in each group of means. |
border |
A vector for the color of the border when bars are displayed. |
density |
Numeric vector for the densitity of lines of the bars. |
angle |
Numeric vector for the angle of lines of the bars. |
lwd |
Numeric vector for the width of lines of error intervals. Also affects the lines of characters as in function points(). |
axis.lty |
Numeric vector for the line type of the x-axis. |
names.arg |
Character vectors are used to name the groups of means taken as the column names of the means matrix or names of means vector elements. When names are not available, they take consecutive capital letter values. |
las |
Integer value (0 to 1) to fix the orientation of values in the y-axis. |
ylim |
Limits of the y-axis, a vector of two elements for the lower and upper limits of the axis, respectively. |
axes |
Logical, wheather the y-axis should appear in the plot. Default value is TRUE |
xpd |
Logical, wheather bars are expanded beyond the ylim. |
add |
Logical, wheather mean and error bars should be added to an existing plot. Default option is FALSE and can not be change for now. |
Value
No values returned.
Author(s)
Guevara, R., roger.guevara@inecol.mx
References
Rohlf, F. (1981). Biometry the principles and practice of statistics in biological research. Zar, J. H. (1999). Biostatistical analysis. Pearson Education India.
Examples
set.seed(42)
means <- matrix(rpois(6, 10), 2, 3)
error <- matrix(runif(6)*2, 2, 3)
meanerrba(means,error,col=c("red", "blue", "purple"), ylim=c(6, 18), xlab="")
abline(h=6, lwd=2)
legend("topright", c("2024", "2024", "2025"), col=c("red", "blue", "purple"),
bty="n", pch=19, ncol=3)
set.seed(42)
means <- matrix(runif(6)*sample(c(1,-1),size=6, replace=TRUE), 2, 3)
error <- matrix(runif(6), 2, 3)/5
meanerrba(t(means),t(error), link="logit", ylim=c(0.2, 0.8),
names=paste("Group",1:3, sep="-"), col=c("blue", "purple"), xlab="")
abline(h=0.2, lwd=2)
legend("topleft", c("Condition A", "Condition B"), col=c("blue", "purple"),
bty="n", pch=c(19,15))
set.seed(42)
means <- matrix(runif(6)*sample(c(1,-1),size=6, replace=TRUE), 2, 3)
error <- matrix(runif(6), 2, 3)/5
meanerrba(t(means),t(error), link="logit", ylim=c(0.2, 0.8),
names=paste("Group",1:3, sep="-"), col="blue", pch=c(0,5), xlab="")
legend("topleft", c("2024", "2025"), col="blue", bty="n", pch=c(0,5))
set.seed(42)
means <- matrix(runif(6)*sample(c(1,-1),size=6, replace=TRUE), 2, 3)
error <- matrix(runif(6), 2, 3)/5
meanerrba(t(means),t(error), link="logit", bars=TRUE, col=c("navy", "gold2"),
ylim=c(0.2, 0.8), xlab="")
legend("topleft", c("2024", "2025"), fill=c("navy", "gold2"), bty="n")
Phylogenetic Beta Biversity for MacroEcology
Description
It estimates the phylogenetic beta diversity of focal cells within a SpatVector object by considering neighboring cells located within a specified buffer.
Usage
pbeta4me(PAM, PHYLO, SHP, width = 10000, index.family="sorensen", verbose =TRUE)
Arguments
PAM |
A presence/absence matrix where sites are organized in rows and species in columns. |
PHYLO |
A phylogenetic rooted tree, which includes branch lengths, is required. The tip labels must exactly match the species names in the presence/absence matrix (PAM). |
SHP |
A SpatVector object representing cells distributed over a geographic space |
width |
Refers to the radius of the buffer. If the SpatVector object uses a latitude/longitude projection, the width is specified in meters. |
index.family |
Choose either the 'sorensen' or 'jaccard' dissimilarity index. |
verbose |
Logical, indicating whether messages and the progress bar are shown. The default is TRUE. |
Value
The function returns a data frame that includes turnover, nestedness, total beta diversity, and the number of cells contributing to the mean diversity of the focal cell. The IDs of the cells are used as the row names of the data frame.
Author(s)
Guevara, R., roger.guevara@inecol.mx
References
Graham, C. H., & Fine, P. V. (2008). Phylogenetic beta diversity: linking ecological and evolutionary processes across space in time. Ecology letters, 11(12), 1265-1277.
Examples
data(PHYLO)
data(PAM)
SHP <- terra::vect(paste(system.file("extdata", package = "inecolr"),
"/test.shp", sep=""))
SHP <- SHP[1:10]
pb_beta <- pbeta4me(PAM, PHYLO, SHP, width = 10000, index.family="sorensen")
head(pb_beta)
data(PHYLO)
data(PAM)
SHP <- terra::vect(paste(system.file("extdata", package = "inecolr"),
"/test.shp", sep=""))
SHP <- SHP[1:10]
pb_beta <- pbeta4me(PAM, PHYLO, SHP, width = 10000, index.family="sorensen",
verbose=FALSE)
head(pb_beta)
Random Forest
Description
Create the most pasimonious groupping hipothesis based on N classification trees (the forest)
Usage
ranfor(data, repetitions=1000, prop=0.85, n.digits = 5, plot=TRUE)
Arguments
data |
A dataframe where the first column is the response (dependent) variable and the rest serve to classify |
repetitions |
Number of iterations |
prop |
Proportion of rows included in each iteration |
n.digits |
N digits to display in the summary table |
plot |
Logical, wheather to plat the result, defualt is TRUE |
Value
Retur a matrix ... .
Author(s)
Guevara, R., roger.guevara@inecol.mx
References
Rohlf, F. (1981). Biometry the principles and practice of statistics in biological research. Zar, J. H. (1999). Biostatistical analysis. Pearson Education India.
Examples
data("iris")
ranfor(iris[,c(5,1:4)])
Stadard Error
Description
Estimate the stadard error of a numeric or integer vector or colums in a matrix.
Usage
se(x)
Arguments
x |
A numeric or integer vector or matrix. |
Value
retur satandar error values based on th formula se = sqrt(var(x)/length(x)).
Author(s)
Guevara, R., roger.guevara@inecol.mx
References
Rohlf, F. (1981). Biometry the principles and practice of statistics in biological research. Zar, J. H. (1999). Biostatistical analysis. Pearson Education India.
Examples
set.seed(42)
data <- matrix(round(rnorm(6, 10, 3),2), 2, 3)
colnames(data)<- LETTERS[1:3]
se <- se(data)
se
Model simplification
Description
Simplifies models based on the residual sum of squares
Usage
simpmod(M, alpha=0.05)
Arguments
M |
A lm, glm. |
alpha |
Crital value to reject the null hypothis when less than or equal to 0.05, the default value.. |
Value
Returns a list of simplification steps up to the sipliest model in a list object.
Author(s)
Guevara, R., roger.guevara@inecol.mx
References
Crawley, the R book
Examples
y <- sort(rnorm(20, 5, 2))
x <- c(1:20)
M <- lm(y~x)
simpmod(M)
Taxonomic Beta Biversity for MacroEcology
Description
It estimates the taxonomic beta diversity of focal cells within a SpatVector object by considering neighboring cells located within a specified buffer.
Usage
tbeta4me(PAM, SHP, width = 10000, index.family="sorensen", verbose=TRUE)
Arguments
PAM |
A presence/absence matrix where sites are organized in rows and species in columns. |
SHP |
A SpatVector object representing cells distributed over a geographic space |
width |
Refers to the radius of the buffer. If the SpatVector object uses a latitude/longitude projection, the width is specified in meters. |
index.family |
Choose either the 'sorensen' or 'jaccard' dissimilarity index. |
verbose |
Logical, indicating whether messages and the progress bar are shown. The default is TRUE. |
Value
The function returns a data frame that includes turnover, nestedness, total beta diversity, and the number of cells contributing to the mean diversity of the focal cell. The IDs of the cells are used as the row names of the data frame.
Author(s)
Guevara, R., roger.guevara@inecol.mx
References
Yang, J., Swenson, N. G., Zhang, G., Ci, X., Cao, M., Sha, L., ... & Lin, L. (2015). Local-scale partitioning of functional and phylogenetic beta diversity in a tropical tree assemblage. Scientific Reports, 5(1), 12731.
Examples
data(PAM)
SHP <- terra::vect(paste(system.file("extdata", package = "inecolr"),
"/test.shp", sep=""))
SHP <- SHP[1:10]
t_beta <- tbeta4me(PAM, SHP, width = 10000, index.family="sorensen")
head(t_beta)
data(PAM)
SHP <- terra::vect(paste(system.file("extdata", package = "inecolr"),
"/test.shp", sep=""))
SHP <- SHP[1:10]
t_beta <- tbeta4me(PAM, SHP, width = 10000, index.family="sorensen", verbose=FALSE)
head(t_beta)