
No Database? No Problem: Using discord with Simple Family Structures
Mason Garrison
Source:vignettes/links.Rmd
links.Rmd
Introduction
The {discord} package was originally developed for use with the National Longitudinal Survey of Youth (NLSY), but its functionality extends far beyond that. When paired with its sister package {BGmisc}, discord can be applied to any dataset containing basic family structure information, allowing researchers to explore genetic and environmental influences without requiring pre-constructed kinship links.
This vignette demonstrates how to:
- Construct kinship links from simple family data (e.g., individual ID, mother ID, father ID).
- Simulate phenotyipic data based on known genetic and environmental structures.
- Fit a discordant-kinship regression model using the simulated data.
We use tools from {BGmisc} and a toy dataset to illustrate the workflow.
Loading Packages and Data
We begin by loading the required packages and a built-in dataset from {BGmisc}.
We rename the family ID column to avoid naming conflicts and generate a pedigree-encoded data frame.
df_potter <- potter
names(df_potter)[names(df_potter) == "famID"] <- "oldfam"
df_potter <- ped2fam(df_potter,
famID = "famID",
personID = "personID"
)
We also verify and repair sex coding to ensure compatibility with downstream pedigree operations.
df_potter <- checkSex(df_potter,
code_male = 1,
code_female = 0,
verbose = FALSE, repair = TRUE
)
ggpedigree(potter, config = list(
label_method = "geom_text",
label_nudge_y = -.25
)) +
labs(title = "Pedigree Plot of the Potter Dataset") +
theme(legend.position = "bottom")

Pedigree plot of the Potter dataset
The pedigree plot provides a visual representation of the kinship structure in the dataset. Each node represents an individual, and the edges indicate familial relationships.
Constructing Kinship Links
To extract the necessary kinship information, we need to compute two matrices: the additive genetic relatedness matrix and the shared environment matrix. These matrices are derived from the pedigree data and are essential for understanding the genetic and environmental relationships among individuals.
Using {BGmisc}, we compute:
The additive genetic relatedness matrix (add).
The shared environment matrix (cn), indicating whether kin were raised together (1) or apart (0).
The ped2add()
function computes the additive genetic
relatedness matrix, which quantifies the genetic similarity between
individuals based on their pedigree information. The
ped2cn()
function computes the shared environment matrix,
which indicates whether individuals were raised in the same environment
(1) or different environments (0).
The resulting matrices are symmetric, with diagonal elements representing self-relatedness (1.0). The off-diagonal elements represent the relatedness between pairs of individuals, with values ranging from 0 (no relatedness) to 0.5 (full siblings) to 1 (themselves).
We convert the component matrices into a long-form data frame of kin
pairs using com2links()
. Self-pairs and duplicate entries
are removed.
df_links <- com2links(
writetodisk = FALSE,
ad_ped_matrix = add,
cn_ped_matrix = cn,
drop_upper_triangular = TRUE
) %>%
filter(ID1 != ID2)
df_links %>%
slice(1:10) %>%
knitr::kable()
ID1 | ID2 | addRel | cnuRel |
---|---|---|---|
1 | 2 | 0.500 | 1 |
3 | 4 | 0.500 | 1 |
1 | 6 | 0.500 | 0 |
2 | 6 | 0.250 | 0 |
3 | 6 | 0.500 | 0 |
4 | 6 | 0.250 | 0 |
3 | 7 | 0.250 | 0 |
4 | 7 | 0.500 | 0 |
5 | 7 | 0.500 | 0 |
6 | 7 | 0.125 | 0 |
As you can see, the df_links
data frame contains pairs
of individuals (ID1 and ID2) along with their additive genetic
relatedness (addRel
) and shared environment status
(cnuRel
). These data are in wide format, with each row
representing a unique pair of individuals.
Further, we can tally the number of pairs by relatedness and shared environment to understand the composition of the dataset.
df_links %>%
group_by(addRel, cnuRel) %>%
tally()
#> # A tibble: 5 × 3
#> # Groups: addRel [4]
#> addRel cnuRel n
#> <dbl> <dbl> <int>
#> 1 0.0625 0 3
#> 2 0.125 0 47
#> 3 0.25 0 104
#> 4 0.5 0 50
#> 5 0.5 1 32
As you can see, the dataset contains a variety of kinship pairs, including full siblings, parent-child, aunt-nephew, cousins, and unrelated individuals, with varying degrees of shared environment.
For this demonstration, we will focus on cousins. Cousins share some genetic relatedness but typically do not share the same environment. (In contrast, full siblings share both genetic relatedness and environment. Half-siblings share genetic relatedness but sometimes do not share the same environment, making them less ideal for this demonstration.)
We then extract two subsets:
Full siblings: additive relatedness = 0.5 and shared environment = 1
Cousins: additive relatedness = 0.125 and shared environment = 0
df_siblings <- df_links %>%
filter(addRel == .5) %>% # only full siblings %>%
filter(cnuRel == 1) # only kin raised in the same home
df_cousin <- df_links %>%
filter(addRel == .125) %>% # only cousins %>%
filter(cnuRel == 0) # only kin raised in separate homes
Now for the rest of the vignette, we will use the cousin subset
(df_cousin
) to illustrate the process of simulating
phenotypic data and fitting a discordant-kinship regression model.
However, given the small sample size of cousins in this dataset, we will
simulate four datasets worth of cousins and combine them to increase the
sample size.
Simulate Phenotypic Data
To simulate phenotypic data, we need to create a data frame that
includes the kinship information and the outcome variables. We will
simulate two outcome variables (y1 and y2) for each kin pair in the
dataset. The kinsim()
function from {discord} is used to
generate the simulated data based on the specified variance structure.
For convenience, we will generate data for all the cousins in
df_links
. However, we could also generate data for the full
siblings or any other kinship pairs.
set.seed(1234)
syn_df <- discord::kinsim(
mu_all = c(2, 2),
cov_a = .4,
cov_e = .4,
c_vector = df_cousin$cnuRel,
r_vector = df_cousin$addRel
) %>%
select(-c(
A1_1, A1_2, A2_1, A2_2,
C1_1, C1_2, C2_1, C2_2,
E1_1, E1_2, E2_1, E2_2,
r
))
The simulated data reflect a known variance structure: additive genetic covariance = .4, genetic relatedness of 0.125, no shared environment, and residual (unique environment) variance = 0.4. Latent component scores are dropped from the final dataset, but they can be useful for debugging and understanding the underlying structure of the data.
We bind the simulated outcome data to the links data to prepare it for modeling.
data_demo <- cbind(df_cousin, syn_df)
data_demo %>% arrange(ID1, ID2)
#> ID1 ID2 addRel cnuRel y1_1 y1_2 y2_1 y2_2
#> 1 3 21 0.125 0 0.69846814 -0.94636348 0.920375422 0.87956344
#> 2 3 22 0.125 0 3.40823165 0.17476391 4.545797248 0.27764247
#> 3 3 23 0.125 0 -1.94372204 -1.45718300 2.836008802 2.43565764
#> 4 6 7 0.125 0 3.01882933 5.15425162 0.704106398 0.93334805
#> 5 21 24 0.125 0 0.50565312 0.07001350 1.127500331 4.28525278
#> 6 21 25 0.125 0 -0.32320318 -0.33395629 4.726914459 1.85020416
#> 7 21 26 0.125 0 0.63227002 0.62905494 2.260364719 1.32571921
#> 8 21 27 0.125 0 -0.31606328 3.82847961 -0.928464875 2.00919033
#> 9 21 28 0.125 0 4.08069628 1.50350387 2.867664982 1.77157482
#> 10 21 29 0.125 0 -1.95737955 0.70051737 0.008410092 2.07361756
#> 11 21 30 0.125 0 1.70970996 1.05415555 1.782903640 1.79155270
#> 12 21 103 0.125 0 2.06682749 1.65011592 2.187000009 1.20469998
#> 13 21 104 0.125 0 1.14229170 -0.19141877 0.792392616 1.18283278
#> 14 22 24 0.125 0 0.10668397 1.59669587 -0.487594881 1.75466144
#> 15 22 25 0.125 0 1.16168365 0.41534542 2.608283934 1.47646921
#> 16 22 26 0.125 0 1.31783290 -0.03437594 4.609091853 0.89717615
#> 17 22 27 0.125 0 0.60695320 2.18907810 1.287713853 2.57998769
#> 18 22 28 0.125 0 0.64517182 -0.84749188 3.601714590 1.49074954
#> 19 22 29 0.125 0 0.23819640 0.22751924 2.347311337 3.30945449
#> 20 22 30 0.125 0 0.29585492 2.68083606 1.255927432 1.32303233
#> 21 22 103 0.125 0 0.61098830 1.71517941 3.044302316 1.84972968
#> 22 22 104 0.125 0 -0.38405444 4.37461931 0.133794183 2.12651574
#> 23 23 24 0.125 0 1.22764109 2.47305999 1.091900867 0.94020534
#> 24 23 25 0.125 0 2.02299360 1.76964074 3.246089835 -0.67759088
#> 25 23 26 0.125 0 2.65651806 3.13207779 -2.440870298 0.80347887
#> 26 23 27 0.125 0 3.20883406 4.97559692 5.728700996 4.79207586
#> 27 23 28 0.125 0 2.14002232 1.77686133 1.561928173 -2.27507118
#> 28 23 29 0.125 0 1.97789035 3.61150936 2.540224327 4.89868499
#> 29 23 30 0.125 0 -0.28597900 2.31880167 1.522478333 3.15312027
#> 30 23 103 0.125 0 1.25830077 2.56570250 2.124383422 0.94834372
#> 31 23 104 0.125 0 -1.73002590 0.25970615 1.968454264 1.74940144
#> 32 24 26 0.125 0 4.63606567 3.17611587 4.046641787 -0.37020995
#> 33 24 27 0.125 0 3.91419476 5.21805811 3.353984387 3.64524852
#> 34 24 28 0.125 0 1.18870807 2.46329166 1.085839815 5.59984940
#> 35 24 29 0.125 0 2.63545918 4.55539599 0.987938143 3.17816912
#> 36 24 30 0.125 0 -1.73645162 -0.66918356 0.620498160 1.31457695
#> 37 25 26 0.125 0 4.40528856 2.56768987 4.316321007 1.81883759
#> 38 25 27 0.125 0 4.39406665 4.45812180 5.449926010 6.78769670
#> 39 25 28 0.125 0 1.92473158 2.92607522 1.028981788 3.65074244
#> 40 25 29 0.125 0 1.44725719 -0.68754952 1.918919094 3.20822306
#> 41 25 30 0.125 0 -0.77502103 0.35202165 -1.142066713 1.83747531
#> 42 26 29 0.125 0 2.86533392 1.38588316 5.783580786 4.02286105
#> 43 26 30 0.125 0 1.83054314 4.36852908 0.538535089 1.01900506
#> 44 27 29 0.125 0 1.14203289 2.45824537 1.908453123 0.88470985
#> 45 27 30 0.125 0 1.77537479 0.87306128 1.751892601 3.49409381
#> 46 28 29 0.125 0 2.59893560 3.57686790 1.249120243 2.53547824
#> 47 28 30 0.125 0 3.60956359 2.12462877 4.330495330 3.48404661
#> 48 1003 1021 0.125 0 1.97005047 0.86691992 3.631197656 2.20815334
#> 49 1003 1022 0.125 0 -0.15759982 -0.41923324 3.150476415 1.38444915
#> 50 1003 1023 0.125 0 1.58157176 1.07812202 3.368944830 0.58233747
#> 51 1006 1007 0.125 0 1.02561600 1.18820265 4.839705156 4.11321093
#> 52 1021 1024 0.125 0 4.09898798 4.41780710 2.356276650 2.48734185
#> 53 1021 1025 0.125 0 0.61923263 0.52475541 0.386341967 2.27451119
#> 54 1021 1026 0.125 0 0.27188594 4.59616061 2.220132171 3.46335522
#> 55 1021 1027 0.125 0 3.24118586 4.52126835 1.310222584 3.60144597
#> 56 1021 1028 0.125 0 2.76422054 0.34599818 0.207905244 0.24019578
#> 57 1021 1029 0.125 0 2.45912415 4.00315580 1.252753115 0.49789009
#> 58 1021 1030 0.125 0 1.14402607 2.88778769 1.089960632 2.33271469
#> 59 1021 1103 0.125 0 0.25594259 1.17784922 0.770764924 4.41577350
#> 60 1021 1104 0.125 0 0.32445587 1.81049085 -0.244551632 4.44735120
#> 61 1022 1024 0.125 0 -1.24667825 2.85584665 -2.959229330 4.02400566
#> 62 1022 1025 0.125 0 2.05539924 3.66501205 2.013244451 1.86203992
#> 63 1022 1026 0.125 0 3.65491976 6.15431123 -0.202493502 0.32969429
#> 64 1022 1027 0.125 0 1.16406220 3.81316688 1.276968823 0.90232357
#> 65 1022 1028 0.125 0 0.86448282 2.95959568 0.599598152 0.71448731
#> 66 1022 1029 0.125 0 3.58532908 3.99371218 6.378181325 5.47584129
#> 67 1022 1030 0.125 0 1.69497955 2.86669091 -0.225854290 -0.81749862
#> 68 1022 1103 0.125 0 1.40431244 2.88463385 4.607148200 5.91609649
#> 69 1022 1104 0.125 0 4.27152819 0.30722793 4.872168397 3.64951112
#> 70 1023 1024 0.125 0 3.59158082 3.38144551 0.952843381 3.40585862
#> 71 1023 1025 0.125 0 3.88499849 3.19639146 0.586539169 -0.14286518
#> 72 1023 1026 0.125 0 0.67531549 2.15256522 1.948371322 -0.07765736
#> 73 1023 1027 0.125 0 4.26096106 3.81601896 1.691358704 1.77031286
#> 74 1023 1028 0.125 0 2.99906768 1.35737104 3.195730118 -0.13214421
#> 75 1023 1029 0.125 0 2.88522812 2.58155270 3.001070723 4.76937503
#> 76 1023 1030 0.125 0 1.75123355 2.96488921 -1.035578410 0.03823606
#> 77 1023 1103 0.125 0 2.43846989 3.06971490 0.123468616 3.32049729
#> 78 1023 1104 0.125 0 1.93017752 1.39917782 2.433469579 2.16233863
#> 79 1024 1026 0.125 0 -0.43374688 -0.06272909 4.982572634 2.60994865
#> 80 1024 1027 0.125 0 2.21803221 1.23787061 1.126569563 0.93694929
#> 81 1024 1028 0.125 0 1.38870183 4.16369227 -0.692760735 0.71367105
#> 82 1024 1029 0.125 0 1.58310104 -1.26832941 1.329082525 1.95908061
#> 83 1024 1030 0.125 0 1.32035834 4.85643317 3.302375894 2.51316624
#> 84 1025 1026 0.125 0 3.59676436 2.04180467 2.912525670 5.15465238
#> 85 1025 1027 0.125 0 1.10099308 1.46007986 3.710292891 5.09908936
#> 86 1025 1028 0.125 0 4.59312035 4.36154651 1.681457524 2.28199151
#> 87 1025 1029 0.125 0 0.61848274 0.36035748 6.497993750 2.50039040
#> 88 1025 1030 0.125 0 3.48832726 3.34003451 0.362942463 -2.32757983
#> 89 1026 1029 0.125 0 -1.08618410 -0.24153814 0.913675780 0.44234687
#> 90 1026 1030 0.125 0 0.35884886 3.88177318 -0.681890293 1.36569217
#> 91 1027 1029 0.125 0 1.52733343 2.64921283 5.646962827 3.72562085
#> 92 1027 1030 0.125 0 3.16252779 4.33411425 2.979207093 5.05642709
#> 93 1028 1029 0.125 0 -0.10878472 0.21797880 1.635037625 1.67453784
#> 94 1028 1030 0.125 0 3.11922568 1.69961400 4.270490471 0.15978179
#> 95 2003 2021 0.125 0 0.01008397 2.44158723 1.322590152 2.33622972
#> 96 2003 2022 0.125 0 2.01934447 2.95303985 1.236639184 2.02773182
#> 97 2003 2023 0.125 0 0.26860603 1.67712628 1.516460509 2.66787015
#> 98 2006 2007 0.125 0 1.44467994 -0.68939671 3.430320437 1.73366172
#> 99 2021 2024 0.125 0 2.23342467 0.59073535 2.527318154 2.72603021
#> 100 2021 2025 0.125 0 1.80353492 1.90511687 2.797104308 2.21214100
#> 101 2021 2026 0.125 0 1.10039888 0.87537897 -2.108694064 -0.20026249
#> 102 2021 2027 0.125 0 1.48005433 0.54139392 4.939996901 -1.53868372
#> 103 2021 2028 0.125 0 3.40507461 1.87489676 5.818278830 3.72290982
#> 104 2021 2029 0.125 0 1.02992789 1.01430745 0.633210514 0.77333629
#> 105 2021 2030 0.125 0 0.73843784 3.41853478 1.370284646 2.40891773
#> 106 2021 2103 0.125 0 1.83515087 -0.24408870 0.519215576 0.73095029
#> 107 2021 2104 0.125 0 1.86076376 2.11784844 0.261609339 -1.86684480
#> 108 2022 2024 0.125 0 6.03581109 0.87870788 2.163792006 1.93584311
#> 109 2022 2025 0.125 0 2.20781859 2.22768530 5.595201840 2.57560949
#> 110 2022 2026 0.125 0 2.16656823 3.14739351 1.401375351 -0.82456389
#> 111 2022 2027 0.125 0 0.33960815 0.50426109 -0.033185453 1.63945645
#> 112 2022 2028 0.125 0 2.59337726 2.53547772 1.422447358 1.06792557
#> 113 2022 2029 0.125 0 3.53585567 2.72955386 4.309858841 2.00404090
#> 114 2022 2030 0.125 0 0.24191951 1.61860826 2.961277893 2.97456886
#> 115 2022 2103 0.125 0 2.76259013 2.99805201 2.879406044 2.04825804
#> 116 2022 2104 0.125 0 0.17863724 2.32111598 0.989582532 1.90574311
#> 117 2023 2024 0.125 0 0.85413022 -0.32477344 2.001552441 -0.80069432
#> 118 2023 2025 0.125 0 -0.14438991 3.88321536 1.891014193 2.00181430
#> 119 2023 2026 0.125 0 0.95286359 3.43715198 0.916249062 5.10745059
#> 120 2023 2027 0.125 0 -1.10226117 2.05078301 0.946049664 1.99646315
#> 121 2023 2028 0.125 0 -0.19377461 0.62219590 5.828265237 1.96073720
#> 122 2023 2029 0.125 0 2.01484951 1.85046488 2.381261527 1.20398637
#> 123 2023 2030 0.125 0 -0.74965765 1.64923567 -0.539468692 3.38459278
#> 124 2023 2103 0.125 0 0.50969692 2.64555462 3.120357717 2.98949647
#> 125 2023 2104 0.125 0 3.04450676 2.46671868 -0.403829763 0.10458979
#> 126 2024 2026 0.125 0 2.51986484 3.93154528 1.113910477 1.61168605
#> 127 2024 2027 0.125 0 2.53019627 1.60585604 1.988084132 0.47649356
#> 128 2024 2028 0.125 0 4.27475318 0.95053455 0.029041203 -1.17864367
#> 129 2024 2029 0.125 0 1.15161991 5.78313763 3.294968782 6.23941239
#> 130 2024 2030 0.125 0 -0.34503000 -1.06570049 -0.395208953 3.52886851
#> 131 2025 2026 0.125 0 2.11080593 -1.31638895 2.620668208 0.60045839
#> 132 2025 2027 0.125 0 4.72982427 1.59253143 4.793803346 3.02691709
#> 133 2025 2028 0.125 0 2.20746340 1.16728822 -0.897716639 1.87890973
#> 134 2025 2029 0.125 0 3.94616445 3.05117068 5.174353420 0.94910996
#> 135 2025 2030 0.125 0 1.06814719 1.79692613 1.494132136 -0.30824558
#> 136 2026 2029 0.125 0 3.55379258 2.54987701 1.553701091 2.76705626
#> 137 2026 2030 0.125 0 1.62559671 0.41623414 1.798336555 3.35331044
#> 138 2027 2029 0.125 0 1.51625614 1.58330224 0.548875650 1.22092007
#> 139 2027 2030 0.125 0 3.12610673 1.79950357 4.002116182 3.44915127
#> 140 2028 2029 0.125 0 -0.37933517 1.20077497 1.034803595 2.59159702
#> 141 2028 2030 0.125 0 0.08028772 2.54972068 0.673647068 3.34630930
#> 142 3003 3021 0.125 0 4.65901585 2.46208313 1.147799043 0.30225144
#> 143 3003 3022 0.125 0 3.81028920 4.59435753 6.783472656 4.26143539
#> 144 3003 3023 0.125 0 2.84216887 4.63638366 2.467818845 3.39928514
#> 145 3006 3007 0.125 0 5.95134313 3.09842371 2.636060952 2.19043814
#> 146 3021 3024 0.125 0 2.54173633 3.11894396 2.295140123 3.32239768
#> 147 3021 3025 0.125 0 -0.38298492 -1.52546840 3.515103858 2.67872699
#> 148 3021 3026 0.125 0 4.28584353 2.57547280 2.922155461 -0.70478093
#> 149 3021 3027 0.125 0 4.52202218 2.28168026 4.512342523 0.45095853
#> 150 3021 3028 0.125 0 -0.41458214 2.25220537 2.808565102 1.17027626
#> 151 3021 3029 0.125 0 2.87024861 0.51652949 0.846841149 0.83058299
#> 152 3021 3030 0.125 0 3.15793736 2.67247992 4.027732834 2.32755332
#> 153 3021 3103 0.125 0 3.86904319 2.71737770 5.120842228 5.99486488
#> 154 3021 3104 0.125 0 5.04816052 4.30687146 0.408299557 3.22804038
#> 155 3022 3024 0.125 0 4.96152870 5.14451023 4.345665353 6.28411321
#> 156 3022 3025 0.125 0 -0.50561122 2.79024989 -1.934272383 2.14916384
#> 157 3022 3026 0.125 0 2.10739233 1.25059764 0.719657134 -0.37070075
#> 158 3022 3027 0.125 0 1.84457902 4.53345514 4.933572109 3.35250856
#> 159 3022 3028 0.125 0 -0.38268250 0.93948816 0.220654492 2.91992787
#> 160 3022 3029 0.125 0 3.26987253 2.79825318 4.058094835 2.69270426
#> 161 3022 3030 0.125 0 3.06442615 2.35015794 0.863525982 1.61770401
#> 162 3022 3103 0.125 0 1.10394236 1.93109335 4.753874204 4.37904371
#> 163 3022 3104 0.125 0 1.98892023 2.81513896 1.929299889 1.44441959
#> 164 3023 3024 0.125 0 2.22884773 1.32953739 1.904015405 4.14374992
#> 165 3023 3025 0.125 0 0.59670275 -0.52636417 1.875099550 1.93705955
#> 166 3023 3026 0.125 0 0.13376614 4.01233588 -0.207419145 2.19578293
#> 167 3023 3027 0.125 0 0.46220675 1.85183681 1.771619127 1.58463867
#> 168 3023 3028 0.125 0 2.27410594 1.20729710 4.138487409 2.16666481
#> 169 3023 3029 0.125 0 2.19700574 4.02109917 2.364186913 3.11850319
#> 170 3023 3030 0.125 0 -0.20670768 1.21616472 -0.573181452 1.87992379
#> 171 3023 3103 0.125 0 1.86976958 0.75346005 3.090506914 1.51927796
#> 172 3023 3104 0.125 0 -0.58662320 1.25401426 0.468282479 3.36337822
#> 173 3024 3026 0.125 0 2.23071444 2.66227080 0.768315712 -1.31241064
#> 174 3024 3027 0.125 0 0.16077848 -0.77550135 0.287979143 2.83815333
#> 175 3024 3028 0.125 0 1.92010853 1.89998323 2.435850076 2.50709028
#> 176 3024 3029 0.125 0 3.00350049 -0.56041797 -0.738078918 -0.24161493
#> 177 3024 3030 0.125 0 4.47708078 2.08467606 2.710970064 -0.36804116
#> 178 3025 3026 0.125 0 1.90456281 4.43778148 1.819197036 4.97611898
#> 179 3025 3027 0.125 0 1.55562773 3.20476645 2.720820270 3.56492634
#> 180 3025 3028 0.125 0 1.22874118 0.75472499 1.676522705 1.23371711
#> 181 3025 3029 0.125 0 4.12084706 1.74583890 0.929460845 1.70657868
#> 182 3025 3030 0.125 0 3.81292620 0.16066754 4.547564942 3.57377017
#> 183 3026 3029 0.125 0 1.66493817 4.36855933 4.154288057 0.90363406
#> 184 3026 3030 0.125 0 2.70899631 5.30832010 0.056656084 0.02094620
#> 185 3027 3029 0.125 0 0.28073869 4.89326313 3.318536893 6.72969712
#> 186 3027 3030 0.125 0 -3.36720075 0.08347487 1.310483533 2.89885735
#> 187 3028 3029 0.125 0 0.82986781 3.71128780 2.182342996 2.88598385
#> 188 3028 3030 0.125 0 0.94215452 1.76695164 -0.323438087 4.02811982
#> id
#> 1 2
#> 2 3
#> 3 4
#> 4 1
#> 5 5
#> 6 8
#> 7 11
#> 8 16
#> 9 21
#> 10 26
#> 11 34
#> 12 42
#> 13 45
#> 14 6
#> 15 9
#> 16 12
#> 17 17
#> 18 22
#> 19 27
#> 20 35
#> 21 43
#> 22 46
#> 23 7
#> 24 10
#> 25 13
#> 26 18
#> 27 23
#> 28 28
#> 29 36
#> 30 44
#> 31 47
#> 32 14
#> 33 19
#> 34 24
#> 35 29
#> 36 37
#> 37 15
#> 38 20
#> 39 25
#> 40 30
#> 41 38
#> 42 31
#> 43 39
#> 44 32
#> 45 40
#> 46 33
#> 47 41
#> 48 49
#> 49 50
#> 50 51
#> 51 48
#> 52 52
#> 53 55
#> 54 58
#> 55 63
#> 56 68
#> 57 73
#> 58 81
#> 59 89
#> 60 92
#> 61 53
#> 62 56
#> 63 59
#> 64 64
#> 65 69
#> 66 74
#> 67 82
#> 68 90
#> 69 93
#> 70 54
#> 71 57
#> 72 60
#> 73 65
#> 74 70
#> 75 75
#> 76 83
#> 77 91
#> 78 94
#> 79 61
#> 80 66
#> 81 71
#> 82 76
#> 83 84
#> 84 62
#> 85 67
#> 86 72
#> 87 77
#> 88 85
#> 89 78
#> 90 86
#> 91 79
#> 92 87
#> 93 80
#> 94 88
#> 95 96
#> 96 97
#> 97 98
#> 98 95
#> 99 99
#> 100 102
#> 101 105
#> 102 110
#> 103 115
#> 104 120
#> 105 128
#> 106 136
#> 107 139
#> 108 100
#> 109 103
#> 110 106
#> 111 111
#> 112 116
#> 113 121
#> 114 129
#> 115 137
#> 116 140
#> 117 101
#> 118 104
#> 119 107
#> 120 112
#> 121 117
#> 122 122
#> 123 130
#> 124 138
#> 125 141
#> 126 108
#> 127 113
#> 128 118
#> 129 123
#> 130 131
#> 131 109
#> 132 114
#> 133 119
#> 134 124
#> 135 132
#> 136 125
#> 137 133
#> 138 126
#> 139 134
#> 140 127
#> 141 135
#> 142 143
#> 143 144
#> 144 145
#> 145 142
#> 146 146
#> 147 149
#> 148 152
#> 149 157
#> 150 162
#> 151 167
#> 152 175
#> 153 183
#> 154 186
#> 155 147
#> 156 150
#> 157 153
#> 158 158
#> 159 163
#> 160 168
#> 161 176
#> 162 184
#> 163 187
#> 164 148
#> 165 151
#> 166 154
#> 167 159
#> 168 164
#> 169 169
#> 170 177
#> 171 185
#> 172 188
#> 173 155
#> 174 160
#> 175 165
#> 176 170
#> 177 178
#> 178 156
#> 179 161
#> 180 166
#> 181 171
#> 182 179
#> 183 172
#> 184 180
#> 185 173
#> 186 181
#> 187 174
#> 188 182
summary(data_demo)
#> ID1 ID2 addRel cnuRel
#> Min. : 3.0 Min. : 7.0 Min. :0.125 Min. :0
#> 1st Qu.: 759.2 1st Qu.: 781.2 1st Qu.:0.125 1st Qu.:0
#> Median :1515.5 Median :1555.5 Median :0.125 Median :0
#> Mean :1521.6 Mean :1536.6 Mean :0.125 Mean :0
#> 3rd Qu.:2271.8 3rd Qu.:2329.8 3rd Qu.:0.125 3rd Qu.:0
#> Max. :3028.0 Max. :3104.0 Max. :0.125 Max. :0
#> y1_1 y1_2 y2_1 y2_2
#> Min. :-3.3672 Min. :-1.5255 Min. :-2.959 Min. :-2.3276
#> 1st Qu.: 0.6044 1st Qu.: 0.8779 1st Qu.: 0.787 1st Qu.: 0.9394
#> Median : 1.7633 Median : 2.1013 Median : 1.847 Median : 2.0066
#> Mean : 1.7469 Mean : 2.0855 Mean : 2.039 Mean : 2.1207
#> 3rd Qu.: 2.9137 3rd Qu.: 3.1359 3rd Qu.: 3.208 3rd Qu.: 3.3210
#> Max. : 6.0358 Max. : 6.1543 Max. : 6.783 Max. : 6.7877
#> id
#> Min. : 1.00
#> 1st Qu.: 47.75
#> Median : 94.50
#> Mean : 94.50
#> 3rd Qu.:141.25
#> Max. :188.00
data_demo %>%
slice(1:5) %>%
knitr::kable()
ID1 | ID2 | addRel | cnuRel | y1_1 | y1_2 | y2_1 | y2_2 | id |
---|---|---|---|---|---|---|---|---|
6 | 7 | 0.125 | 0 | 3.0188293 | 5.1542516 | 0.7041064 | 0.9333481 | 1 |
3 | 21 | 0.125 | 0 | 0.6984681 | -0.9463635 | 0.9203754 | 0.8795634 | 2 |
3 | 22 | 0.125 | 0 | 3.4082317 | 0.1747639 | 4.5457972 | 0.2776425 | 3 |
3 | 23 | 0.125 | 0 | -1.9437220 | -1.4571830 | 2.8360088 | 2.4356576 | 4 |
21 | 24 | 0.125 | 0 | 0.5056531 | 0.0700135 | 1.1275003 | 4.2852528 | 5 |
The data_demo
data frame now contains the kinship
information along with the simulated outcome variables y1 and y2. Each
row represents a pair of cousins, and the columns include the IDs of the
individuals, their relatedness, and the simulated phenotypic data.
Fitting a Discordant-Kinship Regression Model
We then use discord_regression()
to fit a
discordant-kinship model, predicting y1 from y2. Based on the structure
of the data, we expect that there will be a significant association
between the two outcome variables, as there is a known overlapping
non-shared environment covariance.
The model is fit using the discord_regression()
function, which takes the following arguments:
model_output <- discord_regression(
data = data_demo,
outcome = "y1",
predictors = "y2",
id = "id",
sex = NULL,
race = NULL,
pair_identifiers = c("_1", "_2")
)
summary(model_output)
#>
#> Call:
#> stats::lm(formula = y1_diff ~ y1_mean + y2_diff + y2_mean, data = preppedData)
#>
#> Residuals:
#> Min 1Q Median 3Q Max
#> -1.8511 -0.8476 -0.1705 0.6317 3.6083
#>
#> Coefficients:
#> Estimate Std. Error t value Pr(>|t|)
#> (Intercept) 1.38903 0.17337 8.012 1.25e-13 ***
#> y1_mean 0.06308 0.06038 1.045 0.29749
#> y2_diff 0.13111 0.04081 3.213 0.00155 **
#> y2_mean -0.04300 0.05654 -0.761 0.44789
#> ---
#> Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
#>
#> Residual standard error: 1.108 on 184 degrees of freedom
#> Multiple R-squared: 0.06089, Adjusted R-squared: 0.04557
#> F-statistic: 3.976 on 3 and 184 DF, p-value: 0.00893
The output of the model includes estimates of the regression coefficients, standard errors, and p-values for the association between the two outcome variables.
Alternative Specifications
If desired, one can manually prepare the data using
discord_data()
and then fit separate models for the
individual-level, between-pair, and within-pair effects. This approach
provides more flexibility in specifying the models and allows for a
deeper understanding of the different components of the association.
data_df <- discord_data(
data = data_demo,
outcome = "y1",
predictors = "y2",
id = "id",
sex = NULL,
race = NULL,
demographics = "none",
pair_identifiers = c("_1", "_2")
)
summary(data_df)
#> id y1_1 y1_2 y1_diff
#> Min. : 1.00 Min. :-1.457 Min. :-3.3672 Min. :0.003215
#> 1st Qu.: 47.75 1st Qu.: 1.643 1st Qu.: 0.1777 1st Qu.:0.707853
#> Median : 94.50 Median : 2.723 Median : 1.1478 Median :1.311807
#> Mean : 94.50 Mean : 2.679 Mean : 1.1538 Mean :1.524696
#> 3rd Qu.:141.25 3rd Qu.: 3.882 3rd Qu.: 2.1351 3rd Qu.:2.207785
#> Max. :188.00 Max. : 6.154 Max. : 4.9615 Max. :5.157103
#> y1_mean y2_1 y2_2 y2_diff
#> Min. :-1.700 Min. :-2.109 Min. :-2.959 Min. :-3.9241
#> 1st Qu.: 1.006 1st Qu.: 1.291 1st Qu.: 0.574 1st Qu.:-0.4878
#> Median : 1.912 Median : 2.364 Median : 1.505 Median : 0.8402
#> Mean : 1.916 Mean : 2.478 Mean : 1.682 Mean : 0.7951
#> 3rd Qu.: 2.884 3rd Qu.: 3.602 3rd Qu.: 2.775 3rd Qu.: 2.0682
#> Max. : 5.053 Max. : 6.788 Max. : 6.783 Max. : 6.9832
#> y2_mean
#> Min. :-1.154
#> 1st Qu.: 1.077
#> Median : 1.936
#> Mean : 2.080
#> 3rd Qu.: 2.911
#> Max. : 6.119
lm_ind <- lm(y1_1 ~ y2_1, data = data_df)
summary(lm_ind)
#>
#> Call:
#> lm(formula = y1_1 ~ y2_1, data = data_df)
#>
#> Residuals:
#> Min 1Q Median 3Q Max
#> -4.1295 -0.9876 0.0817 1.1612 3.7977
#>
#> Coefficients:
#> Estimate Std. Error t value Pr(>|t|)
#> (Intercept) 2.30725 0.18995 12.146 <2e-16 ***
#> y2_1 0.14987 0.06282 2.386 0.018 *
#> ---
#> Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
#>
#> Residual standard error: 1.493 on 186 degrees of freedom
#> Multiple R-squared: 0.0297, Adjusted R-squared: 0.02448
#> F-statistic: 5.692 on 1 and 186 DF, p-value: 0.01804
lm_ind2 <- lm(y1_2 ~ y2_2, data = data_df)
summary(lm_ind2)
#>
#> Call:
#> lm(formula = y1_2 ~ y2_2, data = data_df)
#>
#> Residuals:
#> Min 1Q Median 3Q Max
#> -4.4598 -0.9950 -0.0377 0.9567 3.3692
#>
#> Coefficients:
#> Estimate Std. Error t value Pr(>|t|)
#> (Intercept) 0.87682 0.14193 6.178 4e-09 ***
#> y2_2 0.16466 0.05788 2.845 0.00494 **
#> ---
#> Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
#>
#> Residual standard error: 1.416 on 186 degrees of freedom
#> Multiple R-squared: 0.0417, Adjusted R-squared: 0.03655
#> F-statistic: 8.094 on 1 and 186 DF, p-value: 0.004939
lm_between <- lm(y1_mean ~ y2_mean, data = data_df)
summary(lm_between)
#>
#> Call:
#> lm(formula = y1_mean ~ y2_mean, data = data_df)
#>
#> Residuals:
#> Min 1Q Median 3Q Max
#> -3.7107 -0.8579 0.0237 0.9435 3.3296
#>
#> Coefficients:
#> Estimate Std. Error t value Pr(>|t|)
#> (Intercept) 1.5642 0.1713 9.131 <2e-16 ***
#> y2_mean 0.1692 0.0675 2.507 0.013 *
#> ---
#> Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
#>
#> Residual standard error: 1.346 on 186 degrees of freedom
#> Multiple R-squared: 0.03268, Adjusted R-squared: 0.02748
#> F-statistic: 6.284 on 1 and 186 DF, p-value: 0.01304
lm_within <- lm(y1_diff ~ y1_mean + y2_diff + y2_mean, data = data_df)
summary(lm_within)
#>
#> Call:
#> lm(formula = y1_diff ~ y1_mean + y2_diff + y2_mean, data = data_df)
#>
#> Residuals:
#> Min 1Q Median 3Q Max
#> -1.8511 -0.8476 -0.1705 0.6317 3.6083
#>
#> Coefficients:
#> Estimate Std. Error t value Pr(>|t|)
#> (Intercept) 1.38903 0.17337 8.012 1.25e-13 ***
#> y1_mean 0.06308 0.06038 1.045 0.29749
#> y2_diff 0.13111 0.04081 3.213 0.00155 **
#> y2_mean -0.04300 0.05654 -0.761 0.44789
#> ---
#> Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
#>
#> Residual standard error: 1.108 on 184 degrees of freedom
#> Multiple R-squared: 0.06089, Adjusted R-squared: 0.04557
#> F-statistic: 3.976 on 3 and 184 DF, p-value: 0.00893
Conclusion
This vignette demonstrates how {BGmisc} and discord enable researchers to perform discordant-kinship analyses starting from simple family data. There’s no need for pre-constructed kinship links or specialized datasets like the NLSY—just basic family identifiers are sufficient to generate kinship structures and apply powerful behavior genetic models.