%9-27-2011 %Ecological simulation models for Helmus and Ives Phylogenetic species-area %curves %There are several if statements that need to be changed in order to run %the different simulation models: species sorting vs random placement vs limiting similarity, and %the different scenarios: with signal vs without signal vs with signal and %repulsion %The code is currently set to produce one simulation of species sorting %with repulsion global nY nX options=optimset('MaxFunEvals',10^4,'MaxIter',10^4,'TolX',10^-8,'TolFun',10^-8); randn('seed',2); rand('seed',2); %USE THIS R CODE TO PRODUCE A COVARIANCE MATRIX FOR A BALANCED TREE OF 32 SPECIES %library(ape) %write.table(vcv.phylo(compute.brlen(stree(n=32,type="balanced"),power=1)),file="balancedCov.txt",sep=" ",row.names=FALSE,col.names=FALSE) load balancedCov.txt -ascii V=balancedCov; SAcurveMat=[]; %Holds the SA data across multiple reps ntspp=length(V); alphabeticalindex=1:ntspp; %V=V(alphabeticalindex,alphabeticalindex); V=V-min(V(:)); V=V/max(V(:)); V=10^-5*round(10^5*V); if 0 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % Grafen's rho %adjust strength of phylogenetic signal. Not used in published simulations rho=1; V=1-(1-V).^rho; V=V/max(V(:)); %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% end if 1 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % reduce phylogenetic signal in the environment: set to 0.4 to get Fig 2B, % repulsion simulation. Not used for the species sorting and random % placement simulations lambda=.4; Venv=(1-lambda)*V+lambda*eye(ntspp); %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% else Venv=V; end if 1 iD=chol(Venv)'; % simulate with phylosignal else iD=eye(length(Venv(:,1))); %simulate without phylosignal end % number of sites m=100; % set up environmental gradient among sites mx=-(m-1)/2:(m-1)/2; Vscale=20; %scale the strength of the phylogeny SDscale=100; %scale the strength of the phylogeny in the limited dispersal simulation sd=1000; %sd around x*i, speices preferences %set this to 1000 if including repulsion model and 100 otherwise Xscale=1; %scale the strength of the environmental gradient Mscale=.7; %stochasticity for rt=1:1, %Choose number of simulations to perform. Simulated 100 for manuscript if 1 % Simulate Species Sorting e=iD*randn(ntspp,1); e=Vscale*(e-mean(e))/std(e); nx=e; X=exp(-(mx'*ones(1,ntspp)-ones(m,1)*nx').^2/sd); X=Xscale*X; X=diag(1-Mscale*rand(1,m))*X; X0=X; else if 0 % Simulate Random Placement e=iD*randn(ntspp,1); e=Vscale*(e-mean(e))/std(e); X=exp(-(ones(m,1)*e').^2/sd); X=Xscale*X; X=diag(1-Mscale*rand(1,m))*X; X0=X; else % Simulate Limited Dispersal sd=iD*randn(ntspp,1); %simulate dispersal limitation as sigma sd=SDscale*(sd-mean(sd))/std(sd); sd=repmat((sd+abs(min(sd))+1)',m,1); iDopt=eye(length(Venv(:,1))); %simulate optimal value without phylosignal for the neutral model e=iDopt*randn(ntspp,1); e=Vscale*(e-mean(e))/std(e); nx=e; X=exp(-(mx'*ones(1,ntspp)-ones(m,1)*nx').^2./sd); X=Xscale*X; X=diag(1-Mscale*rand(1,m))*X; X0=X; end end if 1 %add phylogenetic repulsion %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % repulsion matrix compscale=1000; Vcomp=V\eye(ntspp); dVcomp=diag(diag(Vcomp).^-.5); Vcomp=dVcomp*Vcomp*dVcomp; Vcomp=compscale*Vcomp; iDcomp=chol(Vcomp)'; bcomp=iDcomp*randn(ntspp,m); bcomp0=0; Xcomp=exp(bcomp0+bcomp)./(1+exp(bcomp0+bcomp)); X=X.*Xcomp'; %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% end Y=(rand(size(X))0; n=sum(y); nspp=n+nspp; if n>1 C=V(y,y); ratio=(n*trace(C)-sum(sum(C)))/(n*(n-1)); pd=tril(2*(1-C),-1); pd=mean(pd(pd~=0)); mpd=mpd+pd; psv=psv+ratio; ct=ct+1; end end nspp=nspp/reps; psv=psv/ct; mpd=mpd/ct; SAcurve=[SAcurve;s a nspp psv mpd]; end end SAcurveMat=[SAcurveMat;repmat(rt,length(SAcurve),1) SAcurve]; end p1=find(SAcurve(:,1)==1); p2=find(SAcurve(:,1)==2); figure(2) subplot(3,1,1) plot(SAcurve(p1,2),SAcurve(p1,3),'r.', SAcurve(p2,2),SAcurve(p2,3),'b.') title('richness one rep') subplot(3,1,2) plot(SAcurve(p1,2),SAcurve(p1,4),'r.',SAcurve(p2,2),SAcurve(p2,4),'b.') title('psv one rep') %axis([min(SAcurve(p1,2)) max(SAcurve(p1,2)) .35 .7]) %Change the axis scaling for the other simulations axis([min(SAcurve(p1,2)) max(SAcurve(p1,2)) .67 .74]) %Axis scaling for the repulsion simulations subplot(3,1,3) plot(SAcurve(p1,2),SAcurve(p1,5),'r.',SAcurve(p2,2),SAcurve(p2,5),'b.') title('mpd one rep') meanSR=accumarray(SAcurveMat(:,2:3),SAcurveMat(:,4),[],@mean); meanPSV=accumarray(SAcurveMat(:,2:3),SAcurveMat(:,5),[],@mean); meanMPD=accumarray(SAcurveMat(:,2:3),SAcurveMat(:,6),[],@mean); figure(3) subplot(3,1,1) plot(SAcurve(p1,2),meanSR(1,:),'r.', SAcurve(p2,2),meanSR(2,:),'b.') title('richness mean of all reps') subplot(3,1,2) plot(SAcurve(p1,2),meanPSV(1,:),'r.', SAcurve(p2,2),meanPSV(2,:),'b.') title('psv mean of all reps') %axis([min(SAcurve(p1,2)) max(SAcurve(p1,2)) .35 .7]) %Change the axis scaling for the other simulations axis([min(SAcurve(p1,2)) max(SAcurve(p1,2)) .67 .74]) %Axis scaling for the repulsion simulations subplot(3,1,3) plot(SAcurve(p1,2),meanMPD(1,:),'r.', SAcurve(p2,2),meanMPD(2,:),'b.') title('mpd mean of all reps') %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %2-27-2011 %Macroevolutionary simulation model with allopatric and in situ speciation (Figure 3) from Helmus and Ives Phylogenetic diversity area curves % number of species at end of evolution n=135; % number of islands m=15; % probabilities of species persistence immediately following speciation % event (to allow different island sizes) psurv=.9.^(1:m); % probability that speciation is sympatric (as opposed to allopatric) psympatric=.9; psvhold=[]; srhold=[]; nreps=100; %Determine number of simulations to perform. 10000 were used for the manuscript. for o=1:nreps %run the program for nreps and take means i=1; V=0; location=ceil(m*rand(1)); while ipsympatric while daughterlocation==location(mother) daughterlocation=ceil(m*rand(1)); end end % determine if the daughter species survives according to psurv, and % add time to V until the next speciation event if rand(1)