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 differMy txt-file is attached. Could someone please help me to find out what I did wrong. Thank you on beforehand, J?rgen -------------- next part -------------- Para Size 1 4.464285714 1 4.196428571 1 4.151785714 1 4.151785714 1 3.973214286 1 4.285714286 1 4.0625 1 4.241071429 1 4.0625 1 4.017857143 1 4.0625 1 4.017857143 1 4.241071429 1 4.151785714 1 4.0625 1 4.285714286 1 4.196428571 1 4.107142857 1 4.0625 1 4.330357143 1 4.375 1 4.196428571 1 4.196428571 1 3.928571429 1 4.0625 1 4.0625 1 4.107142857 1 4.196428571 1 3.928571429 1 4.151785714 1 3.705357143 1 3.75 1 4.107142857 1 3.839285714 1 3.616071429 1 4.0625 1 3.75 1 4.017857143 1 3.571428571 1 4.151785714 1 3.883928571 1 3.705357143 1 4.017857143 1 4.0625 1 4.0625 1 4.196428571 1 3.839285714 1 4.107142857 1 4.017857143 1 3.839285714 1 3.526785714 1 4.196428571 1 4.017857143 1 3.883928571 1 4.0625 1 4.107142857 1 4.285714286 1 3.928571429 1 3.794642857 1 4.107142857 1 4.107142857 1 3.839285714 1 4.241071429 1 4.285714286 1 4.375 0 4.115044248 0 3.495575221 0 4.203539823 0 3.982300885 0 4.026548673 0 4.07079646 0 3.805309735 0 4.07079646 0 3.982300885 0 4.247787611 0 4.203539823 0 3.982300885 0 3.849557522 0 4.115044248 0 3.849557522 0 4.07079646 0 4.203539823 0 3.849557522 0 4.247787611 0 4.026548673 0 4.07079646 0 4.203539823 0 4.026548673 0 3.982300885 0 4.026548673 0 3.584070796 0 4.026548673 0 3.982300885 0 3.761061947 0 3.230088496 0 3.495575221 0 3.982300885 0 3.938053097 0 3.849557522 0 3.982300885 0 3.849557522 0 4.026548673 0 3.938053097 0 4.159292035 0 3.849557522 0 4.026548673 0 3.672566372 0 4.159292035 0 3.761061947 0 4.247787611 0 4.115044248 0 3.539823009 0 3.89380531 0 4.115044248 0 3.849557522 0 4.115044248 0 4.115044248 0 4.07079646 0 3.584070796 0 3.407079646 0 4.203539823
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 differI 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 >
On 26.05.2011 10:45, 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)Several things I'd improve, hence the whole code: dat <- read.table("clipboard", header=TRUE) dat$Para <- factor(dat$Para) model <- glm(Para ~ Size, data=dat, family="binomial") xv <- seq(3.2,4.5,0.01) yv <- predict(model, newdata=data.frame(Size=xv), type="response") with(dat, plot(Size, as.integer(Para)-1)) lines(xv, yv) Best, Uwe Ligges> The error message that I get is: > >> Error in xy.coords(x, y) : 'x' and 'y' lengths differ > > My txt-file is attached. Could someone please help me to find out what I did > wrong. > > Thank you on beforehand, > J?rgen > > > PerBinom.txt > > > Para Size > 1 4.464285714 > 1 4.196428571 > 1 4.151785714 > 1 4.151785714 > 1 3.973214286 > 1 4.285714286 > 1 4.0625 > 1 4.241071429 > 1 4.0625 > 1 4.017857143 > 1 4.0625 > 1 4.017857143 > 1 4.241071429 > 1 4.151785714 > 1 4.0625 > 1 4.285714286 > 1 4.196428571 > 1 4.107142857 > 1 4.0625 > 1 4.330357143 > 1 4.375 > 1 4.196428571 > 1 4.196428571 > 1 3.928571429 > 1 4.0625 > 1 4.0625 > 1 4.107142857 > 1 4.196428571 > 1 3.928571429 > 1 4.151785714 > 1 3.705357143 > 1 3.75 > 1 4.107142857 > 1 3.839285714 > 1 3.616071429 > 1 4.0625 > 1 3.75 > 1 4.017857143 > 1 3.571428571 > 1 4.151785714 > 1 3.883928571 > 1 3.705357143 > 1 4.017857143 > 1 4.0625 > 1 4.0625 > 1 4.196428571 > 1 3.839285714 > 1 4.107142857 > 1 4.017857143 > 1 3.839285714 > 1 3.526785714 > 1 4.196428571 > 1 4.017857143 > 1 3.883928571 > 1 4.0625 > 1 4.107142857 > 1 4.285714286 > 1 3.928571429 > 1 3.794642857 > 1 4.107142857 > 1 4.107142857 > 1 3.839285714 > 1 4.241071429 > 1 4.285714286 > 1 4.375 > 0 4.115044248 > 0 3.495575221 > 0 4.203539823 > 0 3.982300885 > 0 4.026548673 > 0 4.07079646 > 0 3.805309735 > 0 4.07079646 > 0 3.982300885 > 0 4.247787611 > 0 4.203539823 > 0 3.982300885 > 0 3.849557522 > 0 4.115044248 > 0 3.849557522 > 0 4.07079646 > 0 4.203539823 > 0 3.849557522 > 0 4.247787611 > 0 4.026548673 > 0 4.07079646 > 0 4.203539823 > 0 4.026548673 > 0 3.982300885 > 0 4.026548673 > 0 3.584070796 > 0 4.026548673 > 0 3.982300885 > 0 3.761061947 > 0 3.230088496 > 0 3.495575221 > 0 3.982300885 > 0 3.938053097 > 0 3.849557522 > 0 3.982300885 > 0 3.849557522 > 0 4.026548673 > 0 3.938053097 > 0 4.159292035 > 0 3.849557522 > 0 4.026548673 > 0 3.672566372 > 0 4.159292035 > 0 3.761061947 > 0 4.247787611 > 0 4.115044248 > 0 3.539823009 > 0 3.89380531 > 0 4.115044248 > 0 3.849557522 > 0 4.115044248 > 0 4.115044248 > 0 4.07079646 > 0 3.584070796 > 0 3.407079646 > 0 4.203539823 > > > > ______________________________________________ > 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.