This also uses regression but it
uses
lm.fit
which is faster thanlm
. (There also existsfastLm
in rcppArmadillo and rcppEigen that you could try as well.)avoids duplicating regressions by using only unique combinations.
assumes that only triples need to be investigated cutting down the amount of computation (since that seems the case in the post)
assumes all coefficients are integer to clean up the output
The code is:
eps <- .1
combos <- combn(ncol(DF), 3)
for(j in 1:ncol(combos)) {
ix <- combos[, j]
fit <- lm.fit(as.matrix(DF[ix[-1]]), DF[[ix[1]]])
SSE <- sum(resid(fit)^2)
if (SSE < eps) {
ecoef <- round(c(-1, coef(fit)))
names(ecoef)[1] <- names(DF)[ix[1]]
print(ecoef)
}
}
which gives this with the data in the post:
A B C
-1 1 -1
C F G
-1 1 1
G H I
-1 1 1