inst/doc/euclidean-distance.R

## ----packages, message=FALSE--------------------------------------------------
if (!requireNamespace("lexopsdata", quietly = TRUE)) {
  knitr::opts_chunk$set(eval = FALSE)
} else {
  library(lexopsdata)
}
library(dplyr)
library(ggplot2)
library(LexOPS)

## ----hidden-setup, include=FALSE----------------------------------------------
set.seed(1)
theme_set(theme_minimal())

## ----euc-graph1, echo = FALSE, fig.width=4, fig.height=6----------------------
# get values for "puppy"
puppy_values <- filter(lexops, string == "puppy")
# example words of varying distances from "puppy"
example_words <- c("puppy", "trailer", "unhappiness", "cemetery", "love", "trailer", "overzealous", "juvenile", "moose")

# plot
lexops |>
  filter(string %in% example_words) |>
  mutate(
    cnc_dist = abs(CNC.Brysbaert - puppy_values$CNC.Brysbaert),
    val_dist = abs(VAL.Warriner - puppy_values$VAL.Warriner)
  ) |>
  mutate(val_dist_t = 0, cnc_dist_t = 0) %>%
  bind_rows(
    mutate(., val_dist_t = val_dist, cnc_dist_t = cnc_dist)
  ) |>
  ggplot(aes(cnc_dist, val_dist, label = string)) +
  geom_point() +
  geom_text(nudge_y = -0.25) +
  geom_line(
    aes(cnc_dist_t, val_dist_t, group = string),
    linetype = 2,
    alpha = 0.5
  ) +
  geom_text(
    aes(cnc_dist/2, val_dist/2, label = round(sqrt(cnc_dist^2 + val_dist^2), 2)),
    data = ~filter(.x, string != "puppy"),
    colour = "red"
  ) +
  coord_fixed(xlim = c(-0.1, 3.5), ylim = c(-0.1, 6.5)) +
  labs(x = "Concreteness Distance", y = "Valence Distance")

## ----scale--------------------------------------------------------------------
lexops_scaled <- mutate(
  lexops,
  CNC.Brysbaert = scale(CNC.Brysbaert),
  VAL.Warriner = scale(VAL.Warriner)
)

## ----euc-graph2, echo = FALSE, fig.width=6, fig.height=6----------------------
# get values for "puppy"
puppy_values <- filter(lexops_scaled, string == "puppy")

# plot
# note that `c()` is used to remove attributes from scaled variables in puppy_values
lexops_scaled |>
  filter(string %in% example_words) |>
  mutate(
    cnc_dist = abs(CNC.Brysbaert - c(puppy_values$CNC.Brysbaert)),
    val_dist = abs(VAL.Warriner - c(puppy_values$VAL.Warriner))
  ) |>
  mutate(val_dist_t = 0, cnc_dist_t = 0) %>%
  bind_rows(
    mutate(., val_dist_t = val_dist, cnc_dist_t = cnc_dist)
  ) |>
  ggplot(aes(cnc_dist, val_dist, label = string)) +
  geom_point() +
  geom_text(nudge_y = -0.25) +
  geom_line(
    aes(cnc_dist_t, val_dist_t, group = string),
    linetype = 2,
    alpha = 0.5
  ) +
  geom_text(
    aes(cnc_dist/2, val_dist/2, label = round(sqrt(cnc_dist^2 + val_dist^2), 2)),
    data = ~filter(.x, string != "puppy"),
    colour = "red"
  ) +
  coord_fixed(xlim = c(-0.1, 4.5), ylim = c(-0.1, 4.5)) +
  labs(x = "Concreteness Distance", y = "Valence Distance")

## ----euc-graph3, echo = FALSE, fig.width=6, fig.height=6----------------------
lexops_scaled |>
  filter(string %in% example_words) |>
  mutate(
    cnc_dist = abs(CNC.Brysbaert - c(puppy_values$CNC.Brysbaert)),
    val_dist = abs(VAL.Warriner - c(puppy_values$VAL.Warriner))
  ) |>
  mutate(val_dist_t = 0, cnc_dist_t = 0) %>%
  bind_rows(
    mutate(., val_dist_t = val_dist, cnc_dist_t = cnc_dist)
  ) |>
  filter(string %in% c("puppy", "unhappiness")) |>
  ggplot(aes(cnc_dist, val_dist, label = string)) +
  geom_point() +
  geom_text(nudge_y = -0.1) +
  # d(a,b)
  geom_line(
    aes(cnc_dist_t, val_dist_t, group = string),
    linetype = 2,
    alpha = 0.5
  ) +
  geom_text(
    aes(cnc_dist/2, val_dist/2, label = sprintf("d(a,b) = %s", round(sqrt(cnc_dist^2 + val_dist^2), 2))),
    data = ~filter(.x, string != "puppy"),
    colour = "red"
  ) +
  # cnc
  geom_line(
    aes(cnc_dist_t, 0, group = string),
    linetype = 2,
    alpha = 0.5
  ) +
  geom_text(
    aes(cnc_dist/2, 0, label = sprintf("(a_1 - b_1) = %s", round(cnc_dist, 2))),
    data = ~filter(.x, string != "puppy"),
    colour = "red",
    nudge_y = -0.1
  ) +
  # val
  geom_line(
    aes(max(cnc_dist), val_dist_t, group = string),
    linetype = 2,
    alpha = 0.5
  ) +
  geom_text(
    aes(max(cnc_dist), val_dist/2, label = sprintf("(a_2 - b_2) = %s", round(val_dist, 2))),
    data = ~filter(.x, string != "puppy"),
    colour = "red"
  ) +
  coord_fixed(xlim = c(-0.1, 4.5), ylim = c(-0.1, 4.5)) +
  labs(x = "Concreteness Distance", y = "Valence Distance")

## ----euc-graph4, echo = FALSE, fig.width=6, fig.height=6----------------------
# get values for "puppy"
puppy_values <- lexops_scaled |>
  mutate(CNC.Brysbaert = 2*CNC.Brysbaert) |>
  filter(string == "puppy")

# plot
# note that `c()` is used to remove attributes from scaled variables in puppy_values
lexops_scaled |>
  mutate(CNC.Brysbaert = 2*CNC.Brysbaert) |>
  filter(string %in% example_words) |>
  mutate(
    cnc_dist = abs(CNC.Brysbaert - c(puppy_values$CNC.Brysbaert)),
    val_dist = abs(VAL.Warriner - c(puppy_values$VAL.Warriner))
  ) |>
  mutate(val_dist_t = 0, cnc_dist_t = 0) %>%
  bind_rows(
    mutate(., val_dist_t = val_dist, cnc_dist_t = cnc_dist)
  ) |>
  ggplot(aes(cnc_dist, val_dist, label = string)) +
  geom_point() +
  geom_text(nudge_y = -0.25) +
  geom_line(
    aes(cnc_dist_t, val_dist_t, group = string),
    linetype = 2,
    alpha = 0.5
  ) +
  geom_text(
    aes(cnc_dist/2, val_dist/2, label = round(sqrt(cnc_dist^2 + val_dist^2), 2)),
    data = ~filter(.x, string != "puppy"),
    colour = "red"
  ) +
  coord_fixed(xlim = c(-0.1, 9), ylim = c(-0.1, 9)) +
  labs(x = "Concreteness Distance\n(Weight = 2)", y = "Valence Distance\n(Weight = 1)")

## ----euc-graph5, echo = FALSE, fig.width=6, fig.height=6----------------------
# get values for "puppy"
puppy_values <- lexops_scaled |>
  mutate(
    CNC.Brysbaert = (2/mean(c(1, 2)))*CNC.Brysbaert,
    VAL.Warriner = (1/mean(c(1, 2)))*VAL.Warriner
    ) |>
  filter(string == "puppy")

# plot
# note that `c()` is used to remove attributes from scaled variables in puppy_values
lexops_scaled |>
  mutate(
    CNC.Brysbaert = (2/mean(c(1, 2)))*CNC.Brysbaert,
    VAL.Warriner = (1/mean(c(1, 2)))*VAL.Warriner
    ) |>
  filter(string %in% example_words) |>
  mutate(
    cnc_dist = abs(CNC.Brysbaert - c(puppy_values$CNC.Brysbaert)),
    val_dist = abs(VAL.Warriner - c(puppy_values$VAL.Warriner))
  ) |>
  mutate(val_dist_t = 0, cnc_dist_t = 0) %>%
  bind_rows(
    mutate(., val_dist_t = val_dist, cnc_dist_t = cnc_dist)
  ) |>
  ggplot(aes(cnc_dist, val_dist, label = string)) +
  geom_point() +
  geom_text(nudge_y = -0.1) +
  geom_line(
    aes(cnc_dist_t, val_dist_t, group = string),
    linetype = 2,
    alpha = 0.5
  ) +
  geom_text(
    aes(cnc_dist/2, val_dist/2, label = round(sqrt(cnc_dist^2 + val_dist^2), 2)),
    data = ~filter(.x, string != "puppy"),
    colour = "red"
  ) +
  coord_fixed(xlim = c(-0.1, 5), ylim = c(-0.1, 5)) +
  labs(x = "Concreteness Distance\n(Weight = 1.333333)", y = "Valence Distance\n(Weight = 0.666667)")

## ----match1, echo=FALSE-------------------------------------------------------
lexops |>
  filter(string == "moose") |>
  select(string, Length, Zipf.BNC.Written, AoA.Kuperman, CNC.Brysbaert) |>
  knitr::kable()

## ----match2, eval=FALSE-------------------------------------------------------
# lexops |>
#   match_item(
#     "moose",
#     Length = -1:1,
#     Zipf.BNC.Written = -0.2:0.2,
#     AoA.Kuperman = -2:2,
#     CNC.Brysbaert = -0.25:0.25
#   ) |>
#   select(string, Length, Zipf.BNC.Written, AoA.Kuperman, CNC.Brysbaert) |>
#   head(5)

## ----match2-kable, echo=FALSE-------------------------------------------------
lexops |>
  match_item(
    "moose",
    Length = -1:1,
    Zipf.BNC.Written = -0.2:0.2,
    AoA.Kuperman = -2:2,
    CNC.Brysbaert = -0.25:0.25
  ) |>
  select(string, Length, Zipf.BNC.Written, AoA.Kuperman, CNC.Brysbaert) |>
  head(5) |>
  knitr::kable()

## ----match3, eval=FALSE-------------------------------------------------------
# lexops |>
#   mutate(
#     Euc_Dist = euc_dists(lexops, "moose", c(Length, Zipf.BNC.Written, AoA.Kuperman, CNC.Brysbaert))
#   ) |>
#   arrange(Euc_Dist) |>
#   filter(string != "moose") |>
#   select(string, Euc_Dist, Length, Zipf.BNC.Written, AoA.Kuperman, CNC.Brysbaert) |>
#   head(5)

## ----match3-kable, echo=FALSE-------------------------------------------------
lexops |>
  mutate(
    Euc_Dist = euc_dists(lexops, "moose", c(Length, Zipf.BNC.Written, AoA.Kuperman, CNC.Brysbaert))
  ) |>
  arrange(Euc_Dist) |>
  filter(string != "moose") |>
  select(string, Euc_Dist, Length, Zipf.BNC.Written, AoA.Kuperman, CNC.Brysbaert) |>
  head(5) |>
  knitr::kable()

## ----match4, eval=FALSE-------------------------------------------------------
# lexops |>
#   mutate(
#     Euc_Dist = euc_dists(lexops, "moose", c(Length, Zipf.BNC.Written, AoA.Kuperman, CNC.Brysbaert))
#   ) |>
#   match_item(
#     "moose",
#     Euc_Dist = 0:Inf,
#     Zipf.BNC.Written = -0.2:0.2
#   ) |>
#   select(string, Euc_Dist, Length, Zipf.BNC.Written, AoA.Kuperman, CNC.Brysbaert) |>
#   arrange(Euc_Dist) |>
#   head(5)

## ----match4-kable, echo=FALSE-------------------------------------------------
lexops |>
  mutate(
    Euc_Dist = euc_dists(lexops, "moose", c(Length, Zipf.BNC.Written, AoA.Kuperman, CNC.Brysbaert))
  ) |>
  match_item(
    "moose",
    Euc_Dist = 0:Inf,
    Zipf.BNC.Written = -0.2:0.2
  ) |>
  select(string, Euc_Dist, Length, Zipf.BNC.Written, AoA.Kuperman, CNC.Brysbaert) |>
  arrange(Euc_Dist) |>
  head(5) |>
  knitr::kable()

## ----match5, eval=FALSE-------------------------------------------------------
# lexops |>
#   mutate(
#     Euc_Dist = euc_dists(
#       lexops, "moose", c(Length, Zipf.BNC.Written, AoA.Kuperman, CNC.Brysbaert),
#       weights = c(1, 2, 1, 1)
#     )
#   ) |>
#   arrange(Euc_Dist) |>
#   select(string, Euc_Dist, Length, Zipf.BNC.Written, AoA.Kuperman, CNC.Brysbaert) |>
#   head(5)

## ----match5-kable, echo=FALSE-------------------------------------------------
lexops |>
  mutate(
    Euc_Dist = euc_dists(
      lexops, "moose", c(Length, Zipf.BNC.Written, AoA.Kuperman, CNC.Brysbaert),
      weights = c(1, 2, 1, 1)
    )
  ) |>
  arrange(Euc_Dist) |>
  select(string, Euc_Dist, Length, Zipf.BNC.Written, AoA.Kuperman, CNC.Brysbaert) |>
  filter(string != "moose") |>
  head(5) |>
  knitr::kable()

## ----generate1----------------------------------------------------------------
stim <- lexops |>
  split_by(CNC.Brysbaert, 1:2 ~ 4:5) |>
  control_for(Length, 0:0) |>
  control_for(Zipf.BNC.Written, -0.1:0.1) |>
  control_for(AoA.Kuperman, -2:2) |>
  generate(20)

## ----generate1-plot, fig.width=6, fig.height=4--------------------------------
plot_design(stim, "controls")

## ----generate2----------------------------------------------------------------
stim2 <- lexops |>
  split_by(CNC.Brysbaert, 1:2 ~ 4:5) |>
  control_for_euc(
      c(Length, Zipf.BNC.Written, AoA.Kuperman),
      0:0.1,
      name = "euclidean_distance",
      weights = c(0.5, 1, 0.1)
  ) |>
  generate(20)

## ----generate2-plot, fig.width=6, fig.height=4--------------------------------
plot_design(stim2, c("Length", "Zipf.BNC.Written", "AoA.Kuperman"))

## ----generate3----------------------------------------------------------------
stim3 <- lexops |>
  split_by(CNC.Brysbaert, 1:2 ~ 4:5) |>
  control_for_euc(
      c(Length, Zipf.BNC.Written, AoA.Kuperman),
      0:0.1,
      name = "euclidean_distance",
      weights = c(0.5, 1, 0.1)
  ) |>
  control_for(BG.BNC.Written, -0.0025:0.0025) |>
  generate(20)

## ----generate3-plot, fig.width=6, fig.height=4--------------------------------
plot_design(stim3, c("Length", "Zipf.BNC.Written", "AoA.Kuperman", "BG.BNC.Written"))

Try the LexOPS package in your browser

Any scripts or data that you put into this service are public.

LexOPS documentation built on Dec. 1, 2026, 5:06 p.m.