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 |
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.
Package: | netcmc |
Type: | Package |
Version: | 1.0 |
Date: | 2022-01-24 |
License: | GPL (>= 2) |
George Gerogiannis <[email protected]>
## See the examples in the function specific help files.
## See the examples in the function specific help files.
This function transforms a network, which is a data.frame type in a specified format, in to a resultant by
adjacency matrix, where
= 0 if vertex
and
(
) are not adjacent i.e. vertex
and
are not the head/tail of an edge
and
= 1 if vertex
and
(
) are adjacent i.e. vertex
and
are the head/tail of an edge
.
= 0 when
.
getAdjacencyMatrix(rawNetwork)
getAdjacencyMatrix(rawNetwork)
rawNetwork |
The data.frame which encodes information about the network. The dimensions of the matrix are |
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. |
George Gerogiannis
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)
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 given individual IDs and the alters that they have nominated.
getMembershipMatrix(individualID, alters)
getMembershipMatrix(individualID, alters)
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. |
membershipMatrix |
The resultant data.frame. |
George Gerogiannis
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)
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)
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.
getTotalAltersByStatus(individualID, status, alters)
getTotalAltersByStatus(individualID, status, alters)
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. |
totalAltersByStatus |
The resultant data.frame. |
George Gerogiannis
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)
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)
This function that generates samples for a multivariate fixed effects and network model, which is given by
The covariates for the th individual in the
th spatial unit or other grouping are included in a
vector
. The corresponding
vector of fixed effect parameters relating to the
th response are denoted by
, which has an assumed multivariate Gaussian prior with mean
and diagonal covariance matrix
that can be chosen by the user. A conjugate Inverse-Gamma prior is specified for
, and the corresponding hyperparamaterers (
,
) can be chosen by the user.
The vector of random effects for the
th alter is denoted by
, while the
vector of isolation effects for all
outcomes is denoted by
, and both are assigned multivariate Gaussian prior distributions. The unstructured covariance matrix
captures the covariance between the
outcomes at the network level, and a conjugate Inverse-Wishart prior is specified for this covariance matrix
. The corresponding hyperparamaterers (
,
) can be chosen by the user.
The exact specification of each of the likelihoods (binomial, Gaussian, and Poisson) are given below:
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)
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)
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
|
family |
The data likelihood model that must be “gaussian", “poisson" or “binomial". |
W |
A matrix |
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 |
seed |
A seed for the MCMC algorithm. |
trueBeta |
If available, the true value of |
trueURandomEffects |
If available, the true values of |
trueVarianceCovarianceU |
If available, the true value of |
trueSigmaSquaredE |
If available, the true value of |
covarianceBetaPrior |
A scalar prior |
xi |
The degrees of freedom parameter for the Inverse-Wishart
distribution relating to the network random effects |
omega |
The scale parameter for the Inverse-Wishart distribution
relating to the network random effects |
a3 |
The shape parameter for the Inverse-Gamma distribution
relating to the error terms |
b3 |
The scale parameter for the Inverse-Gamma distribution
relating to the error terms |
centerURandomEffects |
A choice to center the network random effects after each iteration of the MCMC sampler. |
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 |
varianceCovarianceUSamples |
The matrix of simulated samples from the posterior
distribution of |
uRandomEffectsSamples |
The matrix of simulated samples from the posterior
distribution of network random effects |
sigmaSquaredESamples |
The vector of simulated samples from the posterior
distribution of |
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 |
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. |
George Gerogiannis
This function that generates samples for a multivariate fixed effects, spatial, and network model, which is given by
The covariates for the th individual in the
th spatial unit or other grouping are included in a
vector
. The corresponding
vector of fixed effect parameters relating to the
th response are denoted by
, which has an assumed multivariate Gaussian prior with mean
and diagonal covariance matrix
that can be chosen by the user. A conjugate Inverse-Gamma prior is specified for
, and the corresponding hyperparamaterers (
,
) 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 , which defines how spatially close the
areal units are to each other. The elements of
can be binary or non-binary, and the most common specification is that
if a pair of areal units (
,
) share a common border or are considered neighbours by some other measure, and
otherwise. Note,
for all
.
measures the variance of these random effects for the
th response, where a conjugate Inverse-Gamma prior is specified for
and the corresponding hyperparamaterers (
,
) can be chosen by the user.
controls the level of spatial autocorrelation. A non-conjugate uniform prior is specified for
.
The vector of random effects for the
th alter is denoted by
, while the
vector of isolation effects for all
outcomes is denoted by
, and both are assigned multivariate Gaussian prior distributions. The unstructured covariance matrix
captures the covariance between the
outcomes at the network level, and a conjugate Inverse-Wishart prior is specified for this covariance matrix
. The corresponding hyperparamaterers (
,
) can be chosen by the user.
The exact specification of each of the likelihoods (binomial, Gaussian, and Poisson) are given below:
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)
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)
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
|
family |
The data likelihood model that must be “gaussian", “poisson" or “binomial". |
squareSpatialNeighbourhoodMatrix |
An |
W |
A matrix |
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 |
seed |
A seed for the MCMC algorithm. |
trueBeta |
If available, the true value of |
trueSpatialRandomEffects |
If available, the true values of |
trueURandomEffects |
If available, the true values of |
trueSpatialTauSquared |
If available, the true values of |
trueSpatialRho |
If available, the true value of |
trueVarianceCovarianceU |
If available, the true value of |
trueSigmaSquaredE |
If available, the true value of |
covarianceBetaPrior |
A scalar prior |
a1 |
The shape parameter for the Inverse-Gamma distribution
relating to the spatial random effects |
b1 |
The scale parameter for the Inverse-Gamma distribution
relating to the spatial random effects |
xi |
The degrees of freedom parameter for the Inverse-Wishart
distribution relating to the network random effects |
omega |
The scale parameter for the Inverse-Wishart distribution
relating to the network random effects |
a3 |
The shape parameter for the Inverse-Gamma distribution
relating to the error terms |
b3 |
The scale parameter for the Inverse-Gamma distribution
relating to the error terms |
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. |
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 |
spatialTauSquaredSamples |
Type: matrix. The matrix of simulated samples from the posterior
distribution of |
spatialRhoSamples |
The vector of simulated samples from the posterior
distribution of |
varianceCovarianceUSamples |
The matrix of simulated samples from the posterior
distribution of |
spatialRandomEffectsSamples |
The matrix of simulated samples from the posterior
distribution of spatial random effects |
uRandomEffectsSamples |
The matrix of simulated samples from the posterior
distribution of network random effects |
sigmaSquaredESamples |
The vector of simulated samples from the posterior
distribution of |
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 |
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. |
George Gerogiannis
This function that generates samples for a multivariate fixed effects, grouping, and network model, which is given by
The covariates for the th individual in the
th spatial unit or other grouping are included in a
vector
. The corresponding
vector of fixed effect parameters relating to the
th response are denoted by
, which has an assumed multivariate Gaussian prior with mean
and diagonal covariance matrix
that can be chosen by the user. A conjugate Inverse-Gamma prior is specified for
, and the corresponding hyperparamaterers (
,
) can be chosen by the user.
The vector of random effects for the $s$th group is denoted by
, which is assigned a joint Gaussian prior distribution with an unstructured covariance matrix
that captures the covariance between the
outcomes. A conjugate Inverse-Wishart prior is specified for the random effects covariance matrix
. The corresponding hyperparamaterers (
,
) can be chosen by the user.
The vector of random effects for the
th alter is denoted by
, while the
vector of isolation effects for all
outcomes is denoted by
, and both are assigned multivariate Gaussian prior distributions. The unstructured covariance matrix
captures the covariance between the
outcomes at the network level, and a conjugate Inverse-Wishart prior is specified for this covariance matrix
. The corresponding hyperparamaterers (
,
) can be chosen by the user.
The exact specification of each of the likelihoods (binomial, Gaussian, and Poisson) are given below:
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)
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)
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
|
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 |
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 |
seed |
A seed for the MCMC algorithm. |
trueBeta |
If available, the true value of |
trueVRandomEffects |
If available, the true values of |
trueURandomEffects |
If available, the true values of |
trueVarianceCovarianceV |
If available, the true value of |
trueVarianceCovarianceU |
If available, the true value of |
trueSigmaSquaredE |
If available, the true value of |
covarianceBetaPrior |
A scalar prior |
xiV |
The degrees of freedom parameter for the Inverse-Wishart
distribution relating to the grouping random effects |
omegaV |
The scale parameter for the Inverse-Wishart distribution
relating to the grouping random effects |
xi |
The degrees of freedom parameter for the Inverse-Wishart
distribution relating to the network random effects |
omega |
The scale parameter for the Inverse-Wishart distribution
relating to the network random effects |
a3 |
The shape parameter for the Inverse-Gamma distribution
relating to the error terms |
b3 |
The scale parameter for the Inverse-Gamma distribution
relating to the error terms |
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. |
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 |
varianceCovarianceVSamples |
The matrix of simulated samples from the posterior
distribution of |
varianceCovarianceUSamples |
The matrix of simulated samples from the posterior
distribution of |
vRandomEffectsSamples |
The matrix of simulated samples from the posterior
distribution of spatial random effects |
uRandomEffectsSamples |
The matrix of simulated samples from the posterior
distribution of network random effects |
sigmaSquaredESamples |
The vector of simulated samples from the posterior
distribution of |
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 |
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. |
George Gerogiannis
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.
## S3 method for class 'netcmc' plot(x, ...)
## S3 method for class 'netcmc' plot(x, ...)
x |
A netcmc object of samples from the posterior distribution of a parameter(s). |
... |
Ignored.s |
Returns a trace plot, density plot and ACF plot for the posterior distribution of a parameter(s) in a netcmc object.
George Gerogiannis
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.
## S3 method for class 'netcmc' print(x, ...)
## S3 method for class 'netcmc' print(x, ...)
x |
A netcmc fitted model object. |
... |
Ignored.s |
Returns a model summary for a netcmc object.
George Gerogiannis
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.
## S3 method for class 'netcmc' summary(object, ...)
## S3 method for class 'netcmc' summary(object, ...)
object |
A netcmc fitted model object. |
... |
Ignored.s |
Returns a model summary for a netcmc object.
George Gerogiannis
This function generates samples for a univariate fixed effects model, which is given by
The covariates for the th individual in the
th spatial unit or other grouping are included in a
vector
. The corresponding
vector of fixed effect parameters are denoted by
, which has an assumed multivariate Gaussian prior with mean
and diagonal covariance matrix
that can be chosen by the user. A conjugate Inverse-Gamma prior is specified for
, and the corresponding hyperparamaterers (
,
) can be chosen by the user.
The exact specification of each of the likelihoods (binomial, Gaussian, and Poisson) are given below:
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)
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)
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
|
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 |
seed |
A seed for the MCMC algorithm. |
trueBeta |
If available, the true values of the |
trueSigmaSquaredE |
If available, the true value of |
covarianceBetaPrior |
A scalar prior |
a3 |
The shape parameter for the Inverse-Gamma distribution
|
b3 |
The scale parameter for the Inverse-Gamma distribution
|
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 |
sigmaSquaredESamples |
The vector of simulated samples from the posterior
distribution of |
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 |
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. |
George Gerogiannis
################################################# #### 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)
################################################# #### 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)
This function generates samples for a univariate network model, which is given by
The covariates for the th individual in the
th spatial unit or other grouping are included in a
vector
. The corresponding
vector of fixed effect parameters are denoted by
, which has an assumed multivariate Gaussian prior with mean
and diagonal covariance matrix
that can be chosen by the user. A conjugate Inverse-Gamma prior is specified for
, and the corresponding hyperparamaterers (
,
) can be chosen by the user.
The vector of alter random effects are denoted by
and modelled as independently Gaussian with mean zero and a constant variance, and due to the row standardised nature of
,
represents the average (mean) effect that the peers of individual
in spatial unit or group
have on that individual.
is an isolation effect, which is an effect for individuals who don't nominate any friends. This is achieved by setting
if individual
nominates no peers and
otherwise, and if
then clearly
as
is the empty set. A conjugate Inverse-Gamma prior is specified for the random effects variance
, and the corresponding hyperparamaterers (
,
) can be chosen by the user.
The exact specification of each of the likelihoods (binomial, Gaussian, and Poisson) are given below:
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)
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)
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
|
family |
The data likelihood model that must be “gaussian", “poisson" or “binomial". |
W |
A matrix |
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 |
seed |
A seed for the MCMC algorithm. |
trueBeta |
If available, the true value of |
trueURandomEffects |
If available, the true value of |
trueSigmaSquaredU |
If available, the true value |
trueSigmaSquaredE |
If available, the true value |
covarianceBetaPrior |
A scalar prior |
a2 |
The shape parameter for the Inverse-Gamma distribution
relating to the network random effects |
b2 |
The scale parameter for the Inverse-Gamma distribution
relating to the network random effects |
a3 |
The shape parameter for the Inverse-Gamma distribution
relating to the error terms |
b3 |
The scale parameter for the Inverse-Gamma distribution
relating to the error terms |
centerURandomEffects |
A choice to center the network random effects after each iteration of the MCMC sampler. |
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 |
sigmaSquaredUSamples |
The vector of simulated samples from the posterior
distribution of |
sigmaSquaredESamples |
The vector of simulated samples from the posterior
distribution of |
uRandomEffectsSamples |
The matrix of simulated samples from the posterior
distribution of network random effects |
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 |
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. |
George Gerogiannis
################################################# #### 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)
################################################# #### 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)
This function generates samples for a univariate network Leroux model, which is given by
The covariates for the th individual in the
th spatial unit or other grouping are included in a
vector
. The corresponding
vector of fixed effect parameters are denoted by
, which has an assumed multivariate Gaussian prior with mean
and diagonal covariance matrix
that can be chosen by the user. A conjugate Inverse-Gamma prior is specified for
, and the corresponding hyperparamaterers (
,
) 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 , which defines how spatially close the
areal units are to each other. The elements of
can be binary or non-binary, and the most common specification is that
if a pair of areal units (
,
) share a common border or are considered neighbours by some other measure, and
otherwise. Note,
for all
.
. Here
is a measure of the variance relating to the spatial random effects
, while
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
. In contrast, a conjugate Inverse-Gamma prior is specified for the random effects variance
, and corresponding hyperparamaterers (
,
) can be chosen by the user.
The vector of alter random effects are denoted by
and modelled as independently Gaussian with mean zero and a constant variance, and due to the row standardised nature of
,
represents the average (mean) effect that the peers of individual
in spatial unit or group
have on that individual.
is an isolation effect, which is an effect for individuals who don't nominate any friends. This is achieved by setting
if individual
nominates no peers and
otherwise, and if
then clearly
as
is the empty set. A conjugate Inverse-Gamma prior is specified for the random effects variance
, and the corresponding hyperparamaterers (
,
) can be chosen by the user.
The exact specification of each of the likelihoods (binomial, Gaussian, and Poisson) are given below:
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)
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)
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
|
family |
The data likelihood model that must be “gaussian", “poisson" or “binomial". |
squareSpatialNeighbourhoodMatrix |
An |
W |
A matrix |
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 |
seed |
A seed for the MCMC algorithm. |
trueBeta |
If available, the true value of |
trueSpatialRandomEffects |
If available, the true value of |
trueURandomEffects |
If available, the true value of |
trueSpatialTauSquared |
If available, the true value of |
trueSpatialRho |
If available, the true value of |
trueSigmaSquaredU |
If available, the true value of |
trueSigmaSquaredE |
If available, the true value of |
covarianceBetaPrior |
A scalar prior |
a1 |
The shape parameter for the Inverse-Gamma distribution
relating to the spatial random effects |
b1 |
The scale parameter for the Inverse-Gamma distribution
relating to the spatial random effects |
a2 |
The shape parameter for the Inverse-Gamma distribution
relating to the network random effects |
b2 |
The scale parameter for the Inverse-Gamma distribution
relating to the network random effects |
a3 |
The shape parameter for the Inverse-Gamma distribution
relating to the error terms |
b3 |
The scale parameter for the Inverse-Gamma distribution
relating to the error terms |
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. |
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 |
spatialTauSquaredSamples |
The vector of simulated samples from the posterior
distribution of |
spatialRhoSamples |
The vector of simulated samples from the posterior
distribution of |
sigmaSquaredUSamples |
The vector of simulated samples from the posterior
distribution of |
sigmaSquaredESamples |
The vector of simulated samples from the posterior
distribution of |
spatialRandomEffectsSamples |
The matrix of simulated samples from the posterior
distribution of spatial/grouping random effects |
uRandomEffectsSamples |
The matrix of simulated samples from the posterior
distribution of network random effects |
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 |
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. |
George Gerogiannis
################################################# #### 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)
################################################# #### 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)
This function generates samples for a univariate network group model, which is given by
The covariates for the th individual in the
th spatial unit or other grouping are included in a
vector
. The corresponding
vector of fixed effect parameters are denoted by
, which has an assumed multivariate Gaussian prior with mean
and diagonal covariance matrix
that can be chosen by the user. A conjugate Inverse-Gamma prior is specified for
, and the corresponding hyperparamaterers (
,
) can be chosen by the user.
The vector of random effects for the groups are collectively denoted by
, and each element is assigned an independent zero-mean Gaussian prior distribution with a constant variance
. A conjugate Inverse-Gamma prior is specified for
. The corresponding hyperparamaterers (
,
) can be chosen by the user.
The vector of alter random effects are denoted by
and modelled as independently Gaussian with mean zero and a constant variance, and due to the row standardised nature of
,
represents the average (mean) effect that the peers of individual
in spatial unit or group
have on that individual.
is an isolation effect, which is an effect for individuals who don't nominate any friends. This is achieved by setting
if individual
nominates no peers and
otherwise, and if
then clearly
as
is the empty set. A conjugate Inverse-Gamma prior is specified for the random effects variance
, and the corresponding hyperparamaterers (
,
) can be chosen by the user.
The exact specification of each of the likelihoods (binomial, Gaussian, and Poisson) are given below:
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)
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)
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
|
family |
The data likelihood model that must be “gaussian", “poisson" or “binomial". |
W |
A matrix |
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 |
seed |
A seed for the MCMC algorithm. |
trueBeta |
If available, the true value of |
trueGroupRandomEffects |
If available, the true value of |
trueURandomEffects |
If available, the true value of |
trueTauSquared |
If available, the true value |
trueSigmaSquaredU |
If available, the true value |
trueSigmaSquaredE |
If available, the true value |
covarianceBetaPrior |
A scalar prior |
a1 |
The shape parameter for the Inverse-Gamma distribution
relating to the group random effects |
b1 |
The shape parameter for the Inverse-Gamma distribution
relating to the group random effects |
a2 |
The shape parameter for the Inverse-Gamma distribution
relating to the network random effects |
b2 |
The scale parameter for the Inverse-Gamma distribution
relating to the network random effects |
a3 |
The shape parameter for the Inverse-Gamma distribution
relating to the error terms |
b3 |
The scale parameter for the Inverse-Gamma distribution
relating to the error terms |
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. |
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 |
tauSquaredSamples |
The vector of simulated samples from the posterior
distribution of |
sigmaSquaredUSamples |
The vector of simulated samples from the posterior
distribution of |
sigmaSquaredESamples |
The vector of simulated samples from the posterior
distribution of |
groupRandomEffectsSamples |
The matrix of simulated samples from the posterior
distribution of spatial/grouping random effects |
uRandomEffectsSamples |
The matrix of simulated samples from the posterior
distribution of network random effects |
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 |
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. |
George Gerogiannis
################################################# #### 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)
################################################# #### 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)