igraph / xdata-igraph

xdata igraph, has been merged into igraph/igraph
GNU General Public License v2.0
18 stars 3 forks source link

doc & testing for adjacency.spectral.embedding #26

Closed jovo closed 10 years ago

dpmcsuss commented 10 years ago

Not sure this is the right place to put this but, as of now I don't think this function works as I expect/want. As far as I can tell it returns the same output as R's svd , ie a list with 3 elements that correspond to the left and right singular vectors and the singular values.

I would think that the function should do the following:

adjacency.spectral.embedding <- function(G, dim, scale=TRUE){
  # INPUT: 
    # G is the graph, 
    # dim is the adjacency matrix
    # scale indicates whether to scale by the sqrt of the singular values
  # OUTPUT: A (number of vertices)x(dim) matrix of "estimated latent positions

  # Basic Outline
  # (1) Compute SVD or preferably truncated SVD if that is faster.
  # (2) If G is directed return the top dim left and right svecs, scaled if appropriate
  # (3) If G is undirected, just return top dim left svecs, scaled if appropriate
}

Here is a function that we wrote, it doesn't quite have all the features but they can be added in. It assumes undirected and scale=TRUE and additionally gives two options for how to compute the matrix decomposition, (which I can't remember why we did right now).

stfp <- function(A, dim, method = "svd"){

    if(method == "svd"){
        S <- irlba(A, nu = dim, nv = dim)
        V <- S$v[,1:dim]
        D <- S$d[1:dim]
    } else {
        S <- eigen(A)
        V <- S$vectors[,1:dim]
        D <- S$values[1:dim]
    }

    if(dim == 1){
        Xhat <- V*sqrt(D)
    }
    else{
      Xhat <- V %*% diag(sqrt(D))
    }

    return(Xhat)
}
jovo commented 10 years ago

good point daniel! have you any interest in updating it according to your preferences with associated documentation? if so, it could be included in the next official igraph release in CRAN....

On Tue, Jan 28, 2014 at 12:00 PM, Daniel Sussman notifications@github.comwrote:

Not sure this is the right place to put this but, as of now I don't think this function works as I expect/want. As far as I can tell it returns the same output as R's svd , ie a list with 3 elements that correspond to the left and right singular vectors and the singular values.

I would think that the function should do the following:

adjacency.spectral.embedding <- function(G, dim, scale=TRUE){

INPUT:

# G is the graph,
# dim is the adjacency matrix
# scale indicates whether to scale by the sqrt of the singular values

OUTPUT: A (number of vertices)x(dim) matrix of "estimated latent positions

Basic Outline

(1) Compute SVD or preferably truncated SVD if that is faster.

(2) If G is directed return the top dim left and right svecs, scaled if appropriate

(3) If G is undirected, just return top dim left svecs, scaled if appropriate

}

Here is a function that we wrote, it doesn't quite have all the features but they can be added in. It assumes undirected and scale=TRUE and additionally gives two options for how to compute the matrix decomposition, (which I can't remember why we did right now).

stfp <- function(A, dim, method = "svd"){

if(method == "svd"){
    S <- irlba(A, nu = dim, nv = dim)
    V <- S$v[,1:dim]
    D <- S$d[1:dim]
} else {
    S <- eigen(A)
    V <- S$vectors[,1:dim]
    D <- S$values[1:dim]
}

if(dim == 1){
    Xhat <- V*sqrt(D)
}
else{
  Xhat <- V %*% diag(sqrt(D))
}

return(Xhat)

}

Reply to this email directly or view it on GitHubhttps://github.com/igraph/xdata-igraph/issues/26#issuecomment-33498664 .

perhaps consider allowing the quest for eudaimonia to guide you openconnecto.me, jovo.me