Prediction when using orthogonal polynomials in regression

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

Prediction when using orthogonal polynomials in regression

Ajay Shah
Folks,

I'm doing fine with using orthogonal polynomials in a regression context:

  # We will deal with noisy data from the d.g.p. y = sin(x) + e
  x <- seq(0, 3.141592654, length.out=20)
  y <- sin(x) + 0.1*rnorm(10)
  d <- lm(y ~ poly(x, 4))
  plot(x, y, type="l"); lines(x, d$fitted.values, col="blue") # Fits great!
  all.equal(as.numeric(d$coefficients[1] + m %*% d$coefficients[2:5]),
            as.numeric(d$fitted.values))

What I would like to do now is to apply the estimated model to do
prediction for a new set of x points e.g.
  xnew <- seq(0,5,.5)

We know that the predicted values should be roughly sin(xnew). What I
don't know is: how do I use the object `d' to make predictions for
xnew?

--
Ajay Shah                                      http://www.mayin.org/ajayshah 
[hidden email]                             http://ajayshahblog.blogspot.com
<*(:-? - wizard who doesn't know the answer.

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Reply | Threaded
Open this post in threaded view
|

Re: Prediction when using orthogonal polynomials in regression

Achim Zeileis
On Thu, 26 Jan 2006 22:10:23 +0530 Ajay Narottam Shah wrote:

> Folks,
>
> I'm doing fine with using orthogonal polynomials in a regression
> context:
>
>   # We will deal with noisy data from the d.g.p. y = sin(x) + e
>   x <- seq(0, 3.141592654, length.out=20)
>   y <- sin(x) + 0.1*rnorm(10)
>   d <- lm(y ~ poly(x, 4))
>   plot(x, y, type="l"); lines(x, d$fitted.values, col="blue")

fitted(d) is usually the preferred way of accessing the fitted values
(although equivalent in this particular case).

> great! all.equal(as.numeric(d$coefficients[1] + m %*% d$coefficients
> [2:5]), as.numeric(d$fitted.values))
>
> What I would like to do now is to apply the estimated model to do
> prediction for a new set of x points e.g.
>   xnew <- seq(0,5,.5)
>
> We know that the predicted values should be roughly sin(xnew). What I
> don't know is: how do I use the object `d' to make predictions for
> xnew?

Use predict:
  predict(d, data.frame(x = xnew))
which is pretty evocative.

Best,
Z

> --
> Ajay Shah
> http://www.mayin.org/ajayshah
> [hidden email]
> http://ajayshahblog.blogspot.com <*(:-? - wizard who doesn't know the
> answer.
>
> ______________________________________________
> [hidden email] mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide!
> http://www.R-project.org/posting-guide.html
>

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Reply | Threaded
Open this post in threaded view
|

Re: Prediction when using orthogonal polynomials in regression

Gabor Grothendieck
In reply to this post by Ajay Shah
On 1/26/06, Ajay Narottam Shah <[hidden email]> wrote:
> Folks,
>
> I'm doing fine with using orthogonal polynomials in a regression context:
>
>  # We will deal with noisy data from the d.g.p. y = sin(x) + e
>  x <- seq(0, 3.141592654, length.out=20)

This has already been answered but note that pi is a built in variable
in R.

>  y <- sin(x) + 0.1*rnorm(10)
>  d <- lm(y ~ poly(x, 4))
>  plot(x, y, type="l"); lines(x, d$fitted.values, col="blue") # Fits great!
>  all.equal(as.numeric(d$coefficients[1] + m %*% d$coefficients[2:5]),
>            as.numeric(d$fitted.values))
>
> What I would like to do now is to apply the estimated model to do
> prediction for a new set of x points e.g.
>  xnew <- seq(0,5,.5)
>
> We know that the predicted values should be roughly sin(xnew). What I
> don't know is: how do I use the object `d' to make predictions for
> xnew?
>
> --
> Ajay Shah                                      http://www.mayin.org/ajayshah
> [hidden email]                             http://ajayshahblog.blogspot.com
> <*(:-? - wizard who doesn't know the answer.
>
> ______________________________________________
> [hidden email] mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
>

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html