# Difference between OLS(statsmodel) and Scikit Linear Regression

I have a question about two different methods from different libraries which seems doing same job. I am trying to make linear regression model.

Here is the code which I using statsmodel library with OLS :

X_train, X_test, y_train, y_test = cross_validation.train_test_split(x, y, test_size=0.3, random_state=1)

model = sm.OLS(y_train, x_train)
results = model.fit()

print "GFT + Wiki / GT  R-squared", results.rsquared


This print out GFT + Wiki / GT R-squared 0.981434611923

and the second one is scikit learn library Linear model method:

model = LinearRegression()
model.fit(X_train, y_train)

predictions = model.predict(X_test)

print 'GFT + Wiki / GT R-squared: %.4f' % model.score(X_test, y_test)


This print out GFT + Wiki / GT R-squared: 0.8543

So my question is the both method prints our R^2 result but one is print out 0.98 and the other one is 0.85.

From my understanding, OLS works with training dataset. So my questions,

• Is there a way that work with test data set with OLS ?
• Is the traning data set score gives us any meaning(In OLS we didn't use test data set)? From my past knowledge we have to work with test data.
• What is the difference between OLS and scikit linear regression. Which one we use for calculating the score of the model ?

Thanks for any help.

So my question is the both method prints our R^2 result but one is print out 0.98 and the other one is 0.85.

There is a lot of information missing here, but it looks like the first example could be finding the correlation between the predicted and actual values of data from your training set and the second example is finding the correlation between the predicted and actual values of your testing set. This would also explain why the correlation in the second result is lower. The performance of a model on novel (testing) data should nearly always be poorer than on the training dataset that was used to fit the model.

Is there a way that work with test data set with OLS ?

The statsmodels documentation describes a predict() method that can be used to make predictions on inputs from a design matrix. You should be able to use this method on your test data once you have fit the model.

Is the traning data set score gives us any meaning(In OLS we didn't use test data set)? From my past knowledge we have to work with test data.

Evaluation on training data can often be used as a quick check for "best case" generalization performance. However, this is often only used as a diagnostic to ensure your model has fit properly and should not be taken to be a true indicator of generalization.