Skip to content
Prev 318567 / 398503 Next

solving x in a polynomial function

Hello,

You're right, it should be names(coef(model)). I had tested the function 
before changing it to allow for a zero intercept. Sorry for the mess.

realroots <- function(model, b){
	is.zero <- function(x, tol = .Machine$double.eps^0.5) abs(x) < tol
	if(names(coef(model))[1] == "(Intercept)")
		r <- polyroot(c(coef(model)[1] - b, coef(model)[-1]))
	else
		r <- polyroot(c(-b, coef(model)))
	Re(r[is.zero(Im(r))])
}

r <- realroots(po.lm, 3)
predict(po.lm, newdata = data.frame(b = r))  # confirm
1 2
3 3

Rui Barradas

Em 01-03-2013 21:06, Mike Rennie escreveu: