Title: | Regression of Network Responses |
---|---|
Description: | Regress network responses (both directed and undirected) onto covariates of interest that may be actor-, relation-, or network-valued. In addition, compute principled variance estimates of the coefficients assuming that the errors are jointly exchangeable. Missing data is accommodated. Additionally implements building and inversion of covariance matrices under joint exchangeability, and generates random covariance matrices from this class. For more detail on methods, see Marrs, Fosdick, and McCormick (2017) <arXiv:1701.05530>. |
Authors: | Frank W. Marrs [aut, cre], Bailey K. Fosdick [aut], Tyler H. McCormick [aut] |
Maintainer: | Frank W. Marrs <[email protected]> |
License: | MIT + file LICENSE |
Version: | 1.0.1 |
Built: | 2024-11-26 05:38:03 UTC |
Source: | https://github.com/cran/netregR |
Build an exchangeable matrix of sparseMatrix class
build_exchangeable_matrix(n, phi, directed = TRUE, dyads = NULL)
build_exchangeable_matrix(n, phi, directed = TRUE, dyads = NULL)
n |
Number of actors in the network, scalar numeric. |
phi |
Appropriate-length vector of parameters, must be length 5 or 6 for directed= |
directed |
Optional logical indicator of whether input data is for a directed network, default is |
dyads |
Optional numeric vector of dyads to subset the matrix to. |
This function builds a covariance matrix in the exchangeable class from the vector of parameters input. See Marrs et.al. (2017).
out |
Exchangeable matrix. |
Marrs, F. W., Fosdick, B. K., & McCormick, T. H., (2017). Standard errors for regression on relational data with exchangeable errors. arXiv preprint arXiv:1701.05530.
rphi
, invert_exchangeable_matrix
n <- 5 build_exchangeable_matrix(n, rphi(n, seed=1))
n <- 5 build_exchangeable_matrix(n, rphi(n, seed=1))
Coef S3 generic for class lmnet
## S3 method for class 'lmnet' coef(object, ...)
## S3 method for class 'lmnet' coef(object, ...)
object |
lmnet object |
... |
ignored |
Prepare covariates and optional response in adjacency matrix form. If undirected, the values are drawn from the lower triangle of the adjacency matrices.
inputs_lmnet(Xlist, Y = NULL, directed = TRUE, add_intercept = TRUE, time_intercept = FALSE)
inputs_lmnet(Xlist, Y = NULL, directed = TRUE, add_intercept = TRUE, time_intercept = FALSE)
Xlist |
List of |
Y |
Optional |
directed |
Optional logical indicator of whether input data is for a directed network, default is |
add_intercept |
Optional logical indicator of whether intercept should be added to X, default is |
time_intercept |
Optional logical indicator of whether separate intercept should be added to X for each observation of the relational matrix, default is |
This function takes a list of network covariates (in adjacency matrix form) and prepares them for the regression code lmnet
. Accomodates 3-dimensional relational arrays with tmax
repeated observations of the network (over time or context). Typical network data with a single observation may be input as matrices, i.e. tmax = 1
.
A list of:
Y |
Vector of responses (column-wise vectorization order) of appropriate length. |
X |
Matrix of covariates (column-wise vectorization order) of appropriate size. |
nodes |
2-column matrix (or 3-column for repeated observations) indicating directed relation pairs to which each entry in |
# tmax = 1 set.seed(1) n <- 10 Xlist <- list(matrix(rnorm(n^2),n,n), matrix(sample(c(0,1), n^2, replace=TRUE),n,n)) Xlist$Y <- matrix(rnorm(n^2), n, n) Xlist$Y[1:5] <- NA r <- inputs_lmnet(Xlist) r lmnet(r$Y,r$X,nodes=r$nodes) # tmax = 4 set.seed(1) n <- 10 tmax <- 4 X1 <- array(rnorm(n^2*tmax),c(n,n,tmax)) X2 <- array(sample(c(0,1), n^2*tmax, replace=TRUE), c(n,n,tmax)) Xlist <- list(X1, X2) Xlist$Y <- array(rnorm(n^2)*tmax, c(n, n, tmax)) Xlist$Y[1:5] <- NA r <- inputs_lmnet(Xlist) head(r$nodes)
# tmax = 1 set.seed(1) n <- 10 Xlist <- list(matrix(rnorm(n^2),n,n), matrix(sample(c(0,1), n^2, replace=TRUE),n,n)) Xlist$Y <- matrix(rnorm(n^2), n, n) Xlist$Y[1:5] <- NA r <- inputs_lmnet(Xlist) r lmnet(r$Y,r$X,nodes=r$nodes) # tmax = 4 set.seed(1) n <- 10 tmax <- 4 X1 <- array(rnorm(n^2*tmax),c(n,n,tmax)) X2 <- array(sample(c(0,1), n^2*tmax, replace=TRUE), c(n,n,tmax)) Xlist <- list(X1, X2) Xlist$Y <- array(rnorm(n^2)*tmax, c(n, n, tmax)) Xlist$Y[1:5] <- NA r <- inputs_lmnet(Xlist) head(r$nodes)
A synthetic data set of standardized, directed interactions between 25 students in a seventh grade class.
A data set with four variables. Includes the true parameters.
25 x 25 x 5 numeric array of directed relations
25 x 25 x 5 numeric array of binary indictors
25 x 25 x 5 numeric array of standardized absolute difference in indicated interest in each subject area
Numeric vector of length 7 that contains true coefficients. The first two (value 1) pertain to shared_project
and grade_difference_abs
. The last three are separate intercepts for each observation of the network.
3000 x 3000 numeric matrix: the true covariance matrix of the errors.
2x6 numeric matrix: true parameters of covariance matrix.
We generated a symthetic data set form a true linear model with jointly exchangeable errors. The interactions (the outcomes) between 25 students represent normalized, directed relations between them in 5 different contexts (texts). The observation could be, for instance, the standardized number of characters texted from one student to another over a month pertaining to five subjects: school, friends, family, significant others, and popular culture. The first covariate, xbinary
, indicates whether both students indicated in a survey that they were interested in each topic. The second covariate, xabs
, measures the absolute, standardized difference in number of characters in total texts of each student of each subject area.
data("interactions")
data("interactions")
Invert an exchangeable matrix
invert_exchangeable_matrix(n, phi, directed = TRUE)
invert_exchangeable_matrix(n, phi, directed = TRUE)
n |
Number of actors in the network, scalar numeric. |
phi |
Appropriate-length vector of parameters, must be length 5 or 6 for directed= |
directed |
Optional logical indicator of whether input data is for a directed network, default is |
This function inverts a covariance matrix of the exchangeable class in a manner much faster than the direct inverse, and the computational cost does not scale with n. See Marrs et. al. (2017). This approach will only work for complete networks.
out |
Parameters of inverted matrix of exchangeable class. |
Marrs, F. W., Fosdick, B. K., & McCormick, T. H., (2017). Standard errors for regression on relational data with exchangeable errors. arXiv preprint arXiv:1701.05530.
rphi
, build_exchangeable_matrix
n <- 10 phi <- rphi(n, seed=1) p <- invert_exchangeable_matrix(n, phi) I1 <- build_exchangeable_matrix(n, phi) %*% build_exchangeable_matrix(n, p) range(I1 - diag(n*(n-1))) # it works
n <- 10 phi <- rphi(n, seed=1) p <- invert_exchangeable_matrix(n, phi) I1 <- build_exchangeable_matrix(n, phi) %*% build_exchangeable_matrix(n, p) range(I1 - diag(n*(n-1))) # it works
This function takes and
values and fits the multiple linear regression
and returns standard errors.
lmnet(Y, X, directed = TRUE, tmax = 1, nodes = NULL, reweight = FALSE, type = "exchangeable", tol = 1e-06, maxit = 10000, ndstop = TRUE, verbose = FALSE)
lmnet(Y, X, directed = TRUE, tmax = 1, nodes = NULL, reweight = FALSE, type = "exchangeable", tol = 1e-06, maxit = 10000, ndstop = TRUE, verbose = FALSE)
Y |
Vector of relations to be regress, of length |
X |
Matrix of covariates to be regressed upon, including intercept if intercept is desired, must have |
directed |
Optional logical indicator of whether input data is for a directed network, default is |
tmax |
Optional numeric of third dimension of relational data array, default is |
nodes |
Optional |
reweight |
Optional logical indicator of whether iteratively reweighted least squares should be used to compute estimate of |
type |
Optional character specifying degree of exchangeability of third dimension of array (when present, i.e. in temporal relational arrays). Default is |
tol |
Optional numeric, tolerance of stopping criteria of iteratively reweighted least squares estimate of |
maxit |
Optional numeric, maximum number of iterations for iteratively reweighted least squares estimate of |
ndstop |
Optional logical indicator of whether negative definite weighting matrix in iteratively reweighted least squares should stop the descent. Default is |
verbose |
Optional logical indicator of whether information from iteratively reweighted least squares estimate of |
This function takes and
values and fits the multiple linear regression
by ordinary least squares or iteratively reweighted least squares as indicated by the input. The covariance structure is exchangeable from that of Marrs et. al. (2017). The standard errors and test statistics are based on the same paper.
The three dimensional relational array case, i.e. temporal relational data, requires a specification of the type of exchangeability in this third dimension. We may assume that different time periods are independent. On the other hand, we might assume each repeated observation is exchangeable (for example decomposing trade networks into sectors of trade: goods vs. services). See Figure 6a of Marrs et. al. (2017) for the exchangeable case and the surrounding discussion for the independent case.
fit |
An |
Marrs, F. W., Fosdick, B. K., & McCormick, T. H., (2017). Standard errors for regression on relational data with exchangeable errors. arXiv preprint arXiv:1701.05530.
set.seed(1) n <- 10 d <- n*(n-1) X <- cbind(1, rnorm(d), sample(c(0,1), d, replace=TRUE)) betatrue <- rep(1,3) Y <- X %*% betatrue + rnorm(d) fit <- lmnet(Y,X) fit fit2 <- lmnet(Y,X,reweight=TRUE) fit2
set.seed(1) n <- 10 d <- n*(n-1) X <- cbind(1, rnorm(d), sample(c(0,1), d, replace=TRUE)) betatrue <- rep(1,3) Y <- X %*% betatrue + rnorm(d) fit <- lmnet(Y,X) fit fit2 <- lmnet(Y,X,reweight=TRUE) fit2
model.matrix S3 generic for class lmnet
## S3 method for class 'lmnet' model.matrix(object, ...)
## S3 method for class 'lmnet' model.matrix(object, ...)
object |
lmnet object |
... |
ignored |
Plot S3 generic for class lmnet
## S3 method for class 'lmnet' plot(x, ...)
## S3 method for class 'lmnet' plot(x, ...)
x |
lmnet object |
... |
ignored |
Print S3 generic for class lmnet
## S3 method for class 'lmnet' print(x, ...)
## S3 method for class 'lmnet' print(x, ...)
x |
lmnet object |
... |
ignored |
Print S3 generic for class summary.lmnet
## S3 method for class 'summary.lmnet' print(x, ...)
## S3 method for class 'summary.lmnet' print(x, ...)
x |
summary.lmnet object |
... |
ignored |
Print S3 generic for summary.vnet object
## S3 method for class 'summary.vnet' print(x, ...)
## S3 method for class 'summary.vnet' print(x, ...)
x |
summary.vnet object |
... |
ignored |
Print S3 generic for vnet object
## S3 method for class 'vnet' print(x, ...)
## S3 method for class 'vnet' print(x, ...)
x |
vnet object |
... |
ignored |
Generate positive definite phi set
rphi(n, seed = NULL, phi6 = FALSE, directed = TRUE)
rphi(n, seed = NULL, phi6 = FALSE, directed = TRUE)
n |
Number of actors in the network, scalar numeric. |
seed |
Optional numeric seed to set, default is |
phi6 |
Optional logical indicator of whether sixth parameter |
directed |
Optional logical indicator of whether input data is for a directed network, default is |
This function generates a set of 5 (or 6, as appropriate) parameters that corresponds to positive definite exchangeable covariance matrix for a network of size n
. See Marrs et. al. (2017).
phi |
Vector of parameters. |
Marrs, F. W., Fosdick, B. K., & McCormick, T. H., (2017). Standard errors for regression on relational data with exchangeable errors. arXiv preprint arXiv:1701.05530.
build_exchangeable_matrix
, invert_exchangeable_matrix
rphi(10, seed=1)
rphi(10, seed=1)
Summary S3 generic for class lmnet
## S3 method for class 'lmnet' summary(object, ...)
## S3 method for class 'lmnet' summary(object, ...)
object |
lmnet object |
... |
ignored |
Summary S3 generic for vnet object
## S3 method for class 'vnet' summary(object, ...)
## S3 method for class 'vnet' summary(object, ...)
object |
vnet object |
... |
ignored |
vcov S3 generic for class lmnet
## S3 method for class 'lmnet' vcov(object, ...)
## S3 method for class 'lmnet' vcov(object, ...)
object |
lmnet object |
... |
ignored |
Stand-alone estimation of exchangeable variance matrix based on residuals and design matrix.
vnet(e = NULL, X = NULL, directed = TRUE, nodes = NULL, type = "exchangeable", tmax = 1, fit = NULL)
vnet(e = NULL, X = NULL, directed = TRUE, nodes = NULL, type = "exchangeable", tmax = 1, fit = NULL)
e |
Optional vector of residuals, of length |
X |
Optional matrix of covariates from regression, must have |
directed |
Optional logical indicator of whether input data is for a directed network, default is |
nodes |
Optional |
type |
Optional string indicating whether the ‘meat’ in the sandwich variance estimator is estimated using exchangeable theory (see Marrs et. al. (2017)) or using dyadic clustering (Fafchamps and Gubert (2007)). |
tmax |
Optional numeric of third dimension of relational data array, default is |
fit |
Optional fitted model object. One of either |
This function takes and
values computes the variance-covariance matrix of
that resulted in the residuals
assuming that the errors are exchangeable, as based on Marrs et. al. (2017) when
type = "exchangeable"
. When type = "dyadic clustering"
, the theory from Fafchamps and Gubert (2007) is implemented.
A an object of class vhat
containing summary information:
vhat |
Estimated variance-covariance matrix of cofficient estimates |
phi |
Vector of variance-covariance parameter estimates. |
corrected |
Logical of whether variance-covariance matrix was corrected from negative definite to positive semi-definite. |
type |
See inputs. |
tmax |
See inputs. |
Marrs, F. W., Fosdick, B. K., & McCormick, T. H., (2017). Standard errors for regression on relational data with exchangeable errors. arXiv preprint arXiv:1701.05530.
Fafchamps, M., & Gubert, F. (2007). Risk sharing and network formation. American Economic Review, 97(2), 75-79.
set.seed(1) n <- 10 d <- n*(n-1) X <- cbind(1, rnorm(d), sample(c(0,1), d, replace=TRUE)) e <- rnorm(d) vnet(e=e,X=X)
set.seed(1) n <- 10 d <- n*(n-1) X <- cbind(1, rnorm(d), sample(c(0,1), d, replace=TRUE)) e <- rnorm(d) vnet(e=e,X=X)
A data set measuring dominance and its behavioral measures in a captive wolf pack.
A data set with three variables
16 x 16 numeric matrix of dominance measures
16 x 16 numeric matrix of difference in ages (column less row)
16 x 16 numeric matrix of indicators of same sex
This is data on a captive family of wolves in Arnheim, Germany. The 16 wolves studied here were housed in a large wooded enclosure and observed in 1978. This matrix displays deference acts. The number in a cell represents the number of occasions on which the row wolf was seen to exhibit a "low posture" display directed toward the column wolf. The behavior could involve approach or retreat, but the fact that it was performed in "low posture" suggests that it was deferent. Data obtained March 20, 2018 from http://moreno.ss.uci.edu/data.html#wolf.
http://moreno.ss.uci.edu/data.html#wolf
Jan A. R. A. M. van Hooff and Joep A. B. Wensing, "Dominance and its behavioral measures in a captive wolf pack," Chapter 11 in Harry Frank, ed., Man and Wolf. Dordrecht: Junk, 1987, pp. 219-252.
data("wolf")
data("wolf")