Как исправить ошибку с кодом средних предельных эффектов в отрицательной биномиальной модели? - PullRequest
0 голосов
/ 19 июня 2020

Я использую набор данных STATA activity.dta с информацией о законодательной деятельности депутатов в Японии. Вот как выглядит содержимое набора данных, когда я удаляю недостающие значения и ограничиваю данные выборами 2012 года:

structure(list(election = c(2012L, 2012L, 2012L, 2012L, 2012L, 
2012L, 2012L, 2012L, 2012L, 2012L), candidate_id = c(12L, 19L, 
49L, 69L, 73L, 89L, 91L, 97L, 103L, 109L), name_engl = c("Mitsubayashi Hiromi", 
"Mihara Asahiko", "Kamikawa Youko", "Ueno Kenichirou", "Shimomura Hakubun", 
"Nakayama Norihiro", "Nakayama Yasuhide", "Nakajima Katsuhito", 
"Nakagawa Toshinao", "Nakagawa Masaharu"), speech_honkaigi = c(0L, 
0L, 2L, 1L, 9L, 0L, 1L, 10L, 0L, 0L), iinkai_attendance = c(111L, 
32L, 28L, 115L, 0L, 91L, 75L, 95L, 99L, 62L), iinkai_director = c(0L, 
31L, 27L, 54L, 0L, 0L, 27L, 0L, 0L, 19L), speech_iinkai = c(2L, 
2L, 0L, 3L, 0L, 2L, 7L, 40L, 1L, 11L), questions = c(0L, 0L, 
0L, 0L, 0L, 0L, 0L, 1L, 0L, 0L), party_office = c(0L, 0L, 0L, 
0L, 0L, 0L, 0L, 0L, 0L, 0L), executive_office = c(0L, 0L, 1L, 
0L, 1L, 0L, 0L, 0L, 0L, 0L), parliamentary_office = c(0L, 1L, 
1L, 1L, 0L, 0L, 1L, 0L, 0L, 1L), num_questions = c(0L, 0L, 0L, 
0L, 0L, 0L, 0L, 1L, 0L, 0L), num_pmbs_initiated = c(0L, 0L, 1L, 
0L, 0L, 0L, 0L, 0L, 0L, 3L), num_pmb_cosponsored = c(0L, 0L, 
0L, 1L, 0L, 0L, 0L, 10L, 0L, 1L), party_id = c("LDP", "LDP", 
"LDP", "LDP", "LDP", "LDP", "LDP", "YP", "LDP", "DPJ"), government = c(1L, 
1L, 1L, 1L, 1L, 1L, 1L, 0L, 1L, 0L), prefecture = c("Saitama", 
"Fukuoka", "Shizuoka", "Shiga", "Tokyo", "Kanagawa", "Osaka", 
"Yamanashi", "Hiroshima", "Mie"), district = c(14L, 9L, 1L, 2L, 
11L, 9L, 4L, 3L, 4L, 2L), block_id = c("Kitakanto", "Kyushu", 
"Tokai", "Kinki", "Tokyo", "Minamikanto", "Kinki", "Minamikanto", 
"Chugoku", "Tokai"), pure_pr = c(0L, 0L, 0L, 0L, 0L, 0L, 0L, 
0L, 0L, 0L), pure_smd = c(0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
0L), incumbency = c("freshman", "previous", "previous", "previous", 
"incumbent", "freshman", "previous", "freshman", "freshman", 
"incumbent"), term = c(0L, 5L, 3L, 1L, 5L, 0L, 2L, 0L, 0L, 5L
), sex = c("male", "male", "female", "male", "male", "male", 
"male", "male", "male", "male"), age = c(57L, 65L, 59L, 47L, 
58L, 44L, 42L, 45L, 42L, 62L), result = c("smd_elected", "smd_elected", 
"smd_elected", "smd_elected", "smd_elected", "zombie", "zombie", 
"zombie", "smd_elected", "smd_elected"), kobo = c(1L, 0L, 0L, 
1L, 0L, 1L, 0L, 1L, 1L, 0L), distbirth = c(1L, 0L, 1L, 1L, 0L, 
0L, 1L, 0L, 1L, 0L), prefbirth = c(1L, 1L, 1L, 1L, 0L, 0L, 1L, 
1L, 1L, 1L), localleg = c(0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
0L), prefleg = c(0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L), mayor = c(0L, 
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L), governor = c(0L, 0L, 0L, 
0L, 0L, 0L, 0L, 0L, 0L, 0L), hoc = c(0L, 0L, 0L, 0L, 0L, 0L, 
0L, 0L, 0L, 0L), list_rank = c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L), voteshare = c(37.92, 45.99, 36.18, 43.3, 45.48, 31.89, 
36.39, 25.6, 57.79, 43.14), margin = c(5.539999008, 16.62999916, 
12.23999977, 11.7699995, 26.21999931, -4.959998608, -2.249999285, 
-7.789999485, 24.13999939, 14.32000065), pr_block = c("Kitakanto", 
"Kyushu", "Tokai", "Kinki", "Tokyo", "Minamikanto", "Kinki", 
"Minamikanto", "Chugoku", "Tokai"), prefecture_id = c(35L, 7L, 
38L, 36L, 41L, 19L, 33L, 47L, 11L, 23L), urban = c("middle category", 
"semi-urban", "semi-urban", "rural", "urban", "urban", "urban", 
"rural", "semi-rural", "middle category"), did = c(0.76, 0.91, 
0.87, 0.26, 1, 0.98, 1, 0.22, 0.49, 0.57), distance_diet = c(33.29619733, 
843.664577, 141.7856858, 314.2946592, 11.67050022, 20.18655462, 
398.2979159, 121.4832334, 650.7236245, 307.8504406), turnout = c(67.19, 
68.21, 70.78, 66.08, 71.75, 75.52, 69.28, 64.33, 64.57, 72.71
), powerplant = c(0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L), pred_poisson = c(0.0124374323059441, 
0.279241134905134, 0.0833740277646496, 0.185103480328401, 0.0781421960854894, 
0.0371133459252611, 0.508344595012736, 0.0429773082638364, 0.0175563058470486, 
0.377740144978912)), datalabel = "", time.stamp = " 7 Apr 2020 19:26", formats = c("%8.0g", 
"%8.0g", "%20s", "%8.0g", "%8.0g", "%8.0g", "%8.0g", "%8.0g", 
"%8.0g", "%8.0g", "%8.0g", "%8.0g", "%8.0g", "%8.0g", "%9s", 
"%8.0g", "%9s", "%8.0g", "%18s", "%8.0g", "%8.0g", "%9s", "%8.0g", 
"%9s", "%8.0g", "%11s", "%8.0g", "%8.0g", "%8.0g", "%8.0g", "%8.0g", 
"%8.0g", "%8.0g", "%8.0g", "%8.0g", "%10.0g", "%10.0g", "%18s", 
"%8.0g", "%15s", "%10.0g", "%10.0g", "%10.0g", "%8.0g"), types = c(252L, 
252L, 20L, 252L, 252L, 252L, 252L, 252L, 251L, 251L, 251L, 252L, 
251L, 251L, 7L, 251L, 9L, 251L, 18L, 251L, 251L, 9L, 251L, 6L, 
251L, 11L, 251L, 251L, 251L, 251L, 251L, 251L, 251L, 251L, 251L, 
255L, 255L, 18L, 251L, 15L, 255L, 255L, 255L, 251L), val.labels = c("", 
"", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", 
"", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", 
"", "", "", "", "", "", "", "", "", "", ""), var.labels = c("", 
"", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", 
"", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", 
"", "", "", "", "", "", "", "", "", "", ""), expansion.fields = list(
    c("margin", "destring", "Characters removed were:"), c("margin", 
    "destring_cmd", "destring margin voteshare, dpcomma replace"
    ), c("voteshare", "destring", "Characters removed were:"), 
    c("voteshare", "destring_cmd", "destring margin voteshare, dpcomma replace"
    ), c("did", "destring", "Characters removed were:"), c("did", 
    "destring_cmd", "destring did distance_ turnout, dpcomma replace"
    ), c("distance_diet", "destring", "Characters removed were:"
    ), c("distance_diet", "destring_cmd", "destring did distance_ turnout, dpcomma replace"
    ), c("turnout", "destring", "Characters removed were:"), 
    c("turnout", "destring_cmd", "destring did distance_ turnout, dpcomma replace"
    )), version = 12L, na.action = structure(c(`2` = 2L, `7` = 7L, 
`9` = 9L, `10` = 10L, `17` = 17L, `19` = 19L, `22` = 22L, `26` = 26L, 
`27` = 27L, `31` = 31L, `33` = 33L, `34` = 34L, `35` = 35L, `36` = 36L, 
`39` = 39L, `40` = 40L, `41` = 41L, `45` = 45L, `48` = 48L, `51` = 51L, 
`52` = 52L, `53` = 53L, `56` = 56L, `58` = 58L, `59` = 59L, `61` = 61L, 
`63` = 63L, `67` = 67L, `70` = 70L, `72` = 72L, `74` = 74L, `80` = 80L, 
`83` = 83L, `89` = 89L, `90` = 90L, `91` = 91L, `96` = 96L, `99` = 99L, 
`100` = 100L, `101` = 101L, `109` = 109L, `110` = 110L, `111` = 111L, 
`114` = 114L, `118` = 118L, `126` = 126L, `127` = 127L, `129` = 129L, 
`130` = 130L, `131` = 131L, `134` = 134L, `136` = 136L, `139` = 139L, 
`140` = 140L, `142` = 142L, `147` = 147L, `148` = 148L, `149` = 149L, 
`150` = 150L, `151` = 151L, `152` = 152L, `154` = 154L, `155` = 155L, 
`163` = 163L, `166` = 166L, `167` = 167L, `168` = 168L, `169` = 169L, 
`174` = 174L, `179` = 179L, `181` = 181L, `184` = 184L, `188` = 188L, 
`198` = 198L, `199` = 199L, `203` = 203L, `206` = 206L, `207` = 207L, 
`208` = 208L, `209` = 209L, `213` = 213L, `215` = 215L, `218` = 218L, 
`223` = 223L, `224` = 224L, `225` = 225L, `226` = 226L, `227` = 227L, 
`228` = 228L, `229` = 229L, `230` = 230L, `231` = 231L, `232` = 232L, 
`233` = 233L, `236` = 236L, `237` = 237L, `239` = 239L, `248` = 248L, 
`260` = 260L, `261` = 261L, `266` = 266L, `269` = 269L, `273` = 273L, 
`275` = 275L, `276` = 276L, `289` = 289L, `293` = 293L, `300` = 300L, 
`303` = 303L, `304` = 304L, `305` = 305L, `306` = 306L, `307` = 307L, 
`311` = 311L, `312` = 312L, `324` = 324L, `326` = 326L, `328` = 328L, 
`329` = 329L, `330` = 330L, `335` = 335L, `336` = 336L, `338` = 338L, 
`340` = 340L, `344` = 344L, `348` = 348L, `351` = 351L, `353` = 353L, 
`355` = 355L, `358` = 358L, `362` = 362L, `374` = 374L, `379` = 379L, 
`380` = 380L, `381` = 381L, `382` = 382L, `385` = 385L, `387` = 387L, 
`390` = 390L, `394` = 394L, `400` = 400L, `402` = 402L, `407` = 407L, 
`408` = 408L, `409` = 409L, `415` = 415L, `421` = 421L, `423` = 423L, 
`424` = 424L, `425` = 425L, `426` = 426L, `427` = 427L, `431` = 431L, 
`433` = 433L, `444` = 444L, `454` = 454L, `459` = 459L, `462` = 462L, 
`463` = 463L, `464` = 464L, `466` = 466L, `469` = 469L, `471` = 471L, 
`476` = 476L, `477` = 477L, `479` = 479L, `487` = 487L, `492` = 492L, 
`502` = 502L, `503` = 503L, `508` = 508L, `512` = 512L, `513` = 513L, 
`516` = 516L, `517` = 517L, `518` = 518L, `519` = 519L, `521` = 521L, 
`528` = 528L, `534` = 534L, `540` = 540L, `546` = 546L, `552` = 552L, 
`553` = 553L, `554` = 554L, `557` = 557L, `560` = 560L, `561` = 561L, 
`566` = 566L, `567` = 567L, `568` = 568L, `572` = 572L, `575` = 575L, 
`576` = 576L, `578` = 578L, `579` = 579L, `584` = 584L, `586` = 586L, 
`591` = 591L, `596` = 596L, `597` = 597L, `606` = 606L, `607` = 607L, 
`608` = 608L, `609` = 609L, `612` = 612L, `623` = 623L, `625` = 625L, 
`627` = 627L, `634` = 634L, `635` = 635L, `636` = 636L, `642` = 642L, 
`648` = 648L, `653` = 653L, `655` = 655L, `657` = 657L, `663` = 663L, 
`664` = 664L, `667` = 667L, `671` = 671L, `681` = 681L, `686` = 686L, 
`693` = 693L, `696` = 696L, `697` = 697L, `698` = 698L, `699` = 699L, 
`703` = 703L, `705` = 705L, `706` = 706L, `709` = 709L, `710` = 710L, 
`727` = 727L, `735` = 735L, `736` = 736L, `737` = 737L, `738` = 738L, 
`739` = 739L, `741` = 741L, `753` = 753L, `759` = 759L, `761` = 761L, 
`762` = 762L, `763` = 763L, `764` = 764L, `765` = 765L, `771` = 771L, 
`774` = 774L, `775` = 775L, `776` = 776L, `777` = 777L, `780` = 780L, 
`784` = 784L, `788` = 788L, `789` = 789L, `790` = 790L, `795` = 795L, 
`796` = 796L, `799` = 799L, `810` = 810L, `814` = 814L, `816` = 816L, 
`829` = 829L, `830` = 830L, `831` = 831L, `835` = 835L, `839` = 839L, 
`842` = 842L, `843` = 843L, `847` = 847L, `848` = 848L, `849` = 849L, 
`850` = 850L, `857` = 857L, `858` = 858L, `872` = 872L, `882` = 882L, 
`888` = 888L, `889` = 889L, `894` = 894L, `897` = 897L, `899` = 899L, 
`905` = 905L, `906` = 906L, `911` = 911L, `915` = 915L, `916` = 916L, 
`917` = 917L, `925` = 925L, `926` = 926L, `928` = 928L, `932` = 932L, 
`934` = 934L, `936` = 936L, `937` = 937L, `940` = 940L, `941` = 941L, 
`942` = 942L, `943` = 943L, `949` = 949L, `953` = 953L, `957` = 957L, 
`960` = 960L, `969` = 969L, `971` = 971L, `973` = 973L, `974` = 974L, 
`975` = 975L, `977` = 977L, `983` = 983L, `985` = 985L, `991` = 991L, 
`999` = 999L, `1006` = 1006L, `1012` = 1012L, `1017` = 1017L, 
`1020` = 1020L, `1025` = 1025L, `1026` = 1026L, `1027` = 1027L, 
`1030` = 1030L, `1034` = 1034L, `1035` = 1035L, `1036` = 1036L, 
`1037` = 1037L, `1038` = 1038L, `1039` = 1039L, `1040` = 1040L, 
`1042` = 1042L, `1043` = 1043L, `1046` = 1046L, `1047` = 1047L, 
`1048` = 1048L, `1050` = 1050L, `1052` = 1052L, `1053` = 1053L, 
`1054` = 1054L, `1056` = 1056L, `1058` = 1058L, `1060` = 1060L, 
`1064` = 1064L, `1068` = 1068L, `1073` = 1073L, `1074` = 1074L, 
`1078` = 1078L, `1082` = 1082L, `1083` = 1083L, `1084` = 1084L, 
`1085` = 1085L, `1086` = 1086L, `1087` = 1087L, `1091` = 1091L, 
`1092` = 1092L, `1093` = 1093L, `1094` = 1094L, `1098` = 1098L, 
`1100` = 1100L, `1107` = 1107L, `1112` = 1112L, `1116` = 1116L, 
`1118` = 1118L, `1119` = 1119L, `1120` = 1120L, `1121` = 1121L, 
`1122` = 1122L, `1128` = 1128L, `1129` = 1129L, `1130` = 1130L, 
`1132` = 1132L, `1133` = 1133L, `1134` = 1134L, `1135` = 1135L, 
`1138` = 1138L, `1139` = 1139L, `1140` = 1140L, `1144` = 1144L, 
`1152` = 1152L, `1154` = 1154L, `1159` = 1159L, `1160` = 1160L, 
`1165` = 1165L, `1166` = 1166L, `1176` = 1176L, `1179` = 1179L, 
`1180` = 1180L, `1181` = 1181L, `1203` = 1203L, `1204` = 1204L, 
`1206` = 1206L, `1207` = 1207L, `1212` = 1212L, `1216` = 1216L, 
`1217` = 1217L, `1225` = 1225L, `1227` = 1227L, `1228` = 1228L, 
`1229` = 1229L, `1230` = 1230L, `1231` = 1231L, `1233` = 1233L, 
`1240` = 1240L, `1243` = 1243L, `1250` = 1250L, `1252` = 1252L, 
`1253` = 1253L, `1259` = 1259L, `1260` = 1260L, `1261` = 1261L, 
`1262` = 1262L, `1263` = 1263L, `1266` = 1266L, `1269` = 1269L, 
`1270` = 1270L, `1273` = 1273L, `1274` = 1274L, `1286` = 1286L, 
`1287` = 1287L, `1293` = 1293L, `1302` = 1302L, `1305` = 1305L, 
`1306` = 1306L, `1315` = 1315L, `1316` = 1316L, `1317` = 1317L, 
`1324` = 1324L, `1325` = 1325L, `1326` = 1326L, `1330` = 1330L, 
`1331` = 1331L, `1332` = 1332L, `1333` = 1333L, `1334` = 1334L, 
`1335` = 1335L, `1340` = 1340L, `1344` = 1344L, `1353` = 1353L, 
`1355` = 1355L, `1359` = 1359L, `1369` = 1369L, `1380` = 1380L, 
`1383` = 1383L, `1386` = 1386L, `1394` = 1394L, `1400` = 1400L, 
`1409` = 1409L, `1410` = 1410L, `1411` = 1411L, `1417` = 1417L, 
`1427` = 1427L, `1428` = 1428L, `1431` = 1431L, `1433` = 1433L, 
`1445` = 1445L, `1446` = 1446L, `1447` = 1447L, `1448` = 1448L, 
`1449` = 1449L, `1451` = 1451L, `1452` = 1452L, `1453` = 1453L, 
`1455` = 1455L, `1456` = 1456L, `1460` = 1460L, `1466` = 1466L, 
`1468` = 1468L, `1469` = 1469L, `1472` = 1472L, `1474` = 1474L, 
`1475` = 1475L, `1482` = 1482L, `1484` = 1484L, `1489` = 1489L, 
`1493` = 1493L, `1498` = 1498L, `1501` = 1501L, `1502` = 1502L, 
`1506` = 1506L, `1507` = 1507L, `1508` = 1508L, `1509` = 1509L, 
`1510` = 1510L, `1513` = 1513L, `1514` = 1514L, `1515` = 1515L, 
`1516` = 1516L, `1522` = 1522L, `1523` = 1523L, `1528` = 1528L, 
`1529` = 1529L, `1530` = 1530L, `1531` = 1531L, `1533` = 1533L, 
`1536` = 1536L, `1537` = 1537L, `1538` = 1538L, `1550` = 1550L, 
`1552` = 1552L, `1557` = 1557L, `1558` = 1558L, `1559` = 1559L, 
`1560` = 1560L, `1561` = 1561L, `1571` = 1571L, `1572` = 1572L, 
`1574` = 1574L, `1575` = 1575L, `1580` = 1580L, `1582` = 1582L, 
`1584` = 1584L, `1585` = 1585L, `1588` = 1588L, `1589` = 1589L, 
`1595` = 1595L, `1600` = 1600L, `1606` = 1606L, `1609` = 1609L, 
`1610` = 1610L, `1612` = 1612L, `1613` = 1613L, `1616` = 1616L, 
`1617` = 1617L, `1619` = 1619L, `1620` = 1620L, `1622` = 1622L, 
`1626` = 1626L, `1630` = 1630L, `1635` = 1635L, `1636` = 1636L, 
`1643` = 1643L, `1649` = 1649L, `1650` = 1650L, `1656` = 1656L, 
`1658` = 1658L, `1659` = 1659L, `1660` = 1660L, `1667` = 1667L, 
`1668` = 1668L, `1675` = 1675L, `1677` = 1677L, `1684` = 1684L, 
`1690` = 1690L, `1701` = 1701L, `1702` = 1702L, `1703` = 1703L, 
`1704` = 1704L, `1709` = 1709L, `1710` = 1710L, `1711` = 1711L, 
`1712` = 1712L, `1713` = 1713L, `1717` = 1717L, `1718` = 1718L, 
`1719` = 1719L, `1720` = 1720L, `1721` = 1721L, `1730` = 1730L, 
`1732` = 1732L, `1733` = 1733L, `1736` = 1736L, `1744` = 1744L, 
`1746` = 1746L, `1748` = 1748L, `1751` = 1751L, `1757` = 1757L, 
`1758` = 1758L, `1759` = 1759L, `1760` = 1760L, `1761` = 1761L, 
`1762` = 1762L, `1763` = 1763L, `1764` = 1764L, `1765` = 1765L, 
`1766` = 1766L, `1767` = 1767L, `1768` = 1768L, `1769` = 1769L, 
`1770` = 1770L, `1771` = 1771L, `1772` = 1772L, `1773` = 1773L, 
`1774` = 1774L, `1777` = 1777L, `1780` = 1780L, `1781` = 1781L, 
`1785` = 1785L, `1787` = 1787L, `1790` = 1790L, `1794` = 1794L, 
`1795` = 1795L, `1799` = 1799L, `1801` = 1801L, `1806` = 1806L, 
`1807` = 1807L, `1808` = 1808L, `1810` = 1810L, `1812` = 1812L, 
`1816` = 1816L, `1817` = 1817L, `1818` = 1818L, `1819` = 1819L, 
`1820` = 1820L, `1822` = 1822L, `1823` = 1823L, `1824` = 1824L, 
`1826` = 1826L, `1829` = 1829L, `1830` = 1830L, `1835` = 1835L, 
`1836` = 1836L, `1837` = 1837L, `1838` = 1838L, `1842` = 1842L, 
`1843` = 1843L, `1845` = 1845L, `1847` = 1847L, `1850` = 1850L, 
`1851` = 1851L, `1854` = 1854L, `1856` = 1856L, `1857` = 1857L, 
`1858` = 1858L, `1859` = 1859L, `1860` = 1860L, `1861` = 1861L, 
`1862` = 1862L, `1865` = 1865L, `1866` = 1866L, `1867` = 1867L, 
`1868` = 1868L, `1871` = 1871L, `1873` = 1873L, `1874` = 1874L
), class = "omit"), row.names = c("4", "16", "30", "38", "44", 
"60", "66", "69", "73", "79"), class = "data.frame")

Вот так выглядит заголовок набора данных:

   election candidate_id           name_engl speech_honkaigi iinkai_attendance
4      2012           12 Mitsubayashi Hiromi               0               111
16     2012           19      Mihara Asahiko               0                32
30     2012           49      Kamikawa Youko               2                28
38     2012           69     Ueno Kenichirou               1               115
44     2012           73   Shimomura Hakubun               9                 0
60     2012           89   Nakayama Norihiro               0                91
   iinkai_director speech_iinkai questions party_office executive_office
4                0             2         0            0                0
16              31             2         0            0                0
30              27             0         0            0                1
38              54             3         0            0                0
44               0             0         0            0                1
60               0             2         0            0                0
   parliamentary_office num_questions num_pmbs_initiated num_pmb_cosponsored party_id
4                     0             0                  0                   0      LDP
16                    1             0                  0                   0      LDP
30                    1             0                  1                   0      LDP
38                    1             0                  0                   1      LDP
44                    0             0                  0                   0      LDP
60                    0             0                  0                   0      LDP
   government prefecture district    block_id pure_pr pure_smd incumbency term    sex age
4           1    Saitama       14   Kitakanto       0        0   freshman    0   male  57
16          1    Fukuoka        9      Kyushu       0        0   previous    5   male  65
30          1   Shizuoka        1       Tokai       0        0   previous    3 female  59
38          1      Shiga        2       Kinki       0        0   previous    1   male  47
44          1      Tokyo       11       Tokyo       0        0  incumbent    5   male  58
60          1   Kanagawa        9 Minamikanto       0        0   freshman    0   male  44
        result kobo distbirth prefbirth localleg prefleg mayor governor hoc list_rank
4  smd_elected    1         1         1        0       0     0        0   0         1
16 smd_elected    0         0         1        0       0     0        0   0         1
30 smd_elected    0         1         1        0       0     0        0   0         1
38 smd_elected    1         1         1        0       0     0        0   0         1
44 smd_elected    0         0         0        0       0     0        0   0         1
60      zombie    1         0         0        0       0     0        0   0         1
   voteshare    margin    pr_block prefecture_id           urban  did distance_diet turnout
4      37.92  5.539999   Kitakanto            35 middle category 0.76      33.29620   67.19
16     45.99 16.629999      Kyushu             7      semi-urban 0.91     843.66458   68.21
30     36.18 12.240000       Tokai            38      semi-urban 0.87     141.78569   70.78
38     43.30 11.770000       Kinki            36           rural 0.26     314.29466   66.08
44     45.48 26.219999       Tokyo            41           urban 1.00      11.67050   71.75
60     31.89 -4.959999 Minamikanto            19           urban 0.98      20.18655   75.52
   powerplant pred_poisson
4           0   0.01243743
16          0   0.27924113
30          0   0.08337403
38          0   0.18510348
44          0   0.07814220
60          0   0.03711335

Я запускаю отрицательную биномиальную регрессию из-за чрезмерной дисперсии данных:

rm(list=ls())
library(foreign)
dat <- read.dta(file = 'activity.dta', convert.factors = TRUE)
dat_clear <- na.omit(dat)
datc_2012 <- dat_clear[dat_clear$election == 2012, ]

library(MASS)
summary(m2.negbin <- glm.nb(num_pmbs_initiated ~
voteshare + sex + age + term
+ parliamentary_office, data = datc_2012,
link = "log"))

После этого я начинаю вычислять средний предельный эффект переменной parliamentary_office с помощью подхода наблюдаемых значений. Я беру все наблюдения как их ковариаты, я изменяю parliamentary_office от 0 до 1, а затем беру среднюю разницу в прогнозируемых значениях:

#AME observed values#
df.0 <- data.frame(cbind(1,
m2.negbin[["model"]]$voteshare,
m2.negbin[["model"]]$sex,
m2.negbin[["model"]]$age,
m2.negbin[["model"]]$term,
m2.negbin[["model"]]$parliamentary_office))

colnames(df.0) <- names(coef(m2.negbin))
df.1 <- df.0
df.0[,"parliamentary_office"] <- 0
df.1[,"parliamentary_office"] <- 1

library(MASS)
set.seed(1)
b.sim <- mvrnorm(1000, coef(m2.negbin),
vcov(m2.negbin))

lin.preds.0 <- as.matrix(df.0) %*% t(b.sim)
lin.preds.1 <- as.matrix(df.1) %*% t(b.sim)

pred.count.0 <- exp(linpreds.0)
pred.count.1 <- exp(linpreds.1)

pred.mean.0 <- apply(pred.count.0, 2,
mean)
pred.mean.1 <- apply(pred.count.1, 2,
mean)

ame <- mean(pred.mean.1 - pred.mean.0)
bounds <- quantile(pred.mean.1 - pred.mean.0,
prob = c(0.025, 0.975)

И когда я дойду до строк «lin.preds .0 <- as.matrix (df.0)% <em>% t (b.sim) "и" lin.preds.1 <- as.matrix (df.1)% </em>% t (b. sim) "Я получаю следующие ошибки:

Error in as.matrix(df.0) %*% t(b.sim) : 
  requires numeric/complex matrix/vector arguments
Error in as.matrix(df.1) %*% t(b.sim) : 
  requires numeric/complex matrix/vector arguments

Как я могу это исправить и в чем причина ошибки?

...