Skip to content
Prev 275420 / 398506 Next

using predict.lm() within a function

Hi Michael:

Try this:

show.beta <- function(model, x = 'x',
                      x1, x2, label, col="black", ...) {
   abline(model, col=col, lwd=2)
   xs <- data.frame(c(x1, x2, x2))
   names(xs) <- attr(model$coefficients, 'names')[2]
   ys <- predict(model, xs)
   lines(cbind(xs,ys[c(1,1,2)]), col=col)
   text(x2, mean(ys[1:2]), label, col=col, ...)
}

Both of your examples worked for me. The trick is to match the
variable name in the model with the variable name in the prediction
data frame. There are several ways to access that, but I chose the
first opportunity I saw:
List of 12
 $ coefficients : Named num [1:2] 0.501 0.351
  ..- attr(*, "names")= chr [1:2] "(Intercept)" "xx"

The line where I assign the names to xs is where the attr() function
comes into play. The first argument is the choice of list component
from the model object, while the second is the name of the attribute
to extract. This one returns a vector whose second component is what
we want.

HTH,
Dennis
On Mon, Oct 24, 2011 at 2:30 PM, Michael Friendly <friendly at yorku.ca> wrote: