Search code examples
rlinear-regressionpolynomialsintercept

Fitting a polynomial with a known intercept


I am using lm(y~poly(x,2)) to fit a second-order polynomial to my data. But I just couldn't find a way to specify a known intercept value. How can I fit a polynomial model with a known intercept value (say 'k') using lm?


Solution

  •  lm(y~-1+x+I(x^2)+offset(k))
    

    should do it.

    • -1 suppresses the otherwise automatically added intercept term
    • x adds a linear term
    • I(x^2) adds a quadratic term; the I() is required so that R interprets ^2 as squaring, rather than taking an interaction between x and itself (which by formula rules would be equivalent to x alone)
    • offset(k) adds the known constant intercept

    I don't know whether poly(x,2)-1 would work to eliminate the intercept; you can try it and see. Subtracting the offset from your data should work fine, but offset(k) might be slightly more explicit. You might have to make k a vector (i.e. replicate it to the length of the data set, or better include it as a column in the data set and pass the data with data=...