Init
library(kirkegaard)
## Loading required package: tidyverse
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr 1.1.2 ✔ readr 2.1.4
## ✔ forcats 1.0.0 ✔ stringr 1.5.0
## ✔ ggplot2 3.4.2 ✔ tibble 3.2.1
## ✔ lubridate 1.9.2 ✔ tidyr 1.3.0
## ✔ purrr 1.0.1
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag() masks stats::lag()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
## Loading required package: magrittr
##
##
## Attaching package: 'magrittr'
##
##
## The following object is masked from 'package:purrr':
##
## set_names
##
##
## The following object is masked from 'package:tidyr':
##
## extract
##
##
## Loading required package: weights
##
## Loading required package: Hmisc
##
##
## Attaching package: 'Hmisc'
##
##
## The following objects are masked from 'package:dplyr':
##
## src, summarize
##
##
## The following objects are masked from 'package:base':
##
## format.pval, units
##
##
## Loading required package: assertthat
##
##
## Attaching package: 'assertthat'
##
##
## The following object is masked from 'package:tibble':
##
## has_name
##
##
## Loading required package: psych
##
##
## Attaching package: 'psych'
##
##
## The following object is masked from 'package:Hmisc':
##
## describe
##
##
## The following objects are masked from 'package:ggplot2':
##
## %+%, alpha
##
##
##
## Attaching package: 'kirkegaard'
##
##
## The following object is masked from 'package:psych':
##
## rescale
##
##
## The following object is masked from 'package:assertthat':
##
## are_equal
##
##
## The following object is masked from 'package:purrr':
##
## is_logical
##
##
## The following object is masked from 'package:base':
##
## +
load_packages(
lavaan
)
## This is lavaan 0.6-15
## lavaan is FREE software! Please report any bugs.
##
## Attaching package: 'lavaan'
##
## The following object is masked from 'package:psych':
##
## cor2cov
theme_set(theme_bw())
options(
digits = 3
)
Analysis
#parameters
mz_r = .50
dz_r = .25
bw_latent_d = 1
pairs_per_group = 1e5
prob_sd = 0
prob_mean = 1
#matrices
mz_mat = matrix(c(1, mz_r, mz_r, 1), ncol = 2)
dz_mat = matrix(c(1, dz_r, dz_r, 1), ncol = 2)
#bias vector, random chance that gets convicted/arrested for hostile racism reasons
bias_vector = seq(0, 1, by = .025) %>% rep(3) %>% sort()
#simulate mz twins
set.seed(1)
results = map_df(bias_vector, function(bias) {
# browser()
#simulate mz's
mz_white_latent = MASS::mvrnorm(n = pairs_per_group, mu = c(0, 0), Sigma = mz_mat)
#dz's
dz_white_latent = MASS::mvrnorm(n = pairs_per_group, mu = c(0, 0), Sigma = dz_mat)
#blacks
mz_black_latent = MASS::mvrnorm(n = pairs_per_group, mu = c(bw_latent_d, bw_latent_d), Sigma = mz_mat)
dz_black_latent = MASS::mvrnorm(n = pairs_per_group, mu = c(bw_latent_d, bw_latent_d), Sigma = dz_mat)
#join vectors
d = cbind(
mz_white_latent,
dz_white_latent,
mz_black_latent,
dz_black_latent
) %>% as.matrix() %>% set_colnames(c("white_mz_1", "white_mz_2", "white_dz_1", "white_dz_2", "black_mz_1", "black_mz_2", "black_dz_1", "black_dz_2"))
#prob of getting caught
d_chance_for_caught = pnorm(d, mean = prob_mean, sd = prob_sd, lower.tail = T)
#convert to binary by sampling
d_caught = matrix(rbinom(length(as.vector(d_chance_for_caught)), size = 1, prob = as.vector(d_chance_for_caught)), nrow = nrow(d))
#add bias
bias_caughts = rbinom(pairs_per_group*4, size = 1, prob = bias)
d_caught[, 5:8] = d_caught[, 5:8] + bias_caughts
d_caught[d_caught > 1] = 1
#twin tetrachorics
tetras = psych::tetrachoric(d_caught)
#h2s from falconer
tibble(
bias = bias,
white_h2 = try_else((tetras$rho[1, 2]-tetras$rho[3, 4]) * 2, else. = NA),
black_h2 = try_else((tetras$rho[5, 6]-tetras$rho[7, 8]) * 2, else. = NA)
)
})
## Warning in psych::tetrachoric(d_caught): Item = had no variance and was deleted
## Warning in psych::tetrachoric(d_caught): Item = had no variance and was deleted
## Warning in psych::tetrachoric(d_caught): Item = had no variance and was deleted
## Warning in psych::tetrachoric(d_caught): Item = had no variance and was deleted
## Warning in psych::tetrachoric(d_caught): Item = had no variance and was deleted
## Warning in psych::tetrachoric(d_caught): Item = had no variance and was deleted
## Warning in psych::tetrachoric(d_caught): Item = had no variance and was deleted
## Warning in psych::tetrachoric(d_caught): Item = had no variance and was deleted
## Warning in psych::tetrachoric(d_caught): Item = had no variance and was deleted
## Warning in psych::tetrachoric(d_caught): Item = had no variance and was deleted
## Warning in psych::tetrachoric(d_caught): Item = had no variance and was deleted
## Warning in psych::tetrachoric(d_caught): Item = had no variance and was deleted
#plot results
results %>%
pivot_longer(cols = -bias) %>%
ggplot(aes(bias, value, color = name)) +
geom_smooth() +
scale_color_discrete("Parameter") +
scale_x_continuous("Random extra chance of getting caught that only applies to Blacks", labels = scales::percent) +
scale_y_continuous("Heritability estimate from MZ-DZ design", labels = scales::percent)
## `geom_smooth()` using method = 'loess' and formula = 'y ~ x'
## Warning: Removed 3 rows containing non-finite values (`stat_smooth()`).
