# Linear combinations of coefficients in R

2018-04-12 1 minute read

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.1668 0.0771

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.1668 0.0771