Linear combinations of coefficients in R

2018/04/12

Updated: 2019-09-26.

One of the few features I miss from Stata is the very-intuitive lincom command. Most of the time I recreate that functionality with survey::svycontrast. But, I always forget the syntax. This code demonstrates a minimum working example.

library(survey)

N <- 100
df <- data.frame(x1 = rnorm(N), x2 = rnorm(N))
df$y <- 1 + 3 * df$x1 - 2 * df$x2 + rnorm(N, 0, 0.5)

fit <- lm(y ~ x1 + x2, data = df)

Having fit the model, we can pass unnamed vector with the right number of coefficients to get our desired linear combination:

svycontrast(fit, c(0, 1, 1))
##          contrast     SE
## contrast   1.1205 0.0683

Or a named vector with any number of coefficients (as long as the names match). One “gotcha” to keep in mind: I have found that the latter syntax fails for some versions of survey and may be OS-dependent.

svycontrast(fit, c("x2" = 1, "x1" = 1))
##          contrast     SE
## contrast   1.1205 0.0683

Finally, note that it’s also possible to do the same manually:

vars <- c("x1", "x2")
sum(coef(fit)[vars])
## [1] 1.120517
sqrt(sum(vcov(fit)[vars, vars]))
## [1] 0.06832892

Of course, it would be slightly more complicated to estimate a linear combination of estimators with coefficients \(\neq\) 1.