sta-363-s20 / community

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

Lab 02 Ex9 #26

Closed skywang0407 closed 4 years ago

skywang0407 commented 4 years ago

Hi Professor McGowan, About my QDA model, I can run it by chunks in the coding window, but it can not be knitted as below: Q4

So I only commit and push my RMD, which works with the chunk.

Thanks!

LucyMcGowan commented 4 years ago

I think the reason it’s not working is g was overwritten to be a factor variable somewhere in a previous chunk - can you double check that?

juserrano commented 4 years ago

Professor @LucyMcGowan , g was overwritten when we had to get the confusion matrix in exercise 7. How can we change the code so this does not happen?

test %>%
  mutate(g_factor=as.factor(g)) %>%
  conf_mat(g_factor, lda_predicted_class)
skywang0407 commented 4 years ago

Actually, I have changed the factor variable as g_test so it shouldn't rewrite g. However, it still only works by chunks and cannot knit. Q5

LucyMcGowan commented 4 years ago

If you clear everything out (click Session > Restart R) and run the chunks one by one from the top, where do you see the error? Can you paste the error you see?

juserrano commented 4 years ago

This is the error I get, which seems to be the same error @skywang0407 got:

Error in qda.default(x, grouping, ...) : rank deficiency in group aa

My code for the qda is: model_qda <- qda(factor(g) ~ ., data = train) predictions_qda <- predict(model_qda)

LucyMcGowan commented 4 years ago

If you remove factor() (so just g~.) does the error go away?

juserrano commented 4 years ago

No, same error happens.

LucyMcGowan commented 4 years ago

ah! I think I know what it is. In the previous exercises we added the predicted values from LDA to the train data frame and so since were using the . It is also trying to use that variable to fit the model...add this right before you fit qda:

train$lda_predicted_class <- NULL

The other way to fix this would be instead of overwriting the train data to add this variable, you could create a data frame with a different name:

train_new <- train %>%
  mutate(lda_predicted_class <- predict(model)$class)
juserrano commented 4 years ago

That worked! Thank you! Only thing is that my confusion matrix shows that all observations are being predicted right. Was this expected? ` {r train-qda} train$lda_predicted_class <- NULL model_qda <- qda(g ~ ., data = train) predictions_qda <- predict(model_qda)

class <- predict(model_qda, newdata = train)$class train <- train %>% mutate(qda_predicted_class = class)

train %>% mutate(g_factor=as.factor(g)) %>% conf_mat(g_factor, qda_predicted_class)

` Truth Prediction aa ao dcl iy sh aa 519 0 0 0 0 ao 0 759 0 0 0 dcl 0 0 562 0 0 iy 0 0 0 852 0 sh 0 0 0 0 648

skywang0407 commented 4 years ago

Got it, thank you two!

skywang0407 commented 4 years ago

Unfortunately, it still doesn't work on mine. I am just lost. Q6

skywang0407 commented 4 years ago

It can work by chunk, I just don't get it why it doesn't work by knit. Q7

I am wondering if I can submit two html so that one has the lda model and another one has the qda model by opening up another work.

Cindyyyhey commented 4 years ago

@skywang0407 @LucyMcGowan Mine the same! The confusion matrix work by chunk but could not knit.

LucyMcGowan commented 4 years ago

@skywang0407 I just looked at your Rstudio Cloud instance and it knit for me. It looks like you reloaded the data to get it to knit (which works!)

@Cindyyyhey try to add this to the top of the chunk that calls the first qda() function.

train <- read_csv("data/train.csv")

I believe the problem overall is you edited the train along the way and then tried to use the edited data frame in the qda function.

ConnorReardon commented 4 years ago

@LucyMcGowan my code is still not knitting, but each time I try it seems to be a new reason. I can typically run it, but I've only gotten as far as a 56% progress on the knit.

LucyMcGowan commented 4 years ago

@ConnorReardon I just logged into your Rstudio cloud instance and it knit for me 🤔

skywang0407 commented 4 years ago

Done! It finally knits! OMG, after so much effort. Thank you all!

jdtrat commented 4 years ago

Hello! Time to jump on this bandwagon. My code works in chunks, like described above, but will not knit. When it gets to exercise 9, I reload the data as follows:

reloading the training and test data in order to rerun the models for QDA

test <- read_csv("data/test.csv") train <- read_csv("data/train.csv")

This will knit. However, when I create a QDA model/prediction/heatmap afterwards, it stops working and doesn't knit as seen below. No error pops up like @midupree wrote in #40

Screen Shot 2020-02-10 at 10 33 11 PM

Any help would be greatly appreciated!

skywang0407 commented 4 years ago

Actually, I think your problem is about internet, cause I met this problem before too. It seems R just stops running. You can try clear your workplace and run each chunk one by one and knit again. It might help.

jdtrat commented 4 years ago

Thanks for the idea, but I still run into the same error :/ I even moved it into another workspace and it still stops like that. I'll keep trying I guess