# R code for “The efficiency of the lazy scientist”

Here’s the R code I used for the post “The efficiency of the lazy scientist”. Feel free to grab it and tinker.

# Code for Efficiency Of The Lazy Scientist
par(mfrow=c(2,2))

# Set T = true underlying value of the thing to be measured

T <- 100

# Generate H = hard to measure variate centred on T
# Observations of H are ~T + N(0,sdH) where VH is small
# Generate E = easy to measure variate centred on T
# Observations of E are ~T + N(0,sdE) where VE is large

sdH <-2
sdE <-8

H <- rnorm(10000,mean=100, sd=sdH)
E <- rnorm(10000, mean=100, sd=sdE)

hist(H, main = paste(“Distribution of H”),xlim=c(70,130))
summary(H)
sd(H)
hist(E, main = paste(“Distribution of E”),xlim=c(70,130))
summary(E)
sd(E)

write.table(H, “c:/data/H.txt”, sep=”,”)
write.table(E, “c:/data/E.txt”, sep=”,”)

# Now draw samples from H and E to examine their distribution.
# First, draw 10000 samples of size NH from H
# and calculate the mean of each sample.

NH<-10
Hdraws <- sample(H, NH*10000, replace = TRUE, prob = NULL)
Hdraws <- matrix(Hdraws,NH)
Hdrawmeans = apply(Hdraws, 2, mean)
summary(Hdrawmeans)
sd(Hdrawmeans)
hist(Hdrawmeans, main = paste(“Distribution of estimates via H”),xlim=c(95,105))
write.table(Hdrawmeans, “c:/data/Hdrawmeans.txt”, sep=”,”)

# Next, draw 10000 samples of size NE from E
# and calculate the mean of each sample.

NE<-200
Edraws <- sample(E, NE*10000, replace = TRUE, prob = NULL)
Edraws <- matrix(Edraws,NE)
Edrawmeans = apply(Edraws, 2, mean)
summary(Edrawmeans)
sd(Edrawmeans)
hist(Edrawmeans, main = paste(“Distribution of estimates via E”),xlim=c(95,105))
write.table(Edrawmeans, “c:/data/Edrawmeans.txt”, sep=”,”)