Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MNLTrainTest doing 6 k-fold validations rather than breaking into testing and training. #1

Open
TWhiteleyAPHA opened this issue Jul 1, 2022 · 0 comments

Comments

@TWhiteleyAPHA
Copy link

The MNLTrainTest function is not doing as described. The function inputs dictates a percent_cross but then k-fold cross validation occurs- with 6 folds hard coded in.

If you would like my rewrite, I have rewritten the function to repeat lines 21-41 nboot times and save the return the mean output.


MNLTrainTest<-function(mnl_input="mnl_input_0.csv",percent_cross=0.7,nboot=100)
{
  library(nnet)  
  set.seed(123)  
  
  #Read in data
  data<-read.table(mnl_input,sep=",",header = TRUE)
  #data <- subset(data, select = -c(NA.) )
  data2<-subset(data,select=-1)
  
  accuracy=c()
  
  for(i in seq(1,nboot)){

    trainingRows <- sample(1:nrow(data2), percent_cross*nrow(data))
    training <- data2[trainingRows, ]
    testing <- data2[-trainingRows, ]

    multinomModel <- multinom(Source ~ ., data=training, maxit=1000) # multinom Model

    # predicted_scores <- predict (multinomModel, testing, "probs") 
    predicted_class <- predict (multinomModel, testing)
    # table(predicted_class, testing2$Source)
    
    # Calculate the number of correctly predicted sources.
    accuracy<-c(accuracy,mean(as.character(predicted_class) == as.character(testing$Source)))
  }
  
  return(mean(accuracy))
  
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant