Error In Object$tables And Subscript Out Of Bounds
Challenge Wed 29 May 2013 – Wed 31 Jul 2013 (3 years ago) Dashboard ▼ Home Data Make a submission Information Description Evaluation Rules Prizes Timeline Winners Forum Leaderboard Public Private Competition Forum All Forums » Amazon.com - Employee Access Challenge Subscript out of bounds error numerical 0 probability for all classes with observation on NaiveBayes Start Watching « Prev Topic » Next Topic 0 votes I am attempting to naive bayes subscript out of bounds build a Naive Bayes model. I am using the NaiveBayes function from klaR package but can't seem to produce a model. I load the naivebayes in r data and create the model successfully, however when I use the predict function I get the following message: Error in object$tables[[v]][, nd] : subscript out of bounds In addition: Warning messages: 1: In FUN(1:58921[[3L]], ...) : Numerical 0 probability for all classes with observation 1 2: In FUN(1:58921[[3L]], ...) : Numerical 0 probability for all classes with observation 2 I appreciate this is a novice question, but I have searched the internet (stackexchance, cross validation etc) and am still stuck, so any assistance would be much appreciated. #1 | Posted 3 years ago Permalink soates Posts 63 | Votes 70 Joined 4 Nov '11 | Email User 1 vote My guess is that there are categories in the test data that are not in the training data. A way to deal with this in R is to combine the training and test sets into one dataset, then declare the variables as factors. After doing that, split the training and test into separate data frames again. This ensures that all the levels that are in either the training or test sets are known to exist and the naive Bayes function can give a 0 (or something) to the ones that aren't in the training set. If this still doesn't work, I would suggest looking for a different implementation of naive Bayes. #2 | Posted 3 years ago Permalink Andrew Landgraf Posts 15 | Votes 11 Joined 18 Aug '10 | Email User 0 votes Thanks so much for replying so quickly. As a novice the problem of unseen levels has definitelybeen a barrier to creating entries so its great to hear a solution! Of course I now feel dumb for not thinking of it myself. #3 | Posted 3 years ago Permalink soates Posts 63 | Votes 70 Joined 4 Nov '11 | Email User Reply You must be logged in to reply to this topic. Log in » Flagging notifies Kaggle that this message is spam, inappropriate, abusive, or violates rules. Do not use flagging to indicate you disagree with an opinion or to hide a post. Start Watching « Back to forum © 2016 Kaggle Inc Our Team Careers Terms Privacy Contact/Support
means on a scatter diagram Messages sorted by: [ date ] [ thread ] [ subject ] [ author ] On 8/22/07, Polly He
Threaded Open this post in threaded view ♦ ♦ | Report Content as Inappropriate ♦ ♦ "subscript out of bounds" Error in predict.naivebayes I'm trying to fit a naive Bayes model and predict on a new data set using the functions naivebayes and predict (package = e1071). R version 2.5.1 on a Linux machine My data set looks http://r.789695.n4.nabble.com/quot-subscript-out-of-bounds-quot-Error-in-predict-naivebayes-td832763.html like this. "class" is the response and k1 - k3 are the independent variables. All of them are factors. https://github.com/rstudio/shiny/issues/1199 The response has 52 levels and k1 - k3 have 2-6 levels. I have about 9,300 independent variables but omit the long list here for simple demonstration. There are no missing values in the observations. class k1 k2 k3 1 0 0 1 8 0 0 0 # model fitting, I also tried setting laplace=0 but didn't help nbmodel <- naiveBayes(class~., data=train, laplace=1) # predict nb.fit <- predict(nbmodel, x.test[,-1]) subscript out First I had no trouble fitting the model. R also returned the predictions for some of my large data sets. But for some data sets, R can fit the model (no error message, nb.model$tables look ok). When I invoked the predict function, it kept giving me the following message: # my data set has 1 response variable and 9318 independent variables Error in FUN(1:9319[[4L]], ...) : subscript out of bounds # Here's what traceback() returns 10: FUN(1:9319[[4L]], ...) 9: lapply(X, FUN, ...) 8: sapply(1:nattribs, function(v) { nd <- ndata[v] subscript out of if (is.na(nd)) rep(1, length(object$apriori)) else { prob <- if (isnumeric[v]) { msd <- object$tables[[v]] dnorm(nd, msd[, 1], msd[, 2]) } else object$tables[[v]][, nd] prob[prob == 0] <- threshold prob } }) 7: log(sapply(1:nattribs, function(v) { nd <- ndata[v] if (is.na(nd)) rep(1, length(object$apriori)) else { prob <- if (isnumeric[v]) { msd <- object$tables[[v]] dnorm(nd, msd[, 1], msd[, 2]) } else object$tables[[v]][, nd] prob[prob == 0] <- threshold prob } })) 6: apply(log(sapply(1:nattribs, function(v) { nd <- ndata[v] if (is.na(nd)) rep(1, length(object$apriori)) else { prob <- if (isnumeric[v]) { msd <- object$tables[[v]] dnorm(nd, msd[, 1], msd[, 2]) } else object$tables[[v]][, nd] prob[prob == 0] <- threshold prob } })), 1, sum) 5: FUN(1:30[[1L]], ...) 4: lapply(X, FUN, .
Sign in Pricing Blog Support Search GitHub This repository Watch 250 Star 1,947 Fork 1,071 rstudio/shiny Code Issues 189 Pull requests 5 Projects 0 Wiki Pulse Graphs New issue subscript out of bounds #1199 Closed abiyug opened this Issue May 26, 2016 · 8 comments Projects None yet Labels None yet Milestone No milestone Assignees No one assigned 3 participants abiyug commented May 26, 2016 I am trying to run a reactive shiny app, and keep getting 'subscription out of bound' error. Warning: Error in .subset2: subscript out of bounds Stack trace (innermost first): 86: reactiveValuesToList 85: as.data.frame Tried to get answer from the community on google and stackoverflow, but no answers. And I am reading that this maybe a bug on renderTable or shiny. Here is the full code to recreate. library(shiny) library(stats) ui <- fluidPage( h1("Temp Predictor for US Mainland"), sidebarLayout( sidebarPanel( numericInput( "new_lat", "Enter latitude between 20 and 60:", 21 ) ), mainPanel( plotOutput("table") ) ) ) server <- function(input, output, session) { load("regression.lm.RData") lat <- reactive({ as.data.frame(reactiveValuesToList(input$new_lat)) }) output$table <- renderTable( predict(regression.lm, lat()) ) } shinyApp(ui, server) abiyug changed the title from Subscript subscript out of bounds to subscript out of bounds May 26, 2016 bborgesr commented May 27, 2016 It looks like the problem is the way you're using reactiveValuesToList(). You cannot pass input$new_lat on to reactiveValuesToList() because input$new_lat is not a reactivevalues object. You can check this yourself by running is.reactivevalues(input$new_lat) and printing the result somewhere inside your server function. But, in any case, I'm a little confused as to what exactly are you trying to do there? input$new_lat is a number -- are you trying to convert it to a 1x1 dataframe? Or do you want to replicate that number a certain number of times (A) to get Ax1 or 1xA or even AxA? In any of these cases, you absolutely don't need reactiveValuesToList. In fact, if all you want is to generate a 1x1 dataframe then as.data.frame(input$new_lat) is all you need. A side note: you cannot use renderTable for a plotOutput slot. It looks to me like what you really want is a table, so you should change plotOutput to tableOutput. Otherwise, no table will appear. abiyug c