Federico Bonofiglio
2011-Mar-19 11:16 UTC
[R] strange PREDICTIONS from a PIECEWISE LINEAR (mixed) MODEL
Hi Dears, When I introduce an interaciton in a piecewise model I obtain some quite unusual results. If that would't take u such a problem I'd really appreciate an advise from you. I've reproduced an example below... Many thanks x<-rnorm(1000) y<-exp(-x)+rnorm(1000) plot(x,y) abline(v=-1,col=2,lty=2) mod<-lm(y~x+x*(x>-1)) summary(mod) yy<-predict(mod) lines(x[order(x)],yy[order(x)],col=2,lwd=2) #--lme #grouping factor, unbalanced g<-as.character(c(1:200)) id<-sample(g,size=1000,replace=T, prob=sample(0:1,200,rep=T)) table(id) #unbalanced mod2<-lme(y~x+x*(x>-1),random=~x|id, data=data.frame(x,y,id)) summary(mod2) newframe<-data.frame( #fictious id id="fictious", x) newframe[1:5,] #predictions yy2<-predict(mod2,level=0, newdata=newframe) lines(x[order(x)],yy2[order(x)],col="blue",lwd=2) # add variable in the model z<-rgamma(1000,4,6) mod3<-lme(y~x+x*(x>-1)+z ,random=~x|id, data=data.frame(x,y,z,id)) summary(mod3) #new id newframe2<-data.frame( #fictious id id="fictious", x, z) #predict yy3<-predict(mod3,level=0, newdata=newframe2) lines(x[order(x)],yy3[order(x)],col="green",lwd=2) # ADD INTERACTION z:x mod4<-lme(y~x+x*(x>-1)+ z+ z:x+ z:x*(x>-1) ,random=~x|id, data=data.frame(x,y,z,id)) #predict yy4<-predict(mod4,level=0, newdata=newframe2) lines(x[order(x)],yy4[order(x)],col="violet",lwd=2) #something bizarre #starts to happen #in the predicted values # they begin to jiggle around the straight line -- *Little u can do against ignorance,....it will always disarm u: is the 2nd principle of thermodinamics made manifest, ...entropy in expansion.**....But setting order is the real quest 4 truth, ......and the mission of a (temporally) wise dude. * [[alternative HTML version deleted]]
Kevin Wright
2011-Mar-21 14:26 UTC
[R] strange PREDICTIONS from a PIECEWISE LINEAR (mixed) MODEL
1. Try using set.seed for better reproducibility. 2. Does this really work for you? I get mydf <- data.frame(x, y,id) mod2<-lme(y ~ x + x*(x>-1), random=~x|id, + data=mydf) Error in lme.formula(y ~ x + x * (x > -1), random = ~x | id, data = mydf) : nlminb problem, convergence error code = 1 message = iteration limit reached without convergence (9) Enter a frame number, or 0 to exit 1: lme(y ~ x + x * (x > -1), random = ~x | id, data = mydf) 2: lme.formula(y ~ x + x * (x > -1), random = ~x | id, data = mydf) Kevin On Sat, Mar 19, 2011 at 6:16 AM, Federico Bonofiglio <bonoricus@gmail.com>wrote:> Hi Dears, > > When I introduce an interaciton in a piecewise model I obtain some quite > unusual results. > > If that would't take u such a problem I'd really appreciate an advise from > you. > > I've reproduced an example below... > > Many thanks > > > > > x<-rnorm(1000) > > y<-exp(-x)+rnorm(1000) > > plot(x,y) > abline(v=-1,col=2,lty=2) > > > mod<-lm(y~x+x*(x>-1)) > > summary(mod) > > yy<-predict(mod) > > lines(x[order(x)],yy[order(x)],col=2,lwd=2) > > > #--lme > > #grouping factor, unbalanced > > g<-as.character(c(1:200)) > id<-sample(g,size=1000,replace=T, > prob=sample(0:1,200,rep=T)) > > table(id) #unbalanced > > > > mod2<-lme(y~x+x*(x>-1),random=~x|id, > data=data.frame(x,y,id)) > > summary(mod2) > > > newframe<-data.frame( #fictious id > id="fictious", > x) > > newframe[1:5,] > > #predictions > > yy2<-predict(mod2,level=0, newdata=newframe) > > > lines(x[order(x)],yy2[order(x)],col="blue",lwd=2) > > > > # add variable in the model > > z<-rgamma(1000,4,6) > > mod3<-lme(y~x+x*(x>-1)+z > ,random=~x|id, > data=data.frame(x,y,z,id)) > > summary(mod3) > > > #new id > > newframe2<-data.frame( #fictious id > id="fictious", > x, > z) > > > #predict > > yy3<-predict(mod3,level=0, newdata=newframe2) > > > lines(x[order(x)],yy3[order(x)],col="green",lwd=2) > > > > # ADD INTERACTION z:x > > > > mod4<-lme(y~x+x*(x>-1)+ > > z+ > z:x+ > z:x*(x>-1) > > ,random=~x|id, > data=data.frame(x,y,z,id)) > > > > #predict > > yy4<-predict(mod4,level=0, newdata=newframe2) > > > lines(x[order(x)],yy4[order(x)],col="violet",lwd=2) #something bizarre > #starts to happen > #in the predicted values > > # they begin to jiggle around the straight line > > > > > > > > > -- > *Little u can do against ignorance,....it will always disarm u: > is the 2nd principle of thermodinamics made manifest, ...entropy in > expansion.**....But setting order is the real quest 4 truth, ......and the > mission of a (temporally) wise dude. > * > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help@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. >[[alternative HTML version deleted]]
Apparently Analagous Threads
- PREDICTIONS from a PIECEWISE LINEAR (mixed) MODEL: THEY AIN'T LINEAR BETWEEN BREAK POINTS!!
- SLLOOOWWW function ...
- as.formula and lme ( Fixed effects: Error in as.vector(x, "list") : cannot coerce to vector)
- as.formula and lme ( Fixed effects: Error in as.vector(x, "list") : cannot coerce to vector)
- lm with predictor matrix