本文介绍了R:predict()中数字"envir" arg的长度不为1的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用predict()函数通过将变量传递到模型中来预测R中的值.

I'm trying to predict a value in R using the predict() function, by passing along variables into the model.

我遇到以下错误:

Error in eval(predvars, data, env) : 
  numeric 'envir' arg not of length one

这是我的data frame,名称为df:

Here is my data frame, name df:

df <- read.table(text = '
     Quarter Coupon      Total
1   "Dec 06"  25027.072  132450574
2   "Dec 07"  76386.820  194154767
3   "Dec 08"  79622.147  221571135
4   "Dec 09"  74114.416  205880072
5   "Dec 10"  70993.058  188666980
6   "Jun 06"  12048.162  139137919
7   "Jun 07"  46889.369  165276325
8   "Jun 08"  84732.537  207074374
9   "Jun 09"  83240.084  221945162
10  "Jun 10"  81970.143  236954249
11  "Mar 06"   3451.248  116811392
12  "Mar 07"  34201.197  155190418
13  "Mar 08"  73232.900  212492488
14  "Mar 09"  70644.948  203663201
15  "Mar 10"  72314.945  203427892
16  "Mar 11"  88708.663  214061240
17  "Sep 06"  15027.252  121285335
18  "Sep 07"  60228.793  195428991
19  "Sep 08"  85507.062  257651399
20  "Sep 09"  77763.365  215048147
21  "Sep 10"  62259.691  168862119', header=TRUE)


str(df)
'data.frame':   21 obs. of  3 variables:
 $ Quarter   : Factor w/ 24 levels "Dec 06","Dec 07",..: 1 2 3 4 5 7 8 9 10 11 ...
 $ Coupon: num  25027 76387 79622 74114 70993 ...
 $ Total: num  132450574 194154767 221571135 205880072 188666980 ...

代码:

model <- lm(df$Total ~ df$Coupon)

> model

Call:
lm(formula = df$Total ~ df$Coupon)

Coefficients:
(Intercept)    df$Coupon  
  107286259         1349 

现在,当我运行predict时,出现上面显示的错误.

Now, when I run predict, I get the error I showed above.

> predict(model, df$Total, interval="confidence")
Error in eval(predvars, data, env) : 
  numeric 'envir' arg not of length one

知道我要去哪里错了吗?

Any idea where I am going wrong?

谢谢

推荐答案

这里有几个问题:

  1. predict()newdata参数需要一个 predictor 变量.因此,您应该将其值传递给Coupon而不是Total,后者是模型中的 response 变量.

  1. The newdata argument of predict() needs a predictor variable. You should thus pass it values for Coupon, instead of Total, which is the response variable in your model.

预测变量需要作为数据帧中的命名列传递,以便predict()知道它所传递的数字代表什么. (当您考虑具有多个预测变量的更复杂的模型时,对此的需求就变得很明显.)

The predictor variable needs to be passed in as a named column in a data frame, so that predict() knows what the numbers its been handed represent. (The need for this becomes clear when you consider more complicated models, having more than one predictor variable).

为此,您的原始调用应通过data参数传递df,而不是直接在公式中使用它. (通过这种方式,newdata中的列名称将能够与公式的RHS上的名称匹配.)

For this to work, your original call should pass df in through the data argument, rather than using it directly in your formula. (This way, the name of the column in newdata will be able to match the name on the RHS of the formula).

合并了这些更改后,它将起作用:

With those changes incorporated, this will work:

model <- lm(Total ~ Coupon, data=df)
new <- data.frame(Coupon = df$Coupon)
predict(model, newdata = new, interval="confidence")

这篇关于R:predict()中数字"envir" arg的长度不为1的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-11 22:12