如何从线性模型(lm)预测x值 [英] How to predict x values from a linear model (lm)
问题描述
我有以下数据集:
x <- c(0, 40, 80, 120, 160, 200)
y <- c(6.52, 5.10, 4.43, 3.99, 3.75, 3.60)
我使用lm()
计算了线性模型:
I calculated a linear model using lm()
:
model <- lm(y ~ x)
如果我有新的y
值,例如,我想知道x
的预测值. ynew <- c(5.5, 4.5, 3.5)
,但是如果使用predict()
函数,它将仅计算新的y
值.
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.
如果我有新的y
值,如何预测新的x
值?
How can I predict new x
values if I have new y
values?
推荐答案
由于这是化学中的典型问题(根据校准预测值),因此包chemCal
提供了inverse.predict
.但是,此功能仅限于具有模型公式y〜x或y〜x-1的lm或rlm类的单变量模型对象".
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.
如果我没记错的话,那就是负数.出现SE是因为函数期望斜率始终为正. SE的绝对值仍然应该正确.
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.
这篇关于如何从线性模型(lm)预测x值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!