3
$\begingroup$

I'm trying to model a simple system, with data:

zoom    FOV
0       48.00069715
4000    30.9484929
8000    18.73417224
12000   9.690813595
16000   4.286154297

Doing

> model.lin = lm(data = zFOV, formula = FOV ~ zoom)
> coef(model.lin)

gives a reasonable

 (Intercept)         zoom 
44.069419039 -0.002717169 

which I interpret as $\small FOV = 44 -0.0027*zoom$

However

> model.poly = lm(data = zFOV, formula = FOV ~ poly(zoom,2))
> coef(model.poly)

gives

   (Intercept) poly(zoom, 2)1 poly(zoom, 2)2 
  22.33207      -34.36977        7.07335 

Which I understand to mean $\small FOV = 22.3 - 34.4 * zoom + 7.07 * zoom^2$, which gives complete garbage.

What am I misunderstanding here?

$\endgroup$
1

2 Answers 2

6
$\begingroup$

To get what you want, you can type

poly(zoom, 2, raw = TRUE)

Without the raw = TRUE option, R orthogonalizes and normalizes the basis polynomials. This has some advantages (e.g. in studying p values, numerical stability) but also, as you recognized, the disadvantage of complicated interpretation of coefficients.

$\endgroup$
2
  • 2
    $\begingroup$ To get the advantages of orthogonal polynomials with simplified interpretation you need to convert coefficients from orthogonal to raw form. I gave an inelegant function to do that job here stackoverflow.com/questions/31457230/… $\endgroup$
    – user20637
    Commented Sep 18, 2015 at 15:05
  • $\begingroup$ That is very useful! $\endgroup$
    – Michael M
    Commented Sep 18, 2015 at 15:30
2
$\begingroup$

One easy way to do this is to define our own quadratic term and just use the lm function directly on that

zoom <- c(0,4000,8000,12000,16000)
FOV <- c(48.00069715,30.9484929,18.73417224,9.690813595,4.286154297)

plot(zoom,FOV)

datFr <- data.frame('FOV'=FOV,'zoom'=zoom, 'zoom2'=zoom^2)

model.lin = lm(data = datFr, formula = FOV ~ zoom+zoom2)
coefs <- coef(model.lin)

predVecX <-seq(min(zoom),max(zoom),len=100)
predVecY <- coefs[1]+coefs[2]*predVecX+coefs[3]*predVecX^2

plot(zoom,FOV)
lines(predVecX,predVecY,col='red')
title('Fitted quadratic')

Now the fit looks nice:

Quadratic fit

If you read the description from ?poly it says:

Returns or evaluates orthogonal polynomials of degree 1 to degree over the specified set of points x: these are all orthogonal to the constant polynomial of degree 0. Alternatively, evaluate raw polynomials.

So this is not generating the formula you want, as you were trying to do. You need to have the raw argument if you would like to use it like you intend.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.