title: How can I train a model in statsmodels?
body: <p>This is a pretty straightforward question and I know some will be inclined to give a -1, but please let me explain better.</p>
  <p>Most of statsmodels tutorials in the internet (such as <a href="https://www.statsmodels.org/stable/gettingstarted.html" rel="nofollow noreferrer">this</a>, <a href="https://www.learndatasci.com/tutorials/predicting-housing-prices-linear-regression-using-python-pandas-statsmodels/" rel="nofollow noreferrer">this</a> and <a href="https://medium.com/evidentebm/linear-regression-using-statsmodels-d0db5fef16bb" rel="nofollow noreferrer">this</a>) usually create a Linear Regression without splitting the dataset into train and test. They create a linear regression using this sintax:</p>
  <pre><code>import statsmodels.formula.api as sm
  sm.ols('y~x1+x2+x3', data=df).fit()
  </code></pre>
  <p>There is no need to say how dangerous is to build a model without a test dataset.</p>
  <p>My question here is how can I create a linear regression with statsmodels, using train and test split?</p>
  <p>After searching a lot, I found this approach:</p>
  <pre><code>from sklearn.model_selection import train_test_split
  
  X_train, X_test, y_train, y_test = train_test_split(
  features, target, train_size=0.8, random_state=42
  )
  
  import statsmodels.api as sm
  
  smfOLS = smf.OLS(X_train, y_train).fit()
  </code></pre>
  <p>However, I'm getting this error:</p>
  <pre><code>AttributeError: module 'statsmodels.formula.api' has no attribute 'OLS'
  </code></pre>
  <p>I know I should provide a dataset, but unfortunately, I'm working with confidential data. But any dataset you have should be enough to understand the situation.</p>