model.matrix.earth: Get the earth basis matrix

Description Usage Arguments Value See Also Examples

View source: R/model.matrix.earth.R

Description

Get the basis matrix of an earth model.

Usage

1
2
3
4
5
6
7
## S3 method for class 'earth'
model.matrix(object = stop("no 'object' argument"),
    x = NULL, subset = NULL, which.terms = NULL,
    trace = 0,
    ...,
    Env = parent.frame(),
    Callers.name = "model.matrix.earth")

Arguments

object

An earth model. This is the only required argument.

x

Default is NULL, meaning use the original data used to build the earth model (after taking the original subset, if any).

Else x can be a data frame, a matrix, or a vector with length equal to a multiple of the number of columns of the original input matrix x. (There is some leniency here. For example, column names aren't necessary if x has the same number of predictors originally used to build the earth model.)

subset

Which rows to use in x. Default is NULL, meaning use all of x.

which.terms

Which terms to use. Default is NULL, meaning all terms in the earth model (i.e. the terms in object$selected.terms).

trace

Default 0. Set to non-zero to see which data model.matrix.earth is using.

...

Unused, but provided for generic/method consistency.

Env

For internal use.

Callers.name

For internal use (used by earth in trace messages).

Value

A basis matrix bx of the same form returned by earth. The format of bx is described in earth.object.

If x, subset, and which.terms are all NULL (the default), this function returns the model's bx. In this case, it is perhaps easier to simply use object$bx.

The matrix bx can be used as the input matrix to lm or glm, as shown below in the example. In fact, that is what earth does internally after the pruning pass — it calls lm.fit, and additionally glm if earth's glm argument is used.

See Also

earth

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
# Example 1

data(trees)
earth.mod <- earth(Volume ~ ., data = trees) # standard earth model
summary(earth.mod, decomp = "none")  # "none" to print terms in same order as lm.mod below
bx <- model.matrix(earth.mod)        # earth model's basis mat (equivalent to bx <- earth.mod$bx)
lm.mod <- lm(trees$Volume ~ bx[,-1]) # -1 to drop intercept
summary(lm.mod)                      # yields same coeffs as above summary
                                     # displayed t values are not meaningful

# Example 2

earth.mod <- earth(Volume~., data=trees) # standard earth model
summary(earth.mod, decomp = "none")  # "none" to print terms in same order as lm.mod below
bx <- model.matrix(earth.mod)        # earth model's basis mat (equivalent to bx <- earth.mod$bx)
bx <- bx[, -1]                       # drop intercept column
bx <- as.data.frame(bx)              # lm requires a data frame
bx$Volume <- trees$Volume            # add Volume to data
lm.mod <- lm(Volume~., data=bx)      # standard linear regression on earth's basis mat
summary(lm.mod)                      # yields same coeffs as above summary
                                     # displayed t values are not meaningful

Example output

Loading required package: Formula
Loading required package: plotmo
Loading required package: plotrix
Loading required package: TeachingDemos
Call: earth(formula=Volume~., data=trees)

              coefficients
(Intercept)     29.0599535
h(Girth-14.2)    6.2295143
h(14.2-Girth)   -3.4198062
h(Height-75)     0.5813644

Selected 4 of 5 terms, and 2 of 2 predictors
Termination condition: RSq changed by less than 0.001 at 5 terms
Importance: Girth, Height
Number of terms at each degree of interaction: 1 3 (additive model)
GCV 11.25439    RSS 209.1139    GRSq 0.959692    RSq 0.9742029

Call:
lm(formula = trees$Volume ~ bx[, -1])

Residuals:
    Min      1Q  Median      3Q     Max 
-4.9771 -2.1783  0.0834  1.3755  5.3712 

Coefficients:
                      Estimate Std. Error t value Pr(>|t|)    
(Intercept)            29.0600     1.0733  27.075  < 2e-16 ***
bx[, -1]h(Girth-14.2)   6.2295     0.3896  15.991 2.71e-15 ***
bx[, -1]h(14.2-Girth)  -3.4198     0.3315 -10.317 7.24e-11 ***
bx[, -1]h(Height-75)    0.5814     0.1590   3.657  0.00109 ** 
---
Signif. codes:  0***0.001**0.01*0.05.’ 0.1 ‘ ’ 1

Residual standard error: 2.783 on 27 degrees of freedom
Multiple R-squared:  0.9742,	Adjusted R-squared:  0.9713 
F-statistic: 339.9 on 3 and 27 DF,  p-value: < 2.2e-16

Call: earth(formula=Volume~., data=trees)

              coefficients
(Intercept)     29.0599535
h(Girth-14.2)    6.2295143
h(14.2-Girth)   -3.4198062
h(Height-75)     0.5813644

Selected 4 of 5 terms, and 2 of 2 predictors
Termination condition: RSq changed by less than 0.001 at 5 terms
Importance: Girth, Height
Number of terms at each degree of interaction: 1 3 (additive model)
GCV 11.25439    RSS 209.1139    GRSq 0.959692    RSq 0.9742029

Call:
lm(formula = Volume ~ ., data = bx)

Residuals:
    Min      1Q  Median      3Q     Max 
-4.9771 -2.1783  0.0834  1.3755  5.3712 

Coefficients:
                Estimate Std. Error t value Pr(>|t|)    
(Intercept)      29.0600     1.0733  27.075  < 2e-16 ***
`h(Girth-14.2)`   6.2295     0.3896  15.991 2.71e-15 ***
`h(14.2-Girth)`  -3.4198     0.3315 -10.317 7.24e-11 ***
`h(Height-75)`    0.5814     0.1590   3.657  0.00109 ** 
---
Signif. codes:  0***0.001**0.01*0.05.’ 0.1 ‘ ’ 1

Residual standard error: 2.783 on 27 degrees of freedom
Multiple R-squared:  0.9742,	Adjusted R-squared:  0.9713 
F-statistic: 339.9 on 3 and 27 DF,  p-value: < 2.2e-16

earth documentation built on Oct. 23, 2020, 5:08 p.m.