Re: [R] user defined panel function - Solved

From: Martin Henry H. Stevens <HStevens_at_MUOhio.edu>
Date: Tue 30 Aug 2005 - 06:57:46 EST

Problem Solved (see below) - Many thanks to Sundar Dorai-Raj! Hank Stevens
On Aug 29, 2005, at 3:44 PM, Sundar Dorai-Raj wrote:

>
>
> Martin Henry H. Stevens wrote:
>> Mac OS 10.3.9 R framework v. 2.1.1
>>
>> I am attempting to put a fitted curve into each panel of a lattice
>> graph, but am failing to do so. I have tried writing a very
>> sophisticated function to do so. The function seems to work when used
>> with plot(), but does not do so inside a panel function in xyplot().
>> Any pointers would be appreciated.
>>
>> #The example data
>> fact <- gl(2,7)
>> x <- rep(1:7,2)
>> y <- c(1,1,2,3,2,3,4,1,2,1,2,3,3,4)
>> plot(jitter(y/6) ~ x)
>>
>> # The following user defined function puts a curve (I believe the
>> correct one) into the scatterplot
>> panel.predglm <- function(x, y) {
>> model.trial <- glm(cbind(y,6-y) ~ poly(x,2),
>> family=quasibinomial(link="logit"))
>> xfit <- seq(1,7, length=21)
>> yfit <- predict(model.trial, newdata=data.frame(x=xfit),
>> type="response")
>> lines(xfit,yfit) }
>>
>> panel.predglm(x, y)
>>
>>
>> # My attempt to use it in a lattice xyplot, however, fails. It draws a
>> curve which in most cases is outside the dimensions of the plot. I
>> suspect that the prediction is on the scale of the link functions.
>>
>> library(lattice)
>> xyplot(y/6 ~ x|fact, ylim=c(0,.8),
>> panel=function(x, y,...) {
>> panel.xyplot(jitter(x),jitter(y))
>> panel.predglm(x,y) }
>> )
>>
>> Any thoughts?
>
> Two:
>
> 1. The "y" argument in your panel function ranges from 0 to 1 and not 0
> to 6 as your plot example assumes.
>
> 2. You need to use llines in your panel function and not lines.
>
> Here's a working example:
>
> library(lattice)
> fact <- gl(2,7)
> x <- rep(1:7,2)
> y <- c(1,1,2,3,2,3,4,1,2,1,2,3,3,4)
>
> # The following user defined function puts a curve (I believe the
> correct one) into the scatterplot
> panel.predglm <- function(x, y) {
> model.trial <- glm(cbind(y,6-y) ~ poly(x,2),
> family=quasibinomial(link="logit"))
> xfit <- seq(1, 7, length=21)
> yfit <- predict(model.trial, newdata=data.frame(x=xfit),
> type="response")
> llines(xfit,yfit)
> }
>
> xyplot(y/6 ~ x|fact, ylim=c(0,.8),
> panel = function(x, y, ...) {
> panel.xyplot(jitter(x), jitter(y))
> panel.predglm(x, y * 6)
> })
>
> ______________________________________________
> R-help@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
>
>
Dr. Martin Henry H. Stevens, Assistant Professor 338 Pearson Hall
Botany Department
Miami University
Oxford, OH 45056

Office: (513) 529-4206
Lab: (513) 529-4262
FAX: (513) 529-4243

http://www.cas.muohio.edu/botany/bot/henry.html
http://www.muohio.edu/ecology/
http://www.muohio.edu/botany/

"E Pluribus Unum"

R-help@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 Received on Tue Aug 30 07:05:50 2005

This archive was generated by hypermail 2.1.8 : Sun 23 Oct 2005 - 16:02:49 EST