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
?
To force the fitted curve go through Origin (0,0), you can just fix the intercept to 0 for a linear or polynomial model. To force the fitted curve go through a specific point in raw data, you can set a higher weight for the point.
The first degree polynomial equation. is a line with slope a. A line will connect any two points, so a first degree polynomial equation is an exact fit through any two points with distinct x coordinates.
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; 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 interceptI 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=...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With