Creating ab-line for logistic regression

19 views
Skip to first unread message

Andrew Jackson

unread,
Oct 7, 2009, 11:15:03 AM10/7/09
to ggplot2
Hi,

Disclaimer:

First up - I am not a stats man - I am a struggling computer science
guy - so sorry if I make a meal of explaining this.

Problem:

I am trying to figure out how to plot my fitted model created by
applying logistic regression to my data. My data and R code for
creating my model are attached.

What I want is to have two repression lines - as in the chart attached
which was created with the following code
ggplot(stack.data,aes(x=version,y=fail/(pass+fail
+notexe),colour=impl,group=impl)) + geom_point() +
stat_smooth(method=lm)

The example on the ggplot site for logistic regression is based on
having raw 0s and 1s. I cant see how to apply my data to this
approach. (http://had.co.nz/ggplot2/stat_smooth.html)

Also my data is categorical, so I have no idea how the code above even
worked - because the coefficients created in my model dont seem to
provide slopes. So I dont know how the ) "+ stat_smooth(method=lm)"
clause created the attached graph. Does it ignore the fact that
version is a factor?


Any help would be great,
Thanks,
Andrew

stack.csv
stack.r
analysisb2.pdf

Andrew Jackson

unread,
Oct 8, 2009, 9:33:39 AM10/8/09
to Andrew Jackson, ggplot2
Hey,

Anyone had a chance to have a look at this post? I am writing up my
phd and an answer to this question is very important for me so your
help would be very much appreciated.

Also - I have done things like "defactor" my explanatory variables so
that I get slopes and intercepts - but I still dont get anything like
the chart I posted. So the questions below remain unanswered.

Thanks,
Andrew
> <stack.csv><stack.r>
>
> - <analysisb2.pdf>
>
>
>
> ---------------------------------------------------------
> Andrew Jackson
> Distributed Systems Group,
> Department of Computer Science,
> Trinity College Dublin.
> Tel: +353 (01) 869 1543
> Email: Andrew....@cs.tcd.ie
> Web: www.cs.tcd.ie/Andrew.Jackson
> ---------------------------------------------------------
>
>

---------------------------------------------------------
Andrew Jackson
Distributed Systems Group,
Department of Computer Science,
Trinity College Dublin.
Tel: +353 (01) 869 1543
Email: Andrew....@cs.tcd.ie
Web: www.cs.tcd.ie/Andrew.Jackson
---------------------------------------------------------


hadley wickham

unread,
Oct 8, 2009, 9:45:00 AM10/8/09
to Andrew Jackson, ggplot2
> Also - I have done things like "defactor" my explanatory variables so
> that I get slopes and intercepts - but I still dont get anything like
> the chart I posted. So the questions below remain unanswered.

Why would you want to convert categorical variables into continuous?

Here's how I'd visualise your data and model:

library(ggplot2)
stack.data <- read.csv("stack.csv")
stack.data$version <- factor(stack.data$version)
stack.data$prob <- with(stack.data, fail / (pass+fail+notexe))

model.bin <- glm(cbind(fail,(pass+fail+notexe)) ~ impl + version,
family = binomial,data = stack.data)

grid <- with(stack.data, expand.grid(
impl = levels(impl),
version = levels(version)
))
grid$prob <- predict(model.bin, grid, type = "response")

ggplot(stack.data, aes(version, prob, colour = impl)) +
geom_point() +
geom_line(aes(group = impl), data = grid)


If it's not obvious from the plot, your model isn't very good. Have
you mislabelled impl for version 1?

Hadley


--
http://had.co.nz/

Reply all
Reply to author
Forward
0 new messages