R language: A preliminary study of relational networks

Source: Internet
Author: User

Social networking Analysis (social network ANALYSIS,SNA) has gradually become another favorite in the field of data mining. The essence of SNA is to analyze the community phenomenon of the whole sample by using the relationship between each sample (and therefore also become a relational network), and analyze the role of sample points in community formation and the relationship between communities. The SNA is implemented using the Igraph package in the R language.


Establishing a relational network with R language

(1) Raw data preparation
From<-c ("A", "a", "E", "B", "B", "C", "D", "D", "D", "F")
To<-c ("C", "E", "C", "E", "C", "D", "G", "G", "F", "D")
Data<-data.frame (From=from,to=to)
Data
# # from to
# # 1 A C
# # 2 A E
# 3 E C
# # 4 B E
# # 5 B C
# # 6 C D
# 7 D G
# 8 D G
# # 9 D F
# # ten F D
# then read the name of each sample point:
(Labels<-union (Unique (data[,1)), unique (data[,2]))
# # [1] "a" "E" "B" "C" "D" "F" "G"
# Create a vector ID for the dot ID, and labels the sample name represented by it as the element name.
Ids<-1:length (labels)
Names (IDs) <-labels;ids
# # A E b c D f g
# 1 2 3 4 5 6 7
# by mapping the element value and the element name in the vector IDs, the data frame is transformed into a matrix edges.
From<-as.character (data[,1]); To<-as.character (data[,2])
Edges<-matrix (c (Ids[from],ids[to]), ncol=2)
Edges
# # [, 1] [, 2]
# # [1,] 1 4
# # [2,] 1 2
# # [3,] 2 4
# # [4,] 3 2
# # [5,] 3 4
# # [6,] 4 5
# # [7,] 5 7
# # [8,] 5 7
# # [9,] 5 6
# # [10,] 6 5
(2) Establishing a network of relationships
# 1) Use the Graph.empty function to create an empty relational network g (without defining any point and line information), when the parameter directed=true, the relational network is a graph, when the directed=false is a graph without
Library (Igraph)
G<-graph.empty (DIRECTED=F)
# 2) Add the length (lables) point vertices to the relational network G using the Add.vertices function and add the name Information V (g) $label for each point.
G<-add.vertices (g,length (labels))
V (g) $label =labels
# 3) Use the Add.edges function to add line edges information to the relational network.
G<-add.edges (g,t (edges))
# 4) Merge the repeated lines and weight the number of repetitions as the line weights.
E (g) $weight <-count.multiple (g); E (g) $weight
# # [1] 1 1 1 1 1 1 2 2 2 2
G<-simplify (g,remove.multiple=true,remove.loops=true,edge.attr.comb= "mean")
E (g) $weight
# # [1] 1 1 1 1 1 1 2 2
# The Count.multiple (g) Here is used to calculate the number of repetitions of each line in Network G. Use the simplify function to delete repeated lines (remove.multiple=true) in the diagram and update the line weights E (g) $weight (edge.attr.comb= "mean") using the number of repetitions
Since then, a complete relational network G has been established. The following extracts the department objects in the above code as parameters, and gives a custom function init.igraph the user to establish a relational network


Init.graph<-function (data,dir=f,rem.multi=t) {
Labels<-union (Unique (data[,1]), unique (data[,2]))
Ids<-1:length (labels); names (IDs) <-labels
From<-as.character (data[,1]); To<-as.character (data[,2])
Edges<-matrix (c (Ids[from],ids[to]), ncol=2)
G<-graph.empty (Directed=dir)
G<-add.vertices (g,length (labels))
V (g) $label <-labels
G<-add.edges (g,t (edges))
if (Rem.multi) {
E (g) $weight <-count.multiple (g)
G<-simplify (g,remove.multiple=true,remove.loops=true,edge.attr.comb= "mean")
}
G
}
The data for this parameter is the box that includes the point name and the line data. It is divided into from and to columns. For example:

From<-c ("A", "a", "E", "B", "B", "C", "D", "D", "D", "F")
To<-c ("C", "E", "C", "E", "C", "D", "G", "G", "F", "D")
Data<-data.frame (From=from,to=to)
The parameter dir is a logical variable, which is set to True when a forward graph is established, otherwise it is a non-direction graph. The parameter rem.multi is a logical variable that is set to True when the duplicate variable is deleted and the line weight is updated weight, otherwise it is not deleted and the line weight weight constant is 1.

(3) The implementation of the basic concept of R language

Use the Init.graph function to create an anisotropic and a forward graph, and then use the plot function to draw the image. The code is as follows:
Par (MFCOL=C)
G.undir<-init.graph (data)
Plot (G.undir,edge.width=e (G.undir) $weight, main= "No graph G.undir",
Edge.label=e (G.undir) $weight)
G.undir<-init.graph (data,dir=t)
Plot (G.undir,edge.width=e (G.undir) $weight, main= "Map G.undir",
Edge.label=e (G.undir) $weight)
Par (mfcol=c ())

This is done by setting the Edge.width parameter of the plot function to E (g) $weight, making the line with higher weights thicker.


R language: A preliminary study of relational networks

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.