简体   繁体   English

如何从线性模型(lm)预测x值

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

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() : 我用lm()计算了一个线性模型:

model <- lm(y ~ x)

I want know the predicted values of x if I have new y values, eg ynew <- c(5.5, 4.5, 3.5) , but if I use the predict() function, it calculates only new y values. 如果我有新的y值,我想知道x的预测值,例如ynew <- c(5.5, 4.5, 3.5) ,但是如果我使用predict()函数,它只计算新的y值。

How can I predict new x values if I have new y values? 如果我有新的y值,我如何预测新的x值?

I think you just have to use the algebra to invert y=a+b*x to x=(ya)/b : 我想你只需要使用代数将y=a+b*x反转为x=(ya)/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 ... 在这里查看您的“数据”,我认为非线性回归可能会更好......

在此输入图像描述

Since this is a typical problem in chemistry (predict values from a calibration), package chemCal provides inverse.predict . 由于这是在化学的一个典型问题(预测从校准值),封装chemCal提供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." 但是,此函数仅限于“类别lm或rlm的单变量模型对象[s],模型公式为y~x或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. 警告:如果您需要inverse.predict大量值,此函数非常慢并且不合适。

If I remember correctly, the neg. 如果我没记错的话,那就是。 SEs occur because the function expects the slope to be always positive. SE发生是因为函数期望斜率始终为正。 Absolute values of SE should still be correct. SE的绝对值应该仍然是正确的。

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. 如果你的关系是非单调的,或者你有多个预测值,那么对于给定的y值,可能有多个x值,你需要决定如何处理它。

One option that could be slow (and may be the method used in the other packages mentioned) is to use the uniroot function: 一个可能很慢的选项(可能是所提到的其他包中使用的方法)是使用uniroot函数:

x <- runif(100, min=-1,max=2)
y <- exp(x) + rnorm(100,0,0.2)

fit <- lm( y ~ poly(x,3), x=TRUE )
(tmp <- uniroot( function(x) predict(fit, data.frame(x=x)) - 4, c(-1, 2) )$root)
library(TeachingDemos)
plot(x,y)
Predict.Plot(fit, 'x', data=data.frame(x=x), add=TRUE, ref.val=tmp)

You could use the TkPredict function from the TeachingDemos package to eyeball a solution. 您可以使用TeachingDemos包中的TkPredict函数来解决问题。

Or you could get a fairly quick approximation by generating a lot of predicted points, then feeding them to the approxfun or splinfun functions to produce the approximations: 或者您可以通过生成大量预测点来获得相当快速的近似值,然后将它们馈送到approxfunsplinfun函数以生成近似值:

tmpx <- seq(min(x), max(x), length.out=250)
tmpy <- predict(fit, data.frame(x=tmpx) )
tmpfun <- splinefun( tmpy, tmpx )
tmpfun(4)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM