sta-363-s20 / community

Discussion, Q&A, everything you want to say, formatted nicely
1 stars 0 forks source link

Question on Ex 7 #22

Closed Feifan-990807 closed 4 years ago

Feifan-990807 commented 4 years ago

Hi Dr. McGowan,

I was doing lab 2 and having trouble with plotting the confusion matrix. The following attached is my code. It seems like we have already done with adding the predicted class using the mutate function in previous exercise, so I just typed in the conf_mat code directly, but it showed that my code didn’t work.

Thank you!

Screen Shot 2020-02-04 at 10 20 46 PM
LucyMcGowan commented 4 years ago

The conf_mat() function is expecting to work on a data frame. If lda_predicted_class is already in your data frame, then you can do this:

test %>%
  mutate(g = as.factor(g)) %>%
  conf_mat(g, lda_predicted_class) %>%
  autoplot(type = "heatmap")

Otherwise, you'll need to add it with a mutate function like this:

test %>%
  mutate(g = as.factor(g),
               lda_predicted_class = predict(model_3, newdata = test)) %>%
  conf_mat(g, lda_predicted_class) %>%
  autoplot(type = "heatmap")

Also, it looks like you have another issue in your code, you are fitting your lda model on your test data (models are always fit on the training data)

Cindyyyhey commented 4 years ago

Hi Professor Lucy,

My code for exercise 7 also have some problems. The following is my question 7. If I click the green run button, it can generate the confusion matrix successfully. However, if I knit the whole, the same error saying that "variable 260 appears to be constant.." Also, the question saying that "Plot a confusion matrix using the test data frame." Do we use the train data or the test data?

model <- lda(g ~ ., data = test)
predictions <- predict(model)
test %>%
  mutate(predicted_class = predictions$class,
         g=as.factor(g)) %>%
  conf_mat(g, predicted_class) %>%
  autoplot(type = "heatmap")
LucyMcGowan commented 4 years ago

I think there are a couple of things that may be going on, first the model should be fit on the training data (data=train), the predicted values should be calculated using test (predict(model, newdata=test)) but in terms of not knitting, one way to try to troubleshoot it is to clear out your environment (the broom in the environment window) and run the chunks one at a time from the top, this can help identify the issue

Cindyyyhey commented 4 years ago

fix it! Thanks a lot.