Predict()-也许我不明白 [英] Predict() - Maybe I'm not understanding it

查看:87
本文介绍了Predict()-也许我不明白的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

今天早些时候发布有关使用predict函数遇到的错误.我能够纠正该错误,并认为自己走在正确的道路上.

I posted earlier today about an error I was getting with using the predict function. I was able to get that corrected, and thought I was on the right path.

我有很多观察(事实),并且我想推断或预测一些数据点.我使用lm创建一个模型,然后尝试将predict与将用作预测变量输入的实际值一起使用.

I have a number of observations (actuals) and I have a few data points that I want to extrapolate or predict. I used lm to create a model, then I tried to use predict with the actual value that will serve as the predictor input.

此代码与我以前的帖子都重复了,但是在这里是:

This code is all repeated from my previous post, but here it is:

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, data=df)

> model

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

Coefficients:
(Intercept)    df$Coupon  
  107286259         1349 

预测代码(基于先前的帮助):

Predict code (based on previous help):

(这些是我要用来获取预测值的预测值)

(These are the predictor values I want to use to get the predicted value)

Quarter = c("Jun 11", "Sep 11", "Dec 11")
Total = c(79037022, 83100656, 104299800)
Coupon = data.frame(Quarter, Total)

Coupon$estimate <- predict(model, newdate = Coupon$Total)

现在,当我运行它时,我收到以下错误消息:

Now, when I run that, I get this error message:

Error in `$<-.data.frame`(`*tmp*`, "estimate", value = c(60980.3823396919,  : 
  replacement has 21 rows, data has 3

我用来构建模型的原始数据框包含21个观测值.我现在正在尝试根据模型预测3个值.

My original data frame that I used to build the model had 21 observations in it. I am now trying to predict 3 values based on the model.

我不是很了解这个功能,或者我的代码有错误.

I either don't truly understand this function, or have an error in my code.

我们将不胜感激.

谢谢

推荐答案

首先,您要使用

model <- lm(Total ~ Coupon, data=df)

不是 model <-lm(df$Total ~ df$Coupon, data=df).

第二,通过说lm(Total ~ Coupon),您正在拟合使用Total作为响应变量,以Coupon作为预测变量的模型.也就是说,您的模型的格式为Total = a + b*Coupon,其中ab为要估计的系数.请注意,响应位于~的左侧,而预测变量位于右侧.

Second, by saying lm(Total ~ Coupon), you are fitting a model that uses Total as the response variable, with Coupon as the predictor. That is, your model is of the form Total = a + b*Coupon, with a and b the coefficients to be estimated. Note that the response goes on the left side of the ~, and the predictor(s) on the right.

因此,当您要求R为模型提供预测值时,您必须提供一组新的 predictor 值,即Coupon而不是Total的新值

Because of this, when you ask R to give you predicted values for the model, you have to provide a set of new predictor values, ie new values of Coupon, not Total.

第三,根据您对newdata的说明来判断,看来您实际上是在根据Total的函数来拟合Coupon的模型,而不是相反.为此:

Third, judging by your specification of newdata, it looks like you're actually after a model to fit Coupon as a function of Total, not the other way around. To do this:

model <- lm(Coupon ~ Total, data=df)
new.df <- data.frame(Total=c(79037022, 83100656, 104299800))
predict(model, new.df)

这篇关于Predict()-也许我不明白的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆