LGTrader
LGTrader

Reputation: 2429

Getting the 'model' out of Earth

Sorry for the lousy title. Not sure how I should phrase it.

I'm playing around with the Earth package to see about regressing a neural network signal using more or less standard indicators. The data file is 1000's of rows and currently 187 columns (186 indicator results) with my target variable in the last column. The code I wrote is very simple, and leaves out any in-sample vs out-of-sample issues for now, but at least it seems to function:

library(earth)
MyData = read.csv("C:\\Users\\TSIT\\\\GS-Pass12.csv",header=TRUE)
x=data.frame(MyData[,1:ncol(MyData)-1])
y=MyData[,ncol(MyData)]

a = earth(x,y,nprune=5)
summary(a, digits = 2, style = "pmax")

and the output of summary looks pretty reasonable:

summary(a, digits = 2, style = "pmax")
Call: earth(x=x, y=y, nprune=5)

y =  1.2
  - 31 * pmax(0, Percent.Difference.from.Moving.Average..C..10. -   0.096)    
  + 10 * pmax(0, 0.096 - Percent.Difference.from.Moving.Average..C..10.)    
  + 25 * pmax(0, Percent.Difference.from.Moving.Average..C..15. -  0.14) 
  - 16 * pmax(0,  0.14 - Percent.Difference.from.Moving.Average..C..15.) 

Selected 5 of 116 terms, and 2 of 185 predictors  Importance:
Percent.Difference.from.Moving.Average..C..15.,
Value.Oscillator..C..8..26..1.-unused, ... Number of terms at each
degree of interaction: 1 4 (additive model) GCV 0.083    RSS 239   
GRSq 0.66    RSq 0.66

The thing I'm struggling with now is how to get the resulting model (y) out of a and into some sort of R variables so that I can use it. Can someone point me in the right direction here?

Thanks in advance.

Upvotes: 4

Views: 3929

Answers (1)

Dirk is no longer here
Dirk is no longer here

Reputation: 368201

The format() function can be used:

R> library(earth)
R> example(earth)
[... stuff omitted ...]
R> cat(format(a), "\n")
  27.2459
  +  6.17669 * h(Girth-14)
  -  3.26623 * h(14-Girth)
  + 0.491207 * h(Height-72)
R> 

There are also alternative formats:

R> cat(format(a, style="pmax"), "\n")
  27.2459
  +  6.17669 * pmax(0,  Girth -     14) 
  -  3.26623 * pmax(0,     14 -  Girth) 
  + 0.491207 * pmax(0, Height -     72) 

R> cat(format(a, style="bf"), "\n")
  27.2459
  +  6.17669 * bf1
  -  3.26623 * bf2
  + 0.491207 * bf3

   bf1  h(Girth-14)
   bf2  h(14-Girth)
   bf3  h(Height-72)

R> 

Upvotes: 6

Related Questions