Limited Least Square Regression - Matlab or R

I perform least squares regression on some data, the function takes the form

y ~ a + b*x 

and I want the regression line to go through a specific point P (x, y) (which is not the beginning). How can i do this?

I use the lm command in R and the base GUI in Matlab. I think I could use the constrOptim command (in R) or translate the origin to point P, but I wonder if there is a specific command there.

I only need a solution for one of these programs, then I can use the coefficients in another.

+4
source share
1 answer

Just center the data accordingly and force the regression through the "source":

 lm(y ~ I(x-x0)-1, offset=rep(y0,nrow(dat)) data=dat) 

You may then need to adjust the capture ratio accordingly.

edited : offset should be a vector of the correct length. Another way to do this:

 set.seed(1) d <- data.frame(x=1:10,y=rnorm(10,mean=1:10,sd=0.1)) x0 <- 3 y0 <- 3 (lm1 <- lm(y ~ I(x-x0)-1, offset=y0, data=data.frame(d,y0))) 

This gives a slope of 1.005. I think the interception will be coef(lm1)*(-y0/x0) .

+5
source

Source: https://habr.com/ru/post/1484207/


All Articles