Last updated: 2021-03-19

Checks: 7 0

Knit directory: soybean_exploration/

This reproducible R Markdown analysis was created with workflowr (version 1.6.2). The Checks tab describes the reproducibility checks that were applied when the results were created. The Past versions tab lists the development history.


Great! Since the R Markdown file has been committed to the Git repository, you know the exact version of the code that produced these results.

Great job! The global environment was empty. Objects defined in the global environment can affect the analysis in your R Markdown file in unknown ways. For reproduciblity it’s best to always run the code in an empty environment.

The command set.seed(20210309) was run prior to running the code in the R Markdown file. Setting a seed ensures that any results that rely on randomness, e.g. subsampling or permutations, are reproducible.

Great job! Recording the operating system, R version, and package versions is critical for reproducibility.

Nice! There were no cached chunks for this analysis, so you can be confident that you successfully produced the results during this run.

Great job! Using relative paths to the files within your workflowr project makes it easier to run your code on other machines.

Great! You are using Git for version control. Tracking code development and connecting the code version to the results is critical for reproducibility.

The results in this page were generated with repository version a814702. See the Past versions tab to see a history of the changes made to the R Markdown and HTML files.

Note that you need to be careful to ensure that all relevant files for the analysis have been committed to Git prior to generating the results (you can use wflow_publish or wflow_git_commit). workflowr only checks the R Markdown file, but you know if there are other scripts or data files that it depends on. Below is the status of the Git repository when the results were generated:


Ignored files:
    Ignored:    .Rhistory
    Ignored:    .Rproj.user/

Note that any generated files, e.g. HTML, png, CSS, etc., are not included in this status report because it is ok for generated content to have uncommitted changes.


These are the previous versions of the repository in which changes were made to the R Markdown (analysis/NBS_component_analysis.Rmd) and HTML (docs/NBS_component_analysis.html) files. If you’ve configured a remote Git repository (see ?wflow_git_remote), click on the hyperlinks in the table below to view the files as they were in that past version.

File Version Author Date Message
html 4fc0f35 Lyron Winderbaum 2021-03-19 Build site.
Rmd 24e8424 Lyron Winderbaum 2021-03-19 wflow_publish(“analysis/NBS_component_analysis.Rmd”)

# # Cleanup and Global Settings
# rm(list = ls())
# if (!is.null(sessionInfo()$otherPkgs)) {
#   invisible(lapply(paste0('package:', names(sessionInfo()$otherPkgs)), 
#                    detach, character.only=TRUE, unload=TRUE))
# }
# graphics.off()
# options(stringsAsFactors = FALSE)

library(tidyverse)
── Attaching packages ────────────────────────────────── tidyverse 1.2.1 ──
✔ ggplot2 3.2.1     ✔ purrr   0.3.2
✔ tibble  2.1.3     ✔ dplyr   0.8.3
✔ tidyr   1.0.0     ✔ stringr 1.4.0
✔ readr   1.3.1     ✔ forcats 0.4.0
── Conflicts ───────────────────────────────────── tidyverse_conflicts() ──
✖ dplyr::filter() masks stats::filter()
✖ dplyr::lag()    masks stats::lag()

Conceptual Framework

First, some notation. Let:

  • \(p_{ij}\) denote an indicator function for the presence absence values with value \(1\) for presence and \(0\) for absence of gene \(j\) in line \(i\),
  • \(y_i\) denote the yield of line \(i\),
  • \(n\) denote the number of lines and \(d\) denote the number of genes,
  • \(n_j = \sum_ip_{ij}\) denote the number of lines with presence of gene \(j\),
  • \(n_{j^*} = \sum_i(1 - p_{ij})\) denote the number of lines with absence of gene \(j\),
  • \(\bar{y} = \frac{1}{n}\sum_iy_i\) denote the mean yield,
  • \(\bar{y}_j = \frac{1}{n_j}\sum_ip_{ij}y_i\) denote the mean yield of lines with presence of gene \(j\), and
  • \(\bar{y}_{j^*} = \frac{1}{n_{j^*}}\sum_i(1 - p_{ij})y_i\) denote the mean yield of lines with absence of gene \(j\).

Note that \(\bar{y} = \frac{n_j}{n} \bar{y}_j + \frac{n_{j^*}}{n} \bar{y}_{j^*}\) and that \(n = n_j + n_{j^*}\) for all \(j\).

We can created coded yield data which here we will explore in more detail:

\[ x_{ij} = p_{ij}\bar{y}_j + (1 - p_{ij})\bar{y}_{j^*} \]

Note that \[\begin{align*} \frac{1}{n}\sum_ix_{ij} &= \frac{1}{n}\sum_i(p_{ij}\bar{y}_j + (1 - p_{ij})\bar{y}_{j^*}) \\ &= \frac{\bar{y}_j}{n}\sum_ip_{ij} + \frac{\bar{y}_{j^*}}{n}\sum_i(1 - p_{ij}) \\ &= \frac{n_j}{n} \bar{y}_j + \frac{n_{j^*}}{n} \bar{y}_{j^*} \\ &= \bar{y} \end{align*}\] for all \(j\).

Let \(X_j\) denote the column vector with entries \([x_{ij}]_i\). If we consider the yield data to be pre-centered to have \(\bar{y} = 0\) then the \(X_j\) will also be centered by construction for all \(j\). One important consequence of this centering is that \(\frac{n_j}{n} \bar{y}_j + \frac{n_{j^*}}{n} \bar{y}_{j^*} = 0\) and so \(\bar{y}_{j^*} = -\frac{n_j}{n_{j^*}} \bar{y}_j\). We will consider the \(y_i\) to be centered from here on. The variance of the \(X_j\) would then be proportional to \[\begin{align*} X_j^TX_j &= \sum_ix_{ij}^2 \\ &= \sum_i(p_{ij}\bar{y}_j + (1 - p_{ij})\bar{y}_{j^*})^2 \\ &= \sum_i\left(p_{ij}^2\bar{y}_j^2 + 2p_{ij}(1 - p_{ij})\bar{y}_j\bar{y}_{j^*} + (1 - p_{ij})^2\bar{y}_{j^*}^2\right) \\ &= \sum_ip_{ij}\bar{y}_j^2 + \sum_i(1 - p_{ij})\bar{y}_{j^*}^2 \\ &= n_j\bar{y}_j^2 + n_{j^*}\bar{y}_{j^*}^2 \\ &= n_j\bar{y}_j^2 + n_{j^*}(-\frac{n_j}{n_{j^*}} \bar{y}_j)^2 \\ &= n_j\bar{y}_j^2 (1 + \frac{n_j}{n_{j^*}}) \\ &= \frac{n_j}{n_{j^*}}\bar{y}_j^2 (n_{j^*} + n_j) \\ &= \frac{n_j}{n_{j^*}}n\bar{y}_j^2 \\ \end{align*}\] and \[\begin{align*} X_j^TX_k &= \sum_ix_{ij}x_{ik} \\ &= \sum_i(p_{ij}\bar{y}_j + (1 - p_{ij})\bar{y}_{j^*})(p_{ik}\bar{y}_k + (1 - p_{ik})\bar{y}_{k^*}) \\ &= \sum_i\left(p_{ij}p_{ik}\bar{y}_j\bar{y}_k + (1 - p_{ij})p_{ik}\bar{y}_{j^*}\bar{y}_k + p_{ij}(1 - p_{ik})\bar{y}_j\bar{y}_{k^*} + (1 - p_{ij})(1 - p_{ik})\bar{y}_{j^*}\bar{y}_{k^*}\right) \\ \end{align*}\]

These four terms represent the four outcomes in a two-by-two table, perhaps worth investigating further. Let:

  • \(n_{jk} = \sum_ip_{ij}p_{ik}\),
  • \(n_{{j^*}k} = \sum_i(1 - p_{ij})p_{ik}\),
  • \(n_{j{k^*}} = \sum_ip_{ij}(1 - p_{ik})\),
  • \(n_{{j^*}{k^*}} = \sum_i(1 - p_{ij})(1 - p_{ik})\),
  • \(\bar{y}_{jk} = \frac{1}{n_{jk}}\sum_ip_{ij}p_{ik}y_i\),
  • \(\bar{y}_{{j^*}k} = \frac{1}{n_{{j^*}k}}\sum_i(1 - p_{ij})p_{ik}y_i\),
  • \(\bar{y}_{j{k^*}} = \frac{1}{n_{j{k^*}}}\sum_ip_{ij}(1 - p_{ik})y_i\),
  • \(\bar{y}_{{j^*}{k^*}} = \frac{1}{n_{{j^*}{k^*}}}\sum_i(1 - p_{ij})(1 - p_{ik})y_i\).

These relate to the earlier quantities in the following ways

  • \(n_j = n_{jk} + n_{j{k^*}}\), \(n_{j^*} = n_{{j^*}k} + n_{{j^*}{k^*}}\), \(n_{k} = n_{jk} + n_{{j^*}k}\), \(n_{k^*} = n_{j{k^*}} + n_{{j^*}{k^*}}\),
  • \(\bar{y}_j = \frac{n_{jk}}{n_j}\bar{y}_{jk} + \frac{n_{j{k^*}}}{n_j}\bar{y}_{j{k^*}}\), \(\bar{y}_{j^*} = \frac{n_{{j^*}k}}{n_{j^*}}\bar{y}_{{j^*}k} + \frac{n_{{j^*}{k^*}}}{n_{j^*}}\bar{y}_{{j^*}{k^*}}\), \(\bar{y}_k = \frac{n_{jk}}{n_k}\bar{y}_{jk} + \frac{n_{{j^*}k}}{n_k}\bar{y}_{{j^*}k}\), \(\bar{y}_{k^*} = \frac{n_{j{k^*}}}{n_{k^*}}\bar{y}_{j{k^*}} + \frac{n_{{j^*}{k^*}}}{n_{k^*}}\bar{y}_{{j^*}{k^*}}\).

This two-way table notation allows us to write

\[\begin{align*} X_j^TX_k &= n_{jk}\bar{y}_j\bar{y}_k + n_{{j^*}k}\bar{y}_{j^*}\bar{y}_k + n_{j{k^*}}\bar{y}_j\bar{y}_{k^*} + n_{{j^*}{k^*}}\bar{y}_{j^*}\bar{y}_{k^*} \\ &= n_{jk}\bar{y}_j\bar{y}_k + n_{{j^*}k}(-\frac{n_j}{n_{j^*}} \bar{y}_j)\bar{y}_k + n_{j{k^*}}\bar{y}_j(-\frac{n_k}{n_{k^*}} \bar{y}_k) + n_{{j^*}{k^*}}(-\frac{n_j}{n_{j^*}} \bar{y}_j)(-\frac{n_k}{n_{k^*}} \bar{y}_k) \\ &= \bar{y}_j\bar{y}_k \left( n_{jk} - n_{{j^*}k}(\frac{n_j}{n_{j^*}}) - n_{j{k^*}}(\frac{n_k}{n_{k^*}}) + n_{{j^*}{k^*}}(\frac{n_j}{n_{j^*}})(\frac{n_k}{n_{k^*}}) \right) \\ \end{align*}\]

Notice that if \(\frac{n_j}{n_{j^*}} = \frac{n_{jk}}{n_{{j^*}k}}\) then \(X_j^TX = 0\). This corresponds to the two genes being independant in presence/absence, which results in a covariance of zero.

Let \(\eta\) be a \(d\)-dimensional unit vector i.e. with entries \(\eta_j \in \mathbb{R}\) such that \(\sum_j \eta_j^2 = 1\), and let \(\mathbb{X}\) be the data matrix with entries \(x_{ij}\), then consider \[\begin{align*} (\eta \mathbb{X})^T \eta \mathbb{X} &= \sum_i \left( \sum_j \eta_j x_{ij} \right)^2 \\ &= \sum_i \sum_j \sum_k \eta_j \eta_k x_{ij} x_{ik} \\ &= \sum_j \eta_j^2 \sum_i x_{ij}^2 + 2\sum_j \sum_{k < i} \eta_j \eta_k \sum_i x_{ij} x_{ik} \\ &= \sum_j \eta_j^2 X_j^TX_j + 2\sum_j \sum_{k < i} \eta_j \eta_k X_j^TX_k \\ \end{align*}\]

Read in and subset data

# Read tidy data saved at the of initial_data_organisation.Rmd
load(file.path('data', 'pav_data.RData'))

# Reduce to NBS genes
nbs_table = pav_table[, c(TRUE, names(pav_table)[-1] %in% nbs$Name)]

# Merge on Yield and reduce to lines for which we have yield data
meta.df = subset(meta.df, !is.na(Yield))
nbs_table = merge(meta.df[, c('Line', 'Yield')], nbs_table)

# Reduce to genes with some decent level of variation
nbs_table = nbs_table[, c(TRUE, TRUE, colMeans(nbs_table[, -(1:2)]) <= 0.98 & colMeans(nbs_table[, -(1:2)]) >= 0.02)]

# Simplify gene names slightly
names(nbs_table) = sub('00.1.p$', '', names(nbs_table))
names(nbs_table) = sub('^GlymaLee.', 'GL', names(nbs_table))
names(nbs_table) = sub('^UWASoyPan', 'UWA', names(nbs_table))

# Move Line information into row.names
row.names(nbs_table) = nbs_table$Line
nbs_table$Line = NULL

sessionInfo()
R version 3.6.3 (2020-02-29)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: Ubuntu 18.04.5 LTS

Matrix products: default
BLAS:   /usr/lib/x86_64-linux-gnu/openblas/libblas.so.3
LAPACK: /usr/lib/x86_64-linux-gnu/libopenblasp-r0.2.20.so

locale:
 [1] LC_CTYPE=en_AU.UTF-8       LC_NUMERIC=C              
 [3] LC_TIME=en_AU.UTF-8        LC_COLLATE=en_AU.UTF-8    
 [5] LC_MONETARY=en_AU.UTF-8    LC_MESSAGES=en_AU.UTF-8   
 [7] LC_PAPER=en_AU.UTF-8       LC_NAME=C                 
 [9] LC_ADDRESS=C               LC_TELEPHONE=C            
[11] LC_MEASUREMENT=en_AU.UTF-8 LC_IDENTIFICATION=C       

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] forcats_0.4.0   stringr_1.4.0   dplyr_0.8.3     purrr_0.3.2    
[5] readr_1.3.1     tidyr_1.0.0     tibble_2.1.3    ggplot2_3.2.1  
[9] tidyverse_1.2.1

loaded via a namespace (and not attached):
 [1] tidyselect_1.1.0 xfun_0.10        haven_2.3.1      lattice_0.20-41 
 [5] colorspace_1.4-1 vctrs_0.3.1      generics_0.0.2   htmltools_0.4.0 
 [9] yaml_2.2.0       rlang_0.4.6      later_1.0.0      pillar_1.4.2    
[13] withr_2.1.2      glue_1.3.1       modelr_0.1.5     readxl_1.3.1    
[17] lifecycle_0.1.0  munsell_0.5.0    gtable_0.3.0     workflowr_1.6.2 
[21] cellranger_1.1.0 rvest_0.3.4      evaluate_0.14    knitr_1.25      
[25] httpuv_1.5.2     broom_0.5.2      Rcpp_1.0.3       promises_1.1.0  
[29] backports_1.1.5  scales_1.0.0     jsonlite_1.6     fs_1.3.1        
[33] hms_0.5.1        digest_0.6.23    stringi_1.4.3    grid_3.6.3      
[37] rprojroot_1.3-2  cli_1.1.0        tools_3.6.3      magrittr_1.5    
[41] lazyeval_0.2.2   crayon_1.3.4     whisker_0.4      pkgconfig_2.0.3 
[45] xml2_1.2.2       lubridate_1.7.4  assertthat_0.2.1 rmarkdown_1.16  
[49] httr_1.4.1       rstudioapi_0.10  R6_2.4.0         nlme_3.1-149    
[53] git2r_0.26.1     compiler_3.6.3