I am trying to plot the predicting line for a GLM with proportional data. After having troubles with the example from "Crawley" I found this link:
Problems plotting GLM data of binomial proportional data
My data consists out of 3 columns: Altitude, number of beetles and number of other insects. I want to investigate the change of beetle proportion throughout the different altitudes.
After importing the table I calculated the proportion as shown in the topic linked above.
Then
y=cbind(mean$beetles,mean$rest)
model=glm(y~altitude,family=binomial(logit),data=data)
plot(p~data$altitude, ylab="Beetles (proportional)", xlab="Altitude")
p is the propotion i calculated before
lines(mean$altitude,predict(model,type="response"))
I then get this plot:
this is obviously not correct but i cannot figure out what is wrong here.
I would be very pleased if you helped me.
this is the little data set:
altitude,beetles,rest
304,88,35
842,7,18
877,21,27
1505,2,25
1118,11,19
702,25,24
806,16,17
960,16,32
975,34,61
1112,11,17
1348,11,32
502,58,24
1428,2,13
1521,0,35
1040,9,6
1412,8,19
1231,5,19
980,14,16
1053,2,10
Best Answer
Your question does not specify how the p was calculated, but I assume it is the percentage of beetles on every plot you have sampled. I can't see the image you've attached either (I'm probably out of luck for today), but judging from the code you've posted, I'd guess you need to sort the predicted values before plotting them with lines(). Something like this should work:
Or, a simpler method, sort the data frame first: