### Lecture 4

Lecture 4
Runs
• Q:
– Toss a coin 1000000 times.
– Run is an unbroken sequence of H in a row
– What is the length of the longest run
• Take guesses (closest will get a cookie)
• We will write an R code togather
Issues
• Case sensitive dog and Dog are different
• Careful of extra spaces
• Brackets
–()
• arguments of functions sum(coin); for(i in 1:10),
if(coin<1)
• grouping math formulas (1+3)*(4+5)
– [ ] indexing valus in a list coin[10]
– { } grouping commands togather
Are we smarter than chimps?
• In each of the following pair one country has
twice the child mortality rate than the other.
– Malaysia or Russia
– Poland or South Korea
– Pakistan or Vietnam
– Thailand or South Africa
– Sri Lanka or Turkey
• In each of the following pair one country has
twice the child mortality rate than the other.
– Malaysia or Russia
– Poland or South Korea
– Pakistan or Vietnam
– Thailand or South Africa
– Sri Lanka or Turkey
• Collect data and draw a histogram
Chimps histogram
plot(0:5,20*dbinom(0:5,5,.5),type="h",xlab="number of correct answers",ylab="average number of chimps")
• 1000 classes of chimps simulated.
– Classes ordered based on average performance
– Will Show: Worst class, 2.5%, 5%, 25%
nclass=1000
nchimps=20
nquestions=5
count1=matrix(0,nrow=nclass,ncol=nquestions+1)
m1=rep(0,nclass)
for (i in 1:nclass){
data1=rbinom(nchimps,nquestions,.5)
count1[i,]=tabulate(data1,nquestions+1)
m1[i]=mean(data1)
}
i1=order(m1)
i2=order(count1[,1])
par(mfrow=c(2,2))
plot(0:5,count1[i1[1],],type="h",xlab="number of correct answers",ylab="average number of chimps",sub="1:1000")
plot(0:5,count1[i1[25],],type="h",xlab="number of correct answers",ylab="average number of chimps",sub="25:1000")
plot(0:5,count1[i1[50],],type="h",xlab="number of correct answers",ylab="average number of chimps",sub="50:1000")
plot(0:5,count1[i1[250],],type="h",xlab="number of correct answers",ylab="average number of chimps",sub="250:1000")
Chimps histogram