0
library(earth)

y <- cumsum(sample(c(-1, 1),100, TRUE))
p1 <- cumsum(sample(c(-1, 1),100, TRUE))
p2 <- cumsum(sample(c(-1, 1),100, TRUE))
p3 <- cumsum(sample(c(-1, 1),100, TRUE))
n1 <- cumsum(sample(c(-1, 1),100, TRUE))
n2 <- cumsum(sample(c(-1, 1),100, TRUE))

df1  <- data.frame(y,p1,p2,p3,n1,n2)
df1

I am using earth for a non linear regression. I have some constraints where
the maximum number of splines in the first variable is 2, the second variable is 3, the third variable is 4 etc. Also the p variables have to have a positive slope and the n variables a negative slope.

I can set a global maximum for the number of splines:

fit <- earth(y ~ ., data = df1,nprune=50,trace=5)

but I am having problems setting a maximum number of splines and slope sign for each variable individually. I tried:

fit <- earth(y ~ ., data = df1,nprune=c(2,3,4,5,6))
plotmo(fit)
summary(fit, digits = 2, style = "pmax")

but I get an error.

How can I control the max number of splines and slope sign for each variables so as to avoid Spurious Regression?

I was thinking that there may be a way to set conditions before the model selection process. Possibly by making some slight alteration of the earth function but I have not yet been able to work out ho to do it. This function is hidden but can be viewed as below:

To view the hidden S3 functions:

methods(earth)
getAnywhere(earth.default)
getAnywhere(earth.formula)

Here is the link for viewing a hiddden S3 function: How can I view the source code for a function?

To view all the code (R, C, Fortran etc) download the .tar.gz source package from CRAN, unpack it and the R source code is available in the "R" subdirectory.

also: cerebralmastication.com/2009/02/finding-the-source-in-r

to view S4 functions use:

showMethods()
getMethod()

Thank you for your help.

Community
  • 1
  • 1
adam.888
  • 7,686
  • 17
  • 70
  • 105
  • I didn't see a facility to do what you request in the help page `?earth`. Is there a reason to think there is code to meet your request within that package? – IRTFM Mar 13 '16 at 00:03
  • I was thinking there may be a selection function before the final pruning step that could be modified. – adam.888 Mar 13 '16 at 05:29
  • If there is then you should be looking at the code. "Open source" means just that. – IRTFM Mar 16 '16 at 04:04
  • What exactly should I do to see the open source code so I can modify it to my requirements? – adam.888 Mar 20 '16 at 10:40
  • https://svn.r-project.org/R/trunk/ ... Also search on "Ligges R accessing sources". I also think an SO search would have delivered useful results. – IRTFM Mar 20 '16 at 15:59
  • Thank for the help on accessing open source. – adam.888 Mar 23 '16 at 16:46

0 Answers0