# CHAP 18 Styracaceae exercise
#Preliminaries
Styracaceae<-c(3,2,1,2,3,1,1,2,2,5,8,87) # numbers of spp in same order as genus list
Styracaceae<-sort(Styracaceae)
num_genera<-length(Styracaceae)
nsp<-sum(Styracaceae)
list3<-c(1:(nsp-1))
1) Styrax
2)
largest<-NULL
for(i in 1:1000000){
breaks<-sample(list3,(num_genera-1)) # creating break points that are integers between 1 and num_species minus 1
breaks<-sort(breaks)
# if a random break happens to be number 1, the vector ādā has a zero added to its left hand end so that one minus zero will give the number of species in the first random genus
d<-c(min(list3)-1, breaks)
# if a random break point happens to be the number of species minus one, the last length of the broken stick must just include the last species. This is allowed for in āeā by adding max(list3)+1 (i.e. nsp).
e<-c(breaks,max(list3)+1)
r<-sort(e-d)
largest<-c(largest,r[12])}
length(which(largest>=87))
## When we ran it it was about 1 in 100,000
3)
i) Possibly paraphyletic with the other genera just being recognised as separate by taxonomists because they have some unique derived character state
ii) It has been particularly evolutionarily successfull because of some combination of traits, such as being a generalist capable of evolving to fill a range of niches
iii) The ladybird data are from a rather species-poor island (with a few recent introductions) whereas the Styracaceae data are the global numbers of species
4)
mem<-NULL
start_time<-proc.time()
for(i in 1:10000){
breaks<-sample(list3,(num_genera-1))
breaks<-sort(breaks)
d<-c(min(list3)-1, breaks)
e<-c(breaks,max(list3)+1)
r<-sort(e-d)
mem<-rbind(mem,r)}
proc.time()-start_time
largest<-NULL
start_time<-proc.time() # start computer clock
for(i in 1:10000){
breaks<-sample(list3,(num_genera-1))
breaks<-sort(breaks)
d<-c(min(list3)-1, breaks)
e<-c(breaks,max(list3)+1)
r<-sort(e-d)
largest<-c(largest,r[12])}
proc.time()-start_time