daily pastebin goal
80%
SHARE
TWEET

Untitled

a guest Feb 11th, 2019 55 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. install.packages("sna")
  2. install.packages("igraph")
  3. library(sna)
  4. library(igraph)
  5.  
  6. # This is the Log-likelihood function in Meneses et al.'s article (2017).
  7. # This function receives as inputs:
  8. #    p: parameter of randomness, based on Watts and Strogatz's model, and
  9. #    AdjMat: adjacency matrix of the social network.
  10.  
  11. loglike <- function(p, AdjMat){
  12.     n <- nrow(AdjMat)                 # n: number of nodes
  13.     Dens <- sna::gden(AdjMat)         # Dens: network density of the social network.
  14.     K <- round(Dens * (n - 1)/2, 0)   # K: number of neighbors a node has to its rights side in the regular lattice before rewiring.
  15.  
  16.     # logProbs: vector of the logarithm of the probabilities of observing the degree centrality of each node in social network, based on Menezes et al.'s paper.
  17.     logProbs <- log(apply(X = matrix(seq(n), ncol = 1),
  18.         MARGIN = 1,
  19.         FUN = function(j){
  20.             m <- sum(AdjMat[, j])
  21.             LowLim <- max(c(2 * K - m, 0))
  22.             UppLim <- min(c(n - 1 - m, 2 * K))
  23.             a <- 1/2 * p * (n - 1 - 2 * K)/n
  24.             b <- p/n
  25.             Factor1 <- dbinom(x = LowLim:UppLim, size = 2 * K, prob = a)
  26.             Factor2 <- dbinom(x = m - 2 * K + LowLim:UppLim, size = (n - 2) * K, prob = b)
  27.             sum(Factor1 * Factor2)
  28.             }
  29.         ))
  30.     sum(logProbs)      # output: loglikelihood of observed data.
  31.     }
  32.  
  33. # This part of the program attemps to build the sampling distribution of the maximum likelihood estimator (MLE) of the randomness parameter p, with 0 <= p <= 1.
  34.  
  35. p <- 0.2     # True value of the randomness parameter.
  36.  
  37. # This part of the program simulates 1000 times a social network of N nodes with density d and parameter of randomness p. For each social network, I obtain the MLE of p. Then, I build the sampling distribution of the MLE of p.
  38.  
  39. d <- 0.1  
  40. N <- 2000
  41. MLE_p <- NULL
  42. for(i in 1:100){
  43.     K <- round(d * (N - 1)/2, 0)
  44.     AdjMat <- as.matrix(igraph::get.adjacency(igraph::watts.strogatz.game(dim = 1,
  45.     size = N, nei = K, p = p)))
  46.     OptProblem <- nlminb(start = runif(1),
  47.         objective = function(x) -loglike(p = x, AdjMat = AdjMat),
  48.         lower = 0, upper = 1,
  49.         control = list(trace = TRUE))
  50.     MLE_p <- c(MLE_p, OptProblem$par)
  51.     }
  52.  
  53. hist(MLE_p, col = "gray60")
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top