# [S] bug in predict()

Gerhard Koop (koop@icafe-funclub.de)
Sat, 09 May 1998 14:43:28 +0200

There appears to be a serious bug in the predict- function. After having analysed it further I think it lies in
an internal routine which is utilized by the model.matrix() function ...
Anyone who is using predict should be warned ...
I'm sorry for this lengthy exposition but I think it shows the matter more clearly .
I sent a copy to support and doc.

#
#DEMO for the lm- and predict- funktions:
#
# Rawdata:
# ! b1 b2
# --+-----------
# a1! (1) (7)
# !
# a2! (3) (?)
# !
#Problem: predict thr response at the combination(a2,b2)
#using a linear model with additive main effects
#
#It is quite obvious that the solution is: (?) = (9)
#
#Part 1) below says just that !
#
#But part 3) reveals an error in the predict- function
#because predict claims: (?)=(1) ????????
#

##########################
# generate the rawdata
##########################
bacteria <- c(1,3,7)
#generate factor
A<- factor(paste("a", c('1','2','1'), sep=""))
B<- factor(paste("b", c('1','1','2'), sep=""))
#put everything together
raw.df <- data.frame(bacteria, A,B)

##########################
# generate lm - object
##########################
raw.lm <- lm(bacteria~ A + B,data=raw.df
,model=T
,x=T
,y=T
)
raw.lm\$fitted.values
raw.lm\$coefficients
raw.lm\$effects
raw.lm\$model
raw.lm\$x
raw.lm\$y

#####################################################
# 1) predict response at (a1,b1),(a2,b2)
#####################################################

#generate factor
A<- factor(paste("a", c('1','2'), sep=""))
B<- factor(paste("b", c('1','2'), sep=""))
#put everything together
unbekannt.df <- data.frame(A,B)

unbekannt.df # ok ?
predict(raw.lm,unbekannt.df)

#####################################################
# 2) predict response at (a1,b1)
#####################################################

#generate factor
A<- factor(paste("a", c('1'), sep=""))
B<- factor(paste("b", c('1'), sep=""))
#put everything together
unbekannt.df <- data.frame(A,B)

unbekannt.df # ok ?
predict(raw.lm,unbekannt.df)

#####################################################
# 3) predict response at (a2,b2)
#####################################################

#generate factor
A<- factor(paste("a", c('2'), sep=""))
B<- factor(paste("b", c('2'), sep=""))
#put everything together
unbekannt.df <- data.frame(A,B)

unbekannt.df # ok ?
predict(raw.lm,unbekannt.df)

-----------------------------------------------------------------------
This message was distributed by s-news@wubios.wustl.edu. To unsubscribe
send e-mail to s-news-request@wubios.wustl.edu with the BODY of the
message: unsubscribe s-news