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

classic Classic list List threaded Threaded
9 messages Options
Reply | Threaded
Open this post in threaded view
|

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

Luigi
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

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: How to predict/interpolate new Y given knwon Xs and Ys?

Jeff Newmiller
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 <[hidden email]> 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
>
>______________________________________________
>[hidden email] 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.

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: How to predict/interpolate new Y given knwon Xs and Ys?

Luigi
I see, so predict is mono-directional: only gives x with a know y but
not the other way round. Thank you

On Tue, Jan 26, 2021 at 10:20 AM Jeff Newmiller
<[hidden email]> wrote:

>
> 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 <[hidden email]> 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
> >
> >______________________________________________
> >[hidden email] 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.



--
Best regards,
Luigi

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: How to predict/interpolate new Y given knwon Xs and Ys?

Rui Barradas
In reply to this post by Luigi
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
>
> ______________________________________________
> [hidden email] 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.
>

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: How to predict/interpolate new Y given knwon Xs and Ys?

Luigi
In reply to this post by Jeff Newmiller
Dear Jeff,
I am not sure if I understood the procedure properly but it looks like it works:
```
Y <-c(1.301030,  1.602060,  1.903090,  2.204120,  2.505150,  2.806180,
 3.107210,  3.408240,  3.709270,
4.010300,  4.311330,  4.612360,  4.913390,  5.214420,  5.515450,
5.816480,  6.117510,  6.418540,
6.719570,  7.020599,  7.321629,  7.622658,  7.923686,  8.224713,
8.525735,  8.826751,  9.127752,
9.428723,  9.729637, 10.030434, 10.330998, 10.631096, 10.930265,
11.227580, 11.521213, 11.807577,
12.079787, 12.325217, 12.523074, 12.647915, 12.693594, 12.698904,
12.698970, 12.698970, 12.698970)
X <- 1:45
plot(Y~X)
raw_value <- predict(lm(X[1:39]~Y[1:39]), newdata = data.frame(Y=6))
x <- unname(raw_value[!is.na(raw_value)]) # x= 16.62995
points(x, 6, pch = 16)
```
Here I used the points 1:39 because afterward there is a bend. But I
am not clear why I need to use `lm(X~Y),` instead of `lm(Y~X)`.
Thank you

On Tue, Jan 26, 2021 at 10:20 AM Jeff Newmiller
<[hidden email]> wrote:

>
> 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 <[hidden email]> 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
> >
> >______________________________________________
> >[hidden email] 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.



--
Best regards,
Luigi

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: How to predict/interpolate new Y given knwon Xs and Ys?

aBBy Spurdle, ⍺XY
I got 16.60964.
Your curve is not linear up to the 39th point.
And as your points appear to be deterministic and nonlinear, splines
are likely to be easier to use.

Here's a base-only solution (if you don't like my kubik suggestion):

g <- splinefun (X, Y)
f <- function (x) g (x) - 6
uniroot (f, c (1, 45) )$root


On Wed, Jan 27, 2021 at 10:30 PM Luigi Marongiu
<[hidden email]> wrote:

>
> Dear Jeff,
> I am not sure if I understood the procedure properly but it looks like it works:
> ```
> Y <-c(1.301030,  1.602060,  1.903090,  2.204120,  2.505150,  2.806180,
>  3.107210,  3.408240,  3.709270,
> 4.010300,  4.311330,  4.612360,  4.913390,  5.214420,  5.515450,
> 5.816480,  6.117510,  6.418540,
> 6.719570,  7.020599,  7.321629,  7.622658,  7.923686,  8.224713,
> 8.525735,  8.826751,  9.127752,
> 9.428723,  9.729637, 10.030434, 10.330998, 10.631096, 10.930265,
> 11.227580, 11.521213, 11.807577,
> 12.079787, 12.325217, 12.523074, 12.647915, 12.693594, 12.698904,
> 12.698970, 12.698970, 12.698970)
> X <- 1:45
> plot(Y~X)
> raw_value <- predict(lm(X[1:39]~Y[1:39]), newdata = data.frame(Y=6))
> x <- unname(raw_value[!is.na(raw_value)]) # x= 16.62995
> points(x, 6, pch = 16)
> ```
> Here I used the points 1:39 because afterward there is a bend. But I
> am not clear why I need to use `lm(X~Y),` instead of `lm(Y~X)`.
> Thank you
>
> On Tue, Jan 26, 2021 at 10:20 AM Jeff Newmiller
> <[hidden email]> wrote:
> >
> > 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 <[hidden email]> 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
> > >
> > >______________________________________________
> > >[hidden email] 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.
>
>
>
> --
> Best regards,
> Luigi
>
> ______________________________________________
> [hidden email] 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.

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: How to predict/interpolate new Y given knwon Xs and Ys?

Luigi
That is right, I removed the points above 39, and also got 16.60964,
which looks good to me. Splines is good and I will use it for
non-linear interpolation. For now I used linear conversion to keep it
simple but looks to me it is getting out of hand. I simply need to do
the following interpolation:
```
Y <-c(1.301030,  1.602060,  1.903090,  2.204120,  2.505150,  2.806180,
# log transformation of the original data
        3.107210,  3.408240,  3.709270,
       4.010300,  4.311330,  4.612360,  4.913390,  5.214420,  5.515450,
       5.816480,  6.117510,  6.418540,
       6.719570,  7.020599,  7.321629,  7.622658,  7.923686,  8.224713,
       8.525735,  8.826751,  9.127752,
       9.428723,  9.729637, 10.030434, 10.330998, 10.631096, 10.930265,
       11.227580, 11.521213, 11.807577,
       12.079787, 12.325217, 12.523074, 12.647915, 12.693594, 12.698904,
       12.698970, 12.698970, 12.698970)
X <- 1:45
plot(Y~X)
points(16.60964, 6, pch=16)
segments(0, 6, 16.60964, 6)
arrows(16.60964, 6, 16.60964, 1)
# given Y find X
```
How shall I use predict then? The use of other packages when predict
can do it would be overkill...
Thank you

On Wed, Jan 27, 2021 at 11:32 AM Abby Spurdle <[hidden email]> wrote:

>
> I got 16.60964.
> Your curve is not linear up to the 39th point.
> And as your points appear to be deterministic and nonlinear, splines
> are likely to be easier to use.
>
> Here's a base-only solution (if you don't like my kubik suggestion):
>
> g <- splinefun (X, Y)
> f <- function (x) g (x) - 6
> uniroot (f, c (1, 45) )$root
>
>
> On Wed, Jan 27, 2021 at 10:30 PM Luigi Marongiu
> <[hidden email]> wrote:
> >
> > Dear Jeff,
> > I am not sure if I understood the procedure properly but it looks like it works:
> > ```
> > Y <-c(1.301030,  1.602060,  1.903090,  2.204120,  2.505150,  2.806180,
> >  3.107210,  3.408240,  3.709270,
> > 4.010300,  4.311330,  4.612360,  4.913390,  5.214420,  5.515450,
> > 5.816480,  6.117510,  6.418540,
> > 6.719570,  7.020599,  7.321629,  7.622658,  7.923686,  8.224713,
> > 8.525735,  8.826751,  9.127752,
> > 9.428723,  9.729637, 10.030434, 10.330998, 10.631096, 10.930265,
> > 11.227580, 11.521213, 11.807577,
> > 12.079787, 12.325217, 12.523074, 12.647915, 12.693594, 12.698904,
> > 12.698970, 12.698970, 12.698970)
> > X <- 1:45
> > plot(Y~X)
> > raw_value <- predict(lm(X[1:39]~Y[1:39]), newdata = data.frame(Y=6))
> > x <- unname(raw_value[!is.na(raw_value)]) # x= 16.62995
> > points(x, 6, pch = 16)
> > ```
> > Here I used the points 1:39 because afterward there is a bend. But I
> > am not clear why I need to use `lm(X~Y),` instead of `lm(Y~X)`.
> > Thank you
> >
> > On Tue, Jan 26, 2021 at 10:20 AM Jeff Newmiller
> > <[hidden email]> wrote:
> > >
> > > 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 <[hidden email]> 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
> > > >
> > > >______________________________________________
> > > >[hidden email] 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.
> >
> >
> >
> > --
> > Best regards,
> > Luigi
> >
> > ______________________________________________
> > [hidden email] 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.



--
Best regards,
Luigi

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: How to predict/interpolate new Y given knwon Xs and Ys?

Jeff Newmiller
Your piecewise-linear function Y(X) is not invertible as a whole so there is no general solution per algebra.

You keep saying predict can do this, but as I pointed out and Rui ventured to expand the math on, predict cannot calculate an inverse even if you swap the inputs. And as Abby pointed out, you are abusing lm by applying it to data with systematic errors (negligible random residuals).

If you know which portion of the curve you want a result in, you can use a subset of your data to create an invertible piecewise linear function::

approx( Y[1:39], X[1:39], 6 )$y

On January 27, 2021 2:46:40 AM PST, Luigi Marongiu <[hidden email]> wrote:

>That is right, I removed the points above 39, and also got 16.60964,
>which looks good to me. Splines is good and I will use it for
>non-linear interpolation. For now I used linear conversion to keep it
>simple but looks to me it is getting out of hand. I simply need to do
>the following interpolation:
>```
>Y <-c(1.301030,  1.602060,  1.903090,  2.204120,  2.505150,  2.806180,
># log transformation of the original data
>        3.107210,  3.408240,  3.709270,
>       4.010300,  4.311330,  4.612360,  4.913390,  5.214420,  5.515450,
>       5.816480,  6.117510,  6.418540,
>       6.719570,  7.020599,  7.321629,  7.622658,  7.923686,  8.224713,
>       8.525735,  8.826751,  9.127752,
>       9.428723,  9.729637, 10.030434, 10.330998, 10.631096, 10.930265,
>       11.227580, 11.521213, 11.807577,
>      12.079787, 12.325217, 12.523074, 12.647915, 12.693594, 12.698904,
>       12.698970, 12.698970, 12.698970)
>X <- 1:45
>plot(Y~X)
>points(16.60964, 6, pch=16)
>segments(0, 6, 16.60964, 6)
>arrows(16.60964, 6, 16.60964, 1)
># given Y find X
>```
>How shall I use predict then? The use of other packages when predict
>can do it would be overkill...
>Thank you
>
>On Wed, Jan 27, 2021 at 11:32 AM Abby Spurdle <[hidden email]>
>wrote:
>>
>> I got 16.60964.
>> Your curve is not linear up to the 39th point.
>> And as your points appear to be deterministic and nonlinear, splines
>> are likely to be easier to use.
>>
>> Here's a base-only solution (if you don't like my kubik suggestion):
>>
>> g <- splinefun (X, Y)
>> f <- function (x) g (x) - 6
>> uniroot (f, c (1, 45) )$root
>>
>>
>> On Wed, Jan 27, 2021 at 10:30 PM Luigi Marongiu
>> <[hidden email]> wrote:
>> >
>> > Dear Jeff,
>> > I am not sure if I understood the procedure properly but it looks
>like it works:
>> > ```
>> > Y <-c(1.301030,  1.602060,  1.903090,  2.204120,  2.505150,
>2.806180,
>> >  3.107210,  3.408240,  3.709270,
>> > 4.010300,  4.311330,  4.612360,  4.913390,  5.214420,  5.515450,
>> > 5.816480,  6.117510,  6.418540,
>> > 6.719570,  7.020599,  7.321629,  7.622658,  7.923686,  8.224713,
>> > 8.525735,  8.826751,  9.127752,
>> > 9.428723,  9.729637, 10.030434, 10.330998, 10.631096, 10.930265,
>> > 11.227580, 11.521213, 11.807577,
>> > 12.079787, 12.325217, 12.523074, 12.647915, 12.693594, 12.698904,
>> > 12.698970, 12.698970, 12.698970)
>> > X <- 1:45
>> > plot(Y~X)
>> > raw_value <- predict(lm(X[1:39]~Y[1:39]), newdata =
>data.frame(Y=6))
>> > x <- unname(raw_value[!is.na(raw_value)]) # x= 16.62995
>> > points(x, 6, pch = 16)
>> > ```
>> > Here I used the points 1:39 because afterward there is a bend. But
>I
>> > am not clear why I need to use `lm(X~Y),` instead of `lm(Y~X)`.
>> > Thank you
>> >
>> > On Tue, Jan 26, 2021 at 10:20 AM Jeff Newmiller
>> > <[hidden email]> wrote:
>> > >
>> > > 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
><[hidden email]> 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
>> > > >
>> > > >______________________________________________
>> > > >[hidden email] 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.
>> >
>> >
>> >
>> > --
>> > Best regards,
>> > Luigi
>> >
>> > ______________________________________________
>> > [hidden email] 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.

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: How to predict/interpolate new Y given knwon Xs and Ys?

Luigi
Thank you for the clarification.

On Wed, Jan 27, 2021 at 3:32 PM Jeff Newmiller <[hidden email]> wrote:

>
> Your piecewise-linear function Y(X) is not invertible as a whole so there is no general solution per algebra.
>
> You keep saying predict can do this, but as I pointed out and Rui ventured to expand the math on, predict cannot calculate an inverse even if you swap the inputs. And as Abby pointed out, you are abusing lm by applying it to data with systematic errors (negligible random residuals).
>
> If you know which portion of the curve you want a result in, you can use a subset of your data to create an invertible piecewise linear function::
>
> approx( Y[1:39], X[1:39], 6 )$y
>
> On January 27, 2021 2:46:40 AM PST, Luigi Marongiu <[hidden email]> wrote:
> >That is right, I removed the points above 39, and also got 16.60964,
> >which looks good to me. Splines is good and I will use it for
> >non-linear interpolation. For now I used linear conversion to keep it
> >simple but looks to me it is getting out of hand. I simply need to do
> >the following interpolation:
> >```
> >Y <-c(1.301030,  1.602060,  1.903090,  2.204120,  2.505150,  2.806180,
> ># log transformation of the original data
> >        3.107210,  3.408240,  3.709270,
> >       4.010300,  4.311330,  4.612360,  4.913390,  5.214420,  5.515450,
> >       5.816480,  6.117510,  6.418540,
> >       6.719570,  7.020599,  7.321629,  7.622658,  7.923686,  8.224713,
> >       8.525735,  8.826751,  9.127752,
> >       9.428723,  9.729637, 10.030434, 10.330998, 10.631096, 10.930265,
> >       11.227580, 11.521213, 11.807577,
> >      12.079787, 12.325217, 12.523074, 12.647915, 12.693594, 12.698904,
> >       12.698970, 12.698970, 12.698970)
> >X <- 1:45
> >plot(Y~X)
> >points(16.60964, 6, pch=16)
> >segments(0, 6, 16.60964, 6)
> >arrows(16.60964, 6, 16.60964, 1)
> ># given Y find X
> >```
> >How shall I use predict then? The use of other packages when predict
> >can do it would be overkill...
> >Thank you
> >
> >On Wed, Jan 27, 2021 at 11:32 AM Abby Spurdle <[hidden email]>
> >wrote:
> >>
> >> I got 16.60964.
> >> Your curve is not linear up to the 39th point.
> >> And as your points appear to be deterministic and nonlinear, splines
> >> are likely to be easier to use.
> >>
> >> Here's a base-only solution (if you don't like my kubik suggestion):
> >>
> >> g <- splinefun (X, Y)
> >> f <- function (x) g (x) - 6
> >> uniroot (f, c (1, 45) )$root
> >>
> >>
> >> On Wed, Jan 27, 2021 at 10:30 PM Luigi Marongiu
> >> <[hidden email]> wrote:
> >> >
> >> > Dear Jeff,
> >> > I am not sure if I understood the procedure properly but it looks
> >like it works:
> >> > ```
> >> > Y <-c(1.301030,  1.602060,  1.903090,  2.204120,  2.505150,
> >2.806180,
> >> >  3.107210,  3.408240,  3.709270,
> >> > 4.010300,  4.311330,  4.612360,  4.913390,  5.214420,  5.515450,
> >> > 5.816480,  6.117510,  6.418540,
> >> > 6.719570,  7.020599,  7.321629,  7.622658,  7.923686,  8.224713,
> >> > 8.525735,  8.826751,  9.127752,
> >> > 9.428723,  9.729637, 10.030434, 10.330998, 10.631096, 10.930265,
> >> > 11.227580, 11.521213, 11.807577,
> >> > 12.079787, 12.325217, 12.523074, 12.647915, 12.693594, 12.698904,
> >> > 12.698970, 12.698970, 12.698970)
> >> > X <- 1:45
> >> > plot(Y~X)
> >> > raw_value <- predict(lm(X[1:39]~Y[1:39]), newdata =
> >data.frame(Y=6))
> >> > x <- unname(raw_value[!is.na(raw_value)]) # x= 16.62995
> >> > points(x, 6, pch = 16)
> >> > ```
> >> > Here I used the points 1:39 because afterward there is a bend. But
> >I
> >> > am not clear why I need to use `lm(X~Y),` instead of `lm(Y~X)`.
> >> > Thank you
> >> >
> >> > On Tue, Jan 26, 2021 at 10:20 AM Jeff Newmiller
> >> > <[hidden email]> wrote:
> >> > >
> >> > > 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
> ><[hidden email]> 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
> >> > > >
> >> > > >______________________________________________
> >> > > >[hidden email] 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.
> >> >
> >> >
> >> >
> >> > --
> >> > Best regards,
> >> > Luigi
> >> >
> >> > ______________________________________________
> >> > [hidden email] 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.



--
Best regards,
Luigi

______________________________________________
[hidden email] 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.