[R] How to predict/interpolate new Y given knwon Xs and Ys?

Rui Barradas ru|pb@rr@d@@ @end|ng |rom @@po@pt
Tue Jan 26 10:50:25 CET 2021


Hello,

You can predict y on x, not the other way around, like you are doing in 
the second call to predict.lm.

The 10 values you are getting are the predicted values on the original x 
values, just see that x=7.5 gives ypred=30, right in the middle of x=7 
and x=8 -> ypred=29 and ypred=31.

As for the inverse regression, how do you account for the errors? In 
linear regression the only rv is the errors vector, the inverse of

y = a + b*x + e

is not

x = (y - a)/b

though you can write a function that computes this value:

pred_x <- function(model, newdata){
   beta <- coef(model)
   y <- newdata[[1]]
   x <- (y - beta[1])/beta[2]
   unname(x)
}
pred_x(model, data.frame(y = 26))
#[1] 5.5


There is a CRAN package, investr that computes the standard errors:

investr::calibrate(model, y0 = 26)
#estimate    lower    upper
#     5.5      5.5      5.5


See the decumentation in [1]

[1] https://CRAN.R-project.org/package=investr


Hope this helps,

Rui Barradas

Às 09:11 de 26/01/21, Luigi Marongiu escreveu:
> Hello,
> I have a series of x/y and a model. I can interpolate a new value of x
> using this model, but I get funny results if I give the y and look for
> the correspondent x:
> ```
>> x = 1:10
>> y = 2*x+15
>> model <- lm(y~x)
>> predict(model, data.frame(x=7.5))
>   1
> 30
>> predict(model, data.frame(y=26))
>   1  2  3  4  5  6  7  8  9 10
> 17 19 21 23 25 27 29 31 33 35
> Warning message:
> 'newdata' had 1 row but variables found have 10 rows
>> data.frame(x=7.5)
>      x
> 1 7.5
>> data.frame(y=26)
>     y
> 1 26
> ```
> what is the correct syntax?
> Thank you
> Luigi
> 
> ______________________________________________
> R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>



More information about the R-help mailing list