Friday, June 18, 2021

Some of the useful codes in R (Applied Econometrics with R)

  ■ Some codes in R


Note: Dep var = Dependent Variable.

           Ind var = Independent Variable.

           Var 1 = Variable 1

           Var 2 = Variable 2

a) Taking log values

lvar1 <- log(var1, base = 10)


b) Correlation Test

#install.packages (corTest)

library (corTest)

cor.test(var1, var2)


c) Unit Roots Test (without deseaonalisation)

# install.packages(fUnitRoots)

library(fUnitRoots)


d) Testing Unit Root at Level (ADF)

adfTest (lvar1, lags = 0, type = "c")


e) Taking 1st difference

d.var1 <-diff (var1)


f) Lag selection criteria

#install.packages (vars)

library (vars)

Say, 

kk <-cbind (lvar1, lvar2, ... lvarn)

VARselect (kk) $selection


g) Co Integration  (Johannessen)

Say,

JC1 <- ca.jo (kk, type = "trace", ecdet = "none", K =3)


h) Eigen Value Stat

JC1 <- ca.jo (kk, type = "eigen", ecdet = "none", k = 3)

i) VECM (in p - 1 lags)

Say, 

model1 = VECM (data.frame (vars), lag = n, r = m, estim = "ML")

where, r = no. of co integrating relationship (will discuss later)


j) Impulse Response Function

plot (irf (model1, n.ahead=10))

for 10 periods.


k) Multicollinearity in R

Packages are - car, faraway, VIF, fmsb, HH.

Say, 

#install.packages (faraway)

library (faraway)

model1 = lm (depvar~ind vars)

faraway : : vif (model1)

The result obtained should be less than 5.


l) Serial Correlation in R

Box.Test (var, lag = n, type = "Ljung-Box")

Box.Test (var, lag = n, type = "Box-Pierce")

Serial correlation if exist we can make the model into GARCH.


m) Heteroscedasticity in R

#install.packages (lmtest)

model1 = lm (var1~ var2)

bp test (model1) Breush Pagan test


n) Normality Test

Shapiro - Wilk test

Say a hypothetical test

c < - LC $ variable [1:n]

shapiro.test (c)

If the result p value is more than .05 then the data is normally distributed.


o) Ramsey Test

#install.packages (lmtest)

library (lmtest)

model1 = lm (var 1 ~ var 2)

resettest (model1)

Let's see the results, Ho: There's linearity in model.

If the p value is more than .05 then our null hypothesis is accepted then linearity in the model.


To be contd...

Aditya Pokhrel 

P.S Comments are lauded



No comments:

Post a Comment

Regression Discontinuity - How to determine whether it is Sharp or Fuzzy RD ? Simplest Look.           Regression discontinuity design is ga...