Flaunk
Flaunk

Reputation: 23

R Periodic Missing Plot Points

I am having issues with R plots (probability density and scatter plots, see examples below) where R does not plot some data, in a regular periodic way.

This is a little hard to describe, so hopefully the pictures are helpful. Researching this question was somewhat difficult as any terms for "missing data" or "plot missing" even with terms "periodic" returns issues about imputation or visualization of missing data. If anyone has a better ideas for a question title, please suggest too. Below if some code that produces the problem on my system:

d2<-structure(list(height = c(151.765, 139.7, 136.525, 156.845, 145.415, 163.83, 
149.225, 168.91, 147.955, 165.1, 154.305, 151.13, 144.78, 149.9, 150.495, 
163.195, 157.48, 143.9418, 161.29, 156.21, 146.4, 148.59, 147.32, 147.955, 
161.925, 146.05, 146.05, 152.7048, 142.875, 142.875, 147.955, 160.655, 151.765, 
162.8648, 171.45, 147.32, 147.955, 154.305, 143.51, 146.7, 157.48, 165.735, 
152.4, 141.605, 158.8, 155.575, 164.465, 151.765, 161.29, 154.305, 145.415, 
145.415, 152.4, 163.83, 144.145, 153.67, 142.875, 167.005, 158.4198, 165.735, 
149.86, 154.94, 160.9598, 161.925, 147.955, 159.385, 148.59, 136.525, 158.115, 
144.78, 156.845, 179.07, 170.18, 146.05, 147.32, 162.56, 152.4, 160.02, 149.86, 
142.875, 167.005, 159.385, 154.94, 162.56, 152.4, 170.18, 146.05, 159.385, 
151.13, 160.655, 169.545, 158.75, 149.86, 153.035, 161.925, 162.56, 149.225, 
163.195, 161.925, 145.415, 163.195, 151.13, 150.495, 170.815, 157.48, 152.4, 
147.32, 145.415, 157.48, 154.305, 167.005, 142.875, 152.4, 160, 159.385, 149.86, 
160.655, 160.655, 149.225, 140.97, 154.94, 141.605, 160.02, 150.1648, 155.575, 
156.21, 153.035, 167.005, 149.86, 147.955, 159.385, 161.925, 155.575, 159.385, 
146.685, 172.72, 166.37, 141.605, 151.765, 156.845, 148.59, 157.48, 149.86, 
147.955, 153.035, 160.655, 149.225, 138.43, 162.56, 149.225, 158.75, 149.86, 
158.115, 156.21, 148.59, 143.51, 154.305, 157.48, 157.48, 154.305, 168.275, 
145.415, 149.225, 154.94, 162.56, 156.845, 161.0106, 144.78, 143.51, 149.225, 
149.86, 165.735, 144.145, 157.48, 154.305, 163.83, 156.21, 144.145, 162.56, 
146.05, 154.94, 144.78, 146.685, 152.4, 163.83, 165.735, 156.21, 152.4, 140.335, 
163.195, 151.13, 171.1198, 149.86, 163.83, 141.605, 149.225, 146.05, 161.29, 
162.56, 145.415, 170.815, 159.385, 159.4, 153.67, 160.02, 150.495, 149.225, 
142.875, 142.113, 147.32, 162.56, 164.465, 160.02, 153.67, 167.005, 151.13, 
153.035, 139.065, 152.4, 154.94, 147.955, 144.145, 155.575, 150.495, 155.575, 
154.305, 157.48, 168.91, 150.495, 160.02, 167.64, 144.145, 145.415, 160.02, 
164.465, 153.035, 149.225, 160.02, 149.225, 153.67, 150.495, 151.765, 158.115, 
149.225, 151.765, 154.94, 161.29, 148.59, 160.655, 157.48, 167.005, 157.48, 
152.4, 152.4, 161.925, 152.4, 159.385, 142.24, 168.91, 160.02, 158.115, 152.4, 
155.575, 154.305, 156.845, 156.21, 168.275, 147.955, 157.48, 160.7, 161.29, 
150.495, 163.195, 148.59, 148.59, 161.925, 153.67, 151.13, 163.83, 153.035, 
151.765, 156.21, 140.335, 158.75, 142.875, 151.9428, 161.29, 160.9852, 144.78, 
160.02, 160.9852, 165.989, 157.988, 154.94, 160.655, 147.32, 146.7, 147.32, 
172.9994, 158.115, 147.32, 165.989, 149.86, 161.925, 163.83, 160.02, 154.94, 
152.4, 146.05, 151.9936, 151.765, 144.78, 160.655, 151.13, 153.67, 147.32, 
139.7, 157.48, 154.94, 143.51, 158.115, 147.32, 160.02, 165.1, 154.94, 153.67, 
141.605, 163.83, 161.29, 154.9, 161.3, 170.18, 149.86, 160.655, 154.94, 166.37, 
148.2852, 151.765, 148.59, 153.67, 146.685, 154.94, 156.21, 160.655, 146.05, 
156.21, 152.4, 162.56, 142.875, 162.56, 156.21, 158.75), weight = c(47.8256065, 
36.4858065, 31.864838, 53.0419145, 41.276872, 62.992589, 38.2434755, 55.4799715, 
34.869885, 54.487739, 49.89512, 41.220173, 36.0322145, 47.7, 33.849303, 
48.5626935, 42.3258035, 38.3568735, 48.987936, 42.7226965, 35.493574, 
37.9032815, 35.4652245, 40.312989, 55.111428, 37.5063885, 38.498621, 46.606578, 
38.838815, 35.5786225, 47.400364, 47.8823055, 49.4131785, 49.384829, 56.5572525, 
39.12231, 49.89512, 41.2485225, 38.55532, 42.4, 44.6504625, 58.5984165, 
46.719976, 44.22522, 50.9, 54.317642, 45.8978405, 48.024053, 52.219779, 
47.62716, 45.642695, 42.410852, 36.4858065, 55.9335635, 37.194544, 48.307548, 
37.3362915, 47.173568, 47.286966, 57.549485, 37.931631, 47.2019175, 43.204638, 
50.2636635, 39.3774555, 50.689, 39.4341545, 36.28736, 46.266384, 42.2691045, 
47.62716, 55.7067675, 48.5626935, 42.807745, 35.0683315, 56.755699, 51.255896, 
47.230267, 40.936678, 32.715323, 57.0675435, 42.977842, 39.9444455, 45.9545395, 
41.106775, 47.5988105, 37.5063885, 45.019006, 42.2691045, 54.8562825, 53.523856, 
52.1914295, 42.410852, 49.5832755, 41.730464, 56.018612, 42.1557065, 53.0986135, 
50.235314, 42.52425, 49.101334, 38.498621, 49.8100715, 59.760746, 47.9390045, 
39.292407, 36.8826995, 42.127357, 44.565414, 47.853956, 55.1964765, 32.998818, 
40.879979, 51.2, 49.044635, 53.4388075, 54.090846, 55.3665735, 42.240755, 
40.936678, 49.6966735, 44.338618, 45.9545395, 41.95726, 51.482692, 44.111822, 
32.205032, 56.755699, 52.673371, 36.4858065, 48.8461885, 56.9541455, 42.0990075, 
50.178615, 46.549879, 61.80191, 48.987936, 31.524644, 35.2951275, 45.642695, 
43.885026, 45.5576465, 39.008912, 41.163474, 45.245802, 53.637254, 52.3048275, 
39.0939605, 45.699394, 40.3980375, 51.482692, 38.668718, 39.235708, 44.338618, 
39.519203, 31.071052, 46.776675, 40.6248335, 50.178615, 41.276872, 54.6, 
44.9906565, 35.8054185, 45.2174525, 48.1091015, 45.6710445, 48.420946, 
41.1918235, 38.4135725, 42.127357, 38.2434755, 48.3358975, 38.9238635, 
40.029494, 50.2069645, 54.2892925, 45.6, 39.4341545, 43.204638, 31.864838, 
45.4442485, 38.045029, 36.0889135, 40.879979, 47.910655, 47.7122085, 46.379782, 
41.163474, 36.5992045, 48.137451, 36.7126025, 56.5572525, 38.6970675, 
47.4854125, 36.2023115, 41.276872, 44.7638605, 50.4337605, 55.281525, 37.931631, 
58.456669, 44.4236665, 44.4, 44.565414, 44.622113, 40.483086, 44.0834725, 
34.416293, 32.772022, 35.947166, 49.5549, 53.183662, 37.081146, 40.5114355, 
50.6038575, 43.9700745, 49.89, 33.5941575, 43.8566765, 48.137451, 42.751046, 
33.906002, 39.7176495, 35.947166, 50.915702, 45.756093, 49.214732, 58.8252125, 
43.4597835, 51.9646335, 50.688906, 34.246196, 39.3774555, 59.5622995, 52.16308, 
39.972795, 43.941725, 54.601137, 45.075705, 41.333571, 41.900561, 42.524, 
43.147939, 40.82328, 42.864444, 46.209685, 47.853956, 42.52425, 48.5059945, 
45.869491, 52.900167, 47.570461, 43.544832, 43.431434, 53.2120115, 44.678812, 
47.2019175, 31.6663915, 56.4438545, 55.791816, 47.4854125, 45.1607535, 
45.529297, 48.874538, 46.5782285, 43.885026, 56.0469615, 40.086193, 50.802304, 
46.3, 49.3564795, 44.111822, 51.0291, 40.766581, 37.5630875, 51.59609, 
44.8205595, 43.4030845, 46.719976, 39.5475525, 34.7848365, 39.292407, 
37.4496895, 48.6760915, 35.606972, 43.714929, 48.19415, 50.972401, 43.998424, 
48.19415, 46.6916265, 56.415505, 48.591043, 48.2224995, 47.4854125, 35.550273, 
36.6, 48.9595865, 51.255896, 46.5215295, 36.967748, 48.647742, 38.045029, 
47.286966, 55.394923, 54.204244, 48.477645, 43.0628905, 34.189497, 49.951819, 
44.338618, 33.45241, 47.286966, 46.1246365, 47.400364, 40.8516295, 50.348712, 
45.132404, 42.240755, 41.6454155, 45.2174525, 51.255896, 49.271431, 51.199197, 
43.8566765, 35.5219235, 42.88542, 46.776675, 41.8722115, 38.2, 43.3, 53.637254, 
42.977842, 39.7743485, 43.3463855, 52.673371, 38.441922, 42.807745, 35.890467, 
44.22522, 38.0733785, 44.111822, 44.0267735, 47.8823055, 39.405805, 41.050076, 
40.82328, 47.0318205, 34.246196, 52.16308, 54.0624965, 52.5316235), age = c(63, 
63, 65, 41, 51, 35, 32, 27, 19, 54, 47, 66, 73, 20, 65.3, 36, 44, 31, 39, 29, 
56, 45, 19, 29, 30, 24, 35, 33, 27, 32, 36, 24, 30, 24, 52, 42, 19, 55, 43, 20, 
18, 42, 44, 60, 20, 37, 50, 50, 31, 25, 23, 52, 79.3, 35, 27, 38, 39, 30, 24, 
51, 46, 22, 29, 38, 30, 45, 47, 79, 45, 54, 31, 23, 41, 23, 36, 30, 34, 44, 43, 
73.3, 38, 43, 33, 35, 29, 58, 53, 51, 48, 29, 41, 81.75, 35, 46, 29, 42, 27, 22, 
43, 53, 43, 41, 50, 33, 62, 49, 22, 29, 33, 34, 42, 40, 27, 25, 29, 45, 26, 45, 
45, 85.5999999999999, 26, 24, 57, 22, 24, 21, 79, 50, 40, 64, 32, 38.7, 26, 63, 
62, 22, 41, 19, 74, 41, 33, 53, 18, 37, 61, 44, 35, 23, 55, 53, 59, 57, 35, 29, 
62, 18, 51, 19, 42, 25, 41, 37, 82, 28, 50, 43, 31, 67, 39, 18, 48, 30, 64, 72, 
68, 44, 43, 34, 62, 44, 31, 29, 62, 67, 57, 32, 24, 77, 62, 67, 70, 37, 58, 35, 
30, 26, 21, 41, 46, 49, 28, 83, 54, 54, 68, 68, 56, 57, 22, 40, 19, 41, 
75.9000000000001, 73.9000000000001, 49, 26, 88, 68, 33, 26, 56, 34, 74, 69, 50, 
44, 18, 41, 27, 38, 57, 64.5, 42, 24, 71, 49.5, 33, 28, 47, 27, 55, 
83.4000000000001, 63, 52, 49, 31, 35, 35, 24, 41, 32, 43, 63, 21, 55, 38, 28, 
36, 38, 48, 45, 38, 21, 50, 41, 30, 21, 38, 19, 31, 21, 50, 39, 44, 36, 36, 18, 
58, 58, 33, 21.5, 26, 22, 28, 42, 21, 19, 48, 46, 25, 51, 25, 28, 26, 54, 66, 
20, 25, 38, 51, 48, 27, 22, 60, 43, 27, 30, 29, 23, 30, 41, 42, 43, 35, 75.5, 
64, 38, 24.2, 26, 19, 43, 38, 23, 49, 41, 23, 43, 21, 24, 20, 20, 34, 29, 65, 
46, 43, 39, 43, 70, 26, 48, 44, 33, 41, 37.4, 53, 49, 27, 31, 31, 21, 68), male 
= c(1L, 0L, 0L, 1L, 0L, 1L, 0L, 1L, 0L, 1L, 0L, 1L, 0L, 0L, 0L, 1L, 1L, 0L, 1L, 
0L, 1L, 0L, 0L, 1L, 1L, 0L, 0L, 0L, 0L, 0L, 0L, 1L, 1L, 1L, 1L, 0L, 0L, 1L, 0L, 
1L, 1L, 1L, 0L, 0L, 0L, 0L, 1L, 0L, 1L, 0L, 0L, 0L, 1L, 1L, 0L, 1L, 0L, 1L, 0L, 
1L, 0L, 0L, 1L, 1L, 0L, 1L, 0L, 0L, 1L, 0L, 1L, 1L, 1L, 0L, 0L, 0L, 0L, 1L, 0L, 
0L, 1L, 1L, 0L, 1L, 0L, 1L, 0L, 1L, 0L, 1L, 1L, 1L, 0L, 0L, 1L, 1L, 0L, 1L, 1L, 
0L, 1L, 0L, 0L, 1L, 1L, 0L, 0L, 0L, 1L, 0L, 1L, 0L, 0L, 1L, 1L, 0L, 1L, 1L, 0L, 
0L, 1L, 0L, 1L, 0L, 0L, 0L, 0L, 1L, 0L, 0L, 1L, 1L, 0L, 1L, 0L, 1L, 1L, 1L, 0L, 
1L, 0L, 0L, 0L, 0L, 0L, 1L, 0L, 0L, 1L, 0L, 1L, 0L, 1L, 0L, 1L, 0L, 0L, 1L, 1L, 
0L, 1L, 0L, 1L, 1L, 1L, 0L, 1L, 0L, 0L, 0L, 0L, 1L, 0L, 1L, 0L, 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92L, 93L, 94L, 96L, 97L, 
98L, 100L, 102L, 103L, 104L, 105L, 106L, 107L, 108L, 112L, 113L, 117L, 118L, 
119L, 120L, 121L, 122L, 123L, 125L, 126L, 128L, 129L, 130L, 133L, 134L, 135L, 
136L, 137L, 138L, 140L, 142L, 143L, 146L, 147L, 150L, 151L, 154L, 155L, 156L, 
158L, 159L, 160L, 161L, 162L, 163L, 165L, 166L, 167L, 168L, 169L, 170L, 173L, 
174L, 175L, 176L, 177L, 178L, 179L, 180L, 181L, 182L, 183L, 184L, 185L, 190L, 
191L, 192L, 193L, 194L, 195L, 197L, 198L, 199L, 202L, 204L, 205L, 206L, 207L, 
208L, 209L, 210L, 211L, 212L, 214L, 215L, 216L, 218L, 219L, 223L, 224L, 225L, 
226L, 228L, 229L, 230L, 231L, 233L, 234L, 235L, 236L, 237L, 238L, 239L, 242L, 
244L, 245L, 247L, 248L, 250L, 251L, 252L, 253L, 254L, 255L, 256L, 258L, 259L, 
260L, 261L, 262L, 263L, 265L, 267L, 268L, 269L, 270L, 272L, 274L, 275L, 276L, 
277L, 278L, 279L, 281L, 282L, 283L, 284L, 285L, 286L, 287L, 288L, 289L, 293L, 
294L, 295L, 296L, 297L, 300L, 303L, 304L, 305L, 306L, 309L, 310L, 311L, 313L, 
314L, 315L, 316L, 317L, 319L, 320L, 321L, 322L, 323L, 330L, 336L, 337L, 340L, 
341L, 342L, 343L, 346L, 347L, 350L, 351L, 352L, 353L, 357L, 358L, 366L, 367L, 
372L, 374L, 375L, 380L, 383L, 386L, 387L, 388L, 389L, 393L, 397L, 398L, 400L, 
403L, 406L, 410L, 411L, 412L, 413L, 414L, 415L, 417L, 418L, 419L, 420L, 422L, 
423L, 424L, 425L, 427L, 428L, 430L, 431L, 434L, 436L, 437L, 438L, 439L, 442L, 
443L, 444L, 445L, 446L, 447L, 448L, 451L, 452L, 454L, 458L, 460L, 461L, 462L, 
464L, 465L, 469L, 470L, 473L, 474L, 477L, 478L, 479L, 480L, 482L, 483L, 485L, 
486L, 489L, 491L, 492L, 494L, 496L, 498L, 499L, 500L, 501L, 502L, 503L, 506L, 
507L, 510L, 511L, 515L, 516L, 517L, 519L, 524L, 525L, 529L, 530L, 533L, 534L, 
535L, 538L, 541L, 542L, 544L), class = "data.frame")

mu.list <- seq( from=140, to=160 , length.out=200 )
sigma.list <- seq( from=4 , to=9 , length.out=200 )
post <- expand.grid( mu=mu.list , sigma=sigma.list )
mu.list <- seq( from=140, to=160 , length.out=200 )
sigma.list <- seq( from=4 , to=9 , length.out=200 )
post <- expand.grid( mu=mu.list , sigma=sigma.list )
post$LL <- sapply( 1:nrow(post) , function(i) sum( dnorm(
  d2$height ,
  mean=post$mu[i] ,
  sd=post$sigma[i] ,
  log=TRUE ) ) )
post$prod <- post$LL + dnorm( post$mu , 178 , 20 , TRUE ) +
  dunif( post$sigma , 0 , 50 , TRUE )
post$prob <- exp( post$prod - max(post$prod) )
sample.rows <- sample( 1:nrow(post) , size=1e4 , replace=TRUE , prob=post$prob )
sample.mu <- post$mu[ sample.rows ]
sample.sigma <- post$sigma[ sample.rows ]

But when I plot this, R is not plotting certain points at periodic intervals:

plot(sample.mu)

X

But no problem plotting the other vector plot(sample.sigma)

X

I have reproduced this using Rcmdr, so does not appear to be specific to Rstudio (my normal IDE). What is it about the vector sample.mu that is causing this problem? Also, if anyone has a better idea as to what to title this question, please suggest. Thanks in advance for your help.

The output of sessionInfo() for me is:

R version 3.4.4 (2018-03-15) Platform: x86_64-pc-linux-gnu (64-bit) Running under: Linux Mint 18.3

Matrix products: default BLAS: /usr/lib/openblas-base/libblas.so.3 LAPACK: /usr/lib/libopenblasp-r0.2.18.so

locale: [1] LC_CTYPE=en_US.UTF-8 LC_NUMERIC=C
3 LC_TIME=en_US.UTF-8 LC_COLLATE=en_US.UTF-8 5 LC_MONETARY=en_US.UTF-8 LC_MESSAGES=en_US.UTF-8 [7] LC_PAPER=en_US.UTF-8 LC_NAME=C [9] LC_ADDRESS=C LC_TELEPHONE=C [11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C

attached base packages: [1] parallel stats graphics grDevices utils datasets [7] methods base

other attached packages: [1] rethinking_1.59 rstan_2.17.3
StanHeaders_2.17.2 4 ggplot2_3.0.0

loaded via a namespace (and not attached): [1] Rcpp_0.12.18
rstudioapi_0.6 magrittr_1.5 4 bindr_0.1.1
MASS_7.3-50 munsell_0.5.0 [7] colorspace_1.3-2
lattice_0.20-35 R6_2.2.0 [10] rlang_0.2.2
plyr_1.8.4 dplyr_0.7.4 [13] tools_3.4.4
grid_3.4.4 gtable_0.2.0 [16] loo_2.0.0
coda_0.19-1 withr_2.1.2 [19] matrixStats_0.54.0 yaml_2.2.0 lazyeval_0.2.1 [22] assertthat_0.2.0
tibble_1.4.2 crayon_1.3.4 [25] bindrcpp_0.2.2
gridExtra_2.3 inline_0.3.15 [28] glue_1.3.0
compiler_3.4.4 pillar_1.3.0 [31] scales_1.0.0
stats4_3.4.4 mvtnorm_1.0-8 [34] pkgconfig_2.0.2

Upvotes: 1

Views: 154

Answers (1)

ekstroem
ekstroem

Reputation: 6181

There are a number of reasons for the plots you see and they all have to do with the way you generate and sample your data.

First, both mu.list and sigma.list consist of random samples of your two lists and the both have length 200. You are sampling rows from this expanded list (via expand.grid()) but because of the probabilities of the individual rows and the order of the lists produced by expand.grid() you extract different number of unique values for each of the lists.

> length(unique(sample.mu))
[1] 32
> length(unique(sample.sigma))
[1] 82

You can see why if you plot the post$mu and post$sigma against the probabilities of being selected:

> plot(post$prod, post$mu)
> plot(post$prod, post$sigma)

This produces this graph

enter image description here

As you can see the curvature along the right-hand side (more likely to be sampled) is stronger for post$mu than for post$sigma. Consequently, you get more unique values for sigma than you do for mu. The lower number of unique values for mu can be seen in your plot because of the original grid from mu.list. Because of the larger number of unique values for sigma, you see overplotting of the circles so the grid "disappears".

In other words: no points are missing and the plots are as expected.

Upvotes: 0

Related Questions