Analysis
#parameters
mz_r = .50
dz_r = .25
bw_latent_d = 1
pairs_per_group = 1e5
#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()
#function
run_simulations = function(bias_vector, prob_sd = 0, prob_mean = 1) {
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)
)
})
}
#simulate mz twins
set.seed(1)
results = run_simulations(bias_vector)
## 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
#with randomness
results_05random = run_simulations(bias_vector, prob_sd = 0.5)
## 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) +
ggtitle("Based on simulated MZ-DZ data", subtitle = "No randomness")
## `geom_smooth()` using method = 'loess' and formula = 'y ~ x'
## Warning: Removed 3 rows containing non-finite values (`stat_smooth()`).

GG_save("figs/heritability, race, police bias.png")
## `geom_smooth()` using method = 'loess' and formula = 'y ~ x'
## Warning: Removed 3 rows containing non-finite values (`stat_smooth()`).
results_05random %>%
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) +
ggtitle("Based on simulated MZ-DZ data", subtitle = "With some randomness")
## `geom_smooth()` using method = 'loess' and formula = 'y ~ x'
## Warning: Removed 3 rows containing non-finite values (`stat_smooth()`).

GG_save("figs/heritability, race, police bias randomness.png")
## `geom_smooth()` using method = 'loess' and formula = 'y ~ x'
## Warning: Removed 3 rows containing non-finite values (`stat_smooth()`).