# [R] Problems with mars in R in the case of nonlinear functions

Janne Huttunen jmhuttun at stat.berkeley.edu
Thu Jun 12 22:35:35 CEST 2008

```Hi,

I'm trying to use mars function in R to interpolate nonlinear
multivariate functions.
However, it seems that mars gives me a fit which uses only very few
basis function and

For example, I have tried the following code to test mars:

require("mda")

f <- function(x,y) { x^2-y^2 };
#f <- function(x,y) { x+2*y };

# Grid
x <- seq(-1,1,length=10);
x <- outer(x*0,x,FUN="+"); y <- t(x);
X <- cbind(as.vector(x),as.vector(y));

# Data
z <- f(x,y);

fit <- mars(X,as.vector(z),nk=200,penalty=2,thresh=1e-3,degree=2);

# Plotting
par(mfrow=c(1,2),pty="s")
lims <- c(min(c(min(z),min(fit\$fitted))),max(c(max(z),max(fit\$fitted))))
xlab='x',ylab='y',zlab='z',main='true',phi=25,theta=55,zlim=lims)
persp(z=matrix(fit\$fitted.values,nrow=nrow(x),byrow=F),ticktype='detailed',
col='lightblue',
phi=25,theta=55,zlim=lims)

(the code is also here if someone wants to try it:
http://venda.uku.fi/~jmhuttun/R/marstest.R)

The results are here: http://venda.uku.fi/~jmhuttun/R/R-10.pdf . The
fitted model contains only
5 terms which is not enough in this case. Adjusting parameters like nk,
thresh, penalty and degree
seems only have minor effect or no effect at all. It's also strange that
when I increase
the number of points in the grid, the results are ever worse:
see e.g. http://venda.uku.fi/~jmhuttun/R/R-20.pdf for a 20x20 grid.
However Mars seems to work well with linear functions (e.g. with the
function which
is commented in the above code).

Do anyone know what is wrong in this case? Do I miss something is there
something
wrong in my code?

This seems not to be a problem with MARS method in general. For example,
Friedman's MARS implementation (ran in Matlab) gives a rather good fit:
see http://venda.uku.fi/~jmhuttun/R/Matlab.pdf .

Thank you

Janne

--
Janne Huttunen
University of California
Department of Statistics
367 Evans Hall Berlekey, CA 94720-3860
email: jmhuttun at stat.berkeley.edu
phone: +1-510-502-5205
office room: 449 Evans Hall

```