| Title: | Manipulate and Analyze Round Robin Dyadic Data |
|---|---|
| Description: | Provides utilities for processing and analyzing dyadic data collected using a round-robin design, in which each person in a group rates or interacts with every other person on at least one variable. Data manipulation functions prepare datasets for dyadic data analysis by creating the actor and partner dummy variables required by the social relations model (SRM). Analysis functions implement the SRM using multilevel modeling via a custom 'nlme' covariance class ('pdSRM'), following the approach of Snijders and Kenny (1999) <doi:10.1111/j.1475-6811.1999.tb00204.x> and Knight and Humphrey (2019) <doi:10.1037/0000115-019>. The package estimates group, actor, partner, and relationship variance components along with generalized and dyadic reciprocity correlations, and supports both null and fixed-effects models. |
| Authors: | Andrew Knight [aut, cre, cph] (ORCID: <https://orcid.org/0000-0001-6700-1903>) |
| Maintainer: | Andrew Knight <[email protected]> |
| License: | MIT + file LICENSE |
| Version: | 2.0.0 |
| Built: | 2026-11-16 12:47:59 UTC |
| Source: | https://github.com/andrewpknight/roundrobinr |
Internal method called by coef.pdMat to extract the
three underlying SRM parameters: actor standard deviation, partner
standard deviation, and actor-partner correlation.
## S3 method for class 'pdSRM' coef(object, unconstrained = TRUE, ...)## S3 method for class 'pdSRM' coef(object, unconstrained = TRUE, ...)
object |
an object inheriting from |
unconstrained |
logical; if |
... |
additional arguments (currently unused) |
a named numeric vector with elements std. dev-a,
std. dev-p, and corr.
d <- createDummies( group.id = "groupId", act.id = "actId", part.id = "partId", d = sampleDyadData[sampleDyadData$timeId == 1, ], merge.original = TRUE ) o <- nlme::lme( liking ~ 1, random = list(groupId = nlme::pdBlocked(list( nlme::pdIdent(~1), pdSRM(~ -1 + a1 + a2 + a3 + a4 + p1 + p2 + p3 + p4) ))), correlation = nlme::corCompSymm(form = ~1 | groupId / pdSRM_dyad_id), data = d, na.action = stats::na.omit )d <- createDummies( group.id = "groupId", act.id = "actId", part.id = "partId", d = sampleDyadData[sampleDyadData$timeId == 1, ], merge.original = TRUE ) o <- nlme::lme( liking ~ 1, random = list(groupId = nlme::pdBlocked(list( nlme::pdIdent(~1), pdSRM(~ -1 + a1 + a2 + a3 + a4 + p1 + p2 + p3 + p4) ))), correlation = nlme::corCompSymm(form = ~1 | groupId / pdSRM_dyad_id), data = d, na.action = stats::na.omit )
Internal method called by corMatrix to reconstruct
the correlation matrix from the compressed SRM parameters.
## S3 method for class 'pdSRM' corMatrix(object, ...)## S3 method for class 'pdSRM' corMatrix(object, ...)
object |
an object inheriting from |
... |
additional arguments (currently unused) |
the correlation matrix corresponding to the positive-definite
matrix represented by object, with a "stdDev" attribute
giving the standard deviations
d <- createDummies( group.id = "groupId", act.id = "actId", part.id = "partId", d = sampleDyadData[sampleDyadData$timeId == 1, ], merge.original = TRUE ) o <- nlme::lme( liking ~ 1, random = list(groupId = nlme::pdBlocked(list( nlme::pdIdent(~1), pdSRM(~ -1 + a1 + a2 + a3 + a4 + p1 + p2 + p3 + p4) ))), correlation = nlme::corCompSymm(form = ~1 | groupId / pdSRM_dyad_id), data = d, na.action = stats::na.omit )d <- createDummies( group.id = "groupId", act.id = "actId", part.id = "partId", d = sampleDyadData[sampleDyadData$timeId == 1, ], merge.original = TRUE ) o <- nlme::lme( liking ~ 1, random = list(groupId = nlme::pdBlocked(list( nlme::pdIdent(~1), pdSRM(~ -1 + a1 + a2 + a3 + a4 + p1 + p2 + p3 + p4) ))), correlation = nlme::corCompSymm(form = ~1 | groupId / pdSRM_dyad_id), data = d, na.action = stats::na.omit )
cpsrm() is a friendly, tidyverse-style entry point for the
Co-Partner SRM. Give it raw long-format data and the names of the
outcome, actor, and group columns; it builds the actor/partner dummy
matrices with create_cp_dummies and fits the model with
cpsrm_run. Use cpsrm_run directly when you
need to supply your own dummy matrices (e.g. dummies built once and
reused across several model calls).
cpsrm( data, dv, actor_id, group_id, location_id = NULL, weight_partners = TRUE, ... )cpsrm( data, dv, actor_id, group_id, location_id = NULL, weight_partners = TRUE, ... )
data |
a |
dv |
character; column name of the outcome variable |
actor_id |
character; column name of the actor/person identifier |
group_id |
character; column name of the group identifier |
location_id |
character or |
weight_partners |
logical; passed to both
|
... |
additional arguments passed through to
|
an object of class "cpsrm"; see cpsrm_run
for the full return value. The dummy-construction output from
create_cp_dummies is attached as $dummies for
reference (e.g. to inspect $dummies$group_size_table).
cpsrm_run for the full-control interface,
create_cp_dummies for the dummy-matrix construction this
wrapper calls internally
# cpsrm() expects one row per PERSON per GROUP, with every other group # member treated as a simultaneous co-partner (e.g. one row per player # per team, as in three-person golf teams). sampleDyadData is a # round-robin/directed-dyad dataset (multiple rows per actor per group, # one per rated partner) and is NOT the right shape for this function -- # see create_dummies()/srm_run() for that design instead. This example # simulates a small, correctly-shaped dataset: 30 three-person teams # drawn from a pool of 90 players. set.seed(1) team_dat <- do.call(rbind, lapply(1:30, function(g) { data.frame(player = sample(1:90, 3), team = g) })) team_dat$score <- rnorm(nrow(team_dat), mean = 70, sd = 3) fit <- cpsrm( data = team_dat, dv = "score", actor_id = "player", group_id = "team", zero_rho = TRUE, # keep the toy example fast/well-behaved se_method = "none", stage1_maxit = 100, stage2_maxit = 100 ) print(fit)# cpsrm() expects one row per PERSON per GROUP, with every other group # member treated as a simultaneous co-partner (e.g. one row per player # per team, as in three-person golf teams). sampleDyadData is a # round-robin/directed-dyad dataset (multiple rows per actor per group, # one per rated partner) and is NOT the right shape for this function -- # see create_dummies()/srm_run() for that design instead. This example # simulates a small, correctly-shaped dataset: 30 three-person teams # drawn from a pool of 90 players. set.seed(1) team_dat <- do.call(rbind, lapply(1:30, function(g) { data.frame(player = sample(1:90, 3), team = g) })) team_dat$score <- rnorm(nrow(team_dat), mean = 70, sd = 3) fit <- cpsrm( data = team_dat, dv = "score", actor_id = "player", group_id = "team", zero_rho = TRUE, # keep the toy example fast/well-behaved se_method = "none", stage1_maxit = 100, stage2_maxit = 100 ) print(fit)
Fits the Co-Partner Social Relations Model (CP-SRM) using restricted
maximum likelihood (REML). The "block" method uses the Woodbury
matrix identity for fast computation on large datasets; the "loop"
method constructs the full covariance matrix by iterating over groups.
cpsrm_run( dv, actor_id, group_id, data, actor_dummies, partner_dummies, group_sizes = NULL, location_id = NULL, method = c("block", "loop"), zero_rho = FALSE, zero_actor = FALSE, zero_partner = FALSE, zero_group = FALSE, zero_location = FALSE, weight_partners = TRUE, fixed_effects = NULL, start = NULL, maxit = 200000L, stage1_maxit = 1000L, stage2_maxit = 1000L, tol = 1e-10, se_method = c("hessian", "none"), rho_boundary = 0.99, optimizer = c("Nelder-Mead", "L-BFGS-B"), verbose = FALSE, verbose_every = 50L )cpsrm_run( dv, actor_id, group_id, data, actor_dummies, partner_dummies, group_sizes = NULL, location_id = NULL, method = c("block", "loop"), zero_rho = FALSE, zero_actor = FALSE, zero_partner = FALSE, zero_group = FALSE, zero_location = FALSE, weight_partners = TRUE, fixed_effects = NULL, start = NULL, maxit = 200000L, stage1_maxit = 1000L, stage2_maxit = 1000L, tol = 1e-10, se_method = c("hessian", "none"), rho_boundary = 0.99, optimizer = c("Nelder-Mead", "L-BFGS-B"), verbose = FALSE, verbose_every = 50L )
dv |
character; name of the dependent variable column |
actor_id |
character; name of the actor identifier column |
group_id |
character; name of the group identifier column |
data |
a |
actor_dummies |
character vector; actor dummy column names |
partner_dummies |
character vector; partner dummy column names |
group_sizes |
integer vector or |
location_id |
character or |
method |
character; |
zero_rho |
logical; fix actor-partner correlation at zero. Default
|
zero_actor |
logical; fix actor variance at zero. Default |
zero_partner |
logical; fix partner variance at zero. Default
|
zero_group |
logical; fix group variance at zero. Default |
zero_location |
logical; fix location variance at zero. Default
|
weight_partners |
logical; weight partner dummies by
|
fixed_effects |
character vector or |
start |
named numeric vector or |
maxit |
integer; maximum optimizer iterations. Default |
stage1_maxit |
integer; stage 1 Nelder-Mead iterations. Default
|
stage2_maxit |
integer; stage 2 Nelder-Mead iterations. Default
|
tol |
numeric; convergence tolerance. Default |
se_method |
character; |
rho_boundary |
numeric; boundary warning threshold. Default |
optimizer |
character; |
verbose |
logical; print progress. Default |
verbose_every |
integer; print every N evaluations. Default |
an object of class "cpsrm"
Because each row sums multiple independent partner effects (its
group_size - 1), can be reported on two
different, equally valid bases when weight_partners = FALSE:
RAW ( itself): the variance of the
partner-effect parameter, on the same per-person basis as
. Use RAW whenever comparing the magnitude of
partner effects to actor effects (e.g. tests of generalized
reciprocity).
COMBINED (partner's row multiplied by
group_size - 1): partner identity's total contribution to
the variance of a single observed score. Use COMBINED only when
reading a full variance decomposition in which all components must
sum to the outcome's total variance.
Mixing the two conventions in the same comparison is a common source of
error: because COMBINED mechanically multiplies partner's numerator
while RAW does not, a naive COMBINED-vs-RAW comparison can make partner
effects look several times more (or less) important than actor effects
even when the two are, per person, identical in magnitude.
print.cpsrm/summary.cpsrm report both whenever
weight_partners = FALSE and group size is constant across the
data; when group size varies, or weight_partners = TRUE (partner
dummies already rescaled to average rather than sum partner effects),
only RAW is shown.
cpsrm_run reports a Wald z-statistic (estimate / SE) for each
variance component, but variance components are bounded below by zero,
so the usual two-sided reference distribution is not correct for
testing whether a variance component is zero. The standard correction
(Self & Liang, 1987; Snijders & Bosker, 2012; Stram & Lee, 1994) is to
fit the model with and without the component of interest (e.g.
zero_partner = TRUE vs. FALSE), form the likelihood-ratio
statistic from each fit's fit["reml_loglik"]
(), and compute a
ONE-SIDED p-value:
p <- 0.5 * pchisq(LRT, df = 1, lower.tail = FALSE)
Testing TWO variance components jointly at their boundary (e.g. actor and partner both zero) is NOT a simple halving of the df = 2 chi-square, because both parameters are boundary-constrained; it requires the three-part mixture (Self & Liang, 1987):
p <- 0.25 * pchisq(LRT, df = 0, lower.tail = FALSE) +
0.50 * pchisq(LRT, df = 1, lower.tail = FALSE) +
0.25 * pchisq(LRT, df = 2, lower.tail = FALSE)
(the df = 0 term is always 0 for any LRT > 0). Applying the
plain, uncorrected pchisq p-value, or halving the df = 2 joint
test the same way as a single-component test, both misstate
significance and are not valid inferential procedures for this model.
# Prepare dummies using create_cp_dummies(), bind onto data, then call # cpsrm_run(). See create_cp_dummies() documentation for a full example.# Prepare dummies using create_cp_dummies(), bind onto data, then call # cpsrm_run(). See create_cp_dummies() documentation for a full example.
cpsrm_run insteadcpsrmRun has been renamed to cpsrm_run.
Key argument changes: course.id -> location_id;
actor.dummies -> actor_dummies;
partner.dummies -> partner_dummies;
zero.rho -> zero_rho; zero.actor -> zero_actor;
zero.partner -> zero_partner;
zero.group -> zero_group;
zero.course -> zero_location;
weight.partners -> weight_partners;
fixed.effects -> fixed_effects;
stage1.maxit -> stage1_maxit;
stage2.maxit -> stage2_maxit;
se.method -> se_method;
rho.boundary -> rho_boundary;
verbose.every -> verbose_every.
cpsrmRun( dv, actor.id, group.id, actor.dummies, partner.dummies, data, course.id = NULL, zero.rho = FALSE, zero.actor = FALSE, zero.partner = FALSE, zero.group = FALSE, zero.course = FALSE, weight.partners = TRUE, fixed.effects = NULL, stage1.maxit = 1000L, stage2.maxit = 1000L, se.method = c("hessian", "none"), rho.boundary = 0.99, verbose.every = 50L, ... )cpsrmRun( dv, actor.id, group.id, actor.dummies, partner.dummies, data, course.id = NULL, zero.rho = FALSE, zero.actor = FALSE, zero.partner = FALSE, zero.group = FALSE, zero.course = FALSE, weight.partners = TRUE, fixed.effects = NULL, stage1.maxit = 1000L, stage2.maxit = 1000L, se.method = c("hessian", "none"), rho.boundary = 0.99, verbose.every = 50L, ... )
dv |
string; name of the outcome variable |
actor.id |
string; name of the actor identifier variable |
group.id |
string; name of the group identifier variable |
actor.dummies |
character vector of actor dummy column names |
partner.dummies |
character vector of partner dummy column names |
data |
a |
course.id |
string or |
zero.rho |
logical; fix actor-partner correlation at zero |
zero.actor |
logical; fix actor variance at zero |
zero.partner |
logical; fix partner variance at zero |
zero.group |
logical; fix group variance at zero |
zero.course |
logical; fix course/location variance at zero |
weight.partners |
logical; weight partner dummies |
fixed.effects |
character vector or |
stage1.maxit |
integer; stage 1 iterations |
stage2.maxit |
integer; stage 2 iterations |
se.method |
character; |
rho.boundary |
numeric; boundary threshold |
verbose.every |
integer; verbose print frequency |
... |
additional arguments passed to |
see cpsrm_run
Generates the actor and partner dummy matrices required by
cpsrm_run. Unlike create_dummies, which
produces dummies for the standard Social Relations Model, this function
is designed for the Co-Partner SRM where each observation involves one
actor and all other members of the group acting simultaneously as
partners. Variable group sizes (2, 3, 4, or mixed) are fully supported.
create_cp_dummies( data, actor_id, group_id, weight_partners = TRUE, prefix_actor = "A", prefix_partner = "P" )create_cp_dummies( data, actor_id, group_id, weight_partners = TRUE, prefix_actor = "A", prefix_partner = "P" )
data |
a |
actor_id |
character; column name of the actor/person identifier |
group_id |
character; column name of the group identifier |
weight_partners |
logical; if |
prefix_actor |
character; prefix for actor dummy columns. Default
|
prefix_partner |
character; prefix for partner dummy columns. Default
|
a list with elements:
actor_matmatrix of actor dummies (n x n_persons)
partner_matmatrix of partner dummies, optionally weighted (n x n_persons)
actor_namescharacter vector of actor dummy column names
partner_namescharacter vector of partner dummy column names
group_sizesinteger vector of length n giving the group size
for each observation; pass this to cpsrm_run() via the
group_sizes argument
person_levelscharacter vector of person IDs (column order)
n_personsinteger; number of unique persons
n_groupsinteger; number of unique groups
group_size_tabletable of group size frequencies
# create_cp_dummies() expects one row per PERSON per GROUP, with every # other group member treated as a simultaneous co-partner (e.g. one row # per player per team, as in three-person golf teams). sampleDyadData is # a round-robin/directed-dyad dataset (multiple rows per actor per # group, one per rated partner) and is NOT the right shape for this # function -- see create_dummies()/srm_run() for that design instead. # This example simulates a small, correctly-shaped dataset: 30 # three-person teams drawn from a pool of 90 players. set.seed(1) team_dat <- do.call(rbind, lapply(1:30, function(g) { data.frame(player = sample(1:90, 3), team = g) })) dummies <- create_cp_dummies( data = team_dat, actor_id = "player", group_id = "team" ) cat("Persons:", dummies$n_persons, "\n") cat("Groups:", dummies$n_groups, "\n")# create_cp_dummies() expects one row per PERSON per GROUP, with every # other group member treated as a simultaneous co-partner (e.g. one row # per player per team, as in three-person golf teams). sampleDyadData is # a round-robin/directed-dyad dataset (multiple rows per actor per # group, one per rated partner) and is NOT the right shape for this # function -- see create_dummies()/srm_run() for that design instead. # This example simulates a small, correctly-shaped dataset: 30 # three-person teams drawn from a pool of 90 players. set.seed(1) team_dat <- do.call(rbind, lapply(1:30, function(g) { data.frame(player = sample(1:90, 3), team = g) })) dummies <- create_cp_dummies( data = team_dat, actor_id = "player", group_id = "team" ) cat("Persons:", dummies$n_persons, "\n") cat("Groups:", dummies$n_groups, "\n")
Generates the actor and partner dummy variables required to fit the Social Relations Model (SRM) using multilevel modeling, following the approach of Snijders and Kenny (1999). The function produces N actor dummies and N partner dummies, where N is the maximum group size in the dataset.
create_dummies( group_id, act_id, part_id, data, include_self = FALSE, merge_original = FALSE )create_dummies( group_id, act_id, part_id, data, include_self = FALSE, merge_original = FALSE )
group_id |
string; name of the group identifier variable |
act_id |
string; name of the actor identifier variable |
part_id |
string; name of the partner identifier variable |
data |
a |
include_self |
logical; if |
merge_original |
logical; if |
a data.frame containing:
pdSRM_act_idinteger internal actor identifier
pdSRM_part_idinteger internal partner identifier
pdSRM_act_numinteger actor slot number within group
pdSRM_part_numinteger partner slot number within group
pdSRM_dyad_idinteger undirected dyad identifier
a1, a2, ...actor dummy variables
p1, p2, ...partner dummy variables
If merge_original = TRUE, all original variables are appended.
Snijders, T. A. B., & Kenny, D. A. (1999). The social relations model for family data: A multilevel approach. Personal Relationships, 6, 471–486. doi:10.1111/j.1475-6811.1999.tb00204.x
d_out <- create_dummies( group_id = "groupId", act_id = "actId", part_id = "partId", data = sampleDyadData ) head(d_out)d_out <- create_dummies( group_id = "groupId", act_id = "actId", part_id = "partId", data = sampleDyadData ) head(d_out)
create_dummies insteadcreateDummies has been renamed to create_dummies.
The argument names have also changed: group.id -> group_id,
act.id -> act_id, part.id -> part_id,
d -> data, include.self -> include_self,
merge.original -> merge_original.
createDummies( group.id, act.id, part.id, d, include.self = FALSE, merge.original = FALSE )createDummies( group.id, act.id, part.id, d, include.self = FALSE, merge.original = FALSE )
group.id |
string; name of the group identifier variable |
act.id |
string; name of the actor identifier variable |
part.id |
string; name of the partner identifier variable |
d |
a |
include.self |
logical; retain self-ratings? Default |
merge.original |
logical; merge back onto original data? Default
|
see create_dummies
Internal method called by pdConstruct to initialize
a pdSRM object. Enforces the SRM constraint of equal actor
variances, equal partner variances, and a single actor-partner covariance.
## S3 method for class 'pdSRM' pdConstruct( object, value = numeric(0), form = stats::formula(object), nam = nlme::Names(object), data = sys.frame(sys.parent()), ... )## S3 method for class 'pdSRM' pdConstruct( object, value = numeric(0), form = stats::formula(object), nam = nlme::Names(object), data = sys.frame(sys.parent()), ... )
object |
an object inheriting from |
value |
an optional initialization value |
form |
an optional one-sided linear formula |
nam |
an optional vector of character strings |
data |
an optional data frame in which to evaluate the variables |
... |
optional arguments passed to other methods |
a pdSRM object representing the SRM positive-definite matrix
d <- createDummies( group.id = "groupId", act.id = "actId", part.id = "partId", d = sampleDyadData[sampleDyadData$timeId == 1, ], merge.original = TRUE ) o <- nlme::lme( liking ~ 1, random = list(groupId = nlme::pdBlocked(list( nlme::pdIdent(~1), pdSRM(~ -1 + a1 + a2 + a3 + a4 + p1 + p2 + p3 + p4) ))), correlation = nlme::corCompSymm(form = ~1 | groupId / pdSRM_dyad_id), data = d, na.action = stats::na.omit )d <- createDummies( group.id = "groupId", act.id = "actId", part.id = "partId", d = sampleDyadData[sampleDyadData$timeId == 1, ], merge.original = TRUE ) o <- nlme::lme( liking ~ 1, random = list(groupId = nlme::pdBlocked(list( nlme::pdIdent(~1), pdSRM(~ -1 + a1 + a2 + a3 + a4 + p1 + p2 + p3 + p4) ))), correlation = nlme::corCompSymm(form = ~1 | groupId / pdSRM_dyad_id), data = d, na.action = stats::na.omit )
Internal method called by pdMatrix to reconstruct the
full covariance matrix from the three stored SRM parameters (actor SD,
partner SD, actor-partner correlation).
## S3 method for class 'pdSRM' pdMatrix(object, factor = FALSE)## S3 method for class 'pdSRM' pdMatrix(object, factor = FALSE)
object |
an object inheriting from |
factor |
logical; if |
if factor is FALSE, the positive-definite matrix
represented by object; if TRUE, an upper triangular
Cholesky factor with a logDet attribute
d <- createDummies( group.id = "groupId", act.id = "actId", part.id = "partId", d = sampleDyadData[sampleDyadData$timeId == 1, ], merge.original = TRUE ) o <- nlme::lme( liking ~ 1, random = list(groupId = nlme::pdBlocked(list( nlme::pdIdent(~1), pdSRM(~ -1 + a1 + a2 + a3 + a4 + p1 + p2 + p3 + p4) ))), correlation = nlme::corCompSymm(form = ~1 | groupId / pdSRM_dyad_id), data = d, na.action = stats::na.omit )d <- createDummies( group.id = "groupId", act.id = "actId", part.id = "partId", d = sampleDyadData[sampleDyadData$timeId == 1, ], merge.original = TRUE ) o <- nlme::lme( liking ~ 1, random = list(groupId = nlme::pdBlocked(list( nlme::pdIdent(~1), pdSRM(~ -1 + a1 + a2 + a3 + a4 + p1 + p2 + p3 + p4) ))), correlation = nlme::corCompSymm(form = ~1 | groupId / pdSRM_dyad_id), data = d, na.action = stats::na.omit )
Creates the positive-definite matrix class used to specify the
actor-partner covariance structure of the Social Relations Model within
lme. This class enforces the SRM constraint that all
actors share a single variance, all partners share a single variance, and
a single actor-partner covariance (generalized reciprocity) is estimated.
pdSRM( value = numeric(0), form = NULL, nam = NULL, data = sys.frame(sys.parent()) )pdSRM( value = numeric(0), form = NULL, nam = NULL, data = sys.frame(sys.parent()) )
value |
an optional initialization value, inherited from
|
form |
an optional one-sided linear formula specifying the row/column names for the matrix |
nam |
an optional vector of character strings specifying the row/column names for the matrix |
data |
inherited from the surrounding |
a pdMat object representing a positive-definite matrix
conforming to the SRM covariance structure
Knight, A. P., & Humphrey, S. E. (2019). Dyadic data analysis. In S. E. Humphrey & J. M. LeBreton (Eds.), The Handbook for Multilevel Theory, Measurement, and Analysis (pp. 423–447). American Psychological Association. doi:10.1037/0000115-019
Snijders, T. A. B., & Kenny, D. A. (1999). The social relations model for family data: A multilevel approach. Personal Relationships, 6, 471–486. doi:10.1111/j.1475-6811.1999.tb00204.x
d <- createDummies( group.id = "groupId", act.id = "actId", part.id = "partId", d = sampleDyadData[sampleDyadData$timeId == 1, ], merge.original = TRUE ) o <- nlme::lme( liking ~ 1, random = list(groupId = nlme::pdBlocked(list( nlme::pdIdent(~1), pdSRM(~ -1 + a1 + a2 + a3 + a4 + p1 + p2 + p3 + p4) ))), correlation = nlme::corCompSymm(form = ~1 | groupId / pdSRM_dyad_id), data = d, na.action = stats::na.omit )d <- createDummies( group.id = "groupId", act.id = "actId", part.id = "partId", d = sampleDyadData[sampleDyadData$timeId == 1, ], merge.original = TRUE ) o <- nlme::lme( liking ~ 1, random = list(groupId = nlme::pdBlocked(list( nlme::pdIdent(~1), pdSRM(~ -1 + a1 + a2 + a3 + a4 + p1 + p2 + p3 + p4) ))), correlation = nlme::corCompSymm(form = ~1 | groupId / pdSRM_dyad_id), data = d, na.action = stats::na.omit )
Print a cpsrm Object
## S3 method for class 'cpsrm' print(x, digits = 4, ...)## S3 method for class 'cpsrm' print(x, digits = 4, ...)
x |
an object of class |
digits |
integer; number of digits to print. Default |
... |
additional arguments (currently unused) |
x, invisibly
A simulated directed dyadic dataset in long format, suitable for demonstrating the functions in this package. The dataset contains observations from a round-robin design in which each person in a group rated every other person. Ratings were collected at two time points.
sampleDyadDatasampleDyadData
A data frame with 1548 rows and 16 variables:
integer; group identification variable
integer; actor (rater) identification variable
integer; partner (ratee) identification variable
integer; time point identifier (1 or 2)
integer; directed dyad-level measure of contact frequency
integer; directed dyad-level measure of liking
numeric; actor-level measure of extraversion
numeric; actor-level measure of agreeableness
integer; actor-level binary gender indicator (1 = male)
integer; actor-level age in years
numeric; partner-level measure of extraversion
numeric; partner-level measure of agreeableness
integer; partner-level binary gender indicator (1 = male)
integer; partner-level age in years
numeric; group-level measure of cohesion
numeric; group-level measure of efficacy
Simulated data created for package demonstration purposes.
Computes pseudo R-squared values for each SRM variance component by
comparing a null model (intercept only) with a predictor model (with
fixed effects). The pseudo R-squared for each component is
(null - predicted) / null, reflecting the proportion of each
variance component explained by the fixed effects.
srm_pseudo_rsq(null_model, predict_model)srm_pseudo_rsq(null_model, predict_model)
null_model |
an |
predict_model |
an |
a data.frame with three columns and four rows (Group,
Actor, Partner, Dyad):
nullvariance component from the null model
predictvariance component from the predictor model
pseudoR2pseudo R-squared: (null - predict) / null
d <- create_dummies( group_id = "groupId", act_id = "actId", part_id = "partId", data = sampleDyadData[sampleDyadData$timeId == 1, ], merge_original = TRUE ) null_mod <- nlme::lme( liking ~ 1, random = list(groupId = nlme::pdBlocked(list( nlme::pdIdent(~1), pdSRM(~ -1 + a1 + a2 + a3 + a4 + p1 + p2 + p3 + p4) ))), correlation = nlme::corCompSymm(form = ~1 | groupId / pdSRM_dyad_id), data = d, na.action = stats::na.omit ) pred_mod <- nlme::lme( liking ~ actEx + partEx + contact, random = list(groupId = nlme::pdBlocked(list( nlme::pdIdent(~1), pdSRM(~ -1 + a1 + a2 + a3 + a4 + p1 + p2 + p3 + p4) ))), correlation = nlme::corCompSymm(form = ~1 | groupId / pdSRM_dyad_id), data = d, na.action = stats::na.omit ) srm_pseudo_rsq(null_model = null_mod, predict_model = pred_mod)d <- create_dummies( group_id = "groupId", act_id = "actId", part_id = "partId", data = sampleDyadData[sampleDyadData$timeId == 1, ], merge_original = TRUE ) null_mod <- nlme::lme( liking ~ 1, random = list(groupId = nlme::pdBlocked(list( nlme::pdIdent(~1), pdSRM(~ -1 + a1 + a2 + a3 + a4 + p1 + p2 + p3 + p4) ))), correlation = nlme::corCompSymm(form = ~1 | groupId / pdSRM_dyad_id), data = d, na.action = stats::na.omit ) pred_mod <- nlme::lme( liking ~ actEx + partEx + contact, random = list(groupId = nlme::pdBlocked(list( nlme::pdIdent(~1), pdSRM(~ -1 + a1 + a2 + a3 + a4 + p1 + p2 + p3 + p4) ))), correlation = nlme::corCompSymm(form = ~1 | groupId / pdSRM_dyad_id), data = d, na.action = stats::na.omit ) srm_pseudo_rsq(null_model = null_mod, predict_model = pred_mod)
A wrapper function that fits the Social Relations Model (SRM) on a
directed dyadic dataset using restricted maximum likelihood via
lme. The function creates the necessary actor and
partner dummy variables, constructs the SRM covariance structure using
pdSRM, and returns both the raw lme output and a
formatted variance decomposition table.
srm_run(dv, group_id, act_id, part_id, fe_vars = NULL, data)srm_run(dv, group_id, act_id, part_id, fe_vars = NULL, data)
dv |
string; name of the directed dyadic criterion (outcome) variable |
group_id |
string; name of the group identifier variable |
act_id |
string; name of the actor identifier variable |
part_id |
string; name of the partner identifier variable |
fe_vars |
character vector of fixed-effect predictor variable names,
or |
data |
a |
a named list with two elements:
lme.outputthe full lme model object
srm.outputa data.frame from srm_var_pct
giving variances, percentages, and reciprocity correlations
Knight, A. P., & Humphrey, S. E. (2019). Dyadic data analysis. In S. E. Humphrey & J. M. LeBreton (Eds.), The Handbook for Multilevel Theory, Measurement, and Analysis (pp. 423–447). American Psychological Association. doi:10.1037/0000115-019
Snijders, T. A. B., & Kenny, D. A. (1999). The social relations model for family data: A multilevel approach. Personal Relationships, 6, 471–486. doi:10.1111/j.1475-6811.1999.tb00204.x
o <- srm_run( dv = "liking", group_id = "groupId", act_id = "actId", part_id = "partId", fe_vars = c("actEx", "partEx", "contact"), data = sampleDyadData[sampleDyadData$timeId == 1, ] ) o$srm.outputo <- srm_run( dv = "liking", group_id = "groupId", act_id = "actId", part_id = "partId", fe_vars = c("actEx", "partEx", "contact"), data = sampleDyadData[sampleDyadData$timeId == 1, ] ) o$srm.output
Extracts and formats the variance components and reciprocity correlations
from an lme object fitted with the
pdSRM covariance structure. Returns group, actor, partner,
and dyadic (relationship) variances as both raw values and percentages of
total variance, along with generalized reciprocity (actor-partner
correlation) and dyadic reciprocity.
srm_var_pct(object)srm_var_pct(object)
object |
an |
a data.frame with two columns and six rows:
variances.and.covariancesGroup, Actor, Partner, and Dyad variances; Generalized Reciprocity covariance; Dyadic Reciprocity covariance
percents.and.correlationsvariance percentages for the four components; Generalized Reciprocity correlation; Dyadic Reciprocity correlation
Kenny, D. A., Kashy, D. A., & Cook, W. L. (2006). Dyadic Data Analysis. Guilford Press.
d <- create_dummies( group_id = "groupId", act_id = "actId", part_id = "partId", data = sampleDyadData[sampleDyadData$timeId == 1, ], merge_original = TRUE ) o <- nlme::lme( liking ~ 1, random = list(groupId = nlme::pdBlocked(list( nlme::pdIdent(~1), pdSRM(~ -1 + a1 + a2 + a3 + a4 + p1 + p2 + p3 + p4) ))), correlation = nlme::corCompSymm(form = ~1 | groupId / pdSRM_dyad_id), data = d, na.action = stats::na.omit ) srm_var_pct(o)d <- create_dummies( group_id = "groupId", act_id = "actId", part_id = "partId", data = sampleDyadData[sampleDyadData$timeId == 1, ], merge_original = TRUE ) o <- nlme::lme( liking ~ 1, random = list(groupId = nlme::pdBlocked(list( nlme::pdIdent(~1), pdSRM(~ -1 + a1 + a2 + a3 + a4 + p1 + p2 + p3 + p4) ))), correlation = nlme::corCompSymm(form = ~1 | groupId / pdSRM_dyad_id), data = d, na.action = stats::na.omit ) srm_var_pct(o)
srm_pseudo_rsq insteadsrmPseudoRSq has been renamed to srm_pseudo_rsq.
The argument names have also changed: null.model ->
null_model, predict.model -> predict_model.
srmPseudoRSq(null.model, predict.model)srmPseudoRSq(null.model, predict.model)
null.model |
an |
predict.model |
an |
see srm_pseudo_rsq
srm_run insteadsrmRun has been renamed to srm_run.
The argument names have also changed: groupId -> group_id,
actId -> act_id, partId -> part_id,
feVars -> fe_vars.
srmRun(dv, groupId, actId, partId, feVars = NULL, data)srmRun(dv, groupId, actId, partId, feVars = NULL, data)
dv |
string; name of the outcome variable |
groupId |
string; name of the group identifier variable |
actId |
string; name of the actor identifier variable |
partId |
string; name of the partner identifier variable |
feVars |
character vector of fixed-effect predictor names, or
|
data |
a |
see srm_run
srm_var_pct insteadsrmVarPct has been renamed to srm_var_pct.
srmVarPct(object)srmVarPct(object)
object |
an |
see srm_var_pct
Summarize a cpsrm Object
## S3 method for class 'cpsrm' summary(object, ...)## S3 method for class 'cpsrm' summary(object, ...)
object |
an object of class |
... |
additional arguments passed to |
object, invisibly
Internal method that produces a summary.pdMat representation of
a pdSRM object, used by lme when printing
model output.
## S3 method for class 'pdSRM' summary(object, structName = "Social Relations Model", ...)## S3 method for class 'pdSRM' summary(object, structName = "Social Relations Model", ...)
object |
an object inheriting from |
structName |
a character string describing the covariance structure;
defaults to |
... |
optional arguments passed to other methods |
an object of class summary.pdMat with additional attributes
structName and noCorrelation
d <- createDummies( group.id = "groupId", act.id = "actId", part.id = "partId", d = sampleDyadData[sampleDyadData$timeId == 1, ], merge.original = TRUE ) o <- nlme::lme( liking ~ 1, random = list(groupId = nlme::pdBlocked(list( nlme::pdIdent(~1), pdSRM(~ -1 + a1 + a2 + a3 + a4 + p1 + p2 + p3 + p4) ))), correlation = nlme::corCompSymm(form = ~1 | groupId / pdSRM_dyad_id), data = d, na.action = stats::na.omit )d <- createDummies( group.id = "groupId", act.id = "actId", part.id = "partId", d = sampleDyadData[sampleDyadData$timeId == 1, ], merge.original = TRUE ) o <- nlme::lme( liking ~ 1, random = list(groupId = nlme::pdBlocked(list( nlme::pdIdent(~1), pdSRM(~ -1 + a1 + a2 + a3 + a4 + p1 + p2 + p3 + p4) ))), correlation = nlme::corCompSymm(form = ~1 | groupId / pdSRM_dyad_id), data = d, na.action = stats::na.omit )