我想对我的社交网络数据执行 Anova 测试,以检查节点分类属性是否与节点的网络约束(连续变量)相关联。
我知道可以使用 UCINET 程序完成此分析,但我想使用 R,因为我正在 R 中进行其余的分析。
据我了解,我需要首先对图进行排列,然后测量节点约束,然后计算所有排列网络的方差分析。
有人知道我如何在 R 中做到这一点吗?
可复制样本
检查某种类型是否与较少的约束相关。
g2 <- make_graph(~ A --+ B:D, B --+ D:F, C--+B , D--+A:B:F:E, E--+ D, F--+ A:E:G, G--+ F)
type<-c("one","two","three","one","two","three","one")
vertex_attr(g2,"type")<-type
constraint<-igraph::constraint(g2)
g2_N<-intergraph::asNetwork(g2)
我按照 Hobson 等人的指示发现了两种潜在的方法。等人。 (2021):
线性回归
adj_matrix<-as_matrix(g2)
obs<-coef(lm(constraint~type))[2:4]
reference_table <- data.frame(
Column1 = numeric(9999), # Empty numeric column
Column2 = numeric(9999), # Empty numeric column
Column3 = numeric(9999))
colnames(reference_table)<-c("one","two","three")
MAT_T<-sna::rmperm(adj_matrix)
for(i in 1:9999){
cp<-igraph::constraint(as_igraph(MAT_T))
reference_table[i,]<-coef(lm(cp~type))[2:4]
MAT_T<-sna::rmperm(MAT_T)
}
reference_table2<-rbind(obs,reference_table)
colnames(reference_table2)<-c("one","two","three")
#p-values
p1<-sum(obs[1]<reference_table2$one)/length(reference_table2$one)
p2<-sum(obs[2]<reference_table2$two)/length(reference_table2$two)
p3<-sum(obs[3]<reference_table2$three)/length(reference_table2$three)
#histogram
par(
xpd=FALSE,
mfrow=c(2,2)
)
hist(reference_table$one,las=1,xlim=c(-1,1),col="grey",border=NA,main="Reference distribution type one",xlab="Test statistic values",cex.lab=1.5)
lines(x=c(obs[1],obs[1]),y=c(0,5000),col="red",lwd=4)
lines(x=rep(quantile(reference_table2$one,0.025),2),y=c(0,5000),col="darkblue",lwd=2,lty=2) lines(x=rep(quantile(reference_table2$one,0.975),2),y=c(0,5000),col="darkblue",lwd=2,lty=2)
hist(reference_table$two,las=1,xlim=c(-1,1),col="grey",border=NA,main="Reference distribution type two",xlab="Test statistic values",cex.lab=1.5)
lines(x=c(obs[2],obs[2]),y=c(0,5000),col="red",lwd=4) lines(x=rep(quantile(reference_table2$two,0.025),2),y=c(0,5000),col="darkblue",lwd=2,lty=2)
lines(x=rep(quantile(reference_table2$two,0.975),2),y=c(0,5000),col="darkblue",lwd=2,lty=2)
hist(reference_table$three,las=1,xlim=c(-1,1),col="grey",border=NA,main="Reference distribution type three",xlab="Test statistic values",cex.lab=1.5)
lines(x=c(obs[3],obs[3]),y=c(0,5000),col="red",lwd=4)
lines(x=rep(quantile(reference_table2$three,0.025),2),y=c(0,5000),col="darkblue",lwd=2,lty=2)
lines(x=rep(quantile(reference_table2$three,0.975),2),y=c(0,5000),col="darkblue",lwd=2,lty=2)
方差分析 我尝试遵循类似的方法,但用于方差分析测试而不是线性回归。然而,我不确定选择哪个观测值来比较观测值,因为似乎使用 p 值并没有 100% 的意义。
#First we plot the relationship
# calculate constraint in the observed network
obs<-constraint
#We then choose our test statistic. do anova
model.aov<-aov(obs~type)
summary_result <- summary(model.aov)
p_value <- summary_result[[1]]$`Pr(>F)`[1] # is this a good choice?
#conduct permutation
reference<-numeric()
MAT_T<-sna::rmperm(adj_matrix)
for(i in 1:999){
cp<-igraph::constraint(as_igraph(MAT_T))
model.aov<-aov(cp~ type)
summary_result <- summary(model.aov)
reference[i]<-summary_result[[1]]$`Pr(>F)`[1]
MAT_T<-sna::rmperm(MAT_T)
}
reference2<-c(p_value,reference)
#We can then calculate a p value by comparing the observed p-value to those in the reference dataset.
permutation_p<-sum(p_value<reference2)/length(reference2)
#histogram
par(xpd=FALSE)
hist(reference,las=1,xlim=c(0,1),col="grey",border=NA,main="Reference distribution",xlab="Test statistic values",cex.lab=1.5)
lines(x=c(p_value,p_value),y=c(0,5000),col="red",lwd=4)
lines(x=rep(quantile(reference2,0.025),2),y=c(0,5000),col="darkblue",lwd=2,lty=2)
lines(x=rep(quantile(reference2,0.975),2),y=c(0,5000),col="darkblue",lwd=2,lty=2)