format.earth {earth}R Documentation

Format "earth" objects

Description

Return a string representing an earth expression.

Usage

## S3 method for class 'earth':
format(x = stop("no 'x' arg"),
       digits = getOption("digits"), use.names = TRUE,
       decomp = "anova",  style = "h", valid.names = FALSE, ...)

Arguments

x An earth object. This is the only required argument.
digits Number of significant digits. The default is getOption(digits).
use.names If TRUE (default), use variable names. Else use names of the form x[,1].
decomp One of
"anova" (default) order the terms using the "anova decomposition" i.e. in increasing order of interaction
"none" order the terms as created during the earth forward pass.
style Formatting style. One of
"h" (default) more compact
"pmax" for those who prefer it and for compatibility with old versions of earth.
valid.names If TRUE convert variable names to valid "C style" names by replacing invalid characters with an underscore (currently only replaces ":").
... Unused, but provided for generic/method consistency.

Value

A character representation of the earth object.
If there are multiple responses, format.earth will return multiple strings.
If there are embedded GLM model(s), the string(s) for the GLM model(s) follow the strings for the standard earth model(s).

Note

The FAQ section for earth has some comments on the "anova" ordering.

Using format.earth, perhaps after hand editing the returned string, you can create an alternative to predict.earth. For example:

as.func <- function(
               object, digits = 8, use.names = FALSE, ...)
  eval(parse(text=paste(
    "function(x)\n",
    "{\n",
    "if(is.vector(x))\n",
    "  x <- matrix(x, nrow = 1, ncol = length(x))\n",
    "with(as.data.frame(x),\n",
    format(object, digits = digits, use.names = use.names, style = "pmax", ...),
    ")\n",
    "}\n", sep = "")))
a <- earth(Volume ~ ., data = trees)
my.func <- as.func(a, use.names = FALSE)
my.func(c(10,80))     # yields 17.82973
predict(a, c(10,80))  # yields 17.82973, but is slower
The earth package also provides a function format.lm. It has arguments as follows
format.lm(x, digits=getOption("digits"), use.names=TRUE, valid.names=FALSE)
Strictly speaking, format.lm doesn't belong in the earth package. Example:
a <- lm(Volume ~ Height*Girth, data = trees)
cat(format(a, valid.names=TRUE))

# yields:
#    69.4
#    -  1.30 * Height
#    -  5.86 * Girth
#    + 0.135 * Height_Girth

See Also

earth, pmax,

Examples

a <- earth(Volume ~ ., data = trees)
cat(format(a))

# yields:
#    23.2
#    +  5.75 * h(Girth-12.9)
#    -  2.87 * h(12.9-Girth)
#    + 0.718 * h(Height-76)

cat(format(a, style="pmax")) # default formatting style prior to earth version 1.4

# yields:
#    23.2
#    +  5.75 * pmax(0,  Girth -   12.9)
#    -  2.87 * pmax(0,   12.9 -  Girth)
#    + 0.718 * pmax(0, Height -     76)

[Package earth version 2.0-2 Index]