Package 'netcmc'

Title: Spatio-Network Generalised Linear Mixed Models for Areal Unit and Network Data
Description: Implements a class of univariate and multivariate spatio-network generalised linear mixed models for areal unit and network data, with inference in a Bayesian setting using Markov chain Monte Carlo (MCMC) simulation. The response variable can be binomial, Gaussian, or Poisson. Spatial autocorrelation is modelled by a set of random effects that are assigned a conditional autoregressive (CAR) prior distribution following the Leroux model (Leroux et al. (2000) <doi:10.1007/978-1-4612-1284-3_4>). Network structures are modelled by a set of random effects that reflect a multiple membership structure (Browne et al. (2001) <doi:10.1177/1471082X0100100202>).
Authors: George Gerogiannis, Mark Tranmer, Duncan Lee
Maintainer: George Gerogiannis <[email protected]>
License: GPL (>= 2)
Version: 1.0.2
Built: 2024-11-27 06:54:47 UTC
Source: CRAN

Help Index


An R Package for Bayesian Social Network Modelling

Description

Implements a class of univariate and multivariate spatio-network generalised linear mixed models, with inference in a Bayesian setting using Markov chain Monte Carlo (MCMC) simulation. The response variable can be binomial, Gaussian, and Poisson.

Details

Package: netcmc
Type: Package
Version: 1.0
Date: 2022-01-24
License: GPL (>= 2)

Author(s)

George Gerogiannis <[email protected]>

Examples

## See the examples in the function specific help files.

A function that extracts valuable properties from a raw social network.

Description

This function transforms a network, which is a data.frame type in a specified format, in to a resultant nn by nn adjacency matrix, where aija_{ij} = 0 if vertex ii and jj (iji \neq j) are not adjacent i.e. vertex ii and jj are not the head/tail of an edge ee and aija_{ij} = 1 if vertex ii and jj (iji \neq j) are adjacent i.e. vertex ii and jj are the head/tail of an edge ee. aija_{ij} = 0 when i=ji = j.

Usage

getAdjacencyMatrix(rawNetwork)

Arguments

rawNetwork

The data.frame which encodes information about the network. The dimensions of the matrix are nn by (l+1)(l+1).The data.frame contains one column corresponding to the labels for each of the nn vertices in the network, the column name for this should be ‘labels’. The other ll columns corresponds to the corresponds to the vertices which are adjacent to each of the nn vertices in the network. It is important to note that the label of a vertex should not be 0. The nnth vertex can be adjacent to a maximum of ll other vertices.

Value

adjacencyMatrix

The resultant adjaceny matrix for the rawNetwork data.frame.

nonnominators

The individuals in the social network who are nominees of at least one other individual but were not in the set of individuals who did the nominating.

vertexNoOutdegrees

The individuals in the social network that have an outdegree of 0.

vertexNoIndegrees

The individuals in the social network that have an indegree of 0.

vertexIsolates

The individuals in the social network that have an outdegree and indegree of 0.

Author(s)

George Gerogiannis

Examples

rawNetwork = matrix(NA, 4, 3)
rawNetwork = as.data.frame(rawNetwork)
colnames(rawNetwork)[1] = "labels"
rawNetwork[, 1] = c("A", "B", "C", "D")
rawNetwork[, 2] = c(0, "C", "D", 0)
rawNetwork[, 3] = c("B", 0, "A", "C")
getAdjacencyMatrix(rawNetwork)

rawNetwork = matrix(NA, 4, 3)
rawNetwork = as.data.frame(rawNetwork)
colnames(rawNetwork)[2] = "labels"
rawNetwork[, 1] = c(NA, "Charlie", "David", 0)
rawNetwork[, 2] = c("Alistar", "Bob", "Charlie", "David")
rawNetwork[, 3] = c("Bob", NA, "Alistar", "Charlie")
getAdjacencyMatrix(rawNetwork)

rawNetwork = matrix(NA, 4, 3)
rawNetwork = as.data.frame(rawNetwork)
colnames(rawNetwork)[1] = "labels"
rawNetwork[, 1] = c(245, 344, 234, 104)
rawNetwork[, 2] = c(NA, 234, 104, NA)
rawNetwork[, 3] = c(344, 0, 245, 234)
getAdjacencyMatrix(rawNetwork)

rawNetwork = matrix(NA, 4, 3)
rawNetwork = as.data.frame(rawNetwork)
colnames(rawNetwork)[1] = "labels"
rawNetwork[, 1] = c(245, 344, 234, 104)
rawNetwork[, 2] = c(32, 234, 104, 0)
rawNetwork[, 3] = c(344, 20, 245, 234)
getAdjacencyMatrix(rawNetwork)

rawNetwork = matrix(NA, 4, 3)
rawNetwork = as.data.frame(rawNetwork)
colnames(rawNetwork)[1] = "labels"
rawNetwork[, 1] = c("Alistar", "Bob", "Charlie", "David")
rawNetwork[, 2] = c(NA, "Charlie", "David", 0)
rawNetwork[, 3] = c("Bob", "Blaine", "Alistar", "Charlie")
getAdjacencyMatrix(rawNetwork)

rawNetwork = matrix(NA, 4, 3)
rawNetwork = as.data.frame(rawNetwork)
colnames(rawNetwork)[1] = "labels"
rawNetwork[, 1] = c("Alistar", "Bob", "Charlie", "David")
rawNetwork[, 2] = c(0, "Charlie", 0, 0)
rawNetwork[, 3] = c("Bob", "Blaine", "Alistar", 0)
getAdjacencyMatrix(rawNetwork)

rawNetwork = matrix(NA, 4, 3)
rawNetwork = as.data.frame(rawNetwork)
colnames(rawNetwork)[1] = "labels"
rawNetwork[, 1] = c(245, 344, 234, 104)
rawNetwork[, 2] = c(32, 0, 104, 0)
rawNetwork[, 3] = c(34, 0, 245, 234)
getAdjacencyMatrix(rawNetwork)

A function that generates a data.frame that is the membership matrix of the network.

Description

A function that generates a data.frame that is the membership matrix of the network given individual IDs and the alters that they have nominated.

Usage

getMembershipMatrix(individualID, alters)

Arguments

individualID

A data.frame which stores the IDs of the individuals that nominate alters.

alters

A data.frame which stores the alters of a given individual.

Value

membershipMatrix

The resultant data.frame.

Author(s)

George Gerogiannis

Examples

individualID = data.frame(c(1, 2, 3))
  alters = data.frame(c(5, 3, 2), c(5, 6, 1))
  getMembershipMatrix(individualID, alters)
  
  individualID = data.frame(c(1, 2, 3))
  alters = data.frame(c(NA, 3, 2), c(NA, NA, 1))
  getMembershipMatrix(individualID, alters)
  
  individualID = data.frame(c(1, 2, 3))
  alters = data.frame(c(NA, 3, NA), c(NA, NA, 1))
  getMembershipMatrix(individualID, alters)
  
  individualID = data.frame(c(1, 2, 3))
  alters = data.frame(c(NA, 3, NA), c(6, NA, 1))
  getMembershipMatrix(individualID, alters)

A function that generates a data.frame that stores the number of alters with a given level of a factor an individual has.

Description

This is a function that can be used to generates a data.frame that stores the number of alters with a given level of a factor an individual has.

Usage

getTotalAltersByStatus(individualID, status, alters)

Arguments

individualID

A data.frame which stores the IDs of the individuals that nominate alters.

status

A data.frame which stores the levels of a variable.

alters

A data.frame which stores the alters of a given individual.

Value

totalAltersByStatus

The resultant data.frame.

Author(s)

George Gerogiannis

Examples

individualID = data.frame(c(1, 2, 3, 4))
status = data.frame(c(10, 20, 30, 20))
alters = data.frame(c(4, 3, 2, 1), c(3, 4, 1, 2), c(2, 1, 4, 3))
totalAltersByStatus = getTotalAltersByStatus(individualID, status, alters)

individualID = data.frame(c(1, 2, 3, 4))
status = data.frame(c("RegularSmoke", "Nonsmoker", "CasualSmoker", "Nonsmoker"))
alters = data.frame(c(4, 3, 2, 1), c(3, 4, 1, 2), c(5, 1, 5, 3))
totalAltersByStatus = getTotalAltersByStatus(individualID, status, alters)

individualID = data.frame(c(1, 2, 3, 4))
status = data.frame(c(NA, "Nonsmoker", "CasualSmoker", "Nonsmoker"))
alters = data.frame(c(4, 3, 2, 1), c(3, 4, 1, 2), c(5, 1, 5, 3))
totalAltersByStatus = getTotalAltersByStatus(individualID, status, alters)

individualID = data.frame(c(10, 20))
status = data.frame(c(NA, "Nonsmoker"))
alters = data.frame(c(NA, 10), c(20, NA))
totalAltersByStatus = getTotalAltersByStatus(individualID, status, alters)

individualID = data.frame(c(NA, 20))
status = data.frame(c("Smoker", "Nonsmoker"))
alters = data.frame(c(NA, 10), c(20, NA))
totalAltersByStatus = getTotalAltersByStatus(individualID, status, alters)

A function that generates samples for a multivariate fixed effects and network model.

Description

This function that generates samples for a multivariate fixed effects and network model, which is given by

Yisrμisrf(yisrμisr,σer2)   i=1,,Ns, s=1,,S, r=1,,R,Y_{i_sr}|\mu_{i_sr} \sim f(y_{i_sr}| \mu_{i_sr}, \sigma_{er}^{2}) ~~~ i=1,\ldots, N_{s},~s=1,\ldots,S ,~r=1,\ldots,R,

g(μisr)=xisβr+jnet(is)wisjujr+wisur,g(\mu_{i_sr}) = \boldsymbol{x}^\top_{i_s} \boldsymbol{\beta}_{r} + \sum_{j\in \textrm{net}(i_s)}w_{i_sj}u_{jr}+ w^{*}_{i_s}u^{*}_{r},

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

uj=(u1j,,uRj)N(0,Σu),\boldsymbol{u}_{j} = (u_{1j},\ldots, u_{Rj}) \sim \textrm{N}(\boldsymbol{0}, \boldsymbol{\Sigma}_{\boldsymbol{u}}),

u=(u1,,uR)N(0,Σu),\boldsymbol{u}^{*} = (u_{1}^*,\ldots, u_{R}^*) \sim \textrm{N}(\boldsymbol{0}, \boldsymbol{\Sigma}_{\boldsymbol{u}}),

ΣuInverse-Wishart(ξu,Ωu),\boldsymbol{\Sigma}_{\boldsymbol{u}} \sim \textrm{Inverse-Wishart}(\xi_{\boldsymbol{u}}, \boldsymbol{\Omega}_{\boldsymbol{u}}),

σer2Inverse-Gamma(α3,ξ3).\sigma_{er}^{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 relating to the rrth response are denoted by βr\boldsymbol{\beta}_{r}, 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 σer2\sigma_{er}^{2}, and the corresponding hyperparamaterers (α3\alpha_{3}, ξ3\xi_{3}) can be chosen by the user.

The R×1R \times 1 vector of random effects for the jjth alter is denoted by uj=(uj1,,ujR)R×1\boldsymbol{u}_{j} = (u_{j1}, \ldots, u_{jR})_{R \times 1}, while the R×1R \times 1 vector of isolation effects for all RR outcomes is denoted by u=(u1,,uR)\boldsymbol{u}^{*} = (u_{1}^*,\ldots, u_{R}^*), and both are assigned multivariate Gaussian prior distributions. The unstructured covariance matrix Σu\boldsymbol{\Sigma}_{\boldsymbol{u}} captures the covariance between the RR outcomes at the network level, and a conjugate Inverse-Wishart prior is specified for this covariance matrix Σu\boldsymbol{\Sigma}_{\boldsymbol{u}}. The corresponding hyperparamaterers (ξu\xi_{\boldsymbol{u}}, Ωu\boldsymbol{\Omega}_{\boldsymbol{u}}) can be chosen by the user.

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

Binomial: YisrBinomial(nisr,θisr) and g(μisr)=ln(θisr/(1θisr)),\textrm{Binomial:} ~ Y_{i_sr} \sim \textrm{Binomial}(n_{i_sr}, \theta_{i_sr}) ~ \textrm{and} ~ g(\mu_{i_sr}) = \textrm{ln}(\theta_{i_sr} / (1 - \theta_{i_sr})),

Gaussian: YisrN(μisr,σer2) and g(μisr)=μisr,\textrm{Gaussian:} ~ Y_{i_sr} \sim \textrm{N}(\mu_{i_sr}, \sigma_{er}^{2}) ~ \textrm{and} ~ g(\mu_{i_sr}) = \mu_{i_sr},

Poisson: YisrPoisson(μisr) and g(μisr)=ln(μisr).\textrm{Poisson:} ~ Y_{i_sr} \sim \textrm{Poisson}(\mu_{i_sr}) ~ \textrm{and} ~ g(\mu_{i_sr}) = \textrm{ln}(\mu_{i_sr}).

Usage

multiNet(formula, data, trials, family, W, numberOfSamples = 10, burnin = 0, 
thin = 1, seed = 1, trueBeta = NULL, trueURandomEffects = NULL, 
trueVarianceCovarianceU = NULL, trueSigmaSquaredE = NULL, 
covarianceBetaPrior = 10^5, xi, omega, a3 = 0.001, b3 = 0.001, 
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 nisrn_{i_sr}. Only used if family\texttt{family}=“binomial".

family

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

W

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

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 β1,,βR\boldsymbol{\beta}_{1}, \ldots, \boldsymbol{\beta}_{R}.

trueURandomEffects

If available, the true values of u1,,uJ,u\boldsymbol{u}_{1}, \ldots, \boldsymbol{u}_{J}, \boldsymbol{u}^{*}.

trueVarianceCovarianceU

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

trueSigmaSquaredE

If available, the true value of σe12\sigma_{e1}^{2}, \ldots, σeR2\sigma_{eR}^{2}. Only used if family\texttt{family}=“gaussian".

covarianceBetaPrior

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

xi

The degrees of freedom parameter for the Inverse-Wishart distribution relating to the network random effects ξu\xi_{\boldsymbol{u}}.

omega

The scale parameter for the Inverse-Wishart distribution relating to the network random effects Ωu\boldsymbol{\Omega}_{\boldsymbol{u}}.

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".

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.

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 β1,,βR\boldsymbol{\beta}_{1}, \ldots, \boldsymbol{\beta}_{R} parameters in the model.

varianceCovarianceUSamples

The matrix of simulated samples from the posterior distribution of Σu\boldsymbol{\Sigma}_{\boldsymbol{u}} in the model.

uRandomEffectsSamples

The matrix of simulated samples from the posterior distribution of network random effects u1,,uJ,u\boldsymbol{u}_{1}, \ldots, \boldsymbol{u}_{J}, \boldsymbol{u}^{*} in the model.

sigmaSquaredESamples

The vector of simulated samples from the posterior distribution of σe12\sigma_{e1}^{2}, \ldots, σeR2\sigma_{eR}^{2} in the model. Only used if family\texttt{family}=“gaussian".

acceptanceRates

The acceptance rates of parameters 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


A function that generates samples for a multivariate fixed effects, spatial, and network model.

Description

This function that generates samples for a multivariate fixed effects, spatial, and network model, which is given by

Yisrμisrf(yisrμisr,σer2)   i=1,,Ns, s=1,,S, r=1,,R,Y_{i_sr}|\mu_{i_sr} \sim f(y_{i_sr}| \mu_{i_sr}, \sigma_{er}^{2}) ~~~ i=1,\ldots, N_{s},~s=1,\ldots,S ,~r=1,\ldots,R,

g(μisr)=xisβr+ϕsr+jnet(is)wisjujr+wisur,g(\mu_{i_sr}) = \boldsymbol{x}^\top_{i_s} \boldsymbol{\beta}_{r} + \phi_{sr} + \sum_{j\in \textrm{net}(i_s)}w_{i_sj}u_{jr}+ w^{*}_{i_s}u^{*}_{r},

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

ϕr=(ϕ1r,,ϕSr)N(0,τr2(ρr(diag(A1)A)+(1ρr)I)1),\boldsymbol{\phi}_{r} = (\phi_{1r},\ldots, \phi_{Sr}) \sim \textrm{N}(\boldsymbol{0}, \tau_{r}^{2}(\rho_{r}(\textrm{diag}(\boldsymbol{A1})-\boldsymbol{A})+(1-\rho_{r})\boldsymbol{I})^{-1}),

uj=(u1j,,uRj)N(0,Σu),\boldsymbol{u}_{j} = (u_{1j},\ldots, u_{Rj}) \sim \textrm{N}(\boldsymbol{0}, \boldsymbol{\Sigma}_{\boldsymbol{u}}),

u=(u1,,uR)N(0,Σu),\boldsymbol{u}^{*} = (u_{1}^*,\ldots, u_{R}^*) \sim \textrm{N}(\boldsymbol{0}, \boldsymbol{\Sigma}_{\boldsymbol{u}}),

τr2Inverse-Gamma(a1,b1),\tau_{r}^{2} \sim \textrm{Inverse-Gamma}(a_{1}, b_{1}),

ρrUniform(0,1),\rho_{r} \sim \textrm{Uniform}(0, 1),

ΣuInverse-Wishart(ξu,Ωu),\boldsymbol{\Sigma}_{\boldsymbol{u}} \sim \textrm{Inverse-Wishart}(\xi_{\boldsymbol{u}}, \boldsymbol{\Omega}_{\boldsymbol{u}}),

σer2Inverse-Gamma(α3,ξ3).\sigma_{er}^{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 relating to the rrth response are denoted by βr\boldsymbol{\beta}_{r}, 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 σer2\sigma_{er}^{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. τr2\tau^{2}_{r} measures the variance of these random effects for the rrth response, where a conjugate Inverse-Gamma prior is specified for τr2\tau^{2}_{r} and the corresponding hyperparamaterers (a1a_{1}, b1b_{1}) can be chosen by the user. ρr\rho_{r} controls the level of spatial autocorrelation. A non-conjugate uniform prior is specified for ρr\rho_{r}.

The R×1R \times 1 vector of random effects for the jjth alter is denoted by uj=(uj1,,ujR)R×1\boldsymbol{u}_{j} = (u_{j1}, \ldots, u_{jR})_{R \times 1}, while the R×1R \times 1 vector of isolation effects for all RR outcomes is denoted by u=(u1,,uR)\boldsymbol{u}^{*} = (u_{1}^*,\ldots, u_{R}^*), and both are assigned multivariate Gaussian prior distributions. The unstructured covariance matrix Σu\boldsymbol{\Sigma}_{\boldsymbol{u}} captures the covariance between the RR outcomes at the network level, and a conjugate Inverse-Wishart prior is specified for this covariance matrix Σu\boldsymbol{\Sigma}_{\boldsymbol{u}}. The corresponding hyperparamaterers (ξu\xi_{\boldsymbol{u}}, Ωu\boldsymbol{\Omega}_{\boldsymbol{u}}) can be chosen by the user.

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

Binomial: YisrBinomial(nisr,θisr) and g(μisr)=ln(θisr/(1θisr)),\textrm{Binomial:} ~ Y_{i_sr} \sim \textrm{Binomial}(n_{i_sr}, \theta_{i_sr}) ~ \textrm{and} ~ g(\mu_{i_sr}) = \textrm{ln}(\theta_{i_sr} / (1 - \theta_{i_sr})),

Gaussian: YisrN(μisr,σer2) and g(μisr)=μisr,\textrm{Gaussian:} ~ Y_{i_sr} \sim \textrm{N}(\mu_{i_sr}, \sigma_{er}^{2}) ~ \textrm{and} ~ g(\mu_{i_sr}) = \mu_{i_sr},

Poisson: YisrPoisson(μisr) and g(μisr)=ln(μisr).\textrm{Poisson:} ~ Y_{i_sr} \sim \textrm{Poisson}(\mu_{i_sr}) ~ \textrm{and} ~ g(\mu_{i_sr}) = \textrm{ln}(\mu_{i_sr}).

Usage

multiNetLeroux(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, 
trueVarianceCovarianceU = NULL, trueSigmaSquaredE = NULL, 
covarianceBetaPrior = 10^5, a1 = 0.001, b1 = 0.001, xi, omega, 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 nisrn_{i_sr}. 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 β1,,βR\boldsymbol{\beta}_{1}, \ldots, \boldsymbol{\beta}_{R}.

trueSpatialRandomEffects

If available, the true values of ϕ1,,ϕR\boldsymbol{\phi}_{1}, \ldots, \boldsymbol{\phi}_{R}.

trueURandomEffects

If available, the true values of u1,,uJ,u\boldsymbol{u}_{1}, \ldots, \boldsymbol{u}_{J}, \boldsymbol{u}^{*}.

trueSpatialTauSquared

If available, the true values of τ12,,τR2\tau^{2}_{1}, \ldots, \tau^{2}_{R}.

trueSpatialRho

If available, the true value of ρ1,,ρR\rho_{1}, \ldots, \rho_{R}.

trueVarianceCovarianceU

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

trueSigmaSquaredE

If available, the true value of σe12\sigma_{e1}^{2}, \ldots, σeR2\sigma_{eR}^{2}. Only used if family\texttt{family}=“gaussian".

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}.

xi

The degrees of freedom parameter for the Inverse-Wishart distribution relating to the network random effects ξu\xi_{\boldsymbol{u}}.

omega

The scale parameter for the Inverse-Wishart distribution relating to the network random effects Ωu\boldsymbol{\Omega}_{\boldsymbol{u}}.

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 β1,,βR\boldsymbol{\beta}_{1}, \ldots, \boldsymbol{\beta}_{R} parameters in the model.

spatialTauSquaredSamples

Type: matrix. The matrix of simulated samples from the posterior distribution of τ12,,τR2\tau^{2}_{1}, \ldots, \tau^{2}_{R} in the model.

spatialRhoSamples

The vector of simulated samples from the posterior distribution of ρ1,,ρR\rho_{1}, \ldots, \rho_{R} in the model.

varianceCovarianceUSamples

The matrix of simulated samples from the posterior distribution of Σu\boldsymbol{\Sigma}_{\boldsymbol{u}} in the model.

spatialRandomEffectsSamples

The matrix of simulated samples from the posterior distribution of spatial random effects ϕ1,,ϕR\boldsymbol{\phi}_{1}, \ldots, \boldsymbol{\phi}_{R} in the model.

uRandomEffectsSamples

The matrix of simulated samples from the posterior distribution of network random effects u1,,uJ,u\boldsymbol{u}_{1}, \ldots, \boldsymbol{u}_{J}, \boldsymbol{u}^{*} in the model.

sigmaSquaredESamples

The vector of simulated samples from the posterior distribution of σe12\sigma_{e1}^{2}, \ldots, σeR2\sigma_{eR}^{2} in the model. Only used if family\texttt{family}=“gaussian".

acceptanceRates

The acceptance rates of parameters in the model from the MCMC sampling scheme .

spatialRandomEffectsAcceptanceRate

The acceptance rates of spatial 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


A function that generates samples for a multivariate fixed effects, grouping, and network model.

Description

This function that generates samples for a multivariate fixed effects, grouping, and network model, which is given by

Yisrμisrf(yisrμisr,σer2)   i=1,,Ns, s=1,,S, r=1,,R,Y_{i_sr}|\mu_{i_sr} \sim f(y_{i_sr}| \mu_{i_sr}, \sigma_{er}^{2}) ~~~ i=1,\ldots, N_{s},~s=1,\ldots,S ,~r=1,\ldots,R,

g(μisr)=xisβrvsr+jnet(is)wisjujr+wisur,g(\mu_{i_sr}) = \boldsymbol{x}^\top_{i_s} \boldsymbol{\beta}_{r} v_{sr} + \sum_{j\in \textrm{net}(i_s)}w_{i_sj}u_{jr}+ w^{*}_{i_s}u^{*}_{r},

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

vs=(vs1,,vsR)N(0,Σv)vs=(vs1,,vsR)N(0,Σv),\boldsymbol{v}_{s} = (v_{s1},\ldots, v_{sR}) \sim \textrm{N}(\boldsymbol{0}, \boldsymbol{\Sigma}_{\boldsymbol{v}})\boldsymbol{v}_{s} = (v_{s1},\ldots, v_{sR}) \sim \textrm{N}(\boldsymbol{0}, \boldsymbol{\Sigma}_{\boldsymbol{v}}),

uj=(u1j,,uRj)N(0,Σu),\boldsymbol{u}_{j} = (u_{1j},\ldots, u_{Rj}) \sim \textrm{N}(\boldsymbol{0}, \boldsymbol{\Sigma}_{\boldsymbol{u}}),

u=(u1,,uR)N(0,Σu),\boldsymbol{u}^{*} = (u_{1}^*,\ldots, u_{R}^*) \sim \textrm{N}(\boldsymbol{0}, \boldsymbol{\Sigma}_{\boldsymbol{u}}),

ΣvInverse-Wishart(ξv,Ωv),\boldsymbol{\Sigma}_{\boldsymbol{v}} \sim \textrm{Inverse-Wishart}(\xi_{\boldsymbol{v}}, \boldsymbol{\Omega}_{\boldsymbol{v}}),

ΣuInverse-Wishart(ξu,Ωu),\boldsymbol{\Sigma}_{\boldsymbol{u}} \sim \textrm{Inverse-Wishart}(\xi_{\boldsymbol{u}}, \boldsymbol{\Omega}_{\boldsymbol{u}}),

σer2Inverse-Gamma(α3,ξ3).\sigma_{er}^{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 relating to the rrth response are denoted by βr\boldsymbol{\beta}_{r}, 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 σer2\sigma_{er}^{2}, and the corresponding hyperparamaterers (α3\alpha_{3}, ξ3\xi_{3}) can be chosen by the user.

The R×1R \times 1 vector of random effects for the $s$th group is denoted by vs=(vs1,,vsR)R×1\boldsymbol{v}_{s} = (v_{s1}, \ldots, v_{sR})_{R \times 1}, which is assigned a joint Gaussian prior distribution with an unstructured covariance matrix Σv\boldsymbol{\Sigma}_{\boldsymbol{v}} that captures the covariance between the RR outcomes. A conjugate Inverse-Wishart prior is specified for the random effects covariance matrix Σv\boldsymbol{\Sigma}_{\boldsymbol{v}}. The corresponding hyperparamaterers (ξv\xi_{\boldsymbol{v}}, Ωv\boldsymbol{\Omega}_{\boldsymbol{v}}) can be chosen by the user.

The R×1R \times 1 vector of random effects for the jjth alter is denoted by uj=(uj1,,ujR)R×1\boldsymbol{u}_{j} = (u_{j1}, \ldots, u_{jR})_{R \times 1}, while the R×1R \times 1 vector of isolation effects for all RR outcomes is denoted by u=(u1,,uR)\boldsymbol{u}^{*} = (u_{1}^*,\ldots, u_{R}^*), and both are assigned multivariate Gaussian prior distributions. The unstructured covariance matrix Σu\boldsymbol{\Sigma}_{\boldsymbol{u}} captures the covariance between the RR outcomes at the network level, and a conjugate Inverse-Wishart prior is specified for this covariance matrix Σu\boldsymbol{\Sigma}_{\boldsymbol{u}}. The corresponding hyperparamaterers (ξu\xi_{\boldsymbol{u}}, Ωu\boldsymbol{\Omega}_{\boldsymbol{u}}) can be chosen by the user.

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

Binomial: YisrBinomial(nisr,θisr) and g(μisr)=ln(θisr/(1θisr)),\textrm{Binomial:} ~ Y_{i_sr} \sim \textrm{Binomial}(n_{i_sr}, \theta_{i_sr}) ~ \textrm{and} ~ g(\mu_{i_sr}) = \textrm{ln}(\theta_{i_sr} / (1 - \theta_{i_sr})),

Gaussian: YisrN(μisr,σer2) and g(μisr)=μisr,\textrm{Gaussian:} ~ Y_{i_sr} \sim \textrm{N}(\mu_{i_sr}, \sigma_{er}^{2}) ~ \textrm{and} ~ g(\mu_{i_sr}) = \mu_{i_sr},

Poisson: YisrPoisson(μisr) and g(μisr)=ln(μisr).\textrm{Poisson:} ~ Y_{i_sr} \sim \textrm{Poisson}(\mu_{i_sr}) ~ \textrm{and} ~ g(\mu_{i_sr}) = \textrm{ln}(\mu_{i_sr}).

Usage

multiNetRand(formula, data, trials, family, V, W, numberOfSamples = 10, burnin = 0, 
thin = 1, seed = 1, trueBeta = NULL, trueVRandomEffects = NULL, 
trueURandomEffects = NULL, trueVarianceCovarianceV = NULL, 
trueVarianceCovarianceU = NULL, trueSigmaSquaredE = NULL, 
covarianceBetaPrior = 10^5, xiV, omegaV, xi, omega, a3 = 0.001, 
b3 = 0.001, centerVRandomEffects = 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 nisrn_{i_sr}. Only used if family\texttt{family}=“binomial".

family

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

V

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

W

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

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 β1,,βR\boldsymbol{\beta}_{1}, \ldots, \boldsymbol{\beta}_{R}.

trueVRandomEffects

If available, the true values of v1,,vS\boldsymbol{v}_{1}, \ldots, \boldsymbol{v}_{S}.

trueURandomEffects

If available, the true values of u1,,uJ,u\boldsymbol{u}_{1}, \ldots, \boldsymbol{u}_{J}, \boldsymbol{u}^{*}.

trueVarianceCovarianceV

If available, the true value of Σv\boldsymbol{\Sigma}_{\boldsymbol{v}}.

trueVarianceCovarianceU

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

trueSigmaSquaredE

If available, the true value of σe12\sigma_{e1}^{2}, \ldots, σeR2\sigma_{eR}^{2}. Only used if family\texttt{family}=“gaussian".

covarianceBetaPrior

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

xiV

The degrees of freedom parameter for the Inverse-Wishart distribution relating to the grouping random effects ξv\xi_{\boldsymbol{v}}.

omegaV

The scale parameter for the Inverse-Wishart distribution relating to the grouping random effects Ωv\boldsymbol{\Omega}_{\boldsymbol{v}}.

xi

The degrees of freedom parameter for the Inverse-Wishart distribution relating to the network random effects ξu\xi_{\boldsymbol{u}}.

omega

The scale parameter for the Inverse-Wishart distribution relating to the network random effects Ωu\boldsymbol{\Omega}_{\boldsymbol{u}}.

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".

centerVRandomEffects

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.

V

The grouping 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 β1,,βR\boldsymbol{\beta}_{1}, \ldots, \boldsymbol{\beta}_{R} parameters in the model.

varianceCovarianceVSamples

The matrix of simulated samples from the posterior distribution of Σv\boldsymbol{\Sigma}_{\boldsymbol{v}} in the model.

varianceCovarianceUSamples

The matrix of simulated samples from the posterior distribution of Σu\boldsymbol{\Sigma}_{\boldsymbol{u}} in the model.

vRandomEffectsSamples

The matrix of simulated samples from the posterior distribution of spatial random effects v1,,vS\boldsymbol{v}_{1}, \ldots, \boldsymbol{v}_{S} in the model.

uRandomEffectsSamples

The matrix of simulated samples from the posterior distribution of network random effects u1,,uJ,u\boldsymbol{u}_{1}, \ldots, \boldsymbol{u}_{J}, \boldsymbol{u}^{*} in the model.

sigmaSquaredESamples

The vector of simulated samples from the posterior distribution of σe12\sigma_{e1}^{2}, \ldots, σeR2\sigma_{eR}^{2} in the model. Only used if family\texttt{family}=“gaussian".

acceptanceRates

The acceptance rates of parameters in the model from the MCMC sampling scheme.

vRandomEffectsAcceptanceRate

The acceptance rates of 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


A function that plots visual MCMC diagnostics of the fitted model.

Description

This function takes a netcmc object of samples from the posterior distribution of a parameter(s) and returns a visual convergence diaagnostics in the form of a density plot, trace plot, and ACF plot.

Usage

## S3 method for class 'netcmc'
plot(x, ...)

Arguments

x

A netcmc object of samples from the posterior distribution of a parameter(s).

...

Ignored.s

Value

Returns a trace plot, density plot and ACF plot for the posterior distribution of a parameter(s) in a netcmc object.

Author(s)

George Gerogiannis


A function that gets a summary of the fitted model.

Description

This function takes a netcmc object and returns a summary of the fitted model. The summary includes, for selected parameters, posterior medians and 95 percent credible intervals, the effective number of independent samples and the Geweke convergence diagnostic in the form of a Z-score.

Usage

## S3 method for class 'netcmc'
print(x, ...)

Arguments

x

A netcmc fitted model object.

...

Ignored.s

Value

Returns a model summary for a netcmc object.

Author(s)

George Gerogiannis


A function that gets a summary of the fitted model.

Description

This function takes a netcmc object and returns a summary of the fitted model. The summary includes, for selected parameters, posterior medians and 95 percent credible intervals, the effective number of independent samples and the Geweke convergence diagnostic in the form of a Z-score.

Usage

## S3 method for class 'netcmc'
summary(object, ...)

Arguments

object

A netcmc fitted model object.

...

Ignored.s

Value

Returns a model summary for a netcmc object.

Author(s)

George Gerogiannis


A function that generates samples for a univariate fixed effects model.

Description

This function generates samples for a univariate fixed effects 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β,g(\mu_{i_s}) = \boldsymbol{x}^\top_{i_s} \boldsymbol{\beta},

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

σ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.

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

uni(formula, data, trials, family, numberOfSamples = 10, burnin = 0, thin = 1, seed = 1,
trueBeta = NULL, trueSigmaSquaredE = NULL, covarianceBetaPrior = 10^5, 
a3 = 0.001, b3 = 0.001)

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".

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 values of the β\boldsymbol{\beta}.

trueSigmaSquaredE

If available, the true value of σe2\sigma_{e}^{2}. Only used if family\texttt{family}=“gaussian".

covarianceBetaPrior

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

a3

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

b3

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

Value

call

The matched call.

y

The response used.

X

The design matrix used.

standardizedX

The standardized design 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.

sigmaSquaredESamples

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

acceptanceRates

The acceptance rates of parameters 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
  #################################################
  
  #### Generate the covariates and response data
  observations <- 100
  X <- matrix(rnorm(2 * observations), ncol = 2)
  colnames(X) <- c("x1", "x2")
  beta <- c(2, -2, 2)
  logit <- cbind(rep(1, observations), X) %*% beta
  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 <- uni(formula = formula, data = data, family="binomial", 
                        trials = trials, numberOfSamples = 10000, 
                        burnin = 10000, thin = 10, seed = 1)
## End(Not run)

A function that generates samples for a univariate network model.

Description

This function generates samples for a univariate network 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β+jnet(is)wisjuj+wisu,g(\mu_{i_s}) = \boldsymbol{x}^\top_{i_s} \boldsymbol{\beta} + \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}),

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

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

σ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.

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

uniNet(formula, data, trials, family, W, numberOfSamples = 10, burnin = 0, thin = 1, 
seed = 1, trueBeta = NULL, trueURandomEffects = NULL, trueSigmaSquaredU = NULL,
trueSigmaSquaredE = NULL, covarianceBetaPrior = 10^5, a2 = 0.001, b2 = 0.001, 
a3 = 0.001, b3 = 0.001, 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".

W

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

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}.

trueURandomEffects

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

trueSigmaSquaredU

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

trueSigmaSquaredE

If available, the true value σ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}.

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".

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.

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.

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.

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 .

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) }))
  
  #### Generate the covariates and response data
  X <- matrix(rnorm(2 * observations), ncol = 2)
  colnames(X) <- c("x1", "x2")
  beta <- c(1, -0.5, 0.5)
  sigmaSquaredU <- 1
  uRandomEffects <- rnorm(numberOfMultipleClassifications, mean = 0, 
                          sd = sqrt(sigmaSquaredU))

  logTheta <- cbind(rep(1, observations), X) %*% beta + W %*% uRandomEffects
  Y <- rpois(n = observations, lambda = exp(logTheta))
  data <- data.frame(cbind(Y, X))
  
  #### Run the model
  formula <- Y ~ x1 + x2
  ## Not run: model <- uniNet(formula = formula, data = data, family="poisson",
                           W = W, numberOfSamples = 10000, burnin = 10000, 
                           thin = 10, seed = 1)
## End(Not run)

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)

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

Description

This function generates samples for a univariate network group 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β+vs+jnet(is)wisjuj+wisu,g(\mu_{i_s}) = \boldsymbol{x}^\top_{i_s} \boldsymbol{\beta} + v_{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}),

vsN(0,τ2),v_{s} \sim \textrm{N}(0, \tau^{2}),

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}),

σ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.

The S×1S \times 1 vector of random effects for the groups are collectively denoted by v=(v1,,vS)S×1\boldsymbol{v} = (v_{1}, \ldots, v_{S})_{S \times 1}, and each element is assigned an independent zero-mean Gaussian prior distribution with a constant variance τ2\tau^{2}. A conjugate Inverse-Gamma prior is specified for τ2\tau^{2}. The 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

uniNetRand(formula, data, trials, family, groupAssignment, W, numberOfSamples = 10, 
burnin = 0, thin = 1, seed = 1, trueBeta = NULL, 
trueGroupRandomEffects = NULL, trueURandomEffects = NULL, 
trueTauSquared = 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, 
centerGroupRandomEffects = 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".

W

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

groupAssignment

The binary matrix of individual's assignment to groups 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}.

trueGroupRandomEffects

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

trueURandomEffects

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

trueTauSquared

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

trueSigmaSquaredU

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

trueSigmaSquaredE

If available, the true value σ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 group random effects α1\alpha_{1}.

b1

The shape parameter for the Inverse-Gamma distribution relating to the group 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".

centerGroupRandomEffects

A choice to center the group 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.

groupAssignment

The group 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.

tauSquaredSamples

The vector of simulated samples from the posterior distribution of τ2\tau^{2} 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.

groupRandomEffectsSamples

The matrix of simulated samples from the posterior distribution of spatial/grouping random effects v\boldsymbol{v} 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 .

groupRandomEffectsAcceptanceRate

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 single level classification
  numberOfSingleClassifications <- 20
  factor = sample(1:numberOfSingleClassifications, observations, TRUE)
  V = matrix(NA, ncol = numberOfSingleClassifications, nrow = observations)
  for(i in 1:length(factor)){
    for(j in 1:numberOfSingleClassifications){
      if(factor[i] == j){
        V[i, j] = 1
      } else {
        V[i, j] = 0
      }
    }
  }
  
  #### Generate the covariates and response data
  X <- matrix(rnorm(2 * observations), ncol = 2)
  colnames(X) <- c("x1", "x2")
  beta <- c(1, -0.5, 0.5)
  tauSquared <- 0.5
  vRandomEffects <- rnorm(numberOfSingleClassifications, mean = 0, 
                          sd = sqrt(tauSquared))
  sigmaSquaredU <- 1
  uRandomEffects <- rnorm(numberOfMultipleClassifications, mean = 0, 
                          sd = sqrt(sigmaSquaredU))

  logTheta <- cbind(rep(1, observations), X) %*% beta + V %*% vRandomEffects 
            + W %*% uRandomEffects
  Y <- rpois(n = observations, lambda = exp(logTheta))
  data <- data.frame(cbind(Y, X))
  
  #### Run the model
  formula <- Y ~ x1 + x2
  ## Not run: model <- uniNetRand(formula = formula, data = data, family="poisson", 
                               W = W, groupAssignment = V, 
                               numberOfSamples = 10000, burnin = 10000,
                               thin = 10, seed = 1)
## End(Not run)