2014-10-02 59 views
2

我有一個線性單變量回歸的多項式模型,我必須預測xy對於新的數據集。 該模型是一樣的東西使用多項式模型進行逆預測

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

我在一個相當狹窄的範圍X,其中模型是單調的使用這種模式。 我想我不能使用功能predict.lm ...有什麼建議? 謝謝。

+0

一個快速的方法,但也許不是最好的是收集來自x和y的樣本,然後擬合模型,其中輸入爲y – Donbeo 2014-10-02 11:23:39

+1

@Donbeo不,這是一個不同的模型(關於誤差項不同的假設)。 – Roland 2014-10-02 11:30:26

回答

2

這是基於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)) 

resulting plot

#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