Smoothing Points with lowess() and loess()
Understanding lowess() and loess()
- lowess(): Stands for “Locally Weighted Scatterplot Smoothing.” It fits a smooth curve to the data using locally weighted regression. It’s particularly useful for exploring trends in data.
- loess(): Stands for “Locally Estimated Scatterplot Smoothing.” It provides a more flexible, polynomial-based smoothing and can handle more complex relationships.
Using lowess()
Syntax:
lowess(x, y = NULL, f = 2/3, delta = NULL, iter = 3, ...)
- x: Numeric vector of x-coordinates.
- y: Numeric vector of y-coordinates (if x is a matrix, y can be omitted).
- f: The smoothing parameter (between 0 and 1). Default is 2/3.
- delta: Bandwidth parameter for large datasets.
- iter: Number of iterations for robust fitting (default is 3).
Example:
# Generate example data x <- 1:20 y <- x + rnorm(20) # Create a basic plot plot(x, y, main = "LOWESS Smoothing Example") # Apply LOWESS smoothing smoothed <- lowess(x, y) # Add the smoothed line to the plot lines(smoothed, col = "blue")
- smoothed: Contains the smoothed values returned by lowess().
- lines(): Adds the smoothed line to the existing plot.
Using loess()
Syntax:
loess(formula, data, span = 0.75, degree = 2, family = "symmetric", ...)
- formula: A formula describing the model (e.g., y ~ x).
- data: Data frame containing the variables.
- span: The smoother parameter, analogous to f in lowess() (default is 0.75).
- degree: Degree of the local polynomial (default is 2, which fits a quadratic model).
- family: Type of smoothing (e.g., “symmetric” or “adaptative”).
Example:
# Generate example data data <- data.frame(x = 1:20, y = (1:20) + rnorm(20)) # Create a basic plot plot(data$x, data$y, main = "LOESS Smoothing Example") # Apply LOESS smoothing fit <- loess(y ~ x, data = data) # Predict values pred <- predict(fit) # Add the smoothed line to the plot lines(data$x, pred, col = "red")
- fit: LOESS model fitted to the data.
- predict(): Predicts values based on the LOESS model.
Comparing lowess() and loess()
- lowess() is simpler and less flexible but is often sufficient for basic smoothing tasks.
- loess() provides more control and can handle more complex data relationships.
Comparison Example:
# Generate example data x <- 1:30 y <- sin(x / 2) + rnorm(30) # Create a basic plot plot(x, y, main = "Comparing LOWESS and LOESS") # Apply LOWESS smoothing lowess_smoothed <- lowess(x, y) lines(lowess_smoothed, col = "blue", lty = 1) # Apply LOESS smoothing loess_fit <- loess(y ~ x) loess_smoothed <- predict(loess_fit) lines(x, loess_smoothed, col = "red", lty = 2) # Add a legend legend("topright", legend = c("LOWESS", "LOESS"), col = c("blue", "red"), lty = c(1, 2))
- lty: Line type to differentiate between LOWESS and LOESS lines.
Adjusting Parameters
- f (for lowess()) and span (for loess()) control the amount of smoothing. Smaller values result in more localized smoothing, while larger values result in more global smoothing.
Example:
# Generate example data x <- 1:50 y <- sin(x / 5) + rnorm(50) # Basic plot plot(x, y, main = "Adjusting Smoothing Parameters") # LOWESS with different smoothing parameter lowess_smoothed_1 <- lowess(x, y, f = 0.2) lines(lowess_smoothed_1, col = "blue", lty = 1) # LOESS with different span loess_fit_1 <- loess(y ~ x, span = 0.2) loess_smoothed_1 <- predict(loess_fit_1) lines(x, loess_smoothed_1, col = "red", lty = 2)
Visualizing the Effect of Smoothing
To understand how smoothing affects the plot, you can overlay multiple smoothing results or compare smoothed curves with the original data.
Example:
# Generate example data x <- 1:40 y <- cos(x / 3) + rnorm(40) # Basic plot plot(x, y, main = "Effect of Smoothing Parameters") # LOWESS with default smoothing parameter lowess_smoothed_default <- lowess(x, y) lines(lowess_smoothed_default, col = "blue", lty = 1) # LOESS with a large span loess_fit_large_span <- loess(y ~ x, span = 1) loess_smoothed_large_span <- predict(loess_fit_large_span) lines(x, loess_smoothed_large_span, col = "red", lty = 2) # LOESS with a small span loess_fit_small_span <- loess(y ~ x, span = 0.1) loess_smoothed_small_span <- predict(loess_fit_small_span) lines(x, loess_smoothed_small_span, col = "green", lty = 3) # Add a legend legend("topright", legend = c("LOWESS", "LOESS (Large Span)", "LOESS (Small Span)"), col = c("blue", "red", "green"), lty = c(1, 2, 3))
Summary
- lowess(): Simple, robust smoothing for exploring trends.
- f: Smoothing parameter (default 2/3).
- loess(): Flexible polynomial-based smoothing for complex data.
- span: Smoothing parameter (default 0.75).
- degree: Degree of the local polynomial (default 2).
- Comparison: lowess() is simpler; loess() offers more flexibility.
- Parameter Adjustment: Control the level of smoothing with f and span.
- Visualization: Compare different smoothing results to understand effects.
Post Views: 75