
Position of a state with respect to a trajectory
Source:R/state_to_trajectory.R
state_to_trajectory.RdDefine the position of a state with respect to a reference trajectory based on its distance from the trajectory and the length and direction of the trajectory.
Usage
state_to_trajectory(
d,
trajectories,
states,
target_states,
reference,
method,
coordStates = NULL
)Arguments
- d
Either a symmetric matrix or an object of class
distcontaining the dissimilarities between each pair of states.- trajectories
Vector indicating the trajectory or site to which each state in
dbelongs.- states
Vector of integers indicating the order of the states in
dfor each trajectory (assign 1 if the state does not belong to any trajectory).- target_states
Vector of integers indicating the indices in
trajectoriesandstatesof the ecological states for which their relative position will be calculated.- reference
Vector of the same class of
trajectoriesor object of classRETRAindicating the reference trajectory to calculate the relative position of thetarget_states.- method
Method to calculate the distance and relative position of the
target_statesand thereference. One of"nearest_state"or"projection"(see Details).- coordStates
Matrix containing the coordinates of each state (rows) and axis (columns) of a metric ordination space (see Details).
Value
The function state_to_trajectory() returns a data frame of four columns
including the distance and relative_position between the target_state and
the reference.
Depending on the
method,distanceis calculated as the dissimilarity between thetarget_statesand their respective nearest state in thereferenceor the dissimilarity to their projections onto thereference.The
relative_positionis a value that ranges between 0 (if the nearest state or projected point coincides with the firstreferencestate) and 1 (if the nearest state or projected point coincides with the lastreferencestate).
Details
state_to_trajectory() can calculate the distance and relative position of
one or more target_states relative to a reference trajectory by two
different methods:
"nearest_state"returns the dissimilarity of thetarget_statesto the nearest state of thereferencetrajectory (distance) and calculates the relative position of the nearest state within thereference."projection"returns the dissimilarity of thetarget_statesto their projection onto thereferencetrajectory and calculates the relative position of the projected state within thereference. When thetarget_statescannot be projected onto any of the segments forming thereferenceand in cases in which the dissimilarity to nearest state of thereferenceis smaller than the dissimilarity to the projected state,state_to_trajectory()uses the nearest state in thereferenceto computedistanceandrelative_position. This method requiresdto be metric (i.e. to satisfy the triangle inequality). Ifdis not metric,state_to_trajectory()calculates the Euclidean distance within a transformed space generated through multidimensional scaling (Borg and Groenen, 2005). To use the state coordinates in a different metric space, use thecoordStatesargument.
Examples
# State dissimilarities
d <- vegan::vegdist(EDR_data$EDR3$abundance[, paste0("sp", 1:12)], method = "bray")
trajectories <- EDR_data$EDR3$abundance$traj
states <- EDR_data$EDR3$abundance$state
# Calculate the representative trajectories of an EDR to be used as reference
RT <- retra_edr(d,
trajectories = trajectories,
states = states,
minSegs = 10)
# Define the target states
target_states <- as.integer(c(1, 16, 55))
# Calculate the position of the target states with respect to the representative
# trajectories of an EDR
state_to_trajectory(d, trajectories = trajectories,
states = states,
target_states = target_states,
reference = RT,
method = "nearest_state")
#> target_state reference distance relative_position
#> 1 1 T1 0.04522613 0.0000000
#> 2 1 T2 0.75879397 0.3418326
#> 3 16 T1 0.07537688 0.7195150
#> 4 16 T2 0.07537688 0.3418326
#> 5 55 T1 0.04477612 0.2328098
#> 6 55 T2 0.58000000 0.3418326