문제

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])
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top