我有这个数据集:
x <- c(0, 40, 80, 120, 160, 200)
y <- c(6.52, 5.10, 4.43, 3.99, 3.75, 3.60)
Run Code Online (Sandbox Code Playgroud)
我使用lm()以下方法计算了线性模型
model <- lm(y ~ x)
Run Code Online (Sandbox Code Playgroud)
我想知道x如果我有新y值的预测值,例如ynew <- c(5.5, 4.5, 3.5),但如果我使用该predict()函数,它只计算新y值.
x如果我有新y值,如何预测新值?
我认为你只需要使用代数即可y=a+b*x反转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)
Run Code Online (Sandbox Code Playgroud)
看看你在这里的“数据”,我认为非线性回归可能更好......

由于这是化学中的典型问题(通过校准预测值),包chemCal提供了inverse.predict. 但是,此函数仅限于“具有模型公式 y ~ x 或 y ~ x - 1 的 lm 或 rlm 类的单变量模型对象”。
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")
Run Code Online (Sandbox Code Playgroud)
警告:如果您需要逆向预测大量值,则此函数非常慢并且不适合。
如果我没记错的话,是否定的。SE 的出现是因为函数期望斜率始终为正。SE 的绝对值应该仍然是正确的。