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? -- Sam Steingold (http://sds.podval.org/) on Ubuntu 11.10 (oneiric) X 11.0.11004000 http://www.childpsy.net/ http://truepeace.org http://openvotingconsortium.org http://jihadwatch.org http://iris.org.il http://honestreporting.com Even Windows doesn't suck, when you use Common Lisp
R. Michael Weylandt <michael.weylandt@gmail.com>
2012-Apr-04 15:30 UTC
[R] plot with a regression line(s)
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. 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? > > -- > Sam Steingold (http://sds.podval.org/) on Ubuntu 11.10 (oneiric) X 11.0.11004000 > http://www.childpsy.net/ http://truepeace.org http://openvotingconsortium.org > http://jihadwatch.org http://iris.org.il http://honestreporting.com > Even Windows doesn't suck, when you use Common Lisp > > ______________________________________________ > R-help at r-project.org 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.
> -----Original Message----- > From: r-help-bounces at r-project.org > [mailto:r-help-bounces at r-project.org] On Behalf Of Sam Steingold > > 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?There is no guarantee that x is sorted in ascending order for a glm (or any other model), so lines(x, fitted()) can give very spiny results. Even if sorted, non-linear fits with large gaps in x will not give smooth lines. Better to use something along the lines of the budworm example in the glm help page, which uses predict() on a new sequence. If you want something a bit more general, you can use either range(x) to get the new sequence limits or par("usr")[1:2] to gets the current plot x limits. S Ellison ******************************************************************* This email and any attachments are confidential. Any use...{{dropped:8}}