获得警告:“'newdata'有1行但找到的变量有32行”在predict.lm上

获得警告:“'newdata'有1行但找到的变量有32行”在predict.lm上

我在R中使用预测和lm函数时发现了特性。我对相同数据的数据帧和向量得到了不同的结果。


DataFrame代码:


data(mtcars)

fitCar<-lm(mtcars$mpg~mtcars$wt)

predict(fitCar,

        data.frame(x=mean(mtcars$wt)),

        interval="confidence")

输出:


     fit       lwr      upr

1  23.282611 21.988668 24.57655

2  21.919770 20.752751 23.08679

3  24.885952 23.383008 26.38890

4  20.102650 19.003004 21.20230

5  18.900144 17.771469 20.02882

6  18.793255 17.659216 19.92729

7  18.205363 17.034274 19.37645

8  20.236262 19.136179 21.33635

9  20.450041 19.347720 21.55236

10 18.900144 17.771469 20.02882

11 18.900144 17.771469 20.02882

12 15.533127 14.064349 17.00190

13 17.350247 16.104455 18.59604

14 17.083024 15.809403 18.35664

15  9.226650  6.658271 11.79503

16  8.296712  5.547468 11.04596

17  8.718926  6.052112 11.38574

18 25.527289 23.927797 27.12678

19 28.653805 26.519252 30.78836

20 27.478021 25.554415 29.40163

21 24.111004 22.715653 25.50635

22 18.472586 17.319886 19.62529

23 18.926866 17.799465 20.05427

24 16.762355 15.452833 18.07188

25 16.735633 15.423002 18.04826

26 26.943574 25.112491 28.77466

27 25.847957 24.198041 27.49787

28 29.198941 26.963760 31.43412

29 20.343151 19.242185 21.44412

30 22.480940 21.268498 23.69338

31 18.205363 17.034274 19.37645

32 22.427495 21.219818 23.63517

警告信息:


'newdata'有1行,但找到的变量有32行


当我将两个数据分成向量时,我得到了不同的答案


矢量代码


predict(fit,data.frame(x=mean(x)), interval="confidence")

输出:


    fit   lwr   upr

1 20.09 18.99 21.19

造成这种差异的原因是什么?


慕慕森
浏览 803回答 3
3回答

缥缈止盈

这是在您data和您之间使用不同名称的问题,newdata而不是使用向量或数据帧之间的问题。当您使用该lm函数拟合模型然后用于predict进行预测时,predict尝试在您的上查找相同的名称newdata。在您的第一个案例名称x冲突,mtcars$wt因此您得到警告。在这里看到我说的一个例子:这是你做的,没有得到错误:a <- mtcars$mpgx <- mtcars$wt#here you use x as a namefitCar <- lm(a ~ x)&nbsp;#here you use x again as a name in newdata.predict(fitCar, data.frame(x = mean(x)), interval = "confidence")&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;fit&nbsp; &nbsp; &nbsp; lwr&nbsp; &nbsp; &nbsp; upr1 20.09062 18.99098 21.19027在这种情况下,您可以使用名称x来拟合模型,并使用您的名称x进行预测newdata。这样你就不会得到任何警告,而这正是你所期望的。让我们看看当我适应模型时将名称更改为其他内容时会发生什么:a <- mtcars$mpg#name it b this timeb <- mtcars$wt&nbsp;fitCar <- lm(a ~ b)&nbsp;#here I am using name x as previouslypredict(fitCar, data.frame(x = mean(x)), interval = "confidence")&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;fit&nbsp; &nbsp; &nbsp; &nbsp;lwr&nbsp; &nbsp; &nbsp; upr1&nbsp; 23.282611 21.988668 24.576552&nbsp; 21.919770 20.752751 23.086793&nbsp; 24.885952 23.383008 26.388904&nbsp; 20.102650 19.003004 21.202305&nbsp; 18.900144 17.771469 20.02882Warning message:'newdata' had 1 row but variables found have 32 rows&nbsp;我现在做的唯一的事情就是更改名称x拟合模型的时候b,然后预测使用该名称x的newdata。正如您所看到的,我遇到了与您的问题相同的错误。希望现在很清楚!

牛魔王的故事

在lm函数的公式中,不要使用datasetname $ variablename模式引用变量。而是使用variablename + variablename ...这不会抛出警告:'newdata'有nrow(测试)行,但找到的变量有nrow(train)行。

侃侃尔雅

解决这个问题的方法是使用以下方法:fitCar<-lm(mpg&nbsp;~&nbsp;wt,&nbsp;mtcars)&nbsp;#here&nbsp;you&nbsp;use&nbsp;x&nbsp;as&nbsp;a&nbsp;namepredict(fitCar,data.frame(wt=mean(mtcars$wt)),&nbsp;interval="confidence")
打开App,查看更多内容
随时随地看视频慕课网APP