R.txt
资源名称:R.rar [点击查看]
上传用户:shuj007
上传日期:2008-07-06
资源大小:2k
文件大小:3k
源码类别:

DNA

开发平台:

TEXT

  1. R语言 
  2. buffon=function(n,l,d){
  3. #win.graph()
  4. par(mfrow=c(2,1))
  5. Y<-runif(n,0,d/2);H<-runif(n,0,pi/2)
  6. x<-as.integer(Y<(l*sin(H))/2)
  7. phat<-cumsum(x)/seq(n)
  8. pihat<-(2*l)/(d*phat)
  9. ts.plot(phat,xlab="N",ylab="Estimated Proportion",ylim=c(0,1))
  10. abline(h=(2*l)/(pi*d),col="red")
  11. title("Proportion of Crossings")
  12. ts.plot(pihat,xlab="N",ylab="Estimated Pi")
  13. title("Running Estimate of Pi")
  14. abline(h=pi,col="red")
  15. out<-list(x,pihat[n],phat[n])
  16. names(out)<-c("Outcomes","Pihat,","Phat")
  17. out
  18. }
  19. BallandX<-function(a,b,c,d,n){
  20. print("BallandX(a,b,c,d,n);a<b<c<d,n is an integer")
  21. Allball<-c("W","W","W","B","B")
  22. x<-NULL;x[1]<-0;NoS1<-0;NoS2<-0
  23. for (i in 1:n){
  24. randomBall<-sample(Allball,3)
  25. S1<-runif(n,a,b);S2<-runif(n,c,d)
  26. if(sum(randomBall=="W")>sum(randomBall=="B")){
  27. x[i]<-sample(S1,1);NoS1=NoS1+1
  28. }else {x[1]<-sample(S2,1);NoS2=NoS2+1
  29. }#if
  30. }#for
  31. plot(x);list(NoS1/n,NoS2/n)
  32. }#function
  33. BallandX(1,2,3,4,5000)
  34. par(mfrow=c(3,2))
  35. x<-seq(-0.01,5,0.01);
  36. plot(x,ppois(x,1),type="s",ylab="F(x)",main="Poisson(l)CDF")
  37. Xrp<-rpois(100,1);plot(Xrp,main="Poisson")
  38. plot(function(x) pnorm(x,log=TRUE),-50,10,main="log{Normal Cumulative}")
  39. Xrn<-rnorm(1000);plot(Xrn,main="Normal")
  40. plot(function(x) plnorm(x,meanlog=0,sdlog=1),0,100,main="longnormal CDF")
  41. Xrln<-rlnorm(1000);plot(Xrln,main"Lognormal")
  42. #Rejection sampling for f(x)=x^2*exp(-x)
  43. rej.sim<-function(n){
  44. r<-NULL
  45. for (i in 1:n){
  46. t<--1
  47. while (t<0){
  48. x<-rexp(1,1);
  49. y<-runif(1,0,exp(-x))
  50. if(x>1) t<--y
  51. else t<-x^2*exp(-x) -y
  52. #step3 accept when f(x)-y(x)>0,i.e t>0
  53. }
  54. r[i]<-x;
  55. };
  56. r;
  57. }
  58. par(mfrow=c(1,2));plot(rej.sim(1000));hist(rej.sim(1000))
  59. bvsim<-function(n,me,sd,ro){
  60. x<-rnorm(n,me[1],sd[1])
  61. y<-rnorm(n,me[2]+ro*sd[2]/sd[1]*(x-me[1]),sd[2]*sqrt(1-ro*ro))
  62. cbind(x,y)
  63. }
  64. y<-bvsim(1000,c(0,0),c(1,1),0.5)
  65. sum(y[,1]<1&y[,2]<1)/1000
  66. par(mfrow=c(3,1))
  67. plot(y[,1])
  68. plot(y[,2])
  69. plot(y[,1],y[,2])
  70. bootstrap<-function(x,nboot,theta,...){
  71. z<-list()
  72. data<-matrix(sample(x,size=length(x)*nboot,replace=T),nrow=nboot)
  73. bd<-apply(data,1,theta,...)
  74. est<-theta(x,...)
  75. z$est<-est
  76. z$distn<--bd
  77. z$bias<-mean(bd)-est
  78. z$se<-sqrt(var(bd))
  79. z
  80. }
  81. xx<-c(1:10)
  82. bootstrap(xx,200,mean)$est
  83. hist(bootstrap(xx,200,mean)$distn)
  84. law.dat<-matrix(c(576.,635.,558.,578.,666.,580.,555.,661.,651.,605.,653.,575.,545.,572.,594.,3.39,3.30,2.81,3.03,3.44,3.07,3.00,3.43,3.36,3.13,3.12,2.74,2.76,2.88,2.96),
  85. nrow=15,ncol=2,dimnames=list(character(0),c("LSAT","GPA")))
  86. theta1<-
  87. grb<-read.table("Lab02RCompIntSta_GRB.R",header=T,skip=1#第一行不要读)
  88. flux<-grb[,2#第二列];hist(flux)#做图;
  89. n<-length(flux);xx=sort(flux)#由高到低排列;yy=(1:n)/n;plot(yy,xx);plot(xx,log(1-yy+1/n),xlab="flux",ylab="log(1-F(flux))")
  90. mean(flux)#this is the MLE
  91. reslm<-lm(log(1-yy+1/n)~xx)#模型
  92. fittedvalue<--1/as.numeric(reslm$coefficients[2]#取第二个参数);fittedvalue
  93. source("Lab02RCompIntSta_motor.R",echo=TRUE)
  94. plot(motor.dat[,1],motor.dat[,2])
  95. plot(motor2.dat[,1],motor2.dat[,2])
  96. em.reg(motor2.dat,30)
  97. library("PROcess")
  98. fdat<-system.file("Test",package="PROcess")
  99. fs<-list.files(fdat,full.names=TRUE)
  100. fl<-read.files(fs[1])
  101. plot(fl,type="l",xlab="m/z")
  102. title(basename(fs[1]))
  103. bseoff<-bslnoff(fl,method="loess",bw=0.1,xlab="m/z",plot=TRUE);title(basename(fs[1]))
  104. pkgobj<-isPeak(bseoff,span=81,sm.span=11,plot=TRUE,zerothrsh=2,area.w=0.003,ratio=0.2,main="a)")
  105. specZoom(pkgobj,xlim=c(5000,10000),main="b)")