Panel data models are used when the data is varying across time and cross-sections. In such casing using OLS will have to assume any one of the dimension as constant and other as random, but in reality we cannot assume the country differences to be constant or random. Hence this leads to the concept of unobserved heterogeneity. This tutorial provides illustration to Fixed and Random Effect models which help to incorporate the country differences in the intercept.
Loading Libraries
library(plm) # For panel data models
library(tidyverse) # For data management
library(readr) # For reading csv file
library(gplots) # For paneldata plots
library(foreign)
Reading Data
df <- read_csv(“C:/Users/Noman Arshed/OneDrive/Research projects/Submitted/competitiveness and antiglobalization/antiglobalization.csv”)
Renaming variables
names(df)[names(df) == “year.x”] <- “year”
Transforming variables
df <- df %>% mutate (KOFE = log(KOFEcGI), KOFS = log(KOFSoGI), KOFP = log(KOFPoGI))
df <- df %>% mutate (KOFE2 = log(KOFEcGI) * log(KOFEcGI), KOFS2 = log(KOFSoGI) * log(KOFSoGI), KOFP2 = log(KOFPoGI)*log(KOFPoGI))
Declaring panel data
df.pd <- pdata.frame(df, index = c(“Country”,”year”), drop.index = TRUE)
Unobserved heterogeneity
plotmeans(GCI ~ country, main=”Heterogeineity across countries”, data= df)
plotmeans(GCI ~ year, main=”Heterogeineity across time”, data= df)
Scatter and Fit graphs
df %>% filter(level != “NA”, level != “Not classified”) %>% ggplot(mapping = aes(x= KOFEcGI , y= GCI, color = level))+ geom_point() + theme_bw() + theme(legend.position=”bottom”) + labs(title = “Competitiveness – Economic Globalization Association”, subtitle = “Different Income Levels”)
df %>% filter(level != “NA”, level != “Not classified”) %>% ggplot(mapping = aes(x= KOFSoGI , y= GCI, color = level))+ geom_point() + theme_bw() + theme(legend.position=”bottom”)+ labs(title = “Competitiveness – Social Globalization Association”, subtitle = “Different Income Levels”)
df %>% filter(level != “NA”, level != “Not classified”) %>% ggplot(mapping = aes(x= KOFPoGI , y= GCI, color = level))+ geom_point() + theme_bw() +theme(legend.position=”bottom”) + labs(title = “Competitiveness – Political Globalization Association”, subtitle = “Different Income Levels”)
Descriptive Stats
summary(df)
means <- df %>% group_by(Country) %>% filter (!is.na(KOFEcGI),!is.na(KOFSoGI), !is.na(KOFPoGI)) %>% summarize(meg = mean(KOFEcGI), msg = mean(KOFSoGI), mpg = mean(KOFPoGI))
OLS model
ols <- lm(GCI ~log(KOFEcGI) + KOFE2 + log(KOFSoGI) + KOFS2 + log(KOFPoGI) + KOFP2, data = df)
summary(ols)
FE Model
fe <- plm(GCI ~ log(KOFEcGI) + KOFE2 + log(KOFSoGI) + KOFS2 + log(KOFPoGI) + KOFP2, data = df, index = c(“Country”,”year”), model = “within”)
summary(fe)
Statistics from FE model
fixef(fe)
pFtest(fe, ols)
plmtest(fe, c(“time”), type = (“bp”))
pcdtest(fe, test = (“lm”))
pcdtest(fe, test = (“cd”))
pbgtest(fe)
Re model
re <- plm(GCI ~ log(KOFEcGI) + KOFE2 + log(KOFSoGI) + KOFS2 + log(KOFPoGI) + KOFP2, data = df, index = c(“Country”,”year”), model = “random”)
summary(re)
random <- plm(GCI ~ log(KOFEcGI) + KOFE2 + log(KOFSoGI) + KOFS2 + log(KOFPoGI) + KOFP2, data = df.pd, model = “random”)
FE RE Selection
phtest (fe, re)