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?
Likewise, regression also allows us to predict an X value from any given Y, as long as we have the correlation coefficient of X and Y. There are several ways to calculate a linear regression.
In simple linear regression, we predict scores on one variable from the scores on a second variable. The variable we are predicting is called the criterion variable and is referred to as Y. The variable we are basing our predictions on is called the predictor variable and is referred to as X.
The line of regression of Y on X is given by Y = a + bX where a and b are unknown constants known as intercept and slope of the equation. This is used to predict the unknown value of variable Y when value of variable X is known.
Since this is a typical problem in chemistry (predict values from a calibration), package chemCal
provides inverse.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."
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")
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.
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 ...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With