top of page
Search

Produce animation in R illustrating uniform and Gaussian density




 
#Number of simulations and chunk-size
sN <- 265
sM <- 10
#Line from 0 to 1 will be divided into 40 bins
bin <- seq(0 + (1 / 40) / 2, 1 - (1 / 40) / 2, 1 / 40)
bincnt <- rep(0, 40)
#Vertical distance between dots
icr <- 0.01
printx <- NULL
printy <- NULL
for (i in 1:sN) {
  if (i < 15)
    sMI <- 1
  if (i > 14)
    sMI <- sM
  sim <- runif(sMI)
  Sys.sleep(0.1)
  pointx <- rep(0, sMI)
  pointy <- rep(0, sMI)
  for (j in 1:sMI) {
    idx <- sim[j] %/% (1 / 40) + 1
    pointx[j] <- bin[idx]
    pointy[j] <- bincnt[idx] + 1
    pointy[j] <- max(icr, (pointy[j] * icr) %% 1) %% 1
    bincnt[idx] <- bincnt[idx] + 1
  }
  printx <- c(printx, pointx)
  printy <- c(printy, pointy)
  plot(
    seq(0, 1, 0.01),
    dunif(seq(0, 1, 0.01)),
    type = "l",
    xlab = "value",
    ylab = "Uniform density",
    ylim = c(0, 1.2),
    col = "red",
    lwd = 5
  )
  points(printx, printy, pch = 20)
}




 
#Procedure repeated with Gaussian distribution
sN <- 365
sM <- 5
#Line from -3 to 3 divided into 100 bins
bin <- seq(-3 + (6 / 100) / 2, 3 - (6 / 100) / 2, 6 / 100)
bincnt <- rep(0, 100)

icr <- 0.01
printx <- NULL
printy <- NULL
for (i in 1:sN) {
  if (i < 15)
    sMI <- 1
  if (i > 14)
    sMI <- sM
  sim <- rnorm(sMI)
  Sys.sleep(0.1)
  for (j in 1:sMI) {
    idx <- (3 + sim[j]) %/% (6 / 100)
    if (idx > 0 & idx < 101) {
      pointx <- bin[idx]
      pointy <- bincnt[idx] + 1
      pointy <- max(icr, (pointy * icr) %% 1) %% 1
      printx <- c(printx, pointx)
      printy <- c(printy, pointy)
      bincnt[idx] <- bincnt[idx] + 1
    }
  }
  plot(
    seq(-3, 3, 0.01),
    dnorm(seq(-3, 3, 0.01)),
    type = "l",
    xlab = "value",
    ylab = "Gaussian density",
    ylim = c(0, 0.5),
    col = "red",
    lwd = 5
  )
  points(printx, printy, pch = 20)
}




 
getwd()
setwd("C:/plots")
getwd()

#Procedure repeated with Gaussian distribution
sN <- 365
sM <- 5
#Line from -3 to 3 divided into 100 bins
bin <- seq(-3 + (6 / 100) / 2, 3 - (6 / 100) / 2, 6 / 100)
bincnt <- rep(0, 100)
#install.packages("animation")
#library(animation)
#ani.record(reset = TRUE)
icr <- 0.01
printx <- NULL
printy <- NULL
png("GaussianDensity%03d.png")
for (i in 1:sN) {
  if (i < 15)
    sMI <- 1
  if (i > 14)
    sMI <- sM
  sim <- rnorm(sMI)
  Sys.sleep(0.1)
  for (j in 1:sMI) {
    idx <- (3 + sim[j]) %/% (6 / 100)
    if (idx > 0 & idx < 101) {
      pointx <- bin[idx]
      pointy <- bincnt[idx] + 1
      pointy <- max(icr, (pointy * icr) %% 1) %% 1
      printx <- c(printx, pointx)
      printy <- c(printy, pointy)
      bincnt[idx] <- bincnt[idx] + 1
    }
  }
    plot(
      seq(-3, 3, 0.01),
      dnorm(seq(-3, 3, 0.01)),
      type = "l",
      xlab = "value",
      ylab = "Gaussian density",
      ylim = c(0, 0.5),
      col = "red",
      lwd = 5
    )
    points(printx, printy, pch = 20)
  }
dev.off()

#oopts = ani.options(interval = 0.1)
#ani.replay()

#install.packages("R2SWF")
library(R2SWF)
output = image2swf(sprintf("GaussianDensity%03d.png", 1:365), interval = 0.05)
swf2html(output)

0 views0 comments

Recent Posts

See All

dplyr or base R

dplyr and tidyverse are convenient frameworks for data management and technical analytic programming. With more than 25 years of R...

Comments


bottom of page