可视化滞后自变量模型

huangapple go评论63阅读模式
英文:

visualize lagged independent variable models

问题

我正在寻找一种可视化滞后独立变量回归模型的方法,最好使用ggplot。我特别关注如何在不同滞后值(lag = 0lag = 1lag = 2)下可视化独立变量的影响如何随时间变化。下面是一个示例回归模型和数据:

model_test = fenegbin(Conflict ~ l(Project_n,0:2)+
                        Urban_area+
                        Econ_dev, TC_test,panel.id = ~ID+Year)

请提供代码的翻译,以便我可以为您提供相应的信息。

英文:

I am looking for a way to visualize lagged independent variable regression models, preferably using ggplot. I am particularly interested in visualizing how the impact of the independent variable changes over time, meaning at lag = 0 , lag = 1 , lag= 2. Below you can access an exemplary regression model and data:

model_test = fenegbin(Conflict ~ l(Project_n,0:2)+
Urban_area+
Econ_dev, TC_test,panel.id = ~ID+Year)
structure(list(ID = c(1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 
2, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 5, 5, 
5, 6, 6, 6, 6, 6, 6, 6, 7, 7, 7, 7, 7, 7, 7, 8, 8, 8, 8, 8, 8, 
8, 9, 9, 9, 9, 9, 9, 9, 10, 10, 10, 10, 10, 10, 10, 11, 11, 11, 
11, 11, 11, 11, 12, 12, 12, 12, 12, 12, 12, 13, 13, 13, 13, 13, 
13, 13, 14, 14, 14, 14, 14, 14, 14, 15, 15, 15, 15, 15, 15, 15, 
16, 16, 16, 16, 16, 16, 16, 17, 17, 17, 17, 17, 17, 17, 19, 19, 
19, 19, 19, 19, 19, 22, 22, 22, 22, 22, 22, 22, 25, 25, 25, 25, 
25, 25, 25, 27, 27, 27, 27, 27, 27, 27, 28, 28, 28, 28, 28, 28, 
28, 29, 29, 29, 29, 29, 29, 29, 30, 30, 30, 30, 30, 30, 30, 31, 
31, 31, 31, 31, 31, 31, 32, 32, 32, 32, 32, 32, 32, 33, 33, 33, 
33, 33, 33, 33, 34, 34, 34, 34, 34, 34, 34, 36, 36, 36, 36, 36, 
36, 36, 37, 37, 37, 37, 37, 37, 37, 38, 38, 38, 38, 38, 38, 38, 
39, 39, 39, 39, 39, 39, 39, 40, 40, 40, 40, 40, 40, 40, 42, 42, 
42, 42, 42, 42, 42, 43, 43, 43, 43, 43, 43, 43, 44, 44, 44, 44, 
44, 44, 44, 45, 45, 45, 45, 45, 45, 45, 46, 46, 46, 46, 46, 46, 
46, 47, 47, 47, 47, 47, 47, 47, 48, 48, 48, 48, 48, 48, 48, 50, 
50, 50, 50, 50, 50, 50, 51, 51, 51, 51, 51, 51, 51, 66, 66, 66, 
66, 66, 66, 66, 67, 67, 67, 67, 67, 67, 67, 72, 72, 72, 72, 72, 
72, 72, 75, 75, 75, 75, 75, 75, 75, 76, 76, 76, 76, 76, 76, 76, 
77, 77, 77, 77, 77, 77, 77, 78, 78, 78, 78, 78, 78, 78, 79, 79, 
79, 79, 79, 79, 79, 80, 80, 80, 80, 80, 80, 80, 81, 81, 81, 81, 
81, 81, 81, 82, 82, 82, 82, 82, 82, 82, 83, 83, 83, 83, 83, 83, 
83, 84, 84, 84, 84, 84, 84, 84, 85, 85, 85, 85, 85, 85, 85, 87, 
87, 87, 87, 87, 87, 87, 93, 93, 93, 93, 93, 93, 93, 98, 98, 98, 
98, 98, 98, 98), Year = c(2004, 2005, 2006, 2007, 2008, 2009, 
2010, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2004, 2005, 2006, 
2007, 2008, 2009, 2010, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 
2004, 2005, 2006, 2007, 2008, 2009, 2010, 2004, 2005, 2006, 2007, 
2008, 2009, 2010, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2004, 
2005, 2006, 2007, 2008, 2009, 2010, 2004, 2005, 2006, 2007, 2008, 
2009, 2010, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2004, 2005, 
2006, 2007, 2008, 2009, 2010, 2004, 2005, 2006, 2007, 2008, 2009, 
2010, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2004, 2005, 2006, 
2007, 2008, 2009, 2010, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 
2004, 2005, 2006, 2007, 2008, 2009, 2010, 2004, 2005, 2006, 2007, 
2008, 2009, 2010, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2004, 
2005, 2006, 2007, 2008, 2009, 2010, 2004, 2005, 2006, 2007, 2008, 
2009, 2010, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2004, 2005, 
2006, 2007, 2008, 2009, 2010, 2004, 2005, 2006, 2007, 2008, 2009, 
2010, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2004, 2005, 2006, 
2007, 2008, 2009, 2010, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 
2004, 2005, 2006, 2007, 2008, 2009, 2010, 2004, 2005, 2006, 2007, 
2008, 2009, 2010, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2004, 
2005, 2006, 2007, 2008, 2009, 2010, 2004, 2005, 2006, 2007, 2008, 
2009, 2010, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2004, 2005, 
2006, 2007, 2008, 2009, 2010, 2004, 2005, 2006, 2007, 2008, 2009, 
2010, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2004, 2005, 2006, 
2007, 2008, 2009, 2010, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 
2004, 2005, 2006, 2007, 2008, 2009, 2010, 2004, 2005, 2006, 2007, 
2008, 2009, 2010, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2004, 
2005, 2006, 2007, 2008, 2009, 2010, 2004, 2005, 2006, 2007, 2008, 
2009, 2010, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2004, 2005, 
2006, 2007, 2008, 2009, 2010, 2004, 2005, 2006, 2007, 2008, 2009, 
2010, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2004, 2005, 2006, 
2007, 2008, 2009, 2010, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 
2004, 2005, 2006, 2007, 2008, 2009, 2010, 2004, 2005, 2006, 2007, 
2008, 2009, 2010, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2004, 
2005, 2006, 2007, 2008, 2009, 2010, 2004, 2005, 2006, 2007, 2008, 
2009, 2010, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2004, 2005, 
2006, 2007, 2008, 2009, 2010, 2004, 2005, 2006, 2007, 2008, 2009, 
2010, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2004, 2005, 2006, 
2007, 2008, 2009, 2010, 2004, 2005, 2006, 2007, 2008, 2009, 2010
), Conflict = c(4, 8, 8, 1, 8, 0, 0, 10, 10, 4, 1, 5, 0, 0, 34, 
25, 13, 0, 5, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
1, 2, 1, 0, 1, 0, 0, 1, 2, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
0, 2, 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, 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, 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, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
), Project_n = c(9, 19, 26, 40, 66, 92, 122, 10, 19, 30, 47, 
82, 124, 153, 13, 19, 27, 42, 67, 102, 132, 14, 20, 29, 39, 62, 
95, 126, 8, 13, 20, 34, 58, 88, 116, 15, 21, 30, 43, 69, 99, 
124, 13, 19, 34, 73, 110, 149, 183, 8, 13, 20, 33, 58, 88, 118, 
9, 17, 26, 42, 69, 106, 136, 10, 15, 24, 37, 62, 95, 125, 9, 
17, 23, 34, 58, 91, 125, 8, 13, 20, 33, 58, 87, 118, 13, 19, 
26, 40, 66, 101, 132, 12, 17, 24, 34, 56, 86, 111, 11, 16, 23, 
40, 65, 102, 129, 7, 12, 19, 34, 58, 86, 118, 12, 19, 28, 42, 
65, 92, 122, 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, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 3, 0, 0, 
0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 
0, 0, 0, 0, 1, 0, 0, 0, 9, 9, 9, 9, 0, 0, 0, 12, 12, 12, 12, 
0, 0, 0, 4, 4, 4, 4, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 1, 1, 1, 1, 
0, 0, 0, 2, 2, 2, 2, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 1, 1, 1, 1, 
0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 2, 2, 2, 2, 
0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 1, 1, 1, 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, 1, 1, 1, 1, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 2, 3, 
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, 2, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
0, 0, 0, 0, 0, 0, 3), Urban_area = c(6L, 6L, 12L, 18L, 19L, 22L, 
24L, 430L, 467L, 486L, 508L, 532L, 551L, 567L, 27L, 32L, 43L, 
44L, 47L, 47L, 50L, 19L, 22L, 27L, 32L, 35L, 37L, 40L, 2L, 2L, 
2L, 2L, 3L, 3L, 3L, 12L, 16L, 18L, 26L, 28L, 32L, 37L, 33L, 34L, 
35L, 36L, 37L, 38L, 39L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 8L, 9L, 
10L, 10L, 10L, 10L, 10L, 0L, 0L, 0L, 1L, 4L, 4L, 6L, 12L, 18L, 
25L, 35L, 50L, 61L, 70L, 1L, 1L, 2L, 2L, 2L, 4L, 4L, 17L, 17L, 
18L, 18L, 18L, 19L, 20L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 9L, 11L, 
14L, 14L, 17L, 18L, 20L, 13L, 13L, 15L, 15L, 17L, 17L, 20L, 2L, 
4L, 6L, 8L, 8L, 8L, 8L, 23L, 23L, 23L, 23L, 23L, 23L, 23L, 71L, 
73L, 75L, 76L, 76L, 76L, 76L, 44L, 44L, 44L, 44L, 44L, 44L, 44L, 
10L, 10L, 11L, 11L, 11L, 11L, 11L, 15L, 15L, 15L, 15L, 16L, 16L, 
16L, 35L, 37L, 37L, 38L, 38L, 38L, 38L, 34L, 34L, 34L, 34L, 34L, 
34L, 34L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 35L, 35L, 37L, 37L, 40L, 
42L, 47L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 28L, 28L, 28L, 28L, 29L, 
31L, 36L, 117L, 119L, 120L, 124L, 128L, 131L, 133L, 146L, 164L, 
172L, 180L, 188L, 194L, 199L, 16L, 19L, 19L, 19L, 20L, 22L, 25L, 
24L, 27L, 28L, 31L, 32L, 34L, 34L, 92L, 104L, 105L, 106L, 107L, 
107L, 107L, 868L, 1158L, 1173L, 1201L, 1217L, 1237L, 1254L, 10L, 
10L, 11L, 11L, 11L, 11L, 16L, 31L, 35L, 36L, 39L, 42L, 47L, 48L, 
43L, 50L, 58L, 65L, 73L, 86L, 94L, 70L, 91L, 95L, 97L, 101L, 
103L, 105L, 77L, 78L, 81L, 83L, 85L, 87L, 92L, 41L, 44L, 44L, 
44L, 44L, 44L, 46L, 60L, 67L, 71L, 77L, 80L, 82L, 83L, 12L, 13L, 
14L, 14L, 16L, 17L, 19L, 87L, 87L, 89L, 93L, 94L, 97L, 99L, 79L, 
80L, 81L, 81L, 81L, 81L, 82L, 32L, 37L, 37L, 39L, 42L, 42L, 42L, 
22L, 22L, 22L, 25L, 29L, 42L, 49L, 5L, 5L, 5L, 8L, 10L, 14L, 
20L, 92L, 96L, 96L, 102L, 116L, 121L, 130L, 14L, 15L, 15L, 15L, 
16L, 19L, 21L, 25L, 25L, 25L, 25L, 25L, 27L, 27L, 25L, 34L, 39L, 
42L, 51L, 65L, 79L, 156L, 157L, 157L, 158L, 164L, 168L, 173L, 
89L, 90L, 91L, 91L, 91L, 91L, 91L, 6L, 6L, 6L, 6L, 7L, 7L, 7L, 
30L, 30L, 33L, 36L, 37L, 40L, 42L, 439L, 444L, 446L, 449L, 450L, 
450L, 453L, 78L, 85L, 93L, 111L, 125L, 143L, 152L, 876L, 894L, 
902L, 915L, 928L, 935L, 949L, 2348L, 2578L, 2781L, 2894L, 2999L, 
3100L, 3197L), Econ_dev = c(4804, 4018, 4559, 4324, 4047, 3975, 
4598, 2919, 2663, 3593, 4003, 3563, 3742, 4250, 6794, 5408, 6320, 
6480, 6266, 6028, 7014, 12128, 9850, 11035, 11463, 11430, 11011, 
12261, 10278, 7891, 8543, 8422, 9757, 8692, 8553, 6769, 5920, 
6429, 6211, 5815, 5881, 6250, 8169, 6954, 7142, 7383, 7119, 7084, 
7828, 5615, 4360, 5247, 5413, 5162, 5323, 5853, 4764, 3881, 4213, 
4307, 4258, 4203, 4690, 7394, 5704, 6391, 6618, 7041, 6397, 7342, 
11966, 10659, 9749, 9852, 9541, 9825, 9951, 2562, 2116, 2386, 
2393, 2370, 2306, 2720, 9475, 7036, 7627, 7185, 7908, 7243, 7849, 
3120, 2543, 2914, 2934, 2935, 2878, 3292, 5696, 4376, 5401, 5347, 
5182, 5107, 5859, 10031, 8541, 7087, 8038, 7307, 8327, 7997, 
13052, 12392, 9091, 9578, 9947, 10638, 11015, 457, 340, 363, 
406, 383, 394, 449, 154550, 120170, 138342, 151295, 144230, 138545, 
153883, 180, 129, 142, 169, 147, 155, 174, 94428, 69831, 80755, 
88288, 84440, 81075, 91466, 58166, 42476, 55340, 61178, 57181, 
57125, 64128, 498, 366, 489, 536, 486, 503, 574, 41844, 29495, 
40868, 42019, 41238, 39604, 43691, 83539, 59038, 80555, 85249, 
81528, 82336, 90101, 59722, 43517, 57493, 60387, 58817, 59445, 
64507, 59951, 42007, 58137, 60935, 58607, 58582, 65854, 31267, 
22966, 30560, 31088, 30735, 30003, 33126, 2536, 1855, 2406, 2536, 
2530, 2539, 2852, 97187, 72994, 90563, 98522, 95002, 99091, 97679, 
105600, 88835, 111129, 110397, 106636, 110715, 109066, 40362, 
31781, 39175, 41981, 39824, 41354, 42460, 444, 316, 397, 531, 
477, 462, 569, 9443, 8655, 9772, 10971, 11727, 12359, 15013, 
122314, 98940, 125682, 118406, 116651, 126242, 121709, 107492, 
92109, 113548, 112005, 107757, 116638, 109503, 88132, 67325, 
81771, 93299, 86295, 86996, 89413, 97902, 86883, 95488, 97593, 
97985, 104361, 100891, 133807, 103742, 109176, 112468, 109876, 
130087, 106315, 195783, 175887, 195243, 185581, 183746, 191335, 
188783, 96324, 76212, 77521, 83088, 83716, 93338, 78491, 73514, 
67258, 81518, 77334, 80119, 87578, 80922, 622, 482, 617, 747, 
690, 630, 825, 41875, 27657, 35017, 38052, 37200, 33456, 38477, 
126170, 86623, 117948, 127197, 124610, 117928, 129537, 65893, 
47938, 58724, 59922, 59452, 61282, 64461, 731, 618, 825, 738, 
819, 711, 797, 119483, 77538, 100933, 97671, 100074, 98796, 104611, 
29466, 21157, 28695, 28822, 28364, 29201, 29435, 70, 46, 65, 
61, 60, 60, 60, 70926, 52382, 66268, 69232, 68998, 66811, 70557, 
440, 354, 418, 417, 397, 432, 520, 33580, 24738, 31848, 32536, 
32581, 32132, 33515, 30853, 24034, 29418, 27064, 26928, 28679, 
26238, 42286, 32087, 38332, 40977, 40642, 41642, 41672, 3259, 
2469, 3016, 3014, 3210, 3098, 3198, 93270, 63597, 80240, 77919, 
72782, 77182, 73481, 1583, 1674, 1773, 1233, 1062, 1265, 1247, 
71635, 61740, 69084, 69177, 67109, 71640, 74626)), class = "data.frame", row.names = c(NA, 
-413L))
>    

Thanks for any suggestions!

答案1

得分: 3

有一种方法是预测在三年期内将所有其他变量保持在其平均值时的结果。然后,预测第三年的冲突数量,如果第一年没有项目,但第二年和第三年有平均数量的项目。对于第一年和第三年有平均数量的项目,但第二年没有项目的情况,进行相同的预测。最后,预测如果前两年有平均数量的项目,但第三年没有项目的情况下的冲突。这将显示在滞后2、1和0时没有项目的效果。

现在,对要绘制的每个项目数量执行相同的计算(比如所有值从0到50个项目)。可以按照以下方式执行此操作:

mean_proj <- mean(TC_test$Project_n)

plot_df <- cbind(n_proj = 0:50, do.call(rbind, lapply(0:50, function(x) {
  data.frame(`two years ago` = predict(model_test, 
        newdata = data.frame(Urban_area = mean(TC_test$Urban_area), 
                             Project_n = c(x, mean_proj, mean_proj), 
                             Econ_dev = mean(TC_test$Urban_area), 
                             Year = 2010:2012, ID = 1), type = 'response')[3],
    `last year` = predict(model_test, 
        newdata = data.frame(Urban_area = mean(TC_test$Urban_area), 
                             Project_n = c(mean_proj, x, mean_proj), 
                             Econ_dev = mean(TC_test$Urban_area), 
                             Year = 2010:2012, ID = 1), type = 'response')[3],
    `current year` = predict(model_test, 
        newdata = data.frame(Urban_area = mean(TC_test$Urban_area), 
                             Project_n = c(mean_proj, mean_proj, x), 
                             Econ_dev = mean(TC_test$Urban_area), 
                             Year = 2010:2012, ID = 1), type = 'response')[3],
    check.names = FALSE)
}))) 

现在可以在三个分面中绘制结果,其中一个用于显示每个滞后的项目数量的边际效应:

library(tidyverse)

plot_df %>%
  pivot_longer(-n_proj) %>%
  ggplot(aes(n_proj, value, color = name)) +
  geom_line() +
  scale_y_continuous('Number of conflicts') +
  scale_x_continuous('Number of projects') +
  scale_color_brewer(palette = 'Set1', guide = 'none') +
  facet_wrap(.~name, scales = 'free_y') +
  ggtitle('Predicted conflicts given number of projects...') +
  theme_minimal() +
  theme(panel.border = element_rect(fill = NA, color = 'gray80'))
英文:

One way to do this is to predict the outcome if you hold all other variables steady at their average values over a three year period. Then, predict the number of conflicts in the third year if there were 0 projects in year 1, but an average number of projects in years two and three. Do the same for an average number of projects in the first and third years, but 0 in the second year. Finally, predict conflicts if there are an average number of projects in the first two years, but 0 in year 3. This will show you the effect of having 0 projects at lags 2, 1 and 0.

Now do the same set of calculations for each number of projects you want to plot (say all values from 0 to 50 projects). You could do this as follows:

mean_proj &lt;- mean(TC_test$Project_n)

plot_df &lt;- cbind(n_proj = 0:50, do.call(rbind, lapply(0:50, function(x) {
  data.frame(`two years ago` = predict(model_test, 
        newdata = data.frame(Urban_area = mean(TC_test$Urban_area), 
                             Project_n = c(x, mean_proj, mean_proj), 
                             Econ_dev = mean(TC_test$Urban_area), 
                             Year = 2010:2012, ID = 1), type = &#39;response&#39;)[3],
    `last year` = predict(model_test, 
        newdata = data.frame(Urban_area = mean(TC_test$Urban_area), 
                             Project_n = c(mean_proj, x, mean_proj), 
                             Econ_dev = mean(TC_test$Urban_area), 
                             Year = 2010:2012, ID = 1), type = &#39;response&#39;)[3],
    `current year` = predict(model_test, 
        newdata = data.frame(Urban_area = mean(TC_test$Urban_area), 
                             Project_n = c(mean_proj, mean_proj, x), 
                             Econ_dev = mean(TC_test$Urban_area), 
                             Year = 2010:2012, ID = 1), type = &#39;response&#39;)[3],
    check.names = FALSE)
}))) 

Now you can plot the result in three facets, one to show the marginal effect of the number of projects at each lag:

library(tidyverse)

plot_df %&gt;%
  pivot_longer(-n_proj) %&gt;%
  ggplot(aes(n_proj, value, color = name)) +
  geom_line() +
  scale_y_continuous(&#39;Number of conflicts&#39;) +
  scale_x_continuous(&#39;Number of projects&#39;) +
  scale_color_brewer(palette = &#39;Set1&#39;, guide = &#39;none&#39;) +
  facet_wrap(.~name, scales = &#39;free_y&#39;) +
  ggtitle(&#39;Predicted conflicts given number of projects...&#39;) +
  theme_minimal() +
  theme(panel.border = element_rect(fill = NA, color = &#39;gray80&#39;))

可视化滞后自变量模型

huangapple
  • 本文由 发表于 2023年2月6日 17:57:51
  • 转载请务必保留本文链接:https://go.coder-hub.com/75359786.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定