Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/64.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/elixir/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 警告信息:';新数据&x27;有1行,但找到的变量有0行_R - Fatal编程技术网

R 警告信息:';新数据&x27;有1行,但找到的变量有0行

R 警告信息:';新数据&x27;有1行,但找到的变量有0行,r,R,为什么此代码会导致以下错误: library(ISLR) lm.fit = lm(mpg[-1]~weight[-1], Auto) predict(lm.fit, data.frame(weight = Auto$weight[1])) 它不会导致除1之外的任何其他索引出错。当您使用 Warning message: 'newdata' had 1 row but variables found have 0 rows R创建一个名为weight[-1]的变量。您可以在lm.fit对象中看

为什么此代码会导致以下错误:

library(ISLR)
lm.fit = lm(mpg[-1]~weight[-1], Auto)
predict(lm.fit, data.frame(weight = Auto$weight[1]))
它不会导致除1之外的任何其他索引出错。

当您使用

Warning message:
'newdata' had 1 row but variables found have 0 rows
R创建一个名为
weight[-1]
的变量。您可以在
lm.fit
对象中看到这一点。预测函数的datafame包含
权重
,但不包含
权重[-1]

试一试


我尝试的每个索引都有一个错误。。。编辑:哦,等等——我想我明白了,你的意思是当你把两行中的“1”改成另一个值时,不仅仅是预测行。@Gleb_b只是lm.fit行实际上,预测行中的索引并不重要。我无法解释这种行为。不过我可以得到一个预测。通过相当愚蠢的
predict(lm.fit,new.data=data.frame(Auto))[1]
lm.fit = lm(mpg[-1]~weight[-1], Auto)
lm.fit = lm(mpg~weight, Auto[-1,])