On Thu, 26 May 2011, Jörgen Svan wrote:
Dear all,
I am quite new with R and I have a problem with plotting a binomial
regression line in a plot.
This is what I type in:
model<-glm(Para~Size,binomial)
par(mfrow=c(1,1))
xv<-seq(3.2,4.5,0.01)
yv<-predict(model,list(area=xv),type="response")
plot(Size,Para)
lines(xv,yv)
The error message that I get is:
Error in xy.coords(x, y) : 'x' and 'y' lengths differ
I assume that setting "area" is not correct.
My txt-file is attached. Could someone please help me to find out what I
did wrong.
For a plain scatterplot you could do this:
## read data (and code Para as factor)
d <- read.table("PerBinom.txt", header = TRUE)
d$Para <- factor(d$Para)
## model fit and predicted probabilies
m <- glm(Para ~ Size, data = d, family = binomial)
s <- seq(3.2, 4.5, by = 0.1)
p <- predict(m, data.frame(Size = s), type = "response")
## scatterplot
plot(as.numeric(Para) - 1 ~ Size, data = d)
lines(p ~ s, col = 4)
A similar display can be obtained easily with the "effects" package:
library("effects")
plot(allEffects(m), ask = FALSE, rescale = FALSE)
which also works if there is more than one regressor. See
http://www.jstatsoft.org/v08/i15/ and http://www.jstatsoft.org/v32/i01/
for more details about the "effects" package.
Finally, you could also use exploratory displays, e.g., a spinogram or a a
conditional density plot:
cdplot(Para ~ Size, data = d, ylevels = 2:1)
plot(Para ~ Size, data = d, ylevels = 2:1)
plot(Para ~ Size, data = d, ylevels = 2:1, breaks = quantile(Size))
Adding a regression line to the latter is not completely straightforward
due to the rescaled x axis. You could do something like this:
lines(sapply(s, function(x) mean(d$Size <= x)), p, col = 4)
hth,
Z
Thank you on beforehand,
Jörgen
______________________________________________
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.