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?
I think you just have to use the algebra to invert
y=a+b*x
tox=(y-a)/b
:Looking at your 'data' here, I think a nonlinear regression might be better ...
If your relationship is nonmonotone or if you have multiple predictor values then there can be multiple x-values for a given y-value and you need to decide how to deal with that.
One option that could be slow (and may be the method used in the other packages mentioned) is to use the uniroot function:
You could use the
TkPredict
function from theTeachingDemos
package to eyeball a solution.Or you could get a fairly quick approximation by generating a lot of predicted points, then feeding them to the
approxfun
orsplinfun
functions to produce the approximations:Since this is a typical problem in chemistry (predict values from a calibration), package
chemCal
providesinverse.predict
. However, this function is limited to "univariate model object[s] of class lm or rlm with model formula y ~ x or y ~ x - 1."Warning: This function is quite slow and not suitable, if you need to inverse.predict a large number of values.
If I remember correctly, the neg. SEs occur because the function expects the slope to be always positive. Absolute values of SE should still be correct.