Guest User

Untitled

a guest
Nov 15th, 2018
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.51 KB | None | 0 0
  1. library(tidyverse)
  2. library(magick)
  3.  
  4. # devtools::install_github("thomasp85/patchwork")
  5. library(patchwork)
  6.  
  7. colours = c("#85d4e3",
  8. "#e39f85")
  9.  
  10. set.seed(40)
  11.  
  12. error_sd <- function(icc) {
  13.  
  14. # From the reliability formula: rel = sigmaTrue^2 / sigmaTot^2 = sigmaTrue^2 / (sigmaTrue^2 + sigmaError^2)
  15.  
  16. sqrt(1/icc - 1)
  17. }
  18.  
  19.  
  20. special_dnorm <- function(negative=FALSE, x, mean, sd) {
  21.  
  22. # This makes a density plot that has the same height, regardless of the variance
  23. # Also, the negative argument was for another plot to have them facing left and right
  24.  
  25. maxval <- dnorm(0, mean, sd)
  26. out <- dnorm(x, mean, sd) / maxval
  27.  
  28. out <- out+0.08 # 0.08 is a little offset for aesthetics
  29.  
  30. if(negative) {
  31. out <- out*-1
  32. }
  33.  
  34. return(out)
  35. }
  36.  
  37.  
  38. # Make some points
  39.  
  40. reliability <- tibble(True = rnorm(40)) %>%
  41. arrange(True) %>%
  42. mutate(Subject = 1:n())
  43.  
  44.  
  45. dir.create("gif_figs")
  46.  
  47. make_measplot <- function(icc, n=1) {
  48.  
  49. # Add measurement error
  50.  
  51. errorsd <- error_sd(icc)
  52. reliability$Measured = reliability$True + rnorm(length(reliability$True), sd = errorsd)
  53.  
  54.  
  55. # And error bars
  56.  
  57. reliability$Error_upper <- reliability$True + 1.96*errorsd
  58. reliability$Error_lower <- reliability$True - 1.96*errorsd
  59.  
  60. # Make the distribution plot
  61.  
  62. distroplot <- ggplot(data.frame(x = c(-5, 5)), aes(x)) + # The data is meaningless - not used
  63. stat_function(fun = special_dnorm, colour = colours[1],
  64. size = 1.5, args = list(negative=F, mean=0, sd=1),
  65. geom = "line") +
  66. annotate("segment", x = 1.96, xend = -1.96, y = 0.03, yend = 0.03,
  67. colour = colours[1], size=1) +
  68. stat_function(fun = special_dnorm, colour = colours[2],
  69. size = 1.5, args = list(negative=F, mean=0, sd=errorsd),
  70. geom = "line") +
  71. annotate("segment", x = 1.96*errorsd, xend = -1.96*errorsd, y = 0.05, yend = 0.05,
  72. colour = colours[2], size=1) +
  73. coord_flip() +
  74. theme_void() +
  75. lims(x=c(-5,5)) +
  76. annotate("text", x = 2.5, y = 0.7, label = "True", size=5, colour=colours[1]) +
  77. annotate("text", x = -2.5, y = 0.7, label = "Error", size=5, colour=colours[2])
  78.  
  79.  
  80. # Make the measurement plot
  81.  
  82. relplot <- ggplot(reliability, aes(x=Subject, y=True)) +
  83. geom_point(size=5, alpha=0.3, colour=colours[1]) +
  84. theme_void() +
  85. geom_errorbar(aes(ymin=Error_lower, ymax=Error_upper), width=0, colour=colours[2]) +
  86. coord_cartesian(ylim = c(-5,5)) +
  87. annotate("text", x = 10, y = 4, label = paste("ICC =", round(icc, 2), sep=" "), size=5) +
  88. geom_point(aes(y=Measured))
  89.  
  90.  
  91. # Put them together
  92.  
  93. out <- relplot + distroplot + plot_layout(ncol = 2, widths = c(3, 1))
  94.  
  95. # Making a naming scheme so that they're in the right order
  96.  
  97. icc_error <- 100-(100*icc)
  98.  
  99. # Save
  100.  
  101. ggsave(out, filename = sprintf(paste("gif_figs/", stringr::str_pad(icc_error, side="left", pad="0", width = 2),
  102. "_iccplot_%d.png", sep=""), n), width = 9, height = 5, dpi=200)
  103.  
  104. return(out)
  105.  
  106. }
  107.  
  108.  
  109. # Make a bunch of them
  110.  
  111. iccvals <- c(0.99, 0.9, 0.8, 0.7, 0.6, 0.5, 0.4, 0.2)
  112. each_times <- 6
  113.  
  114. # I was too lazy to do this tidily. Forgive me Hadley for I have sinned.
  115.  
  116. for(i in 1:length(iccvals)) {
  117. for(j in 1:each_times) {
  118. make_measplot(iccvals[i], j)
  119. }
  120. }
  121.  
  122.  
  123. # Make a gif!
  124.  
  125. list.files(path = "./gif_figs/", pattern = "*.png", full.names = T) %>%
  126. map(image_read) %>% # reads each path file
  127. image_join() %>% # joins image
  128. image_animate(fps=2) %>% # animates, can opt for number of loops
  129. image_write("reliability_change.gif") # write to current dir
Add Comment
Please, Sign In to add comment