tirage1=function(p,l,n) { suite=numeric(n) for(j in 1:n) { k=sample(1:4,1) i=1 u=0 vec=c() while(u==0&i<=k) { u=rbinom(1,1,p) vec=c(vec,u) i=i+1 } if(j<=l)cat(k,paste(" "),vec,"\n") suite[j]=u } prop=mean(suite) res=list(prop=prop,proba=(1-(1-p)*(1-(1-p)^4)/(4*p))) res } tirage1(0.2,10,100000) tirage2=function(p,n) { suite=logical(n) for(j in 1:n)suite[j]=rgeom(1,p)+1<=sample(1:4,1) prop=mean(suite) res=list(prop=prop,proba=(1-(1-p)*(1-(1-p)^4)/(4*p))) res } tirage2(0.2,100000) tirage3=function(n,vq) { ddeux=numeric(n) for(i in 1:n) { ddeux[i]=sum((table(sample(1:4,200,replace=T))/200-0.25)^2) } resu= quantile(ddeux,vq) resu } tirage3(1000,c(0.1,0.25,0.50,0.75,0.9)) tirage4=function(n1,n2,x) { vec=numeric(n1) for(j in 1:n1)vec[j]=tirage3(n2,x) plot(vec,xlab="no essai",ylab="D9") abline(h=qchisq(x,3)/800) abline(h=mean(vec),col="red") title(" 200 essais, moyenne des essais et valeur théorique") } tirage4(200,1000,0.9) sondage=function(N,K,n,n0,k,alpha) { res=logical(n0) fb=numeric(n0) fh=numeric(n0) pop=c(rep(1,K),rep(0,N-K)) prop=K/N qq=qnorm(1-(alpha/2)) for(i in 1:n0) { ech=sample(pop,n) moy=mean(ech) s=sqrt(moy*(1-moy)/n) e=qq*s*sqrt((N-n)/(N-1)) fb[i]=moy-e fh[i]=moy+e if(i <=k)cat(c(round(fb[i],4),round(fh[i],4)),"\n") } etend=range(fb,fh) plot(1:n0,fb,type="n",ylim=etend,xlab="no institut",ylab="Proportion observée") segments(1:n0,fb,1:n0,fh) propobs=mean((fb