adeverse / ade4

Analysis of Ecological Data : Exploratory and Euclidean Methods in Environmental Sciences
http://adeverse.github.io/ade4/
39 stars 10 forks source link

RLQ with input from dudi.pco #42

Closed TizianaDrz closed 3 months ago

TizianaDrz commented 3 months ago

Hello, first of all thank you so much for creating this package!

I wondered if I could use the rlq() function when the Q matrix(dudiQ) is computed dudi.pco as follow:

library(ade4)
data(aravo)

all(row.names(aravo$spe)==row.names(aravo$env))
all(colnames(aravo$spe)==row.names(aravo$traits))

afcL.aravo <- dudi.coa(aravo$spe, scannf = FALSE)
acpR.aravo <- dudi.hillsmith(aravo$env, row.w = afcL.aravo$lw,
                             scannf = FALSE)

library(gawdis)
traits <- aravo$traits
dmatrix <- gawdis(traits, w.type= "equal",groups= c(1,1,1,2,3,4,5,6))
dmatrix

#dudi.pco function dudi.pco function should be restricted to Euclidean distance matrices.
#therefore we first check if the matrix is Euclidian and if not we apply the possible correction 
is.euclid(dmatrix)
ldmatrix<- lingoes(dmatrix)
is.euclid(ldmatrix)

acpQ.aravo <- dudi.pco(ldmatrix,row.w = afcL.aravo$cw,
                       scannf=F)

rlq.aravo <- rlq(acpR.aravo, afcL.aravo, acpQ.aravo,
                 scannf = FALSE)
# Order the indices of the trait scores on the first axis
t1 <- order(rlq.aravo$c1[, 1], decreasing = TRUE)

# Extract the ordered scores
ordered_scores_axis1 <- rlq.aravo$c1[t1, 1]

# Extract the corresponding trait names using the ordered indices
ordered_trait_names_axis1 <- names(traits)[t1]
print(ordered_trait_names_axis1)

it works in the sense that I get the rlq() function to provide a result but I get quite a lot of non-named NA axis in the rlq.aravo$c1 extraction. It should have the dimension of the number of traits instead of the dimension of pcoa.trait$rank.

I was wondering if this is an error of this input is not suited for rlq() function

sdray commented 3 months ago

Hi

not sure to understand what is the problem. If you apply pco, it means that you lose the variables and you produce a species x principal coordinates table that will be analyzed in RLQ instead of the species x traits. So the results seems ok:

dim(rlq.aravo$c1) [1] 80 2

head(rlq.aravo$c1) CS1 CS2 A1 -0.93837963 0.001270092 A2 0.06160564 0.669167582 A3 0.16831712 -0.530760846 A4 -0.02587350 -0.029025219 A5 0.04085174 -0.050506835 A6 -0.02488712 -0.096790007

dim(acpQ.aravo$tab) [1] 82 80

TizianaDrz commented 2 months ago

Hi, sorry to bother again. So this last line of code where I rename the output with the variables is NOT correct, right?

# Extract the ordered scores
ordered_scores_axis1 <- rlq.aravo$c1[t1, 1]

# Extract the corresponding trait names using the ordered indices
ordered_trait_names_axis1 <- names(traits)[t1]

I am having a hard time interpreting the results when the results refers to Axis and not traits.

sdray commented 2 months ago

If you apply pco, it meaans that you compute distances and so lose variables. If you want to keep traits you should run pca or other method than runs on raw data not distances.

TizianaDrz commented 2 months ago

I see, but that is not possible as I use Gower distances because I have mixed traits (continuous, binary, fuzzy and so on). But maybe to interpret the results of the RLQ I can first understand the species x principal coordinates table and relate it to the original traits. Thank for your answers.