Want more features on Pastebin? Sign Up, it's FREE!
Guest

R script

By: a guest on Feb 2nd, 2013  |  syntax: R  |  size: 4.20 KB  |  views: 20  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. numsplits<-1000
  2.  
  3. f=file.choose()
  4. text<-readLines(f,encoding="UTF-8")
  5. nrows<-length(text)
  6. writeLines(text[length(text)], con = "D:\\projects\\basicToR\\junk.txt", sep = "\n", useBytes = FALSE)
  7. k<-read.table("D:\\projects\\basicToR\\junk.txt",sep=",")
  8. ncolumns<-length(k)
  9. print("nrows="); print(nrows)
  10. print("ncolumns="); print(ncolumns)
  11.  
  12. t<-read.table(f,sep=",")
  13. nentries=0;for (i in t){for (k in i){nentries=nentries+1}}
  14. print("nentries=");print(nentries)
  15.  
  16. if(nentries==nrows*ncolumns)    {
  17.          print("Columns, rows, and entries check; we're good to go.")
  18.         }
  19. else{
  20.      print("Columns, rows, and entries don't check; please look at your data file to make sure each line has equal no. of entries.")
  21.         }
  22.  
  23.  
  24.  
  25. #t<-read.table(f,sep=",")
  26. #for (i in seq(1, nrows, 1)) {
  27. #       for (j in seq(1, ncolumns, 1)) {
  28. #               if(j!=ncolumns){
  29. #                       print(t[i,j]);print(",");
  30. #               }
  31. #               else{
  32. #                       print(t[i,j]);#chr$(10)
  33. #               }
  34. #       }
  35. #}
  36.  
  37.  
  38.  
  39. nfirsthalf<-ncolumns/2
  40. nsechalf<-ncolumns-nfirsthalf
  41. sumr<-0
  42. sumsteppedr<-0
  43.  
  44. for (split in seq(1, numsplits, 1)){
  45.         firstsofar=0
  46.         sum1=0
  47.         count1=0
  48.         sum2=0
  49.         count2=0
  50.         sumsteppedrspear=0
  51.         sumsteppedrrulon=0
  52.         randnum<-list()
  53.   score1<-list()
  54.   randnum2<-list()
  55.  
  56.         for (i in seq(1, nfirsthalf, 1)) {
  57.                 randnum[i]=round(ncolumns*runif(1, min=0, max=1)+1)
  58.                 taken<-"no"
  59.                 for (q in seq(1,firstsofar,1)) {
  60.                         if( randnum[i]==randnum[q]) {taken="yes"}
  61.                 }
  62.                 if (taken=="yes"){randnum[i]=round(ncolumns*runif(1, min=0, max=1)+1)}
  63.             print("Keeper is");print(randnum(i))
  64.                 firstsofar=firstsofar+1
  65.         }
  66.  
  67.         for (i in seq(1, nfirsthalf, 1)) {
  68.                 print(randnum[i])
  69.         }
  70.  
  71.         j<-0
  72.         for (i in seq(1, ncolumns, 1)) {
  73.                 taken<-"no"
  74.                 for (q in seq(1, nfirsthalf, 1)) {
  75.                         if (randnum[q]==i){taken="yes"}
  76.                 }
  77.                 if (taken=="no"){
  78.                         j=j+1
  79.                         randnum2[j]=i
  80.                 }
  81.         }
  82.  
  83.         for (i in seq(1, nsechalf, 1)) {
  84.                 print("randnum2="); print(randnum2[i])
  85.         }
  86.  
  87.  
  88.         sum1=0
  89.         count1=0
  90.         for (z in seq(1, nrows, 1)) {
  91.                 sum1=0
  92.                 count1=0       
  93.                 for (i in seq(1, nfirsthalf, 1)) {
  94.                         y=randnum[i]
  95.                         if (t[z,y]!="n"){sum1=sum1+as.numeric(t[z,y]);count1=count1+1}
  96.                 }
  97.                 if(count1==0){print("a split where all were missing! line number=");print(z);count1=1}
  98.                 av1[z]=sum1/count1
  99.                 score1[z]=av1[z]*nfirsthalf
  100.         }
  101.  
  102.  
  103.         sum2=0
  104.         count2=0
  105.         for (z in seq(1, nrows, 1)) {
  106.                 sum2=0
  107.                 count2=0
  108.                 for (i in seq(1, nfirsthalf, 1)) {
  109.                         y=randnum2[i]
  110.                         if(t[z,y]!="n"){sum2=sum2+as.numeric(t[z,y]);count2=count2+1}
  111.                 }
  112.                 av2[z]=sum2/count2
  113.                 score2[z]=av2[z]*nsechalf
  114.         }
  115.  
  116.         for (z in seq(1, nrows, 1)) {
  117.                 scoretot[z]=score1[z]+score2[z]
  118.         }
  119.  
  120.         sumxy=0
  121.         sumx=0
  122.         sumy=0
  123.         sumx2=0
  124.         sumy2=0
  125.         sumscoretot2=0
  126.         sumscoretot=0
  127.  
  128.         for (i in seq(1, nrows, 1)) {
  129.                 sumxy=sumxy+score1[i]*score2[i]
  130.                 sumx=sumx+score1[i]
  131.                 sumy=sumy+score2[i]
  132.                 sumx2=sumx2+(score1[i])^2
  133.                 sumy2=sumy2+(score2[i])^2
  134.                 sumscoretot2=sumscoretot2+scoretot[i]^2
  135.                 sumscoretot=sumscoretot+scoretot[i]
  136.         }
  137.  
  138.         r=(sumxy-sumx*sumy/nrows)/((sumx2-sumx^2/nrows)*(sumy2-sumy^2/nrows))^0.5
  139.         sdforx=((1/nrows)*(sumx2-sumx^2/nrows))^0.5
  140.         sdfory=((1/nrows)*(sumy2-sumy^2/nrows))^0.5
  141.         varfortot=((1/nrows)*(sumscoretot2-sumscoretot^2/nrows))
  142.  
  143.         steppedrspear=(2*r)/(1+r)
  144.         steppedrrulon=4*r*sdforx*sdfory/varfortot
  145.         sumsteppedrspear=sumsteppedrspear+steppedrspear
  146.         sumsteppedrrulon=sumsteppedrrulon+steppedrrulon
  147. }
  148.  
  149. avsteppedrspear=sumsteppedrspear/numsplits
  150. print("average of stepped up r's, using Spearman method=");print(avsteppedrspear)
  151. avsteppedrrulon=sumsteppedrrulon/numsplits
  152. print("average of stepped up r's, using Rulon method="); print(avsteppedrrulon)
  153.  
  154. sumvariances=0
  155. for (i in seq(1, ncolumns, 1)) {
  156.         sumfirsts=0
  157.         sumsq=0
  158.         variance=0
  159.         for (j in seq(1, nrows, 1)) {
  160.                 sumfirsts=sumfirsts+as.numeric(t[j,i])
  161.                 sumsq=sumsq+(as.numeric(t[j,i]))^2
  162.         }
  163.         sfs=sumfirsts^2
  164.         variance=(1/nrows)*(sumsq-sfs/nrows)
  165.         sumvariances=sumvariances+variance
  166. }
  167.  
  168. for (i in seq(1, ncolumns, 1)) {
  169.         mean=0
  170.         sumdevs2=0
  171.         sumfirsts=0
  172.         for (j in seq(1, nrows, 1)) {
  173.                 sumfirsts=sumfirsts+as.numeric(t[j,i])
  174.         }
  175.         mean=sumfirsts/nrows
  176.         for (j in seq(1, nrows, 1)) {
  177.                 sumdevs2=sumdevs2+(as.numeric(t[j,i])-mean)^2
  178.         }
  179.         sumvariances=sumvariances+sumdevs2/nrows
  180. }      
  181.  
  182. alphatrad=(ncolumns/(ncolumns-1))*(1-sumvariances/varfortot)
  183.  
  184. print("alphatrad=");print(alphatrad)
clone this paste RAW Paste Data