[R] two comments regarding predict.lm
Ritter, Christian C SRTCL-CTGAS
Christian.C.Ritter at OPC.shell.com
Thu Feb 14 19:42:12 CET 2002
Here is the first one.
It concerns the handling of multiple offsets.
The following lines creates a list with 3 explanatory variables and
one response.
> x<-seq(0,1,length=10);y<-sin(x);z<-cos(x);
w<-x+y+z+rnorm(x)
> data<-list(x=x,y=y,z=z,w=w)
A lm is fitted with one explanatory variable and two offsets. So
far, so good.
> tt<-lm(w~x+offset(y)+offset(z),data=data)
Then predict dies since it can only handle one offset term.
> predict(tt,data)
Error in eval(attr(tt, "variables")[[off.num + 1]], newdata)
:
attempt to select more than one element
The following shows that it works with a single offset term.
> tt<-lm(w~x+offset(y+z),data=data)
> predict(tt,data)
1 2 3 4 5 6
7 8
1.011794 1.302522 1.579625 1.841981 2.088647 2.318876
2.532122 2.728049
9 10
2.906535 3.067673
I would find it useful, if I could handle several offset terms.
Here is the second. It concerns again offsets. Deep in the belly of
predict.lm we find the lines:
predictor <- drop(X[, piv, drop = FALSE] %*% beta[piv])
if (!is.null(offset))
predictor <- predictor + offset
X is the model matrix as obtained by
X <- model.matrix(delete.response(tt), newdata, contrasts =
object$contrasts, xlev = object$xlevels)
and offset is a vector if there is an offset term. Now imagine that
the variables in newdata which make the offset contain some missing values
and that the variables in newdata which correspond to the model matrix dont.
In this case, predictor will be longer than offset and taking the sum
predictor + offset will make no sense. In the current version, this will
result in a warning of "different lengths". I would prefer if the result
were one of the following two:
a) a stop with an error message (since the consequence of
ignoring the warning could be disastrous)
b) that predictor and offset would be constructed of the
same (maximum) length, eventually padded by NA's where necessary
The second choice may make things easier to work with.
Greetings,
Chris
_
platform i386-pc-mingw32
arch x86
os Win32
system x86, Win32
status
major 1
minor 4.1
year 2002
month 01
day 30
language R
>
> Kind Regards,
> Christian Ritter, Statistical Services
>
Tel +32 10 477349/ Fax +32 10 477219
email : Christian.C.Ritter at OPC.Shell.com
Shell Coordination Centre
Shell Research and Technology Centre
Avenue Jean Monnet 1
B-1348 Ottignies Louvain-La-Neuve (Belgium)
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
More information about the R-help
mailing list