<SYSTEM_TASK:>
Given the following text description, write Python code to implement the functionality described below step by step
<END_TASK>
<USER_TASK:>
Description:
 Quandl 2
Step1:  2. Applying some descriptive analytics
Step2:  3. Applying time series analysis
Step3:  4. Running a linear regressions
Step4:  5. VAR
Step5:  6. Panel data
Step6:  7. OLS with Pandas
Step7:  8. Logistic regression
<ASSISTANT_TASK:>
Python Code:
import quandl
with open('quandl_key.txt','r') as f:
    key = f.read()
data = quandl.get(["FRED/GDP","FRED/UNRATE", "FRED/FEDFUNDS", "FRED/CPIAUCSL"],authtoken=key, collapse="annual")
data.head()
data[[0]].head()
Explanation: Quandl 2: Analysis
This notebook describes the following:
1. Getting data from quandl,
2. Applying some descriptive analytics,
3. Applying time series analysis,
4. Running a linear regression,
5. Code: VAR,
6. Code: Fixed effects,
7. OLS with Pandas,
8. Logistic regression.
Note: the notebook itself aims to introduce the "analysis" interface without concetrating on details of rigorous economic research.
Resources:
- Pandas computational functions
1. Getting data from quandl
End of explanation
data.info()
data.describe()
data.corr()
data.cov()
import seaborn as sns
import matplotlib.pyplot as plt
%matplotlib inline
sns.heatmap(data.corr())
Explanation: 2. Applying some descriptive analytics
End of explanation
from statsmodels.tsa.arima_model import ARIMA
from statsmodels.tsa.stattools import acf, pacf #ACF and PACF, which are later explained
acf = acf(data[[0]])
pacf = pacf(data[[0]])
plt.plot(acf) # q: number of MA components
plt.plot(pacf,'r') # p: number of AR components
from statsmodels.tsa.stattools import adfuller
stationarity_test = adfuller(data["FRED/GDP - Value"])
print(stationarity_test[1])
model = ARIMA(data[[0]], order=(1, 1, 0)) #AR(p), I(d), MA(q)
results = model.fit()
plt.plot(data[[0]].diff()) # plot the original data graph
plt.plot(results.fittedvalues, 'r') # plot the fitted graph 
from statsmodels.tsa.arima_model import ARIMAResults
results.summary()
Explanation: 3. Applying time series analysis
End of explanation
from statsmodels.formula.api import ols
model_ols = ols(formula="data[[0]] ~ data[[1]]+data[[2]]+data[[3]]", data=data)
results_ols = model_ols.fit()
results_ols.summary()
data[[0]].hist(bins=5)
data[[0]].diff().hist()
import numpy as np
np.log(data[[0]]).hist()
results_ols.resid.hist()
sns.distplot(results_ols.resid)
Explanation: 4. Running a linear regressions
End of explanation
from statsmodels.tsa.api import VAR
model_var = VAR(mdata[[0]])
results_var = model_var.fit()
results_var.summary()
Explanation: 5. VAR
End of explanation
from pandas.stats.plm import PanelOLS
reg  = PanelOLS(y=data[[0]],x=data[[1]],time_effects=True)
reg
Explanation: 6. Panel data
End of explanation
from pandas.stats.ols import OLS
linear  = OLS(y=data["FRED/GDP - Value"],x=data["FRED/UNRATE - Value"])
linear
Explanation: 7. OLS with Pandas
End of explanation
data[[0]].pct_change().mean()
import numpy as np
data["GDP_status"] = np.where(data[[0]].pct_change()>data[[0]].pct_change().mean(),1,0)
data.head()
data["FRED/UNRATE - Value"]= data["FRED/UNRATE - Value"].fillna(data["FRED/UNRATE - Value"].mean())
from statsmodels.api import Logit
logit = Logit(data['GDP_status'], data["FRED/CPIAUCSL - Value"])
results_logit = logit.fit()
results_logit.summary()
Explanation: 8. Logistic regression
End of explanation
<END_TASK>