Skip to content

Instantly share code, notes, and snippets.

@thomasgilgenast
Last active July 1, 2017 01:09
Show Gist options
  • Save thomasgilgenast/17a53ac22a21fe181cce0096e683e06e to your computer and use it in GitHub Desktop.
Save thomasgilgenast/17a53ac22a21fe181cce0096e683e06e to your computer and use it in GitHub Desktop.
an example of fixed-effects model panel regression in R

Background

A just-for-fun reproduction of the regression analysis in Table 2 of the article "Consumer Spending in China: The Past and the Future" by Jun Nie and Andrew Palmer.

The analysis regresses the consumption share in GDP (NE.CON.PETC.ZS) in selected countries against

  • the young dependency ratio (SP.POP.DPND.YG),
  • the old dependency ratio (SP.POP.DPND.OL), and
  • the share of the population living in urban areas (SP.URB.TOTL.IN.ZS)

using a panel regression with country-fixed effects.

Usage and results

$ Rscript script.R
Oneway (individual) effect Within Model

Call:
plm(formula = NE.CON.PETC.ZS ~ SP.URB.TOTL.IN.ZS + SP.POP.DPND.OL +
    SP.POP.DPND.YG, data = data, model = "within", index = c("Country.Code",
    "Year"))

Unbalanced Panel: n=24, T=14-56, N=1091

Residuals :
    Min.  1st Qu.   Median  3rd Qu.     Max.
-75.3000  -3.1200   0.0521   3.4000  24.9000

Coefficients :
                   Estimate Std. Error t-value  Pr(>|t|)
SP.URB.TOTL.IN.ZS -0.280002   0.039322 -7.1208 1.972e-12 ***
SP.POP.DPND.OL     0.314218   0.095963  3.2743  0.001093 **
SP.POP.DPND.YG     0.122299   0.025076  4.8771 1.241e-06 ***
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

Total Sum of Squares:    61995
Residual Sum of Squares: 44303
R-Squared:      0.28538
Adj. R-Squared: 0.26792
F-statistic: 141.633 on 3 and 1064 DF, p-value: < 2.22e-16

These results are within 3% or so of those in Table 2 (except for the estimated coefficient of SP.URB.TOTL.IN.ZS).

# reproducing Table 2 of "Consumer Spending in China: The Past and the Future" by Jun Nie and Andrew Palmer
# https://www.kansascityfed.org/~/media/files/publicat/econrev/econrevarchive/2016/3q16niepalmer.pdf
library(reshape2)
library(plm)
# define countries (from Table 1) and indicators of interest
countries <- c(
"Afghanistan",
"Bangladesh",
"Brazil",
"Cambodia",
"China",
"Egypt, Arab Rep.",
"India",
"Indonesia",
"Iran, Islamic Rep.",
"Japan",
"Korea, Rep.",
"Lao PDR",
"Malaysia",
"Mexico",
"Mongolia",
"Nepal",
"Nigeria",
"Pakistan",
"Philippines",
"Russian Federation",
"Sri Lanka",
"Thailand",
"Turkey",
"Vietnam"
)
indicators <- c(
"Urban population (% of total)", # SP.URB.TOTL.IN.ZS
"Age dependency ratio, old (% of working-age population)", # SP.POP.DPND.OL
"Age dependency ratio, young (% of working-age population)", # SP.POP.DPND.YG
"Household final consumption expenditure, etc. (% of GDP)" # NE.CON.PETC.ZS
)
# get data from http://data.worldbank.org/data-catalog/world-development-indicators
temp <- tempfile()
download.file("http://databank.worldbank.org/data/download/WDI_csv.zip", temp)
data <- read.csv(unz(temp, "WDIData.csv"), encoding="UTF-8")
unlink(temp)
# reshape data
colnames(data)[1] <- "Country.Name" # remove strange characters in first column name
data <- data[data$Indicator.Name %in% indicators,] # drop unused indicators
data <- data[data$Country.Name %in% countries,] # drop unused countries
data <- data[, !names(data) %in% c("X", "Country.Name", "Indicator.Name")] # drop unused columns
data <- melt(data) # melt to long-form
colnames(data)[3] <- "Year" # rename "variable" column from melt to "Year"
data$Year <- substr(data$Year, 2, 5) # strip the leading X from Year column
data <- dcast(data, Country.Code + Year ~ Indicator.Code) # rotate Indicator.Code out as new columns
# clean data
data <- data[complete.cases(data),] # drop NA's
data <- data[data$Year != "2015",] # drop 2015
data <- data[!(data$Country.Code == "VNM" & data$Year == "1989"),] # drop Vietnam 1989
data <- data[!(data$Country.Code == "LKA" & data$Year %in% sprintf("%s", c(seq(1960, 1964), seq(2011, 2014)))),] # drop Sri Lanka 1960-1964 and 2011-2014
# panel regression under fixed-effects model
fixed <- plm(
NE.CON.PETC.ZS ~ SP.URB.TOTL.IN.ZS + SP.POP.DPND.OL + SP.POP.DPND.YG,
data=data, index=c("Country.Code", "Year"), model="within")
summary(fixed)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment