Skip to content
Prev 248522 / 398506 Next

Extrapolating values from a glm fit

On Wed, 2011-01-26 at 19:25 -1000, Ahnate Lim wrote:
Your original problem was the use of `newdata = as.data.frame(0.5)`.
There are two problems here: i) if you don't name the input (x = 0.5,
say) then you get a data frame with the name(s) "0.5":
0.5
1 0.5

and ii) if you do name it, you still get a data frame with name(s) "0.5"
0.5
1 0.5

In both cases, predict wants to find a variable with the name `x` in the
object supplied to `newdata`. It finds `x` but your `x` in the global
workspace, but warns because it knows that `newdata` was a data frame
with a single row - so there was a mismatch and you likely made a
mistake.

In these cases, `data.frame()` is preferred to `as.data.frame()`:

predict(mylogit, newdata = data.frame(x = 0), type = "response")

or we can use a list, to save a few characters:

predict(mylogit, newdata = list(x = 0), type = "response")

which give:
1 
0.813069
1 
0.813069 

In summary, use `data.frame()` or `list()` to create the object passed
as `newdata` and make sure you give the component containing the new
values a *name* that matches the predictor in the model formula.

HTH

G