| Title: | Floating Grid Permutation Technique |
|---|---|
| Description: | A permutation technique to explore and control for spatial autocorrelation. This package contains low level functions for performing permutations and calculating statistics as well as higher level functions. Higher level functions are an easy to use function for performing spatially restricted permutation tests and summarize and plot results. Compatibility updates by Renova. |
| Authors: | Reinder Radersma [aut, cre], Ben Sheldon [aut] |
| Maintainer: | Reinder Radersma <[email protected]> |
| License: | GPL |
| Version: | 2.3.9001 |
| Built: | 2026-11-19 06:38:11 UTC |
| Source: | https://github.com/renova-r/fgpt |
FGPT is a spatially restricted permutation technique. The package contains both low level functions to produce permuted datasets as well as higher level functions to perform spatially restricted permutation tests and functions to summarize and plot the results.
fgeasy is the single step version of the FGPT, which is most suitable for first time users. Please read the vignette for a more extensive discription of the method and functions.
Reinder Radersma & Ben Sheldon
Reinder Radersma & Ben C. Sheldon, 2015. A new permutation technique to explore and control for spatial autocorrelation. Submitted
The Floating Grid Permutation Technique is a spatially restricted permutation technique. Please read the reference mentioned below and/or the vignette before using this function. fgeasy can be used to perform single step permutation tests. This function can be used for detecting spatial autocorrelation for univariate observations (using Moran's I), pairwise observations (using a correlation coefficient) or multivariate observations for both single and pairwise sets of multivariate observations (using mean distance or relatedness).
fgeasy(xy, group=1, marks, iter=999, ratio=1, scale.seq=seq(from=0, to=max(dist(xy)), length.out=21)[2:21], bootstrap=FALSE, pairwise=FALSE, correlate=FALSE)fgeasy(xy, group=1, marks, iter=999, ratio=1, scale.seq=seq(from=0, to=max(dist(xy)), length.out=21)[2:21], bootstrap=FALSE, pairwise=FALSE, correlate=FALSE)
xy |
Geographical locations of observations. |
group |
Group membership for the observations. |
marks |
Should be a vector containing all observations, a two-column matrix containing two paired observations or a squared matrix containing relationships or distances between all observations (such as genetic relatedness or distance). The latter can be used to perform multivariate analyses. Note that the squared matrix does not need to be symmetrical: for instance, the genetic relatedness between paired individuals can be investigated by using males as columns and females as rows. |
iter |
Number of iterations for every grid cell size. Default is 999, though it is adviseable to perform 9999 interations for the final results. |
ratio |
The ratio between the sides of the grid cells. Default is 1. |
scale.seq |
Sequence of all grid cell sizes that will be tested. The number, order and values of this sequence are completely free, as long as they are numeric. Default is 20 equaly spaced out grid cell sizes from slightly larger than 0 to the maximum distance between any two individuals. |
bootstrap |
|
pairwise |
|
correlate |
If one wants to compare two paired observations correlations between the observations can be used. The argument |
fgeasy returns an object of class "fg", which can be accessed with the functions summary and plot.
Reinder Radersma
Reinder Radersma & Ben C. Sheldon, 2015. A new permutation technique to explore and control for spatial autocorrelation. Submitted
## A simple univariate example ## ## Produce 49 geographical locations in a regular grid. loc.x <- rep(1:7,7) loc.y <- rep(1:7, each=7) ## Produce 49 observations with negative spatial autocorrelation. marks <- c(rep(c(1,2), 24),1)+rnorm(49,0,0.1) fg1 <- fgeasy(xy=cbind(loc.x,loc.y), marks=marks, iter=99) summary(fg1) plot(fg1) ## An example for two paired marks ## ## Produce 20 geographical locations. loc.x <- 1:20 loc.y <- runif(20,0,5) ## Produce 2 x 20 phenotypes. type1 <- 11:30+runif(20,0,5) type2 <- 11:30+runif(20,0,5) fg2 <- fgeasy(xy=cbind(loc.x,loc.y), marks=cbind(type1,type2), iter=99, pairwise=TRUE, correlate="pearson") summary(fg2) plot(fg2)## A simple univariate example ## ## Produce 49 geographical locations in a regular grid. loc.x <- rep(1:7,7) loc.y <- rep(1:7, each=7) ## Produce 49 observations with negative spatial autocorrelation. marks <- c(rep(c(1,2), 24),1)+rnorm(49,0,0.1) fg1 <- fgeasy(xy=cbind(loc.x,loc.y), marks=marks, iter=99) summary(fg1) plot(fg1) ## An example for two paired marks ## ## Produce 20 geographical locations. loc.x <- 1:20 loc.y <- runif(20,0,5) ## Produce 2 x 20 phenotypes. type1 <- 11:30+runif(20,0,5) type2 <- 11:30+runif(20,0,5) fg2 <- fgeasy(xy=cbind(loc.x,loc.y), marks=cbind(type1,type2), iter=99, pairwise=TRUE, correlate="pearson") summary(fg2) plot(fg2)
The fgperm function produces sets of permuted observations or indices using the Floating Grid Permutation Technique, which is a method for spatially restricted permutations. fgploc offers additional functionality to manipulate observations within grid cells, for instance observations could be scaled with grid cells.
fgperm(xy,z=1:dim(xy)[1], scale, group=1, iter=999, ratio=1, FUN=fyshuffle, ..., add.obs=FALSE, as.matrix=FALSE) fgploc(xy, scale, group=1, iter=999, ratio=1, FUN=fyshuffle, FUN.mani, ..., marks, pass.rand=FALSE)fgperm(xy,z=1:dim(xy)[1], scale, group=1, iter=999, ratio=1, FUN=fyshuffle, ..., add.obs=FALSE, as.matrix=FALSE) fgploc(xy, scale, group=1, iter=999, ratio=1, FUN=fyshuffle, FUN.mani, ..., marks, pass.rand=FALSE)
xy |
Two-column matrix with the geographical locations of observations. |
z |
Vector with the observations. If left empty |
scale |
Value indicating the spatial scale of the permutations. |
group |
Optional group membership of observations. |
iter |
Number of iterations for every grid cell size. Default is 999. Note that in order to produce a probability an observation is assigned to any of the geographical locations is a negative function of the distance between its original and assigned location many iterations are needed. |
ratio |
The ratio between the sides of the grid cells. Default is 1. |
FUN |
Function to perform randomizations. Note that the function must be able to randomize one value, which is for instance a issue if using |
FUN.mani |
Function to perform manipulations of the observations within grid cells. This functionality should be used together with |
... |
Optional arguments to |
marks |
Should either be left empty, be a vector or a matrix. When |
add.obs |
If |
as.matrix |
If |
pass.rand |
If |
Before using those functions please read the reference or vignette. Alternatively use the more user-friendly function fgeasy. If there are missing values for the observations, leave z empty and enter the observations as marks in the fgstat function.
fgperm returns a list or a matrix, depending on the setting of argument as.matrix.
Reinder Radersma
Reinder Radersma & Ben C. Sheldon, 2015. A new permutation technique to explore and control for spatial autocorrelation. Submitted
## 200 random geographical locations xy <- array(runif(400,0,2), dim=c(200,2)) ## run fgperm to produce 99 randomizations for scale 1 test <- fgperm(xy, scale=1, iter=99, add.obs=TRUE) ## run fgperm to produce 99 bootstraps for scale 1 test <- fgperm(xy, scale=1, iter=99, FUN=function(x){ x[sample.int(length(x),replace=TRUE)]}, add.obs=TRUE) ## 200 times 200 random distances (e.g. genetic relatedness between mated pairs) trait <- array(rnorm(200*200,0.6,0.1), dim=c(200,200)) ## make the observed pairs more alike diag(trait) <- diag(trait)+0.02 ## make two rows and two colums empty trait[,3] <- NA trait[,50] <- NA trait[6,] <- NA trait[12,] <- NA ## calculate means; will give NAs because there are missing values calc <- fgstat(test,trait,mean) ## calculate means calc <- fgstat(test,trait,mean, na.rm=TRUE) ## plot means hist(calc) abline(v=calc[1], col="red", lwd=2)## 200 random geographical locations xy <- array(runif(400,0,2), dim=c(200,2)) ## run fgperm to produce 99 randomizations for scale 1 test <- fgperm(xy, scale=1, iter=99, add.obs=TRUE) ## run fgperm to produce 99 bootstraps for scale 1 test <- fgperm(xy, scale=1, iter=99, FUN=function(x){ x[sample.int(length(x),replace=TRUE)]}, add.obs=TRUE) ## 200 times 200 random distances (e.g. genetic relatedness between mated pairs) trait <- array(rnorm(200*200,0.6,0.1), dim=c(200,200)) ## make the observed pairs more alike diag(trait) <- diag(trait)+0.02 ## make two rows and two colums empty trait[,3] <- NA trait[,50] <- NA trait[6,] <- NA trait[12,] <- NA ## calculate means; will give NAs because there are missing values calc <- fgstat(test,trait,mean) ## calculate means calc <- fgstat(test,trait,mean, na.rm=TRUE) ## plot means hist(calc) abline(v=calc[1], col="red", lwd=2)
Calculates a set of values for a particular statistic or sets of observations, typically for observed values and multiple sets of randomized observations.
fgstat(rand,marks,FUN=mean, ...)fgstat(rand,marks,FUN=mean, ...)
rand |
A list for which the elements are either sets of randomized variables or randomized index values. |
marks |
Should either be left empty, be a vector or a matrix. When |
FUN |
Any function used to calculate the statistic of interest (e.g. |
... |
Optional arguments to |
fgstat is designed to calculate statistics for spatial explicit data for which randomized data sets are generated with fgperm.
fgstat returns a vector of statistics. If rand is the output of fgperm and add.obs=TRUE, the first value is the statistic for the observed data and the rest for randomizations.
Reinder Radersma
Reinder Radersma & Ben C. Sheldon, 2015. A new permutation technique to explore and control for spatial autocorrelation. Submitted
#### Example for fgrand ## 200 random geographical locations xy <- array(runif(400,0,2), dim=c(200,2)) ## run fgperm to produce 99 randomizations for scale 1 test <- fgperm(xy, scale=1, iter=99, add.obs=TRUE) ## run fgperm to produce 99 bootstraps for scale 1 test <- fgperm(xy, scale=1, iter=99, FUN=function(x){ x[sample.int(length(x),replace=TRUE)]}, add.obs=TRUE) ## 200 times 200 random distances (e.g. genetic relatedness between mated pairs) trait <- array(rnorm(200*200,0.6,0.1), dim=c(200,200)) ## make the observed pairs more alike diag(trait) <- diag(trait)+0.02 ## make two rows and two colums empty trait[,3] <- NA trait[,50] <- NA trait[6,] <- NA trait[12,] <- NA ## calculate means; will give NAs because there are missing values calc <- fgstat(test,trait,mean) ## calculate means calc <- fgstat(test,trait,mean, na.rm=TRUE) ## plot means hist(calc) abline(v=calc[1], col="red", lwd=2)#### Example for fgrand ## 200 random geographical locations xy <- array(runif(400,0,2), dim=c(200,2)) ## run fgperm to produce 99 randomizations for scale 1 test <- fgperm(xy, scale=1, iter=99, add.obs=TRUE) ## run fgperm to produce 99 bootstraps for scale 1 test <- fgperm(xy, scale=1, iter=99, FUN=function(x){ x[sample.int(length(x),replace=TRUE)]}, add.obs=TRUE) ## 200 times 200 random distances (e.g. genetic relatedness between mated pairs) trait <- array(rnorm(200*200,0.6,0.1), dim=c(200,200)) ## make the observed pairs more alike diag(trait) <- diag(trait)+0.02 ## make two rows and two colums empty trait[,3] <- NA trait[,50] <- NA trait[6,] <- NA trait[12,] <- NA ## calculate means; will give NAs because there are missing values calc <- fgstat(test,trait,mean) ## calculate means calc <- fgstat(test,trait,mean, na.rm=TRUE) ## plot means hist(calc) abline(v=calc[1], col="red", lwd=2)
Function to shuffle vectors according to the Fisher-Yates procedure
fyshuffle(x)fyshuffle(x)
x |
Vector containing the sequence which needs to be shuffled. This vector can be of any type and it is allowed to have one element. |
Other than the sample function fyshuffle treats a single value as a vector with one element and will therefore return this element as the shuffled version of the original vector (which are similar).
A randomized version of the input vector.
Reinder Radersma
Durstenfeld (1964) Communications of the ACM 7(7):420
x <- 1:10 fyshuffle(x) y <- c("a","b","c","d","e","f") fyshuffle(y)x <- 1:10 fyshuffle(x) y <- c("a","b","c","d","e","f") fyshuffle(y)
This is a simulated data set of geographical locations and body size measurements for two populations of fresh water Crustaceans. Both populations share the geographical locations and female phenotypes, but not the male phenotypes (m.pheno and m.pheno2 respectively). The first population does not show size assortative pairing, while the second does.
data(Gpulex)data(Gpulex)
An R data object containing one two-column matrix with geographical coordinates (Gp.xy) and three vectors with female (f.pheno) and male body size measurements (m.pheno and m.pheno2).
Code to produce this data set can be found in electronic supplementary materials of the following reference.
Reinder Radersma & Ben C. Sheldon, 2015. A new permutation technique to explore and control for spatial autocorrelation. Submitted
This is a simulated data set of geographical locations and relatedness measures for two populations of Great Tits, a small passerine. Both populations share the geographical locations, but not the relatedness tables. The first population does not show inbreeding avoidance, while the second does.
data(Pmajor)data(Pmajor)
An R data object containing one two-column matrix with geographical coordinates (xy) and two relatedness tables between all females and males of the populations (rel1 and rel2 respectively).
Code to produce this data set can be found in electronic supplementary materials of the following reference.
Reinder Radersma & Ben C. Sheldon, 2015. A new permutation technique to explore and control for spatial autocorrelation. Submitted