-1

我在这个论坛上搜索过,但找不到任何合适的答案。

我正在尝试使用R中的lm()函数来拟合数据。

当我增加回归的阶数时,我会返回一些系数的NAs值。

我没有收到任何奇点警告。

任何人都可以帮我解释这些NA吗?

我补充说,我在 Excel 中重现了这个示例,并且 6 阶拟合没有明显问题。

# Data to be fitted
stock <- c(489197.2129,495385.3703,544941.481,481155.2393,435762.9306,357146.5142,356185.969,368933.2469,293205.1993,247441.8927,183643.7369,166197.9676,157520.3711,192794.255,233347.9248,284770.2749,315789.5782,371253.6707,390370.26,391904.2643,404514.2993,389401.9623,422098.5764,416199.1896,308483.5621,369373.6144,279512.2105,229390.7449,265094.0731,254002.6063,283473.7341,303136.1179,463795.7419,609924.3831,656716.8011,764936.5351,920655.356,944580.607,946031.8681,923518.1477,764901.8927,718968.5814,687068.9807,638677.6326,566646.849,568238.4514,568861.9136,507755.2549,501554.6739,477044.9342,495936.671,463228.2127,453138.3738,480070.4041,545712.6657,577885.5442,611762.2815,601142.7448,514746.6677,461110.9112,380481.1653,386708.9577,387533.3888,387386.6938,410659.1991,556877.152,645031.3825,658208.7034,630659.5014,694707.3162,742454.1986,714466.0588,711948.193,794587.0558,720513.7768,637774.9029,612416.9054,675275.3033,748426.8297,798275.4601,809230.2204,887963.61,976844.1755,951519.2965,984227.0611,1031613.419,1022424.077,954223.6635,955542.5767,920263.327,915265.1223,894546.0728,863364.3203,787588.1825,676158.8976,645554.3779,623867.4582,665281.1634,687277.3358,664088.3827,664183.8062,565105.7676,647452.9797,692280.0696,752396.7808,802440.2146,745383.8062,617119.624,574357.2181,508957.1325,532442.9542,581061.2421,525361.6981,445030.5886,500392.6723,562968.2406,604250.7148,595129.1082,612276.0135,592986.8004,521539.9552,462458.0483,506952.8987,566760.636,652918.6488,735203.327,734013.0201,853466.4269,944496.4215,970809.8151,986741.7525,1051128.913,935281.5542,734450.0057,648969.2357,561200.8083,522026.9359,525941.5821,483971.0487,509913.9941,577511.4155,623777.4897,594309.4065,673049.2068,792564.841,717606.1748,769882.0247,757618.8126,649915.7552,637335.348,643047.1521,584827.5927,594986.2344,616484.6346,682519.916,771948.7174,810259.8153,729710.4213,755624.5028,792269.7553,821344.8099,865256.8204,810055.8891,818226.3348,823462.5172,833754.4351,817210.9106,713206.5412,682863.4516,725996.9946,759555.3406,814912.3816,832986.6209,901940.123,842127.3124,844281.6881,835065.4434,898115.2783,969897.5776,864050.6036,700239.783,740024.1581,688177.6953,720837.362,746235.722,776414.6042,801468.2772,687190.4567,661752.5449,575210.847,657086.9557,741170.8837,667119.5521,716576.0049,798851.6401,925529.028,1113095.162,1170901.816,1245123.324,1118704.034,1112237.559,1077526.324,1071058.372,1056817.839,1170688.049,1147460.898,1034460.346,1103695.303,1237298.977,1158263.736,1152377.973,1216425.62,1259507.168,1230094.739,1303810.435,1271821.888,1489379.948,1497456.885,1373050.412,1354787.605,1361778.393,1153284.081,970091.4123,907278.7659,1119405.724,1167833.909,1106640.238,1089461.159,1227331.968,1291666.639,1286147.056,1206958.849,1067637.209,1020742.997,1011014.154,1115106.152,1057974.568,1056812.59,1058939.012,1020221.806,1077736.49,1110610.504,1110008.193,1114165.552,1088981.295,1112743.615,1090191.485,1035041.804,897884.0154,764819.8401,786705.2071,1047239.939,1041620.036,989888.0243,1052172.174,1142759.928,1202770.868,1354842.679,1386464.631,1284428.757,1227429.952,1191119.178,1220788.93,1337429.916,1484401.276,1541650.876,1578366.413,1696235.083,1487447.482,1326801.659,1366593.384,1377946.19,1322450.948,1306874.043,1368784.977,1439783.001,1511648.674,1652885.159,1744426.741,1730925.901,1808393.081,1842413.869,1695196.299,1716413.086,1656757.555,1391727.704,1431662.545,1352510.013,1445729.796,1498989.601,1468584.044,1355810.704,1283381.625,1341633.292,1401430.509,1479601.908,1345554.608,1360708.621,1547755.19,1510224.416,1836147.717,1933498.647,2003248.251,2039134.04,2197176.515,2273953.329,2119725.123,1883806.835,1796551.502,1826188.718,1937247.67,1790714.402,1797054.648,1570424.416,1499444.414,1472217.319,1574409.675,1576654.181,1428669.458,1443278.681,1453978.398,1724129.787,1653824.594,2010251.997,1964806.887,1795767.829,1819998.743,1848646.139,1795693.654,1626176.807,1510686.457,1742119.961,1577922.47,1568274.698,1399547.138,1450947.192,1390710.466,1485740.833,1772374.119,1903548.298,2207850.281,2297201.405,2153285.523,1935798.325,1991630.36,1861421.8,1792542.838,1846899.839,1981351.805,2068061.716,2148654.112,2108554.414,2050596.262,1899675.419,2142982.747,2152816.563,2284118.374,2187483.675,2193354.528,2358717.33,2528072.391,2451283.945)
# Produce dates from Jan 1983 to Feb 2013
t <- seq(1983.1, 2013.2, length = length(stock))
# Fit Xt =  alpha0 + alpha1^2 + alpha2^3 + alpha3^4 + alpha4^5 + alpha5^6 + eps.
lm(stock ~ poly(t, 6, raw=TRUE))

结果:

 Call:
  lm(formula = stock ~ poly(t, 6, raw = TRUE))

Coefficients:
        (Intercept)  poly(t, 6, raw = TRUE)1  poly(t, 6, raw = TRUE)2  
         -3.929e+11                5.265e+08               -1.984e+05  
poly(t, 6, raw = TRUE)3  poly(t, 6, raw = TRUE)4  poly(t, 6, raw = TRUE)5  
                 NA                8.352e-03                       NA  
poly(t, 6, raw = TRUE)6  
                 NA  
4

1 回答 1

4

Using raw=TRUE defeats the purpose of the poly function, which is to avoid the collinearity problems that arise with the use of the high-school version of polynomial fit that we were taught when we were young. If you want a more complex polynomial fit to that data, then drop the raw=TRUE:

lm(stock ~ poly(t, 6)) 

Call:
lm(formula = stock ~ poly(t, 6))

Coefficients:
(Intercept)  poly(t, 6)1  poly(t, 6)2  poly(t, 6)3  poly(t, 6)4  poly(t, 6)5  
    1030800      8866278      2217055       724873      -611351       580051  
poly(t, 6)6  
     721027  

plot(t, stock)
lines(t, predict(lm(stock ~ poly(t, 6, raw=TRUE)) ) )
lines(t, predict(lm(stock ~ poly(t, 6)) ) , col="red", lwd=3)

enter image description here

于 2013-05-06T16:37:34.570 回答