I'm new to programming and R and I'm having trouble plotting the confusion matrix in relation to my data. I am trying to reproduce with another database the example of the book by Trevor Hastie and et. al. Chapter 4 Lab: Logistic Regression 4.6.2. I will not post the book link here because I don't know if the rules allow it, but it is available for free download on the page of one of the authors (Gareth James). The confusion matrix I get is a 1x2 vector instead of a 2x2 matrix. I am making a prediction with the logistic regression model to make a prediction of whether or not you will have a disease. The variable "cases" has the number of cases in a given period, so I created a new variable (Cases2) in which I put 0 where there were no cases of the disease and 1 where they had. Then I coded the variable for factor, and when it happened at least one case at the site is YES = 1 and otherwise NO = 0.
Data $ Cases2 <- factor (Data $ Cases2, label = c ("No", "Yes"), levels = c ("0", "1"))
Then I applied the logistic regression function and I converted these predicted probabilities into class labels, to Yes or No. The following two commands create a vector of class predictions based on whether the predicted probability of an increase in cases is greater than or less than 0.5.
glm.fit = glm (Cases2 ~ Precip + TempMa + TempMi + Humid, data = Data, family = binomial) glm.probs = predict (glm.fit, type = "response") contrasts (Data $ Case2) glm.pred = rep ("No", 1250) glm.pred [glm.probs> 0.5] = "Yes" table (glm.pred, Data $ Case2)
And I get as a result
glm.pred No Yes No. 979 271
I did exactly as it is in the book and mine is giving this problem, someone can help!
What am I doing wrong? Any help I appreciate!