Re: [R] subscript out of bounds error in lda

From: Prof Brian Ripley <ripley_at_stats.ox.ac.uk>
Date: Fri, 15 Jun 2007 18:11:40 +0100 (BST)

On Fri, 15 Jun 2007, Silvia Lomascolo wrote:

>
> I work with Windows, R version 2.4.1
>
> I'm trying to do a discriminant analysis and, in trying to figure out how to
> do it following the example from R help, I'm getting an error that says
> 'subscript out of bounds'. I don't know what this means and how to solve it
> (I'm very new with R)
>
> I'm doing everything in this made-up test matrix:
>
> group var1 var2 var3
> 1 1 3 55 6
> 2 1 4 66 7
> 3 1 5 55 8
> 4 1 4 66 7
> 5 1 3 44 6
> 6 1 4 55 5
> 7 2 5 88 9
> 8 2 4 99 8
> 9 2 8 88 9
> 10 2 9 76 8
> 11 2 8 66 9
> 12 2 9 99 10
> 13 2 10 100 9
>
> I write:
>
> data.tb<-read.table('locationHere.txt', header=T)
> data.df<-as.data.frame (data.tb)

Wny call as.data.frame on a data frame?

> train<-sample (1:63, 30)

Why sample from 1:63 with 13 rows?

> table (data.df$group[train])
> data.disc<-lda(group~., data.tb, subset = train)
> data.disc
> predict (data.disc, data.df[-train,])$class
>
> This is where I get the message:
>
> Error in `[.data.frame`(data.df, -train, ) :
> subscript out of bounds

traceback() is your friend:

> traceback()

8: `[.data.frame`(data.df, -train, )
7: data.df[-train, ]
6: inherits(x, "data.frame")
5: is.data.frame(data)
4: model.frame.default(Terms, newdata, na.action = na.pass, xlev = 
object$xlevels)
3: model.frame(Terms, newdata, na.action = na.pass, xlev = object$xlevels)
2: predict.lda(data.disc, data.df[-train, ])
1: predict(data.disc, data.df[-train, ])

>

So, let's take a look at the top lines.

> train

  [1] 46 42 30 13 27 63 19 47 3 52 62 16 26 4 61 23 59 44 40 38 25 55 50 10 43
[26] 2 8 31 7 11
> nrow(data.df)

[1] 13

So, you are asking for number 46 out of 13 rows. Now perhaps you didn't show us all the problem, but hopefully this helps you find the error. If not, the bottom of every R-help message says

PLEASE do read the posting guide
http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.

-- 
Brian D. Ripley,                  ripley_at_stats.ox.ac.uk
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272866 (PA)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595

______________________________________________
R-help_at_stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Received on Fri 15 Jun 2007 - 17:33:24 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Fri 15 Jun 2007 - 18:32:01 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.