User Tools

Site Tools


people:kargin:math447:r_examples

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

people:kargin:math447:r_examples [2016/03/05 15:31]
kargin
people:kargin:math447:r_examples [2016/03/05 15:40] (current)
kargin
Line 1: Line 1:
 +== Examples of R-code ==
  
 +=== Example: Approximation of the Gaussian distribution by sums of Bernoulli and uniform random variables. ===
 +
 +<​code>​
 +#Generating data
 +repet <- 10000
 +size <- 100 
 +p <- .5
 +data <- (rbinom(repet,​ size, p) - size * p) / sqrt(size * p * (1-p))
 +hist(data, freq = FALSE)
 +
 +#Histograms
 +
 +#By default you get a frequency histogram. ​
 +#To get a density you set "freq = FALSE"​.
 +#To control the number of bins use "​breaks = "​. ​
 +
 +hist(data, breaks = size/2, col = '​red',​ freq = FALSE)
 +
 +#Plotting the pdf of the normal distribution
 +
 +x <- seq(min(data) - 1, max(data) + 1, .01)
 +lines(x, dnorm(x), col='​green',​ lwd = 4)
 + 
 +#similar exercise for uniform random variables.
 +
 +size <- 3 
 +data <- runif(repet * size)
 +data.matrix ​ <- matrix(data,​ nrow = size)
 +mu <- size * 1/2
 +sigma <- sqrt(size * 1/12)
 +data <- (colSums(data.matrix) - mu )/ sigma
 +hist(data, breaks = 100, col = '​red',​ freq = FALSE)
 +lines(x, dnorm(x), col = '​green',​ lwd = 4)
 +</​code>​