Local smoothing with circular predictors and linear or circular responses
Source:R/circ_circ_loess.R, R/functions.R
circ_loess.RdSmooth a linear response over a circular predictor with circ_lin_loess(),
or a circular response over a circular predictor with circ_circ_loess().
circ_loess() selects the response type with circ_y: FALSE (the default)
chooses the linear response, and TRUE chooses the circular response.
Usage
circ_circ_loess(
formula = NULL,
data = NULL,
angle = NULL,
y = NULL,
xseq = NULL,
circ_space = NULL,
span = 0.75,
response_space = NULL,
...
)
circ_lin_loess(
formula = NULL,
data = NULL,
angle = NULL,
y = NULL,
xseq = NULL,
circ_space = NULL,
span = 0.75,
...
)
circ_loess(
formula = NULL,
data = NULL,
angle = NULL,
y = NULL,
xseq = NULL,
circ_space = NULL,
span = 0.75,
circ_y = FALSE,
response_space = NULL,
...
)Arguments
- formula
the formula, e.g., y ~ x
- data
data to use
- angle
a vector of angles (not used if a formula is provided)
- y
dependent variable vector (not used if a formula is provided)
- xseq
a grid to compute predictions on (optional, the default is to use 500 points spanning the circle)
- circ_space
circular space to use (90, 180, 360, or 2*pi)
- span
a span to adjust the degree of smoothing
- response_space
Period of the circular response. Defaults to
circ_space.- ...
other arguments (ignored)
- circ_y
Logical: use a circular response (
TRUE) or linear response (FALSE, the default). The predictor is circular in both cases.
Value
A fitted object inheriting from circ_loess. The explicit functions
additionally have class circ_lin_loess or circ_circ_loess. Components include:
anglethe angles in the dataythe dependent variable vales in the dataxseqthe grid on which the loess values are estimatedy_estthe estimated loess valuesy_seuncertainty estimates (the linear fit retains the legacy calculation; the circular fit uses weighted SEM of locally unwrapped response residuals)wweightscirc_spacecircular space usedspanspan usedresponse_spaceresponse period for the circular-response fity_circ_sdweighted circular SD for the circular-response fit
Details
Weights for the regression are computed as
\[w = (1-(d/d_{max})^3)^3\]
where d is the angular difference between the point at which the estimate is computed and the angles in the data, and \(d_{max}\) is the maximum possible distance. If span is at least 1, all points are included and \(d_{max} = circ\_space \times span / 2\). Otherwise, the nearest proportion span of the observations is included and \(d_{max}\) is their maximal distance.
The predictor is circular in both functions. Only circ_circ_loess()
uses circular response means. For a circular response, response_space may
differ from circ_space; for example, an orientation predictor can have period
180 while a colour response has period 360. Predictions with no defined mean
direction return NA. Prediction intervals are local approximations centred
on the estimated direction; their endpoints are not wrapped.
Examples
angle <- c(-2, -1, 1, 2)
response <- c(179, 178, -178, -179)
circ_lin_loess(angle = angle, y = response, xseq = 0, circ_space = 360, span = 1)$y_est
#> [1] 0
circ_circ_loess(angle = angle, y = response, xseq = 0, circ_space = 360, span = 1)$y_est
#> [1] -180
library(ggplot2)
p <- ggplot(Pascucci_et_al_2019_data, aes(x = orientation, y = err)) +
geom_point(alpha = 0.05) +
labs(x = "Orientation, deg.", y = "Error, deg.")
p1 <- p + geom_smooth(method = "loess") + ggtitle("Standard LOESS")
p2 <- p + geom_smooth(method = "circ_lin_loess", method.args = list(circ_space = 180, span = 0.5)) +
ggtitle("Circular LOESS, span = 0.5")
p3 <- p + geom_smooth(method = "circ_lin_loess", method.args = list(circ_space = 180, span = 0.2)) +
ggtitle("Circular LOESS, span = 0.2")
(p1 + p2 + p3)
#> `geom_smooth()` using formula = 'y ~ x'
#> `geom_smooth()` using formula = 'y ~ x'
#> `geom_smooth()` using formula = 'y ~ x'