0

我正在 R 上做一个空间计量经济学项目。我有一个包含 81 个国家的数据库,我使用包在世界地图上绘制了这些数据库rworldmap。我必须创建一个权重矩阵来进行莫兰图和计量经济学评估,但我想找到一种方法来只考虑包含数据的国家。否则,当我尝试绘制 Moran 图时,它总是返回数据和权重矩阵长度不同的错误。

这是我正在使用的代码:

library(rworldmap)
library(spdep)

worldmap <- joinCountryData2Map(database, joinCode = "ISO3", nameJoinColumn = "code")

# Recovery of centroids from employment areas
coor <- coordinates(worldmap)

# Definition of the 3 closest neighbors
cartePPV3.knn <- knearneigh(coor, k=3) 
cartePPV3.nb <- knn2nb(cartePPV3.knn)

# weight matrix for the 2 closest neighbors
PPV3.w <- nb2listw(cartePPV3.nb, style = "W", zero.policy = TRUE)
print(PPV3.w, zero.policy=TRUE)

## Characteristics of weights list object:
## Neighbour list object:
## Number of regions: 243 
## Number of nonzero links: 729 
## Percentage nonzero weights: 1.234568 
## Average number of links: 3 
## Non-symmetric neighbours list

## Weights style: W 
## Weights constants summary:
##    n    nn  S0       S1       S2
## W 243 59049 243 133.8889 10

但是对象 PPV3.w 的长度为 243,我的数据长度为 81。您对如何克服这个问题有什么建议吗?

database <- structure(list(code = c("ARG", "ARM", "AUS", "AUT", "AZE", "BEL", "BGR", "BHS", "BLR", "BOL", "BRA", "BWA", "CAN", "CHE", "CHL", 
"CHN", "CIV", "COD", "COL", "CYP", "CZE", "DEU", "DOM", "EGY", 
"ESP", "EST", "FIN", "FRA", "GAB", "GBR", "GHA", "GMB", "GTM", 
"HND", "HRV", "HTI", "HUN", "IDN", "IND", "IRL", "IRQ", "ISR", 
"ITA", "JAM", "JOR", "JPN", "KEN", "KOR", "KWT", "LBR", "LBY", 
"LTU", "LUX", "LVA", "MAR", "MEX", "MLT", "MWI", "MYS", "NLD", 
"NOR", "NZL", "PAN", "PER", "PHL", "POL", "PRT", "PRY", "RUS", 
"SAU", "SDN", "SEN", "SWE", "THA", "TTO", "TUR", "VEN", "VNM", 
"YEM", "ZAF", "ZWE"), sales2010 = c(44287, 4, 185329, 20222, 
2019, 130775, 1123, 6584, 38, 994, 187351, 312, 600161, 275761, 
32645, 303281, 1275, 1642, 24004, 1781, 18636, 363995, 5960, 
14952, 100793, 446, 10643, 211677, 556, 658153, 1159, 2, 4762, 
5057, 556, 291, 19039, 34144, 65621, 271794, 209, 18539, 131316, 
987, 756, 303618, 658, 107154, 6531, 1537, 4311, 316, 28923, 
209, 1955, 227473, 1411, 469, 57286, 242155, 51486, 13592, 10907, 
13722, 21190, 40124, 13935, 1790, 41474, 26593, 1, 152, 32332, 
57996, 7505, 40122, 23803, 3069, 940, 36411, 168), gdp = c(423627422092.49, 
9260284937.79782, 1146138465603.81, 391892746544.69, 52902703376.1056, 
480951629493.033, 50610058210.3696, 10095760000, 5.87e+10, 19649631450.6819, 
2208871646202.82, 12786654365.8738, 1613542812589.39, 583783396408.582, 
218537551220.071, 6087164527421.24, 24884505034.5564, 21565720044.4634, 
286103648654.545, 25732432719.0773, 207477857918.919, 3396354075663.73, 
53860187452.8829, 218888324504.753, 1420722034063, 19685033799.4182, 
249181190476.369, 2642609548930.36, 14358584300.3006, 2475244321361.11, 
32174772955.9748, 1543292392.54605, 41338008612.7727, 15839344591.9842, 
59807966533.2848, 6622541528.56888, 131135561228.374, 755094160363.071, 
1675615335600.56, 222148757313.006, 138516722649.573, 233995676194.679, 
2134017843247.16, 13220556882.7046, 26795901408.4507, 5700098114744.41, 
40000088346.8041, 1094499338702.72, 115419050942.08, 1.998e+09, 
74773444900.5368, 37024718364.5516, 53212476812.2957, 23796118300.8371, 
93216746661.5977, 1057801295584.05, 8749171417.20801, 6959655570.89098, 
255016609232.871, 846554894931.084, 428757038466.841, 146583831538.331, 
29440300000, 147528937028.778, 199590775190.258, 479321460551.189, 
237880908317.652, 27215968615.5548, 1524917468442.01, 528207200000, 
65634109236.7736, 16215069255.5883, 495328620126.028, 341105009515.333, 
22157948396.2042, 771901768870.083, 393192354510.653, 115931749697.241, 
30906749533.221, 375349442837.24, 12041655200), gdppc = c(10385.9644319555, 
3218.37270660563, 52022.1255961876, 46858.0432733717, 5842.80578358576, 
44141.8781415734, 6843.26694975885, 28443.4076553352, 6181.4, 
1955.46155736098, 11286.2430162457, 6434.81565688465, 47450.3184700703, 
74605.7745091454, 12808.034586422, 4550.45359583857, 1211.93033804477, 
334.021572573152, 6326.54946861963, 30818.4799259532, 19808.0710912518, 
41531.9341978689, 5555.39094900238, 2644.81703909751, 30502.7197080776, 
14784.3810806949, 46459.9732548543, 40638.33400426, 8840.73066379785, 
39435.8399018505, 1298.43695159215, 860.637873688125, 2825.48396349692, 
1904.34646496881, 13923.6370524478, 665.627419493396, 13113.5259617277, 
3122.36281521649, 1357.56371913262, 48715.1768553933, 4657.28042580536, 
30693.5930786871, 36000.5201179254, 4704.05445468166, 3690.11326778672, 
44507.6763859172, 951.687961116879, 22086.9529193201, 38577.3816572031, 
513.445698620224, 12064.7806924218, 11953.9384416891, 104965.306078267, 
11344.6933695837, 2839.92516805933, 9271.39823324639, 21107.3644349639, 
478.668589704525, 9040.56625117172, 50950.0343435181, 87693.7900658099, 
33692.0108346543, 8082.02845866252, 5082.35475666351, 2124.05676974627, 
12599.5335818707, 22498.6908586557, 4355.93493867734, 10674.9972024225, 
19262.5476811757, 1489.87691083872, 1278.97775413162, 52817.441365794, 
5076.34299245077, 16683.3553787376, 10672.3892452162, 13825.3580883312, 
1317.89070617836, 1334.78484461341, 7328.61562893966, 948.33185445926
), population = c(40788453, 2877319, 22031750, 8363404, 9054332, 
10895586, 7395599, 354942, 9490583, 10048590, 195713635, 1987105, 
34004889, 7824909, 17062536, 1337705000, 20532950, 64563854, 
45222700, 1112612, 10474410, 81776930, 9695121, 82761235, 46576897, 
1331475, 5363352, 65027507, 1624140, 62766365, 24779619, 1793196, 
14630417, 8317470, 4295427, 9949322, 10000023, 241834215, 1234281170, 
4560155, 29741976, 7623600, 59277417, 2810460, 7261539, 128070000, 
42030676, 49554112, 2991884, 3891356, 6197663, 3097282, 506953, 
2097555, 32343389, 114092963, 414508, 14539612, 28208035, 16615394, 
4889252, 4350700, 3642687, 29027674, 93966780, 38042794, 10573100, 
6248020, 142849449, 27421461, 34545013, 12678148, 9378126, 67195028, 
1328147, 72326988, 28439940, 87967651, 23154855, 51216964, 12697723
), Export = c(81123016360.6088, 2210672987.38497, 260249450614.58, 
197754734995.671, 28234775000, 369294651391.053, 25303890000, 
3196008000, 2.93e+10, 6836994924.1346, 231995637790.95, 5273007889.77465, 
469288008080.745, 372127504216.421, 81955505335.345, 1603944171443.28, 
12593261961.1995, 8866500000, 45882895935.252, 12939073661.2597, 
137029742232.405, 1443337924604.33, 12270500000, 48831200000, 
368535746091.486, 14627605142.6957, 95846565511.4142, 709353195634.283, 
9340231071.72115, 700321794220.098, 9437380000, 270568875.315234, 
10803318404.5824, 4939311177.07018, 22345655941.7061, 1016384389.4025, 
107223068846.779, 166636323443.415, 348035371769.33, 207839505909.957, 
54593900000, 81456300000, 536026782436.075, 4004373830.42721, 
12751830985.9155, 869989651841.154, 8983066272.5496, 546783400000, 
76139508378.4144, 325037491.048954, 49345100000, 23726953550.7431, 
82200741495.4408, 12852987830.0144, 27045459856.5698, 314349098491, 
13394731629.6545, 1222623901.35877, 222011698915.434, 602372107459.307, 
170571688434.974, 43295270715.1102, 2.0558e+10, 39076122270.6046, 
54553954496.6018, 1.91942e+11, 72002887874.4588, 11089285874.2953, 
441833180000, 261831173341.6, 11646421263.4664, 3212491051.87374, 
220276557566.513, 227242774271.888, 12093527322.1487, 1.57445e+11, 
6.8731e+10, 7.9697e+10, 9270503494.65773, 107735282477.838, 3569254416.45001
), Import = c(68779495712.0305, 4537162833.55646, 254063849930.763, 
185620622249.928, 10236392000, 364143639834.488, 26931050000, 
3772597000, 3.68e+10, 6155587585.3598, 243660936841.03, 6633911658.6747, 
500163006070.577, 312065336933.79, 67820076191.149, 1380920299730.48, 
10775405646.026, 10705200000, 48046798936.1514, 15100529525.8354, 
130837451159.024, 1263115070306.35, 17797400000, 59862200000, 
383498000907.792, 13396203288.1648, 92754844880.5398, 752868287730.485, 
4923415827.48669, 749902578366.385, 13925330000, 322618256.760513, 
15213731418.538, 7775195685.41861, 22730499340.6279, 4287321211.20725, 
100281103545.836, 145424174547.834, 439059034248.05, 169386495216.739, 
47191500000, 76941100000, 577495808884.848, 6453776928.46442, 
18241408450.7042, 791792570423.91, 13531331102.0134, 512823900000, 
35354120068.3102, 868019609.325615, 30686400000, 24474123582.2915, 
67383739312.1496, 13173369811.9769, 36998738043.2765, 328707852420, 
13457646140.3091, 2642498673.22905, 181577107696.099, 532071517403.752, 
120110756464.812, 39973075650.9473, 21497300000, 34441341927.0994, 
65648068547.9856, 2.02076e+11, 89499906436.2193, 10292025283.6503, 
320958410000, 174203205866.667, 11371960452.1287, 5201250790.56075, 
196604177787.601, 207558530109.994, 6870500000, 1.97021e+11, 
5.6038e+10, 8.7294e+10, 10629016000, 102953525920.479, 6440274022.1819
), tradecost = c(2.8260194991743, 1.37233406139776, 2.22848303629037, 
1.02221684085353, 1.37512603597665, 0.655749277405991, 0.968892817917845, 
1.44874906814205, 0.888048411497731, 1.51237303563959, 4.64383709593806, 
1.07388433371409, 1.66438818365869, 0.85324394132387, 1.4591000014339, 
2.03934369088039, 1.0648662325473, 1.10188282287504, 3.04593397270418, 
0.917717435135471, 0.774554940050968, 1.25490968513045, 1.79128530602014, 
2.01381431167626, 1.8891732448602, 0.702439636210922, 1.32120533965529, 
1.80725668374763, 1.00665589955806, 1.70680093932396, 1.37718496509929, 
2.60169566919901, 1.58888147940729, 1.24576947916985, 1.32682049209118, 
1.24866310977131, 0.63196590081213, 2.41970440098524, 2.12886195453861, 
0.588900968251111, 1.36087024906885, 1.47726967863538, 1.91645671123355, 
1.26413906124506, 0.864572464690165, 3.43011138184821, 1.77664485890245, 
1.03292921698701, 1.03520759481992, 1.67468933328733, 0.934300180560614, 
0.768130518377497, 0.355735277650511, 0.914308434094678, 1.45550650516963, 
1.64495740864863, 0.32582482982176, 1.80062997656243, 0.631872353879062, 
0.746229143853026, 1.47500148698232, 1.76037879861251, 0.700037807363162, 
2.00671960926238, 1.66046103164356, 1.21649635435739, 1.47292131590025, 
1.27288585880015, 1.99912726940527, 1.21138888396621, 2.85137808763968, 
1.92721259568579, 1.18817824408527, 0.784507787988934, 1.1684199785098, 
2.17764685151773, 3.15136255408517, 0.694239508100683, 1.553140493745, 
1.781534793854, 1.20301923050891), skills = c(9.48, 10.87, 11.77, 
9.89, 10.5, 10.78, 11.45, 11.5, 12, 7.77, 7.66, 8.87, 12.56, 
13.42, 9.71, 7.53, 4.22, 3.61, 8.45, 11.11, 13.16, 12.69, 7.46, 
6.55, 10.3, 12.48, 10.21, 10.64, 7.57, 12.32, 6.76, 2.82, 4.3, 
5.5, 11.42, 4.74, 12.14, 7.26, 5.39, 12.2, 6.38, 12.76, 9.54, 
9.67, 9.21, 11.52, 6.19, 11.89, 6.26, 4.09, 7.31, 11.05, 11.22, 
10.48, 4.24, 8.33, 10.33, 4.29, 9.75, 11.6, 11.8, 11.3, 9.15, 
8.68, 8.18, 11.42, 7.2, 7.24, 11.73, 7.79, 3.13, 2.4, 11.89, 
7.3, 10.37, 6.56, 8.16, 7.45, 2.6, 9.43, 7.25), investmentcost = c(0.509259283542633, 
0.333333343267441, 0.916666686534882, 0.944444417953491, 0.416666686534882, 
0.833333313465118, 0.5, 0.694444477558136, 0.416666686534882, 
0.444444447755814, 0.384259253740311, 0.527777791023254, 0.916666686534882, 
0.861111104488373, 0.75, 0.472222208976746, 0.305555552244186, 
0.111111111938953, 0.365740746259689, 0.833333313465118, 0.6875, 
0.833333313465118, 0.305555552244186, 0.472222238779068, 0.766203701496124, 
0.597222208976746, 1, 0.67592591047287, 0.388888895511627, 0.916666686534882, 
0.416666656732559, 0.587962985038757, 0.356481492519379, 0.395833343267441, 
0.694444417953491, 0.16666667163372, 0.722222208976746, 0.333333343267441, 
0.555555582046509, 0.861111104488373, 0.16666667163372, 0.777777791023254, 
0.576388895511627, 0.418981492519379, 0.555555582046509, 0.805555522441864, 
0.375, 0.592592597007751, 0.555555582046509, 0.222222223877907, 
0.416666686534882, 0.569444417953491, 0.944444417953491, 0.597222208976746, 
0.666666686534882, 0.472222238779068, 0.694444417953491, 0.446759253740311, 
0.555555582046509, 0.944444417953491, 0.944444417953491, 0.972222208976746, 
0.444444447755814, 0.444444447755814, 0.444444447755814, 0.583333373069763, 
0.722222208976746, 0.259259283542633, 0.328703701496124, 0.555555582046509, 
0.277777791023254, 0.388888895511627, 0.972222208976746, 0.527777791023254, 
0.592592597007751, 0.5, 0.287037044763565, 0.472222238779068, 
0.305555552244186, 0.405092597007751, 0.194444447755814), distance = c(8403.226, 
9416.022, 15961.95, 15961.95, 9695.539, 6222.864, 7920.095, 1534.43, 
7450.31, 6565.147, 6794.436, 12786.42, 737.0425, 6603.182, 8080.885, 
11159.25, 7959.889, 10512, 3815.184, 9121.958, 6904.571, 6717.542, 
2371.688, 9357.814, 6092.003, 6972.001, 6943.182, 6169.154, 9679.682, 
5901.343, 8482.468, 6524.287, 3004.99, 2935.833, 7240.354, 2305.789, 
7342.427, 16371.12, 12059.81, 5448.801, 9981.358, 9450.717, 7224.739, 
2326.271, 9540.523, 10918.79, 12152.02, 11185.97, 10539.89, 7523.652, 
7818.324, 7280.807, 6393.923, 7081.517, 6146.918, 3037.916, 7729.347, 
12780.82, 15357.34, 6196.851, 6239.55, 14219.82, 3339.086, 5672.588, 
13794.33, 7183.616, 5740.448, 7414.781, 7834.547, 10858.59, 10544.13, 
6376.861, 6644.253, 14173.7, 3498.427, 8732.645, 3303.033, 13362.15, 
11448.37, 13040.41, 12793.88)), row.names = c(NA, -81L), class = c("tbl_df", 
"tbl", "data.frame"))
4

1 回答 1

0

您需要发布示例数据以获得实际解决方案,但您正在寻找类似的东西

databasenoNA[is.na(databasenoNA$variable)==F,]

编辑:来自您的数据的过滤示例:

databasenoNA[databasenoNA$gdp >= 9e11,]
于 2020-04-23T11:17:28.380 回答