N <- 100
x1 <- rnorm(N)
# make some correlation
x2 <- 0.1*rnorm(N) + 0.1*x1
y <- 0.1*x1 + x2 + rnorm(N)
summary(est <- felm(y ~ x1 + x2))
pt1 <- coef(est)['x1']
pt2 <- coef(est)['x2']
# expected values of coefficients, should match the summary
# and variance, i.e. square of standard errors in the summary
nlexpect(est, quote(c(x1=x1,x2=x2,var=c((x1-pt1)^2,(x2-pt2)^2))))
# the covariance matrix:
nlexpect(est, tcrossprod(as.matrix(c(x1-pt1,x2-pt2))))
#Wald test of single variable
waldtest(est, ~x1)['p.F']
# the same with nlexpect, i.e. probability for observing abs(x1)>abs(pt1) conditional
# on E(x1) = 0.
nlexpect(est, (x1-pt1)^2 > pt1^2)
# which of course is the same as
2*nlexpect(est, x1 < 0)
# then a joint test. Here we find the probability that
# we are further from origo than the point estimates, measured
# in a variance normalized coordinate system.
waldtest(est, ~ x1 | x2)['p.F']
#inverse cholesky
ich <- solve(t(chol(vcov(est)[c('x1','x2'), c('x1','x2')])))
# convert to normalized coordinates
nz <- sum((ich %*% c(pt1,pt2))^2)
# find probability that we're further away
nlexpect(est, sum((ich %*% c(x1-pt1,x2-pt2))^2) > nz)
# or use the .z argument provided automatically
nlexpect(est, sum(.z^2) > nz, coefs=c('x1','x2'))
# Non-linear test:
f <- function(x) c(poly=x[['x1']]*(6*x[['x1']]-x[['x2']]^2))
waldtest(est, f)['p.F']
# In general, for a function f, the non-linear Wald test is something like
# the following:
# expected value of function
Ef <- nlexpect(est, f, coefs=c('x1','x2'))
# point value of function
Pf <- f(c(pt1,pt2))
# similar to a Wald test:
nlexpect(est, function(x) (f(x)-Ef)^2 > Pf^2, c('x1','x2'))
# one-sided
nlexpect(est, function(x) f(x)-Ef > abs(Pf), c('x1','x2'))
# other sided
nlexpect(est, function(x) f(x)-Ef < -abs(Pf), c('x1','x2'))
Run the code above in your browser using DataLab