R:如何在测试集上使用预测

时间:2015-10-05 16:09:02

标签: r model linear

所以我最终会针对不同的大量预测因子进行多元回归分析。确保我正确地输入数据并使用玩具模型获得预期结果。然而,当我尝试使用预测它不能预测新数据时,由于新数据的大小与训练集不同,它给我一个错误。我在互联网上看过并尝试了各种各样的东西,没有一个有效。我几乎准备放弃并编写自己的函数,但我也在使用pls包构建模型,我猜这可能是内部调用它所以我希望保持一致。这是我写的简短剧本:

  x1<-c(1.1,3.4,5.6,1.2,5,6.4,0.9,7.2,5.4,3.1) # Orginal Variables 
  x2<-c(10,21,25,15.2,18.9,19,16.2,22.1,18.6,22)
  y<-2.0*x1+1.12*x2+rnorm(10,mean=0,sd=0.2) # Define output variable 
  X<-data.frame(x1,x2)
  lfit<-lm(y~.,X) # fit model 
  n_fit<-lfit$coefficients

  xg1<-runif(15,1,10) # define new data 
  xg2<-runif(15,10,30)
  X<-data.frame(xg1,xg2)# put into data frame 

  y_guess<-predict(lfit,newdata=X) #Predict based on fit 
  y_actual<-2.0*xg1+1.12*xg2 # actual values because I know the coefficients
  y_pred=n_fit[1]+n_fit[2]*xg1+n_fit[3]*xg2 # What predict should give me  based on fit
  print(y_guess-y_actual) #difference check
  print(y_guess-y_pred)

这些是我得到的值和错误消息:

  [1]  -4.7171499 -16.9936498   6.9181074  -6.1964788 -11.1852816  0.9257043 -13.7968731  -6.6624086  15.5365141  -8.5009428
  [11] -22.8866505   2.0804016  -1.8728602 -18.7670797   1.2251849 
  [1]  -4.582645 -16.903164   7.038968  -5.878723 -11.149987   1.162815 -13.473351  -6.483111  15.731694  -8.456738
  [11] -22.732886   2.390507  -1.662446 -18.627342   1.431469
  Warning messages:
  1: 'newdata' had 15 rows but variables found have 10 rows 
  2: In y_guess - y_actual :
  longer object length is not a multiple of shorter object length
  3: In y_guess - y_pred :
  longer object length is not a multiple of shorter object length

预测系数是1.97和1.13并且截距-0.25,它应该是0但是我加了噪声,这不会造成很大的差异。我如何得到它以便我可以预测一个独立的测试集。

由于

1 个答案:

答案 0 :(得分:4)

在帮助文档中,?predict.lm

"Variables are first looked for in newdata and then searched for in the usual way (which will include the environment of the formula used in the fit)."

data.frame()中创建的X <- data.frame(xg1, xg2)具有不同的名称:(xg1,xg2)。 predict()无法找到原始名称(x1,x2),然后会在公式中搜索正确的变量。结果是您从原始数据中获取拟合值。

通过使新数据中的名称与原始名称一致来解决此问题: X <- data.frame(x1=xg1, x2=xg2)

x1 <- c(1.1, 3.4, 5.6, 1.2, 5, 6.4, 0.9, 7.2, 5.4, 3.1) # Orginal Variables 
x2 <- c(10, 21, 25, 15.2, 18.9, 19, 16.2, 22.1, 18.6, 22)
y <- 2.0*x1 + 1.12*x2 + rnorm(10, mean=0, sd=0.2) # Define output variable 
X <- data.frame(x1, x2)
lfit <- lm(y~., X) # fit model 
n_fit <- lfit$coefficients

xg1 <- runif(15, 1, 10) # define new data 
xg2 <- runif(15, 10, 30)
X <- data.frame(x1=xg1, x2=xg2) # put into data frame 

y_guess <- predict(lfit, newdata=X) #Predict based on fit 
y_actual <- 2.0*xg1 + 1.12*xg2 # actual values because I know the coefficients
y_pred = n_fit[1] + n_fit[2]*xg1 + n_fit[3]*xg2 # What predict should give me  based on fit

> print(y_guess - y_actual) #difference check
           1            2            3            4            5            6            7            8            9           10           11           12           13 
-0.060223916 -0.047790535 -0.018274280 -0.096190467 -0.079490487 -0.063736231 -0.047506981 -0.009523583 -0.047774006 -0.084276807 -0.106322290 -0.030876942 -0.067232989 
          14           15 
-0.023060651 -0.041264431 
> print(y_guess - y_pred)
 1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 
 0  0  0  0  0  0  0  0  0  0  0  0  0  0  0