Load packages + functions

suppressMessages(silent <- lapply(
    c("sandwich", "lmtest", "tidyverse", "MatchIt", "forestplot", "kableExtra"),
    library, character.only=T))
table = function (..., useNA = 'always') base::table(..., useNA = useNA)

Load phenotype file

tab=as_tibble(read.csv("../VegMarkdown/pheno/BioxVeg_pheno_09112022.csv"))
colnames(tab)%>%kbl()%>% 
 scroll_box(width = "200px", height = "200px")
x
FID
IID
Age
Sex
Townsend
Geno_batch
AlcoholFreq
Alanine_aminotransferase
Albumin
Alkaline_phosphatase
Apolipoprotein_A
Apolipoprotein_B
Aspartate_aminotransferase
C_reactive_protein
Calcium
Cholesterol
Creatinine
Cystatin_C
Direct_bilirubin
Gamma_glutamyltransferase
Glucose
HbA1c
HDL_cholesterol
IGF_1
LDL_direct
Lipoprotein_A
Oestradiol
Phosphate
Rheumatoid_factor
SHBG
Testosterone
Total_bilirubin
Total_protein
Triglycerides
Urate
Urea
Vitamin_D
PCA1
PCA2
PCA3
PCA4
PCA5
PCA6
PCA7
PCA8
PCA9
PCA10
PCA11
PCA12
PCA13
PCA14
PCA15
PCA16
PCA17
PCA18
PCA19
PCA20
BMI
SmokeStatus
Veg5yr1
Age2
PreviousSmoker
CurrentSmoker
center1
center2
center3
center4
center5
center6
center7
center8
center9
center10
center11
center12
center13
center14
center15
center16
center17
center18
center19
center20
center21
center22
zblood
zTownsend
zAge
logSHBG
Scr
eGFR
bioavailableTest
freeTest
statins
Cholesterolold
LDL_directold
Apolipoprotein_Bold
tookMed
tookSSRI
Alanine_aminotransferase_rint
Albumin_rint
Alkaline_phosphatase_rint
Apolipoprotein_A_rint
Apolipoprotein_B_rint
Aspartate_aminotransferase_rint
C_reactive_protein_rint
Calcium_rint
Cholesterol_rint
Creatinine_rint
Cystatin_C_rint
Direct_bilirubin_rint
Gamma_glutamyltransferase_rint
Glucose_rint
HbA1c_rint
HDL_cholesterol_rint
IGF_1_rint
LDL_direct_rint
Lipoprotein_A_rint
Oestradiol_rint
Phosphate_rint
Rheumatoid_factor_rint
SHBG_rint
Testosterone_rint
Total_bilirubin_rint
Total_protein_rint
Triglycerides_rint
Urate_rint
Urea_rint
Vitamin_D_rint
eGFR_rint
bioavailableTest_rint
freeTest_rint
tab2=tab%>%select(IID, Veg5yr1 ,Age , Sex , BMI,
                 PCA1:PCA10,
                 AlcoholFreq,
                 PreviousSmoker, CurrentSmoker,
                 zTownsend)

tab3=tab2[complete.cases(tab2),] #lose only 16 vegetarians from 2328 to 2312
table(tab3$Veg5yr1)%>%kbl()
Var1 Freq
0 151998
1 2312
NA 0

1:4 matching without replacement

match.nearest=matchit(Veg5yr1 ~ Age + Sex + BMI +
                          AlcoholFreq +PreviousSmoker+
                          CurrentSmoker + zTownsend+
                          PCA1 + PCA2 + PCA3 +
                          PCA4 + PCA5 ,
                      data=tab3, distance="glm",
                      method="nearest",
                      replace=FALSE,
                      ratio=4)
#Love plot
plot(summary(match.nearest))

summary(match.nearest)
## 
## Call:
## matchit(formula = Veg5yr1 ~ Age + Sex + BMI + AlcoholFreq + PreviousSmoker + 
##     CurrentSmoker + zTownsend + PCA1 + PCA2 + PCA3 + PCA4 + PCA5, 
##     data = tab3, method = "nearest", distance = "glm", replace = FALSE, 
##     ratio = 4)
## 
## Summary of Balance for All Data:
##                Means Treated Means Control Std. Mean Diff. Var. Ratio eCDF Mean
## distance              0.0241        0.0148          0.5500     2.1203    0.2010
## Age                  52.6691       56.4053         -0.4819     0.9721    0.1168
## Sex                   1.6600        1.5415          0.2502     0.9042    0.0593
## BMI                  24.8177       26.9676         -0.5067     0.8378    0.1099
## AlcoholFreq           0.4416        0.4992         -0.1159          .    0.0576
## PreviousSmoker        0.3387        0.3595         -0.0441          .    0.0209
## CurrentSmoker         0.0662        0.0761         -0.0398          .    0.0099
## zTownsend             0.2204       -0.0044          0.2107     1.1439    0.0632
## PCA1                -12.1063      -12.1118          0.0026     1.0017    0.0029
## PCA2                  3.6085        3.6753         -0.0361     1.1100    0.0094
## PCA3                 -1.4092       -1.4400          0.0164     1.1058    0.0058
## PCA4                  0.6801        0.8421         -0.0421     1.1349    0.0095
## PCA5                 -0.9769       -1.0674          0.0140     0.9523    0.0098
##                eCDF Max
## distance         0.3038
## Age              0.1935
## Sex              0.1185
## BMI              0.2201
## AlcoholFreq      0.0576
## PreviousSmoker   0.0209
## CurrentSmoker    0.0099
## zTownsend        0.1079
## PCA1             0.0131
## PCA2             0.0184
## PCA3             0.0172
## PCA4             0.0219
## PCA5             0.0297
## 
## 
## Summary of Balance for Matched Data:
##                Means Treated Means Control Std. Mean Diff. Var. Ratio eCDF Mean
## distance              0.0241        0.0241          0.0018     1.0216    0.0000
## Age                  52.6691       52.5763          0.0120     0.9406    0.0137
## Sex                   1.6600        1.6581          0.0041     0.9976    0.0010
## BMI                  24.8177       24.8616         -0.0104     1.2414    0.0123
## AlcoholFreq           0.4416        0.4513         -0.0196          .    0.0097
## PreviousSmoker        0.3387        0.3329          0.0121          .    0.0057
## CurrentSmoker         0.0662        0.0650          0.0048          .    0.0012
## zTownsend             0.2204        0.2329         -0.0117     0.9396    0.0078
## PCA1                -12.1063      -12.1356          0.0139     0.9735    0.0073
## PCA2                  3.6085        3.6183         -0.0053     1.0615    0.0025
## PCA3                 -1.4092       -1.3949         -0.0076     1.0434    0.0039
## PCA4                  0.6801        0.6932         -0.0034     0.9681    0.0040
## PCA5                 -0.9769       -0.8442         -0.0205     0.9282    0.0081
##                eCDF Max Std. Pair Dist.
## distance         0.0013          0.0022
## Age              0.0308          0.8675
## Sex              0.0019          0.8408
## BMI              0.0300          0.7173
## AlcoholFreq      0.0097          0.9729
## PreviousSmoker   0.0057          0.9402
## CurrentSmoker    0.0012          0.4972
## zTownsend        0.0230          1.0571
## PCA1             0.0252          1.0133
## PCA2             0.0091          0.9927
## PCA3             0.0146          1.0668
## PCA4             0.0123          1.0203
## PCA5             0.0198          1.0846
## 
## Sample Sizes:
##           Control Treated
## All        151998    2312
## Matched      9248    2312
## Unmatched  142750       0
## Discarded       0       0

Join matched participants back with full phenotype table.

md=match.data(match.nearest)

tab4=tab%>%select(IID, Alanine_aminotransferase_rint:freeTest_rint)%>%select(-c(Glucose_rint,Oestradiol_rint, Rheumatoid_factor_rint))
md=left_join(md, tab4)
## Joining, by = "IID"
md=as_tibble(md)
dim(md)
## [1] 11560    52

Run glm with same covariates as matching.

https://kosukeimai.github.io/MatchIt/articles/estimating-effects.html

Full cohort

Initialize for lm

#Fix shorthand names for derived variables from pheno step
md=md%>%rename("Bioavailable_testosterone_rint"="bioavailableTest_rint",
               "Free_testosterone_rint"="freeTest_rint")


phe=md%>%select(Alanine_aminotransferase_rint:Free_testosterone_rint)%>%colnames()

#Arrange vars for plots by category
phe=c("Apolipoprotein_A_rint",
"Apolipoprotein_B_rint",
"HDL_cholesterol_rint","LDL_direct_rint",
"Lipoprotein_A_rint","Cholesterol_rint",
"Triglycerides_rint",

"IGF_1_rint","Vitamin_D_rint",
"Bioavailable_testosterone_rint","Free_testosterone_rint",
"Testosterone_rint","SHBG_rint",

"Alanine_aminotransferase_rint","Albumin_rint",
"Alkaline_phosphatase_rint","Aspartate_aminotransferase_rint",
"C_reactive_protein_rint",

"Gamma_glutamyltransferase_rint",
"Direct_bilirubin_rint","Total_bilirubin_rint","Total_protein_rint",

"Creatinine_rint",
"Cystatin_C_rint","eGFR_rint",
"HbA1c_rint",
"Urate_rint","Urea_rint",

"Calcium_rint",
"Phosphate_rint")

Category=c("Blood lipids", rep("",6),
           "Hormones", rep("",5),
           "Liver markers",rep("",8),
           "Kidney markers",rep("",5),
           "Minerals", "")

phe%>%kbl()%>% 
 scroll_box(width = "200px", height = "200px")
x
Apolipoprotein_A_rint
Apolipoprotein_B_rint
HDL_cholesterol_rint
LDL_direct_rint
Lipoprotein_A_rint
Cholesterol_rint
Triglycerides_rint
IGF_1_rint
Vitamin_D_rint
Bioavailable_testosterone_rint
Free_testosterone_rint
Testosterone_rint
SHBG_rint
Alanine_aminotransferase_rint
Albumin_rint
Alkaline_phosphatase_rint
Aspartate_aminotransferase_rint
C_reactive_protein_rint
Gamma_glutamyltransferase_rint
Direct_bilirubin_rint
Total_bilirubin_rint
Total_protein_rint
Creatinine_rint
Cystatin_C_rint
eGFR_rint
HbA1c_rint
Urate_rint
Urea_rint
Calcium_rint
Phosphate_rint
#initialize
fit=list()
fitBMI=list()
out=matrix(NA, nrow=length(phe), ncol=4)
colnames(out)=c("Trait", "Estimate", "SE", "P")
outBMI=out

Run lm full cohort, no adj-BMI

#----FULL COHORT, NO BMI--------------------------------------------
for (i in 1:length(phe)){
    fit=lm(md[[phe[i]]] ~ Veg5yr1 + Age + Sex + AlcoholFreq +
               PreviousSmoker +CurrentSmoker+ 
               zTownsend+PCA1 + PCA2 + PCA3 +
               PCA4 + PCA5, data = md, na.action = na.exclude)
    
    out[i,1]=phe[i]
    #Cluster-robust standard errors
    co=coeftest(fit, vcov. = vcovCL, cluster = ~subclass)["Veg5yr1",,drop=FALSE]
    out[i,2]=co[1]
    out[i,3]=co[2]
    out[i,4]=co[4]
}


out=as.data.frame(out)
out[2:4]=sapply(out[2:4], as.numeric)
out%>%kbl()%>% 
 scroll_box(width = "600px", height = "200px")
Trait Estimate SE P
Apolipoprotein_A_rint -0.0678330 0.0207699 0.0010948
Apolipoprotein_B_rint -0.1817707 0.0225360 0.0000000
HDL_cholesterol_rint -0.1434914 0.0203234 0.0000000
LDL_direct_rint -0.1813523 0.0224368 0.0000000
Lipoprotein_A_rint -0.0624893 0.0259647 0.0161178
Cholesterol_rint -0.1917923 0.0224022 0.0000000
Triglycerides_rint 0.2139828 0.0221433 0.0000000
IGF_1_rint -0.2074448 0.0224744 0.0000000
Vitamin_D_rint -0.3838228 0.0262896 0.0000000
Bioavailable_testosterone_rint -0.0272579 0.0163224 0.0949612
Free_testosterone_rint -0.0198628 0.0160244 0.2151820
Testosterone_rint -0.0240502 0.0152004 0.1136363
SHBG_rint -0.0116386 0.0220862 0.5982308
Alanine_aminotransferase_rint -0.1505136 0.0216690 0.0000000
Albumin_rint -0.0671204 0.0242745 0.0057015
Alkaline_phosphatase_rint 0.1340596 0.0225922 0.0000000
Aspartate_aminotransferase_rint 0.0341174 0.0221578 0.1236501
C_reactive_protein_rint -0.0551381 0.0234842 0.0188979
Gamma_glutamyltransferase_rint -0.1553539 0.0216983 0.0000000
Direct_bilirubin_rint -0.0115609 0.0248624 0.6419465
Total_bilirubin_rint -0.0249462 0.0225235 0.2680742
Total_protein_rint -0.0705408 0.0247022 0.0043036
Creatinine_rint -0.4428694 0.0176538 0.0000000
Cystatin_C_rint 0.3319466 0.0201453 0.0000000
eGFR_rint -0.0208691 0.0199177 0.2947694
HbA1c_rint 0.0120977 0.0211105 0.5666120
Urate_rint -0.1709024 0.0184686 0.0000000
Urea_rint -0.6144779 0.0230717 0.0000000
Calcium_rint -0.0771083 0.0253304 0.0023397
Phosphate_rint 0.0131200 0.0252895 0.6039165

Add confidence intervals and round long decimals:

#CI range
out$upper=out$Estimate+1.96*out$SE
out$lower=out$Estimate-1.96*out$SE
#Round digits
out$Estimateround=format(round(out$Estimate, digits=3), digits=3)
out$SEround=format(round(out$SE, digits=3), digits=3)
out$Pround=ifelse(out$P < 0.001, 
                  formatC(out$P, digits=1, format="e"), 
                  formatC(out$P, digits=3, format="f"))

Same exact thing as above but with adj-BMI

for (i in 1:length(phe)){
fit=lm(md[[phe[i]]] ~ Veg5yr1 + Age + Sex + BMI + AlcoholFreq +
                PreviousSmoker +CurrentSmoker+ 
                zTownsend+PCA1 + PCA2 + PCA3 +
               PCA4 + PCA5, data = md, na.action = na.exclude)

    outBMI[i,1]=phe[i]
#Cluster-robust standard errors
co=coeftest(fit, vcov. = vcovCL, cluster = ~subclass)["Veg5yr1",,drop=FALSE]
    outBMI[i,2]=co[1]
    outBMI[i,3]=co[2]
    outBMI[i,4]=co[4]
}

outBMI=as.data.frame(outBMI)
outBMI[2:4]=sapply(outBMI[2:4], as.numeric)
#as_tibble(outBMI)
#95CI
outBMI$upper=outBMI$Estimate+1.96*outBMI$SE
outBMI$lower=outBMI$Estimate-1.96*outBMI$SE
#Round digits
outBMI$Estimateround=format(round(outBMI$Estimate, digits=3), digits=3)
outBMI$SEround=format(round(outBMI$SE, digits=3), digits=3)
outBMI$Pround=ifelse(outBMI$P < 0.001, 
            formatC(outBMI$P, digits=1, format="e"), 
            formatC(outBMI$P, digits=3, format="f"))

Get sample sizes

ntot=sapply(md[phe], function(x) sum(!is.na(x)))
ncontr=md%>%filter(Veg5yr1==0)%>%select(all_of(phe))%>%sapply(function(x) sum(!is.na(x)))
ncase=md%>%filter(Veg5yr1==1)%>%select(all_of(phe))%>%sapply(function(x) sum(!is.na(x)))

Make Forest plots

https://cran.r-project.org/web/packages/forestplot/vignettes/forestplot.html

Forest plot: Full cohort, no BMI

# FULL, NO BMI --------------------------------------------------
xtik=c(-0.7,-0.6,-0.5,-0.4,-0.3,-0.2,-0.1,
       0,0.1,0.2,0.3,0.4,0.5)

out$Trait=gsub(out$Trait, pattern = "_", replacement = " ")
out$Trait=gsub(out$Trait, pattern = " rint", replacement="")

out$samp=paste(ncase,ncontr, sep= "/")
out=out%>%select(Trait, Estimateround, Pround, samp,everything())

out=cbind(Category, out)

#Add header
header=tibble(Category="Category",
              Trait="Biomarker",
              Estimateround= "β Veg.",
              Pround="P",
              samp="N (Veg./Non-veg.)")

out2=bind_rows(header, out)
out2%>%kbl()%>% 
 scroll_box(width = "600px", height = "200px")
Category Trait Estimateround Pround samp Estimate SE P upper lower SEround
Category Biomarker β Veg. P N (Veg./Non-veg.) NA NA NA NA NA NA
Blood lipids Apolipoprotein A -0.068 0.001 2011/8021 -0.0678330 0.0207699 0.0010948 -0.0271239 -0.1085421 0.021
Apolipoprotein B -0.182 8.0e-16 2197/8786 -0.1817707 0.0225360 0.0000000 -0.1376001 -0.2259412 0.023
HDL cholesterol -0.143 1.8e-12 2026/8075 -0.1434914 0.0203234 0.0000000 -0.1036575 -0.1833253 0.020
LDL direct -0.181 7.0e-16 2212/8808 -0.1813523 0.0224368 0.0000000 -0.1373761 -0.2253284 0.022
Lipoprotein A -0.062 0.016 1774/7068 -0.0624893 0.0259647 0.0161178 -0.0115984 -0.1133801 0.026
Cholesterol -0.192 1.3e-17 2215/8826 -0.1917923 0.0224022 0.0000000 -0.1478840 -0.2357007 0.022
Triglycerides 0.214 5.3e-22 2212/8819 0.2139828 0.0221433 0.0000000 0.2573838 0.1705818 0.022
Hormones IGF 1 -0.207 3.2e-20 2201/8785 -0.2074448 0.0224744 0.0000000 -0.1633950 -0.2514947 0.022
Vitamin D -0.384 8.2e-48 2130/8521 -0.3838228 0.0262896 0.0000000 -0.3322952 -0.4353504 0.026
Bioavailable testosterone -0.027 0.095 1743/6981 -0.0272579 0.0163224 0.0949612 0.0047340 -0.0592498 0.016
Free testosterone -0.020 0.215 1784/7220 -0.0198628 0.0160244 0.2151820 0.0115451 -0.0512707 0.016
Testosterone -0.024 0.114 1919/7672 -0.0240502 0.0152004 0.1136363 0.0057427 -0.0538430 0.015
SHBG -0.012 0.598 2005/7995 -0.0116386 0.0220862 0.5982308 0.0316503 -0.0549275 0.022
Liver markers Alanine aminotransferase -0.151 4.0e-12 2212/8827 -0.1505136 0.0216690 0.0000000 -0.1080423 -0.1929850 0.022
Albumin -0.067 0.006 2026/8075 -0.0671204 0.0242745 0.0057015 -0.0195425 -0.1146984 0.024
Alkaline phosphatase 0.134 3.0e-09 2215/8826 0.1340596 0.0225922 0.0000000 0.1783404 0.0897789 0.023
Aspartate aminotransferase 0.034 0.124 2206/8792 0.0341174 0.0221578 0.1236501 0.0775466 -0.0093118 0.022
C reactive protein -0.055 0.019 2202/8812 -0.0551381 0.0234842 0.0188979 -0.0091092 -0.1011671 0.023
Gamma glutamyltransferase -0.155 8.6e-13 2212/8819 -0.1553539 0.0216983 0.0000000 -0.1128252 -0.1978826 0.022
Direct bilirubin -0.012 0.642 1888/7560 -0.0115609 0.0248624 0.6419465 0.0371694 -0.0602912 0.025
Total bilirubin -0.025 0.268 2206/8787 -0.0249462 0.0225235 0.2680742 0.0191998 -0.0690923 0.023
Total protein -0.071 0.004 2026/8074 -0.0705408 0.0247022 0.0043036 -0.0221246 -0.1189571 0.025
Kidney markers Creatinine -0.443 4.1e-135 2212/8822 -0.4428694 0.0176538 0.0000000 -0.4082680 -0.4774709 0.018
Cystatin C 0.332 2.8e-60 2213/8830 0.3319466 0.0201453 0.0000000 0.3714314 0.2924619 0.020
eGFR -0.021 0.295 2210/8820 -0.0208691 0.0199177 0.2947694 0.0181696 -0.0599077 0.020
HbA1c 0.012 0.567 2198/8782 0.0120977 0.0211105 0.5666120 0.0534742 -0.0292788 0.021
Urate -0.171 2.6e-20 2209/8817 -0.1709024 0.0184686 0.0000000 -0.1347039 -0.2071009 0.018
Urea -0.614 1.6e-151 2212/8821 -0.6144779 0.0230717 0.0000000 -0.5692574 -0.6596983 0.023
Minerals Calcium -0.077 0.002 2025/8072 -0.0771083 0.0253304 0.0023397 -0.0274608 -0.1267559 0.025
Phosphate 0.013 0.604 2023/8055 0.0131200 0.0252895 0.6039165 0.0626873 -0.0364473 0.025
#png("forest.fullnoBMI.png",width = 13, height = 8, units = 'in', res = 600)

forestplot(out2, mean="Estimate", 
           labeltext=c("Category", "Trait","Estimateround", "Pround", "samp"),
            lower="lower", upper="upper",
           boxsize = 0.4, 
           hrzl_lines = list("2" = gpar(lty = 2)),
           col=fpColors(box = "black",
                       line = "black"),
           vertices = TRUE,
           grid=TRUE,
           zero = 0,
           xticks=xtik,
           mar = unit(c(0,0.5,0,0.5), "in"),
           xlab = "Standardized veg. effect ± 95% CI (no BMI covar.)",
           txt_gp = fpTxtGp(xlab  = gpar(cex = 1.),
                            ticks = gpar(fontfamily = "", cex = 1)
           )
           ) %>% fp_set_zebra_style("#F7F7F7")

#dev.off()

outplot=out
outplot$Pround = cell_spec(outplot$Pround, 
            color=ifelse(as.double(outplot$Pround) <0.0017 , "#36E072", "grey"))

colnames(outplot)=c("Category", "Trait", "BetaVeg",  "P", "N (veg/non-veg)","estimateexact", "SEexact","pexact", "upper", "lower", "SE")

outplot%>%select(Trait, BetaVeg,SE, P, `N (veg/non-veg)`)%>%
     kbl(booktabs = T, linesep = "", escape=FALSE,align="l") %>%
  kable_classic_2(full_width = F)
Trait BetaVeg SE P N (veg/non-veg)
Apolipoprotein A -0.068 0.021 0.001 2011/8021
Apolipoprotein B -0.182 0.023 8.0e-16 2197/8786
HDL cholesterol -0.143 0.020 1.8e-12 2026/8075
LDL direct -0.181 0.022 7.0e-16 2212/8808
Lipoprotein A -0.062 0.026 0.016 1774/7068
Cholesterol -0.192 0.022 1.3e-17 2215/8826
Triglycerides 0.214 0.022 5.3e-22 2212/8819
IGF 1 -0.207 0.022 3.2e-20 2201/8785
Vitamin D -0.384 0.026 8.2e-48 2130/8521
Bioavailable testosterone -0.027 0.016 0.095 1743/6981
Free testosterone -0.020 0.016 0.215 1784/7220
Testosterone -0.024 0.015 0.114 1919/7672
SHBG -0.012 0.022 0.598 2005/7995
Alanine aminotransferase -0.151 0.022 4.0e-12 2212/8827
Albumin -0.067 0.024 0.006 2026/8075
Alkaline phosphatase 0.134 0.023 3.0e-09 2215/8826
Aspartate aminotransferase 0.034 0.022 0.124 2206/8792
C reactive protein -0.055 0.023 0.019 2202/8812
Gamma glutamyltransferase -0.155 0.022 8.6e-13 2212/8819
Direct bilirubin -0.012 0.025 0.642 1888/7560
Total bilirubin -0.025 0.023 0.268 2206/8787
Total protein -0.071 0.025 0.004 2026/8074
Creatinine -0.443 0.018 4.1e-135 2212/8822
Cystatin C 0.332 0.020 2.8e-60 2213/8830
eGFR -0.021 0.020 0.295 2210/8820
HbA1c 0.012 0.021 0.567 2198/8782
Urate -0.171 0.018 2.6e-20 2209/8817
Urea -0.614 0.023 1.6e-151 2212/8821
Calcium -0.077 0.025 0.002 2025/8072
Phosphate 0.013 0.025 0.604 2023/8055

Forest plot: Full cohort, adj-BMI

xtik=c(-0.7,-0.6,-0.5,-0.4,-0.3,-0.2,-0.1,
       0,0.1,0.2,0.3,0.4,0.5)

outBMI$Trait=gsub(outBMI$Trait, pattern = "_", replacement = " ")
outBMI$Trait=gsub(outBMI$Trait, pattern = " rint", replacement="")

outBMI$samp=paste(ncase,ncontr, sep= "/")
outBMI=outBMI%>%select(Trait, Estimateround, Pround, samp,everything())

outBMI=cbind(Category, outBMI)

#Add header
header=tibble(Category="Category",
              Trait="Biomarker",
              Estimateround= "β Veg.",
              Pround="P",
              samp="N (Veg./Non-veg.)")

outBMI2=bind_rows(header, outBMI)
outBMI2%>%kbl()%>% 
 scroll_box(width = "600px", height = "200px")
Category Trait Estimateround Pround samp Estimate SE P upper lower SEround
Category Biomarker β Veg. P N (Veg./Non-veg.) NA NA NA NA NA NA
Blood lipids Apolipoprotein A -0.076 2.2e-04 2011/8021 -0.0756386 0.0204772 0.0002221 -0.0355033 -0.1157739 0.020
Apolipoprotein B -0.175 1.0e-15 2197/8786 -0.1754499 0.0218384 0.0000000 -0.1326466 -0.2182532 0.022
HDL cholesterol -0.155 3.8e-15 2026/8075 -0.1550842 0.0196969 0.0000000 -0.1164783 -0.1936901 0.020
LDL direct -0.176 1.1e-15 2212/8808 -0.1758329 0.0218950 0.0000000 -0.1329188 -0.2187470 0.022
Lipoprotein A -0.062 0.016 1774/7068 -0.0624393 0.0259677 0.0162150 -0.0115426 -0.1133359 0.026
Cholesterol -0.188 1.9e-17 2215/8826 -0.1882372 0.0221170 0.0000000 -0.1448880 -0.2315865 0.022
Triglycerides 0.223 4.0e-26 2212/8819 0.2230082 0.0210410 0.0000000 0.2642486 0.1817678 0.021
Hormones IGF 1 -0.209 2.1e-20 2201/8785 -0.2085186 0.0224777 0.0000000 -0.1644622 -0.2525750 0.022
Vitamin D -0.388 2.1e-49 2130/8521 -0.3875943 0.0260985 0.0000000 -0.3364412 -0.4387474 0.026
Bioavailable testosterone -0.026 0.107 1743/6981 -0.0262846 0.0163052 0.1069897 0.0056736 -0.0582427 0.016
Free testosterone -0.016 0.314 1784/7220 -0.0158583 0.0157441 0.3138386 0.0150001 -0.0467168 0.016
Testosterone -0.024 0.108 1919/7672 -0.0244041 0.0151854 0.1080713 0.0053593 -0.0541675 0.015
SHBG -0.027 0.187 2005/7995 -0.0272222 0.0206457 0.1873517 0.0132433 -0.0676877 0.021
Liver markers Alanine aminotransferase -0.144 6.6e-12 2212/8827 -0.1438391 0.0209243 0.0000000 -0.1028275 -0.1848506 0.021
Albumin -0.072 0.003 2026/8075 -0.0723362 0.0241263 0.0027221 -0.0250487 -0.1196237 0.024
Alkaline phosphatase 0.139 4.4e-10 2215/8826 0.1386650 0.0222002 0.0000000 0.1821775 0.0951525 0.022
Aspartate aminotransferase 0.036 0.108 2206/8792 0.0355081 0.0220870 0.1079411 0.0787985 -0.0077824 0.022
C reactive protein -0.044 0.045 2202/8812 -0.0437832 0.0218261 0.0448801 -0.0010041 -0.0865623 0.022
Gamma glutamyltransferase -0.149 2.1e-12 2212/8819 -0.1486769 0.0211324 0.0000000 -0.1072574 -0.1900965 0.021
Direct bilirubin -0.015 0.534 1888/7560 -0.0153855 0.0247425 0.5340727 0.0331099 -0.0638809 0.025
Total bilirubin -0.028 0.211 2206/8787 -0.0280535 0.0224274 0.2110127 0.0159042 -0.0720111 0.022
Total protein -0.069 0.005 2026/8074 -0.0692859 0.0247195 0.0050744 -0.0208357 -0.1177360 0.025
Kidney markers Creatinine -0.440 1.7e-135 2212/8822 -0.4402147 0.0175224 0.0000000 -0.4058707 -0.4745586 0.018
Cystatin C 0.338 9.9e-66 2213/8830 0.3384178 0.0196317 0.0000000 0.3768959 0.2999397 0.020
eGFR -0.027 0.172 2210/8820 -0.0266182 0.0195047 0.1723731 0.0116110 -0.0648475 0.020
HbA1c 0.014 0.506 2198/8782 0.0139620 0.0210049 0.5062564 0.0551317 -0.0272077 0.021
Urate -0.162 1.8e-20 2209/8817 -0.1621743 0.0174480 0.0000000 -0.1279762 -0.1963724 0.017
Urea -0.613 2.3e-151 2212/8821 -0.6126775 0.0230157 0.0000000 -0.5675668 -0.6577882 0.023
Minerals Calcium -0.078 0.002 2025/8072 -0.0776592 0.0253446 0.0021888 -0.0279838 -0.1273346 0.025
Phosphate 0.010 0.688 2023/8055 0.0101137 0.0252014 0.6881971 0.0595085 -0.0392811 0.025
# outBMI=rbind(colnames(outBMI),outBMI)
# outBMI=as.matrix(outBMI)

#tiff("forest.fullBMI.tiff",width = 13, height = 8, units = 'in', res = 600)

forestplot(outBMI2, mean="Estimate", 
              
           labeltext=c("Category", "Trait","Estimateround", "Pround", "samp"),
           lower="lower", upper="upper",
           boxsize = 0.4, 
           hrzl_lines = list("2" = gpar(lty = 2)),
           col=fpColors(box = "#36E072",
                        line = "black"),
           vertices = TRUE,
           grid=TRUE,
           zero = 0,
           mar = unit(c(0,0.5,0,0.5), "in"),
           xticks=xtik,
           xlab = "Standardized veg. effect ± 95% CI",
           txt_gp = fpTxtGp(xlab  = gpar(cex = 1.),
                            ticks = gpar(fontfamily = "", cex = 1)
                            )
)%>% fp_set_zebra_style("#F7F7F7")

#dev.off()

outBMIplot=outBMI
outBMIplot$Pround = cell_spec(outBMIplot$Pround, 
                           color=ifelse(as.double(outBMIplot$Pround) <(0.05/30), "#36E072", "grey"))

colnames(outBMIplot)=c("Category", "Trait", "BetaVeg",  "P", "N (veg/non-veg)","estimateexact", "SEexact","pexact", "upper", "lower", "SE")


outBMIplot%>%select(Trait, BetaVeg,SE, P, `N (veg/non-veg)`)%>%
    kbl(booktabs = T, linesep = "", escape=FALSE,align="l") %>%
    kable_classic_2(full_width = F)
Trait BetaVeg SE P N (veg/non-veg)
Apolipoprotein A -0.076 0.020 2.2e-04 2011/8021
Apolipoprotein B -0.175 0.022 1.0e-15 2197/8786
HDL cholesterol -0.155 0.020 3.8e-15 2026/8075
LDL direct -0.176 0.022 1.1e-15 2212/8808
Lipoprotein A -0.062 0.026 0.016 1774/7068
Cholesterol -0.188 0.022 1.9e-17 2215/8826
Triglycerides 0.223 0.021 4.0e-26 2212/8819
IGF 1 -0.209 0.022 2.1e-20 2201/8785
Vitamin D -0.388 0.026 2.1e-49 2130/8521
Bioavailable testosterone -0.026 0.016 0.107 1743/6981
Free testosterone -0.016 0.016 0.314 1784/7220
Testosterone -0.024 0.015 0.108 1919/7672
SHBG -0.027 0.021 0.187 2005/7995
Alanine aminotransferase -0.144 0.021 6.6e-12 2212/8827
Albumin -0.072 0.024 0.003 2026/8075
Alkaline phosphatase 0.139 0.022 4.4e-10 2215/8826
Aspartate aminotransferase 0.036 0.022 0.108 2206/8792
C reactive protein -0.044 0.022 0.045 2202/8812
Gamma glutamyltransferase -0.149 0.021 2.1e-12 2212/8819
Direct bilirubin -0.015 0.025 0.534 1888/7560
Total bilirubin -0.028 0.022 0.211 2206/8787
Total protein -0.069 0.025 0.005 2026/8074
Creatinine -0.440 0.018 1.7e-135 2212/8822
Cystatin C 0.338 0.020 9.9e-66 2213/8830
eGFR -0.027 0.020 0.172 2210/8820
HbA1c 0.014 0.021 0.506 2198/8782
Urate -0.162 0.017 1.8e-20 2209/8817
Urea -0.613 0.023 2.3e-151 2212/8821
Calcium -0.078 0.025 0.002 2025/8072
Phosphate 0.010 0.025 0.688 2023/8055
#%>%save_kable(file = "table.forest.fullBMI.png", zoom=5)

Sex-stratified models

Initialize for lm

mdM=md%>%filter(Sex==1)
mdF=md%>%filter(Sex==2)

#Linear model with covariates
fitM=list()
fitcM=list()
fitF=list()
fitcF=list()

outM=matrix(NA, nrow=length(phe), ncol=4)
outF=matrix(NA, nrow=length(phe), ncol=4)
colnames(outM)=c("Trait", "Estimate", "SE", "P")
colnames(outF)=c("Trait", "Estimate", "SE", "P")

Run lm sex-stratified

for (i in 1:length(phe)){
    
    fitM[[i]] <- lm(mdM[[phe[i]]] ~ Veg5yr1 + Age + AlcoholFreq +
                       PreviousSmoker +CurrentSmoker+ 
                       zTownsend+PCA1 + PCA2 + PCA3 +
                       PCA4 + PCA5, data = mdM, na.action = na.exclude)
    
    fitF[[i]] <- lm(mdF[[phe[i]]] ~ Veg5yr1 + Age + AlcoholFreq +
                        PreviousSmoker +CurrentSmoker+ 
                        zTownsend+PCA1 + PCA2 + PCA3 +
                        PCA4 + PCA5, data = mdF, na.action = na.exclude)
    
    outM[i,1]=phe[i]
    outF[i,1]=phe[i]
    #Cluster-robust standard errors
    coM=coeftest(fitM[[i]], vcov. = vcovCL, cluster = ~subclass)["Veg5yr1",,drop=FALSE]
    coF=coeftest(fitF[[i]], vcov. = vcovCL, cluster = ~subclass)["Veg5yr1",,drop=FALSE]
    
    outM[i,2]=coM[1]
    outM[i,3]=coM[2]
    outM[i,4]=coM[4]
    outF[i,2]=coF[1]
    outF[i,3]=coF[2]
    outF[i,4]=coF[4]
}

outM=as.data.frame(outM)
outM[2:4]=sapply(outM[2:4], as.numeric)
as_tibble(outM)%>%kbl(caption="Male")%>% 
    scroll_box(width = "600px", height = "200px")
Male
Trait Estimate SE P
Apolipoprotein_A_rint -0.0952853 0.0364059 0.0089011
Apolipoprotein_B_rint -0.2207853 0.0392515 0.0000000
HDL_cholesterol_rint -0.1641141 0.0366923 0.0000080
LDL_direct_rint -0.2332525 0.0379934 0.0000000
Lipoprotein_A_rint 0.0328448 0.0437274 0.4526356
Cholesterol_rint -0.2397004 0.0385400 0.0000000
Triglycerides_rint 0.1819623 0.0423750 0.0000180
IGF_1_rint -0.1470875 0.0361661 0.0000486
Vitamin_D_rint -0.3399957 0.0445313 0.0000000
Bioavailable_testosterone_rint -0.0136650 0.0271881 0.6152724
Free_testosterone_rint -0.0271668 0.0256020 0.2887110
Testosterone_rint -0.0054171 0.0258406 0.8339647
SHBG_rint 0.0084908 0.0338931 0.8022022
Alanine_aminotransferase_rint -0.1786818 0.0380667 0.0000028
Albumin_rint -0.0725563 0.0392101 0.0643339
Alkaline_phosphatase_rint 0.0918610 0.0371810 0.0135309
Aspartate_aminotransferase_rint -0.0138357 0.0380193 0.7159433
C_reactive_protein_rint -0.0217979 0.0397824 0.5837734
Gamma_glutamyltransferase_rint -0.1839996 0.0335011 0.0000000
Direct_bilirubin_rint 0.0016235 0.0401377 0.9677389
Total_bilirubin_rint -0.0049710 0.0365885 0.8919374
Total_protein_rint -0.1212301 0.0407742 0.0029672
Creatinine_rint -0.5932464 0.0287057 0.0000000
Cystatin_C_rint 0.2584474 0.0326227 0.0000000
eGFR_rint 0.0270818 0.0333761 0.4171801
HbA1c_rint -0.0579008 0.0390234 0.1379605
Urate_rint -0.2125853 0.0301554 0.0000000
Urea_rint -0.6485103 0.0392898 0.0000000
Calcium_rint -0.1079126 0.0410376 0.0085861
Phosphate_rint -0.0043175 0.0457044 0.9247453
outF=as.data.frame(outF)
outF[2:4]=sapply(outF[2:4], as.numeric)
as_tibble(outF)%>%kbl(caption="Female")%>% 
    scroll_box(width = "600px", height = "200px")
Female
Trait Estimate SE P
Apolipoprotein_A_rint -0.0537930 0.0258712 0.0376326
Apolipoprotein_B_rint -0.1610183 0.0276138 0.0000000
HDL_cholesterol_rint -0.1338099 0.0252510 0.0000001
LDL_direct_rint -0.1533478 0.0278392 0.0000000
Lipoprotein_A_rint -0.1128965 0.0324002 0.0004968
Cholesterol_rint -0.1668960 0.0272094 0.0000000
Triglycerides_rint 0.2294534 0.0255783 0.0000000
IGF_1_rint -0.2393446 0.0284029 0.0000000
Vitamin_D_rint -0.4082598 0.0325304 0.0000000
Bioavailable_testosterone_rint -0.0362555 0.0204403 0.0761658
Free_testosterone_rint -0.0145430 0.0204022 0.4759896
Testosterone_rint -0.0361472 0.0188108 0.0547023
SHBG_rint -0.0249352 0.0282841 0.3780273
Alanine_aminotransferase_rint -0.1360117 0.0258480 0.0000001
Albumin_rint -0.0657714 0.0303187 0.0300928
Alkaline_phosphatase_rint 0.1533666 0.0280940 0.0000000
Aspartate_aminotransferase_rint 0.0583374 0.0267167 0.0290268
C_reactive_protein_rint -0.0729257 0.0297921 0.0143957
Gamma_glutamyltransferase_rint -0.1426310 0.0278650 0.0000003
Direct_bilirubin_rint -0.0185505 0.0316874 0.5582862
Total_bilirubin_rint -0.0356154 0.0288654 0.2173023
Total_protein_rint -0.0436369 0.0307202 0.1555203
Creatinine_rint -0.3648762 0.0221789 0.0000000
Cystatin_C_rint 0.3706779 0.0255587 0.0000000
eGFR_rint -0.0457096 0.0250802 0.0684152
HbA1c_rint 0.0489391 0.0247224 0.0477929
Urate_rint -0.1482158 0.0231485 0.0000000
Urea_rint -0.5982422 0.0280654 0.0000000
Calcium_rint -0.0598459 0.0313665 0.0564403
Phosphate_rint 0.0206362 0.0297469 0.4878791
outM$Sex="Male"
outF$Sex="Female"

Add confidence intervals and round long decimals:

#95CI
outM$upper=outM$Estimate+1.96*outM$SE
outM$lower=outM$Estimate-1.96*outM$SE
outM$Estimateround=format(round(outM$Estimate, digits=3), digits=3)
outM$SEround=format(round(outM$SE, digits=3), digits=3)
outM$Pround=ifelse(outM$P < 0.001, 
                  formatC(outM$P, digits=1, format="e"), 
                  formatC(outM$P, digits=3, format="f"))

outF$upper=outF$Estimate+1.96*outF$SE
outF$lower=outF$Estimate-1.96*outF$SE
outF$Estimateround=format(round(outF$Estimate, digits=3), digits=3)
outF$SEround=format(round(outF$SE, digits=3), digits=3)
outF$Pround=ifelse(outF$P < 0.001, 
                   formatC(outF$P, digits=1, format="e"), 
                   formatC(outF$P, digits=3, format="f"))

Get sample sizes

ncontrM=md%>%filter(Veg5yr1==0, Sex==1)%>%select(all_of(phe))%>%sapply(function(x) sum(!is.na(x)))
ncaseM=md%>%filter(Veg5yr1==1, Sex==1)%>%select(all_of(phe))%>%sapply(function(x) sum(!is.na(x)))
ncontrF=md%>%filter(Veg5yr1==0, Sex==2)%>%select(all_of(phe))%>%sapply(function(x) sum(!is.na(x)))
ncaseF=md%>%filter(Veg5yr1==1, Sex==2)%>%select(all_of(phe))%>%sapply(function(x) sum(!is.na(x)))

outM$samp=paste(ncaseM,ncontrM, sep= "/")
outF$samp=paste(ncaseF,ncontrF, sep= "/")
tot=rbind(outM, outF)

Forest plots sex-stratified no-BMI

tot$Trait=gsub(out$Trait, pattern = "_", replacement = " ")
tot$Trait=gsub(out$Trait, pattern = " rint", replacement="")
tot%>%kbl()%>% 
    scroll_box(width = "600px", height = "200px")
Trait Estimate SE P Sex upper lower Estimateround SEround Pround samp
Apolipoprotein A -0.0952853 0.0364059 0.0089011 Male -0.0239298 -0.1666409 -0.095 0.036 0.009 687/2798
Apolipoprotein B -0.2207853 0.0392515 0.0000000 Male -0.1438524 -0.2977183 -0.221 0.039 2.0e-08 745/3014
HDL cholesterol -0.1641141 0.0366923 0.0000080 Male -0.0921972 -0.2360310 -0.164 0.037 8.0e-06 689/2800
LDL direct -0.2332525 0.0379934 0.0000000 Male -0.1587854 -0.3077196 -0.233 0.038 9.1e-10 757/3025
Lipoprotein A 0.0328448 0.0437274 0.4526356 Male 0.1185506 -0.0528610 0.033 0.044 0.453 608/2415
Cholesterol -0.2397004 0.0385400 0.0000000 Male -0.1641620 -0.3152388 -0.240 0.039 5.5e-10 758/3030
Triglycerides 0.1819623 0.0423750 0.0000180 Male 0.2650173 0.0989073 0.182 0.042 1.8e-05 756/3027
IGF 1 -0.1470875 0.0361661 0.0000486 Male -0.0762020 -0.2179730 -0.147 0.036 4.9e-05 752/3013
Vitamin D -0.3399957 0.0445313 0.0000000 Male -0.2527144 -0.4272770 -0.340 0.045 2.9e-14 738/2956
Bioavailable testosterone -0.0136650 0.0271881 0.6152724 Male 0.0396238 -0.0669537 -0.014 0.027 0.615 679/2761
Free testosterone -0.0271668 0.0256020 0.2887110 Male 0.0230132 -0.0773468 -0.027 0.026 0.289 678/2767
Testosterone -0.0054171 0.0258406 0.8339647 Male 0.0452305 -0.0560646 -0.005 0.026 0.834 751/2994
SHBG 0.0084908 0.0338931 0.8022022 Male 0.0749213 -0.0579397 0.008 0.034 0.802 682/2783
Alanine aminotransferase -0.1786818 0.0380667 0.0000028 Male -0.1040710 -0.2532925 -0.179 0.038 2.8e-06 755/3029
Albumin -0.0725563 0.0392101 0.0643339 Male 0.0042956 -0.1494082 -0.073 0.039 0.064 688/2800
Alkaline phosphatase 0.0918610 0.0371810 0.0135309 Male 0.1647358 0.0189862 0.092 0.037 0.014 758/3030
Aspartate aminotransferase -0.0138357 0.0380193 0.7159433 Male 0.0606821 -0.0883536 -0.014 0.038 0.716 754/3020
C reactive protein -0.0217979 0.0397824 0.5837734 Male 0.0561756 -0.0997715 -0.022 0.040 0.584 750/3023
Gamma glutamyltransferase -0.1839996 0.0335011 0.0000000 Male -0.1183374 -0.2496618 -0.184 0.034 4.2e-08 757/3026
Direct bilirubin 0.0016235 0.0401377 0.9677389 Male 0.0802933 -0.0770464 0.002 0.040 0.968 704/2844
Total bilirubin -0.0049710 0.0365885 0.8919374 Male 0.0667425 -0.0766845 -0.005 0.037 0.892 755/3018
Total protein -0.1212301 0.0407742 0.0029672 Male -0.0413128 -0.2011475 -0.121 0.041 0.003 689/2802
Creatinine -0.5932464 0.0287057 0.0000000 Male -0.5369832 -0.6495095 -0.593 0.029 5.6e-90 756/3029
Cystatin C 0.2584474 0.0326227 0.0000000 Male 0.3223879 0.1945069 0.258 0.033 3.0e-15 757/3029
eGFR 0.0270818 0.0333761 0.4171801 Male 0.0924989 -0.0383353 0.027 0.033 0.417 755/3028
HbA1c -0.0579008 0.0390234 0.1379605 Male 0.0185850 -0.1343866 -0.058 0.039 0.138 748/3008
Urate -0.2125853 0.0301554 0.0000000 Male -0.1534806 -0.2716900 -0.213 0.030 2.1e-12 757/3026
Urea -0.6485103 0.0392898 0.0000000 Male -0.5715022 -0.7255184 -0.649 0.039 3.8e-59 757/3029
Calcium -0.1079126 0.0410376 0.0085861 Male -0.0274790 -0.1883462 -0.108 0.041 0.009 687/2802
Phosphate -0.0043175 0.0457044 0.9247453 Male 0.0852632 -0.0938982 -0.004 0.046 0.925 688/2791
Apolipoprotein A -0.0537930 0.0258712 0.0376326 Female -0.0030853 -0.1045006 -0.054 0.026 0.038 1324/5223
Apolipoprotein B -0.1610183 0.0276138 0.0000000 Female -0.1068953 -0.2151413 -0.161 0.028 5.7e-09 1452/5772
HDL cholesterol -0.1338099 0.0252510 0.0000001 Female -0.0843179 -0.1833018 -0.134 0.025 1.2e-07 1337/5275
LDL direct -0.1533478 0.0278392 0.0000000 Female -0.0987829 -0.2079128 -0.153 0.028 3.7e-08 1455/5783
Lipoprotein A -0.1128965 0.0324002 0.0004968 Female -0.0493921 -0.1764010 -0.113 0.032 5.0e-04 1166/4653
Cholesterol -0.1668960 0.0272094 0.0000000 Female -0.1135655 -0.2202265 -0.167 0.027 9.0e-10 1457/5796
Triglycerides 0.2294534 0.0255783 0.0000000 Female 0.2795868 0.1793200 0.229 0.026 3.7e-19 1456/5792
IGF 1 -0.2393446 0.0284029 0.0000000 Female -0.1836750 -0.2950142 -0.239 0.028 4.2e-17 1449/5772
Vitamin D -0.4082598 0.0325304 0.0000000 Female -0.3445001 -0.4720194 -0.408 0.033 9.7e-36 1392/5565
Bioavailable testosterone -0.0362555 0.0204403 0.0761658 Female 0.0038074 -0.0763184 -0.036 0.020 0.076 1064/4220
Free testosterone -0.0145430 0.0204022 0.4759896 Female 0.0254453 -0.0545312 -0.015 0.020 0.476 1106/4453
Testosterone -0.0361472 0.0188108 0.0547023 Female 0.0007220 -0.0730163 -0.036 0.019 0.055 1168/4678
SHBG -0.0249352 0.0282841 0.3780273 Female 0.0305016 -0.0803720 -0.025 0.028 0.378 1323/5212
Alanine aminotransferase -0.1360117 0.0258480 0.0000001 Female -0.0853497 -0.1866737 -0.136 0.026 1.5e-07 1457/5798
Albumin -0.0657714 0.0303187 0.0300928 Female -0.0063468 -0.1251960 -0.066 0.030 0.030 1338/5275
Alkaline phosphatase 0.1533666 0.0280940 0.0000000 Female 0.2084309 0.0983023 0.153 0.028 4.9e-08 1457/5796
Aspartate aminotransferase 0.0583374 0.0267167 0.0290268 Female 0.1107021 0.0059728 0.058 0.027 0.029 1452/5772
C reactive protein -0.0729257 0.0297921 0.0143957 Female -0.0145333 -0.1313182 -0.073 0.030 0.014 1452/5789
Gamma glutamyltransferase -0.1426310 0.0278650 0.0000003 Female -0.0880156 -0.1972464 -0.143 0.028 3.2e-07 1455/5793
Direct bilirubin -0.0185505 0.0316874 0.5582862 Female 0.0435568 -0.0806577 -0.019 0.032 0.558 1184/4716
Total bilirubin -0.0356154 0.0288654 0.2173023 Female 0.0209609 -0.0921916 -0.036 0.029 0.217 1451/5769
Total protein -0.0436369 0.0307202 0.1555203 Female 0.0165747 -0.1038486 -0.044 0.031 0.156 1337/5272
Creatinine -0.3648762 0.0221789 0.0000000 Female -0.3214055 -0.4083469 -0.365 0.022 9.8e-60 1456/5793
Cystatin C 0.3706779 0.0255587 0.0000000 Female 0.4207729 0.3205828 0.371 0.026 5.3e-47 1456/5801
eGFR -0.0457096 0.0250802 0.0684152 Female 0.0034477 -0.0948668 -0.046 0.025 0.068 1455/5792
HbA1c 0.0489391 0.0247224 0.0477929 Female 0.0973950 0.0004831 0.049 0.025 0.048 1450/5774
Urate -0.1482158 0.0231485 0.0000000 Female -0.1028448 -0.1935869 -0.148 0.023 1.6e-10 1452/5791
Urea -0.5982422 0.0280654 0.0000000 Female -0.5432340 -0.6532504 -0.598 0.028 7.8e-98 1455/5792
Calcium -0.0598459 0.0313665 0.0564403 Female 0.0016324 -0.1213241 -0.060 0.031 0.056 1338/5270
Phosphate 0.0206362 0.0297469 0.4878791 Female 0.0789402 -0.0376678 0.021 0.030 0.488 1335/5264
tot$EstimateroundM=tot$Estimateround[tot$Sex=="Male"]
tot$EstimateroundF=tot$Estimateround[tot$Sex=="Female"]
tot$SEroundM=tot$SEround[tot$Sex=="Male"]
tot$SEroundF=tot$SEround[tot$Sex=="Female"]
tot$ProundM=tot$Pround[tot$Sex=="Male"]
tot$ProundF=tot$Pround[tot$Sex=="Female"]
tot$sampM=tot$samp[tot$Sex=="Male"]
tot$sampF=tot$samp[tot$Sex=="Female"]

tot%>%kbl()%>% 
    scroll_box(width = "600px", height = "200px")
Trait Estimate SE P Sex upper lower Estimateround SEround Pround samp EstimateroundM EstimateroundF SEroundM SEroundF ProundM ProundF sampM sampF
Apolipoprotein A -0.0952853 0.0364059 0.0089011 Male -0.0239298 -0.1666409 -0.095 0.036 0.009 687/2798 -0.095 -0.054 0.036 0.026 0.009 0.038 687/2798 1324/5223
Apolipoprotein B -0.2207853 0.0392515 0.0000000 Male -0.1438524 -0.2977183 -0.221 0.039 2.0e-08 745/3014 -0.221 -0.161 0.039 0.028 2.0e-08 5.7e-09 745/3014 1452/5772
HDL cholesterol -0.1641141 0.0366923 0.0000080 Male -0.0921972 -0.2360310 -0.164 0.037 8.0e-06 689/2800 -0.164 -0.134 0.037 0.025 8.0e-06 1.2e-07 689/2800 1337/5275
LDL direct -0.2332525 0.0379934 0.0000000 Male -0.1587854 -0.3077196 -0.233 0.038 9.1e-10 757/3025 -0.233 -0.153 0.038 0.028 9.1e-10 3.7e-08 757/3025 1455/5783
Lipoprotein A 0.0328448 0.0437274 0.4526356 Male 0.1185506 -0.0528610 0.033 0.044 0.453 608/2415 0.033 -0.113 0.044 0.032 0.453 5.0e-04 608/2415 1166/4653
Cholesterol -0.2397004 0.0385400 0.0000000 Male -0.1641620 -0.3152388 -0.240 0.039 5.5e-10 758/3030 -0.240 -0.167 0.039 0.027 5.5e-10 9.0e-10 758/3030 1457/5796
Triglycerides 0.1819623 0.0423750 0.0000180 Male 0.2650173 0.0989073 0.182 0.042 1.8e-05 756/3027 0.182 0.229 0.042 0.026 1.8e-05 3.7e-19 756/3027 1456/5792
IGF 1 -0.1470875 0.0361661 0.0000486 Male -0.0762020 -0.2179730 -0.147 0.036 4.9e-05 752/3013 -0.147 -0.239 0.036 0.028 4.9e-05 4.2e-17 752/3013 1449/5772
Vitamin D -0.3399957 0.0445313 0.0000000 Male -0.2527144 -0.4272770 -0.340 0.045 2.9e-14 738/2956 -0.340 -0.408 0.045 0.033 2.9e-14 9.7e-36 738/2956 1392/5565
Bioavailable testosterone -0.0136650 0.0271881 0.6152724 Male 0.0396238 -0.0669537 -0.014 0.027 0.615 679/2761 -0.014 -0.036 0.027 0.020 0.615 0.076 679/2761 1064/4220
Free testosterone -0.0271668 0.0256020 0.2887110 Male 0.0230132 -0.0773468 -0.027 0.026 0.289 678/2767 -0.027 -0.015 0.026 0.020 0.289 0.476 678/2767 1106/4453
Testosterone -0.0054171 0.0258406 0.8339647 Male 0.0452305 -0.0560646 -0.005 0.026 0.834 751/2994 -0.005 -0.036 0.026 0.019 0.834 0.055 751/2994 1168/4678
SHBG 0.0084908 0.0338931 0.8022022 Male 0.0749213 -0.0579397 0.008 0.034 0.802 682/2783 0.008 -0.025 0.034 0.028 0.802 0.378 682/2783 1323/5212
Alanine aminotransferase -0.1786818 0.0380667 0.0000028 Male -0.1040710 -0.2532925 -0.179 0.038 2.8e-06 755/3029 -0.179 -0.136 0.038 0.026 2.8e-06 1.5e-07 755/3029 1457/5798
Albumin -0.0725563 0.0392101 0.0643339 Male 0.0042956 -0.1494082 -0.073 0.039 0.064 688/2800 -0.073 -0.066 0.039 0.030 0.064 0.030 688/2800 1338/5275
Alkaline phosphatase 0.0918610 0.0371810 0.0135309 Male 0.1647358 0.0189862 0.092 0.037 0.014 758/3030 0.092 0.153 0.037 0.028 0.014 4.9e-08 758/3030 1457/5796
Aspartate aminotransferase -0.0138357 0.0380193 0.7159433 Male 0.0606821 -0.0883536 -0.014 0.038 0.716 754/3020 -0.014 0.058 0.038 0.027 0.716 0.029 754/3020 1452/5772
C reactive protein -0.0217979 0.0397824 0.5837734 Male 0.0561756 -0.0997715 -0.022 0.040 0.584 750/3023 -0.022 -0.073 0.040 0.030 0.584 0.014 750/3023 1452/5789
Gamma glutamyltransferase -0.1839996 0.0335011 0.0000000 Male -0.1183374 -0.2496618 -0.184 0.034 4.2e-08 757/3026 -0.184 -0.143 0.034 0.028 4.2e-08 3.2e-07 757/3026 1455/5793
Direct bilirubin 0.0016235 0.0401377 0.9677389 Male 0.0802933 -0.0770464 0.002 0.040 0.968 704/2844 0.002 -0.019 0.040 0.032 0.968 0.558 704/2844 1184/4716
Total bilirubin -0.0049710 0.0365885 0.8919374 Male 0.0667425 -0.0766845 -0.005 0.037 0.892 755/3018 -0.005 -0.036 0.037 0.029 0.892 0.217 755/3018 1451/5769
Total protein -0.1212301 0.0407742 0.0029672 Male -0.0413128 -0.2011475 -0.121 0.041 0.003 689/2802 -0.121 -0.044 0.041 0.031 0.003 0.156 689/2802 1337/5272
Creatinine -0.5932464 0.0287057 0.0000000 Male -0.5369832 -0.6495095 -0.593 0.029 5.6e-90 756/3029 -0.593 -0.365 0.029 0.022 5.6e-90 9.8e-60 756/3029 1456/5793
Cystatin C 0.2584474 0.0326227 0.0000000 Male 0.3223879 0.1945069 0.258 0.033 3.0e-15 757/3029 0.258 0.371 0.033 0.026 3.0e-15 5.3e-47 757/3029 1456/5801
eGFR 0.0270818 0.0333761 0.4171801 Male 0.0924989 -0.0383353 0.027 0.033 0.417 755/3028 0.027 -0.046 0.033 0.025 0.417 0.068 755/3028 1455/5792
HbA1c -0.0579008 0.0390234 0.1379605 Male 0.0185850 -0.1343866 -0.058 0.039 0.138 748/3008 -0.058 0.049 0.039 0.025 0.138 0.048 748/3008 1450/5774
Urate -0.2125853 0.0301554 0.0000000 Male -0.1534806 -0.2716900 -0.213 0.030 2.1e-12 757/3026 -0.213 -0.148 0.030 0.023 2.1e-12 1.6e-10 757/3026 1452/5791
Urea -0.6485103 0.0392898 0.0000000 Male -0.5715022 -0.7255184 -0.649 0.039 3.8e-59 757/3029 -0.649 -0.598 0.039 0.028 3.8e-59 7.8e-98 757/3029 1455/5792
Calcium -0.1079126 0.0410376 0.0085861 Male -0.0274790 -0.1883462 -0.108 0.041 0.009 687/2802 -0.108 -0.060 0.041 0.031 0.009 0.056 687/2802 1338/5270
Phosphate -0.0043175 0.0457044 0.9247453 Male 0.0852632 -0.0938982 -0.004 0.046 0.925 688/2791 -0.004 0.021 0.046 0.030 0.925 0.488 688/2791 1335/5264
Apolipoprotein A -0.0537930 0.0258712 0.0376326 Female -0.0030853 -0.1045006 -0.054 0.026 0.038 1324/5223 -0.095 -0.054 0.036 0.026 0.009 0.038 687/2798 1324/5223
Apolipoprotein B -0.1610183 0.0276138 0.0000000 Female -0.1068953 -0.2151413 -0.161 0.028 5.7e-09 1452/5772 -0.221 -0.161 0.039 0.028 2.0e-08 5.7e-09 745/3014 1452/5772
HDL cholesterol -0.1338099 0.0252510 0.0000001 Female -0.0843179 -0.1833018 -0.134 0.025 1.2e-07 1337/5275 -0.164 -0.134 0.037 0.025 8.0e-06 1.2e-07 689/2800 1337/5275
LDL direct -0.1533478 0.0278392 0.0000000 Female -0.0987829 -0.2079128 -0.153 0.028 3.7e-08 1455/5783 -0.233 -0.153 0.038 0.028 9.1e-10 3.7e-08 757/3025 1455/5783
Lipoprotein A -0.1128965 0.0324002 0.0004968 Female -0.0493921 -0.1764010 -0.113 0.032 5.0e-04 1166/4653 0.033 -0.113 0.044 0.032 0.453 5.0e-04 608/2415 1166/4653
Cholesterol -0.1668960 0.0272094 0.0000000 Female -0.1135655 -0.2202265 -0.167 0.027 9.0e-10 1457/5796 -0.240 -0.167 0.039 0.027 5.5e-10 9.0e-10 758/3030 1457/5796
Triglycerides 0.2294534 0.0255783 0.0000000 Female 0.2795868 0.1793200 0.229 0.026 3.7e-19 1456/5792 0.182 0.229 0.042 0.026 1.8e-05 3.7e-19 756/3027 1456/5792
IGF 1 -0.2393446 0.0284029 0.0000000 Female -0.1836750 -0.2950142 -0.239 0.028 4.2e-17 1449/5772 -0.147 -0.239 0.036 0.028 4.9e-05 4.2e-17 752/3013 1449/5772
Vitamin D -0.4082598 0.0325304 0.0000000 Female -0.3445001 -0.4720194 -0.408 0.033 9.7e-36 1392/5565 -0.340 -0.408 0.045 0.033 2.9e-14 9.7e-36 738/2956 1392/5565
Bioavailable testosterone -0.0362555 0.0204403 0.0761658 Female 0.0038074 -0.0763184 -0.036 0.020 0.076 1064/4220 -0.014 -0.036 0.027 0.020 0.615 0.076 679/2761 1064/4220
Free testosterone -0.0145430 0.0204022 0.4759896 Female 0.0254453 -0.0545312 -0.015 0.020 0.476 1106/4453 -0.027 -0.015 0.026 0.020 0.289 0.476 678/2767 1106/4453
Testosterone -0.0361472 0.0188108 0.0547023 Female 0.0007220 -0.0730163 -0.036 0.019 0.055 1168/4678 -0.005 -0.036 0.026 0.019 0.834 0.055 751/2994 1168/4678
SHBG -0.0249352 0.0282841 0.3780273 Female 0.0305016 -0.0803720 -0.025 0.028 0.378 1323/5212 0.008 -0.025 0.034 0.028 0.802 0.378 682/2783 1323/5212
Alanine aminotransferase -0.1360117 0.0258480 0.0000001 Female -0.0853497 -0.1866737 -0.136 0.026 1.5e-07 1457/5798 -0.179 -0.136 0.038 0.026 2.8e-06 1.5e-07 755/3029 1457/5798
Albumin -0.0657714 0.0303187 0.0300928 Female -0.0063468 -0.1251960 -0.066 0.030 0.030 1338/5275 -0.073 -0.066 0.039 0.030 0.064 0.030 688/2800 1338/5275
Alkaline phosphatase 0.1533666 0.0280940 0.0000000 Female 0.2084309 0.0983023 0.153 0.028 4.9e-08 1457/5796 0.092 0.153 0.037 0.028 0.014 4.9e-08 758/3030 1457/5796
Aspartate aminotransferase 0.0583374 0.0267167 0.0290268 Female 0.1107021 0.0059728 0.058 0.027 0.029 1452/5772 -0.014 0.058 0.038 0.027 0.716 0.029 754/3020 1452/5772
C reactive protein -0.0729257 0.0297921 0.0143957 Female -0.0145333 -0.1313182 -0.073 0.030 0.014 1452/5789 -0.022 -0.073 0.040 0.030 0.584 0.014 750/3023 1452/5789
Gamma glutamyltransferase -0.1426310 0.0278650 0.0000003 Female -0.0880156 -0.1972464 -0.143 0.028 3.2e-07 1455/5793 -0.184 -0.143 0.034 0.028 4.2e-08 3.2e-07 757/3026 1455/5793
Direct bilirubin -0.0185505 0.0316874 0.5582862 Female 0.0435568 -0.0806577 -0.019 0.032 0.558 1184/4716 0.002 -0.019 0.040 0.032 0.968 0.558 704/2844 1184/4716
Total bilirubin -0.0356154 0.0288654 0.2173023 Female 0.0209609 -0.0921916 -0.036 0.029 0.217 1451/5769 -0.005 -0.036 0.037 0.029 0.892 0.217 755/3018 1451/5769
Total protein -0.0436369 0.0307202 0.1555203 Female 0.0165747 -0.1038486 -0.044 0.031 0.156 1337/5272 -0.121 -0.044 0.041 0.031 0.003 0.156 689/2802 1337/5272
Creatinine -0.3648762 0.0221789 0.0000000 Female -0.3214055 -0.4083469 -0.365 0.022 9.8e-60 1456/5793 -0.593 -0.365 0.029 0.022 5.6e-90 9.8e-60 756/3029 1456/5793
Cystatin C 0.3706779 0.0255587 0.0000000 Female 0.4207729 0.3205828 0.371 0.026 5.3e-47 1456/5801 0.258 0.371 0.033 0.026 3.0e-15 5.3e-47 757/3029 1456/5801
eGFR -0.0457096 0.0250802 0.0684152 Female 0.0034477 -0.0948668 -0.046 0.025 0.068 1455/5792 0.027 -0.046 0.033 0.025 0.417 0.068 755/3028 1455/5792
HbA1c 0.0489391 0.0247224 0.0477929 Female 0.0973950 0.0004831 0.049 0.025 0.048 1450/5774 -0.058 0.049 0.039 0.025 0.138 0.048 748/3008 1450/5774
Urate -0.1482158 0.0231485 0.0000000 Female -0.1028448 -0.1935869 -0.148 0.023 1.6e-10 1452/5791 -0.213 -0.148 0.030 0.023 2.1e-12 1.6e-10 757/3026 1452/5791
Urea -0.5982422 0.0280654 0.0000000 Female -0.5432340 -0.6532504 -0.598 0.028 7.8e-98 1455/5792 -0.649 -0.598 0.039 0.028 3.8e-59 7.8e-98 757/3029 1455/5792
Calcium -0.0598459 0.0313665 0.0564403 Female 0.0016324 -0.1213241 -0.060 0.031 0.056 1338/5270 -0.108 -0.060 0.041 0.031 0.009 0.056 687/2802 1338/5270
Phosphate 0.0206362 0.0297469 0.4878791 Female 0.0789402 -0.0376678 0.021 0.030 0.488 1335/5264 -0.004 0.021 0.046 0.030 0.925 0.488 688/2791 1335/5264
tot=tot%>%select(Trait, EstimateroundM, ProundM, sampM,
                 EstimateroundF, ProundF, sampF, everything())
tot=cbind(Category, tot)
headerM=tibble(Category="Category",
                Trait="Trait",
              Sex="Male")
headerF=tibble(Category="Category",
    Trait="Trait",
              Sex="Female")

tot2=bind_rows(headerM, tot)
tot3=bind_rows(headerF, tot2)
# 

#png("forest.sexstrat.png",width = 10, height =8, units = 'in', res = 600)

tot3 %>%
    group_by(Sex) %>%
    forestplot(mean="Estimate",
               labeltext=c("Category", "Trait"),
               lower="lower", upper="upper",
               boxsize = 0.2,
               ci.vertices = TRUE,
               ci.vertices.height = 0.05,
               shapes_gp = fpShapesGp(box = c("darkred", "blue") %>%
                    lapply(function(x) gpar(fill = x, col = "#555555")),
                            default = gpar(vertices = TRUE)),
               hrzl_lines = list("2" = gpar(lty = 2)),
               grid=TRUE,
               zero = 0,
               xticks=xtik,
               mar = unit(c(0,0.5,0.2,0.5), "in"),
               xlab = "Standardized veg. effect ± 95% CI (no BMI covar.)",
               txt_gp = fpTxtGp(xlab  = gpar(cex = 1.2),
                                ticks = gpar(fontfamily = "", cex = 1)
               ),
               fn.ci_norm = c(fpDrawNormalCI, fpDrawCircleCI)
               )%>% fp_set_zebra_style("#F7F7F7")

#dev.off()

outplot=tot%>%select(Trait, EstimateroundM, SEroundM, ProundM, sampM,
                 EstimateroundF, SEroundF, ProundF, sampF)
outplot$ProundM = cell_spec(outplot$ProundM, 
            color=ifelse(as.double(outplot$ProundM) <0.0017, "#36E072", "grey"))

outplot$ProundF = cell_spec(outplot$ProundF, 
            color=ifelse(as.double(outplot$ProundF) <0.0017, "#36E072", "grey"))

colnames(outplot)=c("Trait", "BetaVegm", "SEm", "Pm", "N (veg/non-veg)m","BetaVegf", "SEf", "Pf", "N (veg/non-veg)f")

outplot%>%
     kbl(booktabs = T, linesep = "", escape=FALSE,align="l") %>%
  kable_classic_2(full_width = F)
Trait BetaVegm SEm Pm N (veg/non-veg)m BetaVegf SEf Pf N (veg/non-veg)f
Apolipoprotein A -0.095 0.036 0.009 687/2798 -0.054 0.026 0.038 1324/5223
Apolipoprotein B -0.221 0.039 2.0e-08 745/3014 -0.161 0.028 5.7e-09 1452/5772
HDL cholesterol -0.164 0.037 8.0e-06 689/2800 -0.134 0.025 1.2e-07 1337/5275
LDL direct -0.233 0.038 9.1e-10 757/3025 -0.153 0.028 3.7e-08 1455/5783
Lipoprotein A 0.033 0.044 0.453 608/2415 -0.113 0.032 5.0e-04 1166/4653
Cholesterol -0.240 0.039 5.5e-10 758/3030 -0.167 0.027 9.0e-10 1457/5796
Triglycerides 0.182 0.042 1.8e-05 756/3027 0.229 0.026 3.7e-19 1456/5792
IGF 1 -0.147 0.036 4.9e-05 752/3013 -0.239 0.028 4.2e-17 1449/5772
Vitamin D -0.340 0.045 2.9e-14 738/2956 -0.408 0.033 9.7e-36 1392/5565
Bioavailable testosterone -0.014 0.027 0.615 679/2761 -0.036 0.020 0.076 1064/4220
Free testosterone -0.027 0.026 0.289 678/2767 -0.015 0.020 0.476 1106/4453
Testosterone -0.005 0.026 0.834 751/2994 -0.036 0.019 0.055 1168/4678
SHBG 0.008 0.034 0.802 682/2783 -0.025 0.028 0.378 1323/5212
Alanine aminotransferase -0.179 0.038 2.8e-06 755/3029 -0.136 0.026 1.5e-07 1457/5798
Albumin -0.073 0.039 0.064 688/2800 -0.066 0.030 0.030 1338/5275
Alkaline phosphatase 0.092 0.037 0.014 758/3030 0.153 0.028 4.9e-08 1457/5796
Aspartate aminotransferase -0.014 0.038 0.716 754/3020 0.058 0.027 0.029 1452/5772
C reactive protein -0.022 0.040 0.584 750/3023 -0.073 0.030 0.014 1452/5789
Gamma glutamyltransferase -0.184 0.034 4.2e-08 757/3026 -0.143 0.028 3.2e-07 1455/5793
Direct bilirubin 0.002 0.040 0.968 704/2844 -0.019 0.032 0.558 1184/4716
Total bilirubin -0.005 0.037 0.892 755/3018 -0.036 0.029 0.217 1451/5769
Total protein -0.121 0.041 0.003 689/2802 -0.044 0.031 0.156 1337/5272
Creatinine -0.593 0.029 5.6e-90 756/3029 -0.365 0.022 9.8e-60 1456/5793
Cystatin C 0.258 0.033 3.0e-15 757/3029 0.371 0.026 5.3e-47 1456/5801
eGFR 0.027 0.033 0.417 755/3028 -0.046 0.025 0.068 1455/5792
HbA1c -0.058 0.039 0.138 748/3008 0.049 0.025 0.048 1450/5774
Urate -0.213 0.030 2.1e-12 757/3026 -0.148 0.023 1.6e-10 1452/5791
Urea -0.649 0.039 3.8e-59 757/3029 -0.598 0.028 7.8e-98 1455/5792
Calcium -0.108 0.041 0.009 687/2802 -0.060 0.031 0.056 1338/5270
Phosphate -0.004 0.046 0.925 688/2791 0.021 0.030 0.488 1335/5264
Apolipoprotein A -0.095 0.036 0.009 687/2798 -0.054 0.026 0.038 1324/5223
Apolipoprotein B -0.221 0.039 2.0e-08 745/3014 -0.161 0.028 5.7e-09 1452/5772
HDL cholesterol -0.164 0.037 8.0e-06 689/2800 -0.134 0.025 1.2e-07 1337/5275
LDL direct -0.233 0.038 9.1e-10 757/3025 -0.153 0.028 3.7e-08 1455/5783
Lipoprotein A 0.033 0.044 0.453 608/2415 -0.113 0.032 5.0e-04 1166/4653
Cholesterol -0.240 0.039 5.5e-10 758/3030 -0.167 0.027 9.0e-10 1457/5796
Triglycerides 0.182 0.042 1.8e-05 756/3027 0.229 0.026 3.7e-19 1456/5792
IGF 1 -0.147 0.036 4.9e-05 752/3013 -0.239 0.028 4.2e-17 1449/5772
Vitamin D -0.340 0.045 2.9e-14 738/2956 -0.408 0.033 9.7e-36 1392/5565
Bioavailable testosterone -0.014 0.027 0.615 679/2761 -0.036 0.020 0.076 1064/4220
Free testosterone -0.027 0.026 0.289 678/2767 -0.015 0.020 0.476 1106/4453
Testosterone -0.005 0.026 0.834 751/2994 -0.036 0.019 0.055 1168/4678
SHBG 0.008 0.034 0.802 682/2783 -0.025 0.028 0.378 1323/5212
Alanine aminotransferase -0.179 0.038 2.8e-06 755/3029 -0.136 0.026 1.5e-07 1457/5798
Albumin -0.073 0.039 0.064 688/2800 -0.066 0.030 0.030 1338/5275
Alkaline phosphatase 0.092 0.037 0.014 758/3030 0.153 0.028 4.9e-08 1457/5796
Aspartate aminotransferase -0.014 0.038 0.716 754/3020 0.058 0.027 0.029 1452/5772
C reactive protein -0.022 0.040 0.584 750/3023 -0.073 0.030 0.014 1452/5789
Gamma glutamyltransferase -0.184 0.034 4.2e-08 757/3026 -0.143 0.028 3.2e-07 1455/5793
Direct bilirubin 0.002 0.040 0.968 704/2844 -0.019 0.032 0.558 1184/4716
Total bilirubin -0.005 0.037 0.892 755/3018 -0.036 0.029 0.217 1451/5769
Total protein -0.121 0.041 0.003 689/2802 -0.044 0.031 0.156 1337/5272
Creatinine -0.593 0.029 5.6e-90 756/3029 -0.365 0.022 9.8e-60 1456/5793
Cystatin C 0.258 0.033 3.0e-15 757/3029 0.371 0.026 5.3e-47 1456/5801
eGFR 0.027 0.033 0.417 755/3028 -0.046 0.025 0.068 1455/5792
HbA1c -0.058 0.039 0.138 748/3008 0.049 0.025 0.048 1450/5774
Urate -0.213 0.030 2.1e-12 757/3026 -0.148 0.023 1.6e-10 1452/5791
Urea -0.649 0.039 3.8e-59 757/3029 -0.598 0.028 7.8e-98 1455/5792
Calcium -0.108 0.041 0.009 687/2802 -0.060 0.031 0.056 1338/5270
Phosphate -0.004 0.046 0.925 688/2791 0.021 0.030 0.488 1335/5264

Run lm sex-stratified adj-BMI

for (i in 1:length(phe)){
    
    fitM[[i]] <- lm(mdM[[phe[i]]] ~ Veg5yr1 + Age + BMI + AlcoholFreq +
                       PreviousSmoker +CurrentSmoker+ 
                       zTownsend+PCA1 + PCA2 + PCA3 +
                       PCA4 + PCA5, data = mdM, na.action = na.exclude)
    
    fitF[[i]] <- lm(mdF[[phe[i]]] ~ Veg5yr1 + Age + BMI + AlcoholFreq +
                        PreviousSmoker +CurrentSmoker+ 
                        zTownsend+PCA1 + PCA2 + PCA3 +
                        PCA4 + PCA5, data = mdF, na.action = na.exclude)
    
    outM[i,1]=phe[i]
    outF[i,1]=phe[i]
    #Cluster-robust standard errors
    coM=coeftest(fitM[[i]], vcov. = vcovCL, cluster = ~subclass)["Veg5yr1",,drop=FALSE]
    coF=coeftest(fitF[[i]], vcov. = vcovCL, cluster = ~subclass)["Veg5yr1",,drop=FALSE]
    
    outM[i,2]=coM[1]
    outM[i,3]=coM[2]
    outM[i,4]=coM[4]
    outF[i,2]=coF[1]
    outF[i,3]=coF[2]
    outF[i,4]=coF[4]
}

outM=as.data.frame(outM)
outM[2:4]=sapply(outM[2:4], as.numeric)
as_tibble(outM)%>%kbl(caption="Male adj-BMI")%>% 
    scroll_box(width = "600px", height = "200px")
Male adj-BMI
Trait Estimate SE P Sex upper lower Estimateround SEround Pround samp
Apolipoprotein_A_rint -0.1267279 0.0355128 0.0003639 Male -0.0239298 -0.1666409 -0.095 0.036 0.009 687/2798
Apolipoprotein_B_rint -0.1925021 0.0380941 0.0000005 Male -0.1438524 -0.2977183 -0.221 0.039 2.0e-08 745/3014
HDL_cholesterol_rint -0.2083215 0.0348524 0.0000000 Male -0.0921972 -0.2360310 -0.164 0.037 8.0e-06 689/2800
LDL_direct_rint -0.2112578 0.0372037 0.0000000 Male -0.1587854 -0.3077196 -0.233 0.038 9.1e-10 757/3025
Lipoprotein_A_rint 0.0316919 0.0438591 0.4699919 Male 0.1185506 -0.0528610 0.033 0.044 0.453 608/2415
Cholesterol_rint -0.2238045 0.0381212 0.0000000 Male -0.1641620 -0.3152388 -0.240 0.039 5.5e-10 758/3030
Triglycerides_rint 0.2301472 0.0397995 0.0000000 Male 0.2650173 0.0989073 0.182 0.042 1.8e-05 756/3027
IGF_1_rint -0.1502783 0.0361740 0.0000334 Male -0.0762020 -0.2179730 -0.147 0.036 4.9e-05 752/3013
Vitamin_D_rint -0.3567426 0.0442040 0.0000000 Male -0.2527144 -0.4272770 -0.340 0.045 2.9e-14 738/2956
Bioavailable_testosterone_rint -0.0291545 0.0267282 0.2754473 Male 0.0396238 -0.0669537 -0.014 0.027 0.615 679/2761
Free_testosterone_rint -0.0259667 0.0256344 0.3111496 Male 0.0230132 -0.0773468 -0.027 0.026 0.289 678/2767
Testosterone_rint -0.0269085 0.0248827 0.2795819 Male 0.0452305 -0.0560646 -0.005 0.026 0.834 751/2994
SHBG_rint -0.0350940 0.0314007 0.2638066 Male 0.0749213 -0.0579397 0.008 0.034 0.802 682/2783
Alanine_aminotransferase_rint -0.1384570 0.0361785 0.0001318 Male -0.1040710 -0.2532925 -0.179 0.038 2.8e-06 755/3029
Albumin_rint -0.0808145 0.0392909 0.0397775 Male 0.0042956 -0.1494082 -0.073 0.039 0.064 688/2800
Alkaline_phosphatase_rint 0.1032799 0.0370858 0.0053813 Male 0.1647358 0.0189862 0.092 0.037 0.014 758/3030
Aspartate_aminotransferase_rint 0.0002263 0.0378300 0.9952281 Male 0.0606821 -0.0883536 -0.014 0.038 0.716 754/3020
C_reactive_protein_rint 0.0211213 0.0375527 0.5738468 Male 0.0561756 -0.0997715 -0.022 0.040 0.584 750/3023
Gamma_glutamyltransferase_rint -0.1486263 0.0320317 0.0000036 Male -0.1183374 -0.2496618 -0.184 0.034 4.2e-08 757/3026
Direct_bilirubin_rint -0.0102032 0.0400712 0.7990270 Male 0.0802933 -0.0770464 0.002 0.040 0.968 704/2844
Total_bilirubin_rint -0.0133426 0.0365542 0.7151251 Male 0.0667425 -0.0766845 -0.005 0.037 0.892 755/3018
Total_protein_rint -0.1152243 0.0409079 0.0048797 Male -0.0413128 -0.2011475 -0.121 0.041 0.003 689/2802
Creatinine_rint -0.5768706 0.0284864 0.0000000 Male -0.5369832 -0.6495095 -0.593 0.029 5.6e-90 756/3029
Cystatin_C_rint 0.2854492 0.0314230 0.0000000 Male 0.3223879 0.1945069 0.258 0.033 3.0e-15 757/3029
eGFR_rint -0.0019476 0.0322265 0.9518123 Male 0.0924989 -0.0383353 0.027 0.033 0.417 755/3028
HbA1c_rint -0.0396035 0.0386380 0.3054344 Male 0.0185850 -0.1343866 -0.058 0.039 0.138 748/3008
Urate_rint -0.1764115 0.0286627 0.0000000 Male -0.1534806 -0.2716900 -0.213 0.030 2.1e-12 757/3026
Urea_rint -0.6424311 0.0392741 0.0000000 Male -0.5715022 -0.7255184 -0.649 0.039 3.8e-59 757/3029
Calcium_rint -0.1105072 0.0411100 0.0072205 Male -0.0274790 -0.1883462 -0.108 0.041 0.009 687/2802
Phosphate_rint -0.0088235 0.0457907 0.8472122 Male 0.0852632 -0.0938982 -0.004 0.046 0.925 688/2791
outF=as.data.frame(outF)
outF[2:4]=sapply(outF[2:4], as.numeric)
as_tibble(outF)%>%kbl(caption="Female adj-BMI")%>% 
    scroll_box(width = "600px", height = "200px")
Female adj-BMI
Trait Estimate SE P Sex upper lower Estimateround SEround Pround samp
Apolipoprotein_A_rint -0.0507849 0.0254139 0.0457243 Female -0.0030853 -0.1045006 -0.054 0.026 0.038 1324/5223
Apolipoprotein_B_rint -0.1669199 0.0267955 0.0000000 Female -0.1068953 -0.2151413 -0.161 0.028 5.7e-09 1452/5772
HDL_cholesterol_rint -0.1312893 0.0243112 0.0000001 Female -0.0843179 -0.1833018 -0.134 0.025 1.2e-07 1337/5275
LDL_direct_rint -0.1578437 0.0272018 0.0000000 Female -0.0987829 -0.2079128 -0.153 0.028 3.7e-08 1455/5783
Lipoprotein_A_rint -0.1133998 0.0323857 0.0004661 Female -0.0493921 -0.1764010 -0.113 0.032 5.0e-04 1166/4653
Cholesterol_rint -0.1696707 0.0269326 0.0000000 Female -0.1135655 -0.2202265 -0.167 0.027 9.0e-10 1457/5796
Triglycerides_rint 0.2221814 0.0241788 0.0000000 Female 0.2795868 0.1793200 0.229 0.026 3.7e-19 1456/5792
IGF_1_rint -0.2383082 0.0283668 0.0000000 Female -0.1836750 -0.2950142 -0.239 0.028 4.2e-17 1449/5772
Vitamin_D_rint -0.4043642 0.0323105 0.0000000 Female -0.3445001 -0.4720194 -0.408 0.033 9.7e-36 1392/5565
Bioavailable_testosterone_rint -0.0409250 0.0200896 0.0416879 Female 0.0038074 -0.0763184 -0.036 0.020 0.076 1064/4220
Free_testosterone_rint -0.0205851 0.0195642 0.2927619 Female 0.0254453 -0.0545312 -0.015 0.020 0.476 1106/4453
Testosterone_rint -0.0396346 0.0187474 0.0345460 Female 0.0007220 -0.0730163 -0.036 0.019 0.055 1168/4678
SHBG_rint -0.0225707 0.0262092 0.3891722 Female 0.0305016 -0.0803720 -0.025 0.028 0.378 1323/5212
Alanine_aminotransferase_rint -0.1409599 0.0251099 0.0000000 Female -0.0853497 -0.1866737 -0.136 0.026 1.5e-07 1457/5798
Albumin_rint -0.0642046 0.0299417 0.0320438 Female -0.0063468 -0.1251960 -0.066 0.030 0.030 1338/5275
Alkaline_phosphatase_rint 0.1487686 0.0273806 0.0000001 Female 0.2084309 0.0983023 0.153 0.028 4.9e-08 1457/5796
Aspartate_aminotransferase_rint 0.0578253 0.0266927 0.0303181 Female 0.1107021 0.0059728 0.058 0.027 0.029 1452/5772
C_reactive_protein_rint -0.0839135 0.0267741 0.0017306 Female -0.0145333 -0.1313182 -0.073 0.030 0.014 1452/5789
Gamma_glutamyltransferase_rint -0.1478597 0.0270909 0.0000000 Female -0.0880156 -0.1972464 -0.143 0.028 3.2e-07 1455/5793
Direct_bilirubin_rint -0.0154163 0.0315130 0.6247159 Female 0.0435568 -0.0806577 -0.019 0.032 0.558 1184/4716
Total_bilirubin_rint -0.0321196 0.0287448 0.2638580 Female 0.0209609 -0.0921916 -0.036 0.029 0.217 1451/5769
Total_protein_rint -0.0438038 0.0307217 0.1539664 Female 0.0165747 -0.1038486 -0.044 0.031 0.156 1337/5272
Creatinine_rint -0.3668075 0.0220102 0.0000000 Female -0.3214055 -0.4083469 -0.365 0.022 9.8e-60 1456/5793
Cystatin_C_rint 0.3647104 0.0245915 0.0000000 Female 0.4207729 0.3205828 0.371 0.026 5.3e-47 1456/5801
eGFR_rint -0.0407539 0.0243187 0.0938163 Female 0.0034477 -0.0948668 -0.046 0.025 0.068 1455/5792
HbA1c_rint 0.0450948 0.0244823 0.0655259 Female 0.0973950 0.0004831 0.049 0.025 0.048 1450/5774
Urate_rint -0.1543325 0.0218372 0.0000000 Female -0.1028448 -0.1935869 -0.148 0.023 1.6e-10 1452/5791
Urea_rint -0.6000758 0.0280218 0.0000000 Female -0.5432340 -0.6532504 -0.598 0.028 7.8e-98 1455/5792
Calcium_rint -0.0596322 0.0313444 0.0571512 Female 0.0016324 -0.1213241 -0.060 0.031 0.056 1338/5270
Phosphate_rint 0.0214230 0.0295982 0.4692184 Female 0.0789402 -0.0376678 0.021 0.030 0.488 1335/5264
outM$Sex="Male"
outF$Sex="Female"

Add confidence intervals and round long decimals: sex-stratified adj-BMI

#95CI
outM$upper=outM$Estimate+1.96*outM$SE
outM$lower=outM$Estimate-1.96*outM$SE
outM$Estimateround=format(round(outM$Estimate, digits=3), digits=3)
outM$SEround=format(round(outM$SE, digits=3), digits=3)
outM$Pround=ifelse(outM$P < 0.001, 
                  formatC(outM$P, digits=1, format="e"), 
                  formatC(outM$P, digits=3, format="f"))

outF$upper=outF$Estimate+1.96*outF$SE
outF$lower=outF$Estimate-1.96*outF$SE
outF$Estimateround=format(round(outF$Estimate, digits=3), digits=3)
outM$SEround=format(round(outM$SE, digits=3), digits=3)
outF$Pround=ifelse(outF$P < 0.001, 
                   formatC(outF$P, digits=1, format="e"), 
                   formatC(outF$P, digits=3, format="f"))

Get sample sizes sex-stratified adj-BMI

ncontrM=md%>%filter(Veg5yr1==0, Sex==1)%>%select(all_of(phe))%>%sapply(function(x) sum(!is.na(x)))
ncaseM=md%>%filter(Veg5yr1==1, Sex==1)%>%select(all_of(phe))%>%sapply(function(x) sum(!is.na(x)))
ncontrF=md%>%filter(Veg5yr1==0, Sex==2)%>%select(all_of(phe))%>%sapply(function(x) sum(!is.na(x)))
ncaseF=md%>%filter(Veg5yr1==1, Sex==2)%>%select(all_of(phe))%>%sapply(function(x) sum(!is.na(x)))

outM$samp=paste(ncaseM,ncontrM, sep= "/")
outF$samp=paste(ncaseF,ncontrF, sep= "/")
tot=rbind(outM, outF)

Forest plots sex-stratified adj-BMI

tot$Trait=gsub(out$Trait, pattern = "_", replacement =" ") 
tot$Trait=gsub(out$Trait, pattern =" rint", replacement="") 
tot%>%kbl()%>% 
    scroll_box(width = "600px", height = "200px")
Trait Estimate SE P Sex upper lower Estimateround SEround Pround samp
Apolipoprotein A -0.1267279 0.0355128 0.0003639 Male -0.0571227 -0.1963331 -0.127 0.036 3.6e-04 687/2798
Apolipoprotein B -0.1925021 0.0380941 0.0000005 Male -0.1178377 -0.2671664 -0.193 0.038 4.5e-07 745/3014
HDL cholesterol -0.2083215 0.0348524 0.0000000 Male -0.1400107 -0.2766323 -0.208 0.035 2.5e-09 689/2800
LDL direct -0.2112578 0.0372037 0.0000000 Male -0.1383386 -0.2841771 -0.211 0.037 1.5e-08 757/3025
Lipoprotein A 0.0316919 0.0438591 0.4699919 Male 0.1176557 -0.0542720 0.032 0.044 0.470 608/2415
Cholesterol -0.2238045 0.0381212 0.0000000 Male -0.1490870 -0.2985221 -0.224 0.038 4.7e-09 758/3030
Triglycerides 0.2301472 0.0397995 0.0000000 Male 0.3081542 0.1521403 0.230 0.040 7.9e-09 756/3027
IGF 1 -0.1502783 0.0361740 0.0000334 Male -0.0793773 -0.2211794 -0.150 0.036 3.3e-05 752/3013
Vitamin D -0.3567426 0.0442040 0.0000000 Male -0.2701027 -0.4433824 -0.357 0.044 9.4e-16 738/2956
Bioavailable testosterone -0.0291545 0.0267282 0.2754473 Male 0.0232327 -0.0815417 -0.029 0.027 0.275 679/2761
Free testosterone -0.0259667 0.0256344 0.3111496 Male 0.0242768 -0.0762102 -0.026 0.026 0.311 678/2767
Testosterone -0.0269085 0.0248827 0.2795819 Male 0.0218615 -0.0756785 -0.027 0.025 0.280 751/2994
SHBG -0.0350940 0.0314007 0.2638066 Male 0.0264513 -0.0966393 -0.035 0.031 0.264 682/2783
Alanine aminotransferase -0.1384570 0.0361785 0.0001318 Male -0.0675471 -0.2093669 -0.138 0.036 1.3e-04 755/3029
Albumin -0.0808145 0.0392909 0.0397775 Male -0.0038044 -0.1578245 -0.081 0.039 0.040 688/2800
Alkaline phosphatase 0.1032799 0.0370858 0.0053813 Male 0.1759681 0.0305916 0.103 0.037 0.005 758/3030
Aspartate aminotransferase 0.0002263 0.0378300 0.9952281 Male 0.0743731 -0.0739205 0.000 0.038 0.995 754/3020
C reactive protein 0.0211213 0.0375527 0.5738468 Male 0.0947246 -0.0524820 0.021 0.038 0.574 750/3023
Gamma glutamyltransferase -0.1486263 0.0320317 0.0000036 Male -0.0858441 -0.2114084 -0.149 0.032 3.6e-06 757/3026
Direct bilirubin -0.0102032 0.0400712 0.7990270 Male 0.0683364 -0.0887427 -0.010 0.040 0.799 704/2844
Total bilirubin -0.0133426 0.0365542 0.7151251 Male 0.0583035 -0.0849887 -0.013 0.037 0.715 755/3018
Total protein -0.1152243 0.0409079 0.0048797 Male -0.0350449 -0.1954037 -0.115 0.041 0.005 689/2802
Creatinine -0.5768706 0.0284864 0.0000000 Male -0.5210372 -0.6327040 -0.577 0.028 1.2e-86 756/3029
Cystatin C 0.2854492 0.0314230 0.0000000 Male 0.3470384 0.2238601 0.285 0.031 1.6e-19 757/3029
eGFR -0.0019476 0.0322265 0.9518123 Male 0.0612163 -0.0651116 -0.002 0.032 0.952 755/3028
HbA1c -0.0396035 0.0386380 0.3054344 Male 0.0361269 -0.1153340 -0.040 0.039 0.305 748/3008
Urate -0.1764115 0.0286627 0.0000000 Male -0.1202326 -0.2325903 -0.176 0.029 8.3e-10 757/3026
Urea -0.6424311 0.0392741 0.0000000 Male -0.5654539 -0.7194083 -0.642 0.039 3.7e-58 757/3029
Calcium -0.1105072 0.0411100 0.0072205 Male -0.0299317 -0.1910827 -0.111 0.041 0.007 687/2802
Phosphate -0.0088235 0.0457907 0.8472122 Male 0.0809263 -0.0985732 -0.009 0.046 0.847 688/2791
Apolipoprotein A -0.0507849 0.0254139 0.0457243 Female -0.0009736 -0.1005962 -0.051 0.026 0.046 1324/5223
Apolipoprotein B -0.1669199 0.0267955 0.0000000 Female -0.1144007 -0.2194391 -0.167 0.028 4.9e-10 1452/5772
HDL cholesterol -0.1312893 0.0243112 0.0000001 Female -0.0836394 -0.1789392 -0.131 0.025 6.9e-08 1337/5275
LDL direct -0.1578437 0.0272018 0.0000000 Female -0.1045282 -0.2111593 -0.158 0.028 6.8e-09 1455/5783
Lipoprotein A -0.1133998 0.0323857 0.0004661 Female -0.0499237 -0.1768758 -0.113 0.032 4.7e-04 1166/4653
Cholesterol -0.1696707 0.0269326 0.0000000 Female -0.1168828 -0.2224585 -0.170 0.027 3.2e-10 1457/5796
Triglycerides 0.2221814 0.0241788 0.0000000 Female 0.2695719 0.1747909 0.222 0.026 5.1e-20 1456/5792
IGF 1 -0.2383082 0.0283668 0.0000000 Female -0.1827092 -0.2939072 -0.238 0.028 5.3e-17 1449/5772
Vitamin D -0.4043642 0.0323105 0.0000000 Female -0.3410358 -0.4676927 -0.404 0.033 1.5e-35 1392/5565
Bioavailable testosterone -0.0409250 0.0200896 0.0416879 Female -0.0015493 -0.0803006 -0.041 0.020 0.042 1064/4220
Free testosterone -0.0205851 0.0195642 0.2927619 Female 0.0177607 -0.0589308 -0.021 0.020 0.293 1106/4453
Testosterone -0.0396346 0.0187474 0.0345460 Female -0.0028896 -0.0763795 -0.040 0.019 0.035 1168/4678
SHBG -0.0225707 0.0262092 0.3891722 Female 0.0287992 -0.0739407 -0.023 0.028 0.389 1323/5212
Alanine aminotransferase -0.1409599 0.0251099 0.0000000 Female -0.0917446 -0.1901753 -0.141 0.026 2.1e-08 1457/5798
Albumin -0.0642046 0.0299417 0.0320438 Female -0.0055188 -0.1228904 -0.064 0.030 0.032 1338/5275
Alkaline phosphatase 0.1487686 0.0273806 0.0000001 Female 0.2024345 0.0951027 0.149 0.028 5.7e-08 1457/5796
Aspartate aminotransferase 0.0578253 0.0266927 0.0303181 Female 0.1101429 0.0055077 0.058 0.027 0.030 1452/5772
C reactive protein -0.0839135 0.0267741 0.0017306 Female -0.0314363 -0.1363908 -0.084 0.030 0.002 1452/5789
Gamma glutamyltransferase -0.1478597 0.0270909 0.0000000 Female -0.0947615 -0.2009578 -0.148 0.028 5.0e-08 1455/5793
Direct bilirubin -0.0154163 0.0315130 0.6247159 Female 0.0463492 -0.0771817 -0.015 0.032 0.625 1184/4716
Total bilirubin -0.0321196 0.0287448 0.2638580 Female 0.0242202 -0.0884595 -0.032 0.029 0.264 1451/5769
Total protein -0.0438038 0.0307217 0.1539664 Female 0.0164108 -0.1040184 -0.044 0.031 0.154 1337/5272
Creatinine -0.3668075 0.0220102 0.0000000 Female -0.3236674 -0.4099476 -0.367 0.022 3.2e-61 1456/5793
Cystatin C 0.3647104 0.0245915 0.0000000 Female 0.4129098 0.3165110 0.365 0.026 4.8e-49 1456/5801
eGFR -0.0407539 0.0243187 0.0938163 Female 0.0069109 -0.0884186 -0.041 0.025 0.094 1455/5792
HbA1c 0.0450948 0.0244823 0.0655259 Female 0.0930800 -0.0028905 0.045 0.025 0.066 1450/5774
Urate -0.1543325 0.0218372 0.0000000 Female -0.1115317 -0.1971334 -0.154 0.023 1.7e-12 1452/5791
Urea -0.6000758 0.0280218 0.0000000 Female -0.5451531 -0.6549985 -0.600 0.028 1.1e-98 1455/5792
Calcium -0.0596322 0.0313444 0.0571512 Female 0.0018028 -0.1210672 -0.060 0.031 0.057 1338/5270
Phosphate 0.0214230 0.0295982 0.4692184 Female 0.0794355 -0.0365895 0.021 0.030 0.469 1335/5264
tot$EstimateroundM=tot$Estimateround[tot$Sex=="Male"] 
tot$EstimateroundF=tot$Estimateround[tot$Sex=="Female"] 
tot$SEroundM=tot$SEround[tot$Sex=="Male"]
tot$SEroundF=tot$SEround[tot$Sex=="Female"]
tot$ProundM=tot$Pround[tot$Sex=="Male"] 
tot$ProundF=tot$Pround[tot$Sex=="Female"] 
tot$sampM=tot$samp[tot$Sex=="Male"] 
tot$sampF=tot$samp[tot$Sex=="Female"]

tot%>%kbl()%>% 
    scroll_box(width = "600px", height = "200px")
Trait Estimate SE P Sex upper lower Estimateround SEround Pround samp EstimateroundM EstimateroundF SEroundM SEroundF ProundM ProundF sampM sampF
Apolipoprotein A -0.1267279 0.0355128 0.0003639 Male -0.0571227 -0.1963331 -0.127 0.036 3.6e-04 687/2798 -0.127 -0.051 0.036 0.026 3.6e-04 0.046 687/2798 1324/5223
Apolipoprotein B -0.1925021 0.0380941 0.0000005 Male -0.1178377 -0.2671664 -0.193 0.038 4.5e-07 745/3014 -0.193 -0.167 0.038 0.028 4.5e-07 4.9e-10 745/3014 1452/5772
HDL cholesterol -0.2083215 0.0348524 0.0000000 Male -0.1400107 -0.2766323 -0.208 0.035 2.5e-09 689/2800 -0.208 -0.131 0.035 0.025 2.5e-09 6.9e-08 689/2800 1337/5275
LDL direct -0.2112578 0.0372037 0.0000000 Male -0.1383386 -0.2841771 -0.211 0.037 1.5e-08 757/3025 -0.211 -0.158 0.037 0.028 1.5e-08 6.8e-09 757/3025 1455/5783
Lipoprotein A 0.0316919 0.0438591 0.4699919 Male 0.1176557 -0.0542720 0.032 0.044 0.470 608/2415 0.032 -0.113 0.044 0.032 0.470 4.7e-04 608/2415 1166/4653
Cholesterol -0.2238045 0.0381212 0.0000000 Male -0.1490870 -0.2985221 -0.224 0.038 4.7e-09 758/3030 -0.224 -0.170 0.038 0.027 4.7e-09 3.2e-10 758/3030 1457/5796
Triglycerides 0.2301472 0.0397995 0.0000000 Male 0.3081542 0.1521403 0.230 0.040 7.9e-09 756/3027 0.230 0.222 0.040 0.026 7.9e-09 5.1e-20 756/3027 1456/5792
IGF 1 -0.1502783 0.0361740 0.0000334 Male -0.0793773 -0.2211794 -0.150 0.036 3.3e-05 752/3013 -0.150 -0.238 0.036 0.028 3.3e-05 5.3e-17 752/3013 1449/5772
Vitamin D -0.3567426 0.0442040 0.0000000 Male -0.2701027 -0.4433824 -0.357 0.044 9.4e-16 738/2956 -0.357 -0.404 0.044 0.033 9.4e-16 1.5e-35 738/2956 1392/5565
Bioavailable testosterone -0.0291545 0.0267282 0.2754473 Male 0.0232327 -0.0815417 -0.029 0.027 0.275 679/2761 -0.029 -0.041 0.027 0.020 0.275 0.042 679/2761 1064/4220
Free testosterone -0.0259667 0.0256344 0.3111496 Male 0.0242768 -0.0762102 -0.026 0.026 0.311 678/2767 -0.026 -0.021 0.026 0.020 0.311 0.293 678/2767 1106/4453
Testosterone -0.0269085 0.0248827 0.2795819 Male 0.0218615 -0.0756785 -0.027 0.025 0.280 751/2994 -0.027 -0.040 0.025 0.019 0.280 0.035 751/2994 1168/4678
SHBG -0.0350940 0.0314007 0.2638066 Male 0.0264513 -0.0966393 -0.035 0.031 0.264 682/2783 -0.035 -0.023 0.031 0.028 0.264 0.389 682/2783 1323/5212
Alanine aminotransferase -0.1384570 0.0361785 0.0001318 Male -0.0675471 -0.2093669 -0.138 0.036 1.3e-04 755/3029 -0.138 -0.141 0.036 0.026 1.3e-04 2.1e-08 755/3029 1457/5798
Albumin -0.0808145 0.0392909 0.0397775 Male -0.0038044 -0.1578245 -0.081 0.039 0.040 688/2800 -0.081 -0.064 0.039 0.030 0.040 0.032 688/2800 1338/5275
Alkaline phosphatase 0.1032799 0.0370858 0.0053813 Male 0.1759681 0.0305916 0.103 0.037 0.005 758/3030 0.103 0.149 0.037 0.028 0.005 5.7e-08 758/3030 1457/5796
Aspartate aminotransferase 0.0002263 0.0378300 0.9952281 Male 0.0743731 -0.0739205 0.000 0.038 0.995 754/3020 0.000 0.058 0.038 0.027 0.995 0.030 754/3020 1452/5772
C reactive protein 0.0211213 0.0375527 0.5738468 Male 0.0947246 -0.0524820 0.021 0.038 0.574 750/3023 0.021 -0.084 0.038 0.030 0.574 0.002 750/3023 1452/5789
Gamma glutamyltransferase -0.1486263 0.0320317 0.0000036 Male -0.0858441 -0.2114084 -0.149 0.032 3.6e-06 757/3026 -0.149 -0.148 0.032 0.028 3.6e-06 5.0e-08 757/3026 1455/5793
Direct bilirubin -0.0102032 0.0400712 0.7990270 Male 0.0683364 -0.0887427 -0.010 0.040 0.799 704/2844 -0.010 -0.015 0.040 0.032 0.799 0.625 704/2844 1184/4716
Total bilirubin -0.0133426 0.0365542 0.7151251 Male 0.0583035 -0.0849887 -0.013 0.037 0.715 755/3018 -0.013 -0.032 0.037 0.029 0.715 0.264 755/3018 1451/5769
Total protein -0.1152243 0.0409079 0.0048797 Male -0.0350449 -0.1954037 -0.115 0.041 0.005 689/2802 -0.115 -0.044 0.041 0.031 0.005 0.154 689/2802 1337/5272
Creatinine -0.5768706 0.0284864 0.0000000 Male -0.5210372 -0.6327040 -0.577 0.028 1.2e-86 756/3029 -0.577 -0.367 0.028 0.022 1.2e-86 3.2e-61 756/3029 1456/5793
Cystatin C 0.2854492 0.0314230 0.0000000 Male 0.3470384 0.2238601 0.285 0.031 1.6e-19 757/3029 0.285 0.365 0.031 0.026 1.6e-19 4.8e-49 757/3029 1456/5801
eGFR -0.0019476 0.0322265 0.9518123 Male 0.0612163 -0.0651116 -0.002 0.032 0.952 755/3028 -0.002 -0.041 0.032 0.025 0.952 0.094 755/3028 1455/5792
HbA1c -0.0396035 0.0386380 0.3054344 Male 0.0361269 -0.1153340 -0.040 0.039 0.305 748/3008 -0.040 0.045 0.039 0.025 0.305 0.066 748/3008 1450/5774
Urate -0.1764115 0.0286627 0.0000000 Male -0.1202326 -0.2325903 -0.176 0.029 8.3e-10 757/3026 -0.176 -0.154 0.029 0.023 8.3e-10 1.7e-12 757/3026 1452/5791
Urea -0.6424311 0.0392741 0.0000000 Male -0.5654539 -0.7194083 -0.642 0.039 3.7e-58 757/3029 -0.642 -0.600 0.039 0.028 3.7e-58 1.1e-98 757/3029 1455/5792
Calcium -0.1105072 0.0411100 0.0072205 Male -0.0299317 -0.1910827 -0.111 0.041 0.007 687/2802 -0.111 -0.060 0.041 0.031 0.007 0.057 687/2802 1338/5270
Phosphate -0.0088235 0.0457907 0.8472122 Male 0.0809263 -0.0985732 -0.009 0.046 0.847 688/2791 -0.009 0.021 0.046 0.030 0.847 0.469 688/2791 1335/5264
Apolipoprotein A -0.0507849 0.0254139 0.0457243 Female -0.0009736 -0.1005962 -0.051 0.026 0.046 1324/5223 -0.127 -0.051 0.036 0.026 3.6e-04 0.046 687/2798 1324/5223
Apolipoprotein B -0.1669199 0.0267955 0.0000000 Female -0.1144007 -0.2194391 -0.167 0.028 4.9e-10 1452/5772 -0.193 -0.167 0.038 0.028 4.5e-07 4.9e-10 745/3014 1452/5772
HDL cholesterol -0.1312893 0.0243112 0.0000001 Female -0.0836394 -0.1789392 -0.131 0.025 6.9e-08 1337/5275 -0.208 -0.131 0.035 0.025 2.5e-09 6.9e-08 689/2800 1337/5275
LDL direct -0.1578437 0.0272018 0.0000000 Female -0.1045282 -0.2111593 -0.158 0.028 6.8e-09 1455/5783 -0.211 -0.158 0.037 0.028 1.5e-08 6.8e-09 757/3025 1455/5783
Lipoprotein A -0.1133998 0.0323857 0.0004661 Female -0.0499237 -0.1768758 -0.113 0.032 4.7e-04 1166/4653 0.032 -0.113 0.044 0.032 0.470 4.7e-04 608/2415 1166/4653
Cholesterol -0.1696707 0.0269326 0.0000000 Female -0.1168828 -0.2224585 -0.170 0.027 3.2e-10 1457/5796 -0.224 -0.170 0.038 0.027 4.7e-09 3.2e-10 758/3030 1457/5796
Triglycerides 0.2221814 0.0241788 0.0000000 Female 0.2695719 0.1747909 0.222 0.026 5.1e-20 1456/5792 0.230 0.222 0.040 0.026 7.9e-09 5.1e-20 756/3027 1456/5792
IGF 1 -0.2383082 0.0283668 0.0000000 Female -0.1827092 -0.2939072 -0.238 0.028 5.3e-17 1449/5772 -0.150 -0.238 0.036 0.028 3.3e-05 5.3e-17 752/3013 1449/5772
Vitamin D -0.4043642 0.0323105 0.0000000 Female -0.3410358 -0.4676927 -0.404 0.033 1.5e-35 1392/5565 -0.357 -0.404 0.044 0.033 9.4e-16 1.5e-35 738/2956 1392/5565
Bioavailable testosterone -0.0409250 0.0200896 0.0416879 Female -0.0015493 -0.0803006 -0.041 0.020 0.042 1064/4220 -0.029 -0.041 0.027 0.020 0.275 0.042 679/2761 1064/4220
Free testosterone -0.0205851 0.0195642 0.2927619 Female 0.0177607 -0.0589308 -0.021 0.020 0.293 1106/4453 -0.026 -0.021 0.026 0.020 0.311 0.293 678/2767 1106/4453
Testosterone -0.0396346 0.0187474 0.0345460 Female -0.0028896 -0.0763795 -0.040 0.019 0.035 1168/4678 -0.027 -0.040 0.025 0.019 0.280 0.035 751/2994 1168/4678
SHBG -0.0225707 0.0262092 0.3891722 Female 0.0287992 -0.0739407 -0.023 0.028 0.389 1323/5212 -0.035 -0.023 0.031 0.028 0.264 0.389 682/2783 1323/5212
Alanine aminotransferase -0.1409599 0.0251099 0.0000000 Female -0.0917446 -0.1901753 -0.141 0.026 2.1e-08 1457/5798 -0.138 -0.141 0.036 0.026 1.3e-04 2.1e-08 755/3029 1457/5798
Albumin -0.0642046 0.0299417 0.0320438 Female -0.0055188 -0.1228904 -0.064 0.030 0.032 1338/5275 -0.081 -0.064 0.039 0.030 0.040 0.032 688/2800 1338/5275
Alkaline phosphatase 0.1487686 0.0273806 0.0000001 Female 0.2024345 0.0951027 0.149 0.028 5.7e-08 1457/5796 0.103 0.149 0.037 0.028 0.005 5.7e-08 758/3030 1457/5796
Aspartate aminotransferase 0.0578253 0.0266927 0.0303181 Female 0.1101429 0.0055077 0.058 0.027 0.030 1452/5772 0.000 0.058 0.038 0.027 0.995 0.030 754/3020 1452/5772
C reactive protein -0.0839135 0.0267741 0.0017306 Female -0.0314363 -0.1363908 -0.084 0.030 0.002 1452/5789 0.021 -0.084 0.038 0.030 0.574 0.002 750/3023 1452/5789
Gamma glutamyltransferase -0.1478597 0.0270909 0.0000000 Female -0.0947615 -0.2009578 -0.148 0.028 5.0e-08 1455/5793 -0.149 -0.148 0.032 0.028 3.6e-06 5.0e-08 757/3026 1455/5793
Direct bilirubin -0.0154163 0.0315130 0.6247159 Female 0.0463492 -0.0771817 -0.015 0.032 0.625 1184/4716 -0.010 -0.015 0.040 0.032 0.799 0.625 704/2844 1184/4716
Total bilirubin -0.0321196 0.0287448 0.2638580 Female 0.0242202 -0.0884595 -0.032 0.029 0.264 1451/5769 -0.013 -0.032 0.037 0.029 0.715 0.264 755/3018 1451/5769
Total protein -0.0438038 0.0307217 0.1539664 Female 0.0164108 -0.1040184 -0.044 0.031 0.154 1337/5272 -0.115 -0.044 0.041 0.031 0.005 0.154 689/2802 1337/5272
Creatinine -0.3668075 0.0220102 0.0000000 Female -0.3236674 -0.4099476 -0.367 0.022 3.2e-61 1456/5793 -0.577 -0.367 0.028 0.022 1.2e-86 3.2e-61 756/3029 1456/5793
Cystatin C 0.3647104 0.0245915 0.0000000 Female 0.4129098 0.3165110 0.365 0.026 4.8e-49 1456/5801 0.285 0.365 0.031 0.026 1.6e-19 4.8e-49 757/3029 1456/5801
eGFR -0.0407539 0.0243187 0.0938163 Female 0.0069109 -0.0884186 -0.041 0.025 0.094 1455/5792 -0.002 -0.041 0.032 0.025 0.952 0.094 755/3028 1455/5792
HbA1c 0.0450948 0.0244823 0.0655259 Female 0.0930800 -0.0028905 0.045 0.025 0.066 1450/5774 -0.040 0.045 0.039 0.025 0.305 0.066 748/3008 1450/5774
Urate -0.1543325 0.0218372 0.0000000 Female -0.1115317 -0.1971334 -0.154 0.023 1.7e-12 1452/5791 -0.176 -0.154 0.029 0.023 8.3e-10 1.7e-12 757/3026 1452/5791
Urea -0.6000758 0.0280218 0.0000000 Female -0.5451531 -0.6549985 -0.600 0.028 1.1e-98 1455/5792 -0.642 -0.600 0.039 0.028 3.7e-58 1.1e-98 757/3029 1455/5792
Calcium -0.0596322 0.0313444 0.0571512 Female 0.0018028 -0.1210672 -0.060 0.031 0.057 1338/5270 -0.111 -0.060 0.041 0.031 0.007 0.057 687/2802 1338/5270
Phosphate 0.0214230 0.0295982 0.4692184 Female 0.0794355 -0.0365895 0.021 0.030 0.469 1335/5264 -0.009 0.021 0.046 0.030 0.847 0.469 688/2791 1335/5264
tot=tot%>%select(Trait, EstimateroundM, ProundM, 
                 sampM, EstimateroundF, ProundF, sampF, everything())

tot=cbind(Category, tot)
headerM=tibble(Category="Category",
               Trait="Trait",
               Sex="Male",
               ProundM="P (male)",
               ProundF="P (female)")
headerF=tibble(Category="Category",
               Trait="Trait",
               Sex="Female",
               ProundM="P (male)",
               ProundF="P (female)")
tot2=bind_rows(headerM, tot) 
tot3=bind_rows(headerF, tot2) 

#png("forest.sexstrat-BMI.png",width = 12, height =8, units = 'in', res = 600)

tot3 %>% group_by(Sex) %>% 
    forestplot(mean="Estimate", labeltext=c("Category", "Trait", "ProundM", "ProundF"), 
               lower="lower", upper="upper", 
               boxsize = 0.2, 
               ci.vertices = TRUE, 
               ci.vertices.height = 0.05, 
               shapes_gp = fpShapesGp(box = c("darkred", "blue") %>% 
                    lapply(function(x) gpar(fill = x, col = "#555555")), 
                default = gpar(vertices = TRUE)), 
               hrzl_lines = list("2" = gpar(lty = 2)), 
               grid=TRUE, 
               zero = 0, 
               xticks=xtik, 
               mar = unit(c(0,0.5,0.2,0.5), "in"),
               xlab = "Standardized veg. effect ± 95% CI", 
               txt_gp = fpTxtGp(xlab  = gpar(cex = 1),
                                ticks = gpar(fontfamily = "", cex = 1)
               ),
               fn.ci_norm = c(fpDrawNormalCI, fpDrawCircleCI)
    )%>% fp_set_zebra_style("#F7F7F7")

#dev.off()

outplot=tot%>%select(Trait, EstimateroundM, SEroundM, ProundM, sampM,
                 EstimateroundF, SEroundF, ProundF, sampF)
outplot$ProundM = cell_spec(outplot$ProundM, 
            color=ifelse(as.double(outplot$ProundM) <(0.05/30), "#36E072", "grey"))

outplot$ProundF = cell_spec(outplot$ProundF, 
            color=ifelse(as.double(outplot$ProundF) <(0.05/30), "#36E072", "grey"))

colnames(outplot)=c("Trait", "BetaVegm", "SEm", "Pm", "N (veg/non-veg)m","BetaVegf", "SEf", "Pf", "N (veg/non-veg)f")

outplot%>%
     kbl(booktabs = T, linesep = "", escape=FALSE,align="l") %>%
  kable_classic_2(full_width = F)
Trait BetaVegm SEm Pm N (veg/non-veg)m BetaVegf SEf Pf N (veg/non-veg)f
Apolipoprotein A -0.127 0.036 3.6e-04 687/2798 -0.051 0.026 0.046 1324/5223
Apolipoprotein B -0.193 0.038 4.5e-07 745/3014 -0.167 0.028 4.9e-10 1452/5772
HDL cholesterol -0.208 0.035 2.5e-09 689/2800 -0.131 0.025 6.9e-08 1337/5275
LDL direct -0.211 0.037 1.5e-08 757/3025 -0.158 0.028 6.8e-09 1455/5783
Lipoprotein A 0.032 0.044 0.470 608/2415 -0.113 0.032 4.7e-04 1166/4653
Cholesterol -0.224 0.038 4.7e-09 758/3030 -0.170 0.027 3.2e-10 1457/5796
Triglycerides 0.230 0.040 7.9e-09 756/3027 0.222 0.026 5.1e-20 1456/5792
IGF 1 -0.150 0.036 3.3e-05 752/3013 -0.238 0.028 5.3e-17 1449/5772
Vitamin D -0.357 0.044 9.4e-16 738/2956 -0.404 0.033 1.5e-35 1392/5565
Bioavailable testosterone -0.029 0.027 0.275 679/2761 -0.041 0.020 0.042 1064/4220
Free testosterone -0.026 0.026 0.311 678/2767 -0.021 0.020 0.293 1106/4453
Testosterone -0.027 0.025 0.280 751/2994 -0.040 0.019 0.035 1168/4678
SHBG -0.035 0.031 0.264 682/2783 -0.023 0.028 0.389 1323/5212
Alanine aminotransferase -0.138 0.036 1.3e-04 755/3029 -0.141 0.026 2.1e-08 1457/5798
Albumin -0.081 0.039 0.040 688/2800 -0.064 0.030 0.032 1338/5275
Alkaline phosphatase 0.103 0.037 0.005 758/3030 0.149 0.028 5.7e-08 1457/5796
Aspartate aminotransferase 0.000 0.038 0.995 754/3020 0.058 0.027 0.030 1452/5772
C reactive protein 0.021 0.038 0.574 750/3023 -0.084 0.030 0.002 1452/5789
Gamma glutamyltransferase -0.149 0.032 3.6e-06 757/3026 -0.148 0.028 5.0e-08 1455/5793
Direct bilirubin -0.010 0.040 0.799 704/2844 -0.015 0.032 0.625 1184/4716
Total bilirubin -0.013 0.037 0.715 755/3018 -0.032 0.029 0.264 1451/5769
Total protein -0.115 0.041 0.005 689/2802 -0.044 0.031 0.154 1337/5272
Creatinine -0.577 0.028 1.2e-86 756/3029 -0.367 0.022 3.2e-61 1456/5793
Cystatin C 0.285 0.031 1.6e-19 757/3029 0.365 0.026 4.8e-49 1456/5801
eGFR -0.002 0.032 0.952 755/3028 -0.041 0.025 0.094 1455/5792
HbA1c -0.040 0.039 0.305 748/3008 0.045 0.025 0.066 1450/5774
Urate -0.176 0.029 8.3e-10 757/3026 -0.154 0.023 1.7e-12 1452/5791
Urea -0.642 0.039 3.7e-58 757/3029 -0.600 0.028 1.1e-98 1455/5792
Calcium -0.111 0.041 0.007 687/2802 -0.060 0.031 0.057 1338/5270
Phosphate -0.009 0.046 0.847 688/2791 0.021 0.030 0.469 1335/5264
Apolipoprotein A -0.127 0.036 3.6e-04 687/2798 -0.051 0.026 0.046 1324/5223
Apolipoprotein B -0.193 0.038 4.5e-07 745/3014 -0.167 0.028 4.9e-10 1452/5772
HDL cholesterol -0.208 0.035 2.5e-09 689/2800 -0.131 0.025 6.9e-08 1337/5275
LDL direct -0.211 0.037 1.5e-08 757/3025 -0.158 0.028 6.8e-09 1455/5783
Lipoprotein A 0.032 0.044 0.470 608/2415 -0.113 0.032 4.7e-04 1166/4653
Cholesterol -0.224 0.038 4.7e-09 758/3030 -0.170 0.027 3.2e-10 1457/5796
Triglycerides 0.230 0.040 7.9e-09 756/3027 0.222 0.026 5.1e-20 1456/5792
IGF 1 -0.150 0.036 3.3e-05 752/3013 -0.238 0.028 5.3e-17 1449/5772
Vitamin D -0.357 0.044 9.4e-16 738/2956 -0.404 0.033 1.5e-35 1392/5565
Bioavailable testosterone -0.029 0.027 0.275 679/2761 -0.041 0.020 0.042 1064/4220
Free testosterone -0.026 0.026 0.311 678/2767 -0.021 0.020 0.293 1106/4453
Testosterone -0.027 0.025 0.280 751/2994 -0.040 0.019 0.035 1168/4678
SHBG -0.035 0.031 0.264 682/2783 -0.023 0.028 0.389 1323/5212
Alanine aminotransferase -0.138 0.036 1.3e-04 755/3029 -0.141 0.026 2.1e-08 1457/5798
Albumin -0.081 0.039 0.040 688/2800 -0.064 0.030 0.032 1338/5275
Alkaline phosphatase 0.103 0.037 0.005 758/3030 0.149 0.028 5.7e-08 1457/5796
Aspartate aminotransferase 0.000 0.038 0.995 754/3020 0.058 0.027 0.030 1452/5772
C reactive protein 0.021 0.038 0.574 750/3023 -0.084 0.030 0.002 1452/5789
Gamma glutamyltransferase -0.149 0.032 3.6e-06 757/3026 -0.148 0.028 5.0e-08 1455/5793
Direct bilirubin -0.010 0.040 0.799 704/2844 -0.015 0.032 0.625 1184/4716
Total bilirubin -0.013 0.037 0.715 755/3018 -0.032 0.029 0.264 1451/5769
Total protein -0.115 0.041 0.005 689/2802 -0.044 0.031 0.154 1337/5272
Creatinine -0.577 0.028 1.2e-86 756/3029 -0.367 0.022 3.2e-61 1456/5793
Cystatin C 0.285 0.031 1.6e-19 757/3029 0.365 0.026 4.8e-49 1456/5801
eGFR -0.002 0.032 0.952 755/3028 -0.041 0.025 0.094 1455/5792
HbA1c -0.040 0.039 0.305 748/3008 0.045 0.025 0.066 1450/5774
Urate -0.176 0.029 8.3e-10 757/3026 -0.154 0.023 1.7e-12 1452/5791
Urea -0.642 0.039 3.7e-58 757/3029 -0.600 0.028 1.1e-98 1455/5792
Calcium -0.111 0.041 0.007 687/2802 -0.060 0.031 0.057 1338/5270
Phosphate -0.009 0.046 0.847 688/2791 0.021 0.030 0.469 1335/5264
#%>%save_kable(file = "table.forest.sexstrat-BMI.png", zoom=5)