[R] plot with a regression line(s)

David Winsemius dwinsemius at comcast.net
Wed Apr 4 17:40:50 CEST 2012


On Apr 4, 2012, at 11:30 AM, R. Michael Weylandt <michael.weylandt at gmail.com 
 > wrote:

> I'm not sure what your definition of easier would be, but there are  
> some style things you might want to be aware of:
>
> I) the name is likely to hit up against the S3 generic plot() when  
> applied to a glm object. This might lead to strange bugs at some  
> point.
> II) you can test !is.null once and use on.exit() to delay the clean- 
> up call to dev.off()
> III) I'm not sure about glm objects but abline() applied to an lm  
> object automatically plots a best fit line saving you a line or so  
> of code.

If you offer a regression object to abline's 'reg' parameter and that  
object has a coef() method you might get a result. But if the link  
function of the regression is not matched to the plot's scale (as  
might happen with a logistic or poisson fit) then results may not be  
as expected. Most people will probably want to use `predict(reg- 
object, newdata= ...., type = "response")` with .

> IV) You probably don't want to print() m at the end: the REPL will  
> print it automatically in interactive top level calls and it will be  
> rather noisy if you start wrapping this in other calls.
>
> Hope this helps,
> Michael
>
> On Apr 4, 2012, at 11:13 AM, Sam Steingold <sds at gnu.org> wrote:
>
>> I am sure a common need is to plot a scatterplot with some fitted
>> line(s) and maybe save to a file.
>> I have this:
>>
>> plot.glm <- function (x, y, file = NULL, xlab =  
>> deparse(substitute(x)),
>>                     ylab = deparse(substitute(y)), main = NULL) {
>> m <- glm(y ~ x)
>> if (!is.null(file))
>>   pdf(file = file)
>> plot(x, y, xlab = xlab, ylab = ylab, main = main)
>> lines(x, y = m$fitted.values, col = "green")
>> if (!is.null(file))
>>   dev.off()
>> print(m)
>> }
>>
>> is there a better/easier/more general way?
>>
>> -- 

>

David Winsemius, MD
West Hartford, CT



More information about the R-help mailing list