## ----include = FALSE----------------------------------------------------------
knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>",
  fig.width = 6,
  fig.height = 4
)

## -----------------------------------------------------------------------------
library(CircularRegression)

wrap_angle <- function(x) atan2(sin(x), cos(x))

set.seed(20260530)
n <- 120
x1 <- runif(n, -pi, pi)
x2 <- runif(n, -pi, pi)
z2 <- runif(n, 0.2, 1.8)

mu <- atan2(
  sin(x1) + 0.35 * z2 * sin(x2),
  cos(x1) + 0.35 * z2 * cos(x2)
)
y <- wrap_angle(mu + rnorm(n, sd = 0.12))

dat <- data.frame(y = y, x1 = x1, x2 = x2, z2 = z2)

## -----------------------------------------------------------------------------
fit <- circular_regression(y ~ x1 + x2:z2, data = dat)
fit

## -----------------------------------------------------------------------------
fit_hom <- circular_regression(
  y ~ x1 + x2:z2,
  data = dat,
  method = "homogeneous",
  reference = c("name", "x1")
)

fit_cons <- circular_regression(
  y ~ x1 + x2:z2,
  data = dat,
  method = "consensus"
)

coef(fit_hom)
coef(fit_cons)

## -----------------------------------------------------------------------------
new_dat <- dat[1:6, c("x1", "x2", "z2")]

predict(fit, newdata = new_dat)
predict(fit, newdata = new_dat, type = "components")

## -----------------------------------------------------------------------------
res <- residuals(fit)
summary(res)

## -----------------------------------------------------------------------------
plot(
  fitted(fit),
  res,
  xlab = "Fitted direction",
  ylab = "Circular residual",
  pch = 19,
  col = "gray30"
)
abline(h = 0, lty = 2)

## ----eval = FALSE-------------------------------------------------------------
# pkgdown::build_site()

