matplotlib inline

import pandas as pd
import numpy as np
from numpy import log
import statsmodels.formula.api as smf

"""
Explanation: OLS Through StatsModels vs Projection
In this exercise we're going to run a regression using some trade data.  (The regression model is a gravity model, although the details don't really matter for this exercise.)  The idea is to compute the OLS coefficients and other related quantities using 

A regression package, and
The expressions given in the lecture on orthogonal projection.

Note that you need to download the data set "trade_data.csv" as well as this notebook.
Your task is to complete the notebook, as discussed below.
First let's try a standard approach, using StatsModels.
End of explanation
"""


data = pd.read_csv("trade_data.csv")

"""
Explanation: First we read in the data.
End of explanation
"""


data.head()

"""
Explanation: Let's see what it looks like.
End of explanation
"""


data.columns

"""
Explanation: Let's get a full list of columns.
End of explanation
"""


formula = "log(value) ~ log(egdp) + log(igdp) + log(dist)"
model = smf.ols(formula, data)

result = model.fit(cov_type='HC1')
print(result.summary())

"""
Explanation: Let's regress 'value' on 'egdp', 'igdp' and 'dist', all in logs.  To do this we make a formula object.
End of explanation
"""


data2 = data[['value', 'egdp', 'igdp', 'dist']]
data2 = data2.dropna()

y = np.asarray(np.log(data2.value))
X = np.ones((len(y), 4))
X[:, 1] = log(data2.egdp)
X[:, 2] = log(data2.igdp)
X[:, 3] = log(data2.dist)

"""
Explanation: Replication using Projection
Now let's reproduce the same values using the formulas from the lecture on projection.  I'm going to be nice and build $\mathbf X$ and $\mathbf y$ for you.
End of explanation
"""


# Derive betahat using the expression from the lectures
print(betahat)

"""
Explanation: Now reproduce the coefficients by computing $\hat \beta$, using the matrix expression given in the lectures.
End of explanation
"""


# Derive R^2 using y, Py, etc. as defined in the lecture
print(Rsq)

"""
Explanation: Next replicate the value for $R^2$ produced in the table above using the formula given in the lecture slides.
End of explanation
"""


