[18647] in s-news-athena

home help back first fref pref prev next nref lref last post

[S] predict() with newdata-option

daemon@ATHENA.MIT.EDU (Matthias Richard)
Fri Jul 16 12:37:57 1999

Date: Fri, 16 Jul 1999 18:14:43 +0200 (MET DST)
From: Matthias Richard <richard@Psyres-Stuttgart.DE>
Reply-To: Matthias Richard <richard@Psyres-Stuttgart.DE>
To: SPLUS-Diskussionsliste <s-news@wubios.wustl.edu>
Message-Id: <Pine.GSO.3.96.990716180400.8823B-100000@sunserv>
Mime-Version: 1.0
Content-Type: TEXT/PLAIN; charset=US-ASCII



Hello everybody!

I am trying to understand the predict()-function for gam.objects.
I am fitting two independent smoothing functions for two groups and 
want to create predictions using predict() with the 'newdata' 
option. All works fine WITHOUT the 'newdata' option, but I run 
into trouble WITH the option. 

I am using a trick for independent smoothers in different groups:  group
variables are set to NA for the other group and gam()is fitted with
'na.action=na.gam.replace' cf. http://lib.stat.cmu.edu/s-news/Burst/10877
(Thanks to Charles Anderson who pointed to the right spot in the s-news
archives for my former question to the list!!!!) 

I think the trouble is related to this trick, but I am not sure...

My question is:
does anybody have had a similar problem or could anyone check the 
code below and see what's wrong with it? The example below is for a 
simple analysis - I actually need it for a discrete time (DT) 
survival analysis with multiple spells, to obtain the smoothed 
hazard functions for each spell, i.e using predict() without 
'newdata' is of no help in that case...

I got the same error message using gam() for this small example and 
for the DT-survival analysis using my data:

Error in [[: Attempt to select zero elements: structure(..
Dumped

thanks a million in advance!!!

Matthias


EXAMPLE:
###########################################################
### create small dataset
work<-data.frame(
y=c(rep(1:50,1),rep(1:50,1)),
x=c(rep(-1:-50,1),rep(1:50,1)),
grp=c(rep(1,50),rep(2,50))
)
work$x<-work$x+rnorm(100)
x<-c(rep(0,50),((1:50)^2)/50)
work$y<-work$y-x

### different shape for both groups
plot(work$x,work$y)


####### use gam() for modeling smoothed relation betw. y and x
####### trick for independent smoothers in different groups:
####### G1/G2 is set to NA for the other group and gam fitted
####### with 'na.action=na.gam.replace'.
### cf. http://lib.stat.cmu.edu/s-news/Burst/10877 

work$G1<-ifelse(work$grp==1,work$x,NA)
work$G2<-ifelse(work$grp==2,work$x,NA)
work$grp<-factor(work$grp)

object<-gam(
y ~ grp + s(G1) + s(G2),
na.action=na.gam.replace,
data=work)

summary(object)
plot.gam(object)


###### predict values WITHOUT defining 'newdata' works nice
newdata.no<-predict(object, type="response")
### observed vs. predicted
residual<-work$y-newdata.no
plot(newdata.no,work$y)
plot(residual,work$y)
plot(work$x,newdata.no)


###### predict values WITH defining 'newdata' does not work...
###### several modifications of 'newdata.yes' had the same result
newdata.yes<-data.frame(
grp=c(rep(1,50)    ,rep(2,50 )),
G1 =c(rep(-50:-1,1),rep(NA,50)),
G2 =c(rep(NA,50)   ,rep(1:50,1))
)
newdata.yes$grp<-factor(z$grp)
pred<-predict(object,newdata.yes,type="terms")

### yields following error:
Error in [[: Attempt to select zero elements: structure(..
Dumped


##### clean up
#rm(work, object, residual, newdata.no, newdata.yes, x)

#############################################################


----------------------------------------
Matthias Richard (doctoral candidate)

Center for Research on Psychotherapy
Christian-Belser-Str. 79a
70593 Suttgart
Germany
email: richard@psyres-stuttgart.de
telephone: ++49-711-6781-408
http://www.psyres-stuttgart.de

COST Action B6:
'Efficient Psychotherapy of Eating Disorders'
(COoperation of Science and Technology in the European Union)
-----------------------------------------


-----------------------------------------------------------------------
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

home help back first fref pref prev next nref lref last post