使用lm的R中的可变长度不同

时间:2018-09-06 17:12:56

标签: r lm

在R中运行lm时,我会感到讨厌:

  

可变长度不同(为'returnsandp'找到)

我运行以下模型:

# regress apple price return on s&p price return
attach(NewSetSlide.ex)
resultr = lm(returnapple ~ returnsandp)
summary(resultr)

没有比这更简单的了,但是由于某种原因,我得到了上面的错误。

我检查了returnapple和returnandp的长度是否完全相同。那么,请问到底是怎么回事?

有问题的data.frame

NewSetSlide.ex <- structure(list(returnapple = c(0.1412251, 0.07665801, 0.02560235, 
0.09638143, 0.06384145, 0.05163189, -0.1076969, 0.05121892, 0.06428114, 
0.09939652, 0.07271771, 0.06923432, 0.02873109, 0.0721757, -0.0121841, 
0.07196034, 0.1012038, -0.06786657, 0.06142434, 0.09644931, -0.02754909, 
0.005786519, 0.04099078, -0.03320592, -0.03292676, -0.06908485, 
-0.01878077, 0.08340874, -0.01004186, -0.1064195, -0.07524236, 
-0.006677446, 0.133327, -0.139921, 0.06528701, -0.036831, 0.09006266, 
0.01813659, 0.07127628, 0.004334296, -0.02659846, 0.05333548, 
0.04774654, 0.1288835, 0.05323629, -0.00006978558, 0.0634182, 
-0.0533224, 0.03270362, 0.1026693, -0.05655361, 0.09680779, 0.01662336, 
-0.01170586, -0.01063646, 0.0638476, -0.0542103, -0.01501973, 
0.1307637, -0.005598485, 0.02798327, 0.1962269, 0.006725292, 
0), returnsandp = c(0.1159772758, 0.007614392, 0.1104467964, 
0.0359706698, 0.0152313579, 0.0331342721, 0.0189951476, 0.0330947526, 
0.0749868297, -0.0124064592, 0.0323295771, -0.0303030364, 0.0113188732, 
0.0101582303, -0.0151743475, 0.0174258083, -0.0088341409, -0.0092159647, 
-0.0388593467, 0.0134979946, 0.0054655738, -0.05935645, 0.0174692125, 
-0.0164511628, 0.1063320628, -0.0034796438, -0.0000602649, -0.0151122528, 
0.0223743915, 0.0740851449, 0.0086287811, -0.0028700134, -0.0045942764, 
0.0540510532, 0.0121340172, -0.0048475787, -0.0119945162, -0.034724078, 
0.0425088143, 0.0650615875, 0.0450610926, 0.0023665278, 0.0714892769, 
0.052793919, -0.0141481377, 0.0502292875, 0.0141095206, -0.0586828306, 
0.071192607, -0.0854386059, 0.05472933, 0.0214771911, -0.0282882713, 
0.1317668962, 0.0369236189, 0.0263898652, -0.0114502121, 0.0060341972, 
0.0479144906, 0.0482236974, 0.0349588397, -0.0241661652, -0.2176304161, 
-0.0853488645)), class = "data.frame", row.names = c(NA, -64L))

1 个答案:

答案 0 :(得分:0)

最好在data=NewSetSlide.ex函数调用中使用lm参数,以避免命名冲突以及避免attach函数。请看如下:

NewSetSlide.ex <- structure(list(returnapple = c(0.1412251, 0.07665801, 0.02560235, 
0.09638143, 0.06384145, 0.05163189, -0.1076969, 0.05121892, 0.06428114, 
0.09939652, 0.07271771, 0.06923432, 0.02873109, 0.0721757, -0.0121841, 
0.07196034, 0.1012038, -0.06786657, 0.06142434, 0.09644931, -0.02754909, 
0.005786519, 0.04099078, -0.03320592, -0.03292676, -0.06908485, 
-0.01878077, 0.08340874, -0.01004186, -0.1064195, -0.07524236, 
-0.006677446, 0.133327, -0.139921, 0.06528701, -0.036831, 0.09006266, 
0.01813659, 0.07127628, 0.004334296, -0.02659846, 0.05333548, 
0.04774654, 0.1288835, 0.05323629, -0.00006978558, 0.0634182, 
-0.0533224, 0.03270362, 0.1026693, -0.05655361, 0.09680779, 0.01662336, 
-0.01170586, -0.01063646, 0.0638476, -0.0542103, -0.01501973, 
0.1307637, -0.005598485, 0.02798327, 0.1962269, 0.006725292, 
0), returnsandp = c(0.1159772758, 0.007614392, 0.1104467964, 
0.0359706698, 0.0152313579, 0.0331342721, 0.0189951476, 0.0330947526, 
0.0749868297, -0.0124064592, 0.0323295771, -0.0303030364, 0.0113188732, 
0.0101582303, -0.0151743475, 0.0174258083, -0.0088341409, -0.0092159647, 
-0.0388593467, 0.0134979946, 0.0054655738, -0.05935645, 0.0174692125, 
-0.0164511628, 0.1063320628, -0.0034796438, -0.0000602649, -0.0151122528, 
0.0223743915, 0.0740851449, 0.0086287811, -0.0028700134, -0.0045942764, 
0.0540510532, 0.0121340172, -0.0048475787, -0.0119945162, -0.034724078, 
0.0425088143, 0.0650615875, 0.0450610926, 0.0023665278, 0.0714892769, 
0.052793919, -0.0141481377, 0.0502292875, 0.0141095206, -0.0586828306, 
0.071192607, -0.0854386059, 0.05472933, 0.0214771911, -0.0282882713, 
0.1317668962, 0.0369236189, 0.0263898652, -0.0114502121, 0.0060341972, 
0.0479144906, 0.0482236974, 0.0349588397, -0.0241661652, -0.2176304161, 
-0.0853488645)), class = "data.frame", row.names = c(NA, -64L))

resultr <- lm(returnapple ~ returnsandp, dat = NewSetSlide.ex)
summary(resultr)

输出:

Call:
lm(formula = returnapple ~ returnsandp, data = NewSetSlide.ex)

Residuals:
         Min           1Q       Median           3Q          Max 
-0.166599181 -0.041838291  0.003778841  0.044034591  0.166774667 

Coefficients:
                Estimate   Std. Error  t value  Pr(>|t|)   
(Intercept)  0.028595156  0.008677931  3.29516 0.0016294 **
returnsandp -0.035466006  0.160976847 -0.22032 0.8263478   
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

Residual standard error: 0.0672294 on 62 degrees of freedom
Multiple R-squared:  0.0007822871,  Adjusted R-squared:  -0.01533413 
F-statistic: 0.04853977 on 1 and 62 DF,  p-value: 0.8263478