How do I add to a trellis plot the best fit line from a robust fit? I can use panel.lm to add a least squares fit, but there is no panel.rlm function.
Alan S Barnett <asb at mail.nih.gov> NIMH/CBDB
5 messages · Alan S Barnett, Chuck Cleland, Hadley Wickham +2 more
How do I add to a trellis plot the best fit line from a robust fit? I can use panel.lm to add a least squares fit, but there is no panel.rlm function.
Alan S Barnett <asb at mail.nih.gov> NIMH/CBDB
How do I add to a trellis plot the best fit line from a robust fit? I can use panel.lm to add a least squares fit, but there is no panel.rlm function.
How about using panel.abline() instead of panel.lmline()?
fit1 <- coef(lm(stack.loss ~ Air.Flow, data = stackloss))
fit2 <- coef(rlm(stack.loss ~ Air.Flow, data = stackloss))
xyplot(stack.loss ~ Air.Flow, data=stackloss,
panel = function(x, y, ...){
panel.xyplot(x, y, ...)
panel.abline(fit1, type="l", col="blue")
panel.abline(fit2, type="l", col="red")
}, aspect=1)
Chuck Cleland, Ph.D. NDRI, Inc. 71 West 23rd Street, 8th floor New York, NY 10010 tel: (212) 845-4495 (Tu, Th) tel: (732) 512-0171 (M, W, F) fax: (917) 438-0894
How do I add to a trellis plot the best fit line from a robust fit? I can use panel.lm to add a least squares fit, but there is no panel.rlm function.
It's not trellis, but it's really easy to do this with ggplot2:
install.packages("ggplot2", dep=T)
library(ggplot2)
p <- qplot(x, y, data=diamonds)
p + geom_smooth(method="lm")
p + geom_smooth(method="rlm")
p + geom_smooth(method="lm", formula="y ~ poly(x,3)")
see http://had.co.nz/ggplot2/stat_smooth.html for more examples.
Hadley
I don't think the code below does what's requested, as it assumes a single
overall fit for all panels, and I think the requester wanted separate fits
by panel. This can be easily done, of course, by a minor modification:
xyplot( y ~ x | z,
panel = function(x,y,...){
panel.xyplot(x,y,...)
panel.abline(lm(y~x),col="blue",lwd=2)
panel.abline(rlm(y~x),col = "red",lwd=2)
})
Note that the coefficients do not need to be explicitly extracted by coef(),
as panel.abline will do this automatically.
Bert Gunter
Genentech Nonclinical Statistics
South San Francisco, CA 94404
650-467-7374
How do I add to a trellis plot the best fit line from a robust fit? I can use panel.lm to add a least squares fit, but there is no panel.rlm function.
How about using panel.abline() instead of panel.lmline()?
fit1 <- coef(lm(stack.loss ~ Air.Flow, data = stackloss))
fit2 <- coef(rlm(stack.loss ~ Air.Flow, data = stackloss))
xyplot(stack.loss ~ Air.Flow, data=stackloss,
panel = function(x, y, ...){
panel.xyplot(x, y, ...)
panel.abline(fit1, type="l", col="blue")
panel.abline(fit2, type="l", col="red")
}, aspect=1)
Chuck Cleland, Ph.D. NDRI, Inc. 71 West 23rd Street, 8th floor New York, NY 10010 tel: (212) 845-4495 (Tu, Th) tel: (732) 512-0171 (M, W, F) fax: (917) 438-0894 ______________________________________________ R-help at stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
How do I add to a trellis plot the best fit line from a robust fit? I can use panel.lm to add a least squares fit, but there is no panel.rlm function.
Well, panel.lmline (not panel.lm, BTW) is defined as:
panel.lmline
function (x, y, ...)
{
if (length(x) > 0)
panel.abline(lm(as.numeric(y) ~ as.numeric(x)), ...)
}
So it's not much of a stretch to define
panel.rlmline <- function(x, y, ...)
if (require(MASS) && length(x) > 0)
panel.abline(rlm(as.numeric(y) ~ as.numeric(x)), ...)
The other replies have already shown you how you might use this in a call.
-Deepayan