[R] predict newdata question

Felipe Carrillo mazatlanmexico at yahoo.com
Sat Jun 26 08:13:25 CEST 2010


Thanks Bill, that worked great!!
 
> You ask:

# How can I use predict here, 'newdata' 
> crashes
predict(m1,newdata=wolf$predicted);wolf # it doesn't work

To 
> use predict() you need to give a fitted model object (here m1) and a *data 
> frame* to specify the values of the predictors for which you want 
> predictions.  Here wolf$predicted is not a data frame, it is a 
> vector.

What I think you want is

pv <- predict(m1, newdata = 
> wolf)

That will get you linear predictors.  To get probabilities you 
> need to say so as

probs <- predict(m1, newdata = wolf, type = 
> "response")

You can put these back into the data frame if you wish, 
> e.g.

wolf <- within(wold, {
    lpreds <- 
> predict(m1, wolf)
    probs <- predict(m1, wolf, type = 
> "response")
})

Now if you look at 

head(wolf)

you will 
> see two extra columns.


-----Original Message-----
From: > ymailto="mailto:r-help-bounces at r-project.org" 
> href="mailto:r-help-bounces at r-project.org">r-help-bounces at r-project.org 
> [mailto:> href="mailto:r-help-bounces at r-project.org">r-help-bounces at r-project.org] On 
> Behalf Of Felipe Carrillo
Sent: Saturday, 26 June 2010 10:35 AM
To: > ymailto="mailto:r-help at stat.math.ethz.ch" 
> href="mailto:r-help at stat.math.ethz.ch">r-help at stat.math.ethz.ch
Subject: 
> [R] predict newdata question

Hi:
I am using a subset of the below 
> dataset to predict PRED_SUIT for
the whole dataset but I am having trouble 
> with 'newdata'. The model
was created with 153 records and want to predict 
> for 208 records. 

[lots of stuff 
> omitted]

wolf$prob99<-(exp(wolf$predicted))/(1+exp(wolf$predicted))
head(wolf);dim(wolf)
   
> # How can I use predict here, 'newdata' 
> crashes
 predict(m1,newdata=wolf$predicted);wolf  # it doesn't 
> work

Thanks for any hints


Felipe D. Carrillo
Supervisory 
> Fishery Biologist
Department of the Interior
US Fish & Wildlife 
> Service
California, 
> USA




______________________________________________
> ymailto="mailto:R-help at r-project.org" 
> href="mailto:R-help at r-project.org">R-help at r-project.org mailing list
> href="https://stat.ethz.ch/mailman/listinfo/r-help" target=_blank 
> >https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting 
> guide http://www.R-project.org/posting-guide.html
and provide commented, 
> minimal, self-contained, reproducible code.






More information about the R-help mailing list