0

I am trying to learn and implement spectral clustering on a multivariate dataset in R. I have 9 independent variables and 1 dependent variable which is binary.

As a first step to spectral clustering, I am required to create a graph from the given data set (I have about 1000 observations). Because I am just starting out, I don't really care about the method used to create the graph but it will be better if it uses k-nearest neighbors.

I came across a function nng in the package cccd which creates the graph. I used the following:

knnGraph<-nng(as.matrix(data[2:10]),k=3)

This runs fine, except that when I try to visualize the graph by using plot(knnGraph), I get the following error:

Error in layout.norm(layout, -1, 1, -1, 1) : 
  `layout' should have 2 or three columns

I am not sure how to proceed, any help would be much appreciated. I also tried to find a step-by-step tutorial to implement spectral clustering in R, but was unable to find it. Any pointers to any such resource will be much appreciated as well.

Patthebug
  • 4,647
  • 11
  • 50
  • 91

1 Answers1

0

It appears as though nng wants at least a two-dimension matrix. This example seemed to run

library(cccd)
data<-runif(50)
knnGraph<-nng(matrix(data[2:10], ncol=2),k=3)
plot(knnGraph)

Note that I changed as.matrix to matrix and added nol=2 to give it a second dimension. I've never used that function nor that package before so I have no idea what it really want's in that parameter, but it looks like the 9x1 matrix returned by as.matrix(data[2:10]) wasn't going to cut it.

MrFlick
  • 195,160
  • 17
  • 277
  • 295
  • Thanks for your answer but it doesn't seem to work with my dataset. Your `data` has 50 elements (all belonging to 1 variable) whereas my `data` is a dataframe with 10 variables. In other words, my `ncol` is already 9, which is the cause of this problem. I still get the same error. – Patthebug May 07 '14 at 23:43
  • Well, data sure looked like a vector to me. That's why it's always helpful to supply example data when posting a question. Anyway, the function only seems to like matrices with 2 or 3 columns. So perhaps you're passing in the wrong type of data. The documentation isn't that descriptive. Perhaps you can consult the reference they provide to see if the method works with higher dimensions: _D.J. Marchette, Random Graphs for Statistical Pattern Recognition, John Wiley & Sons, 2004._ – MrFlick May 08 '14 at 00:03