[R] predict.glm
Peter Ehlers
ehlers at ucalgary.ca
Sat Jan 16 21:23:43 CET 2010
Maurice,
The 'newdata' argument should be a data.frame with the
same names as used for your predictors in the glm formula.
For the data you quote below (after fixing the missing y1),
try:
yhat <- predict(reg1, newdata = data.frame(x1 = x2))
length(yhat)
[1] 200
A careful reading of ?predict would have told you that:
"newdata optionally, a data frame in which to look for
variables with which to predict."
If that is not your problem, perhaps you could give an
example with data and code that illustrates your problem.
-Peter Ehlers
Maurice Charbit wrote:
> Hi,
>
> See below I reply your message for <https://stat.ethz.ch/pipermail/r-help/2008-April/160966.html>[R] predict.glm & newdata posted on Fri Apr 4 21:02:24 CEST 2008
>
>
> You say it ##works fine but it does not: if you look at the length of yhat2, you will find 100 and not 200 as expected. In fact predict(reg1, data=x2) gives the same results as predict(reg1).
>
>
> So I am still looking for a solution of this problem.
>
>
> best regards
>
> Maurice
>
> ===========
>
> Hi,
>
> could it be the "newdata" argument? When I run predict with the newdata
> argument, I get an error message - a different one though. The second reason
> might be that your dataset is named df, which is defined as a function an
> may produce problems. Try renaming the dataset.
>
> yhat=predict(reg1,newdata=x2)
>
> x1=seq(1:100)
> r.norm1=rnorm(100,0,20)
> x1=r.norm1+x1
>
> x2=x1*2
> r.norm2=rnorm(200,0,20)
> x2=r.norm2+x2
>
> reg1=glm(y1~x1,binomial)
> yhat=predict(reg1) ##prediction works fine
> yhat=predict(reg1,newdata=x2) ##gives error message because of "newdata"
>
>> Error in eval(predvars, data, env) :
> numeric 'envir' arg not of length one
>
> yhat=predict(reg1,data=x2) ##works fine
>
> ##using offset
>
> ofst=rep(0.5,100)
>
> reg1=glm(y1~x1,binomial,offset=ofst)
> yhat=predict(reg1)
> yhat1=predict(reg1,newdata=x2) ##gives error message
> yhat2=predict(reg1,data=x2) ##works fine
>
>
> % =================================================
> % Maurice CHARBIT
> % Ecole Nationale Supérieure des Télécommunications
> % Département TSI
> % 37,39 rue Dareau, pièce A321
> % 75634 - PARIS cedex 13 FRANCE
> % email: maurice.charbit at telecom-paristech.fr
> % Fax: (33) (0)1 45 81 71 44
> % Phone: (33) (0)1 45 81 71 78
> %==================================================|
> [[alternative HTML version deleted]]
>
>
>
> ------------------------------------------------------------------------
>
> ______________________________________________
> R-help at r-project.org mailing list
> 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.
--
Peter Ehlers
University of Calgary
403.202.3921
More information about the R-help
mailing list