This is example 14.7 in the book.
A = matrix(c(0.96,1.33,1.63,2.04,2.32,50,48,46,49,50,6,16,24,42,44),ncol=3)
colnames(A)=c("concentration","number_of_insects","number_killed")
A
myData = as.data.frame(A)
I have stored the data from Example 14.7 in the book as a matrix above. Then I used the command as.data.frame() to convert the matrix to a data frame.
Data frames are better for doing regression analysis. I would like to add one more column to my data frame. The cbind() command lets me do this.
proportion_killed = myData$number_killed/(myData$number_of_insects)
cbind(myData,proportion_killed)
Now I am going to create a best fit logistic model for my data. I call it myGLM because the R command
is glm(), which stands for generalized linear model. There are two ways to enter the data. One
way is to use the proportion_killed numbers for y-values and concentration as the x-values. Alternatively,
instead of using the proprtion_killed vector as the y-values, the glm() command accepts a matrix
formed by using the number of successes in the first column and the number of failures as the second column.
I do this below, but you can also try the other way and see what happens.
myGLM = glm(cbind(number_killed,number_of_insects-number_killed)~concentration,family="binomial",data=myData)
summary(myGLM)
plot(myData$concentration,proportion_killed,ylab='Proportion Killed',xlab='Concentration',main='Logistic Regression')
curve(predict(myGLM,data.frame(concentration=x),type='response'),add=T)
Another good example of logistic regression can be found online at the Cookbook for R website. There they compare the fuel economy (mpg) of old (1970's) cars versus an indicator variable (vs) which is whether the engine cylinders were arranged in a V-shape (e.g., V6 engine) or a straight configuration (e.g., straight 4 cylinder or 6 cylinder engine). A 0 indicates a V-engine and 1 indicates a straight engine (actually the 0 category also includes the RX-4 which has a rotary engine and the Porsche 914's flat boxer engine).