How to predict x values ​​from a linear model (lm)

I have this dataset:

x <- c(0, 40, 80, 120, 160, 200) y <- c(6.52, 5.10, 4.43, 3.99, 3.75, 3.60) 

I calculated a linear model using lm() :

 model <- lm(y ~ x) 

I want to know the predicted values ​​of x if I have new y values, for example. ynew <- c(5.5, 4.5, 3.5) , but if I use the predict() function, it only calculates the new y values.

How can I predict new x values ​​if I have new y values?

+7
source share
3 answers

Since this is a typical problem in chemistry (predict values ​​from calibration), the chemCal package provides inverse.predict . However, this function is limited to "a one-dimensional model object [s] of class lm or rlm with the model formula y ~ x or y ~ x - 1."

 x <- c(0, 40, 80, 120, 160, 200) y <- c(6.52, 5.10, 4.43, 3.99, 3.75, 3.60) plot(x,y) model <- lm(y ~ x) abline(model) require(chemCal) ynew <- c(5.5, 4.5, 3.5) xpred<-t(sapply(ynew,function(y) inverse.predict(model,y)[1:2])) # Prediction Standard Error #[1,] 31.43007 -38.97289 #[2,] 104.7669 -36.45131 #[3,] 178.1037 -39.69539 points(xpred[,1],ynew,col="red") 

A warning. This function is rather slow and not suitable if you need the opposite. Imagine a large number of values.

If I remember correctly, negative. SEs occur because the function expects the slope to always be positive. The absolute values ​​of SE must be correct.

+3
source

I think you just need to use algebra to invert y=a+b*x to x=(ya)/b :

 cc <- coef(model) (xnew <- (ynew-cc[1])/cc[2]) # [1] 31.43007 104.76689 178.10372 plot(x,y abline(model) points(xnew,ynew,col=2) 

Looking at your “data” here, I think non-linear regression could be better ...

enter image description here

+4
source

If your relationship is non-monotonic or you have several predictor values, then for a given y value there may be several x values, and you need to decide how to deal with it.

One option, which can be slow (and can be the method used in the other packages mentioned), is to use the uniroot function:

 x <- runif(100, min=-1,max=2) y <- exp(x) + rnorm(100,0,0.2) fit <- lm( y ~ poly(x,3), x=TRUE ) (tmp <- uniroot( function(x) predict(fit, data.frame(x=x)) - 4, c(-1, 2) )$root) library(TeachingDemos) plot(x,y) Predict.Plot(fit, 'x', data=data.frame(x=x), add=TRUE, ref.val=tmp) 

You can use the TkPredict function from the TeachingDemos package to find a solution.

Or you could get a pretty fast approximation by creating many predicted points and then passing them to the approxfun or splinfun to create the approximations:

 tmpx <- seq(min(x), max(x), length.out=250) tmpy <- predict(fit, data.frame(x=tmpx) ) tmpfun <- splinefun( tmpy, tmpx ) tmpfun(4) 
+2
source

All Articles