Simulate network structure and data (2024)

simNet {modnets}R Documentation

Description

Used for generating moderated and unmoderated adjacency matrices, along withdata based on those model structures.

Usage

simNet( N = 100, p = 5, m = FALSE, m2 = 0.1, b1 = NULL, b2 = NULL, sparsity = 0.5, intercepts = NULL, nIter = 250, msym = FALSE, onlyDat = FALSE, pbar = TRUE, div = 10, gibbs = TRUE, ordinal = FALSE, nLevels = 5, mord = FALSE, time = TRUE, mbinary = FALSE, minOrd = 3, m1 = NULL, m1_range = NULL, m2_range = c(0.1, 0.3), modType = "none", lags = NULL, V = 2, skewErr = FALSE, onlyNets = FALSE, netArgs = NULL, nCores = 1, cluster = "SOCK", getChains = FALSE, const = 1.5, fixedPar = NULL, V2 = 1, ...)

Arguments

N

Numeric value. Total number of subjects.

p

Numeric value. Total number of nodes (excluding moderator).

m

If a value is provided, a moderator is generated and named Min the resultant data. If TRUE, then a normal distribution with amean of 0 will be used to generate the initial value of m, whichwill serve as the population mean for m throughout the simulation.If a numeric value is provided, then this will serve as the populationmean, and all subsequent draws will be taken from a normal distributionwith that mean. If m = "binary", then this will simply set theargument mbinary = TRUE. If m = "ordinal", this will setmord = TRUE. To simulate m from a skewed distribution, thereare two options: if m = "skewed", then the alpha parameter ofthe sn::rmsn will automatically be set to 3.Alternatively, a vector of length two can be supplied, containing theelement "skewed" as well as the desired value of alpha.Lastly, a function can be provided for m if the user wishes tosample m from another distribution. The requirement is that thefunction have only one argument, and only returns a single numeric value.The input of the argument should be the location parameter of the desiredsampling distribution.

m2

Numeric. If m2 >= 1, then this will determine the number ofinteraction effects between the moderator and some node in the network. Ifa value between 0 and 1 is provided, then this determines the probabilityof any given edge being moderated by the moderator.

b1

Can provide an adjacency matrix to use for generating data.

b2

Can provide an interaction matrix for generated moderated data.

sparsity

Numeric value between 0 and 1. Determines the sparsity ofsampled network matrices.

intercepts

A vector of means for sampling node values.

nIter

Number of iterations for generating each instance of a datapointwith the Gibbs sampler.

msym

If TRUE then will force the interaction matrix to besymmetric.

onlyDat

If TRUE then the function only returns the simulateddata.

pbar

If TRUE then a progress bar will be shown as samples aregenerated.

div

A value to use as a sign that the sampler diverged. Can beincreased based on expected range of values. If a datapoint is larger thandiv, then the sampler will stop.

gibbs

If TRUE, then Gibbs sampling will be used. Otherwise,data are generated from the mvtnorm::rmvnormfunction based on the partial correlation matrix that is created.

ordinal

Logical. Determines whether to generate ordinal values or not.

nLevels

Number of levels for the ordinal variables. Only relevant ifordinal is not FALSE.

mord

Logical. Determines whether the moderator variable should besimulated as ordinal.

time

If TRUE then the time it takes to simulate the data isprinted to screen at the end of the sampling.

mbinary

Logical. Determines whether the moderator should be a binaryvariable.

minOrd

The minimum number of unique values allowed for each variable.

m1

Functions similarly to m2, except that this argument refersto the number/probability of main effects of the moderator on any givennode.

m1_range

Numeric vector of length 2. The range of values for moderatormain effect coefficients.

m2_range

Numeric vector of length 2. The range of values for moderatorinteraction effect coefficients.

modType

Determines the type of moderation to employ, such as"none", "full", "partial". If modType = "full", then for anyinteraction terms there will be full moderation, such that all pairwiserelationships for moderated paths will be set to zero. If modType = "partial", then pairwise edges for moderated paths will always be nonzero.If modType = "none", no constraints will be applied (e.g., couldproduce a mix between full and partial moderation).

lags

If TRUE or 1, then arguments are rerouted to themlGVARsim function to simulate temporal data for a singleindividual.

V

Numeric, either 1 or 2. Determines whether to randomize the order ofsimulating node values at each iteration of the Gibbs sampler. If V = 2, then the order is randomized at each iteration. If V = 1, thenthe sampler moves through the nodes from the first to the last in order ateach iteration.

skewErr

The skewness parameter for the alpha argument in thesn::rmsn function. Only relevant when gibbs = FALSE and no moderator is specified.

onlyNets

If TRUE then only the network models are returned,without the data. Could be used to create random models and then simulatedata by another method.

netArgs

Only for use by the internal functionmodnets:::simNet2, which serves as a wrapper for the currentfunction to prevent it from failing.

nCores

Numeric value indicating the number of CPU cores to use for theresampling. If TRUE, then theparallel::detectCores function will beused to maximize the number of cores available.

cluster

Character vector indicating which type of parallelization touse, if nCores > 1. Options include "mclapply" and"SOCK".

getChains

Logical. Determines whether to return the data-generatingchains from the Gibbs sampler.

const

Numeric. The constant to be used by the internalmodnets:::simPcor function.

fixedPar

Numeric. If provided, then this will be set as thecoefficient value for all edges in the network. Provides a way tostandardize the parameter values while varying the sparsity of the network.If length(fixedPar) == 1, then the same value will be used for allparameters. If length(fixedPar) == 2, then the first value will befor pairwise relationships, and the second value will be for interactionterms.

V2

If V2 = 1 and m2 is between 0 and 1, the number ofinteraction terms in the model will be determined by multiplying m2with the number of elements in the interaction matrix and taking theceiling.

...

Additional arguments.

Details

If no moderator is specified then data can be generated directly from apartial correlation matrix by setting gibbs = FALSE, which producesfast simulation results. Alternatively, a Gibbs sampler is used to generatedata, which is the default option. For moderated networks, Gibbs sampling isthe only method available.

Value

Simulated network models as well as data generated from those models.For GGMs, model matrices are always symmetric. For temporal networks (whenlags = 1), columns predict rows.

Warning

Importantly, the Gibbs sampler can easily diverge given certain modelparameters. Generating network data based on moderator variables canproduce data that quickly take on large values due to the presence ofmultiplicative terms. If the simulation fails, first simply try re-runningthe function with a different seed; this will often be sufficient to solvethe problem when default parameters are specified. Additionally, one canincrease the value of div, in case the sampler only divergesslightly or simply produced an anomalous value. This raises the thresholdof tolerated values before the sampler stops. If supplying user-generatedmodel matrices (for the b1 and/or b2 arguments) and thefunction continues to fail, you will likely need to change the parametervalues in those matrices, as it may not be possible to simulate data underthe given values. If simulating the model matrices inside the function (asis the default) and the function continues to fail, try adjusting thefollowing parameters:

  1. Try reducing the value of m2to specify fewer interactions.

  2. Try reducing a range with a smallermaximum for m2_range, to adjust the range of interactioncoefficients.

  3. Try adjusting the corresponding main effect parametersfor the moderator, m1 and m1_range.

  4. Try settingmodType = "full" to reduce the number of main effect parameters.

  5. Try setting a low value(s) for fixedPar, in order to provideparameter values that are known to be lower

An alternative approach could be to use the internal functionsimNet2, which is a wrapper designed to re-run simNet when itfails and automatically adjust simulation parameters such as div tothoroughly test a given parameterization scheme. This function can beaccessed via modnets:::simNet2. There is not documentation for thisfunction, so it is recommended to look at the source code if one wishes touse it This wrapper is also used inside the mnetPowerSim function.

See Also

mlGVARsim, mnetPowerSim, plotNet, net, netInts, plotBoot, plotCoefs

Examples

# Generate a moderated GGM along with dataset.seed(1)x <- simNet(N = 100, p = 3, m = TRUE)net(x) # Get data-generating adjacency matrixnetInts(x) # Get data-generating interaction matrixplot(x) # Plot the moderated network that generated the data# Generate a single-subject GVAR model with dataset.seed(1)x <- simNet(N = 500, p = 3, m = TRUE, lags = 1)net(x, n = 'temporal') # Get the data-generating time-lagged adjacency matrixnet(x, n = 'contemporaneous') # Get the data-generating standardized residual covariance matrixplot(x, which.net = 'beta') # 'beta' is another way of referring to the temporal networkplot(x, which.net = 'pcc') # 'pcc' is another way of referring to the contemporaneous network

[Package modnets version 0.9.0 Index]

Simulate network structure and data (2024)
Top Articles
Latest Posts
Article information

Author: The Hon. Margery Christiansen

Last Updated:

Views: 6242

Rating: 5 / 5 (50 voted)

Reviews: 81% of readers found this page helpful

Author information

Name: The Hon. Margery Christiansen

Birthday: 2000-07-07

Address: 5050 Breitenberg Knoll, New Robert, MI 45409

Phone: +2556892639372

Job: Investor Mining Engineer

Hobby: Sketching, Cosplaying, Glassblowing, Genealogy, Crocheting, Archery, Skateboarding

Introduction: My name is The Hon. Margery Christiansen, I am a bright, adorable, precious, inexpensive, gorgeous, comfortable, happy person who loves writing and wants to share my knowledge and understanding with you.