fit_p2: Fitting function for p2 models

Description Usage Arguments Details Value Author(s) References Examples

View source: R/p2.R

Description

Estimates a p2 model using the Laplace approximation.

Usage

1
2
3
fit_p2(y, XnS, XnR, XvD, XvC, M = 0, seed = NULL, trace = FALSE,
  init = NULL, penalized = FALSE, penSigma = NULL, opt = nlminb,
  singular.ok = TRUE, ...)

Arguments

y

An adjacency matrix of size g x g.

XnS

Matrix of sender effects of size g x ks.

XnR

Matrix of receiver effects of size g x kr.

XvD

Density effects, a 3-dim array of size g x g x kd.

XvC

Reciprocity effects, a 3-dim array of size g x g x kc.

M

Number of replication for TMB-based importance sampling. Default is 0, giving the Laplace approximation (strongly recommended).

seed

Random seed for importance sampling. Default is NULL.

trace

TRUE for tracing information during the estimation. Default is FALSE.

init

Optional starting value for model parameters. Default is NULL.

penalized

Set TRUE for penalized estimation of variance parameters. Default is FALSE.

penSigma

Optional variance matrix of random effects to be used as user-defined penalty in penalized estimation. Default is NULL.

opt

Name of the optimising function. Default is nlminb.

singular.ok

Should singular variance matrix of random effects be allowed? Default is TRUE.

...

Optional arguments passed to the optimiser.

Details

The function allows for penalized estimation, which may be recommendable to prevent numerical issues, such as estimated variance matrix of random effects close to singularity. The fit is carried out by means of the TMB package, and by selecting M>0 it would be possible to employ the importance sampler made available by that package. However, the function fitIS provides a safer alternative for estimation based on importance sampling.

Value

The returned value is an object of class "p2", a list containing the following components:

theta

the vector of model estimates.

loglik

the log likelihood function at the estimate.

AIC, BIC

model selection criteria at the estimate.

XnS.null, XnR.null

logical, flagging whether there are no sender or receiver effects, respectively.

seed

the random seed used for estimation (when M>0).

theta.cov

Variance matrix of estimates.

theta.se

Standard errors of estimates.

opt

Optimiser employed for estimation.

opt.details

Object returned by the optimiser.

ADobj

Object returned by TMB:MakeADFun, containing the log likelihood function and its gradient (among its components).

model.data

List containing all the model data, fed to TMB:MakeADFun.

sdrep

Summary object returned by TMB::sdreport. This is an object with many slots, useful for TMB users.

ranef, ranef.se

Estimated random effects and their standard errors.

Sigma, Sigma.se

Estimated variance matrix of random effects and related standard errors.

rho, rho.se

Estimated correlation of random effects and its standard error.

M

Number of importance sampling replications.

penflag, penSigma

Arguments for penalized estimation.

Author(s)

Ruggero Bellio

References

Bellio, R. and Soriani, N. (2019). Maximum likelihood estimation based on the Laplace approximation for p2 network regression models. Submitted manuscript.

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# Analysis of the  kracknets data from the NetData package
library(NetData)
data(kracknets)
# data preparation
g <- 21
Y <- matrix(0, g, g)
ind <-1
for(i in 1:nrow(friendship_data_frame)){
   sele <- friendship_data_frame[i, ]
   Y[sele$ego, sele$alter] <- sele$friendship_tie
   }
Xn <- model.matrix(~ AGE + TENURE, attributes)[, -1]
XvD <- array(1, dim=c(g, g, 4))
for(i in 1:g)
 for(j in 1:g){
    XvD[i, j, 2] <- as.numeric(attributes$DEPT[i]==attributes$DEPT[j])
    XvD[i, j, 3] <- as.numeric(attributes$LEVEL[i]==attributes$LEVEL[j])
    XvD[i, j, 4] <- abs(attributes$AGE[i] - attributes$AGE[j])
  }
XvC <- array(1, dim=c(g, g, 1))
# Now we are ready to fit the model
mod <- fit_p2(Y, Xn, Xn, XvD, XvC)
print(mod)

rugbel/p2model documentation built on March 8, 2021, 8:16 p.m.