Let me state my confusion with the help of an example,

#making datasets
x1<-iris[,1]
x2<-iris[,2]
x3<-iris[,3]
x4<-iris[,4]
dat<-data.frame(x1,x2,x3)
dat2<-dat[1:120,]
dat3<-dat[121:150,]

#Using a linear model to fit x4 using x1, x2 and x3 where training set is first 120 obs.
model<-lm(x4[1:120]~x1[1:120]+x2[1:120]+x3[1:120])

#Usig the coefficients' value from summary(model), prediction is done for next 30 obs.
-.17947-.18538*x1[121:150]+.18243*x2[121:150]+.49998*x3[121:150]

#Same prediction is done using the function "predict"
predict(model,dat3)

My confusion is: the two outcomes of predicting the last 30 values differ, may be to a little extent, but they do differ. Whys is it so? should not they be exactly same?

有帮助吗?

解决方案

The difference is really small, and I think is just due to the accuracy of the coefficients you are using (e.g. the real value of the intercept is -0.17947075338464965610... not simply -.17947).

In fact, if you take the coefficients value and apply the formula, the result is equal to predict:

intercept <- model$coefficients[1]
x1Coeff <- model$coefficients[2]
x2Coeff <- model$coefficients[3]
x3Coeff <- model$coefficients[4]

intercept + x1Coeff*x1[121:150] + x2Coeff*x2[121:150] + x3Coeff*x3[121:150]

其他提示

You can clean your code a bit. To create your training and test datasets you can use the following code:

# create training and test datasets
train.df <- iris[1:120, 1:4] 
test.df <- iris[-(1:120), 1:4]

# fit a linear model to predict Petal.Width using all predictors
fit <- lm(Petal.Width ~ ., data = train.df)
summary(fit)

# predict Petal.Width in test test using the linear model
predictions <- predict(fit, test.df)

# create a function mse() to calculate the Mean Squared Error
mse <- function(predictions, obs) {
  sum((obs - predictions) ^ 2) / length(predictions)
}

# measure the quality of fit
mse(predictions, test.df$Petal.Width)

The reason why your predictions differ is because the function predict() is using all decimal points whereas on your "manual" calculations you are using only five decimal points. The summary() function doesn't display the complete value of your coefficients but approximate the to five decimal points to make the output more readable.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top