ady6831983
ady6831983

Reputation: 133

igraph: error message from get.adjacency: not a graph object

I am doing this. I keep getting the same error over and over. Thanks in advance.

library(igraph)
library(sand)

zoo=read.csv("C:\\Documents and Settings\\kkk\\Desktop\\moo.data",header=FALSE)

a<-get.adjacency(moo)
**Error in get.adjacency(zoo) : Not a graph object**

V(moo)
**Error in V(moo) : Not a graph object**

EDIT Further info from comments

I am running the following commands and get the following error.

library(igraph) 
library(igraphdata) 
library(sand) 

zoo=read.csv("C:\\Documents and Settings\\kkk\\Desktop\\zoo.data",header=FALSE) 
a <- get.adjacency(zoo) 
# Error in get.adjacency(zoo) : Not a graph object 

vcount(zoo)
# Error in vcount(zoo) : Not a graph object 

How do i know what are the graph objects which support the zoo data

DATA

The first20 rows of dataset zoo. (Full data available from here with a description here)

zoo <- structure(list(V1 = c("aardvark", "antelope", "bass", "bear", 
"boar", "buffalo", "calf", "carp", "catfish", "cavy", "cheetah", 
"chicken", "chub", "clam", "crab", "crayfish", "crow", "deer", 
"dogfish", "dolphin"), V2 = c(1L, 1L, 0L, 1L, 1L, 1L, 1L, 0L, 
0L, 1L, 1L, 0L, 0L, 0L, 0L, 0L, 0L, 1L, 0L, 0L), V3 = c(0L, 0L, 
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 1L, 0L, 0L, 0L, 0L, 1L, 0L, 
0L, 0L), V4 = c(0L, 0L, 1L, 0L, 0L, 0L, 0L, 1L, 1L, 0L, 0L, 1L, 
1L, 1L, 1L, 1L, 1L, 0L, 1L, 0L), V5 = c(1L, 1L, 0L, 1L, 1L, 1L, 
1L, 0L, 0L, 1L, 1L, 0L, 0L, 0L, 0L, 0L, 0L, 1L, 0L, 1L), V6 = c(0L, 
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 1L, 0L, 0L, 0L, 0L, 1L, 
0L, 0L, 0L), V7 = c(0L, 0L, 1L, 0L, 0L, 0L, 0L, 1L, 1L, 0L, 0L, 
0L, 1L, 0L, 1L, 1L, 0L, 0L, 1L, 1L), V8 = c(1L, 0L, 1L, 1L, 1L, 
0L, 0L, 0L, 1L, 0L, 1L, 0L, 1L, 1L, 1L, 1L, 1L, 0L, 1L, 1L), 
    V9 = c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 0L, 1L, 
    0L, 0L, 0L, 0L, 1L, 1L, 1L), V10 = c(1L, 1L, 1L, 1L, 1L, 
    1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 0L, 0L, 0L, 1L, 1L, 1L, 1L
    ), V11 = c(1L, 1L, 0L, 1L, 1L, 1L, 1L, 0L, 0L, 1L, 1L, 1L, 
    0L, 0L, 0L, 0L, 1L, 1L, 0L, 1L), V12 = c(0L, 0L, 0L, 0L, 
    0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
    0L), V13 = c(0L, 0L, 1L, 0L, 0L, 0L, 0L, 1L, 1L, 0L, 0L, 
    0L, 1L, 0L, 0L, 0L, 0L, 0L, 1L, 1L), V14 = c(4L, 4L, 0L, 
    4L, 4L, 4L, 4L, 0L, 0L, 4L, 4L, 2L, 0L, 0L, 4L, 6L, 2L, 4L, 
    0L, 0L), V15 = c(0L, 1L, 1L, 0L, 1L, 1L, 1L, 1L, 1L, 0L, 
    1L, 1L, 1L, 0L, 0L, 0L, 1L, 1L, 1L, 1L), V16 = c(0L, 0L, 
    0L, 0L, 0L, 0L, 1L, 1L, 0L, 1L, 0L, 1L, 0L, 0L, 0L, 0L, 0L, 
    0L, 0L, 0L), V17 = c(1L, 1L, 0L, 1L, 1L, 1L, 1L, 0L, 0L, 
    0L, 1L, 0L, 0L, 0L, 0L, 0L, 0L, 1L, 1L, 1L), V18 = c(1L, 
    1L, 4L, 1L, 1L, 1L, 1L, 4L, 4L, 1L, 1L, 2L, 4L, 7L, 7L, 7L, 
    2L, 1L, 4L, 1L)), .Names = c("V1", "V2", "V3", "V4", "V5", 
"V6", "V7", "V8", "V9", "V10", "V11", "V12", "V13", "V14", "V15", 
"V16", "V17", "V18"), row.names = c(NA, 20L), class = "data.frame")

Upvotes: 0

Views: 4316

Answers (1)

jlhoward
jlhoward

Reputation: 59415

Your question is rather vague so this is a bit of a guess at what you are trying to accomplish. You seem to be trying to create an adjacency matrix from a graph, but you have neither. So in your case you have to create the adjacency matrix first, and then create the graph from that.

The file you link to is a csv file containing attribute data about 101 animals (in the rows). In addition to the animal's name there are 17 attributes (in the columns). Generally, an element is 1 if the animal has that attribute, 0 otherwise. There are two exceptions to this which complicate matters a bit: column 14 has the number of legs, and column 18 has the animal "class", an integer between 1 an 7. In a sense both of these are categorical variables. The main point is that this is not a graph object.

Assuming you want to think about this as a graph, and that the animals (rows) will be the vertices, an adjacency matrix is a square matrix (101 X 101 in this case), wherein each element [i,j] identifies how "strongly connected" animal i is to animal j.

An adjacency matrix is a special case of a similarity matrix, wherein each element [i,j] is a metric that captures how "similar" row i is to row j. To create one we first need to define "similarity". There are many ways to do this. The one I have chosen is to simply count the number of attributes that animals i and j have in common. This corresponds to the number of columns in zoo where zoo[i,col]==zoo[j,col], excluding columns where both rows contain 0. Fortunately in R this is very easy using the dist(...) function in the proxy package.

url <- "https://archive.ics.uci.edu/ml/machine-learning-databases/zoo/zoo.data"
zoo <- read.csv(url,header=FALSE,stringsAsFactors=FALSE)
library(proxy)
sim   <- function(x,y) sum(x==y) - sum(x==0 & y==0)
adj.M <- as.matrix(dist(zoo[-c(1,18)],method=sim))
adj.M[1:5,1:5]
#   1 2 3 4 5
# 1 0 7 3 8 8
# 2 7 0 3 7 8
# 3 3 3 0 3 4
# 4 8 7 3 0 8
# 5 8 8 4 8 0

So animals 1 and 2 have a similarity score of 7, which means that there have 7 attributes in common. Animals 1 and 3 have a score of 3, meaning they have 3 attributes in common, etc.

The function sim(x,y) is defined to calculate the similarity between two rows, and the function dist(...) applies this function to every combination of rows in zoo, excluding columns 1 and 18. We exclude column 1 because it has the names, and column 18 because it has the "class" which presumably is derived from the information in the other columns.

If all you want is the adjacency matrix, you could stop here, without ever resorting to igraph. If you want to take advantage of the functionality in igraph to analyze and visualize the relationship between animals, then you have to create an igraph object.

# colnames(adj.M) <- zoo[[1]]   # uncomment to display animal names in plot
g <- graph.adjacency(adj.M, weighted=TRUE, mode="undirected")

So g is an undirected graph with edge weights drawn from the adjacency matrix. Now we can, as an example only, calculate the community structure of g based on the edge weights.

communities <- leading.eigenvector.community(g)
groups      <- membership(communities)
# do groups map to animal "class" from data?
by(zoo[c(1,18)],groups,print)

This basically groups the animals so that animals in a given group are more highly connected (larger edge weights) to other animals in the same group than to animals in other groups. By examining the output of the last line above, you can see that this approach successfully put all animals from class=1 into group 1, and generally puts animals from class 4, 5, and 7 in group 2, and animals from class 2, 3, and 6 in group 3.

As an example of using igraph for visualization, we make a new graph with just the first 20 animals, calculate community structure as above, and plot the graph with vertices color coded based on group. The Fruchterman-Reingold layout tries to place the vertices so that vertices with higher edge weights are closer together.

sub.g       <- induced.subgraph(g,V(g)[1:20])   # just the first 20 animals
communities <- leading.eigenvector.community(sub.g)
groups      <- membership(communities)
set.seed(1)   # for reproducible layout
plot(sub.g, layout=layout.fruchterman.reingold(sub.g,weights=E(sub.g)$weight),
     vertex.color=groups+1)

Clearly to take advantage of this you need to know or learn at least a little bit about graph theory. At the very least, you should read the documentation on ?graph.adjacency, ?communities, and ?layout.

Upvotes: 7

Related Questions