I have a panel dataset C_sf1 of 922 cities and 155 variables that looks like this:
structure(list(FID = c("AT001C1", "AT002C1"), E_pc_2000 = c(10.2,
10.2), E_pc_2001 = c(10.7, 10.7), E_pc_2002 = c(10.8, 10.8),
E_pc_2003 = c(11.5, 11.5), E_pc_2004 = c(11.4, 11.4), E_pc_2005 = c(11.5,
11.5), geometry = structure(list(structure(list(list(structure(c(16.4197,
16.4176, 16.4156, 16.4065, 16.4038, 16.4018, 16.4015, 16.4011,
16.398, 16.3951, 16.3941, 16.394, 16.3946, 16.3948, 16.3944,
16.3921, 16.3917, 16.3914, 16.3907, 16.3889, 16.3887, 16.3894,
16.3894, 16.3889, 16.3885, 16.3866, 16.3858, 16.3834, 16.3826,
16.3802, 16.3797, 16.3797, 16.3799, 16.3815, 16.3826, 16.3838,
16.3833, 16.3846, 16.3838, 16.3853, 16.3855, 16.3801, 16.3793,
16.3784, 16.378, 16.3773, 16.3763, 16.3755, 16.3751, 16.3752,
16.3783, 16.3782, 16.3775, 16.3763, 16.3723, 16.3694, 16.3691,
16.369, 16.3692, 16.3696, 16.3692, 16.3685, 16.3657, 16.3631,
16.3619, 16.3603, 16.3596, 16.3587, 16.3585, 16.3575, 16.3553,
16.3545, 16.3533, 16.3529, 16.3495, 16.3475, 16.3465, 16.3454,
16.3446, 16.3445, 16.3444, 16.3446, 16.3458, 16.348, 16.346,
16.3445, 16.3437, 16.3408, 16.3348, 16.3306, 16.329, 16.3236,
16.3212, 16.3193, 16.3179, 16.316, 16.3136, 16.3099, 16.3074,
16.3056, 16.3043, 16.3031, 16.3015, 16.2998, 16.2962, 16.2918,
16.2888, 16.2881, 16.2866, 16.2865, 16.2869, 16.2884, 16.2898,
16.2909, 16.2909, 16.291, 16.2902, 16.2856, 16.282, 16.2785,
16.2718, 16.2688, 16.2657, 16.2644, 16.2639, 16.2638, 16.2645,
16.2652, 16.268, 16.269, 16.2695, 16.2694, 16.2689, 16.2664,
16.2655, 16.2654, 16.2654, 16.265, 16.264, 16.2635, 16.2595,
16.2582, 16.2578, 16.2569, 16.2564, 16.2518, 16.2488, 16.2474,
16.2454, 16.2439, 16.2427, 16.2413, 16.2387, 16.2374, 16.2373,
16.2376, 16.2387, 16.2397, 16.2402, 16.2398, 16.2367, 16.2335,
16.2301, 16.2264, 16.2242, 16.2221, 16.2198, 16.2166, 16.2149,
16.2137, 16.2104, 16.2085, 16.2067, 16.2057, 16.2049, 16.2046,
16.2051, 16.2051, 16.2047, 16.2009, 16.1996, 16.1978, 16.1961,
16.1959, 16.196, 16.1957, 16.1951, 16.1951, 16.1958, 16.1969,
16.1982, 16.1984, 16.1978, 16.1967, 16.1955, 16.1938, 16.1901,
16.1848, 16.184, 16.184, 16.1839, 16.1841, 16.1844, 16.1847,
16.1854, 16.1856, 16.1869, 16.1886, 16.1932, 16.1951, 16.1965,
16.1975, 16.1978, 16.1965, 16.1961, 16.1962, 16.1968, 16.1971,
16.1977, 16.1994, 16.2008, 16.2035, 16.2057, 16.2067, 16.2077,
16.2085, 16.2073, 16.2065, 16.2063, 16.2054, 16.2042, 16.2027,
16.2037, 16.2086, 16.2088, 16.207, 16.2055, 16.2048, 16.2048,
16.2047, 16.2045, 16.204, 16.2035, 16.2016, 16.1997, 16.1986,
16.1975, 16.1961, 16.1947, 16.193, 16.1913, 16.1906, 16.19,
16.1894, 16.1885, 16.1875, 16.1867, 16.1858, 16.1851, 16.1844,
16.1842, 16.1845, 16.1848, 16.1856, 16.1864, 16.1888, 16.1911,
16.1936, 16.1946, 16.1956, 16.1961, 16.1967, 16.1969, 16.1971,
16.197, 16.1969, 16.1971, 16.198, 16.1988, 16.1992, 16.1996,
16.2006, 16.2016, 16.2032, 16.2048, 16.2105, 16.2114, 16.2181,
16.2238, 16.2239, 16.2226, 16.2197, 16.2193, 16.2179, 16.2154,
16.2137, 16.211, 16.2104, 16.2104, 16.2106, 16.2113, 16.2125,
16.2134, 16.216, 16.2184, 16.2194, 16.2199, 16.2205, 16.2208,
16.2215, 16.2241, 16.2314, 16.2348, 16.2369, 16.2376, 16.2364,
16.2385, 16.2383, 16.2312, 16.2265, 16.2186, 16.2169, 16.2154,
16.2151, 16.216, 16.2193, 16.2209, 16.2235, 16.2245, 16.2291,
16.2307, 16.2328, 16.2348, 16.2377, 16.2418, 16.2456, 16.2484,
16.2496, 16.25, 16.2512, 16.2522, 16.2527, 16.2534, 16.2559,
16.2569, 16.2594, 16.2599, 16.2668, 16.2717, 16.2729, 16.2818,
16.2834, 16.2846, 16.2915, 16.2975, 16.2985, 16.2985, 16.2986,
16.2987, 16.299, 16.2991, 16.2992, 16.2992, 16.2989, 16.3003,
16.3008, 16.3061, 16.3073, 16.3103, 16.3121, 16.3125, 16.3127,
16.314, 16.3146, 16.3178, 16.3183, 16.3187, 16.3197, 16.3206,
16.3222, 16.325, 16.3266, 16.3319, 16.3324, 16.3348, 16.3362,
16.3387, 16.3425, 16.3429, 16.3443, 16.3453, 16.3463, 16.349,
16.351, 16.3524, 16.3527, 16.3546, 16.3548, 16.3564, 16.3585,
16.3587, 16.3605, 16.3649, 16.3688, 16.3731, 16.3814, 16.3816,
16.3818, 16.3848, 16.3857, 16.3873, 16.3936, 16.3945, 16.3971,
16.398, 16.3994, 16.403, 16.405, 16.4081, 16.4088, 16.4099,
16.4109, 16.4115, 16.4119, 16.4119, 16.4121, 16.4123, 16.4139,
16.4186, 16.4192, 16.4208, 16.421, 16.4215, 16.4224, 16.4232,
16.424, 16.4251, 16.4268, 16.4272, 16.4296, 16.4333, 16.4351,
16.436, 16.4366, 16.4366, 16.4362, 16.4359, 16.4352, 16.4351,
16.4348, 16.4346, 16.4341, 16.4337, 16.4332, 16.4325, 16.4321,
16.4335, 16.4347, 16.437, 16.4437, 16.4457, 16.4492, 16.4517,
16.4536, 16.4551, 16.4645, 16.4658, 16.467, 16.4681, 16.4715,
16.4724, 16.4735, 16.4751, 16.4762, 16.477, 16.4774, 16.4776,
16.4795, 16.4811, 16.4845, 16.4884, 16.4926, 16.4943, 16.4976,
16.4992, 16.5036, 16.5087, 16.5105, 16.5114, 16.5118, 16.5142,
16.5196, 16.5284, 16.5309, 16.5331, 16.5378, 16.5401, 16.5404,
16.5426, 16.5473, 16.5506, 16.5529, 16.5623, 16.5684, 16.575,
16.576, 16.5762, 16.5767, 16.5769, 16.5771, 16.5775, 16.578,
16.578, 16.5782, 16.578, 16.578, 16.5772, 16.5767, 16.5757,
16.5747, 16.5742, 16.5743, 16.5743, 16.5744, 16.5742, 16.5737,
16.5733, 16.5725, 16.5714, 16.5706, 16.5695, 16.5689, 16.5674,
16.5666, 16.5655, 16.5647, 16.5643, 16.5636, 16.563, 16.5628,
16.5623, 16.5616, 16.5609, 16.5605, 16.5602, 16.5597, 16.559,
16.5581, 16.5575, 16.5567, 16.5561, 16.5553, 16.5548, 16.5544,
16.5543, 16.5545, 16.5549, 16.5551, 16.5553, 16.5552, 16.5548,
16.5542, 16.5535, 16.5529, 16.5524, 16.552, 16.5514, 16.5507,
16.5501, 16.5499, 16.5497, 16.5495, 16.5485, 16.5482, 16.5478,
16.5468, 16.5466, 16.5466, 16.5471, 16.5477, 16.5484, 16.5492,
16.5494, 16.5498, 16.5499, 16.5501, 16.5501, 16.55, 16.5498,
16.5497, 16.5495, 16.5492, 16.5486, 16.5478, 16.547, 16.5463,
16.5452, 16.5442, 16.543, 16.5424, 16.5416, 16.5411, 16.541,
16.5412, 16.5413, 16.5417, 16.5423, 16.5432, 16.5437, 16.5441,
16.5444, 16.5445, 16.5444, 16.5441, 16.5434, 16.5429, 16.5422,
16.5419, 16.5412, 16.5404, 16.5393, 16.5384, 16.5371, 16.5365,
16.5364, 16.5364, 16.5364, 16.5369, 16.5373, 16.538, 16.5387,
16.5392, 16.5396, 16.5398, 16.5396, 16.538, 16.5365, 16.5365,
16.5365, 16.5365, 16.5367, 16.5366, 16.5365, 16.5365, 16.5365,
16.5366, 16.5367, 16.5368, 16.5368, 16.5369, 16.537, 16.5371,
16.5372, 16.5373, 16.5373, 16.5374, 16.5376, 16.5377, 16.5378,
16.538, 16.5381, 16.5382, 16.5383, 16.5384, 16.5385, 16.5385,
16.5385, 16.5385, 16.5386, 16.5386, 16.5389, 16.5393, 16.5399,
16.54, 16.54, 16.5401, 16.5401, 16.5401, 16.5399, 16.5396,
16.5391, 16.5386, 16.5386, 16.5385, 16.5384, 16.5383, 16.5382,
16.538, 16.5378, 16.5375, 16.5373, 16.5371, 16.5369, 16.5367,
16.5364, 16.5362, 16.5363, 16.5364, 16.5365, 16.5375, 16.5377,
16.5385, 16.54, 16.5406, 16.5413, 16.5423, 16.543, 16.5439,
16.545, 16.5456, 16.5461, 16.5467, 16.5469, 16.5465, 16.5467,
16.548, 16.55, 16.5518, 16.5521, 16.552, 16.5502, 16.5488,
16.5484, 16.5482, 16.5479, 16.5474, 16.5463, 16.5445, 16.5423,
16.5417, 16.5414, 16.5424, 16.5419, 16.5416, 16.542, 16.5421,
16.5468, 16.5417, 16.5379, 16.5329, 16.5326, 16.5263, 16.5213,
16.5192, 16.5128, 16.514, 16.5087, 16.5074, 16.5079, 16.5083,
16.5088, 16.5095, 16.5098, 16.5116, 16.5129, 16.5137, 16.5145,
16.5142, 16.5125, 16.5128, 16.5094, 16.5041, 16.5021, 16.4983,
16.4951, 16.494, 16.4898, 16.484, 16.4813, 16.4809, 16.4809,
16.4809, 16.4819, 16.4824, 16.4833, 16.4835, 16.4835, 16.4834,
16.4836, 16.4829, 16.4813, 16.4809, 16.4816, 16.4772, 16.4748,
16.472, 16.4715, 16.4647, 16.4606, 16.4596, 16.452, 16.4483,
16.4402, 16.4401, 16.4401, 16.4402, 16.438, 16.4379, 16.438,
16.4363, 16.4339, 16.4336, 16.4299, 16.4271, 16.4247, 16.4226,
16.4197, 48.3222, 48.3221, 48.3218, 48.3196, 48.3191, 48.3185,
48.3173, 48.3171, 48.3177, 48.3184, 48.3188, 48.3191, 48.3201,
48.3215, 48.3217, 48.3219, 48.3216, 48.321, 48.3206, 48.3205,
48.3202, 48.3195, 48.3191, 48.3186, 48.3184, 48.3188, 48.3187,
48.3174, 48.3169, 48.3154, 48.3147, 48.3137, 48.3128, 48.31,
48.3085, 48.306, 48.3053, 48.3039, 48.3029, 48.302, 48.301,
48.3018, 48.3011, 48.2995, 48.2986, 48.2976, 48.2963, 48.2958,
48.2949, 48.2944, 48.2901, 48.2892, 48.2886, 48.2879, 48.2878,
48.2874, 48.2867, 48.2859, 48.285, 48.2839, 48.2835, 48.2833,
48.284, 48.2846, 48.2846, 48.2842, 48.2842, 48.2842, 48.2842,
48.2843, 48.2843, 48.2844, 48.2848, 48.2852, 48.2894, 48.2896,
48.2897, 48.2895, 48.2887, 48.2883, 48.288, 48.2878, 48.2867,
48.2845, 48.2838, 48.283, 48.2828, 48.2816, 48.2803, 48.2791,
48.2788, 48.2791, 48.2792, 48.279, 48.2785, 48.2776, 48.2754,
48.2737, 48.2728, 48.2725, 48.2725, 48.2726, 48.2732, 48.2734,
48.2728, 48.2717, 48.2706, 48.2701, 48.2679, 48.2668, 48.2663,
48.2658, 48.2654, 48.2646, 48.2641, 48.2634, 48.2628, 48.2607,
48.2595, 48.2586, 48.2564, 48.2559, 48.2556, 48.2552, 48.2548,
48.2544, 48.254, 48.2539, 48.2535, 48.2532, 48.2525, 48.252,
48.2514, 48.25, 48.2491, 48.2485, 48.2479, 48.2475, 48.2471,
48.2467, 48.2432, 48.2418, 48.2407, 48.2397, 48.2395, 48.2401,
48.24, 48.2398, 48.239, 48.2389, 48.2392, 48.2398, 48.2405,
48.2411, 48.242, 48.243, 48.2446, 48.2461, 48.2471, 48.2476,
48.2486, 48.2498, 48.2514, 48.2536, 48.255, 48.2567, 48.2578,
48.2585, 48.2587, 48.2593, 48.2619, 48.2626, 48.2626, 48.2623,
48.2613, 48.2593, 48.2563, 48.2556, 48.255, 48.2533, 48.252,
48.2488, 48.2451, 48.2436, 48.2418, 48.2378, 48.2357, 48.2347,
48.2339, 48.2332, 48.2327, 48.2324, 48.2316, 48.2307, 48.2291,
48.228, 48.2258, 48.2243, 48.2238, 48.2237, 48.2234, 48.2233,
48.2227, 48.2223, 48.222, 48.222, 48.222, 48.2224, 48.2226,
48.2225, 48.2222, 48.2218, 48.2214, 48.2203, 48.2196, 48.2185,
48.2165, 48.2161, 48.2151, 48.2137, 48.2131, 48.2125, 48.2117,
48.211, 48.2095, 48.2087, 48.208, 48.2073, 48.2072, 48.2067,
48.2062, 48.2061, 48.2045, 48.2031, 48.203, 48.1993, 48.1968,
48.1942, 48.1934, 48.1924, 48.1914, 48.1905, 48.1897, 48.1879,
48.1861, 48.1852, 48.1844, 48.183, 48.1816, 48.1802, 48.1789,
48.1783, 48.1777, 48.1762, 48.1746, 48.1731, 48.1727, 48.1724,
48.172, 48.1716, 48.1712, 48.1709, 48.1705, 48.1701, 48.1697,
48.1686, 48.1676, 48.1664, 48.1656, 48.1648, 48.164, 48.1632,
48.1621, 48.1609, 48.1601, 48.1593, 48.1581, 48.1569, 48.1556,
48.1556, 48.1555, 48.1558, 48.156, 48.1561, 48.1562, 48.1556,
48.1554, 48.1547, 48.1536, 48.1524, 48.1529, 48.1534, 48.1526,
48.1521, 48.1515, 48.151, 48.1501, 48.1494, 48.1485, 48.1479,
48.1472, 48.1463, 48.1453, 48.1417, 48.1406, 48.14, 48.1392,
48.1375, 48.1364, 48.1352, 48.1352, 48.1361, 48.1364, 48.1364,
48.1358, 48.1347, 48.1343, 48.1325, 48.1315, 48.1297, 48.1282,
48.1273, 48.1265, 48.1258, 48.1253, 48.1243, 48.1238, 48.1245,
48.1247, 48.1269, 48.1274, 48.1283, 48.129, 48.1297, 48.1301,
48.1308, 48.131, 48.1308, 48.1308, 48.1306, 48.1304, 48.1303,
48.1302, 48.1298, 48.1298, 48.1302, 48.1304, 48.1335, 48.1333,
48.1332, 48.1308, 48.1307, 48.1305, 48.1296, 48.1286, 48.1287,
48.1283, 48.128, 48.1279, 48.1275, 48.1273, 48.1269, 48.1266,
48.1262, 48.1251, 48.1245, 48.1217, 48.121, 48.1197, 48.1197,
48.1217, 48.1231, 48.1256, 48.1269, 48.1261, 48.1269, 48.1276,
48.1292, 48.1307, 48.1333, 48.138, 48.1376, 48.1368, 48.1367,
48.1362, 48.1359, 48.1353, 48.1345, 48.1344, 48.134, 48.1337,
48.1336, 48.1332, 48.1328, 48.1322, 48.1321, 48.1309, 48.1309,
48.1304, 48.1296, 48.1296, 48.1293, 48.1287, 48.1278, 48.1272,
48.1265, 48.1264, 48.1264, 48.126, 48.1259, 48.1257, 48.125,
48.1249, 48.1247, 48.1244, 48.1235, 48.122, 48.1209, 48.1189,
48.1187, 48.1186, 48.1187, 48.1189, 48.1194, 48.1202, 48.1204,
48.1206, 48.1207, 48.1209, 48.1209, 48.1216, 48.1217, 48.122,
48.1226, 48.1229, 48.1229, 48.1225, 48.122, 48.1219, 48.1212,
48.1204, 48.12, 48.1199, 48.1202, 48.1204, 48.1213, 48.1219,
48.123, 48.1232, 48.1237, 48.124, 48.1256, 48.1278, 48.1303,
48.131, 48.1314, 48.1379, 48.138, 48.1381, 48.1384, 48.1384,
48.1387, 48.1389, 48.1393, 48.14, 48.1462, 48.147, 48.1478,
48.1484, 48.1507, 48.1514, 48.1522, 48.1533, 48.1541, 48.1548,
48.1556, 48.1562, 48.157, 48.1568, 48.1561, 48.1554, 48.1586,
48.1581, 48.1572, 48.1568, 48.157, 48.1577, 48.1581, 48.1583,
48.1586, 48.1591, 48.1572, 48.1538, 48.1525, 48.151, 48.147,
48.1452, 48.145, 48.1438, 48.1422, 48.1413, 48.1407, 48.139,
48.1377, 48.1361, 48.1363, 48.1369, 48.1386, 48.1393, 48.1402,
48.1418, 48.1428, 48.1433, 48.144, 48.146, 48.1476, 48.1506,
48.152, 48.1538, 48.1554, 48.1563, 48.1573, 48.1582, 48.159,
48.1597, 48.1604, 48.161, 48.1616, 48.1623, 48.1627, 48.163,
48.1631, 48.1631, 48.1631, 48.1629, 48.1628, 48.1625, 48.1621,
48.1616, 48.1612, 48.161, 48.1609, 48.1609, 48.1613, 48.1616,
48.1622, 48.1629, 48.1638, 48.1641, 48.1643, 48.1644, 48.1644,
48.1642, 48.1639, 48.1636, 48.1633, 48.163, 48.1627, 48.1624,
48.1622, 48.162, 48.1621, 48.1621, 48.1623, 48.1624, 48.1626,
48.163, 48.1635, 48.1642, 48.165, 48.166, 48.1665, 48.1675,
48.1681, 48.1689, 48.1704, 48.1709, 48.1713, 48.1714, 48.1716,
48.1718, 48.172, 48.1723, 48.1727, 48.1732, 48.1737, 48.1742,
48.1749, 48.1756, 48.1761, 48.1767, 48.177, 48.1773, 48.1775,
48.1775, 48.1775, 48.1773, 48.1771, 48.1771, 48.1771, 48.1771,
48.1768, 48.1765, 48.1762, 48.176, 48.1758, 48.1756, 48.1752,
48.1748, 48.1745, 48.1739, 48.1735, 48.1733, 48.1731, 48.1732,
48.1733, 48.1735, 48.1734, 48.1732, 48.173, 48.1727, 48.1727,
48.173, 48.1732, 48.1734, 48.1736, 48.1739, 48.174, 48.174,
48.174, 48.174, 48.1738, 48.1738, 48.1741, 48.1745, 48.1757,
48.1771, 48.1774, 48.1778, 48.179, 48.1803, 48.1814, 48.182,
48.1826, 48.1828, 48.1831, 48.1833, 48.1835, 48.1837, 48.1839,
48.1842, 48.1846, 48.1851, 48.1855, 48.1856, 48.186, 48.1864,
48.1867, 48.1869, 48.1872, 48.1874, 48.1876, 48.1879, 48.188,
48.1882, 48.1884, 48.1887, 48.1892, 48.1895, 48.1897, 48.191,
48.1915, 48.1923, 48.1924, 48.1927, 48.193, 48.1935, 48.1938,
48.1942, 48.1947, 48.1954, 48.1961, 48.1963, 48.1965, 48.1967,
48.1972, 48.1976, 48.1981, 48.1984, 48.1988, 48.1991, 48.1993,
48.1995, 48.1997, 48.2001, 48.2003, 48.201, 48.2014, 48.202,
48.2039, 48.2043, 48.2055, 48.2075, 48.2082, 48.2089, 48.2099,
48.2109, 48.2122, 48.2137, 48.2148, 48.2161, 48.2184, 48.2196,
48.2215, 48.2233, 48.2283, 48.2333, 48.2367, 48.2377, 48.2388,
48.2395, 48.2401, 48.2404, 48.2416, 48.2423, 48.2426, 48.2428,
48.2429, 48.2429, 48.243, 48.2437, 48.2482, 48.2487, 48.2497,
48.2511, 48.2513, 48.2631, 48.2639, 48.2642, 48.2629, 48.262,
48.2632, 48.2645, 48.2669, 48.2679, 48.2712, 48.2734, 48.2737,
48.2752, 48.2761, 48.277, 48.2783, 48.2787, 48.2812, 48.2821,
48.2839, 48.2855, 48.2859, 48.2866, 48.2873, 48.288, 48.2896,
48.2904, 48.2914, 48.2924, 48.2926, 48.2931, 48.294, 48.2944,
48.2938, 48.2926, 48.2922, 48.2898, 48.2876, 48.286, 48.2849,
48.2848, 48.2794, 48.2789, 48.2776, 48.2757, 48.2749, 48.273,
48.2737, 48.2751, 48.2769, 48.2772, 48.2808, 48.2829, 48.283,
48.2864, 48.2882, 48.2919, 48.296, 48.2989, 48.3031, 48.3099,
48.3126, 48.3164, 48.317, 48.3174, 48.3176, 48.3193, 48.3204,
48.3212, 48.3217, 48.3222), .Dim = c(807L, 2L)))), class = c("XY",
"MULTIPOLYGON", "sfg")), structure(list(list(structure(c(15.4008,
15.3968, 15.3961, 15.3958, 15.3971, 15.3978, 15.3973, 15.3957,
15.3919, 15.3911, 15.3904, 15.3823, 15.3817, 15.3814, 15.3781,
15.3778, 15.3758, 15.3736, 15.3723, 15.3696, 15.367, 15.3668,
15.3661, 15.3619, 15.3585, 15.3583, 15.3582, 15.358, 15.3584,
15.3619, 15.364, 15.3638, 15.3634, 15.3626, 15.3522, 15.3515,
15.3513, 15.3512, 15.3517, 15.3526, 15.3533, 15.3581, 15.3594,
15.3602, 15.3607, 15.3642, 15.3651, 15.3652, 15.3652, 15.3652,
15.3675, 15.3718, 15.3728, 15.3739, 15.3723, 15.3682, 15.3682,
15.3684, 15.374, 15.3772, 15.3795, 15.3804, 15.3808, 15.3805,
15.3794, 15.3761, 15.3761, 15.3753, 15.3743, 15.3714, 15.3705,
15.3693, 15.3682, 15.3674, 15.3664, 15.3654, 15.364, 15.3618,
15.3612, 15.3598, 15.3592, 15.3592, 15.3609, 15.3613, 15.366,
15.3662, 15.3684, 15.3687, 15.3687, 15.3673, 15.3674, 15.3675,
15.3681, 15.3693, 15.3718, 15.3764, 15.378, 15.3858, 15.3897,
15.3912, 15.3924, 15.3941, 15.3956, 15.3986, 15.4033, 15.4076,
15.4096, 15.4102, 15.422, 15.4228, 15.434, 15.4373, 15.4396,
15.4404, 15.4409, 15.442, 15.4422, 15.4429, 15.4476, 15.4466,
15.4484, 15.4494, 15.4509, 15.4531, 15.4534, 15.457, 15.4575,
15.4577, 15.4585, 15.4594, 15.4605, 15.4616, 15.4639, 15.4647,
15.4688, 15.4734, 15.4777, 15.48, 15.4846, 15.4862, 15.4882,
15.4888, 15.4875, 15.4884, 15.489, 15.4885, 15.487, 15.4863,
15.486, 15.4836, 15.4833, 15.4829, 15.4834, 15.4849, 15.4867,
15.4865, 15.4846, 15.4901, 15.4934, 15.4982, 15.4982, 15.4981,
15.4979, 15.4995, 15.5034, 15.5001, 15.4988, 15.4981, 15.498,
15.4989, 15.5002, 15.5015, 15.5034, 15.5049, 15.5057, 15.5069,
15.5077, 15.5081, 15.5083, 15.5082, 15.507, 15.5065, 15.5062,
15.506, 15.506, 15.5062, 15.5068, 15.5093, 15.5108, 15.5119,
15.5123, 15.513, 15.5184, 15.5206, 15.522, 15.5255, 15.5264,
15.5271, 15.5308, 15.5336, 15.5327, 15.5307, 15.5287, 15.5277,
15.5264, 15.526, 15.526, 15.5283, 15.5302, 15.5302, 15.5303,
15.5303, 15.5266, 15.5201, 15.5147, 15.5136, 15.5095, 15.5102,
15.5118, 15.5132, 15.5145, 15.5157, 15.5176, 15.5182, 15.5184,
15.5196, 15.5199, 15.5194, 15.5186, 15.5159, 15.5148, 15.5118,
15.5099, 15.5054, 15.5023, 15.4991, 15.4953, 15.4922, 15.4911,
15.4838, 15.4822, 15.4813, 15.48, 15.4792, 15.4783, 15.4771,
15.4682, 15.4651, 15.4637, 15.4624, 15.4596, 15.4459, 15.4423,
15.4369, 15.4298, 15.4268, 15.4248, 15.4223, 15.4189, 15.4173,
15.4137, 15.4108, 15.4089, 15.4073, 15.4044, 15.4008, 47.1349,
47.1327, 47.1323, 47.1322, 47.1287, 47.1254, 47.123, 47.1199,
47.1151, 47.1147, 47.1145, 47.1141, 47.1144, 47.1147, 47.1212,
47.1213, 47.1206, 47.1198, 47.1194, 47.1194, 47.1195, 47.1195,
47.1195, 47.1199, 47.1202, 47.1197, 47.1195, 47.1187, 47.1171,
47.1155, 47.1113, 47.1109, 47.1106, 47.1104, 47.108, 47.1076,
47.1071, 47.1032, 47.1024, 47.1021, 47.102, 47.1028, 47.1028,
47.1026, 47.1022, 47.0977, 47.0962, 47.0948, 47.0946, 47.0914,
47.0877, 47.0878, 47.0876, 47.0872, 47.085, 47.0806, 47.0804,
47.0803, 47.0782, 47.0788, 47.0764, 47.0754, 47.0717, 47.0704,
47.0706, 47.0712, 47.0681, 47.0657, 47.0642, 47.0628, 47.0619,
47.0582, 47.054, 47.0534, 47.0529, 47.0528, 47.0529, 47.0534,
47.0533, 47.0525, 47.0515, 47.0513, 47.0492, 47.0486, 47.0422,
47.0421, 47.0411, 47.0402, 47.039, 47.0326, 47.0322, 47.031,
47.0294, 47.028, 47.0257, 47.0231, 47.0221, 47.0198, 47.0201,
47.0187, 47.0176, 47.0173, 47.0164, 47.0163, 47.0163, 47.0163,
47.0166, 47.0167, 47.0183, 47.0185, 47.0211, 47.0219, 47.0183,
47.0169, 47.0161, 47.0163, 47.0163, 47.0153, 47.0166, 47.0177,
47.0185, 47.0176, 47.0177, 47.0189, 47.019, 47.0182, 47.0171,
47.0166, 47.015, 47.013, 47.0125, 47.012, 47.0129, 47.0132,
47.0136, 47.0153, 47.0167, 47.0172, 47.0193, 47.0197, 47.0203,
47.0205, 47.0213, 47.0217, 47.0219, 47.0227, 47.0219, 47.0217,
47.0223, 47.0246, 47.0249, 47.0255, 47.0256, 47.026, 47.0265,
47.0268, 47.0308, 47.0328, 47.0339, 47.0363, 47.0366, 47.0374,
47.0378, 47.0402, 47.0412, 47.0456, 47.0473, 47.0491, 47.0525,
47.0533, 47.0543, 47.057, 47.0589, 47.0608, 47.0622, 47.066,
47.0673, 47.068, 47.0689, 47.07, 47.074, 47.075, 47.0756,
47.0767, 47.0778, 47.0786, 47.079, 47.0807, 47.082, 47.0837,
47.0863, 47.0903, 47.092, 47.0931, 47.0941, 47.0969, 47.0973,
47.0974, 47.0973, 47.0984, 47.0998, 47.1008, 47.1018, 47.1023,
47.1034, 47.1043, 47.1052, 47.1063, 47.1072, 47.1075, 47.1103,
47.1107, 47.1095, 47.1075, 47.1069, 47.1068, 47.1074, 47.1081,
47.1095, 47.1118, 47.1144, 47.1157, 47.1171, 47.1176, 47.1177,
47.1189, 47.1201, 47.1213, 47.1234, 47.1263, 47.127, 47.1289,
47.1292, 47.1289, 47.1288, 47.1286, 47.1245, 47.1271, 47.1272,
47.1295, 47.1295, 47.1292, 47.1285, 47.1276, 47.1269, 47.1269,
47.1285, 47.1305, 47.1309, 47.1309, 47.1305, 47.1278, 47.127,
47.1276, 47.1279, 47.1281, 47.128, 47.128, 47.1277, 47.128,
47.1309, 47.1332, 47.1338, 47.1339, 47.1336, 47.1349), .Dim = c(266L,
2L)))), class = c("XY", "MULTIPOLYGON", "sfg"))), class = c("sfc_MULTIPOLYGON",
"sfc"), precision = 0, bbox = structure(c(xmin = 15.3512,
ymin = 47.012, xmax = 16.5782, ymax = 48.3222), class = "bbox"), crs = structure(list(
input = "WGS 84", wkt = "GEOGCRS[\"WGS 84\",\n DATUM[\"World Geodetic System 1984\",\n ELLIPSOID[\"WGS 84\",6378137,298.257223563,\n LENGTHUNIT[\"metre\",1]]],\n PRIMEM[\"Greenwich\",0,\n ANGLEUNIT[\"degree\",0.0174532925199433]],\n CS[ellipsoidal,2],\n AXIS[\"latitude\",north,\n ORDER[1],\n ANGLEUNIT[\"degree\",0.0174532925199433]],\n AXIS[\"longitude\",east,\n ORDER[2],\n ANGLEUNIT[\"degree\",0.0174532925199433]],\n ID[\"EPSG\",4326]]"), class = "crs"), n_empty = 0L)), row.names = 1:2, class = c("sf",
"data.frame"), sf_column = "geometry", agr = structure(c(FID = NA_integer_,
E_pc_2000 = NA_integer_, E_pc_2001 = NA_integer_, E_pc_2002 = NA_integer_,
E_pc_2003 = NA_integer_, E_pc_2004 = NA_integer_, E_pc_2005 = NA_integer_
), .Label = c("constant", "aggregate", "identity"), class = "factor"))
My task is to calculate the spatial weights matrix and the spatial lags associated with the future dependent variable of my model: log(E).
- I built the spatial weights matrix for the wide dataframe.
library(spdep)
coord <- st_coordinates(st_centroid(C_sf1))
nb2.knn1 <- knn2nb(knearneigh(coord,k=1,longlat=T))
all.linkedT <- max(unlist(nbdists(nb2.knn1, coord,longlat=T)))
all.linkedT
nb3 <- dnearneigh(coord, 0, all.linkedT,row.names=C_sf$FDI,longlat=TRUE)
n.nb3 <- nbdists(nb3, coord,longlat=TRUE);n.nb3
n.nb3.gl <- lapply(n.nb3, function(x) 1/x); n.nb3.gl
nb3.listw.gl <- nb2listw(nb3, glist=n.nb3.gl,zero.policy=F)
- My problem is that I want to shift to a long format (17k+ observations), keeping the spatial weight matrix. The long format would allow me to have a variable called
E(and notE_2001,E_2002, etc.) from which I can calculate the logarithm.
I tried to use the same code for the dataframe C_sf_long (obtained with reshape), but R returns me a spatial weight matrix of 0 distances. The reason is that in the panel dataset, the same coordinates are repeated for each time unit, so distance and neighbour functions only recognize a city as the neighbour of itself.
- The code for the spatial lags is the following:
lag.E<- lag.listw(nb2listw(nb3),C_sf1$E)
but, as you can see, C_sf1 does not have the variable E, because it is still in the wide format.
So, I want C_sf1 to be in long format, in order to compute the spatial lags, but still I want to keep the spatial weight matrix computed with C_sf1 in wide format.