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
?
Asked
Active
Viewed 1,922 times
7
![](../../users/profiles/3420448.webp)
MaMu
- 1,076
- 2
- 7
- 24
-
7Maybe remove the intercept parameter with `- 1` in the formula, and the subtract the known intercept from your data? – BrodieG Feb 13 '15 at 14:40
-
Thank you @BrodieG. But as Ben pointed out `offset(k)` would be more explicit. – MaMu Feb 16 '15 at 11:26
1 Answers
9
lm(y~-1+x+I(x^2)+offset(k))
should do it.
-1
suppresses the otherwise automatically added intercept termx
adds a linear termI(x^2)
adds a quadratic term; theI()
is required so that R interprets^2
as squaring, rather than taking an interaction betweenx
and itself (which by formula rules would be equivalent tox
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=...
![](../../users/profiles/190277.webp)
Ben Bolker
- 173,430
- 21
- 312
- 389
-
`offset` looks like a great function to do this with; the help for it is a little confusing: "An offset is a term to be added to a linear predictor, such as in a generalised linear model, with known coefficient 1 rather than an estimated coefficient." Known coefficient 1? This must be a typo. – kasterma Feb 13 '15 at 16:03
-
1no, it's just confusing. The point is that the equation is then `y=b0+b1*x1+b2*x2+1*offset` rather than `y=b0+b1*x1+b2*x2+b3*offset` (where `b3` would be the "estimated coefficient"). – Ben Bolker Feb 13 '15 at 16:14
-
Thanks @BenBolker. It works. But to tell the truth, I don't fully understand this formula and I can't find help from R. Could you explain each element of this formula or direct me to where I can find the explanation? – MaMu Feb 16 '15 at 11:24
-
Ohh... I haven't noticed that you edited the answer till now. thank you very much. – MaMu Feb 19 '15 at 17:50
-
Worked perfectly for me .... except I did have to generate a k vector as you suggest – guero64 Apr 15 '20 at 19:23