Hello everyone, Dr. wood told me that I can adapting his example to force cubic spline to pass through certain point. I still have no idea how to achieve this. Suppose we want to force the cubic spline to pass (1,1), how can I achieve this by adapting the following code?
# Penalized example: monotonic penalized regression spline ..... # Generate data from a monotonic truth. x<-runif(100)*4-1;x<-sort(x); f<-exp(4*x)/(1+exp(4*x));y<-f+rnorm(100)*0.1;plot(x,y) dat<-data.frame(x=x,y=y) # Show regular spline fit (and save fitted object) f.ug<-gam(y~s(x,k=10,bs="cr"));lines(x,fitted(f.ug)) # Create Design matrix, constraints etc. for monotonic spline.... sm<-smoothCon(s(x,k=10,bs="cr"),dat,knots=NULL)[[1]] F<-mono.con(sm$xp); # get constraints G<-list(X=sm$X,C=matrix(0,0,0),sp=f.ug$sp,p=sm$xp,y=y,w=y*0+1) G$Ain<-F$A;G$bin<-F$b;G$S<-sm$S;G$off<-0 p<-pcls(G); # fit spline (using s.p. from unconstrained fit) fv<-Predict.matrix(sm,data.frame(x=x))%*%p lines(x,fv,col=2) lines(x,f,col="blue") Thanks a lot!! Victor [[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.