pcls(mgcv)
pcls()所属R语言包:mgcv
Penalized Constrained Least Squares Fitting
惩罚约束最小二乘拟合
译者:生物统计家园网 机器人LoveR
描述----------Description----------
Solves least squares problems with quadratic penalties subject to linear equality and inequality constraints using quadratic programming.
解决与线性等式和不等式约束二次规划二次处罚的最小二乘问题。
用法----------Usage----------
pcls(M)
参数----------Arguments----------
参数:M
is the single list argument to pcls. It should have the following elements:
pcls单列表参数。它应具备以下要素:
yThe response data vector.
yThe响应数据向量。
wA vector of weights for the data (often proportional to the reciprocal of the variance).
华矢量数据的权重(通常是成正比的方差的倒数)。
XThe design matrix for the problem, note that ncol(M$X) must give the number of model parameters, while nrow(M$X) should give the number of data.
XThe设计矩阵问题,注意ncol(M$X)必须给模型参数的数量,而nrow(M$X)应该给予一些数据。
CMatrix containing any linear equality constraints on the problem (e.g. C in Cp=c). If you have no equality constraints initialize this to a zero by zero matrix. Note that there is no need to supply the vector c, it is defined implicitly by the initial parameter estimates p.
CMatrix含有任何线性等式约束问题(如CCp=c)。如果你没有平等约束初始化零零矩阵。请注意,有没有需要提供矢量c,它被定义初始参数隐估计p。
S A list of penalty matrices. S[[i]] is the smallest contiguous matrix including all the non-zero elements of the ith penalty matrix. The first parameter it penalizes is given by off[i]+1 (starting counting at 1).
这是个点球矩阵的列表。 S[[i]]是最小的连续矩阵,包括所有的第i个点球矩阵的非零元素。它惩罚的第一个参数是由off[i]+1(从1开始计数)。
off Offset values locating the elements of M$S in the correct location within each penalty coefficient matrix. (Zero offset implies starting in first location)
关闭定位元素的偏移值M$S在每个罚款系数矩阵内的正确位置。 (零点偏移意味着在第一的位置开始)
sp An array of smoothing parameter estimates.
SP平滑参数估计值的一个数组。
pAn array of feasible initial parameter estimates - these must satisfy the constraints, but should avoid satisfying the inequality constraints as equality constraints.
平移阵列可行的初始参数估计 - 这些必须满足的约束,但应避免满足不等式约束等式约束。
AinMatrix for the inequality constraints A_in p > b.
不等式约束AinMatrix A_in p > b。
binvector in the inequality constraints. </table>
不等式约束binvector。 </ TABLE>
Details
详情----------Details----------
This solves the problem:
这解决了这个问题:
subject to constraints Cp=c and A_in p > b_in, w.r.t. p given the smoothing parameters lambda_i. X is a design matrix, p a parameter vector, y a data vector, W a diagonal weight matrix, S_i a positive semi-definite matrix of coefficients defining the ith penalty and C a matrix of coefficients defining the linear equality constraints on the problem. The smoothing parameters are the lambda_i. Note that X must be of full column rank, at least when projected into the null space of any equality constraints. A_in is a matrix of coefficients defining the inequality constraints, while b_in is a vector involved in defining the inequality constraints.
受约束Cp=c和A_in p > b_in,w.r.t., p平滑参数lambda_i。 X是p参数向量,y数据向量,W重量对角线矩阵,S_i半正定矩阵的设计矩阵,系数确定第i罚款和C定义的线性等式约束问题的系数矩阵。平滑参数lambda_i。注意X必须是列满秩的,至少在预计到任何等式约束的空空间。 A_in是确定不等式约束系数矩阵,而b_in是参与确定不等式约束矢量。
Quadratic programming is used to perform the solution. The method used is designed for maximum stability with least squares problems: i.e. X'X is not formed explicitly. See Gill et al. 1981.
二次规划用于执行的解决方案。所采用的方法是专为最高的稳定性与最小二乘问题:即X'X没有形成明确。吉尔等。 1981年。
值----------Value----------
The function returns an array containing the estimated parameter vector.
该函数返回一个数组,包含估计的参数向量。
作者(S)----------Author(s)----------
Simon N. Wood <a href="mailto:simon.wood@r-project.org">simon.wood@r-project.org</a>
参考文献----------References----------
Press, London.
Journal on Scientific Computing 15(5):1126-1133
参见----------See Also----------
mgcv mono.con
mgcvmono.con
举例----------Examples----------
# first an un-penalized example - fit E(y)=a+bx subject to a>0[首先是未,惩罚的例子 - 适合E(Y)= A + BX> 0]
set.seed(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),S=list(),
Ain=matrix(0,1,2),bin=0,C=matrix(0,0,0),sp=array(0,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)
# 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)[合样条曲线(使用不受约束适合s.p.的)]
fv<-Predict.matrix(sm,data.frame(x=x))%*%p
lines(x,fv,col=2)
# now a tprs example of the same thing....[现在的TPRS例如同样的事情,....]
f.ug<-gam(y~s(x,k=10));lines(x,fitted(f.ug))
# Create Design matrix, constriants etc. for monotonic spline....[创建单调样条设计矩阵constriants,等....]
sm<-smoothCon(s(x,k=10,bs="tp"),dat,knots=NULL)[[1]]
xc<-0:39/39 # points on [0,1] [点[0,1]]
nc<-length(xc) # number of constraints[约束数]
xc<-xc*4-1 # points at which to impose constraints[点时施加限制]
A0<-Predict.matrix(sm,data.frame(x=xc))
# ... A0%*%p evaluates spline at xc points[... A0%*%P在XC点评估的样条]
A1<-Predict.matrix(sm,data.frame(x=xc+1e-6))
A<-(A1-A0)/1e-6
## ... approx. constraint matrix (A%*%p is -ve [#...约。约束矩阵(%*%,P-VE]
## spline gradient at points xc)[#样条梯度点XC)]
G<-list(X=sm$X,C=matrix(0,0,0),sp=f.ug$sp,y=y,w=y*0+1,S=sm$S,off=0)
G$Ain<-A; # constraint matrix[约束矩阵]
G$bin<-rep(0,nc); # constraint vector[约束向量]
G$p<-rep(0,10);G$p[10]<-0.1
# ... monotonic start params, got by setting coefs of polynomial part[...单调启动的params,通过设置多项式部分coefs的了]
p<-pcls(G); # fit spline (using s.p. from unconstrained fit)[合样条曲线(使用不受约束适合s.p.的)]
fv2<-Predict.matrix(sm,data.frame(x=x))%*%p
lines(x,fv2,col=3)
######################################[#####################################]
## monotonic additive model example...[#单调可加性模型的例子...]
######################################[#####################################]
## First simulate data...[#首先模拟数据......]
set.seed(10)
f1 <- function(x) 5*exp(4*x)/(1+exp(4*x));
f2 <- function(x) {
ind <- x > .5
f <- x*0
f[ind] <- (x[ind] - .5)^2*10
f
}
f3 <- function(x) 0.2 * x^11 * (10 * (1 - x))^6 +
10 * (10 * x)^3 * (1 - x)^10
n <- 200
x <- runif(n); z <- runif(n); v <- runif(n)
mu <- f1(x) + f2(z) + f3(v)
y <- mu + rnorm(n)
## Preliminary unconstrained gam fit...[#初步不受约束的自由亚齐运动适合...]
G <- gam(y~s(x)+s(z)+s(v,k=20),fit=FALSE)
b <- gam(G=G)
## generate constraints, by finite differencing[#产生的限制,有限差分]
## using predict.gam ....[#使用predict.gam ....]
eps <- 1e-7
pd0 <- data.frame(x=seq(0,1,length=100),z=rep(.5,100),
v=rep(.5,100))
pd1 <- data.frame(x=seq(0,1,length=100)+eps,z=rep(.5,100),
v=rep(.5,100))
X0 <- predict(b,newdata=pd0,type="lpmatrix")
X1 <- predict(b,newdata=pd1,type="lpmatrix")
Xx <- (X1 - X0)/eps ## Xx %*% coef(b) must be positive [#XX%*%系数(二)必须是积极的]
pd0 <- data.frame(z=seq(0,1,length=100),x=rep(.5,100),
v=rep(.5,100))
pd1 <- data.frame(z=seq(0,1,length=100)+eps,x=rep(.5,100),
v=rep(.5,100))
X0 <- predict(b,newdata=pd0,type="lpmatrix")
X1 <- predict(b,newdata=pd1,type="lpmatrix")
Xz <- (X1-X0)/eps
G$Ain <- rbind(Xx,Xz) ## inequality constraint matrix[#不等式约束矩阵]
G$bin <- rep(0,nrow(G$Ain))
G$sp <- b$sp
G$p <- coef(b)
G$off <- G$off-1 ## to match what pcls is expecting[#匹配PCLS期待什么]
## force inital parameters to meet constraint[#强制>初始参数,以满足约束]
G$p[11:18] <- G$p[2:9]<- 0
p <- pcls(G) ## constrained fit[#约束适合]
par(mfrow=c(2,3))
plot(b) ## original fit[#原始适合]
b$coefficients <- p
plot(b) ## constrained fit[#约束适合]
## note that standard errors in preceding plot are obtained from[#注意,在前面的图标准误差获得]
## unconstrained fit[#约束适合]
转载请注明:出自 生物统计家园网(http://www.biostatistic.net)。
注:
注1:为了方便大家学习,本文档为生物统计家园网机器人LoveR翻译而成,仅供个人R语言学习参考使用,生物统计家园保留版权。
注2:由于是机器人自动翻译,难免有不准确之处,使用时仔细对照中、英文内容进行反复理解,可以帮助R语言的学习。
注3:如遇到不准确之处,请在本贴的后面进行回帖,我们会逐渐进行修订。
|