Wednesday 31 January 2018

R S6 Class

Introduction to R6 classes

zzz.R

R Packages - What is the file 'zzz.R' used for?

Monday 29 January 2018

Fossbytes

fossbytes

Sublime Text Editor for Linux

10 Best Text Editors For Linux And Programming
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Cited from Install Sublime Text Editor On Ubuntu 16.10 & Ubuntu 16.04

$ sudo add-apt-repository ppa:webupd8team/sublime-text-3
$ sudo apt-get update
$ sudo apt-get install sublime-text-installer

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Download the latest Sublime software from Sublime Text Editor 3

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Cited from How to update Sublime Text-3 in Ubuntu 16.04?

Sublime text editor command "subl"


Monday 15 January 2018

Special Types of Graphs in igraph

Cited from the Book "Statistical Analysis of Network Data With R"

A complete graph is a graph where every vertex is joined to every other vertex by an edge. This concept is perhaps most useful in practice through its role in defining a clique, which is a complete subgraph.

A regular graph is a graph in which every vertex has the same degree. A regular graph with common degree d is called d-regular. An example of a 2-regular graph is the ring. 

A connected graph with no cycles is called a tree. The disjoint union of such graphs is called a forest. A digraph whose underlying graph is a tree is called a directed tree. Often such trees have associated with them a special vertex called a root, which is distinguished by being the only vertex from which there is a directed path to every other vertex in the graph. Such a graph is called a rooted
tree. A vertex preceding another vertex on a path from the root is called an ancestor, while a vertex following another vertex is called a descendant. Immediate ancestors are called parents, and immediate descendants, children. A vertex without any children is called a leaf. The distance from the root to the farthest leaf is called the depth of the tree.

A k-star is a special case of a tree, consisting only of one root and k leaves. Such graphs are useful for conceptualizing a vertex and its immediate neighbors (ignoring any connectivity among the neighbors).

An important generalization of the concept of a tree is that of a directed acyclic graph (i.e., the DAG). A DAG, as its name implies, is a graph that is directed and that has no directed cycles. However, unlike a directed tree, its underlying graph is not necessarily a tree, in that replacing the arcs with undirected edges may leave a graph that contains cycles. 

For example,

is.dag(dg)

A bipartite graph is a graph G = (V,E) such that the vertex set V may be partitioned into two disjoint sets, say V1 and V2, and each edge in E has one endpoint in V1 and the other in V2. Such graphs typically are used to represent ‘membership’ networks, for example, with ‘members’ denoted by vertices in V1, and the corresponding ‘organizations’, by vertices in V2.




R "mfrow" parameter in par() function vs layout() function.

layout, par(mfrow)

Graph Concepts in igraph

Cited from the Book "Statistical Analysis of Network Data With R"

Edges for which both ends connect to a single vertex are called loops.

More than one edge between a pair of vertices are called multi-edges.

An object with either of these properties is called a multi-graph. A graph that is not a multi-graph is called a simple graph, and its edges are referred to as proper edges

It is straightforward to determine whether or not a graph is simple. 

is.simple(g) 

It is straightforward, and indeed not uncommon in practice, to transform a multi-graph into a weighted graph, wherein each resulting proper edge is equipped with a weight equal to the multiplicity of that edge in the original multigraph.

For example,

E(mg)$weight <- 1
wg2 <- simplify(mg)
is.simple(wg2)
TRUE

The most basic notion of connectivity is that of adjacency. Two vertices u,v ∈ V are said to be adjacent if joined by an edge in E. Such vertices are also referred to as neighbors.

For example, the three neighbors of vertex 5 in our toy graph g are

neighbors(g, 5)

Similarly, two edges e1,e2 ∈ E are adjacent if joined by a common endpoint in V. A vertex v ∈ V is incident on an edge e ∈ E if v is an endpoint of e. From this follows the notion of the degree of a vertex v, say d_v, defined as the number of edges incident on v. 

For example, 

degree(g)

For digraphs, vertex degree is replaced by in-degree (i.e., d_v^in) and out-degree (i.e., d_v^out), which count the number of edges pointing in towards and out from a vertex, respectively. 

For example,

degree(dg, mode="in")

degree(dg, mode="out") 

A walk on a graph G, from v_0 to v_l, is an alternating sequence {v_0, e_1, v_1, e_2, ..., v_{l-1}, e_l, v_l}, where the endpoints of e_i are {v_{i-1}, v_i}. The length of this walk is said to be l. 

Refinements of a walk include trails, which are walks without repeated edges, and paths, which are trails without repeated vertices. A trail for which the beginning and ending vertices are the same is called a circuit. Similarly, a walk of length at least three, for which the beginning and ending vertices are the same, but for which all other vertices are distinct from each other, is called a cycle. Graphs containing no cycles are called acyclic.

A vertex v in a graph G is said to be reachable from another vertex u if there exists a walk from u to v. The graph G is said to be connected if every vertex is reachable from every other. A component of a graph is a maximally connected subgraph. That is, it is a connected subgraph of G for which the addition of any other remaining vertex in V would ruin the property of connectivity. 

For example,

is.connected(g)
TRUE

and therefore consists of only a single component
clusters(g)
$membership
 1 1 1 1 1 1 1

$csize
 7

$no
 1

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

A digraph is strongly connected if every vertex is reachable from every other following the directions of the arcs. I.e., for every pair of distinct vertices u and v there exists a directed path from u to v.

A digraph is weakly connected if when considering it as an undirected graph it is connected. I.e., for every pair of distinct vertices u and v there exists an undirected path (potentially running opposite the direction on an edge) from u to v.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Continued 

For example,
is.connected(dg, mode="weak")
TRUE

is.connected(dg, mode="strong")
FALSE

A common notion of distance between vertices on a graph is defined as the length of the shortest path(s) between the vertices (which we set equal to infinity if no such path exists). This distance is often referred to as geodesic distance, with ‘geodesic’ being another name for shortest paths. The value of the longest distance in a graph is called the diameter of the graph.

For example,

diameter(g, weights=NA)

Sunday 14 January 2018

Decorating Network Graphs in igraph

Cited from the Book "Statistical Analysis of Network Data With R"

Vertex, Edge and Graph Attributes

Equipping a graph with attributes is referred to as decorating the graph. Typically, the vertices or edges of a graph (or both) are decorated with attributes, although the graph as a whole may be decorated as well. In igraph, the elements of graph objects (i.e., particularly the vertex and edge sequences, and subsets thereof) may be equipped with attributes simply by using the ‘$’ operator.

For example,

V(dg)$name 

V(dg)$gender <- c("M","F","M")

Edge attributes similarly are values of variables indexed by adjacent vertex pairs. A graph for which the edges are equipped with weights is referred to as a weighted graph. 

More generally, a weighted graph can be defined as a pair (V,E), where V is a set of vertices, as before, but the elements in E are now non-negative numbers, with one such number for each vertex pair. Analogously, the adjacency matrix A for a weighted graph is defined such that the entry A_{ij} is equal to the corresponding weight for the vertex pair i and j.

For example,

is.weighted(g)
wg <- g
E(wg)$weight <- runif(ecount(wg)) 
is.weighted(wg)

In principle, a graph itself may be decorated with an attribute, and indeed, it is possible to equip graph objects with attributes in igraph. The most natural use of this feature arguably is to equip a graph with relevant background information, such as a name.

For example,

g$name <- "Toy Graph

Using Data Frames

In R, a network graph and all vertex and edge attributes can be conveniently represented using two data frames, one with vertex information, and the other, with edge information. Under this approach, the first column of the vertex data frame contains the vertex names (i.e., either the default numerical labels or symbolic), while each of the other columns contain the values of a given vertex attribute. Similarly, the first two columns of the edge data frame contain an edge list defining the graph, while each of the other columns contain the values of a given edge attribute. 

For example,

list.vertex.attributes(g)

(in addition to the vertex name)