# [R] Robust regression of nonlinear function

cstrato cstrato at aon.at
Tue Feb 3 21:48:45 CET 2004

```Dear Spencer and all

As you see, I have changed the subject title, because at the moment
this was my interest.

ad 2, I am checking always MASS first.

ad 1, As mentioned above, I wanted to do a robust fit of a nonlinear
function, although robust nonlinear regression is also of interest
to me. Thank you all for your replies, especially Sundar Dorai-Raj,
who gave the final hint:
lf <- lm(z ~ x + I(x^2) + I(x^3) - 1, data = df)
lf
Coefficients:
x  I(x^2)  I(x^3)
10.972   3.793  -1.942

lf1 <- lm(z1 ~ x + I(x^2) + I(x^3) - 1, data = df1)
lf1
Coefficients:
x   I(x^2)   I(x^3)
-59.053    4.170   -1.072

Now, using rlm from MASS gives the following results:
rlf <- rlm(z ~ x + I(x^2) + I(x^3) - 1, data = df)
rlf
Converged in 3 iterations

Coefficients:
x    I(x^2)    I(x^3)
11.118137  3.793672 -1.943496

rlf1 <- rlm(z1 ~ x + I(x^2) + I(x^3) - 1, data = df1)
rlf1
Converged in 5 iterations

Coefficients:
x    I(x^2)    I(x^3)
-2.169452  3.826027 -1.778487

Comparing lm and rlm reveals that rlm is able to
handle outliers much better than lm.

Best regards
Christian

Spencer Graves wrote:

>      1.  The question of "linear" vs. "nonlinear" means "linear in the
> parameters to be estimated.  All the examples you have given so far are
> linear in the parameters to be estimated.  The fact that they are
> nonlinear in "x" is immaterial.
>      2.  With this hint and the posting guide
> "http://www.R-project.org/posting-guide.html", you may find more
> information.  A search there exposed much discussion of "robust
> regression" and even "robust nonlinear regression", if you actually
> still need that.  In addition, I found useful information on robust
> regression in Venables and Ripley (2002) Modern Applied Statistics with
> S, 4th ed. (Springer).
>      hope this helps.      spencer graves
>
> cstrato wrote:
>
>> Dear all
>>
>> Here is a hopefully better example with regards to
>> nonlinear robust fitting:
>>
>> # fitting a polynomial:
>> x <- seq(-10,10,0.2)
>> y <- 10*x + 4*x*x - 2*x*x*x
>> plot(x,y)
>> z <- jitter(y,amount=300)
>> plot(x,z)
>> df <- as.data.frame(cbind(x,z))
>> nf <- nls(z ~ a*x + b*x*x + c*x*x*x, data=df,
>> +           start=list(a=4,b=2,c=1), trace = TRUE)
>> 127697531 :  4 2 1
>> 2974480 :  10.972123  3.793426 -1.942278
>>
>> # introducing outliers before fitting the  polynomial:
>> z1 <- z
>> z1[c(16,22,23,34,36,42,67,69,72,76)] <-
>> + c(2000,1900,2000,1900,1600,1600,500,-2000,-1700,-1800)
>> plot(x,z1)
>> df1 <- as.data.frame(cbind(x,z1))
>> nf1 <- nls(z1 ~ a*x + b*x*x + c*x*x*x, data=df1,
>> +           start=list(a=4,b=2,c=1), trace = TRUE)
>> 159359174 :  4 2 1
>> 24098548 :  -59.053288   4.169518  -1.072027
>>
>> # plotting the results:
>> y1 <- 10.97*x + 3.79*x*x - 1.94*x*x*x
>> y2 <- -59.05*x + 4.17*x*x - 1.07*x*x*x
>> oldpar <- par(pty="s",mfrow=c(2,2),mar=c(5,5,4,1))
>> plot(x,y)
>> plot(x,z1)
>> plot(x,y1)
>> plot(x,y2)
>> par(oldpar)
>>
>> In my opinion this fit could hardly be considered
>> to be robust.
>>
>> Are there functions in R which can do robust fitting?
>> (Sorrowly, at the moment I could not test the package
>> nlrq mentioned by Roger Koenker)
>>
>> Best regards
>> Christian
>> _._._._._._._._._._._._._._._._
>> C.h.i.s.t.i.a.n S.t.r.a.t.o.w.a
>> V.i.e.n.n.a       A.u.s.t.r.i.a
>> _._._._._._._._._._._._._._._._
>>
>> ______________________________________________
>> R-help at stat.math.ethz.ch mailing list
>> https://www.stat.math.ethz.ch/mailman/listinfo/r-help