<jupyter_start><jupyter_text>Fixed & random effects Table of contents[Intro: Preprocessing data](Intro:-Preprocessing-data)   - [I.1 Load data](I.1-Load-data)   - [I.2 List variables](I.2-List-variables)   - [I.3 Choose x & y variables](I.3-Choose-x-&-y-variables)[Question 1:](Question-1:)Estimate models for infant or child mortality rates in developing countries with explanatory variables such as:GDP per capita, Total fertility rate, Health Expenditures, Immunization rates, etc. Using 5-yearly averages from the World Development Indicators data set. Select explanatory variables in the model so as to also reduce the number of missing observations in the estimation.- [1.1 Basic regression](1.1-Basic-regression)[Question 2:](Question-2:)Explain how you account for between-country differences in the estimation methods.- [2.1 Random effects](2.1-Random-effects)- [2.2 Fixed effects](2.2-Fixed-effects)    - [2.2.1 FE by entity](2.2.1-FE-by-entity)    - [2.2.2 FE by entity, time](2.2.2-FE-by-entity,-time)[Question 3:](Question-3:)Investigate if the OLS estimates differ from estimation methods that treat country specific effects as “fixed” (“dummy”) variables or “random” variables. - [3.1 Compare FE & RE](3.1-Compare-FE-&-RE)- [3.2. Pooled, cluster entity, cluster entity and time](3.2.-Pooled,-cluster-entity,-cluster-entity-and-time)  Intro: Preprocessing data  I.1 Load data<jupyter_code>import numpy as np
import pandas as pd
import statsmodels.api as sm
import matplotlib.pyplot as plt
dataset = pd.read_excel('WDI-subset5y_extended 2014 fall.xlsx', 
                   index_col=[0,1], 
                   parse_dates=True
                  )
dataset.head(6)<jupyter_output><empty_output><jupyter_text>I.2 List variables<jupyter_code>dataset.info()<jupyter_output><class 'pandas.core.frame.DataFrame'>
MultiIndex: 425 entries, ('Afghanistan', Timestamp('1990-01-01 00:00:00')) to ('Zimbabwe', Timestamp('2010-01-01 00:00:00'))
Data columns (total 23 columns):
 #   Column                                                        Non-Null Count  Dtype  
---  ------                                                        --------------  -----  
 0   Birth rate, crude (per 1,000 people)                          422 non-null    float64
 1   Births attended by skilled health staff (% of total)          306 non-null    float64
 2   Fertility rate, total (births per woman)                      421 non-null    float64
 3   GDP per capita growth (annual %)                              413 non-null    float64
 4   GDP per capita, PPP (constant 2005 international $)           402 non-null    float64
 5   Health expenditure per capita (current US$)                   338 non-null    float64
 6   Hospital beds (per 1,000 people)                              293 non-n[...]<jupyter_text>I.3 Choose x & y variables<jupyter_code>#The variableslist can be extracted from the data set by typing in: dataset.columns 

variableslist = [
       #'Birth rate, crude (per 1,000 people)',
       #'Births attended by skilled health staff (% of total)',
       #'Fertility rate, total (births per woman)',
       #'GDP per capita growth (annual %)', -Flow variable, Y is a stock variable
       #'Life expectancy at birth, total (years)', -Highly correlated with Y
       #'Primary education, pupils', -School enrolment primary is a better proxy
       #'Literacy rate, adult female (% of females ages 15 and above)', #Not enough obs
       #'Literacy rate, adult male (% of males ages 15 and above)', -Not enough obs
       #'Hospital beds (per 1,000 people)', #Explanatory variable, -Not enough obs
       #'Health expenditure per capita (current US$)', -Not enough obs
       #'School enrollment, secondary, female (% gross)', -Not enough obs
       #'School enrollment, secondary, male (% gross)', -Not enough obs
    
       #'Mortality rate, infant (per 1,000 live births)', #Y variable NOT chosen for model

#The above variables were not included in the model: 
#It includes the name of the variable and why it was not chosen    
       'Mortality rate, under-5 (per 1,000 live births)', # Y variable (Dependent variables)
       'GDP per capita, PPP (constant 2005 international $)', #Control for overall wealth       
       'Immunization, DPT (% of children ages 12-23 months)', #Treatment control
       'Immunization, measles (% of children ages 12-23 months)', #Treatment control
       'Improved sanitation facilities (% of population with access)', #General health control
       'Improved water source (% of population with access)', #General health control
       'Incidence of tuberculosis (per 100,000 people)', #Systemic disease control
       'Physicians (per 1,000 people)', #Health system control, related to wealth
       'School enrollment, primary, female (% gross)', #Education control
       'School enrollment, primary, male (% gross)', #Education control
       
]
dataset_cleaned = dataset[variableslist].dropna() #Drop the variables we don't wnat to include in the model
x = dataset_cleaned.drop(['Mortality rate, under-5 (per 1,000 live births)'], axis=1) #Drop the y variable to leave only explanatory variables
y = dataset_cleaned[['Mortality rate, under-5 (per 1,000 live births)']]

x = np.log(x)
x = sm.add_constant(x) #Add constant for regression
y = np.log(y)<jupyter_output><empty_output><jupyter_text>Question 1:Estimate models for infant or child mortality rates in developing countries with explanatory variables such as:GDP per capita, Total fertility rate, Health Expenditures, Immunization rates, etc. Using 5-yearly averages from the World Development Indicators data set. Select explanatory variables in the model so as to also reduce the number of missing observations in the estimation.d  1.1 Basic regression<jupyter_code>#Import necessary library to run regression
from linearmodels.panel import PooledOLS #Library for panel data
import statsmodels.api as sm #Library for regression

pooled_res = PooledOLS(y,x).fit(cov_type='robust') #Robust standard errors 
pooled_res.summary #Prints summary
results_text = pooled_res.summary.as_text()

import csv
resultFile = open("table.csv",'w')
resultFile.write(results_text)
resultFile.close()<jupyter_output><empty_output><jupyter_text>Question 2:  2.1 Random effects<jupyter_code>from linearmodels import RandomEffects
re_result = RandomEffects(y,x).fit(cov_type='robust') #Robust standard errors 
re_result.summary
#re_result.variance_decomposition<jupyter_output><empty_output><jupyter_text>2.2 Fixed effects  2.2.1 FE by entity<jupyter_code>from linearmodels import PanelOLS
fe_result = PanelOLS(y,x, entity_effects=True).fit(cov_type='robust')
fe_result<jupyter_output><empty_output><jupyter_text>2.2.2 FE by entity, time<jupyter_code>from linearmodels import PanelOLS
fe_result = PanelOLS(y,x, entity_effects=True, time_effects=True).fit(cov_type='robust')
fe_result<jupyter_output><empty_output><jupyter_text>3.1 Compare FE & RE<jupyter_code>from linearmodels.panel import compare
compare({'FE':fe_result,'RE':re_result,'Pooled regression':pooled_res}).summary

#'''Code to export table to csv'''
#compare({'FE':fe_result,'RE':re_result,'Pooled regression':pooled_res}).summary.as_csv()<jupyter_output><empty_output><jupyter_text>3.2. Pooled, cluster entity, cluster entity and time<jupyter_code>mod = PooledOLS(y, x)
robust = mod.fit(cov_type='robust')
clust_entity = mod.fit(cov_type='clustered', cluster_entity=True)
clust_entity_time = mod.fit(cov_type='clustered', cluster_entity=True, cluster_time=True)
from collections import OrderedDict
res = OrderedDict()
res['Robust'] = robust
res['Entity'] = clust_entity
#res['Entity-Time'] = clust_entity_time
compare(res)<jupyter_output><empty_output>
StarcoderdataJupyterStructuredCleanDedup