r-如何使用分类值估算连续值



连续矢量中缺少值

y <- c(8, 7, NA, 4, 6)

我有的邮政编码

x <- factor(c("94562", "98705", "94122", "94539", "94122"))

我想用邮政编码来估算y。我试过

library(mice)
mice.impute.norm(y = y, ry = is.na(y), x = x)

但我收到一个错误,指示它无法进行交叉产品(由于我的rstudio会话挂起,无法发布特定消息)。

关于如何用鼠标或其他包装做到这一点,有什么想法吗?


编辑:部分数据的dput

structure(list(P = c(430978, 258197, 441424, 4697124, 549553, 
543517, 164859, 878202, 552206, 1277737, 622493, 0, 386436, 296388, 
396152, 2085540, 0, 872755, 705626, 1165616, 660079, 0, 854224, 
2467241, NA, 327414, 172844, 1225882, 392000, 1212863, 482001, 
323318, 854862, 282000, 197983, 0, 257816, 3184589, NA, NA, 351540, 
519250, 0, 715735, 440319, 269234, 473734, 115562, 196519, 578946, 
0, 310298, 1144127, 1245472, 573946, 637205, NA, 202050, NA, 
968296, 2510965, 651258, 358531, 858467, 238151, 1334602, 1724823, 
439336, 113247, 1739823, 918437, 2089680, 390012, 878560, 734073, 
835640, 421741, 823924, 1169276, 388136, 313059, 834817, 750854, 
0, 200761, 871028, 521642, 611000, 1001221, 607588, 596879, 1818999, 
1997605, 634628, 720182, 276853, 1082990, 462157, 162590, 498092, 
395789, 630174, 697803, 780158, 381650, 259782, 540757, 731815, 
1002691, 0, 153433, 416352, 1111262, 1076702, 1286831, 555605, 
0, 139202, 640845, 327670, 269729, 155641, 435379, 833306, 1615806, 
201458, 714396, 282184, 721273, 1442403, 511897, 447289, NA, 
941232, 428255, 3293079, 180466, 394853, NA, 0, 636945, 775164, 
930795, 1758912, 751042, 2006928, 168166, 440602, 967278, 498395, 
2267808, 1677245, 1767264, 411715, 776274, NA, 0, 649445, 467611, 
381720, 1154060, 1338408, 1245837, 771673, NA, 1671251, 1179100, 
1774379, 323477, 2316334, 649605, 212284, 408611, 404153, 649797, 
0, 122709, 776319, 1681236, 265285, 145190, 829636, 170889, 535429, 
453658, 845000, 728687, 1425078, 750081, 0, 943963, 692388, 310913, 
482516, 265311, 295152, 852276, 841878, 367349, 571220, 940994, 
668783, 231262, 2285178, 499754, 2481758, 2552567, 1209911, 1309048, 
0, NA, 970914, 143681, 117653, 2559991, 1530288, 998852, 972048, 
501345, NA, 455011, 1090249, 0, 1754717, 928676, 2228952, 1210306, 
274114, 2996674, 1373548, 348015, 481149, 247406, 1465976, 1843617, 
0, 187732, NA, 730044, 690627, 273611, 2555635, 741534, 678164, 
0, 1174674, 1141709, 1017413, 128923, 880700, 0, 514569, 0, 3981143, 
322187, 780000, 1308740, 275292, 228581, 1673234, 625399, 404182, 
619844, 0, 235631, 0, 981673, 389150, 353951, 326345, 214020, 
1968243, 252044, 209535, 992147, 764947, 543495, 2647744, 530914, 
613501, NA, 278428, 670708, 985255, 1287827, 2597156, 765840, 
0, 1444604, 265677, 993149, 464696, 356783, 590535, 318429, 2086138, 
391269, 567151, 386370, 107666, 406000, 364883, 913950, 336508, 
565462, 0, 737092, 275279, 404449, 486438, 180063, 283177, 1876736, 
524425, 1004098, 347263, 599265, 252279, 1018000, 0, 184479, 
659572, 1876473, 144852, 0, 0, 0, 253781, 284629, 200933, 1333446, 
854077, 625488, 1250172, 224754, 882517, 231475, 166978, 735607, 
230509, 800756, 784914, 420270, 185336, 246928, 614803, 1371731, 
0, 207045, 276819, 512905, 199201, 217407, 282554, NA, 482712, 
950772, 1165216, 0, 447145, 407478, 1384973, 481090, 671244, 
351402, 637502, 553362, 789929, 745108, 271497, 787057, 2623938, 
927413, 774508, 500969, 964854, 526529, 668668, 1126112, 731235, 
1544305, NA, 0, 309125, 457086, 127128, 446126, 139772, 358053, 
363871, 522786, 916538, 452719, 383917, 637407, 336294, 1514003, 
766945, 439585, 301085, 301074, 266695, 346461, 131531, 189778, 
420600, 630623, 179731, 368203, 243744, 518385, 578028, 359323, 
1201273, 1966185, 611000, 305056, 0, 0, 763189, 457651, 2095788, 
2502161, 1904274, 305234, 376261, 1324896, 377826, 1218535, 175428, 
398371, 625510, 555790, NA, 739395, 528774, 1224491, 666071, 
304766, 1136009, 469271, NA, 328784, 399521, NA, 960898, 939321, 
628866, 267014, 352126, 392874, 1403416, 779563, 1454036, 365208, 
1019950, 261490, 181000, NA, 0, NA, 0, 754076, 1154453, 1046000, 
3056705, 147467, 172242, 481561, 1501393, 1023301, 625225, 389466, 
71127, 1225320, 0, 734682, 237290, 957442, 309519, 356858, 233500, 
177190, 486064, NA, 619008, 1348038, 751197, 1557408, 712051, 
1311732, 627650, 292000, 0, 768646, 431135, 637152, 398952, 438161, 
186194, 361569, 232833, 889680, NA, 1132797, 669670, 451444, 
393618, 1257769, 848298, 1076782, 245171, 0, 280950, 0, 0, 1641403, 
439986, 330774, 359512, 1028509, 679340, 371646, 474494, 797887, 
287127, 294498, 714913, 166292, 383565, 643625, 0, 2150467, 244742, 
1764309, 924075, 969097, 1839615, 796188, 1364330, 405164, 216870, 
0, 5785310, 530395, 1043911, 614367, 517762, 537112, 1161056, 
507340, 2219940, 1783959, 391147, 512239, 187435, 0, 1308179, 
0, 970877, 798663, NA, 1524668, 1011186, 555983, 998058, 269345, 
393185, 2187930, 520688, 0, 1399948, 0, 1135497, 730279, 1468040, 
434088, 1450764, NA, 541791, 700064, 919461, 0, 1023378, 1240075, 
547794, 968996, 240392, 358874, 195951, 949183, 1070050, 163521, 
0, 483058, 1784686, 0, 1247666, 1195258, NA, 1044343, 1443992, 
812583, 936031, 190546, 388127, 0, 495595, 377471, 575207, 2608400, 
233725, 0, 465783, 378966, 0, 363322, 1018468, 816970, 0, 1485934, 
700082, 1923973, 5603372, 1890049, 1926850, 0, 443506, 172164, 
658800, 336870, 550835, 1098827, 1398211, 343704, 851671, 1283774, 
337959, 1346075, 1452629, 581806, 885973, 610659, 693601, 3937677, 
92446, 317879, 753399, 309471, 501656, 482898, 1508468, 946956, 
0, 894148, 376428, NA, 666900, 2228116, 1734511, 414936, 633964, 
429246, 0, 282809, NA, 556125, 404724, 396565, 308760, 415532, 
855008, 130680, 450144, 668459, 162911, 578297, 199129, 2180912, 
3818836, 128208, NA, 204887, 812150, 247087, 626391, 1030287, 
202166, 316501, 392917, 622271, 0, 523260, 709000, 185786, 1518284, 
852749, 308630, 0, 1280117, 499133, 560035, 913103, 357553, 861170, 
2300529, 0, 969203, 780080, 386323, NA, 1177880, 534566, 1736941, 
635773, 2568170, 291748, 464465, 665993, 299345, 1728483, 805991, 
261636, 606749, NA, 785862, 2638239, 1744052, 566641, 511034, 
388557, 278391, 892563, 324254, NA, 354527, 1229760, 532942, 
987340, 3183471, 260660, 1531932, 549854, 2020837, 0, 395248, 
127387, 602795, 332431, 506106, 759821, 213757, 970139, 554647, 
708758, 1131534, 546207, 0, 415942, 1176368, 605931, 0, 691959, 
1404907, 565904, 607480, 312361, 163000, 212417, 277627, 1737963, 
1150777, 124624, 209945, 1123399, 581433, 675144, 0, 703212, 
1930374, 404589, 966181, 930596, 614151, 133066, 115956, 176013, 
0, 338516, 1575332, 725400, 390636, 624768, 1261313, 629072, 
204297, 323404, 1151330, 589984, 2305308, NA, 421961, 360286, 
935414, NA, NA, 354431, 766989, 97042, 215050, 935844, 947514, 
641636, 430436, 679528, 762154, 1256202, 173160, 678178, 732307, 
922627, 144878, 1156409, 751874, 0, 327670, 358266, 256644, 868852, 
393758, NA, 902305, 1397937, 257786, 411285, 2168876, 0, 399356, 
663956, 410679, 1349445, 1697725, 679709, 427320, 180366, 4617713, 
152158, 563863, 409620, 673038, 438582, 773626, 372557, 1426526, 
855065, 0, 0, 381822, 394079, 759169, 383184, 642847, 765006, 
0, 634560, 383000, NA, 326743, 1002299, 837598, 355404, 297186, 
1399055, 393458, 80914, 1085741, 2422294, 409800, 621836, 377000, 
744659, 1107878, 649213, 1054000, NA, 741798, 539339, 900273, 
452743, 0, 1210658, 604932, 425000, 752925, 1090672, 394228, 
392651, NA, 323997, 0, 0, 919320, 0, 261913, 264578, 385369, 
307634, 250357, 469804, NA, 508604, 115314, 627150, 228478, 1565967, 
159983, 326029, 303076, 801901, 663535, 982720, 464038, 670967, 
0, 0, 1420048, 612840, 883903, 0, 339793, 361892, 299232, 368794, 
0, 1426134, 3097892, 1e+05, 359364, 0, 616904, 927555, 591748, 
753366, 534360, 698168, 949580, 1190253, 1530686, 822148, 383847, 
922607, 0, 420181, 302075, 155873, 300821, 288721, 304443, 131176, 
907342, 208983, 0, 697454, 1271716, 1756495, 469613, 864668, 
1046899, 1253490, 952853, 911874, 232009, NA, 4596832, 297199, 
969819, 162546, 679205, 714091, 0, 845583, 350619, 475221, 507543, 
130055, 0, 376177, 280697, 485000, 2593670, 1063130, 472895, 
1706345, NA, 292021, 1214084, 623396, NA, 570721, NA), Z = structure(c(440L, 
98L, 108L, 372L, 458L, 152L, 234L, 125L, 350L, 75L, 677L, 434L, 
463L, 142L, 648L, 504L, 503L, 502L, 81L, 72L, 59L, 567L, 572L, 
126L, 508L, 653L, 43L, 380L, 549L, 688L, 191L, 196L, 510L, 184L, 
190L, 2L, 224L, 523L, 225L, 575L, 236L, 242L, 556L, 231L, 257L, 
255L, 246L, 248L, 254L, 260L, 283L, 392L, 396L, 468L, 455L, 181L, 
348L, 285L, 11L, 18L, 596L, 67L, 27L, 31L, 346L, 579L, 68L, 167L, 
309L, 631L, 385L, 661L, 310L, 678L, 422L, 424L, 686L, 436L, 436L, 
437L, 302L, 388L, 153L, 480L, 327L, 168L, 495L, 36L, 572L, 420L, 
451L, 603L, 628L, 633L, 106L, 118L, 97L, 100L, 159L, 187L, 496L, 
284L, 306L, 166L, 202L, 210L, 447L, 469L, 460L, 368L, 219L, 224L, 
650L, 690L, 585L, 239L, 386L, 252L, 558L, 275L, 248L, 272L, 287L, 
379L, 379L, 290L, 304L, 301L, 574L, 590L, 324L, 9L, 331L, 22L, 
341L, 638L, 343L, 353L, 614L, 365L, 78L, 628L, 88L, 623L, 232L, 
637L, 148L, 666L, 411L, 657L, 671L, 422L, 373L, 170L, 691L, 438L, 
471L, 115L, 400L, 299L, 351L, 473L, 173L, 155L, 587L, 79L, 532L, 
396L, 478L, 22L, 642L, 251L, 99L, 182L, 552L, 484L, 129L, 641L, 
199L, 193L, 153L, 52L, 184L, 171L, 445L, 518L, 428L, 428L, 219L, 
639L, 536L, 546L, 228L, 256L, 264L, 374L, 380L, 426L, 294L, 354L, 
452L, 335L, 568L, 568L, 1L, 4L, 321L, 11L, 339L, 386L, 22L, 594L, 
37L, 44L, 619L, 63L, 621L, 617L, 80L, 628L, 377L, 631L, 94L, 
636L, 641L, 664L, 655L, 670L, 421L, 379L, 686L, 447L, 132L, 115L, 
234L, 299L, 323L, 487L, 456L, 563L, 156L, 373L, 573L, 451L, 615L, 
628L, 633L, 668L, 62L, 127L, 397L, 67L, 67L, 212L, 419L, 220L, 
218L, 219L, 224L, 597L, 530L, 238L, 3L, 229L, 249L, 200L, 364L, 
144L, 382L, 173L, 291L, 672L, 324L, 308L, 348L, 116L, 358L, 32L, 
580L, 165L, 28L, 42L, 606L, 507L, 605L, 613L, 621L, 362L, 623L, 
74L, 89L, 634L, 102L, 446L, 419L, 422L, 684L, 119L, 437L, 695L, 
467L, 476L, 461L, 135L, 314L, 413L, 581L, 673L, 79L, 110L, 357L, 
477L, 451L, 624L, 633L, 394L, 646L, 84L, 589L, 488L, 185L, 123L, 
195L, 184L, 519L, 450L, 219L, 221L, 226L, 233L, 640L, 30L, 235L, 
534L, 41L, 174L, 350L, 277L, 543L, 298L, 296L, 333L, 305L, 483L, 
308L, 512L, 70L, 6L, 330L, 337L, 594L, 44L, 349L, 346L, 613L, 
559L, 63L, 69L, 365L, 54L, 77L, 631L, 89L, 414L, 694L, 293L, 
160L, 464L, 234L, 436L, 527L, 522L, 463L, 265L, 172L, 60L, 557L, 
557L, 561L, 423L, 423L, 628L, 616L, 494L, 297L, 203L, 338L, 122L, 
207L, 468L, 222L, 219L, 444L, 481L, 650L, 43L, 449L, 544L, 231L, 
259L, 250L, 261L, 266L, 281L, 286L, 420L, 383L, 326L, 300L, 514L, 
10L, 329L, 329L, 14L, 19L, 117L, 593L, 244L, 621L, 87L, 381L, 
88L, 523L, 643L, 401L, 654L, 462L, 412L, 454L, 669L, 431L, 672L, 
687L, 682L, 91L, 117L, 428L, 684L, 46L, 436L, 86L, 48L, 139L, 
143L, 506L, 474L, 149L, 164L, 398L, 410L, 178L, 607L, 649L, 545L, 
59L, 630L, 361L, 395L, 562L, 591L, 213L, 608L, 622L, 55L, 158L, 
679L, 497L, 152L, 204L, 126L, 462L, 219L, 651L, 45L, 536L, 230L, 
265L, 265L, 572L, 244L, 248L, 276L, 261L, 334L, 291L, 453L, 389L, 
393L, 396L, 383L, 304L, 403L, 311L, 336L, 455L, 455L, 455L, 303L, 
312L, 313L, 346L, 625L, 355L, 57L, 621L, 69L, 242L, 76L, 88L, 
88L, 90L, 384L, 381L, 401L, 402L, 670L, 113L, 419L, 141L, 429L, 
511L, 689L, 433L, 693L, 680L, 459L, 515L, 149L, 351L, 175L, 498L, 
646L, 521L, 40L, 609L, 676L, 63L, 186L, 322L, 572L, 470L, 221L, 
598L, 599L, 531L, 537L, 550L, 265L, 265L, 273L, 364L, 273L, 404L, 
305L, 292L, 308L, 586L, 130L, 592L, 600L, 12L, 43L, 353L, 618L, 
620L, 623L, 359L, 74L, 85L, 634L, 92L, 614L, 643L, 643L, 652L, 
105L, 662L, 659L, 661L, 415L, 685L, 686L, 660L, 469L, 466L, 49L, 
115L, 146L, 315L, 168L, 168L, 168L, 399L, 535L, 551L, 569L, 485L, 
517L, 501L, 387L, 452L, 630L, 33L, 117L, 38L, 112L, 151L, 220L, 
233L, 651L, 533L, 272L, 240L, 278L, 361L, 363L, 318L, 576L, 25L, 
329L, 21L, 134L, 124L, 611L, 575L, 597L, 612L, 43L, 69L, 66L, 
442L, 629L, 631L, 631L, 634L, 51L, 113L, 269L, 683L, 448L, 436L, 
93L, 390L, 451L, 555L, 128L, 154L, 153L, 480L, 237L, 168L, 411L, 
570L, 503L, 502L, 557L, 416L, 583L, 613L, 633L, 180L, 96L, 96L, 
56L, 486L, 489L, 493L, 194L, 83L, 284L, 301L, 164L, 184L, 202L, 
219L, 219L, 542L, 224L, 524L, 524L, 520L, 147L, 435L, 547L, 262L, 
374L, 379L, 289L, 454L, 295L, 396L, 8L, 360L, 586L, 24L, 602L, 
103L, 344L, 613L, 619L, 69L, 502L, 61L, 375L, 631L, 633L, 644L, 
502L, 663L, 665L, 113L, 559L, 681L, 113L, 26L, 411L, 459L, 443L, 
578L, 177L, 588L, 503L, 82L, 72L, 216L, 571L, 597L, 632L, 50L, 
183L, 71L, 692L, 491L, 211L, 499L, 301L, 541L, 539L, 104L, 521L, 
223L, 523L, 234L, 586L, 353L, 235L, 282L, 376L, 316L, 389L, 389L, 
179L, 455L, 301L, 425L, 432L, 577L, 319L, 554L, 321L, 17L, 591L, 
105L, 601L, 216L, 29L, 40L, 656L, 47L, 354L, 628L, 107L, 408L, 
654L, 411L, 661L, 578L, 189L, 681L, 681L, 439L, 472L, 451L, 475L, 
150L, 205L, 188L, 279L, 197L, 163L, 564L, 133L, 503L, 59L, 522L, 
39L, 361L, 161L, 479L, 628L, 633L, 101L, 120L, 426L, 426L, 491L, 
492L, 138L, 136L, 247L, 283L, 138L, 500L, 198L, 111L, 209L, 532L, 
217L, 505L, 577L, 601L, 233L, 529L, 537L, 325L, 243L, 250L, 271L, 
242L, 266L, 270L, 465L, 513L, 202L, 140L, 455L, 1L, 565L, 578L, 
584L, 15L, 594L, 20L, 342L, 43L, 604L, 44L, 356L, 47L, 345L, 
346L, 595L, 58L, 618L, 698L, 366L, 369L, 241L, 109L, 406L, 405L, 
419L, 672L, 675L, 696L, 687L, 427L, 391L, 13L, 466L, 248L, 53L, 
603L, 340L, 171L, 409L, 417L, 516L, 528L, 647L, 526L, 373L, 623L, 
635L, 34L, 95L, 73L, 426L, 482L, 560L, 490L, 192L, 283L, 335L, 
206L, 227L, 209L, 407L, 208L, 220L, 525L, 548L, 566L, 245L, 250L, 
265L, 269L, 268L, 278L, 674L, 274L, 263L, 370L, 270L, 371L, 288L, 
393L, 292L, 320L, 441L, 332L, 336L, 378L, 307L, 365L, 568L, 318L, 
586L, 317L, 35L, 46L, 344L, 347L, 366L, 16L, 619L, 64L, 65L, 
367L, 627L, 630L, 645L, 645L, 430L, 672L, 114L, 425L, 253L, 428L, 
121L, 691L, 697L, 145L, 457L, 131L, 267L, 280L, 328L, 352L, 169L, 
157L, 7L, 610L, 523L, 619L, 82L, 526L, 72L, 582L, 380L, 583L, 
418L, 615L, 600L, 509L, 667L, 647L, 658L, 137L, 23L, 486L, 201L, 
171L, 214L, 215L, 5L, 162L, 176L, 626L, 519L, 619L, 236L, 540L, 
448L, 243L, 538L, 553L, 258L, 258L, 258L), .Label = c("10010", 
"10018", "10019", "10022", "10023", "10030", "10032", "10065", 
"10309", "10310", "10510", "10518", "10533", "10538", "10576", 
"10580", "10589", "10597", "10708", "10960", "11020", "11201", 
"11211", "11226", "11229", "11552", "11565", "11566", "11705", 
"11733", "11937", "11962", "11963", "11976", "12159", "1451", 
"14620", "15010", "1504", "15102", "15211", "15215", "15228", 
"15234", "15236", "15241", "15317", "1536", "15367", "17036", 
"17050", "1742", "18015", "18077", "1810", "1824", "18249", "18708", 
"1890", "18938", "19010", "19026", "19035", "19036", "19040", 
"19060", "19073", "19085", "19087", "19118", "19128", "1921", 
"19317", "19335", "19382", "19403", "19426", "19438", "1945", 
"19460", "1950", "1960", "19610", "19713", "19805", "1983", "20003", 
"20007", "20008", "20016", "20105", "20118", "20124", "20131", 
"20135", "20165", "20170", "20175", "20176", "20190", "20191", 
"20198", "2026", "2067", "20814", "20833", "20895", "21037", 
"21146", "2132", "2135", "2140", "22101", "22201", "22207", "22304", 
"22314", "22724", "23113", "23192", "23225", "23233", "23451", 
"2465", "2478", "2481", "2492", "26037", "26038", "2633", "26501", 
"27127", "2747", "2748", "27502", "27514", "27516", "27517", 
"27518", "27519", "27587", "2760", "27613", "27614", "27701", 
"28056", "2817", "28173", "28203", "28205", "28207", "28210", 
"28211", "28226", "2842", "2865", "2888", "2891", "29205", "29401", 
"29466", "30004", "30024", "30062", "30075", "30080", "30265", 
"30305", "30309", "30312", "30319", "3032", "30324", "30327", 
"30338", "30341", "30345", "30519", "32082", "32708", "32765", 
"32766", "32779", "32792", "32803", "32806", "32817", "32818", 
"32859", "32940", "33016", "33138", "33143", "33176", "33301", 
"33405", "33408", "33414", "33431", "33458", "33463", "33558", 
"33604", "33606", "33609", "33613", "33618", "33625", "33626", 
"33701", "33764", "33767", "34102", "34219", "34983", "34990", 
"35126", "35205", "35213", "35222", "35226", "35242", "35243", 
"35244", "35901", "35951", "3608", "36207", "36265", "36305", 
"37027", "37064", "37138", "37205", "37212", "37215", "37220", 
"42103", "43004", "43022", "43023", "43054", "43055", "43085", 
"43201", "43209", "43212", "43214", "43220", "43221", "44120", 
"44121", "44124", "44130", "44236", "45011", "45040", "45215", 
"45243", "45419", "45429", "45434", "45449", "45690", "46032", 
"46074", "46168", "46202", "46208", "46226", "46260", "47906", 
"48009", "48025", "48073", "48085", "48092", "48098", "48124", 
"48128", "48152", "48216", "48302", "48304", "48310", "48336", 
"48371", "48382", "48439", "49201", "49203", "50131", "50266", 
"52245", "53066", "53125", "53213", "53226", "55044", "55105", 
"55122", "55306", "55318", "55331", "55345", "55347", "55391", 
"55407", "55408", "55410", "55416", "55438", "55439", "55441", 
"55446", "60005", "60010", "60015", "60045", "60056", "60091", 
"60093", "60119", "60126", "60137", "60148", "60173", "60187", 
"60201", "60408", "60515", "60516", "60521", "60527", "60558", 
"60564", "60565", "60585", "60610", "60614", "60622", "60625", 
"61455", "62002", "63011", "63021", "63026", "63108", "63117", 
"63119", "63122", "63123", "63124", "63128", "63131", "63367", 
"6360", "64108", "64112", "64113", "6437", "66047", "66061", 
"6612", "66206", "66207", "66223", "66227", "67219", "6807", 
"6820", "6831", "6840", "6850", "68502", "6851", "68516", "6870", 
"6880", "6897", "70005", "7005", "7006", "70116", "70130", "7030", 
"7031", "7041", "7042", "7045", "7047", "7067", "7068", "7079", 
"7086", "7090", "7104", "72118", "72205", "74008", "74037", "74044", 
"7450", "7458", "75007", "75024", "75028", "75149", "75205", 
"75223", "75225", "75244", "75762", "76107", "76132", "76244", 
"76262", "7701", "77018", "7702", "77024", "77027", "7704", "77056", 
"77057", "7733", "77381", "77401", "77459", "77493", "77578", 
"78006", "78023", "78028", "78130", "78209", "78218", "78248", 
"78249", "78260", "7853", "78613", "78624", "78633", "78717", 
"78730", "78731", "78735", "78746", "78759", "7901", "7920", 
"7922", "7928", "7940", "79407", "80012", "80016", "80020", "80108", 
"80111", "80113", "80121", "80134", "80205", "80206", "80207", 
"80209", "80210", "80211", "80218", "80231", "80238", "80439", 
"80504", "80516", "80525", "8055", "8057", "80906", "80908", 
"80919", "8096", "81612", "83816", "84010", "84045", "84061", 
"84062", "84098", "84103", "84105", "84108", "84121", "85016", 
"85018", "85021", "85045", "85253", "85255", "85258", "85259", 
"85260", "85262", "85331", "8558", "86001", "8730", "8742", "87505", 
"87506", "8801", "8812", "8852", "8873", "90032", "90036", "90048", 
"90049", "90068", "90212", "90265", "90266", "90402", "90404", 
"90503", "90630", "90706", "90740", "90803", "91001", "91011", 
"91030", "91107", "91109", "91207", "91208", "91302", "91303", 
"91316", "91356", "91360", "91362", "91377", "91403", "91411", 
"91506", "91709", "91740", "91773", "91803", "91902", "92007", 
"92056", "92067", "92068", "92117", "92131", "92253", "92603", 
"92617", "92625", "92627", "92629", "92630", "92656", "92658", 
"92660", "92661", "92663", "92672", "92673", "92675", "92677", 
"92679", "92688", "92691", "92692", "92694", "92821", "92833", 
"92886", "93003", "93004", "93035", "93101", "93103", "93105", 
"93108", "93117", "93190", "93532", "93536", "94002", "94019", 
"94022", "94025", "94026", "94027", "94028", "94030", "94043", 
"94061", "94062", "94070", "94080", "94110", "94114", "94118", 
"94122", "94123", "94129", "94301", "94303", "94304", "94306", 
"94501", "94506", "94507", "94518", "94526", "94536", "94549", 
"94553", "94571", "94595", "94602", "94611", "94805", "94901", 
"94925", "94939", "94941", "94947", "94965", "95003", "95030", 
"95032", "95065", "95070", "95125", "95128", "95476", "95613", 
"95616", "95630", "95648", "95661", "95677", "95746", "95747", 
"95816", "95926", "97007", "97034", "97086", "97103", "97201", 
"97205", "97209", "97210", "97211", "97212", "97219", "97221", 
"97223", "97229", "97239", "97376", "97702", "98004", "98033", 
"98036", "98038", "98046", "98053", "98072", "98075", "98101", 
"98103", "98105", "98110", "98112", "98115", "98117", "98125", 
"98126", "98144", "98146", "98178", "98199", "98223", "98236", 
"98260", "98312", "98392", "98506", "98686"), class = "factor")), .Names = c("P", 
"Z"), row.names = c(NA, -1000L), class = "data.frame")

有两种方法可以做到这一点,假设您有一个带有zipcodey:的数据帧

您可以使用创建这样一个带有起始数据的数据帧

df <- data.frame("zipcode" = x, y)

使用数据表

library(data.table)
# Create an imputation function for values that are NA
impute.mean <- function(x) replace(x, is.na(x), mean(x, na.rm = TRUE))
# Cast df as a data table
df <- as.data.table(df)
# Use the function to impute y by zipcode
df[, y := impute.mean(y), by = zipcode]

使用dplyr

library(dplyr)
# Create an imputation function for values that are NA
impute.mean <- function(x) replace(x, is.na(x), mean(x, na.rm = TRUE))
# Use dplyr to group by and run function
df <- df %>%
  group_by(zipcode) %>%
  mutate(
    y = impute.mean(y)
  )

我建议下次找一找;这个问题以前可能已经回答过了,比如这里:

如何用R中的子集均值替换NA(用plyr估算?)

mice中的norm方法的问题在于,它不像R中的其他函数(例如lm)那样识别因子变量。因此,如果你的目标是以分类变量为条件,你必须首先对因子变量进行某种编码。

下面是一个使用分类变量的伪编码方案的示例。

# create data set with dummy coding for Z
dat.dummy <- as.data.frame(model.matrix(~Z, data=dat))
dat.dummy$P <- dat$P
# empty imputation (provides a template for the "predictorMatrix" and
# "method" arguments)
library(mice)
imp0 <- mice(dat.dummy, m=5, maxit=0)
# get defaults
predMatrix <- imp0$predictorMatrix
impMethod <- imp0$method
# choose "norm" for P (instead of the default "pmm")
impMethod["P"] <- "norm"
# impute 5 times, using 5 iterations each
imp1 <- mice(dat.dummy, m=5, maxit=5)

当使用model matrix()时,伪编码会自动完成,这会创建一系列指示变量,表示Z中的不同级别。

使用伪编码数据,mice将执行您想要的操作。

相关内容

  • 没有找到相关文章

最新更新