Nonsense prediction using package segmented in R -
i first fitted poisson glm in r follows:
> y<-c(13,21,12,11,16,9,7,5,8,8) > x<-c(74,81,80,79,89,96,69,88,53,72) > age<-c(50.45194,54.89382,46.52569,44.84934,53.25541,60.16029,50.33870, + 51.44643,38.20279,59.76469) > dat=data.frame(y=y,off.set.term=log(x),age=age) > fit.1=glm(y~age+offset(off.set.term),data=dat,family=poisson)
next tried predictions of response (on log scale) new dataset using predict
function. note set offset term zero.
> newdat=data.frame(age=c(52.09374,50.89329,50.61472,39.13358,44.79453),off.set.term=rep(0,5)) > predict(fit.1,newdata =newdat,type="link") 1 2 3 4 5 -1.964381 -1.956234 -1.954343 -1.876416 -1.914839
next tried package segmented (version 0.3-0.0) in r , fitted segmented glm follows. (the latest version of segmented package (i.e. 0.3-1.0) not seem support offset term when using predict function.)
> library(segmented) > fit.2=segmented(fit.1,seg.z=~age,psi=list(age=mean(age)), + offs=off.set.term,data=newdat)
i used predict function fit.2
predicted values:
> library(segmented) > fit.2=segmented(fit.1,seg.z=~age,psi=list(age=mean(age)),offs=off.set.term,data=newdat) > > predict(fit.2,newdata =newdat,type="link") 1 2 3 4 5 -26.62968 -26.08611 -25.95997 -20.76125 -23.32456
these predicted values different 1 obtained using fit.1
.
the problem seems in offset term because when fit models without offset term, results reasonable , close each other follow:
> fit.3=glm(y~age,data=dat,family=poisson) > newdat.2=data.frame(age=c(52.09374,50.89329,50.61472,39.13358,44.79453)) > predict(fit.3,newdata =newdat.2,type="link") 1 2 3 4 5 2.406016 2.395531 2.393098 2.292816 2.342261 > fit.4=segmented(fit.3,seg.z=~age,psi=list(age=mean(age)),data=newdat.2) > predict(fit.4,newdata =newdat.2,type="link") 1 2 3 4 5 2.577669 2.524503 2.512165 2.003679 2.254396
since got answer segmented package maintainer, decided share here. first, up-date package version 0.3-1.0
install.packages("segmented",type="source")
after updating, running same commands leads to:
> y<-c(13,21,12,11,16,9,7,5,8,8) > x<-c(74,81,80,79,89,96,69,88,53,72) > age<-c(50.45194,54.89382,46.52569,44.84934,53.25541,60.16029,50.33870, + 51.44643,38.20279,59.76469) > dat=data.frame(y=y,off.set.term=log(x),age=age) > fit.1=glm(y~age+offset(off.set.term),data=dat,family=poisson) > > newdat=data.frame(age=c(52.09374,50.89329,50.61472,39.13358,44.79453),off.set.term=rep(0,5)) > predict(fit.1,newdata =newdat,type="link") 1 2 3 4 5 -1.964381 -1.956234 -1.954343 -1.876416 -1.914839 > > library(segmented) > fit.2=segmented(fit.1,seg.z=~age,psi=list(age=mean(age)),offs=off.set.term,data=newdat) > predict(fit.2,newdata =newdat,type="link") error in offset(off.set.term) : object 'off.set.term' not found
so offset term cannot found. trick (for now) first attach newdat
, predict follows:
> attach(newdat) following object masked _by_ .globalenv: age > predict(fit.2,newdata =newdat,type="link") 1 2 3 4 5 -1.825831 -1.853842 -1.860342 -2.128237 -1.996147
the results make sense now. cheers!
Comments
Post a Comment