Re: [R] Plotting two regression lines on one graph

2009-06-07 Thread David Winsemius
Since you have provided no executable data and not even a clear enough  
description of the data to offer advice regarding approaches or  
pitfalls, I will use the example in glm's help page:


counts <- c(18,17,15,20,10,20,25,13,12)
outcome <- gl(3,1,9)
treatment <- gl(3,3)

glm.D93r <- glm(counts ~ outcome, family=poisson())
plot(x=as.numeric(as.character(outcome)), y=counts, type="p")  #need  
to convert from factor to ge points

lines(1:3, c(exp(3.045), exp(3.0445 -0.4543), exp(3.0445 -0.2930) ) )

#the default link for poisson is log, so you need to transform back to  
the original scale to predict counts.



On Jun 7, 2009, at 11:36 PM, Jo Stringer wrote:


I have fitted two glms assuming a poisson distribution which are:

fit1 <- glm(Aids ~ Year, data=aids, family=poisson())

fit2 <- glm(Aids ~ Year+I(Year^2), data=aids, family=poisson())


I am trying to work out how to represent the fitted regression  
curves of fit1 and fit2 on the one graph. I have tried:


graphics.off()

plot(Aids ~ Year, data = aids)


line(glm(Aids ~ Year, data=aids, family=poisson()))
line(glm(Aids ~ Year+I(Year^2), data=aids, family=poisson()))

but this does not work.

Can anyone help me?



David Winsemius, MD
Heritage Laboratories
West Hartford, CT

__
R-help@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.


Re: [R] Plotting two regression lines on one graph

2009-06-07 Thread milton ruser
Hi Jo,

Next time try prepare a reproducible code.
You have several ways of do that (see Dylan Beaudette) and give a look
below.

x<-1:100
y<-7+x*(sqrt(x*runif(100)))

plot(y~x)

mod1<-glm(y~x)
mod2<-glm(y~x+I(x*x))
curve(mod1$coef[1]+mod1$coef[2]*x, col="red", add=T, lwd=3)
curve(mod2$coef[1]+mod2$coef[2]*x+mod2$coef[3]*x*x, col="green", add=T,
lwd=3)

cheers

milton

On Sun, Jun 7, 2009 at 11:36 PM, Jo Stringer  wrote:

> Hi!
>
>
> I have fitted two glms assuming a poisson distribution which are:
>
> fit1 <- glm(Aids ~ Year, data=aids, family=poisson())
>
> fit2 <- glm(Aids ~ Year+I(Year^2), data=aids, family=poisson())
>
>
> I am trying to work out how to represent the fitted regression curves of
> fit1 and fit2 on the one graph. I have tried:
>
> graphics.off()
>
> plot(Aids ~ Year, data = aids)
>
>
> line(glm(Aids ~ Year, data=aids, family=poisson()))
> line(glm(Aids ~ Year+I(Year^2), data=aids, family=poisson()))
>
> but this does not work.
>
> Can anyone help me?
>
> Thanks
>
> Jo
>
> BSES Limited Disclaimer
>
>
> 
>
> This email and any files transmitted with it are confide...{{dropped:15}}
>
> __
> R-help@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.
>

[[alternative HTML version deleted]]

__
R-help@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.


Re: [R] Plotting two regression lines on one graph

2009-06-07 Thread Dylan Beaudette
One approach to this is generating a representative sequence of your
x-variable(s) with seq() or expand.grid(). Next use the predict()
function to make predictions from your glm object along the sequence.
Finally, plot the predictions vs. the new sequence. Putting everything
into a dataframe helps.

# generate some data:
x <- rnorm(10)
y <- x * 2 + rnorm(10, sd=0.5)

# plot
plot(y ~ x)

# generate linear model:
l <- lm(y ~ x)

# generate sequence along x-var
# and predictions
d <- data.frame(x.new=seq(from=-1.5, to=1.2, by=0.1))
d$y.new <- predict(l, data.frame(x=d$x.new))

# add to plot:
lines(y.new ~ x.new, data=d, lwd=2)

Cheers,
Dylan


On Sun, Jun 7, 2009 at 8:36 PM, Jo Stringer wrote:
> Hi!
>
>
> I have fitted two glms assuming a poisson distribution which are:
>
> fit1 <- glm(Aids ~ Year, data=aids, family=poisson())
>
> fit2 <- glm(Aids ~ Year+I(Year^2), data=aids, family=poisson())
>
>
> I am trying to work out how to represent the fitted regression curves of fit1 
> and fit2 on the one graph. I have tried:
>
> graphics.off()
>
> plot(Aids ~ Year, data = aids)
>
>
> line(glm(Aids ~ Year, data=aids, family=poisson()))
> line(glm(Aids ~ Year+I(Year^2), data=aids, family=poisson()))
>
> but this does not work.
>
> Can anyone help me?
>
> Thanks
>
> Jo
>
> BSES Limited Disclaimer
>
> 
>
> This email and any files transmitted with it are confide...{{dropped:15}}
>
> __
> R-help@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.
>

__
R-help@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.


[R] Plotting two regression lines on one graph

2009-06-07 Thread Jo Stringer
Hi!


I have fitted two glms assuming a poisson distribution which are:

fit1 <- glm(Aids ~ Year, data=aids, family=poisson())

fit2 <- glm(Aids ~ Year+I(Year^2), data=aids, family=poisson())


I am trying to work out how to represent the fitted regression curves of fit1 
and fit2 on the one graph. I have tried:

graphics.off()

plot(Aids ~ Year, data = aids)


line(glm(Aids ~ Year, data=aids, family=poisson()))
line(glm(Aids ~ Year+I(Year^2), data=aids, family=poisson()))

but this does not work.

Can anyone help me?

Thanks

Jo

BSES Limited Disclaimer



This email and any files transmitted with it are confide...{{dropped:15}}

__
R-help@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.