网络数据的统计分析-R语言实战

Python017

网络数据的统计分析-R语言实战,第1张

资料:《Statistical Analysis of Network Data with R》

语言R常见的网络分析包:

网络分析研究大部分是描述性的工作。

网络的可视化 即是一门艺术,也是一门科学。

三元闭包体现了社会网络的“传递性”(transitivity),枚举所有节点三元组中构成三角形的比值来表征。

网络的可视化和数值特征化是网络分析的首要步骤之一。

网络可视化视图将数据的多个重要反面整合在一个图表中。

该节点在多大程度上会与同类型或者不同类型的其他节点进行匹配,可以通过一种相关性统计量(所谓的同配系数)进行量化。

将复杂系统中感兴趣的问题与合适的网络概括性度量匹配起来,是网络特征化方法起作用的关键所在。

网络中的频繁子图模式

网络聚类系数的分布,用来检验社会网路的聚集性上

sand安装包

网络数据统计分析 statistical analysis of network data

在CRAN上

G=(V,E)

节点 :vertices 或者 nodes

边:edges 或者 links

节点数量:图的阶数 order

边的数量:图的规模 size

同构图 isomorphic

无向 undirected

有向 directed graph 或者 digraph

边:有向边 directed edges 或 弧 arcs

双向 mutual

小的图形用 formulate来创建

把mg转化为wg2

Zachary 空手道俱乐部网络 (karate club network)

数据集合实际上只存在两个社团,分别以教练为中心和以主管为中心。

Lazega律师网络可视化

srt() 不能用使用 upgrade_graph()d代替

DrL算法,针对大型网络可视化设计的布局算法。

节点的节点,即社区节点(主题节点)

即一个中心节点,一其直接相连的邻居,以及这些节点至今的边。

度值不同的节点以何种方式彼此连接

图的密度

全局聚类系数

局部聚类系数

互惠性 reciprocity

二元组普查

R语言基本数据分析

本文基于R语言进行基本数据统计分析,包括基本作图,线性拟合,逻辑回归,bootstrap采样和Anova方差分析的实现及应用。

不多说,直接上代码,代码中有注释。

1. 基本作图(盒图,qq图)

#basic plot

boxplot(x)

qqplot(x,y)

2. 线性拟合

#linear regression

n = 10

x1 = rnorm(n)#variable 1

x2 = rnorm(n)#variable 2

y = rnorm(n)*3

mod = lm(y~x1+x2)

model.matrix(mod) #erect the matrix of mod

plot(mod) #plot residual and fitted of the solution, Q-Q plot and cook distance

summary(mod) #get the statistic information of the model

hatvalues(mod) #very important, for abnormal sample detection

3. 逻辑回归

#logistic regression

x <- c(0, 1, 2, 3, 4, 5)

y <- c(0, 9, 21, 47, 60, 63) # the number of successes

n <- 70 #the number of trails

z <- n - y #the number of failures

b <- cbind(y, z) # column bind

fitx <- glm(b~x,family = binomial) # a particular type of generalized linear model

print(fitx)

plot(x,y,xlim=c(0,5),ylim=c(0,65)) #plot the points (x,y)

beta0 <- fitx$coef[1]

beta1 <- fitx$coef[2]

fn <- function(x) n*exp(beta0+beta1*x)/(1+exp(beta0+beta1*x))

par(new=T)

curve(fn,0,5,ylim=c(0,60)) # plot the logistic regression curve

3. Bootstrap采样

# bootstrap

# Application: 随机采样,获取最大eigenvalue占所有eigenvalue和之比,并画图显示distribution

dat = matrix(rnorm(100*5),100,5)

no.samples = 200 #sample 200 times

# theta = matrix(rep(0,no.samples*5),no.samples,5)

theta =rep(0,no.samples*5)

for (i in 1:no.samples)

{

j = sample(1:100,100,replace = TRUE)#get 100 samples each time

datrnd = dat[j,]#select one row each time

lambda = princomp(datrnd)$sdev^2#get eigenvalues

# theta[i,] = lambda

theta[i] = lambda[1]/sum(lambda)#plot the ratio of the biggest eigenvalue

}

# hist(theta[1,]) #plot the histogram of the first(biggest) eigenvalue

hist(theta)#plot the percentage distribution of the biggest eigenvalue

sd(theta)#standard deviation of theta

#上面注释掉的语句,可以全部去掉注释并将其下一条语句注释掉,完成画最大eigenvalue分布的功能

4. ANOVA方差分析

#Application:判断一个自变量是否有影响 (假设我们喂3种维他命给3头猪,想看喂维他命有没有用)

#

y = rnorm(9)#weight gain by pig(Yij, i is the treatment, j is the pig_id), 一般由用户自行输入

#y = matrix(c(1,10,1,2,10,2,1,9,1),9,1)

Treatment <- factor(c(1,2,3,1,2,3,1,2,3)) #each {1,2,3} is a group

mod = lm(y~Treatment) #linear regression

print(anova(mod))

#解释:Df(degree of freedom)

#Sum Sq: deviance (within groups, and residuals) 总偏差和

# Mean Sq: variance (within groups, and residuals) 平均方差和

# compare the contribution given by Treatment and Residual

#F value: Mean Sq(Treatment)/Mean Sq(Residuals)

#Pr(>F): p-value. 根据p-value决定是否接受Hypothesis H0:多个样本总体均数相等(检验水准为0.05)

qqnorm(mod$residual) #plot the residual approximated by mod

#如果qqnorm of residual像一条直线,说明residual符合正态分布,也就是说Treatment带来的contribution很小,也就是说Treatment无法带来收益(多喂维他命少喂维他命没区别)

如下面两图分别是

(左)用 y = matrix(c(1,10,1,2,10,2,1,9,1),9,1)和

(右)y = rnorm(9)

的结果。可见如果给定猪吃维他命2后体重特别突出的数据结果后,qq图种residual不在是一条直线,换句话说residual不再符合正态分布,i.e., 维他命对猪的体重有影响。

1.R语言常用在数据统计分析、数据绘图和数据挖掘,是一种编程语言和操作环境。

2.R语言可以下载源代码进行使用,甚至已经编译的可执行文件也能直接下载使用。

3.R语言不只局限于一个平台,可以在常见的Windows系统、MACOS中运行使用,也可以在freeBSD和Linux中运行。

4.R语言可以利用用户编写的包增强,添加R语言中的统计、绘图和IN/OUT功能,可以在经济计量、人文统计中使用。

5.R语言的优势如下:R语言是编程小白的入门语言,语法结构较为简单,而且容易学习,特别是工作中要使用绘图、统计时,学习R语言会非常有优势。

6.R语言是开源软件,是免费的,学习时能大大减少成本。

7.在R语言的内部,有完善的帮助系统,学习中可以根据实例进行查漏补缺。

8.R语言是命令行操作方式,在使用中会更加的灵活,适合初学者入门学习,在数据分析和内容编程中有更好的体验。

9.R语言的安装包仅为40M,相比其他的语言可以说非常的小了。

10.R语言在世界范围的使用率非常广,在职业的规划中R语言很有帮助。