Search code examples
rattrvegan

navigating through complex r-objects with attr


I am performing a multivariate redundancy analysis in vegan with forward selection. At one point i want to extract the significant terms in the reduced model to form the right hand side of a new model. I know where these terms are located in the output of the function, but i fail to extract them:

library(vegan)
data(dune)
data(dune.env)
mod0 <- rda(dune ~ 1, dune.env)  # Model with intercept only
mod1 <- rda(dune ~ ., dune.env)  # Model with all explanatory variables
obj <- ordistep(mod0, scope = formula(mod1))

The significant model variables are here:

obj$terminfo$terms ## or obj$terms
# dune ~ Management + Moisture **<------ this i need**
# attr(,"variables")
# list(dune, Management, Moisture)
# attr(,"factors")
# Management Moisture
# dune                0        0
# Management          1        0
# Moisture            0        1
# attr(,"term.labels")
# [1] "Management" "Moisture"  **<------ alternatively this**
# attr(,"order")
# [1] 1 1
# attr(,"intercept")
# [1] 1
# attr(,"response")
# [1] 1
# attr(,".Environment")
# <environment: R_GlobalEnv

I tried different approaches with $,[[]], attr/which, and [], but failed. In the end, i would to create a vector: rhs <- paste(model.terms, collapse = " + ").

How would one extract the terms located in the given object?


Solution

  • terms(obj) will extract terms, formula(obj) will extract the formula, and update() can be used update the ordination result object. For instance, formula can be changed with update().