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

alexmulo picture alexmulo · Aug 22, 2012 · Viewed 14.1k times · Source

I have this data set:

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 know the predicted values of x if I have new y values, e.g. ynew <- c(5.5, 4.5, 3.5), but if I use the predict() function, it calculates only new y values.

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

Answer

Ben Bolker picture Ben Bolker · Aug 22, 2012

I think you just have to use the algebra to invert y=a+b*x to x=(y-a)/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 a nonlinear regression might be better ...

enter image description here