I would do:
closest.to.zero <- function(x) x[which.min(abs(x))]
closest.to.zero(summary(model2)$coef[,3])
Вопрос
I'm doing some linear regression and testing a bunch of data with a program I'm writing in R.
After implementing a linear model lm()
, I get this:
>>summary(model2)$coef[,3]
(Intercept) sex fat fiber alcohol chol
19.2786166 -6.1693274 2.5304990 3.0357110 1.3205717 -0.8407960
To find the closet to zero, I'm doing this:
mn <- min(abs(summary(model2)$coef[,3]-0))
>> mn
[1] 0.840796
However, mn
should be negative. How can I adjust my method to return the negative version?
(I need it to be negative because I square it after).
Thanks in advance.
Решение
I would do:
closest.to.zero <- function(x) x[which.min(abs(x))]
closest.to.zero(summary(model2)$coef[,3])