marcos carvajalino
2009-Nov-30 13:46 UTC
[R] multiple column argument in formula (MASS:lda)
Dear R Help team, I'm having some problems running a discriminant analysis (MASS:lda) over my data, i have a grouping variable coming from a previous cluster analysis and several discriminators (78 actually). I'm trying to run lda using column index as independent variable in the formula like it's done in the help page for ?formula:>lda(Grupo~paste(names(cien[,3:80]),collapse="+"),data=cien)but i get the following error: Error in model.frame.default(formula = Grupo ~ paste(names(cien[, 3:80]), : different variable length (found in 'paste(names(cien[, 3:80]), collapse = "+")') (This is my english translation of the error msg while i use R in spanish so it should look different in your machines) The funny thing is that if i use:>paste(names(cien[,3:80]),collapse="+")[1] "X.SO.M2+X.SO.M4+X.SO.M6+X.SO.M8+X.SO.M10+X.SO.M12+..... and then copy and paste the result in the formula:> lda(Grupo~X.SO.M2+X.SO.M4+X.SO.M6+X.SO.M8+X.SO.M10+X.SO.M12+....,data=cien)then there's no error!!! the lda runs just fine. I know i might be doing something wrong but i don't know where so could you please enlighten me? Thanks in advance PD. Here's a piece of the data: NOMEST Grupo X.SO.M2 X.SO.M4 X.SO.M6 X.SO.M8 .....(78 var) 1 Ci?naga La Ahuyama 3 95.1 152.3 115.3 97.6 2 Boca del Ca?o Grande 3 91.0 104.2 131.7 89.4 3 Buenavista 3 115.6 102.9 122.0 110.5 4 Ci?naga El Loro 1 104.0 80.9 68.5 50.7 . . . -- Marcos Antonio Carvajalino Fern?ndez Estudiante de Ingenier?a Ambiental y Sanitaria Universidad del Magdalena Santa Marta, Colombia
Hello Marcos, When you use paste(.), it will return a String, and the first argument of lda(.) does not read a string as formula.>lda(Grupo~paste(names(cien[,3:80]),collapse="+"),data=cien)Here's what I'd do: colIndex <- c(1,2,4,5,...) #what ever index of your input data set that you want to use as variables #I presume Grupo is the class column (col=3)? lda(cien[,colIndex], cien[,3]) Hope this helps.. Wai-Kuan Yip http://www.deakin.edu.au/~waiyip http://www.deakin.edu.au/~waiyip -- View this message in context: http://n4.nabble.com/multiple-column-argument-in-formula-MASS-lda-tp931285p931814.html Sent from the R help mailing list archive at Nabble.com.