【问题标题】:Inverse prediction with a polynomial model使用多项式模型进行逆预测
【发布时间】:2014-11-27 08:54:50
【问题描述】:

我有一个用于线性单变量回归的多项式模型,我必须根据 y 预测 x 以获得新数据集。 该模型类似于

f22<- lm(y~I(x^2.0) + I(x^3.0) + I(x^0.5))

我在一个很窄的x范围内使用这个模型,这个模型是单调的。 我想我不能使用函数 predict.lm... 有什么建议吗? 谢谢。

【问题讨论】:

  • 一种快速但可能不是最好的方法是从 x 和 y 收集样本,然后拟合输入为 y 的模型
  • @Donbeo 不,这是一个不同的模型(关于错误项的不同假设)。

标签: r regression prediction


【解决方案1】:

这里是基于uniroot的解决方案:

#some data
x <- 1:100
set.seed(42)
y <- 1.8*x^2 - 0.015*x^3 + 0.4*x^0.5 + rnorm(100)
plot(y~x)

#fitting the model
f22<- lm(y~I(x^2.0) + I(x^3.0) + I(x^0.5), data=data.frame(x, y))
f22

lines(x, predict(f22))

#function to pass to uniroot for inverse prediction
ynew <- 2000 + (1:10)*100 
fun <- function(x1, y1, mod) {
  predict(mod, newdata=data.frame(x=x1)) - y1
}    
#note how the interval is specified
xpred <- sapply(ynew, function(z) uniroot(fun, interval=c(20, 70), mod=f22, y1=z)$root)
#[1] 42.46892 43.86588 45.27242 46.69238 48.12992 49.58967 51.07714 52.59857 54.16165 55.77589

#check if it worked
predict(f22, newdata=data.frame(x=xpred))
#       1        2        3        4        5        6        7        8        9       10 
#2100.000 2200.000 2300.000 2400.001 2500.002 2599.999 2700.000 2800.000 2900.000 3000.000

【讨论】:

    猜你喜欢
    • 2020-08-24
    • 2022-01-24
    • 1970-01-01
    • 2016-02-16
    • 1970-01-01
    • 1970-01-01
    • 2021-12-15
    • 2018-03-02
    • 1970-01-01
    相关资源
    最近更新 更多