library(omicade4)
## Loading required package: ade4
library(mogsa)
library(RSpectra)
library(lubridate)
## 
## Attaching package: 'lubridate'
## The following objects are masked from 'package:base':
## 
##     date, intersect, setdiff, union
library(glmnet)
## Loading required package: Matrix
## Loaded glmnet 4.1-7
library(dplyr)
## 
## Attaching package: 'dplyr'
## The following object is masked from 'package:mogsa':
## 
##     combine
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
library(cowplot)
## 
## Attaching package: 'cowplot'
## The following object is masked from 'package:lubridate':
## 
##     stamp
library(ggplot2)
setwd("/Users/rnili/Desktop/repo/gitLab/cmi-pb-multiomics-main/results/main/cmi_pb_datasets/processed/harmonized")

# Read in metadata
meta.2020<-read.table('clinical_metadata.2020.tsv',sep='\t',header=TRUE,stringsAsFactors=TRUE,row.names=1)
meta.2021<-read.table('clinical_metadata.2021.tsv',sep='\t',header=TRUE,stringsAsFactors=TRUE,row.names=1)

files from imputed data is already normalised so we don’t need to normalise them

# imputed_dir is path to local drive where data is saved
setwd("/Users/rnili/Desktop/repo/gitLab/cmi-pb-multiomics-main/results/main/cmi_pb_datasets/processed/imputed")


# Import imputed datasets
rnaseq_baseline_mat_imputed_20 <- read.csv('rnaseq_baseline_mat_imputed_20_051022.csv',row.names=1)
cytof_baseline_mat_imputed_20 <- read.csv('cytof_baseline_mat_imputed_20_051022.csv',row.names=1)
olink_baseline_mat_imputed_20 <- read.csv('olink_baseline_mat_imputed_20_051022.csv',row.names=1)
abtiters_baseline_mat_imputed_20 <- read.csv('abtiters_baseline_mat_imputed_20_051022.csv',row.names=1)

rnaseq_baseline_mat_imputed_21 <- read.csv('rnaseq_baseline_mat_imputed_21_051022.csv',row.names=1)
cytof_baseline_mat_imputed_21 <- read.csv('cytof_baseline_mat_imputed_21_051022.csv',row.names=1)
olink_baseline_mat_imputed_21 <- read.csv('olink_baseline_mat_imputed_21_051022.csv',row.names=1)
abtiters_baseline_mat_imputed_21 <- read.csv('abtiters_baseline_mat_imputed_21_051022.csv',row.names=1)
tasks_seq<-c('ENSG00000277632','ENSG00000136244','ENSG00000100906','ENSG00000229807')
names(rnaseq_baseline_mat_imputed_20[tasks_seq])
## [1] "ENSG00000277632" "ENSG00000136244" "ENSG00000100906" "ENSG00000229807"
distPlot <- function(col, df){
  p <- 
    ggplot(df) +
    aes_string(col)

  if(is.numeric(df[[col]])) {
    p <- p + geom_density()

  } else {
    p <- p + geom_bar()
  }
}

distPlots <- lapply(tasks_seq, distPlot, df=rnaseq_baseline_mat_imputed_20)
## Warning: `aes_string()` was deprecated in ggplot2 3.0.0.
## ℹ Please use tidy evaluation idioms with `aes()`.
## ℹ See also `vignette("ggplot2-in-packages")` for more information.
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
## generated.
plot_grid(plotlist = distPlots)

# Get age at boost
library(lubridate)
meta.2020$date_of_boost<-parse_date_time(meta.2020$date_of_boost,"ymd")
meta.2020$year_of_birth<-parse_date_time(meta.2020$year_of_birth,"ymd")
meta.2020$age_at_boost<- as.numeric(round(difftime(meta.2020$date_of_boost,
                                                    meta.2020$year_of_birth,units="weeks")/52,2))
meta.2021$date_of_boost<-parse_date_time(meta.2021$date_of_boost,"ymd")
meta.2021$year_of_birth<-parse_date_time(meta.2021$year_of_birth,"ymd")
meta.2021$age_at_boost<- as.numeric(round(difftime(meta.2021$date_of_boost,
                                                    meta.2021$year_of_birth,units="weeks")/52,2))

meta <- rbind(meta.2020[c("age_at_boost", "infancy_vac", "biological_sex")], meta.2021[c("age_at_boost", "infancy_vac", "biological_sex")])

meta$infancy_vac <- as.numeric(meta$infancy_vac)
meta$biological_sex <- as.numeric(meta$biological_sex)
colnames(meta)
## [1] "age_at_boost"   "infancy_vac"    "biological_sex"

All data from abtiter been log2 transformed. Why? to make them normally distributed since our glm uses gaussian as the distribution kernel?

# Get the y data
dataY = read.table('/Users/rnili/Desktop/repo/gitLab/cmi-pb-multiomics-main/results/yData_task_matrix.common_names.mfi_raw.tsv',sep='\t',header=TRUE,stringsAsFactors=TRUE,row.names=1)

dataY <- dataY[c("IgG.PT.day14", "ENSG00000277632.day3", "Monocytes.day1")]
colnames(dataY) <- c("IgG.PT.day14", "CCL3.day3", "Monocytes.day1")
# dataY$IgG.PT.day14 <- log2(dataY[,'IgG.PT.day14']+1)
# dataY$CCL3.day3 <- log2(dataY[,'CCL3.day3']+1)

dataY$subj_id <- rownames(dataY)
colnames(dataY)
## [1] "IgG.PT.day14"   "CCL3.day3"      "Monocytes.day1" "subj_id"
distPlot <- function(col, df){
  p <- 
    ggplot(df) +
    aes_string(col)

  if(is.numeric(df[[col]])) {
    p <- p + geom_density()

  } else {
    p <- p + geom_bar()
  }
}

typeof(dataY)
## [1] "list"
# 
distPlots <- lapply(c("IgG.PT.day14", "CCL3.day3", "Monocytes.day1"), distPlot, df=dataY)
plot_grid(plotlist = distPlots)
## Warning: Removed 4 rows containing non-finite values (`stat_density()`).
## Warning: Removed 22 rows containing non-finite values (`stat_density()`).
## Warning: Removed 41 rows containing non-finite values (`stat_density()`).

colnames(rnaseq_baseline_mat_imputed_20)[which(names(rnaseq_baseline_mat_imputed_20) == "ENSG00000277632")] <- "CCL3"
colnames(rnaseq_baseline_mat_imputed_21)[which(names(rnaseq_baseline_mat_imputed_21) == "ENSG00000277632")] <- "CCL3"

rnaDf <- rbind(rnaseq_baseline_mat_imputed_20["CCL3"], rnaseq_baseline_mat_imputed_21["CCL3"])

abtiterDf <- rbind(abtiters_baseline_mat_imputed_20["IgG.PT"], abtiters_baseline_mat_imputed_21["IgG.PT"])

cytofDf <- rbind(cytof_baseline_mat_imputed_20["Monocytes"], cytof_baseline_mat_imputed_21["Monocytes"])

dataDf1 <- merge(rnaDf, abtiterDf, by='row.names', all=T)
colnames(dataDf1)[1] <- "subj_id" 

dataDf2 <- merge(cytofDf, meta, by='row.names', all=T)
colnames(dataDf2)[1] <- "subj_id" 

dataX <- merge(dataDf1, dataDf2, by='subj_id', all=T)

dataDf <- merge(dataX, dataY, by='subj_id', all=T)
rownames(dataDf) <- dataDf$subj_id

dataDf
##    subj_id      CCL3     IgG.PT  Monocytes age_at_boost infancy_vac
## 1        1  5.333531 2.24397113  7.5260062        30.80           2
## 10      10  4.101398 0.04154169 13.4642547        34.68           2
## 11      11  4.542939 1.77698271 10.7905171        30.76           2
## 12      12        NA         NA         NA        34.68           2
## 13      13  3.399718 1.36806068  5.0304685        19.63           1
## 14      14        NA         NA         NA        23.70           2
## 15      15  4.835874 0.38100554 15.6373679        27.71           2
## 16      16        NA         NA         NA        29.66           2
## 17      17  4.512669 3.45553366 14.4574172        36.82           2
## 18      18  4.083384 1.68734290  0.9568001        19.73           1
## 19      19  4.459300 2.16297720 13.5202756        22.81           2
## 2        2        NA         NA         NA        51.25           2
## 20      20  3.963474 2.55022450 21.3991621        35.78           2
## 21      21  3.823138 2.77448207 15.2266453        33.77           2
## 22      22  4.462314 2.16393744 11.9355890        31.77           2
## 23      23  4.421223 0.40194862 11.7041207        25.82           2
## 24      24  3.106516 0.26992483 11.5201600        24.79           2
## 25      25  4.042732 0.74967184 13.4709985        28.80           2
## 26      26  4.735685 0.72685734  5.5320667        33.85           2
## 27      27  3.782618 0.77080873 13.8965682        19.80           1
## 28      28        NA         NA         NA        34.85           2
## 29      29  4.447249 0.99688351 16.0947648        19.80           1
## 3        3  4.599972 1.06789053 13.0245725        33.89           2
## 30      30        NA         NA         NA        28.84           2
## 31      31  4.693487 0.16545091  4.8790233        27.83           2
## 32      32  5.779549 1.40368966 13.5492381        19.88           1
## 33      33  5.311794 0.63824158  7.6518328        26.87           2
## 34      34        NA         NA         NA        33.93           2
## 35      35  5.434762 1.19302502 13.5420630        25.86           2
## 36      36  3.016496 1.04080942 11.2437986        19.88           1
## 37      37        NA         NA         NA        18.91           1
## 38      38  3.017922 0.77149215 13.7721006        19.88           1
## 39      39        NA         NA         NA        31.92           2
## 4        4  4.094574 1.60723380  5.4908380        28.76           2
## 40      40        NA         NA         NA        22.89           2
## 41      41        NA         NA         NA        31.96           2
## 42      42  5.230280 1.11654738 10.6095832        19.92           1
## 43      43  3.871351 1.00310977 13.6337391        18.91           1
## 44      44  4.518409 0.92865661 23.1032762        18.91           1
## 45      45        NA         NA         NA        19.98           1
## 46      46        NA         NA         NA        18.91           1
## 47      47  6.900831 2.65845646 10.1292397        20.98           1
## 48      48  6.731536 0.04154169 14.9597507        19.11           1
## 49      49        NA         NA         NA        20.11           1
## 5        5  4.916572 2.26243525 13.1258248        25.75           2
## 50      50  8.091139 0.50769248  9.3613078        19.98           1
## 51      51        NA         NA         NA        19.98           1
## 52      52  7.288949 1.97201547 28.5490352        19.07           1
## 53      53  9.867189 4.17711376 12.8294284        19.07           1
## 54      54        NA         NA         NA        20.11           1
## 55      55        NA         NA         NA        20.11           1
## 56      56        NA         NA         NA        20.15           1
## 57      57        NA         NA         NA        21.15           1
## 58      58        NA         NA         NA        20.15           1
## 59      59        NA         NA         NA        20.15           1
## 6        6  4.575554 0.27896948 22.7930283        28.87           2
## 60      60        NA         NA         NA        20.15           1
## 61      61 12.042340 1.00000000 12.8005992        32.38           2
## 62      62  6.937333 1.07602222 13.5692650        25.99           2
## 63      63  6.691953 1.79570532 15.3000000        23.98           2
## 64      64  6.760926 2.04705167 12.9000000        25.99           2
## 65      65  7.605079 0.80140188 15.8000000        29.02           2
## 66      66  5.042425 1.32512343 15.8000000        43.07           2
## 67      67  5.889230 0.70618673 34.6000000        47.24           2
## 68      68  5.853946 1.05459788 13.5000000        47.24           2
## 69      69  5.528321 1.07297696 13.1000000        29.17           2
## 7        7        NA         NA         NA        35.97           2
## 70      70  5.815166 0.47027882 32.3000000        21.15           1
## 71      71  5.987707 2.90659418 12.2000000        21.15           1
## 72      72  5.880881 2.08840338 19.9000000        28.25           2
## 73      73  4.529196 1.88327381 15.7000000        24.23           2
## 74      74  3.871745 0.52016941 36.3000000        24.23           2
## 75      75  5.678804 0.17736981 14.4559889        21.22           1
## 76      76  4.663800 3.53643892 18.0000000        21.22           1
## 77      77  8.217396 0.69081102 28.8000000        31.32           2
## 78      78  7.151839 0.99519368 29.0000000        26.30           2
## 79      79  6.969300 2.32256773 41.5000000        32.32           2
## 8        8        NA         NA         NA        34.27           2
## 80      80  7.178107 0.46841860 21.9000000        27.30           2
## 81      81 10.726589 1.81601157 34.9000000        26.30           2
## 82      82  9.413611 0.76723104 22.5000000        21.28           1
## 83      83  8.588209 1.26356779 32.4000000        20.34           1
## 84      84  7.593122 0.28053529 18.9000000        22.34           1
## 85      85  4.937862 0.33276350 19.7000000        19.39           1
## 86      86  5.362224 0.30403242 23.5000000        21.40           1
## 87      87  5.295650 1.07997538 22.9000000        19.39           1
## 88      88  5.567850 1.04544297 17.6000000        19.39           1
## 89      89  5.130601 0.06807137 15.7000000        22.49           1
## 9        9  4.238481 0.28783862  5.7229578        20.63           1
## 90      90  4.837136 0.33985853 20.6000000        20.49           1
## 91      91  4.140370 0.92749667 25.4000000        21.49           1
## 92      92  4.623047 0.23829881 40.6000000        19.54           1
## 93      93  4.616769 1.57747907 16.3000000        23.56           1
## 94      94  4.785446 2.65824315 26.2000000        20.55           1
## 95      95  4.878235 2.29792327 17.3000000        21.55           1
## 96      96  4.356355 0.76421696 34.2000000        19.54           1
##    biological_sex IgG.PT.day14 CCL3.day3 Monocytes.day1
## 1               1   199.517666       369             NA
## 10              1     3.821589       179             NA
## 11              1   414.513947       144       7.257095
## 12              2    96.874274        NA             NA
## 13              2    58.061932       911             NA
## 14              2   269.001265        NA             NA
## 15              2   100.489455       277      10.585489
## 16              1    86.675397        NA             NA
## 17              1   168.900882       295      16.401488
## 18              1   182.906088        99             NA
## 19              2   170.183735       133             NA
## 2               1           NA        NA             NA
## 20              1   133.269594       480      26.605583
## 21              2    35.368851       238      34.812168
## 22              1    89.989653        82             NA
## 23              1   132.219593       133             NA
## 24              1    23.631765       150             NA
## 25              1    23.099432       188             NA
## 26              1    45.748957       148      16.108508
## 27              1    43.959688       192             NA
## 28              2   116.478279        NA             NA
## 29              2    75.090769       284      25.083209
## 3               1   129.197956       236             NA
## 30              1    62.699730        NA             NA
## 31              1    33.003075       476       8.545243
## 32              2   272.717237       653             NA
## 33              2    13.370949       749      17.703064
## 34              1   172.314446        NA             NA
## 35              2    77.340488       218             NA
## 36              1    41.400275       191      17.446750
## 37              1           NA        NA             NA
## 38              1   121.163359        62             NA
## 39              1    71.881127        NA             NA
## 4               2   144.885339       133       7.211965
## 40              1   145.035713        NA             NA
## 41              2   198.590973        NA             NA
## 42              1   162.631291       504             NA
## 43              1    26.508813       318             NA
## 44              1   293.880891       147      35.241054
## 45              1    38.499070        NA      13.545087
## 46              1   201.239119        NA      30.018793
## 47              1   280.892693      9238       8.663056
## 48              1     0.536000      3997      18.252658
## 49              1   139.999552        NA      10.347126
## 5               2    97.743258       187             NA
## 50              1    52.882062      8629             NA
## 51              2    73.573545        NA             NA
## 52              2   109.997984     10181      23.512649
## 53              1   219.279332      6593             NA
## 54              1   172.363131        NA             NA
## 55              1   120.652266        NA      15.334381
## 56              1   202.521120        NA             NA
## 57              1    83.003313        NA             NA
## 58              1   223.309447        NA             NA
## 59              1   352.958671        NA             NA
## 6               1   167.496355       216      41.380502
## 60              2    21.183724        NA             NA
## 61              1   304.000000       420             NA
## 62              1  1548.451277      1532             NA
## 63              1  1916.701277       615      18.700000
## 64              2   558.451277       644      13.800000
## 65              2  1837.750000      2353      20.400000
## 66              1  2059.500000       989      13.900000
## 67              1  2128.450062       453      31.100000
## 68              2  1565.447441       324      15.500000
## 69              1  1150.200062       792      18.900000
## 7               1    92.687631        NA             NA
## 70              2   201.201277       360      46.100000
## 71              1   916.951277       942      18.200000
## 72              1  1736.250000       302      27.500000
## 73              1  1349.697441       420      23.400000
## 74              1   385.697441       207      50.000000
## 75              1   136.250000       242             NA
## 76              1  1247.000000       205      22.300000
## 77              2   506.500000      1928      31.000000
## 78              1   831.500000       312      35.700000
## 79              2  1019.250000      2348      52.600000
## 8               1           NA        NA             NA
## 80              1   248.500000      3592      26.400000
## 81              2   516.750000      2426      35.600000
## 82              1           NA      8124      20.500000
## 83              1   436.450062       130      27.700000
## 84              1   474.951277       595      25.100000
## 85              1   168.750000       356      18.700000
## 86              1   615.200062       571      25.200000
## 87              2           NA      1068      22.500000
## 88              2           NA      4288      20.200000
## 89              1   133.947441       664      16.600000
## 9               2    17.409695       334             NA
## 90              1   221.950062       439      21.600000
## 91              2   487.447441       175      40.400000
## 92              1   377.750000       275      33.000000
## 93              1   645.250000       291      15.000000
## 94              2   993.750000       140      39.500000
## 95              1  1078.697441       210      23.500000
## 96              2   386.000000       177      35.000000

###Distination plot

distPlot <- function(col, df){
  p <- 
    ggplot(df) +
    aes_string(col)

  if(is.numeric(df[[col]])) {
    p <- p + geom_density()

  } else {
    p <- p + geom_bar()
  }
}


names(dataDf)[2:length(dataDf)]
## [1] "CCL3"           "IgG.PT"         "Monocytes"      "age_at_boost"  
## [5] "infancy_vac"    "biological_sex" "IgG.PT.day14"   "CCL3.day3"     
## [9] "Monocytes.day1"
distPlots <- lapply(names(dataDf)[2:length(dataDf)], distPlot, df=dataDf)
plot_grid(plotlist = distPlots)
## Warning: Removed 24 rows containing non-finite values (`stat_density()`).
## Removed 24 rows containing non-finite values (`stat_density()`).
## Removed 24 rows containing non-finite values (`stat_density()`).
## Warning: Removed 6 rows containing non-finite values (`stat_density()`).
## Warning: Removed 24 rows containing non-finite values (`stat_density()`).
## Warning: Removed 43 rows containing non-finite values (`stat_density()`).

Test model quality

options(warn=-1)
xCols = c("CCL3", "IgG.PT", "Monocytes", "age_at_boost", "infancy_vac", "biological_sex")
yCols = c("IgG.PT.day14", "CCL3.day3", "Monocytes.day1")

pred_cor <- data.frame(matrix(nrow=length(yCols), ncol=3))
rownames(pred_cor) <- yCols
colnames(pred_cor) <- c('pearson.cor.pred.true', 'spearman.cor.pred.true', 'ranked.spearman.cor.pred.true')

rownames(dataDf) <- attr(dataDf, "row.names")
print(typeof(row.names(dataDf)))
## [1] "character"
# 
for (i in 1:length(yCols)){
  all_preds <- c()
  all_true <- c()
  set.seed(1)
  
  filteredY <- na.omit(dataDf[yCols[i]])
  filteredX <- na.omit(dataDf[xCols])
  
  row_int <- intersect(rownames(filteredY), rownames(filteredX))
  
  for (j in 1:length(row_int)){
    train <- row_int[-c(j)]
    xData <- filteredX[train, xCols]
    yData <- filteredY[train,]
    
    a1= nrow(xData[train,])
    a2= nrow(xData[train,]-1)
    
    allidx = row_int
    predidx = setdiff(allidx, train)
    
    # create lasso model
    cvfit_out <- cv.glmnet(x=as.matrix(xData), yData, family='gaussian',
                         alpha=1, nfolds=nrow(xData[train,]))

    preds <- predict(cvfit_out, newx = as.matrix(data.frame(filteredX[predidx,])), s='lambda.min')

    all_preds <- c(all_preds, preds)
    all_true<- c(all_true, filteredY[predidx, yCols[i]])
  }
  
  b = data_frame(all_preds, rank(all_preds,na.last="keep",ties.method="min"), all_true, rank(all_true,na.last="keep",ties.method="min"))

  pred_cor[yCols[i],'pearson.cor.pred.true'] <- cor(all_preds,all_true)
  pred_cor[yCols[i],'spearman.cor.pred.true'] <- cor(all_preds,all_true, method="spearman")
  pred_cor[yCols[i],'ranked.spearman.cor.pred.true'] <- cor(rank(all_preds,na.last="keep",ties.method="min"),rank(all_true,na.last="keep",ties.method="min"), method="spearman")
}

pred_cor
##                pearson.cor.pred.true spearman.cor.pred.true
## IgG.PT.day14               0.2878062              0.4298137
## CCL3.day3                  0.4554237              0.6179585
## Monocytes.day1             0.8026556              0.8240516
##                ranked.spearman.cor.pred.true
## IgG.PT.day14                       0.4298137
## CCL3.day3                          0.6179585
## Monocytes.day1                     0.8240516
typeof(pred_cor)
## [1] "list"

For each model, can assess which features contribute to non-zero coefficients

Consider only choosing models for follow-on analysis that show good correlation scores

size <- length(yCols)
all_models_coef<-vector(mode='list',length=size)
all_models_names<-vector(mode='list',length=size)
all_models<-vector(mode='list',length=size)

for (i in 1:length(yCols)){
  set.seed(1)
  filteredY <- na.omit(dataDf[yCols[i]])
  filteredX <- na.omit(dataDf[xCols])
  
  row_int <- intersect(rownames(filteredY), rownames(filteredX))
  
  # create lasso model
  suppressWarnings(cvfit_out <- cv.glmnet(x=as.matrix(filteredX[row_int,]), as.matrix(filteredY[row_int,]), family='gaussian', alpha=1, nfolds=nrow(filteredX[row_int,])))
  plot(cvfit_out)
  all_models_coef[i]=list(coef(cvfit_out, s = 'lambda.min')[coef(cvfit_out, s = 'lambda.min')[,1]!= 0])
  all_models_names[i]=list(rownames(coef(cvfit_out, s = 'lambda.min'))[coef(cvfit_out, s = 'lambda.min')[,1]!= 0])
}
## <sparse>[ <logic> ]: .M.sub.i.logical() maybe inefficient

## <sparse>[ <logic> ]: .M.sub.i.logical() maybe inefficient

## <sparse>[ <logic> ]: .M.sub.i.logical() maybe inefficient

names(all_models_coef) <- yCols
names(all_models_names) <- yCols
for (i in 1:size){
  all_models[[i]] = data.frame(cbind(all_models_names[[i]],all_models_coef[[i]]))
  colnames(all_models[[i]])<-c("Variable","Coefficient")
  all_models[[i]]$Coefficient<-as.numeric(all_models[[i]]$Coefficient)
  all_models[[i]]$Coefficient=round(all_models[[i]]$Coefficient,3)
  all_models[[i]]<-all_models[[i]] %>% arrange(desc(abs(Coefficient)))
}
names(all_models)<-yCols

all_models
## $IgG.PT.day14
##         Variable Coefficient
## 1    (Intercept)    -672.811
## 2         IgG.PT      90.869
## 3 biological_sex     -79.601
## 4           CCL3      33.668
## 5   age_at_boost      27.876
## 6      Monocytes      13.367
## 
## $CCL3.day3
##       Variable Coefficient
## 1  (Intercept)    -713.729
## 2  infancy_vac    -691.341
## 3         CCL3     534.168
## 4       IgG.PT      10.866
## 5 age_at_boost      -0.127
## 
## $Monocytes.day1
##         Variable Coefficient
## 1    (Intercept)       7.845
## 2    infancy_vac       4.884
## 3 biological_sex       1.847
## 4           CCL3      -1.499
## 5      Monocytes       1.068
## 6         IgG.PT       0.509
## 7   age_at_boost      -0.273
# library(capture)
setwd("/Users/rnili/Desktop/repo/gitLab/cmi-pb-multiomics-main/rasteh/models")
a= append(list(pred_cor),all_models)
names(a)[1] <- 'pred_cor'
sink("allModels_predCor_base_notNormalised.txt")
print(a)
## $pred_cor
##                pearson.cor.pred.true spearman.cor.pred.true
## IgG.PT.day14               0.2878062              0.4298137
## CCL3.day3                  0.4554237              0.6179585
## Monocytes.day1             0.8026556              0.8240516
##                ranked.spearman.cor.pred.true
## IgG.PT.day14                       0.4298137
## CCL3.day3                          0.6179585
## Monocytes.day1                     0.8240516
## 
## $IgG.PT.day14
##         Variable Coefficient
## 1    (Intercept)    -672.811
## 2         IgG.PT      90.869
## 3 biological_sex     -79.601
## 4           CCL3      33.668
## 5   age_at_boost      27.876
## 6      Monocytes      13.367
## 
## $CCL3.day3
##       Variable Coefficient
## 1  (Intercept)    -713.729
## 2  infancy_vac    -691.341
## 3         CCL3     534.168
## 4       IgG.PT      10.866
## 5 age_at_boost      -0.127
## 
## $Monocytes.day1
##         Variable Coefficient
## 1    (Intercept)       7.845
## 2    infancy_vac       4.884
## 3 biological_sex       1.847
## 4           CCL3      -1.499
## 5      Monocytes       1.068
## 6         IgG.PT       0.509
## 7   age_at_boost      -0.273
sink()
# lapply(append(pred_cor,all_models), write, "allModels_predCor_base.txt", append=TRUE, ncolumns=1000)
# saveRDS(all_models, file="base_allModels.RData")
# load(file="base_allModels.RData")

Note that the echo = FALSE parameter was added to the code chunk to prevent printing of the R code that generated the plot.