coins <- sample(c("Heads","Tails"),501,replace=T)
mean(coins=='Heads') =
0.493014barplot(prop.table(table(coins)))
#A blank vector to hold our results
propHeads <- c()
#Let's run this simulation 2000 times
for (i in 1:2000) {
#Re-create data using the true model
coinsdraw <- sample(c("Heads","Tails"),501,replace=T)
#Re-perform our analysis
result <- mean(coinsdraw=="Heads")
#And store the result
propHeads[i] <- result
}
#Let's see what we get on average
stargazer(as.data.frame(propHeads),type='text')
##
## ============================================================
## Statistic N Mean St. Dev. Min Pctl(25) Pctl(75) Max
## ------------------------------------------------------------
## propHeads 2,000 0.500 0.022 0.427 0.485 0.515 0.589
## ------------------------------------------------------------
#And let's look at the distribution of our findings
plot(density(propHeads),xlab="Proportion Heads",main="Mean of 501 Coin Flips over 2000 Samples")
abline(v=mean(propHeads),col='red')
sample()
as before to pick random data from different categories like Heads and Tails, or integers (say, 1:10
)help(Distributions)
.runif(thismanyobs,min,max)
will draw thismanyobs
observations from the range of min
to max
.runif(thismanyobs)
will assume min=0
and max=1
uniformdata <- runif(5)
uniformdata
## [1] 0.1267818 0.8207126 0.2619525 0.4291249 0.1961915
uniformdata <- runif(2000)
hist(uniformdata,xlab="Random Value",main="Distribution of Random Data from Uniform Distribution",probability=TRUE)
rnorm(thismanyobs,mean,sd)
will draw thismanyobs
observations from a normal distribution with mean mean
and standard deviation sd
rnorm(thismanyobs)
will assume mean=0
and sd=1
normaldata <- rnorm(5)
normaldata
## [1] -2.2990547 0.1119890 -0.3365734 2.2590853 -0.7877652
normaldata <- rnorm(2000)
hist(normaldata,xlab="Random Value",main="Distribution of Random Data from Normal Distribution",probability=TRUE)