Why the curve fitting line is disorganized?

35 Views Asked by At

curve fitting

Hello, I want to know why the fitted curve in the picture looks so bad and is not just one smooth line?

plot(df$Q,df$C)
cur=lm(C~I(1/Q),data=df)
lines(df$Q, predict(cur), col = "green")

What am I missing?

1

There are 1 best solutions below

0
IRTFM On

Instead you should sort both the predicted and X values by the X values order. One way to do that is to pass an ordered set of values to the lm function. Then the predicted values will also reflect that ordering:

 plot( df$Q, dg$C )
 cur  <- lm(C~I(1/Q), data=df[order[df$Q), ] )
 lines(df$Q, predict(cur), col = "green")