The problem doesn't have anything to do with cv=TRUE, it has to do with the problem of them checking for convergence on two parameters when there should be only one. The deming() function first calls deming.fit1() to generate initial values and then deming.fit2() to produce the result. In deming.fit2(), there are the lines:
ifit <- deming.fit1(x, y, wt, xstd, ystd, intercept)
if (intercept) {
alpha <- ifit$coefficients[1]
beta <- ifit$coefficients[2]
}
else {
alpha <- 0
beta <- ifit$coefficients
}
And then, in the subsequent 10 iteration loop,
w <- 1/(ystd^2 + beta*xstd^2)
newx <- w*(ystd^2*x + xstd^2* beta*(y- alpha))
newy <- alpha + beta*newx
xstd <- stdpat[1] + stdpat[2]*pmax(0, newx)
ystd <- stdpat[3] + stdpat[4]*pmax(0, newy)
if (intercept)
fit <- optimize(minfun, c(.8, 1.2)*beta, x=x, y=y, wt=wt,
xv=xstd^2, yv=ystd^2)
else fit <- optimize(minfun0, c(.8, 1.2)*beta, x=x, y=y, wt=wt,
xv=xstd^2, yv=ystd^2)
oldbeta <- beta
beta <- fit$minimum
if (intercept) alpha <- afun(beta, x, y, wt=wt, xstd^2, ystd^2)
if (abs(oldbeta - beta)/(abs(beta)+tol) <= tol) break
The optimize() function is a single parameter optimizer, so fit$minimum is a scalar - the optimized parameter value. In the first iteration of this loop, oldbeta is two parameters, the results from deming.fit1(). That is what causes the error. Changing the initial definitions of alpha and beta to:
ifit <- deming.fit1(x, y, wt, xstd, ystd, intercept)
if (intercept) {
alpha <- ifit$coefficients[1]
beta <- ifit$coefficients[2]
}
else {
alpha <- 0
beta <- ifit$coefficients[2]
}
will fix the function. I forked the GitHub repo from CRAN and made the quick change. I will alert the maintainer. In the meantime, you can install the updated package with:
remotes::install_github("davidaarmstrong/deming")
After that and restarting R, your example will work:
library(deming)
x<-c(1,2,3,4,5,6,7,8,9)
y<-c(2,3,4,5,6,7,8,9,10)
wt<-c(1,1,2,1,1,1,1,2,1)
exampledata<-data.frame(x,y,wt)
stdpat=c(0,2.1,0,1)
example<-deming(x~y+0,data=exampledata,weights=wt,cv=TRUE,stdpat=stdpat)
example
#>
#> Call:
#> deming(formula = x ~ y + 0, data = exampledata, weights = wt, cv = TRUE, stdpat = stdpat)
#>
#> n= 9
#> Coef se(coef) lower 0.95 upper 0.95
#> Intercept 0.0000000 0.0000000 0.0000000 0.0000000
#> Slope 0.7748823 0.0510434 0.6748391 0.8749255
#>
#> Scale= NULL
Created on 2025-11-03 with reprex v2.1.1