Luigi Marongiu
2021-Jan-26 09:11 UTC
[R] How to predict/interpolate new Y given knwon Xs and Ys?
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
Jeff Newmiller
2021-Jan-26 09:20 UTC
[R] How to predict/interpolate new Y given knwon Xs and Ys?
model2 <- lm( x~y ) predict(model2, data.frame(y=26)) model2 is however not the inverse of model... if you need that then you need to handle that some other way than using predict, such as an invertible monotonic spline (or in this case a little algebra). On January 26, 2021 1:11:39 AM PST, Luigi Marongiu <marongiu.luigi at gmail.com> wrote:>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 at 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.-- Sent from my phone. Please excuse my brevity.
Rui Barradas
2021-Jan-26 09:50 UTC
[R] How to predict/interpolate new Y given knwon Xs and Ys?
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 at 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. >