# Integer regression coefficients in R

I'd like to fit integer coefficients, e.g. summing to 10, to a regression equation. The absolute values of the coefficients (i.e. predicted y) aren't important, I just want to retain the appropriate relative values. The use case is for an easily interpretable scoring system.

For example, this regression yields the following coefficients (ignoring the intercept):

set.seed(0)
y <- rnorm(100)
x <- matrix(rnorm(300), ncol=3)
m <- lm(y ~ x)
(coef <- m$coefficients[-1]) # x1 x2 x3 # 0.12100965 0.05506511 0.14708549  Rounding with the below code yields a rounding error (sums to 11): round(10 * coef / sum(coef)) # x1 x2 x3 # 4 2 5  A method like this also doesn't guarantee maximally similar weights to the regression equation. This was asked here without satisfactory answers, and might be addressed in this paywalled research paper. Edit: looks like https://stackoverflow.com/questions/792460/how-to-round-floats-to-integers-while-preserving-their-sum may be able to help minimize the roundoff error. If my question is further specified as minimizing the error of a predicted (scaled) y, I'm unsure whether this is an equivalent optimization. • Have you considered integer linear programming? – Matthew Drury Sep 22 '15 at 21:14 • I haven't, that could be a good approach. I'll take a stab if I get a chance. – Max Ghenis Sep 22 '15 at 21:22 ## 1 Answer We can apply the answer to Round vector of numerics to integer while preserving their sum: smart.round <- function(x) { y <- floor(x) indices <- tail(order(x-y), round(sum(x)) - sum(y)) y[indices] <- y[indices] + 1 y }  At that point similar logic gets a reasonable answer: # Setup from question set.seed(0) y <- rnorm(100) x <- matrix(rnorm(300), ncol=3) m <- lm(y ~ x) coef <- m$coefficients[-1]
# Answer
smart.round(10 * coef / sum(coef))
# x1 x2 x3
#  4  2  4


I don't know whether this also minimizes the error of a predicted y, but it does yield something feasible.