2

I would like to perform a Deming regression through the origin including a ratio of variance of 2.1 between the x and y variable, as the data I am working with includes measurement errors in the x and y variable (i.e. the dependent and the independent variable). I am using R (in R Studio). Therefore, I want to use the function deming() from the package deming as this function allows to set the intercept to zero and to include a fixed ratio of variance through setting cv=TRUE and defining the stdpat. However, as soon as I set cv to TRUE (instead of FALSE) I receive an error and I cannot solve it. How can I solve this error message? I created an dataframe to reproduce the error message that I get with my original data.

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)

The (translated) error message I get is:

Error in if (abs(oldbeta - beta)/(abs(beta) + tol) <= tol) break : 
  Condition has length > 1

1 Answer 1

3

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.