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(
)
Simulations
#config
(g_path = sqrt(.80))
## [1] 0.8944272
(e_path = sqrt(1 - g_path^2))
## [1] 0.4472136
n = 10e3
#simulate data
set.seed(1)
d1 = tibble(
genetics = rnorm(n),
environment = rnorm(n),
genetics_effect = genetics * g_path,
environment_effect = environment * e_path,
phenotype = genetics_effect + environment_effect,
deviation_from_mean = phenotype - mean(phenotype),
genetic_deviation_from_mean = genetics_effect - mean(genetics_effect),
environment_deviation_from_mean = environment_effect - mean(environment_effect),
genetic_deviation_prop = genetic_deviation_from_mean / deviation_from_mean,
environmental_deviation_prop = environment_deviation_from_mean / deviation_from_mean,
total_prop = genetic_deviation_prop + environmental_deviation_prop
)
describe2(d1)
#proportions of deviations
d1 %>%
filter(phenotype > 2) %>%
select(genetic_deviation_prop, environmental_deviation_prop) %>%
pivot_longer(everything()) %>%
mutate(
cause = str_replace(name, "_deviation_prop", "")
) %>%
GG_denhist("value", group = "cause") +
scale_fill_discrete("Cause (fraction)") +
scale_x_continuous("Phenotype score")
## Scale for x is already present.
## Adding another scale for x, which will replace the existing scale.
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.

GG_save("figs/2+ sd proportions.png")
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
d1 %>%
filter(phenotype > 2) %>%
describe2()