Econometrics.R 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. ## Econometrics and Statistics
  2. # this makes heavy use of the "desk" packet
  3. # loading the desk-pack and deleting current environment
  4. library (desk)
  5. rm.all()
  6. ## simple LS-estimate [KQ-Schaetzung] ----
  7. # generating some data
  8. x <- c(5,10,15,20,25)
  9. y <- c(0.5,2.5,2.5,4.5,5)
  10. # optional: illustrational plot
  11. plot(x = x,y = y,
  12. main = "Sample Data",
  13. xlab = "exogenous Variable",
  14. ylab = "endogenous Variable")
  15. # least square estimate (German: KQ-Schaetzung)
  16. # Assumption: influence is in the form of
  17. # y = alpha + (beta * x) + u
  18. sdata.est <- ols(y ~ x)
  19. sdata.est
  20. sdata.est$coef[1] # alpha
  21. sdata.est$coef[2] # beta
  22. # optional: illustrational regression-line
  23. abline(a = sdata.est$coef[1],
  24. b = sdata.est$coef[2],
  25. col = "red")
  26. ## Significance test ----
  27. ## t-test
  28. # hypotheses testing for betas significancy (t-test)
  29. # Assumption: beta is significant. Test is for the opposite.
  30. # H0: "beta = 0", sig.lvl is 5%
  31. # desk-packet
  32. sdata.t <- t.test.coef(sdata.est,
  33. q = 0, # value of H0
  34. dir = "both",
  35. sig.level = 0.05,
  36. nh = c(0,1))
  37. sdata.t$results[5]
  38. # H0 rejected, so beta is significant
  39. # manual
  40. sdata.N <- sdata.est$nobs
  41. sdata.Suu <- sdata.est$ssr
  42. sdata.Sxx <- sum((x - mean(x))^2)
  43. sdata.resid.var <- sdata.Suu / (sdata.N - 2)
  44. sdata.resid.var <- sdata.est$sig.squ # ALTERNATIVE
  45. sdata.beta.sd <- sqrt(sdata.resid.var / sdata.Sxx)
  46. sdata.beta.t <- (sdata.est$coef[2] - 0) / sdata.beta.sd
  47. sdata.beta.tcrit <- qt(p = 1 - 0.05,df = sdata.N - 2)
  48. sdata.beta.t <= sdata.beta.tcrit
  49. # t > t.crit, H0 rejected so beta is significant
  50. ## F-Test
  51. # F-Tests are usually done, when the significance of more
  52. # than one variable has to be tested at a time. Generally,
  53. # it is possible to do multiple t-test to test for that,
  54. # however it can be shown that in rare occasions
  55. # (especially, when the t- and F-values or close to their
  56. # criticals) both test come to different results.
  57. # Usually it is more reliable to use the F-test for
  58. # multiple variables.
  59. # However the F-Test is unreliable for one-sided test, so
  60. # a t-test should be used in that case.
  61. # for the sake of demonstration new exogene variables
  62. # are generated first, which both influence the endogene.
  63. # Assumption: influence is in the form of
  64. # y = alpha + (beta1 * x1) + (beta2 * x2) + u
  65. x1 <- c(0.1,0.2,0.5,0.7,1.1)
  66. x2 <- c(3,11,15,22,26)
  67. sdata2.est <- ols(y ~ x1 + x2)
  68. # hypotheses testing for beta1s and beta2s significancy (F-Test)
  69. # Assumption: beta1 and beta2 are significant.
  70. # Test is for the opposite.
  71. # H0: "beta1 = beta2 = 0", sig.lvl is 5%
  72. # desk-packet
  73. sdata2.F <- f.test.coef(sdata2.est,
  74. nh = rbind(c(0,1,0),c(0,0,1)), # testing beta1,beta2
  75. q = c(0,0), # Value of H0 for beta1,beta2
  76. sig.level = 0.05)
  77. sdata2.F$results[5]
  78. # H0 rejected, so beta1 and beta2 are significant
  79. # manual
  80. # help-regression in which H0 is true
  81. sdata2.help.est <- ols(y ~ 1) # beta1 and beta2 have no influence
  82. sdata2.help.est.ssr <- sdata2.help.est$ssr # this is the same as Syy
  83. sdata2.est.ssr <- sdata2.est$ssr
  84. sdata2.Fval <- (((sdata2.help.est.ssr - sdata2.est.ssr) / 2) /
  85. (sdata2.est.ssr / (length(y) - 2 - 1)))
  86. sdata2.Fcrit <- qf(p = 1 - 0.05,df1 = 2,df2 = length(y) - 2 - 1)
  87. (-sdata2.Fcrit) < sdata2.Fval
  88. sdata2.Fval < sdata2.Fcrit
  89. # F is not located in the tollerance-intervall [-Fcrit ; Fcrit]
  90. # so H0 is being rejected