这个问题在这里已经有了答案:
predict x values from simple fitting and annoting it in the plot
(1 个回答)
5年前关闭。
我需要预测相应的x
新值 y
使用拟合模型的值。
预测 y
的常见情况来自新的 x
的值使用 predict
的值很简单功能,但我不知道如何做相反的事情。
对于有多个 x
的情况解决方案,我希望获得 x
范围内的所有解决方案值,即 1-10
.和新的y
将始终在 y
的范围内用于拟合模型的值。
请参阅下面的示例代码,我需要在其中找到新的 x 值 ( new_x
)。
x = seq(1:10)
y = c(60,30,40,45,35,20,10,15,25,10)
fit = lm(y ~ poly(x, 3, raw=T))
plot(x, y)
lines(sort(x), predict(fit)[order(x)], col='red')
new_y = 30
new_x = predict(fit, data.frame(y=new_y)) #This line does not work as intended.
编辑 1:反向拟合
拟合反向关系不会给出相同的模型,因为我们得到了不同的模型/拟合线。
rev_fit = lm(x ~ poly(y, 3, raw=T))
plot(x, y)
lines(sort(x), predict(fit)[order(x)], col='red')
lines(predict(rev_fit)[order(y)], sort(y), col='blue', lty=2)
最佳答案
正如 this answer 中所暗示的那样您应该可以使用 approx()
为你的任务。例如。像这样:
xval <- approx(x = fit$fitted.values, y = x, xout = 30)$y
points(xval, 30, col = "blue", lwd = 5)
给你:
关于r - 使用拟合模型从 Y 值预测 X 值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43322568/