2015-10-05 47 views
0

因此,我将最终对多组预测变量进行多元回归。为了确保我正确地使用数据并通过玩具模型获得预期的结果。但是,当我尝试使用预测时,它并不预测新数据,而且由于新数据的大小与训练集的大小不同,所以它给我一个错误。我在网上查看并尝试了各种各样的东西,但都没有成功。我几乎准备放弃并编写自己的函数,但我也在用pls包构建模型,我猜可能已经在内部调用了它,所以我想要保持一致。这里是短脚本我写道:R:如何在测试集上使用预测

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但我增加了噪音,这不会造成很大的差异。我如何得到它,所以我可以预测一个独立的测试集。

感谢

+0

你需要在'data.frame'在用于''newdata'预测相同的名称()' ,例如。 'X <-data.frame(X1 = XG1,X2 = XG2)' –

回答

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),然后将在公式中搜索正确的变量。结果是您可以从原始数据中获得拟合值。

通过使你们的名字在newdata符合原来的解决这个问题: 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