程序代写代做代考 —


title: “R Notebook”
output:
html_document:
df_print: paged

## Part I: Attribute setup

“`{r}
library(network)
library(ergm)
library(intergraph)
library(sand)
library(dplyr)
source(“mycugtest.R”)
source(“myqaptest.R”)
source(“inv-logit.R”)

g = read.graph(“lotr3.graphml”, format=”graphml”)

typeTable <- data.frame(Type=c("Dwarf", "Elf", "Ent", "Hobbit", "Human", "Orc", "Undead", "Unknown", "Wizard"),Category=c("Fellow", "Fellow", "Other", "Fellow", "Fellow", "Evil", "Evil", "Other", "Fellow")) typeAttr = as.data.frame( V(g)$Type) colnames(typeAttr) <- c("Type") joined = inner_join(typeAttr, typeTable ) ng = set_vertex_attr(g, "Category", value = as.character(joined$Category)) V(ng)$Category ``` ## Part II: CUG and QAP ```{r} cug1 <- mycugtest(ng, assortativity_nominal, cmode="edges",directed = FALSE, types = factor(V(ng)$Type)) print.cug.test(cug1) plot.cug.test(cug1) ``` ```{r} cug2 <- mycugtest(ng, assortativity_nominal, cmode="edges",directed = FALSE, types = factor(V(ng)$Category)) print.cug.test(cug2) plot.cug.test(cug2) ``` ```{r} qap1 <- myqaptest(ng, assortativity_nominal, directed = FALSE, types = factor(V(ng)$Type)) print.qaptest(qap1) plot.qaptest(qap1) ``` ```{r} qap2 <- myqaptest(ng, assortativity_nominal, directed = FALSE, types = factor(V(ng)$Type)) print.qaptest(qap2) plot.qaptest(qap2) ``` ## Part III: ERGM ```{r} net = asNetwork(ng) model1 = ergm(net ~ edges + nodemix("Category", base = c(4,6))) summary(model1) mcmc.diagnostics(model1) m1.gof <- gof(model1) m1.gof plot(m1.gof) ``` ```{r} model1 = ergm(net ~ edges + nodemix("Category", base = c(4,6))) summary(model1) ``` ```{r} mcmc.diagnostics(model1) ``` ```{r} m1.gof <- gof(model1) m1.gof plot(m1.gof) ``` ```{r} model2 = ergm(net ~ edges + nodematch("Category") + degree(1) ) summary(model2) mcmc.diagnostics(model2) m2.gof <- gof(model2) m2.gof plot(m2.gof) ``` ```{r} model3 = ergm(net ~ edges + nodematch("Category") + degree(1) + gwesp(cutoff=7)) summary(model3) mcmc.diagnostics(model3) m3.gof <- gof(model3) m3.gof plot(m3.gof) ``` ```{r} model4 = ergm(net ~ edges + nodemix("Category", base = c(2, 4, 5, 6)) + gwesp(cutoff=7) + degree(2:4)) summary(model4) mcmc.diagnostics(model4) m4.gof <- gof(model4) m4.gof plot(m4.gof) ``` ```{r} model5 = ergm(net ~ edges + nodemix("Category", base = c(2, 4, 5, 6)) + gwesp(cutoff=7) + degree(2:4), control=control.ergm(MCMC.burnin=100000, MCMC.interval=5000, MCMC.samplesize=2048)) summary(model5) mcmc.diagnostics(model5) m5.gof <- gof(model5) m5.gof plot(m5.gof) ``` ## Part IV ```{r} cases <- c("Evil-Evil edge", "Fellow-Fellow edge", "Other edge") edges <- c(1) nodemixEE = c(1, 0, 0) nodemixFF = c(0, 1, 0) cases.df <- data.frame(case=cases, edges=edges, nodemix_EE=nodemixEE, nodemix_FF=nodemixFF) cases.df ``` ```{r} logodds = edges * model1$coef[1] + nodemixEE * model1$coef[2] + c(0, 0, 1) * model1$coef[3] + nodemixFF * model1$coef[4] + c(0, 0, 1) * model1$coef[5] cases.df$Log_odds = logodds cases.df$Cond.probability = invlogit(logodds) ``` ```{r} cases.df ```