0
votes

I have a simple positive/negative test result as a function of a protein concentration, and I am trying to plot the logit regression curve. However, I am consistently getting a error that I cannot fix.

This is my data:

sp <- structure(list(Cry = c(32, 32, 32, 32, 32, 32, 16, 16, 16, 8, 
8, 8, 4, 4, 4, 2, 2, 2, 1, 1, 1, 0.5, 0.5, 0.5, 0.25, 0.25, 0.25, 
0.12, 0.12, 0.06, 0, 0, 0, 0, 0, 0), Positive = c(1L, 1L, 1L, 
1L, 1L, 0L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 0L, 0L, 0L, 0L, 1L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
0L)), class = "data.frame", row.names = c(NA, -36L))

This is my glm:

test.glm <- glm(Positive ~ Cry, family = binomial, data = sp)
summary(test.glm)

I am trying to make predicted values to plot

x_test <- seq(0,32, 0.01)
y_test <- predict(test.glm, newdata =  list(x_test) ,type = "response")

So, when I try to plot it:

plot(sp$Positive ~ log(Cry))
lines(x_test, y_test)

I get this error:

Error in xy.coords(x, y) : 'x' and 'y' lengths differ

because

length(x_test)
3201
length(y_test)
36

The length of y_test ends up being 36 instead of the same length as x_test

Why is my predict function ignoring the length of my newdata and only predicting as many values as I have in my original dataframe?

1
Please do not post an image of code/data/errors: it cannot be copied or searched (SEO), it breaks screen-readers, and it may not fit well on some mobile devices. Ref: meta.stackoverflow.com/a/285557 (and xkcd.com/2116). Please just include the code, console output, or data (e.g., dput(head(x)) or data.frame(...)) directly. - r2evans
Thanks! It is my first post. I figures it wasn`t optimal but I could not figure out how to attach a data.frama directly. - Pedro Brandão Dias

1 Answers

3
votes

Perhaps this is what you were looking for?

test.glm <- glm(Positive ~ Cry, family = binomial, data = sp)
x_test <- seq(0,32,0.01)
y_text <- predict(test.glm,newdata = data.frame(Cry = x_test),type="response")
plot(sp, Positive ~ log(Cry))
lines(x_test,y_test)

enter image description here

Data

sp <- structure(list(Cry = c(32, 32, 32, 32, 32, 32, 16, 16, 16, 8, 
8, 8, 4, 4, 4, 2, 2, 2, 1, 1, 1, 0.5, 0.5, 0.5, 0.25, 0.25, 0.25, 
0.12, 0.12, 0.06, 0, 0, 0, 0, 0, 0), Positive = c(1L, 1L, 1L, 
1L, 1L, 0L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 0L, 0L, 0L, 0L, 1L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
0L)), class = "data.frame", row.names = c(NA, -36L))