Reputation: 13
Hi I am extremely new to R and typically use matlab or c# but currently need to perform some smooth curve fitting, and do some residual analysis, so I've turned to R. I know that there are plenty of questions asked regarding the topic of loess and lowess fitting but the issue isn't the typical "your data is out of order" problem I seem to keep seeing.
Some sample data I am working with can be seen in the plot below
In the end the method I would like to use is loess, but I've also tried lowess and scatter.smooth. My issue is that I can't seem to get these methods working when I plot my data as x1,y1 but they seem to work alright when I plot y1,x1. I expect I'm just totally clueless here, but this seems odd to me.
ord <- order(x)
x1 <- x[ord]
y1 <- y[ord]
plot(x1,y1)
fm = loess(y1~x1)
lines(x1, predict(fm))
ord <- order(y)
x1 <- x[ord]
y1 <- y[ord]
plot(y1,x1)
fm = loess(x1~y1)
lines(y1, predict(fm))
The above plots show that for the x1,y1 plot the fit cuts across the data, clearly not oriented properly, but with a shape that would make sense for the data if it were flipped and rotated. For the y1,x1 plot though, using the same steps but just with the use of x1 and y1 switched in all lines, the fit works fine. I feel like this issue is actually quite a simple one, and just something I'm drawing a blank on. Any help/explanation here would be greatly appreciated, as I would like to be able to plot the data in the intended x1,y1 orientation.
Upvotes: 1
Views: 389
Reputation: 263362
In mathematics one of the meanings of "function" is a relationship where there is only one y-value for each of the x-values in a relationship between two variables. loess
tries to create a function in that meaning. Your data would support a curve that starts high on the left, arcs over to the right and sweeps back to the left. That would then be a 1-2 relation because many of the x-values would have 2 y-values. Mathematically that would lose many desirable features of have a "true" function. You demonstrate that the relationship can be "functional" with your inverse display and loess fit. It would be possible to take that second fit and "invert", which would rotate the curve 90 degrees).
You didn't provide data that would support a coding demonstration, but if you remedy that omission, such a demonstration could be offered.
Upvotes: 1