Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MM robust estimation in ggplot2 using stat_smooth with method = "rlm"

The function rlm (MASS) permits both M and MM estimation for robust regression. I would like to plot the smoother from MM robust regression in ggplot2, however I think that when selecting method = "rlm" in stat_smooth, the estimation method automatically chosen is the M type.

Is there any way of selecting the MM type estimation technique for the rlm function through ggplot2?

Here is my code:

df <-  data.frame("x"=c(119,118,144,127,78.8,98.4,108,50,74,30.4,
50,72,99,155,113,144,102,131,105,127,120,85,153,40.6,133),
"y"=c(1.56,2.17,0.81,1.07,1.12,2.03,0.90,1.48,0.64,
0.91,0.85,0.41,0.55,2.18,1.49,1.56,0.82,0.93,0.84,1.84,
0.78,1.15,3.85,3.30,0.94))      

library(ggplot2)
library(MASS)      

ggplot(df,aes(x=x,y=y))+geom_point()+
stat_smooth(method="rlm",fullrange=TRUE)+xlim(0,160)

I have checked the results with the rlm summary itself, and I am pretty sure ggplot2 is using the (default?) M estimation.

How can I use the MM estimation from the rlm function?

rlm(formula, ...,method = "MM")

Many thanks in advance!

like image 502
Katerina Rapti Avatar asked Jan 13 '23 23:01

Katerina Rapti


1 Answers

Unfortunately both stat_smooth and rlm have a method parameter. That makes it a bit harder:

ggplot(df,aes(x=x,y=y)) +
  geom_point() +
  stat_smooth(method=function(formula,data,weights=weight) rlm(formula,
                                                               data,
                                                               weights=weight,
                                                                method="MM"),
              fullrange=TRUE) +
  xlim(0,160)
like image 197
Roland Avatar answered Jan 19 '23 00:01

Roland