Skip to content

Instantly share code, notes, and snippets.

@PsychBrief
Last active October 23, 2018 21:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save PsychBrief/02cecb4e076bcd02729042bfa5febfd8 to your computer and use it in GitHub Desktop.
Save PsychBrief/02cecb4e076bcd02729042bfa5febfd8 to your computer and use it in GitHub Desktop.
#Binomial distribution of 30 attempts with a probability of .5, simulated 100,000 times
g <- rbinom(100000, 30, .5)
graph1 <- hist(g,xlab = expression(paste(italic(n),'=30, ', italic(P),'=.5')),main = "Simulated data for 30 trials with a probabiltiy of .5", col = "skyblue2")
#Binomial distribution of 30 attempts with a probability of .15, simulated 100,000 times
h <- rbinom(100000, 30, .15)
graph2 <- hist(h,xlab = expression(paste(italic(n),'=30, ', italic(P),'=.15')),main = "Simulated data for 30 trials with a probabiltiy of .15", col = "skyblue2")
#Binomial distribution of 30 attempts with a probability of .001, simulated 100,000 times
j <- rbinom(100000, 30, .01)
graph3 <- hist(j,xlab = expression(paste(italic(n),'=30, ', italic(P),'=.01')),main = "Simulated data for 30 trials with a probabiltiy of .01", col = "skyblue2")
#Binomial distribution of 100 attempts with a probability of .001, simulated 100,000 times
k <- rbinom(100000, 100, .01)
graph4 <- hist(k,xlab = expression(paste(italic(n),'=100, ', italic(P),'=.01')),main = "Simulated data for 100 trials with a probabiltiy of .01", col = "skyblue2")
#Simulated Poisson distribution where the x axis is between 0 and 20 (as these are the vectors of quantiles) and λ=6. n=number of random values to return. lwd=line width. Normal curve line superimposed on the graph with a mean of the simulated data, SD of the square root of the simulated data's mean
x = 0:20; pdf = dpois(x, 6)
y = rpois(10^6, 6); up=max(y)
hist(y, prob=T, br=(-1:up)+.5, col="skyblue2", xlab="x", main="Simulated Sample from a Poisson distribution λ=6 with Normal Approximation")
curve(dnorm(x, mean(y), sd(y)), col="red", lwd=2, add=T)
#Simulated Poisson distribution where the x axis is between 0 and 20 (as these are the vectors of quantiles) and λ=1. n=number of random values to return. lwd=line width. Normal curve line superimposed on the graph with a mean of the simulated data, SD of the square root of the simulated data's mean
m = 0:20; pdf = dpois(m, 1)
n = rpois(10^6, 1); up=max(n)
hist(n, prob=T, br=(-1:up)+.5, col="skyblue2", xlab="x", main="Simulated Sample from a Poisson distribution λ=1")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment