Fitting a polynomial model with a reciprocal in R

Hello folks,

I'm trying to fit a model with the written equation:

y = (a + bx + cx^2 + dx^3) / x

I'm aware to do a polynomial I'd do something like:

lm(y ~ poly(x, 4), dat)

But I'm drawing a blank on how to incorporate that divided term. Would I need to pre-calculate 1/x?

Feel free to demo on iris or mtcars or such like, or just write out the sort of "pseudo-code" without data as I have above!

If you are wondering, I am fitting that specific equation to replicate something that's done in my industry, but on my data, so that I can compare the two.


I assume you meant the powers to be 0, 1, 2, 3

y= a(1/x) + b + cx + dx^2

Yes, apologies - I've rewritten it in the OP. Copy-pasting mistake! It should read;

y = (a + bx + cx^2 + dx^3) / x

is a nice linear regression that will give you what you want then.

myfunc <- function(a,b,c,d,x){

# myfunc(1,2,3,4,1:10)

df1 <- data.frame(x=1:10)
df1$y <-myfunc(10,9,8,7,df1$x)

lm1 <- lm(y ~ I(1/x) + I(x)+I(x^2),data=df1)
(Intercept)      I(1/x)        I(x)      I(x^2) 
          9          10           8           7

Marvellous - thank you very much. I did not know of I()!

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.