Learn R Programming

fgpt (version 2.3)

fgperm: Floating Grid Permutation Technique

Description

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.

Usage

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)

Arguments

xy
Two-column matrix with the geographical locations of observations.
z
Vector with the observations. If left empty z will be a vector of indices from 1 to the total number of geographical locations. Those indices can be later be used for calculating statistics with fgstat.
scale
Value indicating the spatial scale of the permutations. scale should be positive.
group
Optional group membership of observations. group can be used when observations are for instance collected over multiple years.
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 sample. For solutions see the Details and Examples of sample. The default is the Fisher-Yates shuffle fyshuffle.
FUN.mani
Function to perform manipulations of the observations within grid cells. This functionality should be used together with cal.stat to calculate specific statistics.
...
Optional arguments to FUN and FUN.mani.
marks
Should either be left empty, be a vector or a matrix. When z contains observations marks should be left empty. If the randomization procedure is for testing one variable, marks should be a vector for which the row numbers correspond to the index values in z. If the randomization procedure is for testing two variables (for instance the distance between them), marks should be a matrix for which the row numbers correspond to the index values in z.
add.obs
If TRUE the first set in the output will be the observed values.
as.matrix
If TRUE the output will be a matrix. If FALSE the output will be a list, which is needed when analyzing the data with cal.stat.
pass.rand
If TRUE The sets of randomized observations are also passed on to the output. Default is FALSE.

Value

fgperm returns a list or a matrix, depending on the setting of argument as.matrix.

Details

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.

References

Reinder Radersma & Ben C. Sheldon, 2015. A new permutation technique to explore and control for spatial autocorrelation. Submitted

Examples

Run this code

## 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)
  

Run the code above in your browser using DataLab