Related
I've got clustered patient data of 700 patients (clustered into two groups for two different hospitals). I'm trying to figure out if a certain cardiovacsular risk factor exposure (factor with 3 levels: fall, stable [reference], and rise) is related to my binary outcome_pres (numeric with 0's for no outcome and 1's for yes outcome), adjusted for age and sex. To do this I've used a GEE model using the geepack package in R:
library(tidyverse)
library(magrittr)
library(geepack)
data <- structure(list(id = c(23, 30, 92, 122, 132, 141, 157, 158, 167,
175, 200, 230, 237, 257, 283, 297, 336, 339, 357, 376, 379, 421,
425, 431, 436, 437, 443, 449, 458, 505, 518, 521, 546, 547, 573,
613, 618, 644, 655, 672, 697, 730, 750, 755, 780, 786, 798, 853,
862, 874, 882, 916, 945, 948, 979, 982, 1002, 1003, 1006, 1022,
1059, 1069, 1092, 1095, 1116, 1127, 1133, 1162, 1178, 1188, 1201,
1210, 1239, 1242, 1258, 1280, 1281, 1297, 1307, 1318, 1331, 1353,
1369, 1383, 1407, 1463, 1473, 1477, 1485, 1519, 1555, 1567, 1573,
1611, 1636, 1659, 1686, 1700, 1712, 1744, 1766, 1767, 1771, 1778,
1797, 1806, 1810, 1821, 1822, 1875, 1879, 1890, 1903, 1917, 1964,
2007, 2010, 2018, 2028, 2067, 2071, 2077, 2078, 2086, 2090, 2103,
2128, 2148, 2185, 2223, 2239, 2257, 2267, 2283, 2300, 2304, 2332,
2395, 2399, 2407, 2427, 2431, 2434, 2440, 2445, 2449, 2480, 2515,
2533, 2536, 2546, 2552, 2635, 2660, 2697, 2705, 2724, 2725, 2728,
2748, 2778, 2794, 2798, 2830, 2843, 2895, 2902, 2907, 2915, 2924,
2929, 2952, 2955, 2963, 2982, 3016, 3018, 3036, 3065, 3083, 3087,
3177, 3178, 3186, 3196, 3199, 3230, 3272, 3278, 3292, 3302, 3304,
3311, 3312, 3351, 3378, 3399, 3401, 3403, 3445, 3447, 3449, 3457,
3472, 3477, 3486, 3490, 3515, 3516, 3519, 3521, 17001, 64001,
128001, 136001, 177001, 177002, 240001, 240001, 248002, 352001,
370002, 410001, 426001, 443002, 443002, 466002, 466002, 469002,
470001, 489002, 520002, 542001, 595001, 615001, 651001, 651002,
657001, 658002, 665001, 665001, 687002, 698002, 745001, 754001,
800002, 804001, 811002, 818001, 881001, 881001, 920001, 927001,
927001, 943001, 974001, 1015002, 1037001, 1081001, 1081002, 1133001,
1136001, 1141001, 1157001, 1175002, 1206001, 1247001, 1247002,
1283001, 1290002, 1296001, 1307001, 1344002, 1346001, 1346001,
1379001, 1419002, 1427001, 1438001, 1592002, 1652002, 1711001,
1754001, 1763001, 1811001, 1833001, 1878002, 1892001, 1915001,
1915002, 1921001, 1949002, 1961001, 1995001, 2014001, 2022002,
2102001, 2102002, 2138002, 2141001, 2141002, 2193002, 2240001,
2281001, 2281002, 2432001, 2493001, 2517001, 2558001, 2558002,
2588001, 2588001, 2590001, 2601001, 2620002, 2653001, 2678001,
2714001, 2721001, 2721002, 2721002, 2731001, 2777001, 2806001,
2813001, 2862001, 2862002, 2872001, 2872002, 2902001, 2959001,
2967001, 3026002, 3026002, 3037001, 3063002, 3094002, 3126002,
3139001, 3196001, 3233001, 3252001, 3282002, 3290002, 3314001,
3334001, 3334001, 3377002, 3420001, 3427001, 3458001, 3532002,
3558001, 3653001, 3663002, 3675002, 3678002, 3692001, 3757002,
3764002, 3764002, 3774001, 3780001, 3823002, 3823002, 3836001,
3862001, 3916001, 3953001, 4032001, 4042001, 4063001, 4077001,
4084001, 4094002, 4118002, 4140002, 4140002, 4149001, 4168004,
4171001, 4241002, 4254001, 4257002, 4269001, 4270001, 4291001,
4298001, 4302002, 4302002, 4325001, 4325001, 4343002, 4353001,
4353001, 4377001, 4422002, 4447002, 4563002, 4568002, 4578001,
4578002, 4601001, 4620002, 4641002, 4673002, 4687001, 4687002,
4695002, 4695002, 4700002, 4707001, 4707001, 4768002, 4802001,
4821001, 4833001, 4839001, 4839001, 4848001, 4854002, 4893002,
4895002, 4918001, 4969001, 4972001, 4985001, 4985001, 4985002,
4991001, 5045001, 5072002, 5181001, 5234002, 5240001, 5361001,
5361002, 5388002, 5400001, 5467002, 5469001, 5528001, 5588001,
5593001, 5643002, 5676001, 5683002, 5690001, 5708002, 5710002,
5722002, 5722002, 5725002, 5770001, 5826001, 5866001, 5869001,
5982002, 5993001, 6039002, 6054002, 6095001, 6095002, 6121003,
6195002, 6258002, 6260002, 6391001, 6391002, 6414001, 6489002,
6552002, 6555002, 6573001, 6578002, 6582002, 6583002, 6583002,
6588001, 6588002, 6625001, 6625002, 6642001, 6660001, 6806002,
6887002, 6888001, 6893002, 6916001, 6916001, 6953002, 6960001,
6960001, 6991002, 7010001, 7010002, 7021001, 7037001, 7037001,
7038001, 7038002, 7089001, 7121001, 7141001, 7150002, 7173002,
7192001, 7220001, 7301002, 7318002, 7339002, 7344002, 7421001,
7436002, 7522002, 7606002, 7653002, 7681001, 7686001, 7759002,
7781001, 7805001, 7816001, 7837002, 7846001, 7856001, 7856002,
7884002, 7934001, 7971002, 7984001, 7995001, 8016002, 8052001,
8064002, 8064003, 8076001, 8109001, 8125001, 8143001, 8144001,
8194001, 8194002, 8218001, 8247001, 8333001, 8360001, 8401001,
8480001, 8545002, 8556001, 8556002, 8596002, 8599001, 8638001,
8751002, 8943002, 9042001, 9075001, 9112002, 9195001, 9404001,
9437001, 9452002, 9456001, 9528001, 9528002, 9535001, 9556001,
9591001), group = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1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L, 2L, 1L,
1L, 1L, 2L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 2L, 1L, 1L,
2L, 2L, 1L, 1L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 1L, 1L, 1L, 1L,
1L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 1L, 2L, 1L, 2L,
1L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 1L, 1L, 1L, 1L, 2L, 1L, 1L, 2L,
1L, 2L, 1L, 1L, 2L, 1L, 2L, 1L, 1L, 1L, 2L, 1L, 1L, 1L, 2L, 2L,
1L, 2L, 1L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 2L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 2L, 2L, 2L, 2L, 1L, 2L, 2L, 1L, 2L, 1L, 1L, 2L, 1L,
1L, 1L, 1L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 2L, 2L, 1L, 1L, 1L, 1L,
2L, 2L, 1L, 1L, 1L, 2L, 1L, 1L, 2L, 2L, 1L, 2L, 1L, 2L, 1L, 2L,
1L, 2L, 1L, 1L, 2L, 1L, 1L, 2L, 2L, 1L, 2L, 1L, 1L, 1L, 1L, 2L,
2L, 1L, 2L, 1L, 1L, 1L, 1L, 2L, 1L, 2L, 1L, 1L, 2L, 1L, 1L, 2L,
1L, 1L, 1L, 1L, 2L, 1L, 1L, 1L, 1L, 2L, 2L, 1L, 1L, 1L, 1L, 1L,
2L, 2L, 2L, 2L, 2L, 2L, 1L, 2L, 2L, 2L, 1L, 1L, 1L, 2L, 1L, 1L,
2L, 1L, 1L, 1L, 2L, 1L, 1L, 2L, 1L, 1L, 2L, 2L, 2L, 1L, 1L, 1L,
1L, 1L), .Label = c("1", "2", "3"), class = "factor"), age = c(61.6700889801506,
63.3182751540041, 66.611909650924, 64.435318275154, 63.6796714579055,
64.6351813826146, 60.6351813826146, 63.9780971937029, 70.4394250513347,
63.7700205338809, 62.8583162217659, 64.5859000684463, 70.9733059548255,
63.7125256673511, 63.8466803559206, 91.1813826146475, 86.0342231348392,
75.9069130732375, 63.4496919917864, 63.8056125941136, 67.2388774811773,
64.104038329911, 65.284052019165, 64.4188911704312, 82.2203969883641,
65.0814510609172, 71.4277891854894, 62.2395619438741, 77.7385352498289,
61.5030800821355, 82.3244353182751, 69.5687885010267, 78.0752908966461,
65.3826146475017, 62.9267624914442, 63.6988364134155, 63.2553045859001,
62.1409993155373, 64.6351813826146, 63.1512662559891, 66.3764544832307,
71.5947980835044, 63.2826830937714, 65.9657768651608, 63.6194387405886,
63.4414784394251, 61.5359342915811, 78.031485284052, 63.2772073921971,
64.0219028062971, 79.1950718685832, 61.5468856947296, 65.0403832991102,
59.0006844626968, 88.766598220397, 59.9151266255989, 61.8151950718686,
63.4880219028063, 61.5058179329227, 69.5058179329227, 64.6297056810404,
60.2327173169062, 64.7830253251198, 63.1129363449692, 82.3545516769336,
71.7618069815195, 62.9733059548255, 67.5208761122519, 61.3388090349076,
60.7063655030801, 67.3264887063655, 64.9801505817933, 62.9075975359343,
63.3675564681725, 61.9986310746064, 64.2327173169062, 73.5468856947296,
73.9219712525667, 61.9219712525667, 64.5722108145106, 61.2922655715264,
72.4900752908966, 60.3778234086242, 60.8569472963723, 64.2135523613963,
62.362765229295, 60.4298425735797, 64.1834360027378, 63.8412046543463,
62.9240246406571, 61.5797399041752, 63.8357289527721, 63.1950718685832,
66.8720054757016, 62.5242984257358, 73.5687885010267, 64.2245037645448,
63.8576317590691, 63.8494182067077, 61.2621492128679, 73.927446954141,
64.4490075290897, 61.741273100616, 62.6803559206023, 61.5112936344969,
64.3011635865845, 61.2539356605065, 63.4962354551677, 72.2026009582478,
61.4620123203285, 67.917864476386, 70.7871321013005, 66.2313483915127,
61.9055441478439, 62.5653661875428, 79.7262149212868, 61.1143052703628,
61.9192334017796, 63.0554414784394, 72.3586584531143, 63.4140999315537,
61.9247091033539, 71.3620807665982, 65.1553730321697, 64.6461327857632,
61.9000684462697, 63.5619438740589, 73.6317590691307, 61.5195071868583,
62.9514031485284, 64.3066392881588, 61.3278576317591, 68.9418206707734,
62.7652292950034, 83.7289527720739, 62.9295003422313, 61.596167008898,
64.8925393566051, 68.6187542778919, 61.0102669404517, 67.2580424366872,
63.2251882272416, 61.1416837782341, 63.5181382614647, 63.0444900752909,
61.4236824093087, 75.6194387405886, 63.6878850102669, 63.937029431896,
63.2936344969199, 63.8877481177276, 86.984257357974, 77.6673511293635,
62.0752908966461, 75.668720054757, 63.5482546201232, 61.8617385352498,
79.0581793292266, 81.3333333333333, 73.3415468856947, 63.8658453114305,
64.7693360711841, 62.7843942505133, 81.0759753593429, 63.460643394935,
61.3963039014374, 61.6262833675565, 84.4271047227926, 61.2073921971253,
63.4661190965092, 62.8829568788501, 74.9787816563997, 64.662559890486,
64.5338809034908, 62.1054072553046, 63.6878850102669, 66.4832306639288,
64.6899383983573, 80.7529089664613, 61.7960301163587, 64.3832991101985,
63.4442162902122, 63.9342915811088, 63.1978097193703, 72.662559890486,
69.1225188227242, 63.2908966461328, 60.1943874058864, 61.3853524982888,
61.1498973305955, 61.3689253935661, 61.1800136892539, 61.596167008898,
60.7200547570157, 60.3148528405202, 60.6981519507187, 60.4681724845996,
61.1581108829569, 60.662559890486, 61.0212183436003, 60.5776865160849,
60.8323066392882, 61.0075290896646, 60.7775496235455, 61.1745379876797,
59.9780971937029, 60.4626967830253, 60.6297056810404, 61.5852156057495,
60.7830253251198, 61.7029431895962, 69.2375085557837, 71.4414784394251,
67.4770704996578, 67.4305270362765, 61.0075290896646, 59.4743326488706,
67.8275154004107, 72.9062286105407, 65.7604380561259, 57.7330595482546,
63.7618069815195, 66.1300479123888, 69.242984257358, 71.8302532511978,
76.7720739219713, 57.201916495551, 62.1629021218344, 67.2142368240931,
59.4907597535934, 58.0752908966461, 75.8658453114305, 74.5927446954141,
63.0006844626968, 67.9972621492129, 60.2135523613963, 59.7015742642026,
65.1909650924025, 61.4565366187543, 60.2874743326489, 65.4401095140315,
56.8131416837782, 72.5612594113621, 66.6201232032854, 66.0561259411362,
68.4818617385352, 67.315537303217, 70.3600273785079, 68.3832991101985,
57.9000684462697, 62.90212183436, 70.7405886379192, 72.7063655030801,
77.5058179329227, 64.2217659137577, 57.2731006160164, 67.5509924709103,
68.4407939767283, 60.3449691991786, 60.0246406570842, 62.6721423682409,
60.9637234770705, 69.9931553730322, 69.6481861738535, 57.9438740588638,
61.2046543463381, 58.154688569473, 58.0752908966461, 71.0992470910335,
65.0212183436003, 56.6762491444216, 65.4592744695414, 65.7494866529774,
68.0164271047228, 72.788501026694, 70.7077344284736, 63.5564681724846,
66.7843942505133, 63.0444900752909, 65.845311430527, 69.07871321013,
63.3976728268309, 59.1375770020534, 67.356605065024, 67.0937713894593,
60.9719370294319, 74.90212183436, 66.0807665982204, 68.8514715947981,
68.4462696783025, 66.2422997946612, 61.9247091033539, 58.3545516769336,
65.0485968514716, 66.1081451060917, 68.870636550308, 64.1396303901437,
58.1218343600274, 64.5420944558522, 67.1622176591376, 66.6283367556468,
73.7467488021903, 64.4873374401095, 69.45106091718, 63.7289527720739,
87.8439425051335, 66.984257357974, 61.6427104722793, 63.7453798767967,
59.5592060232717, 58.8254620123203, 63.5482546201232, 73.7522245037646,
57.6563997262149, 70.2642026009582, 66.6365503080082, 71.2388774811773,
69.347022587269, 60.1642710472279, 59.9397672826831, 64.4462696783025,
67.3620807665982, 65.3278576317591, 64.8213552361396, 69.6618754277892,
69.5249828884326, 61.3607118412047, 68.5694729637235, 62.8172484599589,
58.113620807666, 67.2854209445585, 68.3039014373717, 63.4579055441478,
68.539356605065, 66.5735797399042, 64.5256673511294, 66.1902806297057,
63.356605065024, 71.7316906228611, 67.3867214236824, 65.5715263518138,
77.2156057494867, 72.2984257357974, 64.4407939767283, 74.0150581793292,
73.2731006160164, 77.7166324435318, 61.3169062286105, 66.7898699520876,
69.1772758384668, 71.3675564681725, 64.104038329911, 68.1286789869952,
71.0362765229295, 60.643394934976, 68.6899383983573, 66.9924709103354,
65.2785763175907, 58.3134839151266, 64.2956878850103, 69.2676249144422,
70.4531143052704, 66.1218343600274, 60.7693360711841, 65.6399726214921,
67.605749486653, 67.1567419575633, 67.6632443531827, 66.4832306639288,
63.7590691307324, 65.6810403832991, 74.5653661875428, 65.7275838466804,
67.3538672142368, 67.7700205338809, 65.1745379876797, 61.8370978781656,
66.6036960985626, 59.0280629705681, 65.7987679671458, 61.0020533880903,
59.8658453114305, 68.2847364818617, 67.2005475701574, 68.1697467488022,
66.0862422997947, 61.9356605065024, 68.1314168377823, 65.0540725530459,
69.45106091718, 70.2313483915127, 75.1594798083504, 81.1444216290212,
60.4188911704312, 65.2457221081451, 67.8412046543463, 69.9082819986311,
61.9438740588638, 88.2108145106092, 69.574264202601, 70.3737166324435,
67.7344284736482, 66.2368240930869, 65.1225188227242, 61.7357973990418,
81.6317590691307, 63.1567419575633, 61.8726899383984, 57.3223819301848,
62.3463381245722, 70.1273100616016, 63.6358658453114, 69.4620123203285,
70.113620807666, 67.7234770704997, 66.7515400410678, 72.8870636550308,
69.2101300479124, 74.0068446269678, 72.7310061601643, 66.0041067761807,
62.7707049965777, 63.5783709787817, 67.501711156742, 66.4339493497604,
67.1375770020534, 64.5941136208077, 69.4674880219028, 66.466803559206,
80.7501711156742, 62.2258726899384, 61.3141683778234, 66.4503764544832,
66.1409993155373, 73.0458590006845, 67.7180013689254, 65.7166324435318,
68.2518822724162, 70.2094455852156, 70.2532511978097, 65.347022587269,
69.9986310746064, 68.5831622176591, 62.7843942505133, 63.006160164271,
65.45106091718, 70.5845311430527, 88.9308692676249, 75.7399041752224,
62.2231348391513, 56.7775496235455, 61.3935660506502, 69.0239561943874,
65.3415468856947, 66.0232717316906, 71.7700205338809, 66.4120465434634,
68.8788501026694, 60.6379192334018, 67.0225872689938, 63.5537303216975,
64.1724845995893, 64.1670088980151, 67.8001368925394, 68.7200547570157,
66.4996577686516, 62.9267624914442, 79.1403148528405, 71.6659822039699,
62.3326488706366, 71.1019849418207, 62.3408624229979, 65.3579739904175,
78.2067077344285, 63.0171115674196, 66.6365503080082, 63.8740588637919,
68.6899383983573, 68.4188911704312, 66.0260095824778, 67.5482546201232,
65.9329226557153, 59.9534565366188, 71.9452429842574, 68.4161533196441,
70.6420260095825, 63.2991101984942, 71.6249144421629, 60.6789869952088,
65.6810403832991, 65.347022587269, 62.4558521560575, 67.5318275154004,
64.9281314168378, 67.129363449692, 67.3292265571526, 65.2375085557837,
59.2881587953457, 64.1396303901437, 65.7713894592745, 65.7166324435318,
60.4818617385353, 66.5215605749487, 72.8186173853525, 68.6789869952088,
65.678302532512, 74.5790554414784, 64.1505817932923, 65.7166324435318,
57.7494866529774, 62.0150581793292, 62.0752908966461, 74.135523613963,
67.64681724846, 72.4900752908966, 65.8891170431211, 76.9308692676249,
68.4709103353867, 66.3983572895277, 69.5605749486653, 66.6721423682409,
65.0403832991102, 67.6386036960986, 67.5318275154004, 62.54893908282,
78.0041067761807, 77.0184804928131, 66.4914442162902, 80.8049281314168,
65.3251197809719, 75.2087611225188, 66.7241615331964, 56.6078028747433,
65.7713894592745, 70.611909650924, 66.6721423682409, 72.227241615332,
77.3114305270363, 82.2450376454483, 65.0294318959617, 63.315537303217,
71.0499657768652, 62.4722792607803, 62.7186858316222, 63.5373032169747,
69.4866529774127, 66.839151266256, 65.4647501711157, 66.8911704312115,
78.403832991102, 65.8590006844627, 64.2765229295003, 79.9671457905544,
85.07871321013, 66.7515400410678, 75.211498973306, 71.3620807665982,
72.2628336755647, 64.9363449691992, 77.9493497604381, 58.5817932922656,
70.2258726899384, 76.0191649555099, 68.4928131416838, 69.1143052703628,
69.3388090349076, 71.7262149212868, 90.9650924024641, 67.7043121149897,
77.9301848049281), sex = structure(c(1L, 1L, 1L, 1L, 1L, 2L,
1L, 2L, 1L, 1L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L,
2L, 2L, 1L, 2L, 2L, 1L, 1L, 1L, 2L, 1L,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1, 1, 0, 0, 0, 1, 1, 1, 0,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1)), row.names = c(NA,
-570L), class = c("tbl_df", "tbl", "data.frame"))
And the model
model <- geeglm(formula=outcome_pres~exposure+sex+age, data=data, id=id, family=binomial("logit"), corstr="ar1")
How do I check if R actually used the 0's of outcome_pres as the reference category in this analysis?
I think you are misunderstanding something here. outcome_pres is your outcome, which you define as a binomial distribution, and has the values 0 and 1:
str(data)
tibble [570 x 6] (S3: tbl_df/tbl/data.frame)
$ id : num [1:570] 23 30 92 122 132 141 157 158 167 175 ...
$ group : Factor w/ 3 levels "1","2","3": 1 1 1 1 1 1 1 1 1 1 ...
$ age : num [1:570] 61.7 63.3 66.6 64.4 63.7 ...
$ sex : Factor w/ 2 levels "Women","Men": 1 1 1 1 1 2 1 2 1 1 ...
$ exposure : Factor w/ 3 levels "Stable","Fall",..: 1 1 1 3 1 3 1 1 1 1 ...
$ outcome_pres: num [1:570] 1 1 1 1 1 0 1 1 1 1 ...
There are no reference category in this case.
If your question is how do I know what is the reference category of my exposure, then you can see it directly in the summary of your model:
summary(model)
Call:
geeglm(formula = outcome_pres ~ exposure + sex + age, family = binomial("logit"),
data = data, id = id, corstr = "ar1")
Coefficients:
Estimate Std.err Wald Pr(>|W|)
(Intercept) -3.652686 1.826395 4.00 0.0455 *
exposureFall 0.718930 0.444144 2.62 0.1055
exposureRise -0.854571 0.325662 6.89 0.0087 **
sexMen 0.000538 0.242922 0.00 0.9982
age 0.080847 0.028230 8.20 0.0042 **
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
You have exposureFall and exposureRise, meaning that your reference is the category not present in the summary, that is stable:
data$exposure %>% unique()
[1] Stable Rise Fall
Levels: Stable Fall Rise
Rise has thus a protective effect (your Odd ratio is exp(-0.854571)) compared to stable
I am not sure why I am still receiving this message when running a base model with all variables in my dataset:
My data, with anonymized variables:
set.seed(1234)
#dput(df)
structure(list(outcome_1= structure(c(2L, 1L, 1L, 2L,
1L, 2L, 1L, 2L, 2L, 1L, 1L, 2L, 2L, 2L, 1L, 2L, 2L, 2L, 2L, 2L,
NA, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 1L, 2L, 2L, 2L, 2L, 2L,
1L, 2L, 1L, NA, 2L, 1L), .Label = c("0", "1"), class = "factor"),
outcome_2= structure(c(2L, 1L, 1L, 1L, 1L, 2L, 1L,
2L, 1L, 1L, 1L, 2L, 2L, 1L, 1L, 2L, 2L, 1L, 2L, 2L, NA, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 1L, 2L, 2L, 2L, 2L, 2L, 1L,
1L, 1L, NA, 2L, 1L), .Label = c("0", "1"), class = "factor"),
outcome_3= structure(c(2L, 1L, 1L, 1L, 1L, 2L, 1L,
2L, 1L, 1L, 1L, 1L, 2L, 1L, 1L, 2L, 2L, 1L, 2L, 1L, NA, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 1L, 1L, 1L, 1L, 2L, 2L, 1L,
1L, 1L, NA, 1L, 1L), .Label = c("0", "1"), class = "factor"),
bl_ep = c(16, 92, 10, 40, 19, 1, 16, 10, 22, 28, 8, 11, 6,
47, 12, 1, 9, 20, 2, 14, 72, 28, 5, 16, 61, 12, 24, 22, 44,
44, 16, 36, 62, 10, 16, 10, 89, 22, 5, 38, 8, 11), bl_days = c(12,
28, 10, 25, 19, 1, 10, 9, 13, 28, 4, 11, 6, 20, 12, 1, 8,
16, 2, 12, 27, 28, 5, 13, 24, 10, 18, 18, 16, 16, 10, 28,
22, 5, 15, 8, 28, 15, 5, 22, 7, 11), score_1 = c(11,
19, 17, 17, 12, 14, 8, 12, 14, 15, 14, 13, 12, 14, 15, 5,
11, 14, 14, 13, 16, 11, 11, 14, 20, 14, 12, 11, 17, 15, 14,
18, 15, 14, 12, 10, 17, 16, 11, 13, 18, 17), score_2 = c(1.1,
1.6, 1.6, 2.8, 1.9, 3.3, 4, 3.8, 1.8, 1.4, 2, 3.55, 1.6,
1.8, 2.4, 3.7, 1.4, 2.9, 3.55, 2.5, 1.6, 3.2, 3.5, 2.4, 3.1,
2.3, 3.8, 3.9, 1.1, 1.7, 2.3, 1.5, 1.9, 3.3, 3, 2.9, 1.6,
3.1, 3.7, 2.8, 1.2, 1.9), score_3 = c(1,
1.22222222222222, 1.11111111111111, 1.88888888888889, 1.44444444444444,
1.44444444444444, 3.22222222222222, 2.77777777777778, 1.11111111111111,
1, 1, 2.83333333333333, 1.22222222222222, 1.875, 1.55555555555556,
2.66666666666667, 1, 2.25, 1.72222222222222, 2.05555555555556,
1.22222222222222, 2, 2, 1.77777777777778, 1.33333333333333,
1.11111111111111, 2.5, 2.55555555555556, 1, 1.22222222222222,
1.77777777777778, 1.22222222222222, 2.44444444444444, 1.55555555555556,
1.77777777777778, 1.66666666666667, 1.11111111111111, 2.33333333333333,
2.88888888888889, 1.55555555555556, 1, 1.25), score_4 = c(1.31428571428571,
1.37142857142857, 1.08571428571429, 1.83809523809524, 1.37142857142857,
1.8952380952381, 4, 3.88571428571429, 3.02857142857143, 2.12222222222222,
1.43333333333333, 3.39047619047619, 1.74285714285714, 1.67619047619048,
2.02857142857143, 3.48571428571429, 1.24761904761905, 3.73333333333333,
3.08571428571429, 2.56666666666667, 1.74285714285714, 2.6952380952381,
3.45714285714286, 2.27619047619048, 1.9047619047619, 2.62857142857143,
3.74285714285714, 3.74285714285714, 1.24761904761905, 1.39047619047619,
1.83809523809524, 2.74285714285714, 4, 1.77142857142857,
3.42857142857143, 3.2, 1.65714285714286, 2.55238095238095,
2.38095238095238, 2.40952380952381, 2.07619047619048, 2.56666666666667
), score_5 = c(1, 1, 1, 1, 1.33333333333333,
1, 3.33333333333333, 3.66666666666667, 1.66666666666667,
1.66666666666667, 2, 2.5, 1.66666666666667, 1, 1.33333333333333,
3, 1, 1.66666666666667, 2.16666666666667, 2.16666666666667,
1.33333333333333, 2.66666666666667, 3, 2.66666666666667,
1.33333333333333, 2.66666666666667, 3, 1.33333333333333,
1, 1, 1, 1, 1, 1.33333333333333, 3, 3.66666666666667, 1.66666666666667,
1.33333333333333, 2.33333333333333, 1.66666666666667, 2,
2), sex = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 1L,
1L, 1L, 1L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L), .Label = c("F", "M"), class = "factor"), age = c(64,
66, 51, 69, 60, 65, 65, 69, 50, 78, 75, 78, 35, 77, 69, 48,
65, 72, 60, 64, 78, 71, 58, 55, 55, 57, 81, 76, 56, 71, 56,
73, 69, 51, 43, 77, 31, 64, 69, 63, 38, 71), childbirth = structure(c(2L,
2L, 2L, 1L, 2L, 2L, 2L, NA, 2L, 2L, 2L, 2L, NA, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 2L, 2L, NA, 1L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 1L, 2L, 2L, 2L, 2L), .Label = c("N",
"Y"), class = "factor"), x1= c(3, 2, 2, NA,
3, 2, 3, NA, 3, 3, 2, 2, NA, 2, 5, 2, 2, 2, 4, 3, 2, 2, 3,
NA, 2, 3, NA, NA, 2, 2, 2, 2, 2, 2, 3, 2, 1, NA, 2, 2, 1,
3), x2= c(0, 0, 0, NA, 1, 0, 0, NA, 0, 0,
0, 0, NA, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, NA, 0, 0, NA, NA,
0, 0, 0, 0, 0, 0, 0, 0, 1, NA, 0, 0, 0, 0), x3= structure(c(4L,
1L, 1L, 2L, 1L, 1L, 1L, NA, 4L, 1L, 1L, 4L, NA, 4L, 1L, 4L,
4L, 4L, 4L, 3L, 1L, 1L, 1L, 2L, 4L, 1L, NA, 2L, 1L, 4L, 1L,
1L, 4L, 4L, 1L, 4L, 4L, 2L, 4L, 4L, 4L, 1L), .Label = c("N",
"NA", "UNK", "Y"), class = "factor"), x4= structure(c(4L,
1L, 1L, 2L, 1L, 1L, 1L, NA, 1L, 1L, 4L, 1L, NA, 1L, 1L, 4L,
3L, 1L, 4L, 4L, 1L, 4L, 4L, 2L, 1L, 4L, NA, 2L, 4L, 1L, 4L,
1L, 1L, 4L, 4L, 1L, 4L, 2L, 4L, 1L, 4L, 4L), .Label = c("N",
"NA", "UNK", "Y"), class = "factor"), x5= structure(c(2L,
2L, 2L, 2L, 2L, 2L, 2L, NA, 2L, 2L, 2L, 2L, NA, 2L, 2L, 1L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, NA, 2L, 2L, 2L, 2L,
2L, 2L, 1L, 1L, 2L, 1L, 2L, 2L, 2L, 1L, 2L), .Label = c("N",
"Y"), class = "factor"), x6= structure(c(2L, 2L, 2L, 1L,
1L, 2L, 2L, NA, 1L, 1L, 1L, 2L, NA, 2L, 2L, 1L, 2L, 2L, 1L,
2L, 2L, 2L, 1L, 1L, 2L, 2L, NA, 2L, 2L, 2L, 1L, 2L, 1L, 1L,
1L, 2L, 2L, 1L, 1L, 1L, 1L, 2L), .Label = c("N", "Y"), class = "factor"),
x7= structure(c(1L, 1L, 1L, 1L, 1L,
1L, 1L, NA, 1L, 1L, 1L, 1L, NA, 1L, 3L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 3L, 1L, NA, 3L, 1L, 1L, 1L, 1L, 1L, 1L, 3L,
3L, 1L, 1L, 1L, 1L, 2L, 3L), .Label = c("N", "NA", "Y"), class = "factor"),
x8= structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, NA, 1L,
2L, 2L, 2L, NA, 1L, 1L, 1L, 1L, 2L, 2L, 1L, 1L, 2L, 1L, 1L,
1L, 1L, NA, 1L, 1L, 2L, 2L, 1L, 1L, 2L, 2L, 2L, 2L, 1L, 1L,
2L, 2L, 2L), .Label = c("N", "Y"), class = "factor"), x9= structure(c(1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 1L, 1L, 1L, 2L,
1L, 2L, 2L, 2L, 1L, 2L, 2L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 1L,
1L, 2L, 2L, 2L, 1L, 1L, 2L, 1L, 2L, 1L, 2L), .Label = c("N",
"Y"), class = "factor"), x10= structure(c(1L, 2L, 2L,
1L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 2L, 1L, 1L, 1L, 2L, 1L,
1L, 1L, 2L, 2L, 1L, 2L, 2L, 2L, 2L, 1L, 2L, 2L, 1L, 2L, 2L,
1L, 1L, 2L, 1L, 2L, 2L, 1L, 1L, 1L), .Label = c("N", "Y"), class = "factor"),
x11= structure(c(1L, 1L, 1L, 2L, 1L, 2L, 1L, 1L,
2L, 1L, 1L, 2L, 1L, 1L, 1L, 1L, 1L, 2L, 1L, 1L, 1L, 2L, 1L,
2L, 2L, 1L, 1L, 1L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L), .Label = c("N", "Y"), class = "factor"),
x12= structure(c(1L, 1L, 1L, 2L, 1L, 1L, 2L, 1L,
1L, 1L, 1L, 1L, 1L, 2L, 1L, 1L, 1L, 1L, 2L, 1L, 1L, 2L, 1L,
1L, 2L, 1L, 1L, 1L, 1L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 2L,
1L, 1L, 1L, 1L), .Label = c("N", "Y"), class = "factor"),
x13= structure(c(2L, 2L, 2L, 1L, 1L, 2L, 1L, 2L,
2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 1L, 1L, 2L, 1L,
2L, 1L, 2L, 1L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 2L, 1L, 1L), .Label = c("N", "Y"), class = "factor"),
x14= c(41, 7, 8, 9, 7, 2, 1, 5, 9, 6, 6, 8,
14, 2, 4, NA, 11, 9, 31, 13, 8, 2, 11, 20, 8, 7, 6, 8, 2,
12, 32, 1, 2, 38, 10, 17, 5, 28, 31, 10, 3, 6), x15= structure(c(3L,
4L, 2L, 2L, 3L, 2L, 3L, 2L, 3L, 2L, 3L, 3L, 5L, 1L, 3L, 3L,
3L, 3L, 3L, 3L, 2L, 2L, 3L, 2L, 3L, 1L, 2L, 2L, 3L, 3L, 3L,
2L, 3L, 3L, 3L, 3L, 3L, 2L, 3L, 3L, 3L, 3L), .Label = c("IATRO",
"IDIO", "OBST", "OBST/IDIO", "TRAUM"), class = "factor"),
x16= structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 2L, 1L, 1L, 2L, 2L, 2L, 2L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 1L, 1L, 2L, 1L, 1L, 2L,
1L, 1L, 1L, 1L, 1L), .Label = c("N", "Y"), class = "factor"),
x17= structure(c(2L, 2L, 1L, 2L, 1L, 2L,
2L, 1L, 1L, 2L, 2L, 1L, 1L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
2L, 1L, 1L, 1L, 2L, 2L, 2L, 1L, 1L, 2L, 1L, 1L, 2L, 1L, 1L,
2L, 1L, 1L, 1L, 1L, 1L), .Label = c("N", "Y"), class = "factor"),
x18= c(31.8, 20, 30.9, 23.3, 22.5, 23.1, 23.6, 25.9, 22.8,
25.2, 30.2, 23.4, 22.2, 29, 24.8, 32.7, 20.8, 28.5, 24.6,
23, 23.4, 21.1, 24.9, 18, 21.7, 27.6, 27, 29, 32.9, 26, 29.3,
27.1, 22.7, 19.7, 25, 22.3, 21.3, 17.5, 20.9, 20.1, 25.1,
22.1), x19= structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 1L, 2L, 1L, 1L, 1L,
1L, 1L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 2L, 2L, 2L), .Label = c("No", "Yes"), class = "factor"),
x20 = structure(c(2L, 1L, 2L, 1L, 2L, 1L, 2L,
1L, 2L, 1L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 2L, 2L, 2L, 2L, 1L,
1L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 2L, 1L, 2L, 2L, 1L, 1L, 2L,
1L, 1L, 1L, 2L, 2L), .Label = c("NO", "YES"), class = "factor"),
x21= structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 2L, 1L, 1L, 1L, 2L, 1L, 2L, 1L, 1L, 1L,
1L, 1L, 2L, 1L, 1L, 1L, 1L, 1L, 2L, 1L, 1L, 1L, 2L, 1L, 2L,
1L, 1L, 1L, 1L, 1L), .Label = c("NO", "YES"), class = "factor")), row.names = c(NA,
-42L), class = c("tbl_df", "tbl", "data.frame"))
logit1 <-glm(outcome_1~., data = df, family = "binomial")
Which yielded the classic error message for a logit model:
#Error in `contrasts<-`(`*tmp*`, value = contr.funs[1 + isOF[nn]]) :
# contrasts can be applied only to factors with 2 or more levels
Ok, so I went to double check that all factor variables indeed have more than 1 unique value, and can verify:
sapply(lapply(df, unique), length)
returned all variables showing 2 or more unique values. Still same error message when I ran the model again.
I even attempted to run one solution I found online:
values_count <- sapply(lapply(df, unique), length)
logit1 <-
lm(outcome_1~ ., df[ , values_count > 1])
What's going on? Am I blind in seeing some variable that is secretly saying it has more than one unique value and does not?
Thank you!
The regression works on the supplied data for simple models, such as
logit1 <-glm(outcome_1~ sex + age, data = df, family = "binomial")
It's a small data set with lots of variables, the computer is not going to be able to pull out the meaningful relationships even if they are there. Start with some exploratory data plots, and think about how the (biological) relationship between your outcomes and other variables in order to come up with hypotheses you can test with you data. Realistically, which measurements do you think actually affect patient outcomes?
I have the following data set:
dat <- structure(list(
cell_name = structure(c(
1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L
), .Label = c("Px", "Cx", "Mx", "Ox", "OC"), class = "factor"),
gexp = c(
0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0.078053491967664,
0.0787946080465952, 0.0849179303351091, 0.0893393503333397,
0.0904401481651504, 0.108991747968639, 0.109472235592895,
0.120876521863314, 0.121633996276386, 0.133260178961047,
0.141422491346724, 0.151765761772331, 0.163039227361379,
0.181821496314555, 0.183023962970076, 0.185012779171506,
0.190674320101334, 0.191500130355834, 0.245151812914058,
0.251786197407558, 0.268528061492397, 0.303601828212538,
0.33030785071184, 0.380051212059645, 0.409937261758804, 0.413185421525087
), sample.category = structure(c(
2L, 1L, 1L, 1L, 1L, 1L,
3L, 1L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 3L,
1L, 1L, 3L, 1L, 3L, 2L, 1L, 1L, 1L, 3L, 2L, 3L, 1L, 1L, 3L,
1L, 1L, 1L, 1L, 3L, 1L, 1L, 1L, 1L, 1L, 1L, 3L, 3L, 1L, 1L,
1L, 2L, 3L, 1L, 3L, 1L, 3L, 1L, 1L, 1L, 2L, 1L, 1L, 1L, 2L,
1L, 3L, 3L, 1L, 3L, 3L, 1L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 2L,
1L, 2L, 1L, 2L, 1L, 1L, 3L, 3L, 2L, 1L, 1L, 3L, 1L, 1L, 3L,
1L, 2L, 1L, 1L, 1L, 3L, 3L, 1L, 1L, 1L, 1L, 2L, 2L, 1L, 2L,
2L, 2L, 3L, 3L, 2L, 2L, 2L, 2L, 3L, 2L, 2L, 3L, 1L, 1L, 1L,
1L, 2L, 1L, 1L, 1L, 2L, 1L, 1L, 1L, 1L
), .Label = c(
"Xt.NT.0hr",
"Xt.Saline.16hr", "Xt.Compound.16hr"
), class = "factor"),
x = c(
-6.12836877150557, -7.88484374327681, -6.18700496001265,
-6.45607224745772, -6.91398421568892, -5.17557040495894,
-5.00434676451704, -5.90220013899824, -5.52279416365645,
-7.23571482939741, -4.00645772261641, -7.60095492644331,
-6.57969895644209, -5.71780339522383, -7.29465762419722,
-6.09494725508711, -6.92634764952681, -7.31916800780318,
-7.69346801085493, -4.80783835692427, -5.9156226281645, -6.23338071150801,
-6.39048472685835, -3.98181144042036, -7.35286227507613,
-6.37823573393843, -5.96767512602827, -4.74095240874312,
-7.12100688262007, -7.69579879088423, -7.40592185301802,
-5.54702035231612, -7.33453170104048, -6.12831488890669,
-7.86401644988081, -5.20023671431563, -6.26484719557783,
-7.81010619444868, -6.60071936888716, -7.31798640532515,
-4.35606614394209, -6.38609496397993, -7.18059436125777,
-6.27779713912031, -7.20054999632857, -7.76712313933394,
-5.52495375914595, -6.24379435820601, -5.23566857619307,
-6.05110780043623, -6.87949982924483, -7.27079001708052,
-6.85096398634932, -5.3437461022856, -3.93442956252119, -7.59850207610152,
-7.65125361723921, -6.25943747801802, -7.33143512053511,
-6.33743230147383, -6.08643952651045, -7.55096713347456,
-7.11144343657515, -5.95002309126875, -6.10922948164961,
-7.18890372557661, -7.12671843810103, -6.24059716506026,
-4.30699292464278, -5.66289655013106, -4.80185262007735,
-7.13948622984907, -6.67150870604536, -7.37687579436323,
-7.78391352934859, -7.2490023736479, -5.74496260924361, -6.03136102004073,
-7.06212893767378, -6.37314883513472, -5.33852473540327,
-6.11003104491255, -5.68365517897627, -7.04923526091597,
-5.93282214446089, -6.32528439803145, -4.86897603316328,
-7.29054347319624, -7.63038436217329, -5.71889964385054,
-6.09542743010542, -4.82401458067915, -5.97893325133345,
-6.71384087843916, -7.20524493498823, -4.3980297212126, -4.11487237257979,
-6.85030833525679, -6.87816754622481, -7.87402716918013,
-5.62621775908491, -4.99655858321211, -4.66852847380659,
-7.57268325133345, -5.39896384520552, -6.60474101347945,
-7.77267066283247, -7.69671145720503, -5.77326957030318,
-7.80957309050581, -4.55219546599409, -6.01630631728194,
-5.50212136549971, -7.76106826109907, -4.21713153166792,
-7.63483706755659, -7.89539233489058, -4.19935838027022,
-5.78868190093062, -5.27231732649824, -6.6918529634001, -7.19847861571333,
-6.77350703520796, -7.29259482665083, -7.66503230376265,
-5.92225924773238, -5.94090358061812, -4.94412461562178,
-5.27848092360518, -6.46139279646895, -4.23630992217085,
-6.28692427916548, -5.00668660445235, -5.03211299223921,
-7.29572287840864, -5.33259049696944
), y = c(
-5.02424657839496,
-6.71462500590045, -4.64553797739703, -4.8909190942641, -5.71065485972125,
-4.82234514254291, -5.28217733401019, -3.866351013362, -4.37375534075458,
-6.48378050821979, -1.45741885650117, -5.84999812144, -5.37730658549029,
-5.34863889712054, -5.33161938685138, -4.89835823076923,
-5.95062935847003, -4.7071119596358, -6.26194823282916, -5.22036922472674,
-4.32524025934894, -3.79248035448749, -4.39562714594562,
-5.28746831911761, -4.56550610560138, -5.81744492548663,
-1.6384685088988, -2.85430014628131, -6.03716719645221, -7.30025113123614,
-7.1568714429732, -6.01424372690875, -6.00170434015948, -3.03584480780322,
-6.57955277460773, -4.41522968310077, -5.37504447001178,
-6.52249014872272, -4.75782311457355, -4.62974846857745,
-4.80379808443744, -4.52536237734515, -5.20433223742206,
-4.70545566576678, -6.43369257944781, -6.41709864634235,
-4.82305062311847, -2.91744268435199, -4.4250496675368, -5.37218845385272,
-4.68633187311847, -2.56733632582385, -2.32696414488513,
-2.86756802099902, -5.36454570788104, -6.49232972162921,
-7.18896258372027, -5.87897027033527, -5.03146756190021,
-3.6963902761336, -4.67556036013324, -7.27969754236896, -4.89728296297748,
-4.84503138560016, -3.55614126223285, -2.56781030195911,
-6.00860703486163, -2.6597498704787, -5.33996284502704, -3.27229035395343,
-5.52028096216876, -6.94654047983844, -5.05352461832721,
-4.85841691988666, -6.13735354441363, -2.54840064543445,
-2.09675896662433, -4.46512854593951, -6.61105263727862,
-6.10234320658404, -4.03706944483478, -4.91794002550799,
-2.51595926779468, -4.77913272875506, -2.05771953362186,
-5.00882280367572, -5.52451956766803, -6.13459790247638,
-6.88176024454791, -5.43877637881, -4.64195335406024, -2.93488967913348,
-4.90864980715472, -3.13988101977069, -2.98691988486011,
-2.36754042404849, -1.61479792493541, -2.42353100079257,
-5.22080147761066, -6.9881349851485, -2.50280356901843, -2.4409405042525,
-5.19058645266254, -6.65987217920978, -5.1250020315047, -4.80788171786029,
-6.56144059199054, -7.22644150751788, -5.39587510126788,
-6.68008864420611, -3.0989873458739, -3.61012685793597, -3.17221487063129,
-7.18178618448932, -2.10658872622211, -7.30663311976153,
-7.09194481867511, -1.59743498015363, -2.7611458351012, -5.10656679171283,
-2.5872366477843, -3.18813729780871, -6.07152092951495, -4.62400186556537,
-6.3747350026961, -2.67246747511584, -6.22303259867389, -2.53317165869433,
-2.45842046040256, -3.20620191591937, -1.96011829870898,
-2.98910189169604, -2.67913473147113, -1.90748242038447,
-6.58255875605304, -2.57597566145618
), cluster = c(
1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1
)
), row.names = c(NA, -136L), class = c("tbl_df", "tbl","data.frame"))
I am making the histogram and scatter plot with this code:
library(tidyverse)
library(ggpubr)
## Making Barplot (histogram)
nbp <- ggpubr::ggbarplot(dat, x = "sample.category", y = "gexp", facet.by = "cell_name", add = "mean_se", scales = "fixed") +
theme(strip.text.x = element_text(size = 20, colour = "black", face = "bold")) +
theme(legend.position = "none") +
xlab("") +
theme(axis.text.x = element_text(angle = 90, hjust = 1, vjust = 0.5))
nbp
## Making scatter plot (histogram)
pge <- ggplot(dat, aes(x, y, color = gexp)) +
geom_point(alpha = 0.9, size = 5) +
scale_color_gradient(low = "#ededed", high = "#67000d", na.value = "#f0f0f0") +
facet_wrap(cell_name ~ sample.category, scales = "free", ncol = 3) +
theme_bw() +
xlab("UMAP 1") +
ylab("UMAP 2")
pge
The bar plot looks like this:
And scatter plot with color scale looks like this:
As you can see in the histogram clearly Xt.Saline.16hr is stronger
than Xt.NT.0hr. But in the scatter plot color scale we get the vivid
impression that Xt.NT.0hr is stronger than Xt.Saline.16hr.
How can I adjust the color scale in scatter plot so that it
matches the histogram?
I post an answer to be able to show some plots.
As remarked in the comment, your plot seems correct. However, boxplot alone might be misleading in this case.
If you add the actual points you will see that many NT points are equal to 0 and some of them peaks just above 0.4. Please, see the plot below, I have used your color scale and geom_jitter to show distribution of your points for the gexp variable.
library(ggplot2)
ggplot(data = dat, aes(x = sample.category, y = gexp, color = gexp)) +
# geom_boxplot() +
facet_grid(.~sample.category, scale = "free_x") +
geom_jitter() +
scale_color_gradient(low = "#ededed", high = "#67000d", na.value = "#f0f0f0") +
theme_bw()
I am trying to make a stacked barplot with two variables. My desired outcome looks like this:
This is the first part of my data. There are 220 more rows:
Type Week Stage
<chr> <dbl> <dbl>
1 Captured 1 2
2 Captured 1 1
3 Captured 1 1
4 Captured 1 2
5 Captured 1 1
6 Captured 1 3
7 Captured 1 NA
8 Captured 1 3
9 Captured 1 2
10 Captured 1 1
So far I'm not getting anywhere, this is my code so far
library(data.table)
dat.m <- melt(newrstudio2, id.vars="Type")
dat.m
library(ggplot2)
ggplot(dat.m, aes(x=Type, y=value, fill=variable)) +
geom_bar(stat="identity")
I guess I need to calculate the number of observations of each stage in each week of each type? I've tried both long and wide data, but I somehow need to combine week with type? I don't know, I'm at a loss.
Alternative way:
set.seed(123)
# sample data
my_data <- data.frame(Type = sample(c("W", "C"), 220, replace = TRUE),
Week = sample(paste0("Week ", 1:4), 220, replace = TRUE),
Stage = sample(paste0('S', 1:4), 220, replace = TRUE))
head(my_data)
library(ggplot2)
ggplot(my_data, aes(x = Type, fill = Stage)) +
geom_bar(aes(y = (..count..)/sum(..count..)), position = "fill") +
facet_grid(. ~ Week, switch="both") +
scale_y_continuous(labels = scales::percent) +
ylab("Stage [%]") +
theme(strip.background = element_blank(),
strip.placement = "outside",
panel.spacing = unit(0, "lines"))
Alternatively we could use base graphics. First, what you're probably most interested in, we should reshape the data.
For this we could split the data per week and run a dcast() over it.
L <- lapply(split(d, d$week), function(x)
data.table::dcast(x, type ~ stage, value.var="stage", fun=length))
d2 <- do.call(rbind, L) # transform back into a data frame
Now – with credits to #alemol – we want the proportions.
d2[-1] <- t(apply(d2[-1], 1, prop.table))
Then we are able to plot relatively simply. Note, that barplot() additionally gives us a vector of bar coordinates which we can use later for the axis() labels.
cols <- c("#ed1c24", "#ff7f27", "#00a2e8", "#fff200") # define stage colors
par(mar=c(5, 5, 3, 5) + .1, xpd=TRUE) # set plot margins
p <- barplot(t(d2[-1]), col=cols, border="white", space=rep(c(.2, 0), 5),
font.axis=2, xaxt="n", yaxt="n", xlab="Week")
axis(1, at=p, labels=rep(c("C", "W"), 5), tick=FALSE, line=0)
axis(1, at=apply(matrix(p, , 2, byrow=TRUE), 1, mean), labels=1:5, tick=FALSE, line=1)
axis(2, at=0:10/10, labels=paste0(seq(0, 100, 10), "%"), line=0, las=2)
legend(12, .5, legend=rev(names(d2[-1])), col=rev(cols), pch=15, title="Stage")
Result:
Data:
d <- structure(list(type = structure(c(1L, 1L, 1L, 1L, 1L, 2L, 2L,
2L, 2L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 2L, 2L, 2L, 1L, 1L, 2L, 2L,
1L, 2L, 2L, 2L, 1L, 1L, 2L, 2L, 1L, 2L, 1L, 2L, 2L, 1L, 1L, 2L,
2L, 2L, 1L, 1L, 1L, 1L, 2L, 1L, 1L, 2L, 2L, 1L, 1L, 2L, 2L, 2L,
2L, 1L, 1L, 2L, 2L, 2L, 1L, 2L, 2L, 2L, 2L, 2L, 1L, 2L, 2L, 1L,
2L, 1L, 1L, 1L, 2L, 1L, 2L, 2L, 2L, 1L, 2L, 2L, 1L, 2L, 1L, 2L,
1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 1L, 1L, 2L, 2L, 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3L, 4L, 2L, 4L, 1L, 1L, 2L, 1L, 3L,
2L, 1L, 3L, 3L, 2L, 2L, 1L, 3L, 2L, 2L, 2L, 1L, 4L, 2L, 4L, 2L,
4L, 3L, 3L, 1L, 3L, 4L, 3L, 2L, 1L, 2L, 4L, 1L, 2L, 4L, 2L, 1L,
2L, 1L, 2L, 2L, 3L, 1L, 3L, 3L, 3L, 2L, 2L, 1L, 2L, 3L, 2L, 2L,
1L, 2L, 1L, 3L, 3L, 2L, 1L, 3L, 4L, 2L, 1L, 2L, 4L, 3L, 4L, 2L,
3L, 2L, 4L, 1L, 4L, 4L, 2L, 1L, 2L)), row.names = c(NA, -250L
), class = "data.frame")
Is this what you're looking for:
set.seed(123)
# sample data
my_data <- data.frame(Type = sample(paste0('T', 1:4), 220, replace = TRUE),
Week = sample(paste0('W', 1:4), 220, replace = TRUE),
Stage = sample(paste0('S', 1:4), 220, replace = TRUE))
ggplot(my_data, aes(x=Week:Type, fill = Stage)) + geom_bar()
I'm looking for an elaboration on the amazing answer already provided about creating an interaction plot with a continuous and categorical variable using the predict function of the (development version) of the lme4 package.
I have run a model with an interaction between three categorical variables: discount_i (0/1), rank_i (0/1), and msg ("No norm","Provincial",and "Norm") including subject random effects (id). My outcome variable (choice) is dichotomous. Specifically, my command is:
m1 <- glmer(choice ~ msg*discount_i*rank_i + (1|id), data=df, family="binomial")
I then create a prediction frame:
predframe <- with(df,expand.grid(rank_i=levels(rank_i),msg=levels(msg),discount_i=levels(discount_i)))
And use the predict function (EDITED):
predframe$pred.logit <- predict(m1,newdata=predframe,REform=NA)
However, this is the point where I part ways with #mnel's instructions. How would I go about graphing the three way interaction between factor variables, rather than a two way interaction between a factor variable and a continuous variable?
Sample data below:
> dput(df[1:700,2:6])
structure(list(time = c(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1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2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L,
1L, 2L, 1L, 1L, 1L, 2L, 2L, 1L, 2L, 1L, 1L, 2L, 2L, 2L, 2L,
1L, 1L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 1L, 2L, 1L,
2L, 2L, 1L, 2L, 2L, 2L, 1L, 2L, 1L, 1L, 1L, 2L, 2L, 2L, 2L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 1L, 1L, 1L, 1L,
1L), .Label = c("0", "1"), class = "factor")), .Names = c("time",
"choice", "msg", "discount_i", "rank_i"), row.names = c("1.1",
"2.1", "3.1", "4.1", "5.1", "6.1", "7.1", "8.1", "9.1", "10.1",
"11.1", "12.1", "13.1", "14.1", "15.1", "16.1", "17.1", "18.1",
"19.1", "20.1", "21.1", "22.1", "23.1", "24.1", "25.1", "26.1",
"27.1", "28.1", "29.1", "30.1", "31.1", "32.1", "33.1", "34.1",
"35.1", "36.1", "37.1", "38.1", "39.1", "40.1", "41.1", "42.1",
"43.1", "44.1", "45.1", "46.1", "47.1", "48.1", "49.1", "50.1",
"51.1", "52.1", "53.1", "54.1", "55.1", "56.1", "57.1", "58.1",
"59.1", "60.1", "61.1", "62.1", "63.1", "64.1", "65.1", "66.1",
"67.1", "68.1", "69.1", "70.1", "71.1", "72.1", "73.1", "74.1",
"75.1", "76.1", "77.1", "78.1", "79.1", "80.1", "81.1", "82.1",
"83.1", "84.1", "85.1", "86.1", "87.1", "88.1", "89.1", "90.1",
"91.1", "92.1", "93.1", "94.1", "95.1", "96.1", "97.1", "98.1",
"99.1", "100.1", "101.1", "102.1", "103.1", "104.1", "105.1",
"106.1", "107.1", "108.1", "109.1", "110.1", "111.1", "112.1",
"113.1", "114.1", "115.1", "116.1", "117.1", "118.1", "119.1",
"120.1", "121.1", "122.1", "123.1", "124.1", "125.1", "126.1",
"127.1", "128.1", "129.1", "130.1", "131.1", "132.1", "133.1",
"134.1", "135.1", "136.1", "137.1", "138.1", "139.1", "140.1",
"141.1", "142.1", "143.1", "144.1", "145.1", "146.1", "147.1",
"148.1", "149.1", "150.1", "151.1", "152.1", "153.1", "154.1",
"155.1", "156.1", "157.1", "158.1", "159.1", "160.1", "161.1",
"162.1", "163.1", "164.1", "165.1", "166.1", "167.1", "168.1",
"169.1", "170.1", "171.1", "172.1", "173.1", "174.1", "175.1",
"176.1", "177.1", "178.1", "179.1", "180.1", "181.1", "182.1",
"183.1", "184.1", "185.1", "186.1", "187.1", "188.1", "189.1",
"190.1", "191.1", "192.1", "193.1", "194.1", "195.1", "196.1",
"197.1", "198.1", "199.1", "200.1", "201.1", "202.1", "203.1",
"204.1", "205.1", "206.1", "207.1", "208.1", "209.1", "210.1",
"211.1", "212.1", "213.1", "214.1", "215.1", "216.1", "217.1",
"218.1", "219.1", "220.1", "221.1", "222.1", "223.1", "224.1",
"225.1", "226.1", "227.1", "228.1", "229.1", "230.1", "231.1",
"232.1", "233.1", "234.1", "235.1", "236.1", "237.1", "238.1",
"239.1", "240.1", "241.1", "242.1", "243.1", "244.1", "245.1",
"246.1", "247.1", "248.1", "249.1", "250.1", "251.1", "252.1",
"253.1", "254.1", "255.1", "256.1", "257.1", "258.1", "259.1",
"260.1", "261.1", "262.1", "263.1", "264.1", "265.1", "266.1",
"267.1", "268.1", "269.1", "270.1", "271.1", "272.1", "273.1",
"274.1", "275.1", "276.1", "277.1", "278.1", "279.1", "280.1",
"281.1", "282.1", "283.1", "284.1", "285.1", "286.1", "287.1",
"288.1", "289.1", "290.1", "291.1", "292.1", "293.1", "294.1",
"295.1", "296.1", "297.1", "298.1", "299.1", "300.1", "301.1",
"302.1", "303.1", "304.1", "305.1", "306.1", "307.1", "308.1",
"309.1", "310.1", "311.1", "312.1", "313.1", "314.1", "315.1",
"316.1", "317.1", "318.1", "319.1", "320.1", "321.1", "322.1",
"323.1", "324.1", "325.1", "326.1", "327.1", "328.1", "329.1",
"330.1", "331.1", "332.1", "333.1", "334.1", "335.1", "336.1",
"337.1", "338.1", "339.1", "340.1", "341.1", "342.1", "343.1",
"344.1", "345.1", "346.1", "347.1", "348.1", "349.1", "350.1",
"351.1", "352.1", "353.1", "354.1", "355.1", "356.1", "357.1",
"358.1", "359.1", "360.1", "361.1", "362.1", "363.1", "364.1",
"365.1", "366.1", "367.1", "368.1", "369.1", "370.1", "371.1",
"372.1", "373.1", "374.1", "375.1", "376.1", "377.1", "378.1",
"379.1", "380.1", "381.1", "382.1", "383.1", "384.1", "385.1",
"386.1", "387.1", "388.1", "389.1", "390.1", "391.1", "392.1",
"393.1", "394.1", "395.1", "396.1", "397.1", "398.1", "399.1",
"400.1", "401.1", "402.1", "403.1", "404.1", "405.1", "406.1",
"407.1", "408.1", "409.1", "410.1", "411.1", "412.1", "413.1",
"414.1", "415.1", "416.1", "417.1", "418.1", "419.1", "420.1",
"421.1", "422.1", "423.1", "424.1", "425.1", "426.1", "427.1",
"428.1", "429.1", "430.1", "431.1", "432.1", "433.1", "434.1",
"435.1", "436.1", "437.1", "438.1", "439.1", "440.1", "441.1",
"442.1", "443.1", "444.1", "445.1", "446.1", "447.1", "448.1",
"449.1", "450.1", "451.1", "452.1", "453.1", "454.1", "455.1",
"456.1", "457.1", "458.1", "459.1", "460.1", "461.1", "462.1",
"463.1", "464.1", "465.1", "466.1", "467.1", "468.1", "469.1",
"470.1", "471.1", "472.1", "473.1", "474.1", "475.1", "476.1",
"477.1", "478.1", "479.1", "480.1", "481.1", "482.1", "483.1",
"484.1", "485.1", "486.1", "487.1", "488.1", "489.1", "490.1",
"491.1", "492.1", "493.1", "494.1", "495.1", "496.1", "497.1",
"498.1", "499.1", "500.1", "501.1", "502.1", "503.1", "504.1",
"505.1", "506.1", "507.1", "508.1", "509.1", "510.1", "511.1",
"512.1", "513.1", "514.1", "515.1", "516.1", "517.1", "518.1",
"519.1", "520.1", "521.1", "522.1", "523.1", "524.1", "525.1",
"526.1", "527.1", "528.1", "529.1", "530.1", "531.1", "532.1",
"533.1", "534.1", "535.1", "536.1", "537.1", "538.1", "539.1",
"540.1", "541.1", "542.1", "543.1", "544.1", "545.1", "546.1",
"547.1", "548.1", "549.1", "550.1", "551.1", "552.1", "553.1",
"554.1", "555.1", "556.1", "557.1", "558.1", "559.1", "560.1",
"561.1", "562.1", "563.1", "564.1", "565.1", "566.1", "567.1",
"568.1", "569.1", "570.1", "571.1", "572.1", "573.1", "574.1",
"575.1", "576.1", "577.1", "578.1", "579.1", "580.1", "581.1",
"582.1", "583.1", "584.1", "585.1", "586.1", "587.1", "588.1",
"589.1", "590.1", "591.1", "592.1", "593.1", "594.1", "595.1",
"596.1", "597.1", "598.1", "599.1", "600.1", "601.1", "602.1",
"603.1", "604.1", "605.1", "606.1", "607.1", "608.1", "609.1",
"610.1", "611.1", "612.1", "613.1", "614.1", "615.1", "616.1",
"617.1", "618.1", "619.1", "620.1", "621.1", "622.1", "623.1",
"624.1", "625.1", "626.1", "627.1", "628.1", "629.1", "630.1",
"631.1", "632.1", "633.1", "634.1", "635.1", "636.1", "637.1",
"638.1", "639.1", "640.1", "641.1", "642.1", "643.1", "644.1",
"645.1", "646.1", "647.1", "648.1", "649.1", "650.1", "651.1",
"652.1", "653.1", "654.1", "655.1", "656.1", "657.1", "658.1",
"659.1", "660.1", "661.1", "662.1", "663.1", "664.1", "665.1",
"666.1", "667.1", "668.1", "669.1", "670.1", "671.1", "672.1",
"673.1", "674.1", "675.1", "676.1", "677.1", "678.1", "679.1",
"680.1", "681.1", "682.1", "683.1", "684.1", "685.1", "686.1",
"687.1", "688.1", "689.1", "690.1", "691.1", "692.1", "693.1",
"694.1", "695.1", "696.1", "697.1", "698.1", "699.1", "700.1"
), class = "data.frame")
Caveat: Have not added the ylab="" to the second overlaid plot call and you probably want to use a non-default ylab for the first one, too. Since the details of this analysis are still opaque to me, I am not standing behind its validity. (Just turning the crank on the machinery.) And there would need to be some further work on the legend. Furthermore, teh ylims were different so would probably want to set them to min and max for c(newpred0, newpred1).
newpred0 <- predict(m1, newdata = predframe[predframe$discount_i=="0", ] ,
REform = NA)
interaction.plot(droplevels(predframe[predframe$discount_i=="0", ])$rank_i,
droplevels(predframe[predframe$discount_i=="0", ])$msg,
newpred0)
newpred1 <- predict(m1, newdata = predframe[predframe$discount_i=="1", ] ,
REform = NA)
par(new=TRUE); # This is the way to overlay base graphics on top of each other
interaction.plot(droplevels(predframe[predframe$discount_i=="1", ])$rank_i,
droplevels(predframe[predframe$discount_i=="1", ])$msg, newpred1,
col="red")