-
Notifications
You must be signed in to change notification settings - Fork 97
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Question: Is it possible to rebuild the original input data from an rda object? #514
Comments
Yes, you can do it, but there is no direct vegan function to do so. Functions Xbar <- Xbar * sqrt(nrow(Xbar) - 1)
Xbar <- sweep(Xbar, 2, cent, "+") So you have to undo the scaling of data in RDA (which we do to be consistent with The following is the slow way: library(vegan)
data(dune, dune.env)
m <- rda(dune ~ Manure, dune.env)
f <- fitted(m, type = "working")
r <- residuals(m, type = "working")
fr <- f+r
cnt <- attr(fr, "scaled:center")
fr <- sqrt(nrow(fr) - 1) * fr
range(sweep(fr, 2, cnt, "+") - dune)
# [1] -2.220446e-15 2.664535e-15 Floating point calculations are not numerically exact, but you got within 2.66 × 10-15 of original values. You can shortcut this by getting directly the sum of working matrices: fr <- ordiYbar(m, "initial")
sweep(sqrt(nrow(fr)-1) * fr, 2, attr(fr, "scaled:center"), "+") which is numerically more stable (max 4.44 × 10-16) as it avoids adding of rounded floating point numbers f <- fitted(m) # involves centring and scaling up
r <- residuals(m, type="working")
f + sqrt(nrow(r)-1) * r This avoids de-centring twice, but you need to scale up the residuals. |
Super cool - speed is not an issue for me in this case, so this is a very elegant solution. Is there a similar way to obtain the input data if rda was called with scale=TRUE? |
Yes, you can rebuild the input data after |
Resurrecting this, since I have a very similar problem with CCA! based on @jarioksa's excellent response above, this is what I ended up using:
Then a PCA can be run on the rebuild original data for the corresponding unconstrained analysis:
Is it possible to do the same with a CCA object from |
@MalteThodberg : yes, it is possible. It is even easier to reconstitute input data in CCA than for RDA. There are many ways, depending on how close to primitives you want to go. Here a method that is based on ## Preliminaries for all examples
library(vegan)
data(dune, dune.env)
m <- cca(dune ~ Manure, dune.env)
## get marginal weights and matrix grand total
rc <- outer(m$rowsum, m$colsum) # outer product of marginal weights
tot <- m$grand.total
## Method 1: use predict
xbar <- predict(m, model="CCA", type="working") + predict(m, model="CA", type="working")
(xbar * sqrt(rc) + rc) * tot If you want to get closer to the primitives, you can get each component of ## Method 2: use result scores and eigenvalues
xbar_CCA <- m$CCA$u %*% diag(sqrt(m$CCA$eig)) %*% t(m$CCA$v)
xbar_CA <- m$CA$u %*% diag(sqrt(m$CA$eig)) %*% t(m$CA$v)
xbar <- xbar_CCA + xbar_CA
(xbar + 1) * rc * tot Finally, if you only want to reconstruct the initial input and you do not want to go to the basics, the easiest way is to extract the internal form with ## Method 3: Use internal working data
(ordiYbar(m, "initial") * sqrt(rc) + rc) * tot |
If i have an rda-object from the rda() function, it is possible to get out the orignial input data? Following the example from the rda() documentation:
Now I would expect that
r + f
would give the original inputdune
, but that doesn't seem the be case:Is there a way a way to re-estimate the original input data from the rda-object?
The text was updated successfully, but these errors were encountered: