r - 使用拟合模型从 Y 值预测 X 值

标签 r lm

这个问题在这里已经有了答案:





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') 

example plot
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) 

example plot 2

最佳答案

正如 this answer 中所暗示的那样您应该可以使用 approx()为你的任务。例如。像这样:

xval <- approx(x = fit$fitted.values, y = x, xout = 30)$y

points(xval, 30, col = "blue", lwd = 5)

给你:

enter image description here

关于r - 使用拟合模型从 Y 值预测 X 值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43322568/

相关文章:

r - 值为0时线性模型中的错误

r - ggplot stacked bar - 隐藏标签但保留标签定位

r - 垂直合并 2 个变量 R tidyverse

r - 按 id 对数据框中的一个变量进行排序

r - 组合模型的两个 MuMIn 子集的项以在 R 中创建新的子集

r - pglm 不使用本地环境中的任何变量

r - 如何在R中的一个输入参数上最小化一个函数

r - 如何在 quantmod 包中创建技术指标

r - 使用 cbind 将 R lm 回归导出到文本

r - CVlm {DAAG} : setting printit = FALSE causes an issue - object 'sumss' not found