函数rlm(MASS)允许M和MM估计稳健回归。我想在ggplot2中绘制来自MM稳健回归的更平滑,但我认为在stat_smooth中选择method =“rlm”时,自动选择的估算方法是M类型。
有没有办法通过ggplot2为rlm函数选择MM类型估计技术?
这是我的代码:
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)
我已经用rlm摘要本身检查了结果,我很确定ggplot2正在使用(默认?)M估计。
如何使用rlm函数中的MM估计?
rlm(formula, ...,method = "MM")
非常感谢提前!
答案 0 :(得分:7)
不幸的是,stat_smooth
和rlm
都有一个method
参数。这让事情变得更难了:
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)