Download R source file
rm(list = ls())
gc()
library(dplyr);
library(readr);
library(magrittr)
library(tidyr)
library(ggplot2)
setwd("C:/jwm/SNH18wd")
AHS_Base <- read_csv ('ahs_wpvar.csv',
col_names = TRUE);
AHS_adjlist <- AHS_Base %>%
select(ego_nid, mfnid_1:mfnid_5, ffnid_1:ffnid_5, grade, sex, commcnt) %>%
filter(commcnt==1);
AHS_Edges <- AHS_adjlist %>%
rename( id = `ego_nid`,
gender = `sex`) %>%
gather(Alter_Label, Target, mfnid_1:mfnid_5, ffnid_1:ffnid_5, na.rm = TRUE)
AHS_Edges=AHS_Edges %>% filter (Target != 99999);
AHS_Edges=AHS_Edges %>%select(id, Target);
library("statnet")
g=as.network(AHS_Edges)
g %v% "grade" <- AHS_adjlist$grade
g %v% "sex" <- AHS_adjlist$sex
g %v% "degree" <- degree(g)
tri=triad.census(g)
obs_tran=gtrans(g)
dyads=dyad.census(g)
dyads
ran_tran=vector(length=500)
j=1
while(j<501){
r=rguman(1,nv=71,mut=dyads[1],asym=dyads[2],null=dyads[3])
ran_tran[j]=gtrans(r)
if (is.na(ran_tran[j])) next else j=j+1
}
dt=as.data.frame(ran_tran)
ggplot(dt,aes(x=ran_tran))+geom_histogram()+
geom_vline(xintercept=obs_tran,
linetype=1,
color="red")
g<-rguman(1,15,mut=0.25,asym=0.05,null=0.7)
cug.test(g,gtrans,cmode="size")
cug.test(g,gtrans,cmode="edges")
cug.test(g,gtrans,cmode="dyad.census")
summarise(dt,mean(r.p.t))
p=length(r.p.t[r.p.t>=Pt])/1000
p
r.triad=triad.census(r)