title: Python: Loop a variable for a simple OLS
body: <p>I am looking to build a function in Python which creates a simple OLS regression with the following equation:</p>
  
  <pre><code> Y_i - Y_i-1 = A + B(X_i - X_i-1) + E
  </code></pre>
  
  <p>In words, Y_Lag = alpha + beta(X_Lag) + Error term</p>
  
  <p>Currently, I have the following dataset (This is a short version)</p>
  
  <p>Note: Y = Historic_Rate</p>
  
  <pre><code>df = pd.DataFrame(np.random.randint(low=0, high=10, size=(5, 5)), columns=['Historic_Rate', 'Overnight', '1M', '3M', '6M'])
  </code></pre>
  
  <p>So, what I'm trying to build is that I iteratively take an X variable and put it into a simple linear regression, the code I have built so far looks like this:</p>
  
  <pre><code>#Start the iteration process for the regression to in turn fit 1 parameter
  
  #Import required packages 
  
  import pandas as pd
  import numpy as np
  import statsmodels.formula.api as sm
  
  #Import dataset
  
  df = pd.DataFrame(np.random.randint(low=0, high=10, size=(5, 5)), columns=['Historic_Rate', 'Overnight', '1M', '3M', '6M'])
  #Y_Lag is always 1 time period only
  
  df['Y_Lag'] = df['Historic_Rate'].shift(1)
  
  #Begin the process with 1 lag, taking one x variable in turn
  
  array = df[0:0]
  array.drop(array.columns[[0,5]], axis=1, inplace=True)
  for X in array:
      df['X_Lag'] = df['X'].shift(1)
      Model = df[df.columns[4:5]]
      Y = Model['Y_Lag']
      X = Model['X_Lag']
  
      Reg_model = sm.OLS(Y,X).fit()
      predictions = model.predict(X) 
      # make the predictions by the model
  
      # Print out the statistics
      model.summary()
  </code></pre>
  
  <p>So, in essence, I'm looking to create a list of the column headers, which, in turn, will systematically go through my loop and each variable will be lagged and then regressed against a lagged Y variable. </p>
  
  <p>I'd also appreciate knowledge of how to output a model.X where X is the Xth iteration of the array, for a dynamic naming of variables.</p>