I have a scatter plot. How can I add non-linear trend line?
|
|
Let's create some data. n <- 100 x <- seq(n) y <- rnorm(n, 50 + 30 * x^(-0.2), 1) Data <- data.frame(x, y) The following shows how you can fit a loess line or the fit of a non-linear regression. plot(y ~ x, Data)
# fit a loess line
loess_fit <- loess(y ~ x, Data)
lines(Data$x, predict(loess_fit), col = "blue")
# fit a non-linear regression
nls_fit <- nls(y ~ a + b * x^(-c), Data, start = list(a = 80, b = 20,
c = 0.2))
lines(Data$x, predict(nls_fit), col = "red")
|
|||
|
|
|
Your question is a bit vague, so I'm going to make some assumptions about what your problem is. It would help a lot if you could put up a scatterplot and describe the data a bit. Please, if I'm making bad assumptions then ignore my answer. First, it's possible that your data describe some process which you reasonably believe is non-linear. For instance, if you're trying to do regression on the distance for a car to stop with sudden braking vs the speed of the car, physics tells us that the energy of the vehicle is proportional to the square of the velocity - not the velocity itself. So you might want to try polynomial regression in this case, and (in R) you could do something like On the other hand, if you've got a line which is "wobbly" and you don't know why it's wobbly, then a good starting point would probably be locally weighted regression, or On the third hand (running out of hands) - you're talking about trends? Is this a temporal problem? If it is, be a little cautious with over interpreting trend lines and statistical significance. Trends in time series can appear in "autoregressive" processes, and for these processes the randomness of the process can occasionally construct trends out of random noise, and the wrong statistical significance test can tell you it's significant when it's not! |
|||
|
|
|
If you use
You can choose how the data is smoothed: see |
|||
|
|
|
Without knowing exactly what you are looking for, using the
See |
|||
|
|
