Home > Enterprise >  Pick a point approach: plotting interaction effects in R
Pick a point approach: plotting interaction effects in R

Time:03-10

On https://philippmasur.de/2018/11/26/visualizing-interaction-effects there is a nice guide to plot interaction effects using a "pick a point approach". Before plotting the data you first seperate the interaction variable in subgroups like this:

data$m_groups <- cut(data$m, breaks = 3) %>% 
  factor(., labels = c("small", "average", "large"))

An then you create the plot:

ggplot(data,
       aes(x = x,
           y = y,
           color = m_groups))  
  geom_point(size = .9,
             alpha = .3)  
  geom_smooth(method = "lm")  
  theme_bw()  
  scale_color_brewer(type = "qual", 
                     palette = 3)  
  labs(x = "Independent variable",
       y = "Dependent variable",
       color = "Moderator")

I would love to replicate this, but I wonder if it would be possible to do the same using estimates of previously estimated model stored in "model1". Also it would be cool to additionally print the impact of x on y in the same plot, without the interaction effect.

This is my model:

model1 <- glm(onset2.x ~ lag(log_imputed_welfarespending_pc)*lag(transnational_rebels)
               lag(gdplog)
               lag(poplog)
               lag(polity2),  
              data = Dseto,
              family = "binomial") 

This would be a sample of my data:

structure(list(onset2.x = c(0, 1, 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, 1, 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, 1, 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, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
0), `lag(log_imputed_welfarespending_pc)` = c(6.7593012777903, 
6.5906651231998, 6.50502624381137, 6.50375350858505, 6.60013892854931, 
6.72307194461142, 6.68548583719299, 6.62819738098995, 6.63618415738598, 
6.50024422126998, 6.50859287618424, 6.51474333156085, 6.51303356711169, 
6.50410023796533, 6.47540904346829, 6.50448499973986, 5.80998735207235, 
5.81727705379893, 5.86565445193157, 5.88304057424441, 5.90629080655763, 
6.39735921904453, 6.40432058443487, 6.40681836643474, 6.38066960492512, 
6.32262127230174, 6.29648764310864, 6.30077399968124, 6.29255371793041, 
6.19051187121207, 6.2854711365989, 4.54171407987324, 5.19711233242954, 
4.9951024728486, 5.31270465505435, 5.48507180067194, 6.2787268191481, 
6.27640989581023, 6.26492726868929, 6.23395262180202, 6.24228589937542, 
6.1402090797172, 5.84791912318113, 5.83972272235945, 5.81808995573724, 
5.89182589874581, 5.74104429959878, 5.8934828939952, 5.84896421451316, 
6.0252962337323, 6.22562887233741, 5.55472442327534, 5.67906668023923, 
5.51481042780819, 5.34225581627061, 5.34852764652201, 5.35080193437077, 
5.43680855172959, 5.59393617048443, 5.31146885957853, 5.14065556537129, 
4.88244311686533, 5.05457962003214, 5.05979678947098, 5.12654284155797, 
5.4173077082201, 5.76073649760509, 5.74816054630802, 5.79653435521148, 
5.76445365691988, 5.74182996383328, 5.92100430517148, 6.06020904563041, 
6.08155124801804, 5.93696326010604, 5.94686580396636, 5.99590247752847, 
6.00052886249622, 6.00802189552364, 6.03485558392025, 6.05484897640356, 
5.96728319447562, 5.97852258945032, 5.90273690561223, 5.91592671848331, 
6.35905603987472, 6.40363367624246, 6.40415950220562, 6.40346552337157, 
6.40448184309475, 6.3320718407156, 6.31350811225528, 6.28185948558745, 
6.29688867341038, 6.39091995545888, 6.61802974626007, 6.33657237498927, 
6.34286788325571, 6.33799676152726, 6.31560759206842, 6.32860447339924, 
6.32090378649727, 6.31257977785617, 5.40239239031352, 5.09806665906792, 
5.46029246370603, 5.45984978281935, 5.58351716307006, 5.54560542668313, 
5.26925561323958, 5.00517909836388, 4.62051016477804, 4.72632154352706, 
4.5752065958078, 4.15696401352561, 4.69873633461989, 4.69979146106265, 
4.74969138049163, 4.73520341810156, 4.8485349459804, 4.95373932684421, 
5.66753744442454, 5.67517441196631, 5.4284841129904, 5.51249426931899, 
5.71450273411283, 5.7321066488029, 5.75282161882761, 5.72596369339562, 
5.71890681649695, 5.71707020116874, 5.71106748315148, 5.20571102825013, 
4.41320796788416, 5.25536308107672, 5.32366281172743, 5.39358224136751, 
5.46353641497876, 4.72101190282858, 4.9126812508147, 5.29389002253102, 
5.22823877721054, 5.11387411791962, 4.99437030042334, 4.74239262414368, 
4.69487990813813, 4.33798777452307, 4.39856302658009, 4.41690953681107, 
4.88815568074723, 5.22520884168216, 5.28608794272274, 5.34062358239678, 
5.45945902795985, 5.96638809144697, 5.95635067167791, 5.88208580243543, 
5.88788455586211, 5.84935296040707, 5.80297163254775, 5.76343493422252, 
6.08991622885961, 6.42592709685561, 5.92029686695, 5.98221612153052, 
6.02981836218773, 6.07779628901138, 6.78128406974621, 6.96649037212505, 
7.0033865280206, 6.87277447972167, 6.90983783812385, 6.78190737093829, 
6.73291453851197, 6.64200871902559, 6.61076851480164, 6.69150641625194, 
6.60936979672534, 6.67863359373105, 6.77136556116043, 6.84938813142573, 
6.93235720614668, 6.96529370343599, 7.00862541779949, 7.04668910046469, 
7.05358695019309, 7.02144179639766, 7.07664392613196, 7.09760130009063, 
7.09530294862347, 6.85251095162095, 6.83232379265712, 6.38984847993806, 
6.41212432113084, 6.33069437847785, 6.36297750876316, 6.7402153683844, 
6.30117321018759, 6.23198008322439, 6.38427624182517, 6.48660400776099
), `lag(transnational_rebels)` = c(-6, -6, -6, -6, -6, -6, -6, 
-6, -6, -6, -6, -6, -6, -6, -6, -6, -6, -6, -6, -6, -6, -6, -6, 
-6, -6, -6, -6, -6, -6, -6, -6, -6, -6, -6, -6, -6, -6, -6, -6, 
-6, -6, -6, -6, -6, -6, -6, -6, -6, -6, -6, -6, -6, -6, -6, -6, 
-6, -6, -6, -6, -6, -6, -6, -6, -6, -6, -6, -6, -6, -6, -6, -6, 
-6, -6, -6, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, 
-5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, 
-5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, 
-5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -3, -3, -3, 
-3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, 
-3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -2, -2, -2, -2, -2, -2, 
-2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, 
-2, -2, -2, -2, -2, -2, -2, -4, -4, -4, -4, -4, -4, -4, -4, -4, 
-4, -4), `lag(gdplog)` = c(8.09759616851807, 7.93351030349731, 
7.92859935760498, 7.98602199554443, 8.05552196502686, 8.11380577087402, 
8.14639472961426, 8.15403270721436, 8.15104961395264, 8.15854740142822, 
8.20108222961426, 8.25206184387207, 8.27090454101562, 8.27844905853271, 
8.26529026031494, 8.26880836486816, 7.05415916442871, 7.10523176193237, 
7.15003967285156, 7.14428615570068, 7.1816930770874, 7.20431327819824, 
7.21993255615234, 7.22550439834595, 7.19265365600586, 7.14027070999146, 
7.15158843994141, 7.15444850921631, 7.1689510345459, 7.12240314483643, 
7.17843914031982, 7.16445398330688, 7.16776943206787, 7.19582462310791, 
7.18890523910522, 7.17078542709351, 7.19680023193359, 7.1955418586731, 
7.20193862915039, 7.22554540634155, 7.23138570785522, 7.15211391448975, 
7.16625261306763, 7.2053689956665, 7.23467016220093, 7.73596143722534, 
7.7418212890625, 7.73381376266479, 7.71063137054443, 7.68661880493164, 
7.7166690826416, 7.72133159637451, 7.76572513580322, 7.73540782928467, 
7.58609390258789, 7.61344385147095, 7.61803722381592, 7.69062852859497, 
7.74523019790649, 7.63229513168335, 7.4865026473999, 7.41296291351318, 
7.41430044174194, 7.39052724838257, 7.41816377639771, 7.5203800201416, 
7.58498859405518, 7.59230804443359, 7.6415958404541, 7.618408203125, 
7.61151885986328, 7.62525463104248, 7.61269044876099, 7.64754676818848, 
7.93900680541992, 7.96620273590088, 8.0360164642334, 8.05749988555908, 
8.06578540802002, 8.10776615142822, 8.17153263092041, 8.10323524475098, 
8.08580303192139, 8.02828025817871, 8.05747509002686, 8.11264228820801, 
8.16908836364746, 8.1848669052124, 8.16506767272949, 8.17934513092041, 
8.11813354492188, 8.11663150787354, 8.0961332321167, 8.12638473510742, 
8.16321468353271, 8.19132137298584, 8.19746589660645, 8.21558380126953, 
8.20100212097168, 8.18875789642334, 8.21648597717285, 8.21533966064453, 
8.22772312164307, 6.94617080688477, 6.99234104156494, 7.01319980621338, 
7.03762006759644, 7.03415966033936, 7.01221466064453, 6.97571992874146, 
6.95642042160034, 6.8946967124939, 6.83216190338135, 6.80883121490479, 
6.77033948898315, 6.7224006652832, 6.72465753555298, 6.73100996017456, 
6.74561643600464, 6.7680869102478, 6.79629993438721, 6.78933143615723, 
6.80787563323975, 6.83053922653198, 6.85379886627197, 6.87458801269531, 
6.90154933929443, 6.92963361740112, 6.91316938400269, 6.91738557815552, 
6.91369247436523, 6.91807746887207, 6.82085371017456, 6.85757064819336, 
6.90036249160767, 6.97865772247314, 7.06007862091064, 7.1407413482666, 
7.25112581253052, 7.30434942245483, 7.23709726333618, 7.17668151855469, 
7.17741203308105, 7.18725919723511, 7.15863800048828, 7.1729416847229, 
7.20700025558472, 7.23641300201416, 7.24051523208618, 7.23921585083008, 
7.2478289604187, 7.26179599761963, 7.27459764480591, 7.30448150634766, 
7.28587675094604, 7.29329490661621, 7.27750825881958, 7.2414436340332, 
7.18665456771851, 7.18668127059937, 7.16614484786987, 7.68012046813965, 
7.5440731048584, 7.56296157836914, 7.6323938369751, 7.69067096710205, 
7.7600269317627, 7.82392549514771, 7.85512781143188, 7.73051691055298, 
7.67579793930054, 7.73610353469849, 7.78830051422119, 7.826096534729, 
7.87319087982178, 7.92678213119507, 8.00985717773438, 8.02846336364746, 
8.0870304107666, 8.184494972229, 8.23387622833252, 8.27194976806641, 
8.35656833648682, 8.4128589630127, 8.46263217926025, 8.48111152648926, 
8.46078777313232, 8.49246501922607, 8.51386833190918, 8.5241117477417, 
8.8844051361084, 8.86780738830566, 8.8311882019043, 8.8820743560791, 
8.82001304626465, 8.9017448425293, 8.92725944519043, 8.85401916503906, 
8.78895473480225, 8.8129940032959, 8.8208703994751), `lag(poplog)` = c(14.6348810195923, 
14.6553297042847, 14.675742149353, 14.6961965560913, 14.7167110443115, 
14.7372531890869, 14.7577867507935, 14.7782516479492, 14.7985992431641, 
14.8188190460205, 14.8388938903809, 14.8587589263916, 14.8783407211304, 
14.8975868225098, 14.9164724349976, 14.9349985122681, 15.7194910049438, 
15.7484312057495, 15.7772645950317, 15.8059396743774, 15.8344058990479, 
15.8626041412354, 15.8904838562012, 15.918007850647, 15.9451494216919, 
15.9718914031982, 15.998218536377, 16.0241088867188, 16.0495071411133, 
16.0743656158447, 16.0986709594727, 16.1224136352539, 16.1455631256104, 
16.168140411377, 16.1900730133057, 16.2111396789551, 16.2310752868652, 
16.2497177124023, 16.2670440673828, 16.2831707000732, 16.2982368469238, 
16.312442779541, 16.32594871521, 16.3388233184814, 16.3511066436768, 
16.5068626403809, 16.5344524383545, 16.5618782043457, 16.5890808105469, 
16.6159172058105, 16.6422138214111, 16.6678619384766, 16.692813873291, 
16.717113494873, 16.7408638000488, 16.7642059326172, 16.7872276306152, 
16.8099479675293, 16.8323059082031, 16.8542232513428, 16.8755836486816, 
16.8963222503662, 16.9164123535156, 16.9358882904053, 16.9548015594482, 
16.9732227325439, 16.9911975860596, 17.0087490081787, 17.0258483886719, 
17.0424346923828, 17.0584239959717, 17.073766708374, 17.0884780883789, 
17.1026039123535, 18.4752349853516, 18.4988193511963, 18.5224075317383, 
18.5459594726562, 18.5694789886475, 18.5929698944092, 18.616397857666, 
18.6397686004639, 18.6629867553711, 18.6858158111572, 18.707971572876, 
18.7292633056641, 18.7496700286865, 18.7692546844482, 18.7880268096924, 
18.8060188293457, 18.823278427124, 18.839786529541, 18.8556060791016, 
18.8709506988525, 18.8860893249512, 18.9012031555176, 18.9163303375244, 
18.9314060211182, 18.9463653564453, 18.9611148834229, 18.9755687713623, 
18.9897327423096, 19.0035858154297, 15.3512516021729, 15.3756427764893, 
15.3998966217041, 15.4239921569824, 15.447772026062, 15.4710493087769, 
15.4937314987183, 15.5157833099365, 15.5373229980469, 15.5585966110229, 
15.5799188613892, 15.601505279541, 15.6233673095703, 15.6454467773438, 
15.6677742004395, 15.6903686523438, 15.7132139205933, 15.7363262176514, 
15.7596254348755, 15.7828874588013, 15.8058404922485, 15.8283023834229, 
15.8502025604248, 15.8715953826904, 15.8925800323486, 15.9133043289185, 
15.9338569641113, 15.9542484283447, 15.9744148254395, 14.8224277496338, 
14.8469953536987, 14.872031211853, 14.897500038147, 14.9235887527466, 
14.950496673584, 14.9782981872559, 15.0069885253906, 15.0363664627075, 
15.066065788269, 15.0956449508667, 15.1247892379761, 15.1534099578857, 
15.1815242767334, 15.2090759277344, 15.2360305786133, 15.2623672485352, 
15.2880611419678, 15.3131046295166, 15.3375101089478, 15.3613014221191, 
15.3845052719116, 15.4071273803711, 15.4291906356812, 15.4507541656494, 
15.4718894958496, 15.4926509857178, 15.5130567550659, 15.5331077575684, 
16.1437168121338, 16.1591453552246, 16.1737537384033, 16.1877193450928, 
16.2014179229736, 16.2153205871582, 16.2297592163086, 16.2448425292969, 
16.2604942321777, 16.276611328125, 16.2930202484131, 16.3095951080322, 
16.3262538909912, 16.343017578125, 16.3599891662598, 16.3773059844971, 
16.3950080871582, 16.4131240844727, 16.4314670562744, 16.4496154785156, 
16.4670543670654, 16.483434677124, 16.4986591339111, 16.5128517150879, 
16.5261707305908, 16.5388565063477, 16.5510921478271, 16.5629005432129, 
16.5742626190186, 17.0577735900879, 17.0740871429443, 17.0903167724609, 
17.1064739227295, 17.1224269866943, 17.1380290985107, 17.1531925201416, 
17.167854309082, 17.1820774078369, 17.1960563659668, 17.2100582122803
)), terms = onset2.x ~ lag(log_imputed_welfarespending_pc) * 
    lag(transnational_rebels)   lag(gdplog)   lag(poplog), na.action = structure(c(`40-1974` = 1L, 
`40-1975` = 2L, `40-1976` = 3L, `40-1977` = 4L, `40-1978` = 5L, 
`40-1979` = 6L, `40-1980` = 7L, `40-1981` = 8L, `40-1982` = 9L, 
`40-1983` = 10L, `40-1984` = 11L, `40-1985` = 12L, `40-1986` = 13L, 
`40-1987` = 14L, `40-1988` = 15L, `40-1989` = 16L, `40-1990` = 17L, 
`40-1991` = 18L, `40-1992` = 19L, `40-1993` = 20L, `40-1994` = 21L, 
`40-1995` = 22L, `40-1996` = 23L, `40-1997` = 24L, `40-1998` = 25L, 
`40-1999` = 26L, `40-2000` = 27L, `40-2001` = 28L, `40-2002` = 29L, 
`41-1974` = 30L, `41-1975` = 31L, `41-1976` = 32L, `41-1977` = 33L, 
`41-1978` = 34L, `41-1979` = 35L, `41-1980` = 36L, `41-1981` = 37L, 
`41-1982` = 38L, `41-1983` = 39L, `41-1984` = 40L, `41-1985` = 41L, 
`41-1986` = 42L, `41-1987` = 43L, `41-1988` = 44L, `41-1989` = 45L, 
`41-1990` = 46L, `41-1991` = 47L, `110-1975` = 263L, `110-1976` = 264L, 
`110-1977` = 265L, `110-1978` = 266L, `110-1979` = 267L, `110-1980` = 268L, 
`110-1981` = 269L, `110-1982` = 270L, `110-1983` = 271L, `110-1984` = 272L, 
`110-1985` = 273L, `110-1986` = 274L, `110-1987` = 275L, `110-1988` = 276L, 
`110-1989` = 277L, `110-1990` = 278L, `110-1991` = 279L, `110-1992` = 280L, 
`110-1993` = 281L, `110-1994` = 282L, `110-1995` = 283L, `110-1996` = 284L, 
`110-1997` = 285L, `110-1998` = 286L, `110-1999` = 287L, `110-2000` = 288L, 
`110-2001` = 289L, `110-2002` = 290L, `130-1974` = 291L, `265-1975` = 553L, 
`265-1976` = 554L, `265-1977` = 555L, `265-1978` = 556L, `265-1979` = 557L, 
`265-1980` = 558L, `265-1981` = 559L, `265-1982` = 560L, `265-1983` = 561L, 
`265-1984` = 562L, `265-1985` = 563L, `265-1986` = 564L, `265-1987` = 565L, 
`265-1988` = 566L, `265-1989` = 567L, `265-1990` = 568L, `290-1974` = 569L, 
`290-1975` = 570L, `290-1976` = 571L, `290-1977` = 572L, `290-1978` = 573L, 
`290-1979` = 574L, `290-1980` = 575L, `290-1981` = 576L, `290-1982` = 577L, 
`290-1983` = 578L, `290-1984` = 579L, `290-1985` = 580L, `290-1986` = 581L, 
`290-1987` = 582L, `290-1988` = 583L, `290-1989` = 584L, `290-1990` = 585L, 
`315-1975` = 628L, `315-1976` = 629L, `315-1977` = 630L, `315-1978` = 631L, 
`315-1979` = 632L, `315-1980` = 633L, `315-1981` = 634L, `315-1982` = 635L, 
`315-1983` = 636L, `315-1984` = 637L, `315-1985` = 638L, `315-1986` = 639L, 
`315-1987` = 640L, `315-1988` = 641L, `315-1989` = 642L, `315-1990` = 643L, 
`315-1991` = 644L, `315-1992` = 645L, `339-1974` = 646L, `339-1975` = 647L, 
`339-1976` = 648L, `339-1977` = 649L, `339-1978` = 650L, `339-1979` = 651L, 
`339-1980` = 652L, `345-1975` = 676L, `345-1976` = 677L, `345-1977` = 678L, 
`345-1978` = 679L, `345-1979` = 680L, `345-1980` = 681L, `345-1981` = 682L, 
`345-1982` = 683L, `345-1983` = 684L, `345-1984` = 685L, `345-1985` = 686L, 
`345-1986` = 687L, `345-1987` = 688L, `345-1988` = 689L, `345-1989` = 690L, 
`345-1990` = 691L, `355-1975` = 694L, `355-1976` = 695L, `355-1977` = 696L, 
`355-1978` = 697L, `355-1979` = 698L, `355-1980` = 699L, `360-1975` = 723L, 
`360-1976` = 724L, `360-1977` = 725L, `360-1978` = 726L, `360-1979` = 727L, 
`360-1980` = 728L, `411-1975` = 781L, `411-1976` = 782L, `411-1977` = 783L, 
`411-1978` = 784L, `411-1979` = 785L, `411-1980` = 786L, `411-1981` = 787L, 
`411-1982` = 788L, `411-1983` = 789L, `411-1984` = 790L, `411-1985` = 791L, 
`438-1975` = 984L, `438-1976` = 985L, `438-1977` = 986L, `438-1978` = 987L, 
`438-1979` = 988L, `500-1975` = 1361L, `500-1976` = 1362L, `500-1977` = 1363L, 
`500-1978` = 1364L, `500-1979` = 1365L, `500-1980` = 1366L, `500-1981` = 1367L, 
`500-1982` = 1368L, `510-1975` = 1419L, `510-1976` = 1420L, `510-1977` = 1421L, 
`510-1978` = 1422L, `510-1979` = 1423L, `510-1980` = 1424L, `510-1981` = 1425L, 
`510-1982` = 1426L, `510-1983` = 1427L, `510-1984` = 1428L, `510-1985` = 1429L, 
`510-1986` = 1430L, `510-1987` = 1431L, `510-1988` = 1432L, `510-1989` = 1433L, 
`510-1990` = 1434L, `520-1975` = 1506L, `520-1976` = 1507L, `520-1977` = 1508L, 
`520-1978` = 1509L, `520-1979` = 1510L, `520-1980` = 1511L, `520-1981` = 1512L, 
`520-1982` = 1513L, `520-1983` = 1514L, `520-1984` = 1515L, `520-1985` = 1516L, 
`520-1986` = 1517L, `520-1987` = 1518L, `520-1988` = 1519L, `520-1989` = 1520L, 
`520-1990` = 1521L, `520-1991` = 1522L, `520-1992` = 1523L, `520-1993` = 1524L, 
`520-1994` = 1525L, `520-1995` = 1526L, `520-1996` = 1527L, `520-1997` = 1528L, 
`520-1998` = 1529L, `520-1999` = 1530L, `520-2000` = 1531L, `520-2001` = 1532L, 
`520-2002` = 1533L, `530-1974` = 1534L, `530-1975` = 1535L, `530-1976` = 1536L, 
`530-1977` = 1537L, `530-1978` = 1538L, `530-1979` = 1539L, `530-1980` = 1540L, 
`530-1981` = 1541L, `540-1976` = 1555L, `540-1977` = 1556L, `540-1978` = 1557L, 
`540-1979` = 1558L, `540-1980` = 1559L, `540-1981` = 1560L, `540-1982` = 1561L, 
`540-1983` = 1562L, `540-1984` = 1563L, `540-1985` = 1564L, `541-1976` = 1583L, 
`541-1977` = 1584L, `541-1978` = 1585L, `541-1979` = 1586L, `541-1980` = 1587L, 
`553-1974` = 1668L, `553-1975` = 1669L, `553-1976` = 1670L, `553-1977` = 1671L, 
`553-1978` = 1672L, `553-1979` = 1673L, `553-1980` = 1674L, `553-1981` = 1675L, 
`553-1982` = 1676L, `553-1983` = 1677L, `553-1984` = 1678L, `553-1985` = 1679L, 
`553-1986` = 1680L, `553-1987` = 1681L, `553-1988` = 1682L, `553-1989` = 1683L, 
`553-1990` = 1684L, `553-1991` = 1685L, `553-1992` = 1686L, `553-1993` = 1687L, 
`553-1994` = 1688L, `553-1995` = 1689L, `553-1996` = 1690L, `553-1997` = 1691L, 
`553-1998` = 1692L, `553-1999` = 1693L, `553-2000` = 1694L, `553-2001` = 1695L, 
`553-2002` = 1696L, `560-1974` = 1697L, `581-1976` = 1814L, `581-1977` = 1815L, 
`581-1978` = 1816L, `581-1979` = 1817L, `581-1980` = 1818L, `620-1975` = 1929L, 
`620-1976` = 1930L, `620-1977` = 1931L, `620-1978` = 1932L, `620-1979` = 1933L, 
`620-1980` = 1934L, `620-1981` = 1935L, `620-1982` = 1936L, `620-1983` = 1937L, 
`620-1984` = 1938L, `620-1985` = 1939L, `620-1986` = 1940L, `620-1987` = 1941L, 
`620-1988` = 1942L, `620-1989` = 1943L, `620-1990` = 1944L, `620-1991` = 1945L, 
`620-1992` = 1946L, `620-1993` = 1947L, `620-1994` = 1948L, `620-1995` = 1949L, 
`620-1996` = 1950L, `620-1997` = 1951L, `620-1998` = 1952L, `620-1999` = 1953L, 
`645-1975` = 2016L, `645-1976` = 2017L, `645-1977` = 2018L, `645-1978` = 2019L, 
`645-1979` = 2020L, `645-1980` = 2021L, `645-1981` = 2022L, `645-1982` = 2023L, 
`645-1983` = 2024L, `645-1984` = 2025L, `645-1985` = 2026L, `645-1986` = 2027L, 
`645-1987` = 2028L, `645-1988` = 2029L, `645-1989` = 2030L, `645-1990` = 2031L, 
`645-1991` = 2032L, `645-1992` = 2033L, `645-1993` = 2034L, `645-1994` = 2035L, 
`645-1995` = 2036L, `645-1996` = 2037L, `645-1997` = 2038L, `660-1975` = 2103L, 
`660-1976` = 2104L, `660-1977` = 2105L, `660-1978` = 2106L, `660-1979` = 2107L, 
`660-1980` = 2108L, `660-1981` = 2109L, `660-1982` = 2110L, `660-1983` = 2111L, 
`660-1984` = 2112L, `660-1985` = 2113L, `660-1986` = 2114L, `660-1987` = 2115L, 
`660-1988` = 2116L, `663-1975` = 2132L, `690-1975` = 2190L, `690-1976` = 2191L, 
`690-1977` = 2192L, `690-1978` = 2193L, `690-1979` = 2194L, `690-1980` = 2195L, 
`690-1981` = 2196L, `690-1982` = 2197L, `690-1983` = 2198L, `690-1984` = 2199L, 
`690-1985` = 2200L, `690-1986` = 2201L, `690-1987` = 2202L, `690-1988` = 2203L, 
`690-1989` = 2204L, `690-1990` = 2205L, `690-1991` = 2206L, `690-1992` = 2207L, 
`690-1993` = 2208L, `690-1994` = 2209L, `690-1995` = 2210L, `692-1975` = 2219L, 
`692-1976` = 2220L, `692-1977` = 2221L, `692-1978` = 2222L, `692-1979` = 2223L, 
`692-1980` = 2224L, `694-1975` = 2248L, `694-1976` = 2249L, `694-1977` = 2250L, 
`694-1978` = 2251L, `694-1979` = 2252L, `694-1980` = 2253L, `694-1981` = 2254L, 
`694-1982` = 2255L, `694-1983` = 2256L, `694-1984` = 2257L, `694-1985` = 2258L, 
`694-1986` = 2259L, `694-1987` = 2260L, `694-1988` = 2261L, `694-1989` = 2262L, 
`694-1990` = 2263L, `694-1991` = 2264L, `694-1992` = 2265L, `694-1993` = 2266L, 
`694-1994` = 2267L, `694-1995` = 2268L, `694-1996` = 2269L, `694-1997` = 2270L, 
`694-1998` = 2271L, `694-1999` = 2272L, `694-2000` = 2273L, `700-1975` = 2335L, 
`700-1976` = 2336L, `700-1977` = 2337L, `700-1978` = 2338L, `700-1979` = 2339L, 
`700-1980` = 2340L, `700-1981` = 2341L, `700-1982` = 2342L, `700-1983` = 2343L, 
`700-1984` = 2344L, `700-1985` = 2345L, `700-1986` = 2346L, `700-1987` = 2347L, 
`700-1988` = 2348L, `700-1989` = 2349L, `700-1990` = 2350L, `700-1991` = 2351L, 
`700-1992` = 2352L, `700-1993` = 2353L, `700-1994` = 2354L, `700-1995` = 2355L, 
`700-1996` = 2356L, `700-1997` = 2357L, `700-1998` = 2358L, `700-1999` = 2359L, 
`700-2000` = 2360L, `700-2001` = 2361L, `700-2002` = 2362L, `710-1974` = 2363L, 
`712-1975` = 2393L, `712-1976` = 2394L, `712-1977` = 2395L, `712-1978` = 2396L, 
`712-1979` = 2397L, `712-1980` = 2398L, `712-1981` = 2399L, `713-1975` = 2422L, 
`713-1976` = 2423L, `713-1977` = 2424L, `713-1978` = 2425L, `713-1979` = 2426L, 
`713-1980` = 2427L, `713-1981` = 2428L, `713-1982` = 2429L, `713-1983` = 2430L, 
`713-1984` = 2431L, `713-1985` = 2432L, `713-1986` = 2433L, `713-1987` = 2434L, 
`713-1988` = 2435L, `713-1989` = 2436L, `713-1990` = 2437L, `713-1991` = 2438L, 
`713-1992` = 2439L, `713-1993` = 2440L, `713-1994` = 2441L, `713-1995` = 2442L, 
`713-1996` = 2443L, `713-1997` = 2444L, `713-1998` = 2445L, `713-1999` = 2446L, 
`713-2000` = 2447L, `713-2001` = 2448L, `713-2002` = 2449L, `731-1974` = 2450L, 
`731-1975` = 2451L, `731-1976` = 2452L, `731-1977` = 2453L, `731-1978` = 2454L, 
`731-1979` = 2455L, `731-1980` = 2456L, `731-1981` = 2457L, `731-1982` = 2458L, 
`731-1983` = 2459L, `731-1984` = 2460L, `731-1985` = 2461L, `731-1986` = 2462L, 
`731-1987` = 2463L, `731-1988` = 2464L, `731-1989` = 2465L, `731-1990` = 2466L, 
`731-1991` = 2467L, `731-1992` = 2468L, `731-1993` = 2469L, `731-1994` = 2470L, 
`731-1995` = 2471L, `731-1996` = 2472L, `731-1997` = 2473L, `731-1998` = 2474L, 
`731-1999` = 2475L, `731-2000` = 2476L, `731-2001` = 2477L, `731-2002` = 2478L, 
`732-1974` = 2479L, `760-1975` = 2509L, `760-1976` = 2510L, `760-1977` = 2511L, 
`760-1978` = 2512L, `760-1979` = 2513L, `760-1980` = 2514L, `760-1981` = 2515L, 
`760-1982` = 2516L, `760-1983` = 2517L, `760-1984` = 2518L, `760-1985` = 2519L, 
`760-1986` = 2520L, `760-1987` = 2521L, `760-1988` = 2522L, `760-1989` = 2523L, 
`760-1990` = 2524L, `760-1991` = 2525L, `760-1992` = 2526L, `760-1993` = 2527L, 
`760-1994` = 2528L, `760-1995` = 2529L, `760-1996` = 2530L, `760-1997` = 2531L, 
`760-1998` = 2532L, `760-1999` = 2533L, `760-2000` = 2534L, `760-2001` = 2535L, 
`760-2002` = 2536L, `771-1974` = 2537L, `775-1975` = 2567L, `775-1976` = 2568L, 
`775-1977` = 2569L, `775-1978` = 2570L, `775-1979` = 2571L, `775-1980` = 2572L, 
`775-1981` = 2573L, `775-1982` = 2574L, `775-1983` = 2575L, `775-1984` = 2576L, 
`775-1985` = 2577L, `775-1986` = 2578L, `775-1987` = 2579L, `775-1988` = 2580L, 
`775-1989` = 2581L, `775-1990` = 2582L, `775-1991` = 2583L, `775-1992` = 2584L, 
`775-1993` = 2585L, `775-1994` = 2586L, `775-1995` = 2587L, `775-1996` = 2588L, 
`775-1997` = 2589L, `775-1998` = 2590L, `775-1999` = 2591L, `775-2000` = 2592L, 
`775-2001` = 2593L, `775-2002` = 2594L, `790-1974` = 2595L, `811-1975` = 2654L, 
`811-1976` = 2655L, `811-1977` = 2656L, `811-1978` = 2657L, `811-1979` = 2658L, 
`811-1980` = 2659L, `811-1981` = 2660L, `811-1982` = 2661L, `811-1983` = 2662L, 
`811-1984` = 2663L, `811-1985` = 2664L, `811-1986` = 2665L, `811-1987` = 2666L, 
`811-1988` = 2667L, `811-1989` = 2668L, `811-1990` = 2669L, `811-1991` = 2670L, 
`811-1992` = 2671L, `811-1993` = 2672L, `811-1994` = 2673L, `811-1995` = 2674L, 
`811-1996` = 2675L, `811-1997` = 2676L, `811-1998` = 2677L, `811-1999` = 2678L, 
`811-2000` = 2679L, `811-2001` = 2680L, `811-2002` = 2681L, `812-1974` = 2682L, 
`812-1975` = 2683L, `812-1976` = 2684L, `812-1977` = 2685L, `812-1978` = 2686L, 
`812-1979` = 2687L, `812-1980` = 2688L, `812-1981` = 2689L, `812-1982` = 2690L, 
`812-1983` = 2691L, `812-1984` = 2692L, `816-1975` = 2712L, `816-1976` = 2713L, 
`820-1974` = 2714L, `910-1976` = 2831L, `910-1977` = 2832L, `910-1978` = 2833L, 
`910-1979` = 2834L, `910-1980` = 2835L, `910-1981` = 2836L, `910-1982` = 2837L, 
`910-1983` = 2838L, `910-1984` = 2839L, `910-1985` = 2840L, `910-1986` = 2841L, 
`910-1987` = 2842L, `910-1988` = 2843L, `910-1989` = 2844L, `910-1990` = 2845L, 
`910-1991` = 2846L, `910-1992` = 2847L, `910-1993` = 2848L, `910-1994` = 2849L, 
`910-1995` = 2850L, `910-1996` = 2851L, `910-1997` = 2852L, `910-1998` = 2853L, 
`910-1999` = 2854L, `910-2000` = 2855L, `910-2001` = 2856L, `910-2002` = 2857L
), class = "omit"), row.names = c("95-1988", "95-1989", "95-1990", 
"95-1991", "95-1992", "95-1993", "95-1994", "95-1995", "95-1996", 
"95-1997", "95-1998", "95-1999", "95-2000", "95-2001", "95-2002", 
"110-1974", "130-1975", "130-1976", "130-1977", "130-1978", "130-1979", 
"130-1980", "130-1981", "130-1982", "130-1983", "130-1984", "130-1985", 
"130-1986", "130-1987", "130-1988", "130-1989", "130-1990", "130-1991", 
"130-1992", "130-1993", "130-1994", "130-1995", "130-1996", "130-1997", 
"130-1998", "130-1999", "130-2000", "130-2001", "130-2002", "135-1974", 
"135-1975", "135-1976", "135-1977", "135-1978", "135-1979", "135-1980", 
"135-1981", "135-1982", "135-1983", "135-1984", "135-1985", "135-1986", 
"135-1987", "135-1988", "135-1989", "135-1990", "135-1991", "135-1992", 
"135-1993", "135-1994", "135-1995", "135-1996", "135-1997", "135-1998", 
"135-1999", "135-2000", "135-2001", "135-2002", "140-1974", "140-1975", 
"140-1976", "140-1977", "140-1978", "140-1979", "140-1980", "140-1981", 
"140-1982", "140-1983", "140-1984", "140-1985", "140-1986", "140-1987", 
"140-1988", "140-1989", "140-1990", "140-1991", "140-1992", "140-1993", 
"140-1994", "140-1995", "140-1996", "140-1997", "140-1998", "140-1999", 
"140-2000", "140-2001", "140-2002", "145-1974", "145-1975", "145-1976", 
"145-1977", "145-1978", "145-1979", "145-1980", "145-1981", "145-1982", 
"145-1983", "145-1984", "145-1985", "145-1986", "145-1987", "145-1988", 
"145-1989", "145-1990", "145-1991", "145-1992", "145-1993", "145-1994", 
"145-1995", "145-1996", "145-1997", "145-1998", "145-1999", "145-2000", 
"145-2001", "145-2002", "150-1974", "150-1975", "150-1976", "150-1977", 
"150-1978", "150-1979", "150-1980", "150-1981", "150-1982", "150-1983", 
"150-1984", "150-1985", "150-1986", "150-1987", "150-1988", "150-1989", 
"150-1990", "150-1991", "150-1992", "150-1993", "150-1994", "150-1995", 
"150-1996", "150-1997", "150-1998", "150-1999", "150-2000", "150-2001", 
"150-2002", "155-1974", "155-1975", "155-1976", "155-1977", "155-1978", 
"155-1979", "155-1980", "155-1981", "155-1982", "155-1983", "155-1984", 
"155-1985", "155-1986", "155-1987", "155-1988", "155-1989", "155-1990", 
"155-1991", "155-1992", "155-1993", "155-1994", "155-1995", "155-1996", 
"155-1997", "155-1998", "155-1999", "155-2000", "155-2001", "155-2002", 
"160-1974", "160-1975", "160-1976", "160-1977", "160-1978", "160-1979", 
"160-1980", "160-1981", "160-1982", "160-1983", "160-1984", "160-1985"
), class = "data.frame")

CodePudding user response:

You can get low/mid/high estimates from a fitted model using the ggeffects package. There are many options for defining the specific moderator values to plot at. I'll demonstrate two -- plotting at user-defined values, and at the mean /- 1 SD. But I definitely recommend looking at the "Marginal Effects and Adjusted Predictions at Specific Values" section of ?ggeffects::ggpredict for a lot of other useful options.

Plotting at selected values

Simply specify the values wanted in square brackets in the terms arg to ggpredict().

library(ggplot2)
library(ggeffects)
library(scales)

preds_543 <- ggpredict(
  model1, 
  terms = c(
    "lag.log_imputed_welfarespending_pc [all]", 
    "lag.transnational_rebels [-5, -4, -3]"
  )
)

plot_template <- ggplot(
    mapping = aes(x, predicted, color = group, fill = group)
  )  
  geom_line()  
  geom_ribbon(
    aes(ymin = conf.low, ymax = conf.high), 
    alpha = .05,
    size = .3,
    linetype = "dotted"
  )  
  theme_minimal()  
  scale_y_continuous(label = percent)  
  labs(
    x = "log(Imputed Welfare Spending) (lagged)",
    y = "Predicted Probability of Onset",
    color = "Transnational\nRebels (lagged)",
    fill = "Transnational\nRebels (lagged)"
  )

plot_template   list(data = preds_543)

Plotting at the mean /- 1 SD

Include meansd in square brackets instead of specific values.

preds_meansd <- ggpredict(
  model1, 
  terms = c(
    "lag.log_imputed_welfarespending_pc [all]", 
    "lag.transnational_rebels [meansd]"
  )
)

plot_template   
  list(data = preds_meansd)  
  scale_color_discrete(
    labels = c("-1 SD", "Mean", " 1 SD"), 
    aesthetics = c("color", "fill")
  )
Data & model prep:
# non-syntactic names in provided data caused problems, therefore changed
names(Dseto) <- gsub("\\)", "", gsub("\\(", ".", names(Dseto)))

# `lag(polity2)` not included in provided data, therefore omitted from model
model1 <- glm(onset2.x ~ lag.log_imputed_welfarespending_pc * lag.transnational_rebels
                lag.gdplog
                lag.poplog,
              #  `lag(polity2)`,  
              data = Dseto,
              family = "binomial") 
  • Related