
Last chance! 50% off unlimited learning
Sale ends in
pcls(M)
pcls
. It should have the
following elements (last 3 are not in argument for mgcv) :ncol(M$X)
must give the number of model parameters, while nrow(M$X)
should give the number of data.start<-sum(M$df[1:(k-1)]^2)
or
start<-0
if k==1
. Then penalty matrix k
has
M$S[start+i+M$df[i]*(j-1)
M$S[]
in
the correct location within each penalty coefficient matrix. (Zero
offset implies starting in first location)M$df[i]
gives the number of rows and columns of
M$S[i]
that are non-zero.Quadratic programming is used to perform the solution. The method used is designed for maximum stability with least squares problems: i.e. ${\bf X}^\prime {\bf X}$ is not formed explicitly. See Gill et al. 1981.
Wood, S.N. (1994) Monotonic smoothing splines fitted by cross validation SIAM Journal on Scientific Computing 15(5):1126-1133
mgcv
mono.con
# first an un-penalized example - fit E(y)=a+bx subject to a>0
n<-100
x<-runif(n);y<-x-0.2+rnorm(n)*0.1
M<-list(X=matrix(0,n,2),p=c(0.1,0.5),off=array(0,0),df=array(0,0),S=0,
Ain=matrix(0,1,2),bin=0,C=matrix(0,0,0),sp<-0,y=y,w=y*0+1)
M$X[,1]<-1;M$X[,2]<-x;M$Ain[1,]<-c(1,0)
pcls(M)->M$p
plot(x,y);abline(M$p,col=2);abline(coef(lm(y~x)),col=3)
# and now a penalized example: a monotonic penalized regression spline .....
# Generate data from a monotonic truth.
set.seed(10);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)
# 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, constriants etc. for monotonic spline....
gam.setup(y~s(x,k=10,bs="cr")-1)->G;GAMsetup(G)->G;F<-mono.con(G$xp);
G$Ain<-F$A;G$bin<-F$b;G$C<-matrix(0,0,0);G$sp<-f.ug$sp;
G$p<-G$xp;G$y<-y;G$w<-y*0+1
p<-pcls(G); # fit spline (using s.p. from unconstrained fit)
# now modify the gam object from unconstrained fit a little, to use it
# for predicting and plotting constrained fit.
p<-c(0,p);f.ug$coefficients<-p;
lines(x,predict.gam(f.ug,newdata=data.frame(x=x)),col=2)
Run the code above in your browser using DataLab