6

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!

Roland
  • 127,288
  • 10
  • 191
  • 288

1 Answers1

10

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)
Roland
  • 127,288
  • 10
  • 191
  • 288
  • Roland, I wonder if you could also answer this follow up question (or should I post it as a new one?) how can I make the range go ot the full range (as fullrange=TRUE suggests) when I set the origin of the plot at zero? It doesn't seem to go to the full range when I do: ggplot(df,aes(x=x,y=y))+geom_point()+ stat_smooth(method="rlm",fullrange=TRUE)+coord_cartesian(xlim=c(0,160),ylim=c(0,4)) – Katerina Rapti Jul 05 '13 at 12:24
  • Sorry, I probably should have posted this in a different place and way, right? – Katerina Rapti Jul 05 '13 at 12:25
  • @KaterinaRapti Are you looking for `scale_x_continuous(limits=c(0,160),expand=c(0,0))`? – Roland Jul 05 '13 at 12:34
  • thanks! Yes, that works in this example, but I've just posted it as another question with code where things don't work out. – Katerina Rapti Jul 05 '13 at 12:46
  • @Roland in your code above I have difficulty grasping where `weights=weight` comes from. The original data frame doesn't include it. While the code works, I was wondering how this "weight" is calculated exactly (by ggplot2?). – FM Kerckhof Feb 14 '16 at 20:19
  • 1
    @FMKerckhof There are no weights, ggplot2 doesn't pass any weights here, so the parameter is missing and `MASS:::rlm.default` handles this internally. However, you *could* specify weights, e.g., map something to `weight` in `aes`. – Roland Feb 15 '16 at 20:05