Skip to content

Instantly share code, notes, and snippets.

@m-note
Last active August 29, 2015 14:26
Show Gist options
  • Save m-note/b4e7bce1349a03cd5430 to your computer and use it in GitHub Desktop.
Save m-note/b4e7bce1349a03cd5430 to your computer and use it in GitHub Desktop.
library(WDI)
x <- WDI(country="all", indicator=c("AG.AGR.TRAC.NO"),
start=1980, end=2011)
y <- na.exclude(x[,3])
mean(y)
# 881532.3
sd(y)
# 2925623
var(y)
# 8.559267e+12
var(y)/10000
# 855926716
# Central Limit Theorem
sample_mean <- NULL
for (i in 1:10000){
sample <- sample(y, 2200, replace = FALSE, prob = NULL)
sample_mean[i] <- mean(sample)
}
hist(sample_mean)
plot(density(sample_mean))
mean(sample_mean)
sd(sample_mean)
sd(y)/sqrt(10000)
# Law of Large Number
#データ数が多いと、サンプル平均は真の平均に近づく
mean(sample(y, 10, replace = FALSE, prob = NULL))
mean(sample(y, 100, replace = FALSE, prob = NULL))
mean(sample(y, 1000, replace = FALSE, prob = NULL))
mean(sample(y, 2000, replace = FALSE, prob = NULL))
mean(sample(y, 3000, replace = FALSE, prob = NULL))
dif_mean <- NULL
for (i in seq(20,3860,20)){
dif_mean[i] <- mean(y) - mean(sample(y, i, replace = FALSE, prob = NULL))
}
plot(dif_mean)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment