# set proper working directory

data <- read.table("shk_data.txt", col.names=c("GDP85","lnGDP85","s_ik","s_ih","ngdelta","lns_ikonngdelta","lns_ihonngdelta","taylor1","taylor2","taylor3"))

# install four packages "car", "sandwich", "msm", and "lmtest"

install.packages("car")
#select a CRAN mirror
install.packages("sandwich")
#select a CRAN mirror
install.packages("msm")
#select a CRAN mirror
install.packages("lmtest")
#select a CRAN mirror

require(car)
require(sandwich)
require(msm)
require(lmtest)

####################################################################
## Physical Capital Only
####################################################################

# CD Log

cdres <- lm(lnGDP85~lns_ikonngdelta,data=data)
theta <- cdres$coef[2]
alpha <- theta/(1+theta)

estmean <- coef(cdres)
estvar <- sandwich(cdres)
deltamethod(~x2/(1+x2), estmean, estvar)

# RESET test

yhat2 <- predict(cdres, data=data)^2
test <- lm(lnGDP85~lns_ikonngdelta+yhat2, data=data)
coef <- coef(test)
stderr <- sqrt(diag(sandwich(test)))
t.stat <- coef/stderr
p.value <- 2*pnorm( -abs( t.stat))

# Test for heteroskedasticity

resid <- as.vector(residuals(cdres))
residsq <- resid^2
pred <- predict(cdres)
test <- lm(residsq~pred+I(pred^2))
linear.hypothesis(test, c("pred =0", "I(pred^2)=0"), "Chisq")

# CD Poisson

cd_poisson_1<-glm(GDP85~1+lns_ikonngdelta, family=poisson, data=data)
theta <- cd_poisson_1$coef[2]
alpha <- theta/(1+theta)

estmean <- coef(cd_poisson_1)
estvar <- sandwich(cd_poisson_1)
deltamethod(~x2/(1+x2), estmean, estvar)

# RESET test

ghat2 <- predict(cd_poisson_1, data=data)^2
test <- glm(GDP85~lns_ikonngdelta+ghat2, family=poisson, data=data)
coef <- coef(test)
stderr <- sqrt(diag(sandwich(test)))
t.stat <- coef/stderr
p.value <- 2*pnorm( -abs( t.stat))
cbind(coef, stderr, t.stat, p.value)

# Test for heteroskedasticity

pred <- exp(predict(cd_poisson_1))
resid <- data$GDP85-pred
residsq <- resid^2
test <- lm(residsq~pred+I(pred^2))
linear.hypothesis(test, c("pred =0", "I(pred^2)=0"), test="Chisq")

test <- nls(residsq~a*x1^theta, start=list(a=10000,theta=5))
df <- 2
sigmahat <- 21280000
SSR <- (sigmahat^2)*96
SST <- sum((residsq-mean(residsq))^2)
R2 <- 1-SSR/SST
LM <- length(residsq)*R2
pchisq(q=LM, df=df, ncp=0, lower.tail = F)

# Replicate CD Poisson using PPML

fn <- function(p)
             sum(   exp(p[1]+p[2]*data$lns_ikonngdelta)  - data$GDP85*(p[1]+p[2]*data$lns_ikonngdelta)     )

cd_poisson_1 <- nlm(fn, p = c(7,2), hessian = TRUE)

theta0 <- cd_poisson_1$estimate[1]
theta <- cd_poisson_1$estimate[2]
alpha <- theta/(1+theta)

B <- cd_poisson_1$hessian

sqrt(diag(solve(B)))

psi <- cbind(exp(theta0+theta*data$lns_ikonngdelta)-data$GDP85, (exp(theta0+theta*data$lns_ikonngdelta)-data$GDP85)*data$lns_ikonngdelta)

M <- crossprod(psi)

S <- solve(B)%*%M%*%solve(B)

estmean <- cd_poisson_1$estimate
estvar <- S
deltamethod(~x2/(1+x2), estmean, estvar)

# Alternatively, one can calculate psi using numericDeriv

fn <- function(p)
             exp(p[1]+p[2]*data$lns_ikonngdelta)  - data$GDP85*(p[1]+p[2]*data$lns_ikonngdelta)

myenv <- new.env()
assign("theta0", theta0, envir = myenv)
assign("theta", theta, envir = myenv)

p <- c(theta0,theta)

expr <- quote(fn(c(theta0, theta)))

psi <- attr(numericDeriv(expr, c("theta0", "theta"), myenv),"gradient")

# CD NLS in level

x1 <- data$s_ik/data$ngdelta
x2 <- data$s_ih/data$ngdelta
cd_nls_1 <- nls(GDP85~a*(x1^theta), start=list(a=1000, theta=0.4),data=data)
theta <- coef(cd_nls_1)[2]
alpha <- theta/(1+theta)

estmean <- coef(cd_nls_1)
estvar <- sandwich(cd_nls_1)
deltamethod(~x2/(1+x2), estmean, estvar)

# RESET test

fhat2 <- predict(cd_nls_1, data=data)^2
test <- nls(GDP85~a*(x1^theta)+gamma*fhat2, start=list(a=1000, theta=0.4, gamma=0), data=data)
coef <- coef(test)
stderr <- sqrt(diag(sandwich(test)))
t.stat <- coef/stderr
p.value <- 2*pnorm( -abs( t.stat))
cbind(coef, stderr, t.stat, p.value)

# Test for heterskedasticity

resid <- as.vector(residuals(cd_nls_1))
residsq <- resid^2
pred <- predict(cd_nls_1)
test <- lm(residsq~pred+I(pred^2))
linear.hypothesis(test, c("pred =0", "I(pred^2)=0"), test="Chisq")

# CES Log

cesres <- lm(lnGDP85~lns_ikonngdelta+taylor1,data=data)
coef <- cesres$coef
theta <- coef[2]
gamma <- coef[3]
alpha <- theta/(1+theta)
sigma <- 1/(1-(2*gamma*(1-alpha)^2)/(alpha))

estmean <- coef(cesres)
estvar <- sandwich(cesres)
deltamethod(~x2/(1+x2), estmean, estvar)
deltamethod(~1/(1-(2*x3*(1-(x2/(1+x2)))^2)/(x2/(1+x2))), estmean, estvar)

# RESET test

yhat2 <- predict(cesres, data=data)^2
test <- lm(lnGDP85~lns_ikonngdelta+taylor1+yhat2, data=data)
coef <- coef(test)
stderr <- sqrt(diag(sandwich(test)))
t.stat <- coef/stderr
p.value <- 2*pnorm( -abs( t.stat))
cbind(coef, stderr, t.stat, p.value)

# Test for heteroskedasticity

resid <- as.vector(residuals(cesres))
residsq <- resid^2
pred <- predict(cesres)
test <- lm(residsq~pred+I(pred^2))
linear.hypothesis(test, c("pred =0", "I(pred^2)=0"), test="Chisq")

# CES NLS in level

x1 <- data$s_ik/data$ngdelta
x2 <- data$s_ih/data$ngdelta
ces_nls_1 <- nls(GDP85~a*(1+theta-theta*x1^rho)^(-1/rho), start=list(a=500,theta=2, rho=-0.2),data=data)
coef <- coef(ces_nls_1)
theta <- coef[2]
rho <- coef[3]
alpha <- theta/(1+theta)
sigma <- 1/(1-rho)

estmean <- coef(ces_nls_1)
estvar <- sandwich(ces_nls_1)
deltamethod(~x2/(1+x2), estmean, estvar)
deltamethod(~1/(1-x3), estmean, estvar)

# RESET test

fhat2 <- predict(ces_nls_1, data=data)^2
test <- nls(GDP85~a*(1+theta-theta*x1^rho)^(-1/rho)+gamma*fhat2, start=list(a=646, theta=3, rho=-0.2, gamma=-0.1), data=data, algorithm="port", nls.control(maxiter = 500, tol = 1e-05, minFactor=1/2048))
coef <- coef(test)
stderr <- sqrt(diag(sandwich(test)))
t.stat <- coef/stderr
p.value <- 2*pnorm( -abs( t.stat))
cbind(coef, stderr, t.stat, p.value)

# Test for heteroskedasticity

resid <- as.vector(residuals(ces_nls_1))
residsq <- resid^2
pred <- predict(ces_nls_1)
test <- lm(residsq~pred+I(pred^2))
linear.hypothesis(test, c("pred =0", "I(pred^2)=0"), test="Chisq")

# CES PPML in level

fn <- function(p)
             sum(   exp(log(p[1])+(-1/p[3])*log(1+p[2]-p[2]*x1^p[3]))  - data$GDP85*(log(p[1])+(-1/p[3])*log(1+p[2]-p[2]*x1^p[3]))     )

ces_poisson_2 <- nlm(fn, p = c(500,2,-0.2), hessian = TRUE)

theta0 <- ces_poisson_2$estimate[1]
theta <- ces_poisson_2$estimate[2]
rho <- ces_poisson_2$estimate[3]

alpha <- theta/(1+theta)
sigma <- 1/(1-rho)

B <- ces_poisson_2$hessian

fn <- function(p)
             exp(log(p[1])+(-1/p[3])*log(1+p[2]-p[2]*x1^p[3]))  - data$GDP85*(log(p[1])+(-1/p[3])*log(1+p[2]-p[2]*x1^p[3]))

myenv <- new.env()
assign("theta0", theta0, envir = myenv)
assign("theta", theta, envir = myenv)
assign("rho", rho, envir = myenv)

p <- c(theta0,theta,rho)

expr <- quote(fn(c(theta0, theta,rho)))

psi <- attr(numericDeriv(expr, c("theta0", "theta", "rho"), myenv),"gradient")

M <- crossprod(psi)

S <- solve(B)%*%M%*%solve(B)

estmean <- ces_poisson_2$estimate
estvar <- S
deltamethod(~x2/(1+x2), estmean, estvar)
deltamethod(~1/(1-x3), estmean, estvar)

# RESET test

ghat <- log(theta0)+(-1/rho)*log(1+theta-theta*x1^rho)
ghat2 <- ghat^2

fn <- function(p)
             sum(   exp(log(p[1])+(-1/p[3])*log(1+p[2]-p[2]*x1^p[3])+p[4]*ghat2)  - data$GDP85*(log(p[1])+(-1/p[3])*log(1+p[2]-p[2]*x1^p[3])+p[4]*ghat2)     )

ces_poisson_5 <- nlm(fn, p = c(1100,1.5,-0.01,0), hessian = TRUE)

theta0 <- ces_poisson_5$estimate[1]
theta <- ces_poisson_5$estimate[2]
rho <- ces_poisson_5$estimate[3]
gamma <- ces_poisson_5$estimate[4]

B <- ces_poisson_5$hessian

fn <- function(p)
             exp(log(p[1])+(-1/p[3])*log(1+p[2]-p[2]*x1^p[3])+p[4]*ghat2)  - data$GDP85*(log(p[1])+(-1/p[3])*log(1+p[2]-p[2]*x1^p[3])+p[4]*ghat2)

myenv <- new.env()
assign("theta0", theta0, envir = myenv)
assign("theta", theta, envir = myenv)
assign("rho", rho, envir = myenv)
assign("gamma", gamma, envir = myenv)

p <- c(theta0,theta,rho,gamma)

expr <- quote(fn(c(theta0, theta,rho,gamma)))

psi <- attr(numericDeriv(expr, c("theta0", "theta", "rho", "gamma"), myenv),"gradient")

M <- crossprod(psi)

S <- solve(B)%*%M%*%solve(B)

stderr <- sqrt(diag(S))
t.stat <- ces_poisson_5$estimate/stderr
p.value <- 2*pnorm( -abs( t.stat))
cbind(coef=ces_poisson_5$estimate, stderr, t.stat, p.value)

# Test for heteroskedasticity

ghat <- log(theta0)+(-1/rho)*log(1+theta-theta*x1^rho)
pred <- exp(ghat)
resid <- data$GDP85-pred
residsq <- resid^2
test <- lm(residsq~pred+I(pred^2))
linear.hypothesis(test, c("pred =0", "I(pred^2)=0"), test="Chisq")

# CES Approximation in level (NLS)

x1 <- data$s_ik/data$ngdelta
ces_appr_1 <- nls(GDP85~0.5*a*(x1^theta)*(2+theta*(1+theta)*rho*log(x1)*log(x1)), start=list(a=500, theta=2, rho=-0.2), data=data)
coef <- coef(ces_appr_1)
theta <- coef[2]
rho <- coef[3]
alpha <- theta/(1+theta)
sigma <- 1/(1-rho)

estmean <- coef(ces_appr_1)
estvar <- sandwich(ces_appr_1)
deltamethod(~x2/(1+x2), estmean, estvar)
deltamethod(~1/(1-x3), estmean, estvar)

# RESET test

fhat2 <- predict(ces_appr_1, data=data)^2
test <- nls(GDP85~0.5*a*(x1^theta)*(2+theta*(1+theta)*rho*log(x1)*log(x1))+gamma*fhat2, start=list(a=500, theta=2, rho=-0.2, gamma=0), data=data)
coef <- coef(test)
stderr <- sqrt(diag(sandwich(test)))
t.stat <- coef/stderr
p.value <- 2*pnorm( -abs( t.stat))
cbind(coef, stderr, t.stat, p.value)

# Test for heteroskedasticity

resid <- as.vector(residuals(ces_appr_1))
residsq <- resid^2
pred <- predict(ces_appr_1)
test <- lm(residsq~pred+I(pred^2))
linear.hypothesis(test, c("pred =0", "I(pred^2)=0"), test="Chisq")

# CES Approximation in level (PPML)

fn <- function(p)
             sum(   (p[1]*x1^p[2]+0.5*p[1]*p[2]*p[3]*(x1^p[2])*(1+p[2])*log(x1)*log(x1))  - data$GDP85*log(p[1]*x1^p[2]+0.5*p[1]*p[2]*p[3]*(x1^p[2])*(1+p[2])*log(x1)*log(x1))     )

ces_poisson_3 <- nlm(fn, p = c(700,2,0), hessian = TRUE)

theta0 <- ces_poisson_3$estimate[1]
theta <- ces_poisson_3$estimate[2]
rho <- ces_poisson_3$estimate[3]

alpha <- theta/(1+theta)
sigma <- 1/(1-rho)

B <- ces_poisson_3$hessian

fn <- function(p)
             (p[1]*x1^p[2]+0.5*p[1]*p[2]*p[3]*(x1^p[2])*(1+p[2])*log(x1)*log(x1))  - data$GDP85*log(p[1]*x1^p[2]+0.5*p[1]*p[2]*p[3]*(x1^p[2])*(1+p[2])*log(x1)*log(x1))

myenv <- new.env()
assign("theta0", theta0, envir = myenv)
assign("theta", theta, envir = myenv)
assign("rho", rho, envir = myenv)

p <- c(theta0,theta,rho)

expr <- quote(fn(c(theta0, theta,rho)))

psi <- attr(numericDeriv(expr, c("theta0", "theta", "rho"), myenv),"gradient")

M <- crossprod(psi)

S <- solve(B)%*%M%*%solve(B)

estmean <- ces_poisson_3$estimate
estvar <- S
deltamethod(~x2/(1+x2), estmean, estvar)
deltamethod(~1/(1-x3), estmean, estvar)

# RESET test

ghat <- log(theta0*x1^theta+0.5*theta0*theta*rho*(x1^theta)*(1+theta)*log(x1)*log(x1))
ghat2 <- ghat^2

fn <- function(p)
             sum(   exp(log(p[1]*x1^p[2]+0.5*p[1]*p[2]*p[3]*(x1^p[2])*(1+p[2])*log(x1)*log(x1))+p[4]*ghat2)  - data$GDP85*(log(p[1]*x1^p[2]+0.5*p[1]*p[2]*p[3]*(x1^p[2])*(1+p[2])*log(x1)*log(x1))+p[4]*ghat2)     )

ces_poisson_4 <- nlm(fn, p = c(700,2,0,0), hessian = TRUE)

theta0 <- ces_poisson_4$estimate[1]
theta <- ces_poisson_4$estimate[2]
rho <- ces_poisson_4$estimate[3]
gamma <- ces_poisson_4$estimate[4]

B <- ces_poisson_4$hessian

fn <- function(p)
             exp(log(p[1]*x1^p[2]+0.5*p[1]*p[2]*p[3]*(x1^p[2])*(1+p[2])*log(x1)*log(x1))+p[4]*ghat2)  - data$GDP85*(log(p[1]*x1^p[2]+0.5*p[1]*p[2]*p[3]*(x1^p[2])*(1+p[2])*log(x1)*log(x1))+p[4]*ghat2)

myenv <- new.env()
assign("theta0", theta0, envir = myenv)
assign("theta", theta, envir = myenv)
assign("rho", rho, envir = myenv)
assign("gamma", gamma, envir = myenv)

p <- c(theta0,theta,rho,gamma)

expr <- quote(fn(c(theta0, theta,rho,gamma)))

psi <- attr(numericDeriv(expr, c("theta0", "theta", "rho", "gamma"), myenv),"gradient")

M <- crossprod(psi)

S <- solve(B)%*%M%*%solve(B)

stderr <- sqrt(diag(S))
t.stat <- ces_poisson_4$estimate/stderr
p.value <- 2*pnorm( -abs( t.stat))
cbind(coef=ces_poisson_4$estimate, stderr, t.stat, p.value)

# Test for heteroskedasticity

ghat <- log(theta0*x1^theta+0.5*theta0*theta*rho*(x1^theta)*(1+theta)*log(x1)*log(x1))
pred <- exp(ghat)
resid <- data$GDP85-pred
residsq <- resid^2
test <- lm(residsq~pred+I(pred^2))
linear.hypothesis(test, c("pred =0", "I(pred^2)=0"), test="Chisq")

####################################################################
## Both Physical and Human Capital
####################################################################

# CD Log

cdres_ext <- lm(lnGDP85~lns_ikonngdelta+lns_ihonngdelta,data=data)
theta1 <- cdres_ext$coef[2]
theta2 <- cdres_ext$coef[3]
alpha <- theta1/(1+theta1+theta2)
beta <- theta2/(1+theta1+theta2)

estmean <- coef(cdres_ext)
estvar <- sandwich(cdres_ext)
deltamethod(~x2/(1+x2+x3), estmean, estvar)
deltamethod(~x3/(1+x2+x3), estmean, estvar)

# RESET test

yhat2 <- predict(cdres_ext, data=data)^2
test <- lm(lnGDP85~lns_ikonngdelta+lns_ihonngdelta+yhat2, data=data)
coef <- coef(test)
stderr <- sqrt(diag(sandwich(test)))
t.stat <- coef/stderr
p.value <- 2*pnorm( -abs( t.stat))
cbind(coef, stderr, t.stat, p.value)

# Test for heteroskedasticity

resid <- as.vector(residuals(cdres_ext))
residsq <- resid^2
pred <- predict(cdres_ext)
test <- lm(residsq~pred+I(pred^2))
linear.hypothesis(test, c("pred =0", "I(pred^2)=0"), test="Chisq")

# CD Poisson

cd_poisson_2<-glm(GDP85~1+lns_ikonngdelta+lns_ihonngdelta, family=poisson, data=data)
theta1 <- cd_poisson_2$coef[2]
theta2 <- cd_poisson_2$coef[3]
alpha <- theta1/(1+theta1+theta2)
beta <- theta2/(1+theta1+theta2)

estmean <- coef(cd_poisson_2)
estvar <- sandwich(cd_poisson_2)
deltamethod(~x2/(1+x2+x3), estmean, estvar)
deltamethod(~x3/(1+x2+x3), estmean, estvar)

# RESET test

ghat2 <- predict(cd_poisson_2, data=data)^2
test <- glm(GDP85~lns_ikonngdelta+lns_ihonngdelta+ghat2, family=poisson, data=data)
coef <- coef(test)
stderr <- sqrt(diag(sandwich(test)))
t.stat <- coef/stderr
p.value <- 2*pnorm( -abs( t.stat))
cbind(coef, stderr, t.stat, p.value)

# Test for heteroskedasticity

pred <- exp(predict(cd_poisson_2))
resid <- data$GDP85-pred
residsq <- resid^2
test <- lm(residsq~pred+I(pred^2))
linear.hypothesis(test, c("pred =0", "I(pred^2)=0"), test="Chisq")

test <- nls(residsq~a*(x1^theta1)*(x2^theta2), start=list(a=10000,theta1=5,theta2=5))
df <- 3
sigmahat <- 12540000
SSR <- (sigmahat^2)*95
SST <- sum((residsq-mean(residsq))^2)
R2 <- 1-SSR/SST
LM <- length(residsq)*R2
pchisq(q=LM, df=df, ncp=0, lower.tail = F)

# CD NLS in level

x1 <- data$s_ik/data$ngdelta
x2 <- data$s_ih/data$ngdelta
cd_nls_2 <- nls(GDP85~a*(x1^theta1)*(x2^theta2), start=list(a=1000, theta1=0.4, theta2=0.4),data=data)
coef <- coef(cd_nls_2)
theta1 <- coef[2]
theta2 <- coef[3]
alpha <- theta1/(1+theta1+theta2)
beta <- theta2/(1+theta1+theta2)

estmean <- coef(cd_nls_2)
estvar <- sandwich(cd_nls_2)
deltamethod(~x2/(1+x2+x3), estmean, estvar)
deltamethod(~x3/(1+x2+x3), estmean, estvar)

# RESET test

fhat2 <- predict(cd_nls_2, data=data)^2
test <- nls(GDP85~a*(x1^theta1)*(x2^theta2)+gamma*fhat2, start=list(a=1000, theta1=0.4, theta2=0.4, gamma=0), data=data)
coef <- coef(test)
stderr <- sqrt(diag(sandwich(test)))
t.stat <- coef/stderr
p.value <- 2*pnorm( -abs( t.stat))
cbind(coef, stderr, t.stat, p.value)

# Test for heteroskedasticity

resid <- as.vector(residuals(cd_nls_2))
residsq <- resid^2
pred <- predict(cd_nls_2)
test <- lm(residsq~pred+I(pred^2))
linear.hypothesis(test, c("pred =0", "I(pred^2)=0"), test="Chisq")

# CES Log

cesres_ext <- nls(lnGDP85~ intercept + (alpha/(1-alpha-beta)) * lns_ikonngdelta + (beta/(1-alpha-beta)) * lns_ihonngdelta + 0.5 * ((sigma-1)/sigma) * (1/((1-alpha-beta)*(1-alpha-beta))) * (alpha * taylor1 + beta * taylor2 - alpha*beta*taylor3) ,start = list(intercept=8, alpha=0.2, beta=0.4, sigma=1.2),data=data)
alpha <- coef(cesres_ext)[2]
beta <- coef(cesres_ext)[3]
sigma <- coef(cesres_ext)[4]

estmean <- coef(cesres_ext)
estvar <- sandwich(cesres_ext)
sqrt(diag(estvar))

# RESET test

fhat2 <- predict(cesres_ext, data=data)^2
test <- nls(lnGDP85~intercept + (alpha/(1-alpha-beta)) * lns_ikonngdelta + (beta/(1-alpha-beta)) * lns_ihonngdelta + 0.5 * ((sigma-1)/sigma) * (1/((1-alpha-beta)*(1-alpha-beta))) * (alpha * taylor1 + beta * taylor2 - alpha*beta*taylor3) + gamma*fhat2, start=list(intercept=8, alpha=0.2, beta=0.4, sigma=1.2, gamma=0), data=data)
coef <- coef(test)
stderr <- sqrt(diag(sandwich(test)))
t.stat <- coef/stderr
p.value <- 2*pnorm( -abs( t.stat))
cbind(coef, stderr, t.stat, p.value)

# Test for heteroskedasticity

resid <- as.vector(residuals(cesres_ext))
residsq <- resid^2
pred <- predict(cesres_ext)
test <- lm(residsq~pred+I(pred^2))
linear.hypothesis(test, c("pred =0", "I(pred^2)=0"), test="Chisq")

# CES NLS in level

x1 <- data$s_ik/data$ngdelta
x2 <- data$s_ih/data$ngdelta
ces_nls_2 <- nls(GDP85~a*(1+theta1+theta2-theta1*x1^rho-theta2*x2^rho)^(-1/rho), start=list(a=1000,theta1=1.2, theta2=0.8, rho=-0.2),data=data)
coef <- coef(ces_nls_2)
theta1 <- coef[2]
theta2 <- coef[3]
rho <- coef[4]
alpha <- theta1/(1+theta1+theta2)
beta <- theta2/(1+theta1+theta2)
sigma <- 1/(1-rho)

estmean <- coef(ces_nls_2)
estvar <- sandwich(ces_nls_2)
deltamethod(~x2/(1+x2+x3), estmean, estvar)
deltamethod(~x3/(1+x2+x3), estmean, estvar)
deltamethod(~1/(1-x4), estmean, estvar)

# RESET test

fhat2 <- predict(ces_nls_2, data=data)^2
test <-  nls(GDP85~a*(1+theta1+theta2-theta1*x1^rho-theta2*x2^rho)^(-1/rho)+gamma*fhat2, start=list(a=1476, theta1=2, theta2=0.7, rho=-0.2, gamma=0),nls.control(maxiter=1000, tol=1e-05), algorithm="port", trace=T, data=data)
coef <- coef(test)
stderr <- sqrt(diag(sandwich(test)))
t.stat <- coef/stderr
p.value <- 2*pnorm( -abs( t.stat))
cbind(coef, stderr, t.stat, p.value)

# Test for heteroskedasticity

resid <- as.vector(residuals(ces_nls_2))
residsq <- resid^2
pred <- predict(ces_nls_2)
test <- lm(residsq~pred+I(pred^2))
linear.hypothesis(test, c("pred =0", "I(pred^2)=0"), test="Chisq")

# CES PPML in level

fn <- function(p)
             sum(   exp(log(p[1])+(-1/p[4])*log(1+p[2]+p[3]-p[2]*x1^p[4]-p[3]*x2^p[4]))  - data$GDP85*(log(p[1])+(-1/p[4])*log(1+p[2]+p[3]-p[2]*x1^p[4]-p[3]*x2^p[4]))     )

ces_poisson_2 <- nlm(fn, p = c(1000,1.2,0.8,-0.2), hessian = TRUE)

theta0 <- ces_poisson_2$estimate[1]
theta1 <- ces_poisson_2$estimate[2]
theta2 <- ces_poisson_2$estimate[3]
rho <- ces_poisson_2$estimate[4]

alpha <- theta1/(1+theta1+theta2)
beta <- theta2/(1+theta1+theta2)
sigma <- 1/(1-rho)

B <- ces_poisson_2$hessian

fn <- function(p)
             exp(log(p[1])+(-1/p[4])*log(1+p[2]+p[3]-p[2]*x1^p[4]-p[3]*x2^p[4]))  - data$GDP85*(log(p[1])+(-1/p[4])*log(1+p[2]+p[3]-p[2]*x1^p[4]-p[3]*x2^p[4]))

myenv <- new.env()
assign("theta0", theta0, envir = myenv)
assign("theta1", theta1, envir = myenv)
assign("theta2", theta2, envir = myenv)
assign("rho", rho, envir = myenv)

p <- c(theta0,theta1,theta2,rho)

expr <- quote(fn(c(theta0, theta1, theta2, rho)))

psi <- attr(numericDeriv(expr, c("theta0", "theta1", "theta2", "rho"), myenv),"gradient")

M <- crossprod(psi)

S <- solve(B)%*%M%*%solve(B)

estmean <- ces_poisson_2$estimate
estvar <- S
deltamethod(~x2/(1+x2+x3), estmean, estvar)
deltamethod(~x3/(1+x2+x3), estmean, estvar)
deltamethod(~1/(1-x4), estmean, estvar)

# RESET test

ghat <- log(theta0)+(-1/rho)*log(1+theta1+theta2-theta1*x1^rho-theta2*x2^rho)
ghat2 <- ghat^2

fn <- function(p)
             sum(   exp(log(p[1])+(-1/p[4])*log(1+p[2]+p[3]-p[2]*x1^p[4]-p[3]*x2^p[4])+p[5]*ghat2)  - data$GDP85*(log(p[1])+(-1/p[4])*log(1+p[2]+p[3]-p[2]*x1^p[4]-p[3]*x2^p[4])+p[5]*ghat2)     )

ces_poisson_6 <- nlm(fn, p = c(1000,1.2,0.8,-0.2,0), hessian = TRUE)

theta0 <- ces_poisson_6$estimate[1]
theta1 <- ces_poisson_6$estimate[2]
theta2 <- ces_poisson_6$estimate[3]
rho <- ces_poisson_6$estimate[4]
gamma <- ces_poisson_6$estimate[5]

B <- ces_poisson_6$hessian

fn <- function(p)
             exp(log(p[1])+(-1/p[4])*log(1+p[2]+p[3]-p[2]*x1^p[4]-p[3]*x2^p[4])+p[5]*ghat2)  - data$GDP85*(log(p[1])+(-1/p[4])*log(1+p[2]+p[3]-p[2]*x1^p[4]-p[3]*x2^p[4])+p[5]*ghat2)

myenv <- new.env()
assign("theta0", theta0, envir = myenv)
assign("theta1", theta, envir = myenv)
assign("theta2", theta, envir = myenv)
assign("rho", rho, envir = myenv)
assign("gamma", gamma, envir = myenv)

p <- c(theta0,theta1,theta2,rho,gamma)

expr <- quote(fn(c(theta0, theta1,theta2,rho,gamma)))

psi <- attr(numericDeriv(expr, c("theta0", "theta1", "theta2", "rho", "gamma"), myenv),"gradient")

M <- crossprod(psi)

S <- solve(B)%*%M%*%solve(B)

stderr <- sqrt(diag(S))
t.stat <- ces_poisson_6$estimate/stderr
p.value <- 2*pnorm( -abs( t.stat))
cbind(coef=ces_poisson_6$estimate, stderr, t.stat, p.value)

# Test for heteroskedasticity

ghat <- log(theta0)+(-1/rho)*log(1+theta1+theta2-theta1*x1^rho-theta2*x2^rho)
pred <- exp(ghat)
resid <- data$GDP85-pred
residsq <- resid^2
test <- lm(residsq~pred+I(pred^2))
linear.hypothesis(test, c("pred =0", "I(pred^2)=0"), test="Chisq")

# CES Approximation in level (NLS)

x1 <- data$s_ik/data$ngdelta
x2 <- data$s_ih/data$ngdelta
ces_appr_2 <- nls(GDP85~0.5*a*(x1^theta1)*(x2^theta2)*(2+theta1*(1+theta1)*rho*log(x1)*log(x1)+2*theta1*theta2*rho*log(x1)*log(x2)+theta2*(1+theta2)*rho*log(x2)*log(x2)), start=list(a=1000, theta1=1.2, theta2=0.8, rho=-0.2), data=data)
coef <- coef(ces_appr_2)
theta1 <- coef[2]
theta2 <- coef[3]
rho <- coef[4]
alpha <- theta1/(1+theta1+theta2)
beta <- theta2/(1+theta1+theta2)
sigma <- 1/(1-rho)

estmean <- coef(ces_appr_2)
estvar <- sandwich(ces_appr_2)
deltamethod(~x2/(1+x2+x3), estmean, estvar)
deltamethod(~x3/(1+x2+x3), estmean, estvar)
deltamethod(~1/(1-x4), estmean, estvar)

# RESET test

fhat2 <- predict(ces_appr_2, data=data)^2
test <- nls(GDP85~0.5*a*(x1^theta1)*(x2^theta2)*(2+theta1*(1+theta1)*rho*log(x1)*log(x1)+2*theta1*theta2*rho*log(x1)*log(x2)+theta2*(1+theta2)*rho*log(x2)*log(x2))+gamma*fhat2, start=list(a=1000, theta1=1.2, theta2=0.8, rho=-0.2, gamma=0), data=data)
coef <- coef(test)
stderr <- sqrt(diag(sandwich(test)))
t.stat <- coef/stderr
p.value <- 2*pnorm( -abs( t.stat))
cbind(coef, stderr, t.stat, p.value)

# Test for heteroskedasticity

resid <- as.vector(residuals(ces_appr_2))
residsq <- resid^2
pred <- predict(ces_appr_2)
test <- lm(residsq~pred+I(pred^2))
linear.hypothesis(test, c("pred =0", "I(pred^2)=0"), test="Chisq")

# CES Approximation in level (PPML)

fn <- function(p)
             sum(   exp(log(0.5)+log(p[1])+p[2]*log(x1)+p[3]*log(x2)+log(2+p[2]*(1+p[2])*p[4]*log(x1)*log(x1)+2*p[2]*p[3]*p[4]*log(x1)*log(x2)+p[3]*(1+p[3])*p[4]*log(x2)*log(x2))      )  - data$GDP85*(log(0.5)+log(p[1])+p[2]*log(x1)+p[3]*log(x2)+log(2+p[2]*(1+p[2])*p[4]*log(x1)*log(x1)+2*p[2]*p[3]*p[4]*log(x1)*log(x2)+p[3]*(1+p[3])*p[4]*log(x2)*log(x2)))     )

ces_poisson_3 <- nlm(fn, p = c(1700,1.5,1,0), hessian = TRUE)

theta0 <- ces_poisson_3$estimate[1]
theta1 <- ces_poisson_3$estimate[2]
theta2 <- ces_poisson_3$estimate[3]
rho <- ces_poisson_3$estimate[4]

alpha <- theta1/(1+theta1+theta2)
beta <- theta2/(1+theta1+theta2)
sigma <- 1/(1-rho)

B <- ces_poisson_3$hessian

fn <- function(p)
             exp(log(0.5)+log(p[1])+p[2]*log(x1)+p[3]*log(x2)+log(2+p[2]*(1+p[2])*p[4]*log(x1)*log(x1)+2*p[2]*p[3]*p[4]*log(x1)*log(x2)+p[3]*(1+p[3])*p[4]*log(x2)*log(x2))      )  - data$GDP85*(log(0.5)+log(p[1])+p[2]*log(x1)+p[3]*log(x2)+log(2+p[2]*(1+p[2])*p[4]*log(x1)*log(x1)+2*p[2]*p[3]*p[4]*log(x1)*log(x2)+p[3]*(1+p[3])*p[4]*log(x2)*log(x2)))

myenv <- new.env()
assign("theta0", theta0, envir = myenv)
assign("theta1", theta1, envir = myenv)
assign("theta2", theta2, envir = myenv)
assign("rho", rho, envir = myenv)

p <- c(theta0,theta1,theta2,rho)

expr <- quote(fn(c(theta0, theta1,theta2,rho)))

psi <- attr(numericDeriv(expr, c("theta0", "theta1", "theta2", "rho"), myenv),"gradient")

M <- crossprod(psi)

S <- solve(B)%*%M%*%solve(B)

estmean <- ces_poisson_3$estimate
estvar <- S
deltamethod(~x2/(1+x2+x3), estmean, estvar)
deltamethod(~x3/(1+x2+x3), estmean, estvar)
deltamethod(~1/(1-x4), estmean, estvar)

# RESET test

ghat <- log(0.5)+log(theta0)+theta1*log(x1)+theta2*log(x2)+log(2+theta1*(1+theta1)*rho*log(x1)*log(x1)+2*theta1*theta2*rho*log(x1)*log(x2)+theta2*(1+theta2)*rho*log(x2)*log(x2))
ghat2 <- ghat^2

fn <- function(p)
             sum(   exp(log(0.5)+log(p[1])+p[2]*log(x1)+p[3]*log(x2)+log(2+p[2]*(1+p[2])*p[4]*log(x1)*log(x1)+2*p[2]*p[3]*p[4]*log(x1)*log(x2)+p[3]*(1+p[3])*p[4]*log(x2)*log(x2))+p[5]*ghat2 )  - data$GDP85*(log(0.5)+log(p[1])+p[2]*log(x1)+p[3]*log(x2)+log(2+p[2]*(1+p[2])*p[4]*log(x1)*log(x1)+2*p[2]*p[3]*p[4]*log(x1)*log(x2)+p[3]*(1+p[3])*p[4]*log(x2)*log(x2))+p[5]*ghat2)     )

ces_poisson_7 <- nlm(fn, p = c(1700,1.5,1,0,0), hessian = TRUE)

theta0 <- ces_poisson_7$estimate[1]
theta1 <- ces_poisson_7$estimate[2]
theta2 <- ces_poisson_7$estimate[3]
rho <- ces_poisson_7$estimate[4]
gamma <- ces_poisson_7$estimate[5]

B <- ces_poisson_7$hessian

fn <- function(p)
               exp(log(0.5)+log(p[1])+p[2]*log(x1)+p[3]*log(x2)+log(2+p[2]*(1+p[2])*p[4]*log(x1)*log(x1)+2*p[2]*p[3]*p[4]*log(x1)*log(x2)+p[3]*(1+p[3])*p[4]*log(x2)*log(x2))+p[5]*ghat2 )  - data$GDP85*(log(0.5)+log(p[1])+p[2]*log(x1)+p[3]*log(x2)+log(2+p[2]*(1+p[2])*p[4]*log(x1)*log(x1)+2*p[2]*p[3]*p[4]*log(x1)*log(x2)+p[3]*(1+p[3])*p[4]*log(x2)*log(x2))+p[5]*ghat2)

myenv <- new.env()
assign("theta0", theta0, envir = myenv)
assign("theta1", theta1, envir = myenv)
assign("theta2", theta2, envir = myenv)
assign("rho", rho, envir = myenv)
assign("gamma", gamma, envir = myenv)

p <- c(theta0,theta1,theta2,rho,gamma)

expr <- quote(fn(c(theta0, theta1,theta2,rho,gamma)))

psi <- attr(numericDeriv(expr, c("theta0", "theta1", "theta2", "rho", "gamma"), myenv),"gradient")

M <- crossprod(psi)

S <- solve(B)%*%M%*%solve(B)

stderr <- sqrt(diag(S))
t.stat <- ces_poisson_7$estimate/stderr
p.value <- 2*pnorm( -abs( t.stat))
cbind(coef=ces_poisson_7$estimate, stderr, t.stat, p.value)

# Test for heteroskedasticity

ghat <- log(0.5)+log(theta0)+theta1*log(x1)+theta2*log(x2)+log(2+theta1*(1+theta1)*rho*log(x1)*log(x1)+2*theta1*theta2*rho*log(x1)*log(x2)+theta2*(1+theta2)*rho*log(x2)*log(x2))
pred <- exp(ghat)
resid <- data$GDP85-pred
residsq <- resid^2
test <- lm(residsq~pred+I(pred^2))
linear.hypothesis(test, c("pred =0", "I(pred^2)=0"), test="Chisq")

# Plots

# Basic

attach(data)

x1 <- data$s_ik/data$ngdelta
x2 <- data$s_ih/data$ngdelta

ces_nls_1 <- nls(GDP85~a*(1+theta-theta*x1^rho)^(-1/rho), start=list(a=2000,theta=1.4, rho=-0.2),data=data)
ces_poisson_1<-glm(GDP85~1+lns_ikonngdelta+taylor1, family=poisson, data=data)
cesres <- lm(lnGDP85~lns_ikonngdelta+taylor1,data=data)

alpha_log <- 0.4984
sigma_log <- 1.5425

alpha_poisson_1 <- 0.6376
sigma_poisson_1 <- 0.9623

alpha_poisson_2 <- 0.6172
sigma_poisson_2 <- 0.9813

alpha_nls <- 0.7486
sigma_nls <- 0.8354

alpha_appr <- 0.7009
sigma_appr <- 0.9499

k_log <- ((1-alpha_log)/((data$ngdelta/data$s_ik)^((sigma_log-1)/sigma_log)-alpha_log))^(sigma_log/(sigma_log-1))
shareK_log <- (alpha_log*k_log^((sigma_log-1)/sigma_log))/(alpha_log*k_log^((sigma_log-1)/sigma_log)+(1-alpha_log))

k_poisson_1 <- ((1-alpha_poisson_1)/((data$ngdelta/data$s_ik)^((sigma_poisson_1-1)/sigma_poisson_1)-alpha_poisson_1))^(sigma_poisson_1/(sigma_poisson_1-1))
shareK_poisson_1 <- (alpha_poisson_1*k_poisson_1^((sigma_poisson_1-1)/sigma_poisson_1))/(alpha_poisson_1*k_poisson_1^((sigma_poisson_1-1)/sigma_poisson_1)+(1-alpha_poisson_1))

k_poisson_2 <- ((1-alpha_poisson_2)/((data$ngdelta/data$s_ik)^((sigma_poisson_2-1)/sigma_poisson_2)-alpha_poisson_2))^(sigma_poisson_2/(sigma_poisson_2-1))
shareK_poisson_2 <- (alpha_poisson_2*k_poisson_2^((sigma_poisson_2-1)/sigma_poisson_2))/(alpha_poisson_2*k_poisson_2^((sigma_poisson_2-1)/sigma_poisson_2)+(1-alpha_poisson_2))

k_nls <- ((1-alpha_nls)/((data$ngdelta/data$s_ik)^((sigma_nls-1)/sigma_nls)-alpha_nls))^(sigma_nls/(sigma_nls-1))
shareK_nls <- (alpha_nls*k_nls^((sigma_nls-1)/sigma_nls))/(alpha_nls*k_nls^((sigma_nls-1)/sigma_nls)+(1-alpha_nls))

k_appr <- ((1-alpha_appr)/((data$ngdelta/data$s_ik)^((sigma_appr-1)/sigma_appr)-alpha_appr))^(sigma_appr/(sigma_appr-1))
shareK_appr <- (alpha_appr*k_appr^((sigma_appr-1)/sigma_appr))/(alpha_appr*k_appr^((sigma_appr-1)/sigma_appr)+(1-alpha_appr))

plot(density(shareK_log), xlim=c(0.35,1), ylim=c(0,20), main="Physical Capital Shares", xlab="", lwd=2)
lines(density(shareK_appr),lty=2, lwd=2)
lines(density(shareK_nls),lty=3, lwd=2)
legend(0.35,20, c("Log-linearized","Approximated NLLS","Level NLLS"), lty=c(1:3), lwd=rep(2,3))

plot(density(shareK_log), xlim=c(0.35,1), ylim=c(0,53), main="Physical Capital Shares", xlab="", lwd=2)
lines(density(shareK_appr),lty=2, lwd=2)
lines(density(shareK_poisson_1),lty=3, lwd=2)
lines(density(shareK_nls),lty=4, lwd=2)
lines(density(shareK_poisson_2),lty=5, lwd=2)
legend(0.67,50, c("OLS (Log)", "NLLS (Approximated)", "PPML (Approximated)", "NLLS", "PPML"), lty=c(1:5), lwd=rep(2,5))

plot(ecdf(shareK_log), verticals= T, do.points = F, main="ECDF of Physical Capital Share",xlab="",ylab="ECDF",lwd=2,col="red")
lines(ecdf(shareK_poisson_1),verticals= T, do.points = F, lty=2,lwd=2,col="green")
lines(ecdf(shareK_nls),verticals= T, do.points = F, lty=3,lwd=2,col="blue")
legend(0.8,0.4, c("Log-linearized", "Poisson", "NLS level"), lty=c(1:3),lwd=rep(2,3),col=c("red","green","blue"))

# Extended

attach(data)

x1 <- data$s_ik/data$ngdelta
x2 <- data$s_ih/data$ngdelta

ces_nls_2 <- nls(GDP85~a*(1+theta1+theta2-theta1*x1^rho-theta2*x2^rho)^(-1/rho), start=list(a=1000,theta1=1.2, theta2=0.8, rho=-0.2),data=data)
ces_poisson_2<-glm(GDP85~1+lns_ikonngdelta+lns_ihonngdelta+taylor1+taylor2+taylor3, family=poisson, data=data)
cesres_ext <- nls(lnGDP85~ intercept + (alpha/(1-alpha-beta)) * lns_ikonngdelta + (beta/(1-alpha-beta)) * lns_ihonngdelta + 0.5 * ((sigma-1)/sigma) * (1/((1-alpha-beta)*(1-alpha-beta))) * (alpha * taylor1 + beta * taylor2 - alpha*beta*taylor3) ,start = list(intercept=8, alpha=0.2, beta=0.4, sigma=1.2),data=data)

alpha_log <- 0.2395
beta_log <- 0.3582
sigma_log <- 1.1894
rho_log <- (sigma_log-1)/sigma_log

alpha_poisson_1 <- 0.2796
beta_poisson_1 <- 0.3140
sigma_poisson_1 <- 1.0590
rho_poisson_1 <- (sigma_poisson_1-1)/sigma_poisson_1

alpha_poisson_2 <- 0.2851
beta_poisson_2 <- 0.3105
sigma_poisson_2 <- 1.0369
rho_poisson_2 <- (sigma_poisson_2-1)/sigma_poisson_2

alpha_nls <- 0.3928
beta_nls <- 0.2851
sigma_nls <- 0.8381
rho_nls <- (sigma_nls-1)/sigma_nls

alpha_appr <- 0.4295
beta_appr <- 0.2773
sigma_appr <- 0.9219
rho_appr <- (sigma_appr-1)/sigma_appr

k_log <- ((1-alpha_log-beta_log)/(((ngdelta/s_ik)^rho_log)-(beta_log*((s_ih/s_ik)^rho_log))-(alpha_log)))^(1/rho_log)
h_log <- ((1-alpha_log-beta_log)/(((ngdelta/s_ih)^rho_log)-(alpha_log*((s_ik/s_ih)^rho_log))-(beta_log)))^(1/rho_log)
shareK_log <- (alpha_log*(k_log^rho_log))/((alpha_log*(k_log^rho_log))+(beta_log*(h_log^rho_log))+(1-alpha_log-beta_log))
shareH_log <- (beta_log*(h_log^rho_log))/((alpha_log*(k_log^rho_log))+(beta_log*(h_log^rho_log))+(1-alpha_log-beta_log))

k_poisson_1 <- ((1-alpha_poisson_1-beta_poisson_1)/(((ngdelta/s_ik)^rho_poisson_1)-(beta_poisson_1*((s_ih/s_ik)^rho_poisson_1))-(alpha_poisson_1)))^(1/rho_poisson_1)
h_poisson_1 <- ((1-alpha_poisson_1-beta_poisson_1)/(((ngdelta/s_ih)^rho_poisson_1)-(alpha_poisson_1*((s_ik/s_ih)^rho_poisson_1))-(beta_poisson_1)))^(1/rho_poisson_1)
shareK_poisson_1 <- (alpha_poisson_1*(k_poisson_1^rho_poisson_1))/((alpha_poisson_1*(k_poisson_1^rho_poisson_1))+(beta_poisson_1*(h_poisson_1^rho_poisson_1))+(1-alpha_poisson_1-beta_poisson_1))
shareH_poisson_1 <- (beta_poisson_1*(h_poisson_1^rho_poisson_1))/((alpha_poisson_1*(k_poisson_1^rho_poisson_1))+(beta_poisson_1*(h_poisson_1^rho_poisson_1))+(1-alpha_poisson_1-beta_poisson_1))

k_poisson_2 <- ((1-alpha_poisson_2-beta_poisson_2)/(((ngdelta/s_ik)^rho_poisson_2)-(beta_poisson_2*((s_ih/s_ik)^rho_poisson_2))-(alpha_poisson_2)))^(1/rho_poisson_2)
h_poisson_2 <- ((1-alpha_poisson_2-beta_poisson_2)/(((ngdelta/s_ih)^rho_poisson_2)-(alpha_poisson_2*((s_ik/s_ih)^rho_poisson_2))-(beta_poisson_2)))^(1/rho_poisson_2)
shareK_poisson_2 <- (alpha_poisson_2*(k_poisson_2^rho_poisson_2))/((alpha_poisson_2*(k_poisson_2^rho_poisson_2))+(beta_poisson_2*(h_poisson_2^rho_poisson_2))+(1-alpha_poisson_2-beta_poisson_2))
shareH_poisson_2 <- (beta_poisson_2*(h_poisson_2^rho_poisson_2))/((alpha_poisson_2*(k_poisson_2^rho_poisson_2))+(beta_poisson_2*(h_poisson_2^rho_poisson_2))+(1-alpha_poisson_2-beta_poisson_2))

k_nls <- ((1-alpha_nls-beta_nls)/(((ngdelta/s_ik)^rho_nls)-(beta_nls*((s_ih/s_ik)^rho_nls))-(alpha_nls)))^(1/rho_nls)
h_nls <- ((1-alpha_nls-beta_nls)/(((ngdelta/s_ih)^rho_nls)-(alpha_nls*((s_ik/s_ih)^rho_nls))-(beta_nls)))^(1/rho_nls)
shareK_nls <- (alpha_nls*(k_nls^rho_nls))/((alpha_nls*(k_nls^rho_nls))+(beta_nls*(h_nls^rho_nls))+(1-alpha_nls-beta_nls))
shareH_nls <- (beta_nls*(h_nls^rho_nls))/((alpha_nls*(k_nls^rho_nls))+(beta_nls*(h_nls^rho_nls))+(1-alpha_nls-beta_nls))

k_appr <- ((1-alpha_appr-beta_appr)/(((ngdelta/s_ik)^rho_appr)-(beta_appr*((s_ih/s_ik)^rho_appr))-(alpha_appr)))^(1/rho_appr)
h_appr <- ((1-alpha_appr-beta_appr)/(((ngdelta/s_ih)^rho_appr)-(alpha_appr*((s_ik/s_ih)^rho_appr))-(beta_appr)))^(1/rho_appr)
shareK_appr <- (alpha_appr*(k_appr^rho_appr))/((alpha_appr*(k_appr^rho_appr))+(beta_appr*(h_appr^rho_appr))+(1-alpha_appr-beta_appr))
shareH_appr <- (beta_appr*(h_appr^rho_appr))/((alpha_appr*(k_appr^rho_appr))+(beta_appr*(h_appr^rho_appr))+(1-alpha_appr-beta_appr))

plot(density(shareK_log), xlim=c(0.2,0.5),ylim=c(0,22),main="Physical Capital Shares", xlab="",lwd=2)
lines(density(shareK_appr),lty=2,lwd=2)
lines(density(shareK_nls),lty=3,lwd=2)
legend(0.2,21, c("Log-linearized","Approximated NLLS","Level NLLS"), lty=c(1:3),lwd=rep(2,3))

plot(density(shareK_log), xlim=c(0.2,0.5),ylim=c(0,60),main="Physical Capital Shares", xlab="",lwd=2)
lines(density(shareK_appr),lty=2,lwd=2)
lines(density(shareK_poisson_1),lty=3,lwd=2)
lines(density(shareK_nls),lty=4,lwd=2)
lines(density(shareK_poisson_2),lty=5,lwd=2)
legend(0.35,55, c("NLLS (Log)", "NLLS (Approximated)", "PPML (Approximated)", "NLLS", "PPML"), lty=c(1:5),lwd=rep(2,5))

plot(ecdf(shareK_log), xlim=c(0.2,0.52), verticals= T, do.points = F, main="ECDF of Physical Capital Shares",xlab="",ylab="ECDF",lwd=2,col="red")
lines(ecdf(shareK_poisson),verticals= T, do.points = F, lty=2,lwd=2,col="green")
lines(ecdf(shareK_nls),verticals= T, do.points = F, lty=3,lwd=2,col="blue")
legend(0.32,0.2, c("Log-linearized", "Poisson", "NLS level"), lty=c(1:3),lwd=rep(2,3),col=c("red","green","blue"))

plot(density(shareH_log), xlim=c(0.18,0.55),ylim=c(0,18),main="Human Capital Shares", xlab="",lwd=2)
lines(density(shareH_appr),lty=2,lwd=2)
lines(density(shareH_nls),lty=3,lwd=2)
legend(0.4,18, c("Log-linearized","Approximated NLLS","Level NLLS"), lty=c(1:3),lwd=rep(2,3))

plot(density(shareH_log), xlim=c(0.2,0.53),ylim=c(0,40),main="Human Capital Shares", xlab="",lwd=2)
lines(density(shareH_appr),lty=2,lwd=2)
lines(density(shareH_poisson_1),lty=3,lwd=2)
lines(density(shareH_nls),lty=4,lwd=2)
lines(density(shareH_poisson_2),lty=5,lwd=2)
legend(0.35,40, c("NLLS (Log)", "NLLS (Approximated)", "PPML (Approximated)", "NLLS", "PPML"), lty=c(1:5),lwd=rep(2,5))

plot(ecdf(shareH_log), xlim=c(0.14,0.52), verticals= T, do.points = F, main="ECDF of Human Capital Shares",xlab="",ylab="ECDF",lwd=2,col="red")
lines(ecdf(shareH_poisson),verticals= T, do.points = F, lty=2,lwd=2,col="green")
lines(ecdf(shareH_nls),verticals= T, do.points = F, lty=3,lwd=2,col="blue")
legend(0.4,0.4, c("Log-linearized", "Poisson", "NLS level"), lty=c(1:3),lwd=rep(2,3),col=c("red","green","blue"))
