title: Statsmodels: Calculate fitted values and R squared
body: <p>I am running a regression as follows (<code>df</code> is a <code>pandas</code> dataframe):</p>
  
  <pre><code>import statsmodels.api as sm
  est = sm.OLS(df['p'], df[['e', 'varA', 'meanM', 'varM', 'covAM']]).fit()
  est.summary()
  </code></pre>
  
  <p>Which gave me, among others, an R-squared of <code>0.942</code>. So then I wanted to plot the original <code>y-values</code> and the fitted values. For this, I sorted the original values:</p>
  
  <pre><code>orig = df['p'].values
  fitted = est.fittedvalues.values
  args = np.argsort(orig)
  import matplotlib.pyplot as plt
  plt.plot(orig[args], 'bo')
  plt.plot(orig[args]-resid[args], 'ro')
  plt.show()
  </code></pre>
  
  <p>This, however, gave me a graph where the values were completely off. Nothing that would suggest an R-squared of <code>0.9</code>. Therefore, I tried to calculate it manually myself:</p>
  
  <pre><code>yBar = df['p'].mean()
  SSTot = df['p'].apply(lambda x: (x-yBar)**2).sum()
  SSReg = ((est.fittedvalues - yBar)**2).sum()  
  1 - SSReg/SSTot
  Out[79]: 0.2618159806908984
  </code></pre>
  
  <p>Am I doing something wrong? Or is there a reason why my computation is so far off what statsmodels is getting? <code>SSTot</code>, <code>SSReg</code> have values of <code>48084</code>, <code>35495</code>.</p>