Home > Software engineering >  How to calculate and plot odds-ratios and their standard errors from a logistic regression in R?
How to calculate and plot odds-ratios and their standard errors from a logistic regression in R?

Time:07-26

I am looking for a way to exactly reproduce this figure:

enter image description here

According to the authors of the plot it shows: "the odds ratio estimated from logistic regression (error bars indicate standard error of the estimate; reference decile was set to decile 5)"

I have a table with the scores, but I am not sure how to calculate the odds-ratios. I have segregated data into deciles based on this score. I have calculated a linear model, but I am not sure of two things:

  1. How to compare the odds-ratios against decile 5.
  2. How to plot the odds-ratios with error bars that indicate standard error of the estimate.

So far, I have tried the following:

prs_table <- prs_table %>%
  mutate(decile = ntile(`Standardized PRS`, 10),
         decile = as.factor(decile),
         `Case control status` = as.factor(`Case control status`))

prs_glm <- glm(`Case control status` ~ decile, data=prs_table, family='binomial') 

Here is a dput() output of 500 samples if you would like to test using the same data:

    structure(list(Cohort = c("clinical", "clinical", "DTR", "clinical", 
"clinical", "clinical", "clinical", "clinical", "clinical", "DTR", 
"clinical", "clinical", "DTR", "clinical", "DTR", "clinical", 
"clinical", "clinical", "DTR", "DTR", "clinical", "clinical", 
"clinical", "DTR", "clinical", "clinical", "DTR", "DTR", "DTR", 
"clinical", "DTR", "DTR", "DTR", "clinical", "clinical", "DTR", 
"clinical", "DTR", "clinical", "DTR", "DTR", "clinical", "DTR", 
"DTR", "DTR", "DTR", "DTR", "DTR", "DTR", "DTR", "clinical", 
"DTR", "DTR", "clinical", "DTR", "clinical", "clinical", "clinical", 
"clinical", "clinical", "clinical", "clinical", "DTR", "DTR", 
"clinical", "DTR", "DTR", "clinical", "DTR", "clinical", "DTR", 
"clinical", "DTR", "clinical", "DTR", "clinical", "clinical", 
"DTR", "clinical", "clinical", "DTR", "DTR", "DTR", "clinical", 
"DTR", "DTR", "clinical", "clinical", "clinical", "clinical", 
"clinical", "clinical", "clinical", "clinical", "clinical", "DTR", 
"DTR", "clinical", "DTR", "DTR", "DTR", "clinical", "clinical", 
"DTR", "DTR", "DTR", "DTR", "DTR", "clinical", "DTR", "DTR", 
"clinical", "clinical", "DTR", "clinical", "clinical", "DTR", 
"DTR", "clinical", "clinical", "DTR", "clinical", "clinical", 
"DTR", "clinical", "DTR", "clinical", "DTR", "DTR", "clinical", 
"clinical", "clinical", "clinical", "clinical", "DTR", "clinical", 
"clinical", "DTR", "clinical", "clinical", "clinical", "clinical", 
"clinical", "clinical", "clinical", "DTR", "DTR", "clinical", 
"clinical", "clinical", "DTR", "clinical", "DTR", "clinical", 
"DTR", "clinical", "clinical", "clinical", "DTR", "DTR", "clinical", 
"clinical", "DTR", "clinical", "DTR", "clinical", "DTR", "clinical", 
"DTR", "clinical", "DTR", "DTR", "clinical", "clinical", "DTR", 
"clinical", "DTR", "DTR", "DTR", "DTR", "clinical", "DTR", "clinical", 
"DTR", "DTR", "clinical", "DTR", "DTR", "clinical", "clinical", 
"clinical", "DTR", "clinical", "clinical", "clinical", "DTR", 
"clinical", "clinical", "clinical", "clinical", "clinical", "clinical", 
"clinical", "clinical", "DTR", "DTR", "DTR", "clinical", "clinical", 
"DTR", "DTR", "clinical", "clinical", "DTR", "clinical", "clinical", 
"clinical", "DTR", "DTR", "DTR", "clinical", "clinical", "clinical", 
"clinical", "DTR", "DTR", "clinical", "DTR", "DTR", "clinical", 
"clinical", "DTR", "clinical", "DTR", "DTR", "DTR", "DTR", "clinical", 
"clinical", "DTR", "clinical", "DTR", "clinical", "DTR", "DTR", 
"DTR", "DTR", "clinical", "clinical", "clinical", "clinical", 
"DTR", "clinical", "DTR", "clinical", "clinical", "clinical", 
"clinical", "DTR", "DTR", "DTR", "clinical", "clinical", "DTR", 
"DTR", "clinical", "clinical", "DTR", "clinical", "DTR", "clinical", 
"DTR", "DTR", "DTR", "clinical", "DTR", "clinical", "DTR", "DTR", 
"DTR", "DTR", "clinical", "clinical", "DTR", "clinical", "DTR", 
"DTR", "DTR", "DTR", "clinical", "clinical", "clinical", "DTR", 
"clinical", "clinical", "clinical", "clinical", "DTR", "DTR", 
"DTR", "clinical", "clinical", "DTR", "clinical", "clinical", 
"clinical", "clinical", "clinical", "DTR", "DTR", "clinical", 
"DTR", "clinical", "DTR", "DTR", "clinical", "DTR", "clinical", 
"clinical", "DTR", "DTR", "clinical", "clinical", "DTR", "DTR", 
"DTR", "clinical", "clinical", "clinical", "DTR", "DTR", "DTR", 
"DTR", "DTR", "DTR", "clinical", "DTR", "DTR", "DTR", "clinical", 
"DTR", "clinical", "clinical", "DTR", "clinical", "DTR", "clinical", 
"clinical", "DTR", "DTR", "DTR", "DTR", "DTR", "clinical", "DTR", 
"clinical", "clinical", "clinical", "DTR", "DTR", "clinical", 
"DTR", "clinical", "DTR", "DTR", "clinical", "clinical", "clinical", 
"clinical", "clinical", "clinical", "clinical", "clinical", "clinical", 
"clinical", "DTR", "clinical", "DTR", "DTR", "clinical", "clinical", 
"clinical", "clinical", "DTR", "DTR", "DTR", "clinical", "clinical", 
"clinical", "clinical", "clinical", "clinical", "DTR", "clinical", 
"clinical", "clinical", "DTR", "DTR", "DTR", "DTR", "clinical", 
"clinical", "clinical", "DTR", "clinical", "clinical", "DTR", 
"clinical", "clinical", "clinical", "DTR", "DTR", "DTR", "DTR", 
"DTR", "DTR", "clinical", "DTR", "DTR", "clinical", "DTR", "DTR", 
"DTR", "clinical", "DTR", "DTR", "DTR", "clinical", "clinical", 
"clinical", "DTR", "clinical", "clinical", "DTR", "DTR", "clinical", 
"clinical", "DTR", "clinical", "DTR", "DTR", "DTR", "DTR", "clinical", 
"clinical", "clinical", "DTR", "clinical", "DTR", "clinical", 
"clinical", "DTR", "DTR", "clinical", "DTR", "clinical", "DTR", 
"clinical", "clinical", "DTR", "DTR", "clinical", "DTR", "clinical", 
"clinical", "DTR", "clinical", "DTR", "clinical", "DTR", "DTR", 
"DTR", "DTR", "DTR", "DTR", "DTR", "clinical", "clinical", "clinical", 
"clinical", "clinical", "clinical", "clinical", "clinical", "DTR", 
"clinical", "clinical", "clinical", "DTR", "clinical", "DTR", 
"clinical", "clinical", "DTR", "clinical", "DTR", "DTR",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0, 1, 1, 1, 1, 1, 1, 0, 0, 1, 0, 0, 0, 0, 
0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 1, 1, 1, 0, 1, 0, 0, 1, 1, 
0, 0, 1, 0, 0, 1, 1, 0, 1, 0, 0, 0, 1, 1, 1, 0, 0, 1, 0, 0, 1, 
0, 0, 0, 1, 0, 0, 1, 1, 0, 0, 1, 1, 1, 0, 1, 1, 1, 1, 0, 1, 0, 
0, 1, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 
1, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 
0, 0, 1, 1, 1, 0, 0, 0, 0, 1, 0, 1, 1, 0, 1, 0, 0, 1, 0, 0, 1, 
1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 1, 
1, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 1), `Disease subtype` = c("Infiltrating", 
"NA", "Other", "NA", "NA", "Infiltrating", "NA", "NA", "NA", 
"Ovarian", "NA", "NA", "NA", "NA", "Ovarian", "NA", "NA", "Infiltrating", 
"NA", "NA", "Infiltrating", "NA", "NA", "NA", "Infiltrating", 
"NA", "NA", "NA", "NA", "Infiltrating", "NA", "NA", "NA", "Infiltrating", 
"Infiltrating", "NA", "Infiltrating", "NA", "NA", "NA", "Other", 
"NA", "Peritoneal", "Adenomyosis", "Infiltrating", "Adenomyosis", 
"NA", "NA", "NA", "NA", "Infiltrating", "NA", "NA", "NA", "NA", 
"NA", "Infiltrating", "NA", "NA", "NA", "Infiltrating", "Other", 
"NA", "Other", "NA", "Ovarian", "NA", "NA", "NA", "NA", "NA", 
"Infiltrating", "NA", "Peritoneal", "Other", "Peritoneal", "NA", 
"Other", "Infiltrating", "NA", "NA", "NA", "Peritoneal", "Infiltrating", 
"NA", "NA", "Infiltrating", "NA", "NA", "Infiltrating", "NA", 
"Infiltrating", "NA", "Infiltrating", "Infiltrating", "Ovarian", 
"NA", "Infiltrating", "NA", "NA", "NA", "Infiltrating", "NA", 
"NA", "Other", "NA", "NA", "NA", "Infiltrating", "Ovarian", "Peritoneal", 
"NA", "NA", "NA", "Infiltrating", "Infiltrating", "NA", "NA", 
"Infiltrating", "NA", "NA", "Infiltrating", "Infiltrating", "NA", 
"Infiltrating", "NA", "Other", "NA", "NA", "NA", "NA", "Infiltrating", 
"NA", "Infiltrating", "Ovarian", "NA", "NA", "NA", "Infiltrating", 
"NA", "Ovarian", "NA", "Infiltrating", "NA", "NA", "NA", "Other", 
"NA", "NA", "NA", "NA", "NA", "NA", "Infiltrating", "NA", "NA", 
"Ovarian", "Infiltrating", "Peritoneal", "NA", "NA", "NA", "Peritoneal", 
"Infiltrating", "NA", "Infiltrating", "Ovarian", "Infiltrating", 
"NA", "NA", "NA", "NA", "NA", "NA", "NA", "NA", "Ovarian", "NA", 
"NA", "NA", "Infiltrating", "NA", "Infiltrating", "NA", "Other", 
"NA", "Ovarian", "NA", "NA", "Infiltrating", "NA", "NA", "NA", 
"Infiltrating", "NA", "NA", "NA", "Infiltrating", "Infiltrating", 
"NA", "NA", "NA", "Infiltrating", "Infiltrating", "Peritoneal", 
"NA", "Adenomyosis", "NA", "Peritoneal", "NA", "Peritoneal", 
"Infiltrating", "Infiltrating", "Other", "Infiltrating", "NA", 
"Infiltrating", "Peritoneal", "Peritoneal", "Other", "NA", "NA", 
"NA", "NA", "NA", "NA", "NA", "Infiltrating", "Ovarian", "Infiltrating", 
"Infiltrating", "NA", "Infiltrating", "Peritoneal", "NA", "NA", 
"Peritoneal", "Infiltrating", "NA", "NA", "NA", "Adenomyosis", 
"NA", "NA", "NA", "Ovarian", "Peritoneal", "Infiltrating", "Infiltrating", 
"Peritoneal", "NA", "NA", "NA", "NA", "Infiltrating", "Infiltrating", 
"NA", "NA", "NA", "NA", "Adenomyosis", "NA", "NA", "NA", "NA", 
"NA", "Other", "NA", "NA", "Peritoneal", "Infiltrating", "Peritoneal", 
"NA", "NA", "NA", "Ovarian", "NA", "NA", "NA", "Ovarian", "Other", 
"Infiltrating", "Infiltrating", "Adenomyosis", "NA", "NA", "NA", 
"Ovarian", "Peritoneal", "NA", "NA", "NA", "Ovarian", "NA", "NA", 
"Infiltrating", "NA", "Adenomyosis", "Adenomyosis", "NA", "Ovarian", 
"Infiltrating", "NA", "NA", "NA", "Other", "NA", "Infiltrating", 
"NA", "NA", "Infiltrating", "NA", "NA", "NA", "NA", "Infiltrating", 
"Peritoneal", "Infiltrating", "NA", "Peritoneal", "NA", "Ovarian", 
"NA", "NA", "Peritoneal", "Peritoneal", "NA", "NA", "Ovarian", 
"Ovarian", "Adenomyosis", "Adenomyosis", "Adenomyosis", "Adenomyosis", 
"NA", "NA", "Ovarian", "NA", "NA", "NA", "NA", "NA", "NA", "NA", 
"NA", "NA", "NA", "NA", "NA", "Other", "NA", "NA", "Other", "Infiltrating", 
"Ovarian", "Infiltrating", "NA", "Infiltrating", "NA", "NA", 
"Infiltrating", "Peritoneal", "NA", "NA", "Adenomyosis", "NA", 
"NA", "Ovarian", "Infiltrating", "NA", "Ovarian", "NA", "NA", 
"NA", "Ovarian", "Adenomyosis", "Infiltrating", "NA", "NA", "Infiltrating", 
"NA", "NA", "Infiltrating", "NA", "NA", "NA", "Ovarian", "NA", 
"NA", "Infiltrating", "Infiltrating", "NA", "NA", "Peritoneal", 
"Infiltrating", "Infiltrating", "NA", "Other", "Ovarian", "Other", 
"Infiltrating", "NA", "Infiltrating", "NA", "NA", "Infiltrating", 
"NA", "Infiltrating", "NA", "NA", "NA", "NA", "NA", "Ovarian", 
"NA", "NA", "NA", "Other", "NA", "NA", "NA", "Ovarian", "NA", 
"NA", "NA", "Adenomyosis", "Other", "NA", "NA", "NA", "NA", "NA", 
"NA", "NA", "Other", "NA", "NA", "NA", "NA", "NA", "NA", "Ovarian", 
"NA", "NA", "NA", "Infiltrating", "NA", "NA", "Ovarian", "Infiltrating", 
"Infiltrating", "NA", "NA", "NA", "NA", "Infiltrating", "NA", 
"Ovarian", "Infiltrating", "NA", "Peritoneal", "NA", "NA", "Infiltrating", 
"NA", "NA", "Infiltrating", "Adenomyosis", "Infiltrating", "Ovarian", 
"NA", "NA", "NA", "NA", "NA", "NA", "NA", "NA", "NA", "NA", "Infiltrating", 
"Infiltrating", "Infiltrating", "NA", "NA", "NA", "NA", "Infiltrating", 
"Peritoneal", "NA", "Peritoneal", "NA", "NA", "NA", "Infiltrating", 
"NA", "NA", "NA", "Ovarian", "Infiltrating"), `Standardized PRS` = c(0.291159147197693, 
0.706357434757487, 1.30009098596799, 0.706357434757487, -0.539237427921894, 
0.291159147197693, -0.124039140362101, -0.124039140362101, -0.124039140362101, 
1.61979366738903, 1.53675400987708, -0.124039140362101, 0.108471900671384, 
-0.539237427921894, 1.71944125640338, 1.12155572231728, -1.36963400304148, 
0.706357434757487, 1.14231563669527, -0.34824621564439, 2.36715058499666, 
-1.36963400304148, 1.12155572231728, -2.66090067735244, 1.53675400987708, 
-0.539237427921894, 0.307767078700085, -0.456197770409935, -0.331638284141998, 
1.53675400987708, -1.23677055102235, -2.37441385893618, -0.119887157486502, 
-0.539237427921894, 1.95195229743687, -0.576605273802276, 0.291159147197693, 
0.859980801154611, -0.539237427921894, -0.854788126467338, 2.13879152683878, 
0.706357434757487, 0.145839746551765, 0.17739481640631, -0.115735174610905, 
-0.0858408979065994, -0.842332177840545, 0.0835600034177963, 
0.154143712302961, 0.0669520719154045, -0.124039140362101, -1.54816926669219, 
-0.456197770409935, 0.706357434757487, -1.85126401661084, -0.539237427921894, 
1.12155572231728, -0.124039140362101, -0.539237427921894, 0.706357434757487, 
-1.78483229060128, -0.954435715481689, -0.0119356027209559, -0.497717599165915, 
-0.539237427921894, 0.145839746551765, -1.48173754068263, 1.12155572231728, 
1.90212850292969, -0.954435715481689, -0.173862934869276, -0.539237427921894, 
1.04266804768092, -0.539237427921894, 1.19629141407804, -1.36963400304148, 
-1.78483229060128, -1.08314718462522, 0.291159147197693, -0.124039140362101, 
1.04266804768092, -1.02501942436685, -0.356550181395586, 1.12155572231728, 
-1.15788287638599, -0.435437856031946, -0.539237427921894, 1.53675400987708, 
-0.539237427921894, -0.954435715481689, -0.954435715481689, -0.124039140362101, 
0.706357434757487, -0.539237427921894, 3.19754716011625, -1.45267366055344, 
0.515366222479982, 0.706357434757487, -2.02149531451036, 0.162447678054157, 
-0.0368474999745436, -0.124039140362101, -0.539237427921894, 
-0.834028212089349, -0.178014917744874, 0.183207592432147, -0.834028212089349, 
-0.194622849247265, -0.539237427921894, -0.987651578486472, 0.411566650590033, 
-0.539237427921894, -0.124039140362101, -0.726076657323802, -0.124039140362101, 
-0.539237427921894, -0.385614061524771, 0.797701058020642, -0.124039140362101, 
-0.954435715481689, -1.75576841047209, 0.706357434757487, 2.78234887255646, 
-1.7350084960941, -0.124039140362101, -1.22846658527115, 1.95195229743687, 
1.19629141407804, -0.385614061524771, -0.124039140362101, -0.954435715481689, 
1.95195229743687, 1.95195229743687, 0.291159147197693, -1.07899520174963, 
-0.539237427921894, -0.539237427921894, -0.290118455386019, 0.291159147197693, 
1.53675400987708, -0.539237427921894, 0.291159147197693, -1.36963400304148, 
-0.124039140362101, -0.539237427921894, 0.287007164322095, -1.70594461596492, 
0.291159147197693, -0.539237427921894, 0.706357434757487, -0.273510523883627, 
0.706357434757487, 1.04266804768092, 1.53675400987708, 0.291159147197693, 
0.706357434757487, -0.954435715481689, -0.124039140362101, 0.253791301317312, 
-0.0285435342233477, -0.954435715481689, -0.539237427921894, 
-0.18631888349607, 1.53675400987708, 2.52907791714498, 0.706357434757487, 
1.39558659210675, 0.291159147197693, 0.394958719087642, -0.954435715481689, 
-0.933675801103699, -1.02501942436685, -1.36963400304148, 1.53675400987708, 
0.851676835403415, -0.539237427921894, 0.0960159520445897, -0.838180194964946, 
-0.148951037615689, 0.141687763676168, 0.291159147197693, -0.501869582041513, 
1.53675400987708, -1.58968909544817, -0.157255003366884, 0.291159147197693, 
0.760333212140261, -0.962739681232885, -0.539237427921894, 0.706357434757487, 
-0.539237427921894, -1.80144022210367, -0.954435715481689, -0.124039140362101, 
-0.124039140362101, -0.593213205304668, -0.539237427921894, -0.539237427921894, 
0.706357434757487, -0.539237427921894, 1.12155572231728, 0.291159147197693, 
-0.124039140362101, -0.539237427921894, 1.97271221181486, -0.0659113801037297, 
1.00031782234982, 0.706357434757487, -0.539237427921894, 0.274551215695302, 
-0.435437856031946, 1.95195229743687, -2.20003057816107, -0.0659113801037297, 
1.12155572231728, -0.954435715481689, -0.539237427921894, 0.760333212140261, 
1.90212850292969, 0.419870616341229, -0.124039140362101, 0.706357434757487, 
0.706357434757487, -0.954435715481689, -1.05823528737164, 1.15477158532206, 
-0.124039140362101, -1.46512960918024, 1.98516816044165, -0.124039140362101, 
-0.539237427921894, 0.00882431165703332, 1.12155572231728, -1.30320227703192, 
0.137535780800569, -0.68870881144342, 0.606709845743136, 0.706357434757487, 
-1.78483229060128, 1.38728262635555, 0.291159147197693, -1.05989608052188, 
0.291159147197693, -1.58553711257258, -1.11636304763001, -1.69764065021372, 
-0.422981907405153, 1.53675400987708, 3.61274544767605, 1.12155572231728, 
-0.539237427921894, -0.256902592381235, -0.539237427921894, 0.328526993078074, 
-0.539237427921894, -0.954435715481689, -1.36963400304148, -0.124039140362101, 
-1.04162735586925, -1.10390709900321, -0.719848683010405, 1.53675400987708, 
-0.124039140362101, 0.685597520379498, -0.0825193116061214, -0.124039140362101, 
-1.36963400304148, 1.45371435236512, 0.706357434757487, -1.99658341725677, 
0.706357434757487, 0.66068562312591, -0.472805701912327, 0.307767078700085, 
-0.124039140362101, 0.141687763676168, -0.124039140362101, 0.000520345905837429, 
-1.07899520174963, -0.476957684787926, 2.41282239662824, 1.12155572231728, 
-0.539237427921894, -0.915407076451068, -1.36963400304148, 0.0752560376666004, 
-0.618125102558256, -0.0991272431085132, -0.68870881144342, -0.539237427921894, 
-1.36963400304148, -0.124039140362101, 0.473846393724003, -0.539237427921894, 
1.12155572231728, -0.124039140362101, 0.706357434757487, 1.73438839475554, 
1.32458768493402, -0.626429068309452, 0.706357434757487, -0.539237427921894, 
-1.1329709791324, -0.954435715481689, -0.539237427921894, -0.539237427921894, 
0.291159147197693, 0.706357434757487, -0.452045787534338, -0.912915886725709, 
1.12155572231728, -0.908763903850112, -0.954435715481689, -0.58906122242907, 
-1.04162735586925, -0.539237427921894, -0.00363163696976005, 
0.291159147197693, 1.12155572231728, 1.09664382506369, 0.436478547843621, 
1.12155572231728, -0.954435715481689, -1.942607639874, -0.476957684787926, 
-0.87139605796973, -0.954435715481689, -0.539237427921894, 0.706357434757487, 
0.835068903901024, -0.552938971411368, -1.40534105577163, 1.126538101768, 
0.703451046744569, -2.3411979959314, -0.954435715481689, 0.710509417633085, 
-1.10390709900321, -1.68518470158693, -0.539237427921894, -1.5315613351898, 
-0.124039140362101, -0.124039140362101, -0.539237427921894, -1.36963400304148, 
2.39621446512585, 0.291159147197693, -0.954435715481689, -0.211230780749657, 
-0.780052434706575, -0.364854147146782, -1.23261856814675, 0.428174582092425, 
-2.20003057816107, -0.717772691572606, -0.124039140362101, -0.954435715481689, 
0.706357434757487, -0.385614061524771, 0.00882431165703332, -0.124039140362101, 
-0.551693376548689, -0.124039140362101, 0.00882431165703332, 
0.815139386098153, -0.124039140362101, -0.124039140362101, 0.291159147197693, 
-0.539237427921894, -0.954435715481689, -0.124039140362101, -0.124039140362101, 
-0.954435715481689, -0.954435715481689, 1.95195229743687, -0.62850505974725, 
-0.539237427921894, -0.182166900620472, 0.291159147197693, 0.706357434757487, 
0.706357434757487, 0.706357434757487, -0.124039140362101, -0.0326955170989461, 
-0.514325530668307, 0.253791301317312, 0.291159147197693, 1.53675400987708, 
-0.124039140362101, 0.706357434757487, 1.12155572231728, -1.78483229060128, 
-0.721924674448204, 1.53675400987708, 2.36715058499666, -0.124039140362101, 
1.02606011617853, 0.851676835403415, -0.00778361984535846, 0.73957329776227, 
-0.954435715481689, -0.124039140362101, 1.95195229743687, -1.66027280433334, 
-1.78483229060128, 0.291159147197693, -0.701164760070215, -0.539237427921894, 
1.12155572231728, -0.539237427921894, 0.212271472561332, -0.950283732606091, 
-0.422981907405153, 0.295311130073291, 1.54505797562827, -0.0285435342233477, 
0.291159147197693, -0.277662506759224, -3.59924880723758, -1.36963400304148, 
-1.25337848252474, 1.12155572231728, 0.00882431165703332, 0.706357434757487, 
0.677293554628302, -1.74995563444625, -0.572453290926678, -0.124039140362101, 
-1.36963400304148, -0.124039140362101, -0.746836571701792, 0.706357434757487, 
-0.539237427921894, 0.722965366259879, 0.365894838958456, -0.124039140362101, 
0.291159147197693, -0.834028212089349, 0.706357434757487, -1.19109873939077, 
0.54027811973357, 2.34639067061867, -0.89215597234772, -0.124039140362101, 
-0.124039140362101, 0.291159147197693, -3.09270689641463, 0.706357434757487, 
1.22120331133163, -0.954435715481689, 0.706357434757487, -0.398070010151565, 
-1.98827945150558, 0.706357434757487, 0.274551215695302, 1.53675400987708, 
0.349286907456065, -1.78483229060128, 0.291159147197693, 0.21642345543693, 
0.0295842260350235, -0.539237427921894, -0.0659113801037297, 
0.291159147197693, -0.954435715481689, 0.623317777245528, 1.53675400987708, 
-0.514740728955866, 0.291159147197693, 1.00530020180054, 1.02190813330293, 
-0.364854147146782, -0.983499595610874, -0.182166900620472, -0.721924674448204, 
0.51951820535558, -0.539237427921894, -0.539237427921894, -0.954435715481689, 
-0.124039140362101, 0.706357434757487, 1.12155572231728, 1.12155572231728, 
0.291159147197693, -0.422981907405153, -0.954435715481689, -0.539237427921894, 
1.53675400987708, -0.933675801103699, -0.539237427921894, -0.460349753285534, 
0.706357434757487, -0.124039140362101, 0.311919061575683, 1.53675400987708, 
-0.966891664108482, -1.86787194811324, 2.33808670486748, 0.967932355920158, 
1.12155572231728), decile = structure(c(7L, 8L, 9L, 8L, 4L, 7L, 
5L, 5L, 5L, 10L, 10L, 5L, 6L, 3L, 10L, 9L, 1L, 8L, 9L, 4L, 10L, 
1L,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 5L, 6L, 6L, 6L, 2L, 
4L, 10L, 9L, 3L, 3L, 1L, 6L, 3L, 6L, 3L, 4L, 1L, 6L, 7L, 4L, 
9L, 5L, 8L, 10L, 9L, 3L, 8L, 3L, 2L, 2L, 4L, 3L, 7L, 8L, 4L, 
3L, 9L, 3L, 2L, 3L, 2L, 3L, 6L, 6L, 9L, 9L, 7L, 9L, 2L, 1L, 4L, 
3L, 2L, 4L, 8L, 9L, 3L, 1L, 9L, 8L, 1L, 2L, 8L, 2L, 1L, 4L, 1L, 
5L, 5L, 4L, 1L, 10L, 7L, 2L, 5L, 3L, 4L, 2L, 7L, 1L, 3L, 5L, 
2L, 8L, 4L, 6L, 5L, 3L, 5L, 6L, 9L, 6L, 5L, 7L, 3L, 2L, 5L, 5L, 
2L, 2L, 10L, 3L, 3L, 5L, 7L, 8L, 8L, 8L, 5L, 6L, 4L, 6L, 7L, 
10L, 5L, 8L, 9L, 1L, 3L, 10L, 10L, 5L, 9L, 9L, 6L, 9L, 2L, 6L, 
10L, 1L, 1L, 6L, 3L, 3L, 9L, 4L, 6L, 2L, 4L, 7L, 10L, 6L, 7L, 
4L, 1L, 1L, 2L, 9L, 6L, 8L, 8L, 1L, 3L, 5L, 1L, 5L, 3L, 8L, 4L, 
8L, 7L, 5L, 7L, 3L, 8L, 2L, 8L, 10L, 3L, 6L, 5L, 6L, 1L, 8L, 
9L, 2L, 8L, 4L, 1L, 8L, 6L, 10L, 7L, 1L, 7L, 6L, 6L, 4L, 6L, 
6L, 2L, 8L, 10L, 4L, 7L, 9L, 9L, 4L, 2L, 5L, 3L, 7L, 4L, 4L, 
2L, 5L, 8L, 9L, 9L, 7L, 4L, 2L, 3L, 10L, 2L, 4L, 4L, 8L, 5L, 
7L, 10L, 2L, 1L, 10L, 9L, 9L), levels = c("1", "2", "3", "4", 
"5", "6", "7", "8", "9", "10"), class = "factor")), row.names = c(NA, 
-500L), class = c("tbl_df", "tbl", "data.frame"))

I thank you in advance for any suggestion!

CodePudding user response:

Logarithm of the Odds vs. Odds

The coefficients from your regression output are on the log odds scale. This is equivalent to log(prob_outcome_is_1 / prob_outcome_is_0). You asked for a visualization of the odds ratios so we need to start with the odds. This requires us to get rid of the logarithm, which can be done by passing the value to the inverse function of the logarithmic function: the exponential function. We can do this by passing all of values from coef() to exp() before plotting with ggplot(). However, you cannot simply exponentiate variance estimates because of their non-linearity. Accordingly, we need to use the variances to get the transformed values.

Plotting Coefficients on Odds Scale

# Required packages
library(dplyr)
library(ggplot2)

# Convert variables to factors and get quantiles
prs_table <- prs_table %>%
  mutate(
    decile = ntile(`Standardized PRS`, 10),
    decile = as.factor(decile),
    `Case control status` = as.factor(`Case control status`)
  )

# Fit regression model
prs_glm <- glm(`Case control status` ~ decile,
               data = prs_table,
               family = 'binomial')

# Put results in data.frame
summs <- prs_glm %>% summary()

# Get point estimates and SEs
results <- bind_cols(coef(prs_glm),
                     summs$coefficients[, 2]) %>%
  setNames(c("estimate", "se"))  %>%
  mutate(decile = 1:10)

# Your coefficients are on the log odds scale, such that a coefficient is
# log(odds_y==1 / odds_y == 0). We can exponentiate to get odds instead.
results_odds <- results %>% mutate(across(.cols = -decile,
                                          ~ exp(.x)))

# Need SEs on the odds scale too
results_odds <- results_odds %>%
  mutate(var_diag = diag(vcov(prs_glm)),
         se = sqrt(estimate ^ 2 * var_diag))

# Plot with  /- 1 SE
ggplot(results_odds, aes(x = as.factor(decile), y = estimate, color))  
  geom_point(stat = "identity", shape = 15)  
  geom_hline(yintercept = 1, linetype = "dashed", color = "grey")  
  geom_errorbar(aes(ymin = estimate - se, ymax = estimate   se), width = 0.4)  
  xlab("PRS Decile")  
  ylab("Odds")

Plotting ratios of coefficients on odds scale

You can get the odds ratios via the ratios of the odds. However, you will then need to decide how you want to calculate the standard error of the odds ratio.

There are many ways to do this, so you will need to decide what assumptions you wish to impose. See here for more information since the normal approximation may not hold well. Bootstrapping the SEs is a common approach. Another approach is the Delta Method. Whether you want exponentiate first then take the ratio or the ratio then exponentiate is up to you.

# Example of calculating SEs with the delta method for
# the ratios of dec1/dec5 and dec2/dec5

library(msm)

estmean <- coef(prs_glm)
estvar <- vcov(prs_glm)
se_dec1 <- deltamethod(~ exp(x1 / x5), estmean, estvar)
se_dec2 <- deltamethod(~ exp(x2 / x5), estmean, estvar)

But for now we'll omit the SEs since it's not clear what "should" be used:


# Get 5th decile's odds
odds_dec5 <- results_odds %>%
  filter(decile == 5) %>%
  pull(estimate)

# Calculate odds ratios
results_or <- results_odds %>%
  mutate(or_dec5 = estimate / odds_dec5)

# Plot OR with no SEs
ggplot(results_or, aes(x = as.factor(decile), y = or_dec5, color))  
  geom_point(stat = "identity", shape = 15)  
  geom_hline(yintercept = 1, linetype = "dashed", color = "grey")  
  xlab("PRS Decile")  
  ylab("Odds Ratio")

Finally, note that the language for the question still remains somewhat vague as the exponentiated coefficients can themselves be understood as "odds ratios.". Accordingly, this would produce the ratio of odds ratios! The crucial part is whether you want to also include the intercept in the quantity - but it sounds like you want to plot the coefficient ratios rather than the predictive outcomes as ratios.

  • Related