OMbook_glossary

Code used for entries in the glossary for the OMbook

Chris Evans https://www.psyctc.org/R_blog/ (PSYCTC.org)https://www.psyctc.org/psyctc/
2021-11-09

I have put this here just to create a link to a developing Rmarkdown file of code I have used for entries in the glossary for the “OMbook”: Outcome measures and evaluation in counselling and psychotherapy.

I’m not claiming the code is good, some of it certainly isn’t, but it works. Some of it uses real data which for now I am not making available until I am sure that is safe and OK with others involved in collecting the data. OK, the code itself is here.

This next bit is just to get a nice plot for the Rblog index.

Show code
library(tidyverse)
throwDice <- function(nThrows, nSides = 6, scoreSides = 1:6){
  ### function to simulate throwing dice (or anything really)
  ### defaults to six sided die with scores 1:6 but you can override that
  if(nThrows <= 0) {
    stop("nThrows must be positive")
  }
  if(nThrows > 8) {
    stop("nThrows must be under 9 (to keep things easy!)")
  }
  if(nThrows - round(nThrows) > .Machine$double.eps) {
    warning("nThrows wasn't integer, rounded to integer")
    nThrows <- round(nThrows)
  }
  newScores <- scoreSides
  while(nThrows > 1) {
    newScores <- as.vector(outer(newScores, scoreSides, FUN = "+"))
    nThrows <- nThrows - 1
  }
  newScores
}
# throwDice(0)
# throwDice(1)
# throwDice(1.1)
# throwDice(11)
# throwDice(2)
# length(throwDice(2))
# min(throwDice(2))
# max(throwDice(2))
# nThrows <- 3
# throwDice(nThrows)
# length(throwDice(nThrows))
# min(throwDice(nThrows))
# max(throwDice(nThrows))
# nThrows <- 4
# throwDice(nThrows)
# length(throwDice(nThrows))
# min(throwDice(nThrows))
# max(throwDice(nThrows))


1:8 %>%
  as_tibble() %>%
  rename(nThrows = value) %>%
  rowwise() %>%
  mutate(score = list(throwDice(nThrows)),
         nThrowsFac = factor(nThrows)) %>%
  ungroup() %>%
  unnest_longer(score) %>%
  group_by(nThrowsFac) %>%
  mutate(nScores = n()) %>%
  ungroup() %>%
  group_by(nThrowsFac, score) %>%
  summarise(nThrows = first(nThrows),
            nScores = first(nScores),
            n = n(),
            p = n / nScores) %>%
  ungroup() -> tibDiceThrows

ggplot(data = tibDiceThrows,
       aes(x = score, y = p, colour = nThrowsFac)) +
  geom_point()  +
  geom_line() +
  ylab("Probability") +
  scale_x_continuous(name = paste0("Total score from n dice (with n from 1 to ",
                                   max(tibDiceThrows$nThrows),
                                   ")"),
                     breaks = seq(2, max(tibDiceThrows$score), 2)) +
  scale_colour_discrete(name = "n(throws)") +
  theme(axis.text.x = element_text(angle = 70, hjust = 1))

Citation

For attribution, please cite this work as

Evans (2021, Nov. 9). Chris (Evans) R SAFAQ: OMbook_glossary. Retrieved from https://www.psyctc.org/R_blog/posts/2021-11-09-ombookglossary/

BibTeX citation

@misc{evans2021ombook_glossary,
  author = {Evans, Chris},
  title = {Chris (Evans) R SAFAQ: OMbook_glossary},
  url = {https://www.psyctc.org/R_blog/posts/2021-11-09-ombookglossary/},
  year = {2021}
}