uniNetLeroux {netcmc}R Documentation

A function that generates samples for a univariate network Leroux model.

Description

This function generates samples for a univariate network Leroux model, which is given by

Yisμisf(yisμis,σe2)   i=1,,Ns, s=1,,S,Y_{i_s}|\mu_{i_s} \sim f(y_{i_s}| \mu_{i_s}, \sigma_{e}^{2}) ~~~ i=1,\ldots, N_{s},~s=1,\ldots,S ,

g(μis)=xisβ+ϕs+jnet(is)wisjuj+wisu,g(\mu_{i_s}) = \boldsymbol{x}^\top_{i_s} \boldsymbol{\beta} + \phi_{s} + \sum_{j\in \textrm{net}(i_s)}w_{i_sj}u_{j} + w^{*}_{i_s}u^{*},

βN(0,αI),\boldsymbol{\beta} \sim \textrm{N}(\boldsymbol{0}, \alpha\boldsymbol{I}),

ϕsϕsN(ρl=1Saslϕlρl=1Sasl+1ρ,τ2ρl=1Sasl+1ρ),\phi_{s} | \boldsymbol{\phi}_{-s} \sim \textrm{N}\bigg(\frac{ \rho \sum_{l = 1}^{S} a_{sl} \phi_{l} }{ \rho \sum_{l = 1}^{S} a_{sl} + 1 - \rho }, \frac{ \tau^{2} }{ \rho \sum_{l = 1}^{S} a_{sl} + 1 - \rho } \bigg),

ujN(0,σu2),u_{j} \sim \textrm{N}(0, \sigma_{u}^{2}),

uN(0,σu2),u^{*} \sim \textrm{N}(0, \sigma_{u}^{2}),

τ2Inverse-Gamma(α1,ξ1),\tau^{2} \sim \textrm{Inverse-Gamma}(\alpha_{1}, \xi_{1}),

ρUniform(0,1),\rho \sim \textrm{Uniform}(0, 1),

σu2Inverse-Gamma(α2,ξ2),\sigma_{u}^{2} \sim \textrm{Inverse-Gamma}(\alpha_{2}, \xi_{2}),

σe2Inverse-Gamma(α3,ξ3).\sigma_{e}^{2} \sim \textrm{Inverse-Gamma}(\alpha_{3}, \xi_{3}).

The covariates for the iith individual in the ssth spatial unit or other grouping are included in a p×1p \times 1 vector xis\boldsymbol{x}_{i_s}. The corresponding p×1p \times 1 vector of fixed effect parameters are denoted by β\boldsymbol{\beta}, which has an assumed multivariate Gaussian prior with mean 0\boldsymbol{0} and diagonal covariance matrix αI\alpha\boldsymbol{I} that can be chosen by the user. A conjugate Inverse-Gamma prior is specified for σe2\sigma_{e}^{2}, and the corresponding hyperparamaterers (α3\alpha_{3}, ξ3\xi_{3}) can be chosen by the user.

Spatial correlation in these areal unit level random effects is most often modelled by a conditional autoregressive (CAR) prior distribution. Using this model spatial correlation is induced into the random effects via a non-negative spatial adjacency matrix A=(asl)S×S\boldsymbol{A} = (a_{sl})_{S \times S}, which defines how spatially close the SS areal units are to each other. The elements of AS×S\boldsymbol{A}_{S \times S} can be binary or non-binary, and the most common specification is that asl=1a_{sl} = 1 if a pair of areal units (Gs\mathcal{G}_{s}, Gl\mathcal{G}_{l}) share a common border or are considered neighbours by some other measure, and asl=0a_{sl} = 0 otherwise. Note, ass=0a_{ss} = 0 for all ss. ϕs=(ϕ1,,ϕs1,ϕs+1,,ϕS)\boldsymbol{\phi}_{-s}=(\phi_1,\ldots,\phi_{s-1}, \phi_{s+1},\ldots,\phi_{S}). Here τ2\tau^{2} is a measure of the variance relating to the spatial random effects ϕ\boldsymbol{\phi}, while ρ\rho controls the level of spatial autocorrelation, with values close to one and zero representing strong autocorrelation and independence respectively. A non-conjugate uniform prior on the unit interval is specified for the single level of spatial autocorrelation ρ\rho. In contrast, a conjugate Inverse-Gamma prior is specified for the random effects variance τ2\tau^{2}, and corresponding hyperparamaterers (α1\alpha_{1}, ξ1\xi_{1}) can be chosen by the user.

The J×1J \times 1 vector of alter random effects are denoted by u=(u1,,uJ)J×1\boldsymbol{u} = (u_{1}, \ldots, u_{J})_{J \times 1} and modelled as independently Gaussian with mean zero and a constant variance, and due to the row standardised nature of W\boldsymbol{W}, jnet(is)wisjuj\sum_{j \in \textrm{net}(i_s)} w_{i_sj} u_{j} represents the average (mean) effect that the peers of individual ii in spatial unit or group ss have on that individual. wisuw^{*}_{i_s}u^{*} is an isolation effect, which is an effect for individuals who don't nominate any friends. This is achieved by setting wis=1w^{*}_{i_s}=1 if individual isi_s nominates no peers and wis=0w^{*}_{i_s}=0 otherwise, and if wis=1w^{*}_{i_s}=1 then clearly jnet(is)wisjujr=0\sum_{j\in \textrm{net}(i_{s})}w_{i_{s}j}u_{jr}=0 as net(is)\textrm{net}(i_{s}) is the empty set. A conjugate Inverse-Gamma prior is specified for the random effects variance σu2\sigma_{u}^{2}, and the corresponding hyperparamaterers (α2\alpha_{2}, ξ2\xi_{2}) can be chosen by the user.

The exact specification of each of the likelihoods (binomial, Gaussian, and Poisson) are given below:

Binomial: YisBinomial(nis,θis) and g(μis)=ln(θis/(1θis)),\textrm{Binomial:} ~ Y_{i_s} \sim \textrm{Binomial}(n_{i_s}, \theta_{i_s}) ~ \textrm{and} ~ g(\mu_{i_s}) = \textrm{ln}(\theta_{i_s} / (1 - \theta_{i_s})),

Gaussian: YisN(μis,σe2) and g(μis)=μis,\textrm{Gaussian:} ~ Y_{i_s} \sim \textrm{N}(\mu_{i_s}, \sigma_{e}^{2}) ~ \textrm{and} ~ g(\mu_{i_s}) = \mu_{i_s},

Poisson: YisPoisson(μis) and g(μis)=ln(μis).\textrm{Poisson:} ~ Y_{i_s} \sim \textrm{Poisson}(\mu_{i_s}) ~ \textrm{and} ~ g(\mu_{i_s}) = \textrm{ln}(\mu_{i_s}).

Usage

uniNetLeroux(formula, data, trials, family,
squareSpatialNeighbourhoodMatrix, spatialAssignment, W, numberOfSamples = 10, 
burnin = 0, thin = 1, seed = 1, trueBeta = NULL, 
trueSpatialRandomEffects = NULL, trueURandomEffects = NULL, 
trueSpatialTauSquared = NULL, trueSpatialRho = NULL, trueSigmaSquaredU = NULL,
trueSigmaSquaredE = NULL, covarianceBetaPrior = 10^5, a1 = 0.001, b1 = 0.001, 
a2 = 0.001, b2 = 0.001, a3 = 0.001, b3 = 0.001, 
centerSpatialRandomEffects = TRUE, centerURandomEffects = TRUE)

Arguments

formula

A formula for the covariate part of the model using a similar syntax to that used in the lm() function.

data

An optional data.frame containing the variables in the formula.

trials

A vector the same length as the response containing the total number of trials nisn_{i_s}. Only used if family\texttt{family}=“binomial".

family

The data likelihood model that must be “gaussian", “poisson" or “binomial".

squareSpatialNeighbourhoodMatrix

An S×SS \times S symmetric and non-negative neighbourhood matrix A=(asl)S×S\boldsymbol{A} = (a_{sl})_{S \times S}.

W

A matrix W\boldsymbol{W} that encodes the social network structure and whose rows sum to 1.

spatialAssignment

The binary matrix of individual's assignment to spatial area used in the model fitting process.

numberOfSamples

The number of samples to generate pre-thin.

burnin

The number of MCMC samples to discard as the burn-in period.

thin

The value by which to thin numberOfSamples\texttt{numberOfSamples}.

seed

A seed for the MCMC algorithm.

trueBeta

If available, the true value of β\boldsymbol{\beta}.

trueSpatialRandomEffects

If available, the true value of ϕ\boldsymbol{\phi}.

trueURandomEffects

If available, the true value of u\boldsymbol{u}.

trueSpatialTauSquared

If available, the true value of τ2\tau^{2}.

trueSpatialRho

If available, the true value ofρ\rho.

trueSigmaSquaredU

If available, the true value of σu2\sigma_{u}^{2}.

trueSigmaSquaredE

If available, the true value of σe2\sigma_{e}^{2}.

covarianceBetaPrior

A scalar prior α\alpha for the covariance parameter of the beta prior, such that the covariance is αI\alpha\boldsymbol{I}.

a1

The shape parameter for the Inverse-Gamma distribution relating to the spatial random effects α1\alpha_{1}.

b1

The scale parameter for the Inverse-Gamma distribution relating to the spatial random effects ξ1\xi_{1}.

a2

The shape parameter for the Inverse-Gamma distribution relating to the network random effects α2\alpha_{2}.

b2

The scale parameter for the Inverse-Gamma distribution relating to the network random effects ξ2\xi_{2}.

a3

The shape parameter for the Inverse-Gamma distribution relating to the error terms α3\alpha_{3}. Only used if family\texttt{family}=“gaussian".

b3

The scale parameter for the Inverse-Gamma distribution relating to the error terms ξ3\xi_{3}. Only used if family\texttt{family}=“gaussian".

centerSpatialRandomEffects

A choice to center the spatial random effects after each iteration of the MCMC sampler.

centerURandomEffects

A choice to center the network random effects after each iteration of the MCMC sampler.

Value

call

The matched call.

y

The response used.

X

The design matrix used.

standardizedX

The standardized design matrix used.

squareSpatialNeighbourhoodMatrix

The spatial neighbourhood matrix used.

spatialAssignment

The spatial assignment matrix used.

W

The network matrix used.

samples

The matrix of simulated samples from the posterior distribution of each parameter in the model (excluding random effects).

betaSamples

The matrix of simulated samples from the posterior distribution of β\boldsymbol{\beta} parameters in the model.

spatialTauSquaredSamples

The vector of simulated samples from the posterior distribution of τ2\tau^{2} in the model.

spatialRhoSamples

The vector of simulated samples from the posterior distribution of ρ\rho in the model.

sigmaSquaredUSamples

The vector of simulated samples from the posterior distribution of σu2\sigma_{u}^{2} in the model.

sigmaSquaredESamples

The vector of simulated samples from the posterior distribution of σe2\sigma_{e}^{2} in the model.

spatialRandomEffectsSamples

The matrix of simulated samples from the posterior distribution of spatial/grouping random effects ϕ\boldsymbol{\phi} in the model.

uRandomEffectsSamples

The matrix of simulated samples from the posterior distribution of network random effects u\boldsymbol{u} in the model.

acceptanceRates

The acceptance rates of parameters in the model (excluding random effects) from the MCMC sampling scheme .

spatialRandomEffectsAcceptanceRate

The acceptance rates of spatial/grouping random effects in the model from the MCMC sampling scheme.

uRandomEffectsAcceptanceRate

The acceptance rates of network random effects in the model from the MCMC sampling scheme.

timeTaken

The time taken for the model to run.

burnin

The number of MCMC samples to discard as the burn-in period.

thin

The value by which to thin numberOfSamples\texttt{numberOfSamples}.

DBar

DBar for the model.

posteriorDeviance

The posterior deviance for the model.

posteriorLogLikelihood

The posterior log likelihood for the model.

pd

The number of effective parameters in the model.

DIC

The DIC for the model.

Author(s)

George Gerogiannis

Examples

  #################################################
  #### Run the model on simulated data
  #################################################
  #### Load other libraries required
  library(MCMCpack)
  
  #### Set up a network
  observations <- 200
  numberOfMultipleClassifications <- 50
  W <- matrix(rbinom(observations * numberOfMultipleClassifications, 1, 0.05), 
              ncol = numberOfMultipleClassifications)
  numberOfActorsWithNoPeers <- sum(apply(W, 1, function(x) { sum(x) == 0 }))
  peers <- sample(1:numberOfMultipleClassifications, numberOfActorsWithNoPeers,
  TRUE)
  actorsWithNoPeers <- which(apply(W, 1, function(x) { sum(x) == 0 }))
  for(i in 1:numberOfActorsWithNoPeers) {
    W[actorsWithNoPeers[i], peers[i]] <- 1
  }
  W <- t(apply(W, 1, function(x) { x / sum(x) }))
  
  #### Set up a spatial structure
  numberOfSpatialAreas <- 100
  factor = sample(1:numberOfSpatialAreas, observations, TRUE)
  spatialAssignment = matrix(NA, ncol = numberOfSpatialAreas, 
                             nrow = observations)
  for(i in 1:length(factor)){
    for(j in 1:numberOfSpatialAreas){
      if(factor[i] == j){
        spatialAssignment[i, j] = 1
      } else {
        spatialAssignment[i, j] = 0
      }
    }
  }
  
  gridAxis = sqrt(numberOfSpatialAreas)
  easting = 1:gridAxis
  northing = 1:gridAxis
  grid = expand.grid(easting, northing)
  numberOfRowsInGrid = nrow(grid)
  distance = as.matrix(dist(grid))
  squareSpatialNeighbourhoodMatrix = array(0, c(numberOfRowsInGrid, 
                                                numberOfRowsInGrid))
  squareSpatialNeighbourhoodMatrix[distance==1] = 1

  #### Generate the covariates and response data
  X <- matrix(rnorm(2 * observations), ncol = 2)
  colnames(X) <- c("x1", "x2")
  beta <- c(2, -2, 2)
  
  spatialRho <- 0.5
  spatialTauSquared <- 2
  spatialPrecisionMatrix = spatialRho * 
    (diag(apply(squareSpatialNeighbourhoodMatrix, 1, sum)) -
     squareSpatialNeighbourhoodMatrix) + (1 - spatialRho) * 
     diag(rep(1, numberOfSpatialAreas))
  spatialCovarianceMatrix = solve(spatialPrecisionMatrix)
  spatialPhi = mvrnorm(n = 1, mu = rep(0, numberOfSpatialAreas), 
                       Sigma = (spatialTauSquared * spatialCovarianceMatrix))
  
  sigmaSquaredU <- 2
  uRandomEffects <- rnorm(numberOfMultipleClassifications, mean = 0, 
                          sd = sqrt(sigmaSquaredU))
  
  logit <- cbind(rep(1, observations), X) %*% beta + 
    spatialAssignment %*% spatialPhi + W %*% uRandomEffects
  prob <- exp(logit) / (1 + exp(logit))
  trials <- rep(50, observations)
  Y <- rbinom(n = observations, size = trials, prob = prob)
  data <- data.frame(cbind(Y, X))
  
  #### Run the model
  formula <- Y ~ x1 + x2
  ## Not run: model <- uniNetLeroux(formula = formula, data = data, 
    family="binomial",  W = W,
    spatialAssignment = spatialAssignment, 
    squareSpatialNeighbourhoodMatrix = squareSpatialNeighbourhoodMatrix,
    trials = trials, numberOfSamples = 10000, 
    burnin = 10000, thin = 10, seed = 1)
## End(Not run)

[Package netcmc version 1.0.2 Index]