为什么带有位置信息的模型在R中不起作用

时间:2019-02-28 06:49:22

标签: r model location

我有此子数据,并尝试使用lqmm包进行处理。

我的问题是,如果我让模型知道因变量的名称,则模型运行良好。但是,如果我让模型知道变量的位置(要求模型自己查找变量),则它不起作用。更具体地说,y在数据的第一列中。在这里,我建立两个模型:

model_1 <- lqmm(fixed=y~x, random=~1, group=id, data=data, weights=data$weight)

model_2 <- lqmm(fixed= data[,1] ~x, random=~1, group=id, data=data_4, weights=data_4$weight)

model_1工作得很好,但是model_2却没有。

由于我要分析成千上万个因变量。这意味着我要运行数千个模型。因此,我将使用循环功能(或并行功能)让模型一个接一个地运行。

任何人都可以帮助我解决如何解决此问题吗?非常感谢!

下面是我的只有一个因变量的子数据。

structure(list(y = c(0.730650409852947, 0.728216844947554, 0.767963386727689, 
0.891327480712956, 0.870997963340122, 0.872775072188941, 0.91635397123202, 
0.823406264110669, 0.829918381227682, 0.780403526888554, 0.818116890846328, 
0.784000713457594, 0.745812807881773, 0.725237449118046, 0.881360961376787, 
0.803437730675946, 0.796654355854877, 0.899503414028554, 0.921515307796896, 
0.900906505510132, 0.849416799530083, 0.847148124051182, 0.842549573350339, 
0.898004262739779, 0.909744724571248, 0.801824118905529, 0.761721812764623, 
0.833628731343284, 0.894758927447442, 0.857694883816786, 0.878278208851706, 
0.869254462170273, 0.821706914423926, 0.80044541719464, 0.847700931747595, 
0.781347150259067, 0.798202073681888, 0.867736724164886, 0.853367042059297, 
0.829895959707364), id = c(14, 14, 18, 22, 22, 22, 23, 25, 25, 
25, 26, 26, 27, 27, 28, 33, 33, 37, 37, 37, 47, 47, 47, 54, 54, 
64, 64, 72, 72, 78, 80, 80, 88, 88, 100, 100, 100, 101, 101, 
101), x = c(49.3220157451866, 49.3220157451866, 57.8198215672973, 
36.622903047587, 36.622903047587, 36.622903047587, 43.4323371060975, 
49.5278043359919, 49.5278043359919, 49.5278043359919, 48.1232404961316, 
48.1232404961316, 31.1127854711425, 31.1127854711425, 37.9682264385375, 
44.6286153851752, 44.6286153851752, 32.9579134200441, 32.9579134200441, 
32.9579134200441, 43.5192851377132, 43.5192851377132, 43.5192851377132, 
41.3467215134667, 41.3467215134667, 50.7958386259009, 50.7958386259009, 
42.0390610102311, 42.0390610102311, 50.0178728651761, 37.5973073779753, 
37.5973073779753, 37.2517969615217, 37.2517969615217, 50.0489337446737, 
50.0489337446737, 50.0489337446737, 34.914930959337, 34.914930959337, 
34.914930959337), weight = c(0.940106931323075, 1.27308556092249, 
0.985858209365709, 4.16028047821646, 1.60424079874887, 1.04527174260652, 
0.978427631147808, 1.07303283776173, 1.51996458961648, 3.69240282395003, 
0.973255510970715, 1.87809501604974, 0.998130102291579, 1.42381720181429, 
1.15527933102421, 1.02957402938536, 1.31981342276168, 1.22321221901807, 
3.17201942074959, 1.13674167341076, 0.984021029075887, 1.36396951412187, 
3.78360745964862, 1.27062020454613, 0.940399688689507, 1.27711407280325, 
0.983414979063376, 0.957791080709983, 1.29604701913859, 0.873806895526164, 
1.18174466417534, 1.0472475515176, 1.68070925571614, 1.21783984541102, 
1.13530898254566, 1.38634555990181, 5.2261616, 0.725463234706692, 
2.73692073127317, 1.12971495463948)), groups = structure(list(
    id = c(5, 10, 14, 18, 22, 23, 25, 26, 27, 28, 33, 37, 47, 
    54, 64, 72, 78, 80, 88, 100, 101, 103, 104, 113, 114, 119, 
    127, 139, 148, 149, 152, 156, 159, 164, 171, 174, 175, 185, 
    186, 188, 189, 192, 200, 208, 210, 212, 217, 230, 233, 236, 
    245, 252, 256, 258, 260, 264, 269, 271, 274, 275, 277, 278, 
    285, 290, 298, 300, 305, 309, 315, 329, 334, 337, 338, 341, 
    343, 344, 346, 347, 349, 353, 360, 362, 363, 373, 377, 381, 
    382, 385, 386, 394, 395, 398, 399, 400, 402, 405, 411, 420, 
    423, 430, 442, 448, 452, 460, 462, 468, 470, 472, 474, 479, 
    483, 484, 504, 506, 507, 508, 510, 511, 512, 516, 518, 519, 
    522, 531, 532, 533, 534, 537, 538, 543, 546, 548, 549, 550, 
    554, 556, 560, 564, 565, 566, 578, 579, 583, 585, 587, 592, 
    594, 597, 605, 607, 609, 616, 619, 620, 624, 627, 629, 632, 
    638, 640, 643, 651, 653, 655, 656, 657, 662, 663, 666, 667, 
    676, 677, 680, 684, 690, 695, 699, 705, 706, 707, 711, 716, 
    723, 725, 727, 736, 738, 741, 743, 747, 749, 751, 752, 753, 
    759, 762, 771, 772, 774, 781, 782, 785, 787, 789, 798, 802, 
    806, 807, 808, 819, 828, 834, 837, 838, 839, 850, 853, 863, 
    865, 866, 867, 872, 873, 880, 885, 891, 893, 896, 905, 906, 
    909, 913, 914, 918, 919, 920, 925, 931, 933, 939, 940, 942, 
    943, 954, 958, 966, 967, 969, 973, 978, 979, 983, 988, 989, 
    992, 1009, 1024, 1026, 1028, 1029, 1031, 1032, 1037, 1041, 
    1044, 1047, 1058, 1067, 1071, 1079, 1081, 1087, 1089, 1095, 
    1101, 1102, 1103, 1104, 1105, 1106, 1109, 1112, 1135, 1136, 
    1143, 1146, 1160, 1161, 1165, 1172, 1177, 1180, 1182, 1184, 
    1186, 1188, 1195, 1199, 1200, 1203, 1204, 1205, 1217, 1218, 
    1221, 1229, 1231, 1234, 1236, 1239, 1240, 1241, 1244, 1247, 
    1252, 1256, 1263, 1264, 1265, 1270, 1274, 1276, 1284, 1285, 
    1293, 1305, 1307, 1312, 1314, 1316, 1317, 1319, 1320, 1321, 
    1322, 1323, 1335, 1340, 1342, 1344, 1346, 1348, 1349, 1353, 
    1358, 1362, 1366, 1369, 1371, 1373, 1379, 1383, 1386, 1389, 
    1393, 1394, 1395, 1396, 1399, 1402, 1403, 1404, 1407, 1408, 
    1409, 1411, 1421, 1425, 1428, 1432, 1437, 1439, 1442, 1445, 
    1448, 1449, 1451, 1454, 1462, 1468, 1470, 1472, 1473, 1478, 
    1481, 1482, 1483, 1484, 1485, 1486, 1488, 1489, 1491, 1493, 
    1494, 1497, 1498, 1503, 1507, 1508, 1511, 1512, 1515, 1516, 
    1517, 1520, 1528, 1530, 1531, 1532, 1534, 1535, 1539, 1542, 
    1543, 1544, 1552, 1553, 1554, 1555, 1563, 1572, 1579, 1581, 
    1582, 1586, 1587, 1588, 1590, 1591, 1594, 1595, 1596, 1599, 
    1600, 1604, 1606, 1608, 1613, 1614, 1615, 1619, 1623, 1625, 
    1626, 1627, 1631, 1632, 1637, 1644, 1663, 1664, 1665, 1666, 
    1668, 1669, 1675, 1678, 1687, 1690, 1692, 1700, 1702, 1704, 
    1713, 1717, 1718, 1720, 1721, 1722, 1723, 1726, 1727, 1733, 
    1734, 1739, 1741, 1743, 1744, 1746, 1749, 1750, 1752, 1754, 
    1756, 1759, 1763, 1766, 1767, 1769, 1773, 1774, 1777, 1786, 
    1789, 1790, 1793, 1796, 1803, 1805, 1811, 1813, 1815, 1818, 
    1825, 1827, 1829, 1833, 1840, 1841, 1843, 1844, 1845, 1846, 
    1848, 1850, 1851, 1853, 1855, 1860, 1866, 1869, 1870, 1871, 
    1872, 1879, 1883, 1884, 1890, 1894, 1900, 1905, 1908, 1911, 
    1912, 1913, 1916, 1917, 1921, 1922, 1923, 1926, 1928, 1929, 
    1930, 1931, 1934, 1935, 1938, 1945, 1947, 1949, 1952, 1953, 
    1957, 1958, 1960, 1962, 1968, 1971, 1972, 1973, 1977, 1979, 
    1982, 1985, 1986, 1988, 1996, 5000, 5003, 5004, 5009, 5017, 
    5022, 5025, 5026, 5030, 5032, 5035, 5036, 5039, 5041, 5042, 
    5048, 5049, 5053, 5064, 5067, 5069, 5070, 5071, 5074, 5075, 
    5079, 5080, 5081, 5083, 5089, 5094, 5095, 5103, 5105, 5108, 
    5111, 5120, 5121, 5122, 5123, 5126, 5127, 5128, 5129, 5135, 
    5140, 5141, 5143, 5146, 5149, 5152, 5154, 5155, 5157, 5159, 
    5162, 5163, 5165, 5168, 5172, 5176, 5183, 5184, 5193, 5194, 
    5198, 5199, 5200, 5203, 5212, 5213, 5214, 5215, 5216, 5218, 
    5223, 5230, 5233, 5239, 5240, 5241, 5244, 5245, 5247, 5249, 
    5250, 5251, 5255, 5256, 5257, 5260, 5261, 5263, 5268, 5270, 
    5271, 5275, 5276, 5280), .rows = list(1:4, 5:8, 9:12, 13:16, 
        17:20, 21:24, 25:28, 29:32, 33:36, 37:40, 41:44, 45:48, 
        49:52, 53:56, 57:60, 61:64, 65:68, 69:72, 73:76, 77:80, 
        81:84, 85:88, 89:92, 93:96, 97:100, 101:104, 105:108, 
        109:112, 113:116, 117:120, 121:124, 125:128, 129:132, 
        133:136, 137:140, 141:144, 145:148, 149:152, 153:156, 
        157:160, 161:164, 165:168, 169:172, 173:176, 177:180, 
        181:184, 185:188, 189:192, 193:196, 197:200, 201:204, 
        205:208, 209:212, 213:216, 217:220, 221:224, 225:228, 
        229:232, 233:236, 237:240, 241:244, 245:248, 249:252, 
        253:256, 257:260, 261:264, 265:268, 269:272, 273:276, 
        277:280, 281:284, 285:288, 289:292, 293:296, 297:300, 
        301:304, 305:308, 309:312, 313:316, 317:320, 321:324, 
        325:328, 329:332, 333:336, 337:340, 341:344, 345:348, 
        349:352, 353:356, 357:360, 361:364, 365:368, 369:372, 
        373:376, 377:380, 381:384, 385:388, 389:392, 393:396, 
        397:400, 401:404, 405:408, 409:412, 413:416, 417:420, 
        421:424, 425:428, 429:432, 433:436, 437:440, 441:444, 
        445:448, 449:452, 453:456, 457:460, 461:464, 465:468, 
        469:472, 473:476, 477:480, 481:484, 485:488, 489:492, 
        493:496, 497:500, 501:504, 505:508, 509:512, 513:516, 
        517:520, 521:524, 525:528, 529:532, 533:536, 537:540, 
        541:544, 545:548, 549:552, 553:556, 557:560, 561:564, 
        565:568, 569:572, 573:576, 577:580, 581:584, 585:588, 
        589:592, 593:596, 597:600, 601:604, 605:608, 609:612, 
        613:616, 617:620, 621:624, 625:628, 629:632, 633:636, 
        637:640, 641:644, 645:648, 649:652, 653:656, 657:660, 
        661:664, 665:668, 669:672, 673:676, 677:680, 681:684, 
        685:688, 689:692, 693:696, 697:700, 701:704, 705:708, 
        709:712, 713:716, 717:720, 721:724, 725:728, 729:732, 
        733:736, 737:740, 741:744, 745:748, 749:752, 753:756, 
        757:760, 761:764, 765:768, 769:772, 773:776, 777:780, 
        781:784, 785:788, 789:792, 793:796, 797:800, 801:804, 
        805:808, 809:812, 813:816, 817:820, 821:824, 825:828, 
        829:832, 833:836, 837:840, 841:844, 845:848, 849:852, 
        853:856, 857:860, 861:864, 865:868, 869:872, 873:876, 
        877:880, 881:884, 885:888, 889:892, 893:896, 897:900, 
        901:904, 905:908, 909:912, 913:916, 917:920, 921:924, 
        925:928, 929:932, 933:936, 937:940, 941:944, 945:948, 
        949:952, 953:956, 957:960, 961:964, 965:968, 969:972, 
        973:976, 977:980, 981:984, 985:988, 989:992, 993:996, 
        997:1000, 1001:1004, 1005:1008, 1009:1012, 1013:1016, 
        1017:1020, 1021:1024, 1025:1028, 1029:1032, 1033:1036, 
        1037:1040, 1041:1044, 1045:1048, 1049:1052, 1053:1056, 
        1057:1060, 1061:1064, 1065:1068, 1069:1072, 1073:1076, 
        1077:1080, 1081:1084, 1085:1088, 1089:1092, 1093:1096, 
        1097:1100, 1101:1104, 1105:1108, 1109:1112, 1113:1116, 
        1117:1120, 1121:1124, 1125:1128, 1129:1132, 1133:1136, 
        1137:1140, 1141:1144, 1145:1148, 1149:1152, 1153:1156, 
        1157:1160, 1161:1164, 1165:1168, 1169:1172, 1173:1176, 
        1177:1180, 1181:1184, 1185:1188, 1189:1192, 1193:1196, 
        1197:1200, 1201:1204, 1205:1208, 1209:1212, 1213:1216, 
        1217:1220, 1221:1224, 1225:1228, 1229:1232, 1233:1236, 
        1237:1240, 1241:1244, 1245:1248, 1249:1252, 1253:1256, 
        1257:1260, 1261:1264, 1265:1268, 1269:1272, 1273:1276, 
        1277:1280, 1281:1284, 1285:1288, 1289:1292, 1293:1296, 
        1297:1300, 1301:1304, 1305:1308, 1309:1312, 1313:1316, 
        1317:1320, 1321:1324, 1325:1328, 1329:1332, 1333:1336, 
        1337:1340, 1341:1344, 1345:1348, 1349:1352, 1353:1356, 
        1357:1360, 1361:1364, 1365:1368, 1369:1372, 1373:1376, 
        1377:1380, 1381:1384, 1385:1388, 1389:1392, 1393:1396, 
        1397:1400, 1401:1404, 1405:1408, 1409:1412, 1413:1416, 
        1417:1420, 1421:1424, 1425:1428, 1429:1432, 1433:1436, 
        1437:1440, 1441:1444, 1445:1448, 1449:1452, 1453:1456, 
        1457:1460, 1461:1464, 1465:1468, 1469:1472, 1473:1476, 
        1477:1480, 1481:1484, 1485:1488, 1489:1492, 1493:1496, 
        1497:1500, 1501:1504, 1505:1508, 1509:1512, 1513:1516, 
        1517:1520, 1521:1524, 1525:1528, 1529:1532, 1533:1536, 
        1537:1540, 1541:1544, 1545:1548, 1549:1552, 1553:1556, 
        1557:1560, 1561:1564, 1565:1568, 1569:1572, 1573:1576, 
        1577:1580, 1581:1584, 1585:1588, 1589:1592, 1593:1596, 
        1597:1600, 1601:1604, 1605:1608, 1609:1612, 1613:1616, 
        1617:1620, 1621:1624, 1625:1628, 1629:1632, 1633:1636, 
        1637:1640, 1641:1644, 1645:1648, 1649:1652, 1653:1656, 
        1657:1660, 1661:1664, 1665:1668, 1669:1672, 1673:1676, 
        1677:1680, 1681:1684, 1685:1688, 1689:1692, 1693:1696, 
        1697:1700, 1701:1704, 1705:1708, 1709:1712, 1713:1716, 
        1717:1720, 1721:1724, 1725:1728, 1729:1732, 1733:1736, 
        1737:1740, 1741:1744, 1745:1748, 1749:1752, 1753:1756, 
        1757:1760, 1761:1764, 1765:1768, 1769:1772, 1773:1776, 
        1777:1780, 1781:1784, 1785:1788, 1789:1792, 1793:1796, 
        1797:1800, 1801:1804, 1805:1808, 1809:1812, 1813:1816, 
        1817:1820, 1821:1824, 1825:1828, 1829:1832, 1833:1836, 
        1837:1840, 1841:1844, 1845:1848, 1849:1852, 1853:1856, 
        1857:1860, 1861:1864, 1865:1868, 1869:1872, 1873:1876, 
        1877:1880, 1881:1884, 1885:1888, 1889:1892, 1893:1896, 
        1897:1900, 1901:1904, 1905:1908, 1909:1912, 1913:1916, 
        1917:1920, 1921:1924, 1925:1928, 1929:1932, 1933:1936, 
        1937:1940, 1941:1944, 1945:1948, 1949:1952, 1953:1956, 
        1957:1960, 1961:1964, 1965:1968, 1969:1972, 1973:1976, 
        1977:1980, 1981:1984, 1985:1988, 1989:1992, 1993:1996, 
        1997:2000, 2001:2004, 2005:2008, 2009:2012, 2013:2016, 
        2017:2020, 2021:2024, 2025:2028, 2029:2032, 2033:2036, 
        2037:2040, 2041:2044, 2045:2048, 2049:2052, 2053:2056, 
        2057:2060, 2061:2064, 2065:2068, 2069:2072, 2073:2076, 
        2077:2080, 2081:2084, 2085:2088, 2089:2092, 2093:2096, 
        2097:2100, 2101:2104, 2105:2108, 2109:2112, 2113:2116, 
        2117:2120, 2121:2124, 2125:2128, 2129:2132, 2133:2136, 
        2137:2140, 2141:2144, 2145:2148, 2149:2152, 2153:2156, 
        2157:2160, 2161:2164, 2165:2168, 2169:2172, 2173:2176, 
        2177:2180, 2181:2184, 2185:2188, 2189:2192, 2193:2196, 
        2197:2200, 2201:2204, 2205:2208, 2209:2212, 2213:2216, 
        2217:2220, 2221:2224, 2225:2228, 2229:2232, 2233:2236, 
        2237:2240, 2241:2244, 2245:2248, 2249:2252, 2253:2256, 
        2257:2260, 2261:2264, 2265:2268, 2269:2272, 2273:2276, 
        2277:2280, 2281:2284, 2285:2288, 2289:2292, 2293:2296, 
        2297:2300, 2301:2304, 2305:2308, 2309:2312, 2313:2316, 
        2317:2320, 2321:2324, 2325:2328, 2329:2332, 2333:2336, 
        2337:2340, 2341:2344, 2345:2348, 2349:2352, 2353:2356, 
        2357:2360, 2361:2364, 2365:2368, 2369:2372, 2373:2376, 
        2377:2380, 2381:2384, 2385:2388, 2389:2392, 2393:2396, 
        2397:2400, 2401:2404, 2405:2408, 2409:2412, 2413:2416, 
        2417:2420, 2421:2424, 2425:2428, 2429:2432, 2433:2436, 
        2437:2440, 2441:2444, 2445:2448, 2449:2452, 2453:2456, 
        2457:2460, 2461:2464, 2465:2468, 2469:2472, 2473:2476, 
        2477:2480, 2481:2484, 2485:2488, 2489:2492, 2493:2496, 
        2497:2500, 2501:2504, 2505:2508, 2509:2512, 2513:2516, 
        2517:2520, 2521:2524, 2525:2528, 2529:2532, 2533:2536, 
        2537:2540, 2541:2544, 2545:2548, 2549:2552, 2553:2556, 
        2557:2560, 2561:2564, 2565:2568, 2569:2572, 2573:2576, 
        2577:2580, 2581:2584, 2585:2588, 2589:2592, 2593:2596, 
        2597:2600, 2601:2604, 2605:2608, 2609:2612, 2613:2616, 
        2617:2620, 2621:2624, 2625:2628, 2629:2632, 2633:2636, 
        2637:2640, 2641:2644, 2645:2648, 2649:2652, 2653:2656, 
        2657:2660, 2661:2664, 2665:2668, 2669:2672)), row.names = c(NA, 
-668L), class = c("tbl_df", "tbl", "data.frame"), .drop = FALSE), row.names = c(9L, 
10L, 13L, 17L, 18L, 19L, 21L, 25L, 26L, 27L, 29L, 30L, 33L, 34L, 
37L, 41L, 42L, 45L, 46L, 47L, 49L, 50L, 51L, 53L, 54L, 57L, 58L, 
61L, 62L, 65L, 69L, 70L, 73L, 74L, 77L, 78L, 79L, 81L, 82L, 83L
), class = "data.frame")

1 个答案:

答案 0 :(得分:0)

R的公式语法不接受data[,1] ~ x之类的东西。但是,您可以使用formula()paste()names()轻松构建公式。假设您有一个data.frame df,而前三列是您要用作线性回归中的因变量的变量;第四列是x变量。这是一种进行方法:

df <- data.frame(
    y1 = c(1, 1, 4, 4, 5, 5, 7, 7),
    y2 = c(1, 2, 1, 2, 7, 8, 7, 8),
    y3 = c(3, 3, 4, 4, 5, 5, 6, 6),
    x  = c(1, 2, 3, 4, 5, 6, 7, 8)
)

for(i in 1:3) {
    form <- formula(paste(names(df)[i], "~ x"))
    out  <- lm(form, data=df)
    print(coef(out))
}