Guest User

Untitled

a guest
Jun 21st, 2018
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.43 KB | None | 0 0
  1. library(tidyverse)
  2. library(gridExtra)
  3. library(grid)
  4. library(imputeTestbench)
  5. library(PSF)
  6. library(imputePSF)
  7. library(imputeTS)
  8. library(scales)
  9.  
  10. # total obs in each simulated time series
  11. n <- length(nottem)
  12.  
  13. # average of nottem for centering simulated data
  14. avenot <- mean(nottem)
  15.  
  16. # create simulated data
  17. # wvs is periodic only, nrms is random only, sim is combo of the two
  18. simdat <- crossing(
  19. mts = seq(0, 1, length = 25),
  20. sds = seq(0, 8, length = 25)
  21. ) %>%
  22. group_by(mts, sds) %>%
  23. nest %>%
  24. mutate(
  25. wvs = map(mts, function(mts) ((as.numeric(nottem) - avenot) * mts) + avenot),
  26. nrm = map(sds, function(sds) rnorm(n = n, sd = sds))
  27. ) %>%
  28. select(-data) %>%
  29. mutate(
  30. sim = pmap(list(wvs, nrm), function(wvs, nrm) wvs + nrm)
  31. )
  32.  
  33. # run imputePSF with imputeTestbench, compare with replacement by means
  34. simres <- simdat %>%
  35. mutate(
  36. errs = map(sim, impute_errors,
  37. methods = c("impute_PSF", "na.mean"), # "na.approx", "na.locf", "na.mean"),
  38. smps = 'mar', missPercentFrom = 30, missPercentTo = 30, blck = 100,
  39. errorParameter = 'mape')
  40. )
  41.  
  42. # optionally save and reload, simres takes a while
  43. save(simres, file = 'simres.RData', compress = 'xz')
  44. # load(file = 'simres.RData')
  45.  
  46. ##
  47. # plot output
  48.  
  49. # example plot for simulated observations, data prep
  50. toplo <- simdat %>%
  51. filter(mts %in% c(0, 0.5, 1) & sds %in% c(0, 4, 8)) %>%
  52. mutate(ind = list(1:n)) %>%
  53. unnest
  54.  
  55. # plot
  56. p1 <- ggplot(toplo, aes(x = ind, y = sim)) +
  57. geom_line(size = 0.25) +
  58. geom_point(size = 0.5, alpha = 0.8, colour = 'black') +
  59. facet_grid(mts ~ sds) +
  60. theme_bw(base_family = 'serif', base_size = 14) +
  61. theme(
  62. strip.background = element_blank()
  63. ) +
  64. scale_y_continuous('Simulated values') +
  65. scale_x_continuous('Index')
  66.  
  67. # plot of imputation differences, data prep
  68. pls <- simres %>%
  69. mutate(
  70. toplo = map(errs, as.data.frame)
  71. ) %>%
  72. select(mts, sds, toplo) %>%
  73. unnest %>%
  74. select(-Parameter) %>%
  75. mutate(
  76. errdf = na.mean - impute_PSF,
  77. dir = ifelse(errdf > 0, '+', '')
  78. )
  79.  
  80. # ggplot base theme
  81. pbase <- theme_bw(base_family = 'serif') +
  82. theme(
  83. panel.grid.major = element_blank(),
  84. panel.grid.minor = element_blank(),
  85. axis.text.x = element_text(size = 8),
  86. axis.text.y = element_text(size = 8),
  87. legend.position = 'top',
  88. legend.direction = 'horizontal',
  89. plot.margin = unit(c(1,1,0,0), "lines"),
  90. strip.background = element_blank(),
  91. strip.text.y = element_text(angle = 0, hjust = 0, vjust = 0.5),
  92. panel.background = element_rect(fill = 'black')
  93. )
  94.  
  95. # plot
  96. p2 <- ggplot(pls) +
  97. geom_tile(aes(y = mts, x = sds, fill = errdf), colour = 'black') +
  98. geom_text(aes(y = mts, x = sds, label = dir)) +
  99. pbase +
  100. scale_y_reverse('Increasing signal', expand = c(0, 0)) +
  101. scale_x_continuous('Increasing noise', expand = c(0, 0)) +
  102. scale_fill_gradient2('Error difference\n(mean replacement - imputePSF)', low = muted("red"), mid = "white", high = muted("green"), midpoint = 0) +
  103. guides(fill = guide_colourbar(barheight = 0.5, barwidth = 8))
  104.  
  105. gparg <- gpar(fontfamily = 'serif', cex = 1.2)
  106.  
  107. # first plot save as jpeg
  108. jpeg('simex.jpg', height = 5, width = 6.5, units = 'in', res = 500, quality = 100)
  109.  
  110. grid.arrange(
  111. arrangeGrob(
  112. p1,
  113. top = textGrob('Increasing noise', gp = gparg),
  114. right = textGrob('Increasing periodicity', rot = 270, gp = gparg))
  115. )
  116. dev.off()
  117.  
  118. # second plot save as jpeg
  119. jpeg('simres.jpg', height = 4, 4.5, units = 'in', res = 500, quality = 100)
  120. p2
  121. dev.off()
Add Comment
Please, Sign In to add comment