Skip to content

Commit

Permalink
add a categorical repsonse example
Browse files Browse the repository at this point in the history
  • Loading branch information
gavinsimpson committed Mar 24, 2023
1 parent d5c6e59 commit cd3decb
Showing 1 changed file with 27 additions and 0 deletions.
27 changes: 27 additions & 0 deletions day-5/categorical-response-example.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# categorical
pkgs <- c("mgcv", "ggplot2", "dplyr", "gratia")
vapply(pkgs, library, logical(1), character.only = TRUE, logical.return = TRUE,
quietly = TRUE)

# Ordered categorical model ocat()
n_categories <- 4
su_eg1_ocat <- data_sim("eg1", n = 200, dist = "ordered categorical",
n_cat = n_categories, seed = 2)
m_ocat <- gam(y ~ s(x0) + s(x1) + s(x2) + s(x3),
family = ocat(R = n_categories), data = su_eg1_ocat, method = "REML")

fitted_values(m_ocat)

draw(m_ocat, scales = "fixed")

ds <- data_slice(m_ocat, x2 = evenly(x2))
fv_ocat <- fitted_values(m_ocat, data = ds)

fv_ocat |>
ggplot(aes(x = x2, y = fitted, colour = category, group = category)) +
geom_ribbon(aes(ymin = lower, ymax = upper, x = x2,
fill = category, colour = NULL),
alpha = 0.2) +
geom_line()

head(predict(m_ocat, type = "response", exclude = "s(individual)"))

0 comments on commit cd3decb

Please sign in to comment.