r - creating data frame for confusionMatrix(0 input, however getting unusual dataframe -


i’m try understand results of prediction object via caret’s confusionmatrix() function, requires table input according http://artax.karlin.mff.cuni.cz/r-help/library/caret/html/confusionmatrix.html, table() creates results understand , not friendly confusionmatrix() function.

here relevant code snippet:

#model creation #convert categorical e values , numeric 1 5 in order regression friendly training_data_subset_numeric <- training_data_subset; testing_data_subset_numeric <- testing_data_subset; training_data_subset_numeric$classe <- as.numeric(training_data_subset$classe) testing_data_subset_numeric$classe <- as.numeric(testing_data_subset$classe) #model exercise.model <- glm(formula = classe ~ ., data = training_data_subset_numeric) #model evaluation exercise.prediction <- predict(exercise.model,newdata = testing_data_subset_numeric) eval_table <- table(exercise.prediction,testing_data_subset$classe) tail(eval_table)  exercise.prediction b c d e    4.35504232913594 1 0 0 0 0    4.47219097065568 1 0 0 0 0    4.50838854075835 1 0 0 0 0    4.6173551930011  0 1 0 0 0    4.69261223447305 0 1 0 0 0    4.73297946213265 0 1 0 0 0 

basically need convert above output , data frame 1 col corresponding prediction value follows rule:

if column 1 , predicted value 1

if column b 1 , predicted value 2

if column c 1 , predicted value 3

if column d 1 , predicted value 4

if column e 1 , predicted value 5

i therefore, wrote function job done:

getpredictresults<- function(x) { # create 1 column & n row data frame num <- data.frame(matrix(0, ncol = 1, nrow = nrow(x)));  (r in 1:nrow(x) ) {  (c in 1:ncol(x) ) {     #if column has value 1 num[1,r] <- 1      if (x[r,'a']== 1)     {         num[1,r] <- 1;     }     #if column b has value 1 num[1,r] <- 2     else if (x[r,'b']== 1)     {         num[1,r] <- 2;     }     #if column c has value 1 num[1,r] <- 3     else if (x[r,'c']== 1)     {         num[1,r] <- 3;     }     #if column d has value 1 num[1,r] <- 4     else if (x[r,'d']== 1)     {         num[1,r] <- 4;     }     #if column e has value 1 num[1,r] <- 5     else if (x[r,'e']== 1)     {         num[1,r] <- 5;     }     else     {     }  }#end inner   }#end outer   return (num); }#end function  exercise.prediction_df <- getpredictresults(eval_table) 

however when typing :

head(exercise.prediction_df) 

im getting unusual output , here bottom snippet:

2    na    na    na    na    na    na    na    na    na    na    na    na    na    na    na    na    na    na   v4840 v4841 v4842 v4843 v4844 v4845 v4846 v4847 v4848 v4849 v4850 v4851 v4852 v4853 v4854 v4855 v4856 v4857 1     5     1     4     5     2     2     5     5     1     2     5     4     5     5     1     5     5     4 2    na    na    na    na    na    na    na    na    na    na    na    na    na    na    na    na    na    na   v4858 v4859 v4860 v4861 v4862 v4863 v4864 v4865 v4866 v4867 v4868 v4869 v4870 v4871 v4872 v4873 v4874 v4875 1     4     2     1     2     5     1     4     5     2     1     4     5     2     4     2     4     4     2 2    na    na    na    na    na    na    na    na    na    na    na    na    na    na    na    na    na    na   v4876 v4877 v4878 v4879 v4880 v4881 v4882 v4883 v4884 v4885 v4886 v4887 v4888 v4889 v4890 v4891 v4892 v4893 1     5     1     1     4     1     2     2     1     1     5     1     4     1     1     1     1     1     1 2    na    na    na    na    na    na    na    na    na    na    na    na    na    na    na    na    na    na   v4894 v4895 v4896 v4897 v4898 v4899 v4900 v4901 v4902 v4903 v4904 1     1     1     1     1     1     1     1     1     2     2     2 2    na    na    na    na    na    na    na    na    na    na    na  [ reached getoption("max.print") -- omitted 4 rows ] 

further investigation shows:

> ncol(exercise.prediction_df) [1] 4904 > nrow(exercise.prediction_df) [1] 4904 

which ncol() should return 1 & nrow() can integer value.

how can fix function, in order create right dataframe input confusionmatrix() function?

thanks.

classe <- cut(runif(100), seq(0, 1, length.out = 5)) levels(a) <- c("a", "b", "c", "d", "e") exercise.prediction <- rnorm(100) eval_table <- table(exercise.prediction, classe)  eval_matrix <- as.matrix(tab)  transform <- apply(eval_matrix, 1, function(x) sum(x * c(1:5))) head(as.data.frame(transform)) 

Comments