#####
#Ketevani Kapanadze 
#####
#Econometrics (Gretl Session)
#####


#PART 1 SINGLE REGRESSION 
# summary and list of the variables 
? summary  children educ electric age
? list 
# Correlation
? corr children educ electric age
# correlation is not causality! those numbers are just correlations and say nothing about causality

------------------------
? ols children educ 
# now lets plot the Y (children) and (educ) from main menu View/Graph/Scatter, lets consider single regression for simplicity (so we have only 1 regressor) 
# plotting the data
.......
# dots are the actual data, and the points on the line is the predicted values
# and the difference between actual and predicted values is the residual
# note :  we can  predict error terms which is residual. but we cannot observe error terms on their own.
# graph blue dots are the data points which we have and the red line is predicted/fitted line 
# here we have X Y and predicted Y


================

# how education affects fertility in Botswana? 
? ols children 0 educ 
# so once we estimate the model this is the estimation output 
# interpretation: every additional 1 year leads to deacrease fertility by 0.202126 unit.  
###############################################################
# Predicted values for the dependent variable
? genr y_hat = $yhat
# now lets graph yi and X from Main Menu
# so we see that the predicted yi along the fittied line which we had before
? genr u_hat=$uhat
# now lets graph ui and X
# graph ui and explanatory variables: why is it important? what does this graph tell us? 
# we can visually test for HETEROSKEDASTICITY! 

#Hypothesis testing (coefficient significantly different from zero)
? ols children 0 educ 
# comment on F-statistic, on p-value, t-ratio, confidence intervals 
# how to calculate t-ratio : fraction of coefficient to standard error
# how to calculate p-value : 4359 is degree of freadom, and 33.17 is t-statistics. so if you want to calculate manually p-valua for t-statistic 
# which is 33 and degree of freedom is around 4000 then you can use the following command. 
? pvalue t 4359 33.17
? help pvalue

##################################################################################################################################################
# PART 2 Multiple linear regression
? ols children 0 educ electric age --robust --anova
# anova command gives us sum of squared error  , sum of residuals .... 
# robust command return ols regression with robust standard errors (correction for heteroskedacity)
? help ols 
# Predicted values for the dependent variable and residuals
? genr yi_m = $yhat
? genr ui_m = $uhat
? help delete #if you want delete some variables you can use the delete command 

#test for heteroskedasticity #manual breusch pagan test
#first stage 
? genr ui = $uhat 
# we saved fitted residuals as ui and then 
#second stage 
? ols ui educ electric age # check for the heteroskedasticity
# we just regress residuals on explanatory variables and see if explanatory variables explain residuals. and if coefficients 
# are significant then we reject null hypothesis  and our null is the following: there is homoskedascity. 
# and how we can correct for heteroskedasicty, we can just run the same equation with robust standard errors. 
###
? ols 0 children educ electric age --robust

#################################################################################################################################################
# PART 3 : Instrumental Variable Model
####
#2sls
#main model
? ols  children educ electric age
# educ is endogeneous.  instrument could be urban, if a person live in urban place
# so it affects the level of educaition and indirectly affects fertility ....

# first stage, regress  endogeneous variable on instrument
? ols educ protest urban electric age
? genr y_hat = $yhat
# save predicted education 
# second stage, regress dependent variable on predicted educ and other regressors 
? ols children y_hat electric age
# we did the exercise manually now lets use the command for TSLS 
? help tsls
? tsls children educ electric age; electric age  protest urban

Hausman test -
  Null hypothesis: OLS estimates are consistent
  Asymptotic test statistic: Chi-square(1) = 48.7404
  with p-value = 2.92189e-012

Sargan over-identification test -
  Null hypothesis: all instruments are valid
  Test statistic: LM = 9.75475
  with p-value = P(Chi-square(1) > 9.75475) = 0.0017886

Weak instrument test - Ho: Instruments are weak
  First-stage F-statistic (2, 4354) = 338.12
  Critical values for desired TSLS maximal size, when running
  tests at a nominal 5% significance level:

     size      10%      15%      20%      25%
    value    19.93    11.59     8.75     7.25
    # here rule of thumb is 10.  so if we tolerate 25% of  relative bias of our instrument, then we can conclude that our instruments are not weak. 7.25 <10 
    # however if we tolerate 10% of relative bias, then we conclude that our instrument are weak<<< the name of the test is stock and yogo!
  Maximal size is probably less than 10%


  ########################################################################################################################################
  ######### PART 4 : limited dependent variable models
  ###logit and Probit 
  #generate dummy variable from children 
  ? genr dum=(children>0)
  ? summary dum 
  #linear probability model 
  ? ols dum educ age urban 
  ? genr y_ols_d=$yhat
  ? summary y_ols_d
  # the main drawback of linear probability model is that we can predict probabilities of sucess (y=1) and failure (y=0) out of range , which is 
  # above 1 and below 0.  after running the summary statistics of predicted y we see that it is indeed true, the maximum of predicted y is 1.3 
  # which is above 1.. 
  # so we face the problem here!
  # the solution is to use probit and logit models, where the CDF, cumulative distribution function is not linear any more, but in case of probit 
  # it is standard normal and in case of logit it is logistic 
  ? #probit
  ? probit dum educ age urban
  ? genr y_prob=$yhat
  ? summary y_prob
  # predicted y-s range is between 0 and 1 .. perfect! we corrected the problem of wrong prediction. 
  # note: we cannt interpret the magnitude of coefficient. we can just say if coefficient is positive, the event is more likely to happen 
  # and if it is negative then "less likely"... If you would like to interpret magnitude level then you should estimate not coefficient but marginal effects
  # and marginal effects is the slope, which is the fourth column in the probit output.. 
  #logit
  ? logit dum educ age urban 

  





















