Higher $r^2$ value on test data than training data? I am trying to create a linear regression model.  I split my data into training and testing data, and built a model.  The $R^2$ value on the training data is 0.840.  Then I ran the model on the test data.  When I calculate the $R^2$, I get 0.982:
y.predicted <- predict(lm1, newdata=test)
y.actual <- donation_test$yval
errors <- (y.actual - y.predicted)
1 - sum(errors^2)/sum(y.actual^2)
[1] 0.9823576  

What I am doing wrong?  It seems very unlikely that my model fits my test data better than my training data.
 A: I think the formula to calculate r-squared is 
R-squared = 1 - (RSS/TSS)

where TSS = sum((y-mean(y))^2) and RSS = sum((y-y.predict)^2)
A: $R^2$ value is not a metric for model selection or model fit. 
The reason for this is that there is inherent variability of data may affect the $R^2$.
Consider the following data sets:

The (Y_ v/s X) plot has more spread than (Y v/s X). As a result the $R^2$ value for the previous(which has more variance) will be lower than the latter(which has less variance).
This proves that you must not use the $R^2$ value to check if the model is fitting the data well or not.
Instead you should check the model assumptions of :


*

*Linear Trend : (from scatter plot)

*Constant variance of error : (from residual v/s fit plot)

*Normal distribution of error : (from QQ plot)

A: One explanation might relate to how you subset your test data (they way you split training and testing data). If your test data only consists of (just a few) similar observations then it is very likely for your R-squared measure to be different than that of the training data.
A good practice is to split X% of the data selected randomly into the training set, and the remaining (100 - X)% into your test data.
Also, generally speaking, you should not be using R-squared for your test data but something like RMSE or MSE instead.
