View source: R/survey_to_diffnet.R
survey_to_diffnet | R Documentation |
diffnet
objectThese convenient functions turn network nomination datasets and edgelists with
vertex attributes datasets into diffnet objects. Both work as wrappers of
edgelist_to_adjmat
and new_diffnet
.
survey_to_diffnet(
dat,
idvar,
netvars,
toavar,
groupvar = NULL,
no.unsurveyed = TRUE,
timevar = NULL,
t = NULL,
undirected = getOption("diffnet.undirected", FALSE),
self = getOption("diffnet.self", FALSE),
multiple = getOption("diffnet.multiple", FALSE),
keep.isolates = TRUE,
recode.ids = TRUE,
warn.coercion = TRUE,
...
)
edgelist_to_diffnet(
edgelist,
w = NULL,
t0 = NULL,
t1 = NULL,
dat,
idvar,
toavar,
timevar = NULL,
undirected = getOption("diffnet.undirected", FALSE),
self = getOption("diffnet.self", FALSE),
multiple = getOption("diffnet.multiple", FALSE),
fill.missing = NULL,
keep.isolates = TRUE,
recode.ids = TRUE,
warn.coercion = TRUE
)
dat |
A data frame. |
idvar |
Character scalar. Name of the id variable. |
netvars |
Character vector. Names of the network nomination variables. |
toavar |
Character scalar. Name of the time of adoption variable. |
groupvar |
Character scalar. Name of cohort variable (e.g. city). |
no.unsurveyed |
Logical scalar. When |
timevar |
Character sacalar. In the case of longitudinal data, name of the time var. |
t |
Integer scalar. Repeat the network |
undirected |
Logical scalar. When |
self |
Logical scalar. When |
multiple |
Logical scalar. When |
keep.isolates |
Logical scalar. When FALSE, rows with |
recode.ids |
Logical scalar. When TRUE ids are recoded using |
warn.coercion |
Logical scalar. When |
... |
Further arguments to be passed to |
edgelist |
Two column matrix/data.frame in the form of ego -source- and alter -target- (see details). |
w |
Numeric vector. Strength of ties (optional). |
t0 |
Integer vector. Starting time of the ties (optional). |
t1 |
Integer vector. Finishing time of the ties (optional). |
fill.missing |
Character scalar. In the case of having unmatching ids
between |
All of netvars
, toavar
and groupvar
must be integers. Were these numeric they are coerced into integers, otherwise,
when neither of both, the function returns with error. idvar
, on the
other hand, should only be integer when calling survey_to_diffnet
,
on the contrary, for edgelist_to_diffnet
, idvar
may be character.
In field work it is not unusual that some respondents nominate unsurveyed
individuals. In such case, in order to exclude them from the analysis,
the user can set no.unsurveyed=TRUE
(the default), telling the
function to exclude such individuals from the adjacency matrix. This is
done by setting variables in netvars
equal to NA
when the
nominated id can't be found in idvar
.
If the network nomination process was done in different groups (location
for example) the survey id numbers may be define uniquely within each group
but not across groups (there may be many individuals with id=1
,
for example). To encompass this issue, the user can tell the function what
variable can be used to distinguish between groups through the groupvar
argument. When groupvar
is provided, function redifines idvar
and the variables in netvars
as follows:
dat[[idvar]] <- dat[[idvar]] + dat[[groupvar]]*z
Where z = 10^nchar(max(dat[[idvar]]))
.
For longitudinal data, it is assumed that the toavar
holds the same
information through time, this is, time-invariable. This as the package does
not yet support variable times of adoption.
The fill.missing
option can take any of these three values: "edgelist"
,
"dat"
, or "both"
. This argument works as follows:
When fill.missing="edgelist"
(or "both"
) the function
will check which vertices show in dat
but do not show in edgelist
.
If there is any, the function will include these in edgelist
as ego to
NA
(so they have no link to anyone), and, if specified, will fill
the t0
, t1
vectors with NA
s for those cases. If
w
is also specified, the new vertices will be set to
min(w, na.rm=TRUE)
.
When fill.missing="dat"
(or "both"
) the function
checks which vertices show in edgelist
but not in dat
. If
there is any, the function will include these in dat
by adding
one row per individual.
A diffnet
object.
Vega Yon
fakesurvey
, fakesurveyDyn
Other data management functions:
diffnet-class
,
edgelist_to_adjmat()
,
egonet_attrs()
,
isolated()
# Loading a fake survey (data frame)
data(fakesurvey)
# Diffnet object keeping isolated vertices ----------------------------------
dn1 <- survey_to_diffnet(fakesurvey, "id", c("net1", "net2", "net3"), "toa",
"group", keep.isolates=TRUE)
# Diffnet object NOT keeping isolated vertices
dn2 <- survey_to_diffnet(fakesurvey, "id", c("net1", "net2", "net3"), "toa",
"group", keep.isolates=FALSE)
# dn1 has an extra vertex than dn2
dn1
dn2
# Loading a longitudinal survey data (two waves) ----------------------------
data(fakesurveyDyn)
groupvar <- "group"
x <- survey_to_diffnet(
fakesurveyDyn, "id", c("net1", "net2", "net3"), "toa", "group" ,
timevar = "time", keep.isolates = TRUE, warn.coercion=FALSE)
plot_diffnet(x, vertex.label = rownames(x))
# Reproducing medInnovationsDiffNet object ----------------------------------
data(medInnovations)
# What are the netvars
netvars <- names(medInnovations)[grepl("^net", names(medInnovations))]
medInnovationsDiffNet2 <- survey_to_diffnet(
medInnovations,
"id", netvars, "toa", "city",
warn.coercion=FALSE)
medInnovationsDiffNet2
# Comparing with the package's version
all(diffnet.toa(medInnovationsDiffNet2) == diffnet.toa(medInnovationsDiffNet)) #TRUE
all(
diffnet.attrs(medInnovationsDiffNet2, as.df = TRUE) ==
diffnet.attrs(medInnovationsDiffNet, as.df = TRUE),
na.rm=TRUE) #TRUE
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.