我试图用三个协变量(X、Y、Z)和两个断点估计变量 V 的断点。
响应变量 V = aX + bY + cZ + d
我模拟数据,其中 (a,b,c,d) 有 3 组值,分别为 (0.6,0.2,0.8,0.15)、(1.6,1.2,1.8,1.15) 和 (3,5,4,2.5)
我使用分段包来估计系数,但得到以下错误:
Error in segmented.lm(linearFit, seg.Z = ~X + Y + Z, psi = list(X = c(NA), :
Bootstrap restart only with a fixed number of breakpoints
这是我的代码,带有数据
#trapezoidal data
ref=c(rep(1,100),seq(1,10,0.05),rep(10,150),seq(10,0,-0.05),rep(0,200))
#covariates
xx=cumsum(ref)
yy=diff(xx)
zz=diff(yy)
#equalizing lengths of above vectors
vecL=length(zz)
xx=xx[1:vecL]
yy=yy[1:vecL]
zz=zz[1:vecL]
#adding noise to covariates
set.seed(10)
X=xx + max(xx)/100*rnorm(vecL)
Y=yy + max(yy)/100*rnorm(vecL)
Z=zz + max(zz)/100*rnorm(vecL)
#three segment response variable, total 830 points
V[1:200] = 0.6 *X[1:200]+ 0.2 *Y[1:200]+ 0.8 *Z[1:200]+ 0.15 + 0.01*rnorm(200)
V[201:400] = 1.6 *X[201:400]+ 1.2 *Y[201:400]+ 1.8 *Z[201:400]+ 1.15 + 0.01*rnorm(200)
V[401:830] = 3.0 *X[401:830]+ 5.0 *Y[401:830]+ 4.0 *Z[401:830]+ 2.50 + 0.01*rnorm(430)
##linear model
linearFit=lm(formula=V~X+Y+Z)
summary(linearFit)
##segmented
segFit=segmented(linearFit,seg.Z=~X+Y+Z,psi=list(X=c(NA),Y=c(NA),Z=c(NA)),control=seg.control(display=TRUE, K=4, stop.if.error=FALSE))
这是输出:
segFit=segmented(linearFit,seg.Z=~X+Y+Z,psi=list(X=c(NA),Y=c(NA),Z=c(NA)),control=seg.control(display=TRUE, K=4, stop.if.error=FALSE))
Error in segmented.lm(linearFit, seg.Z = ~X + Y + Z, psi = list(X = c(NA), :
Bootstrap restart only with a fixed number of breakpoints
我是否正确设置了 psi 和控制?任何帮助表示赞赏。