Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Apr 13th, 2012  |  syntax: R  |  size: 1.81 KB  |  hits: 16  |  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. #Evgueni Neiman
  2. #Math 452
  3. #Homework No. 3
  4.  
  5. ### Question 1 ###
  6.  
  7. x=rpois(20,4); k1=rpois(20,5); k2=rpois(10,5);
  8. names(x)=letters[k1]; out=letters[k2];
  9.  
  10. i = 1;
  11. new_x = NULL;
  12. while(i <= length(x))
  13. {
  14.         j = 1;
  15.         found = FALSE
  16.         while(j <= length(out))
  17.         {
  18.                 if(names(x[i]) == out[j])
  19.                 {
  20.                         found = TRUE;
  21.                         j = 100000;
  22.                 }
  23.                 j = j + 1;             
  24.         }                      
  25.                                
  26.         if(found == FALSE)
  27.         {
  28.                 new_x = c(new_x,x[i]);
  29.         }
  30.  
  31.         i = i + 1;
  32. }
  33.  
  34. print(x); print(out); print(new_x);
  35.  
  36. #I am sure there is a simpler function to find out but this
  37. #should make sense...
  38.  
  39.  
  40. ### Question 2 ###
  41.  
  42. ##function matrix_repeat takes in a matrix as its first argument
  43. ###and a vector as its second argument
  44.  
  45.  
  46. rep_mat = function(x,w)
  47. {
  48.  
  49.         row = dim(x)[1];                #get dimensions
  50.         col = dim(x)[2];
  51.                
  52.         len = length(w);
  53.        
  54.         i = 1;
  55.  
  56.         while(i <= len)
  57.         {
  58.                 temp = matrix(rep(x[i,],each=w[i]),nrow=w[i]); 
  59.                 new_x = rbind(new_x,temp);
  60.                 i = i + 1;
  61.                
  62.         }
  63.         return(new_x)
  64.        
  65.         rm(new_x)
  66. }
  67.  
  68. ## to test this out:
  69.  
  70. X = matrix(1:9,3,3)
  71. z = c(1,2,3)
  72.  
  73. rep_mat(X,z);
  74.  
  75. ### Question 3 ###
  76.  
  77. box = function(x)
  78. {
  79.         q1 = quantile(x,.25);
  80.       q2 = median(x);
  81.       q3 = quantile(x,.75);
  82.  
  83.       xmax = max(x);
  84.       xmin = min(x);
  85.  
  86.       IQR = q3 - q1;
  87.  
  88.       lh = q1 - 3*IQR;
  89.       uh = q3 + 3*IQR;
  90.  
  91.       xmin_h=min(x[x>lh]);
  92.       xmax_h=max(x[x<uh]);
  93.  
  94.       xlim = c(0,4);
  95.       ylim = c(xmin,xmax);
  96.  
  97.       plot(1,1,xlim,ylim,type="n");
  98.  
  99.       #print(q1);
  100.  
  101.       rect(1,q1,3,q3);
  102.  
  103.       lines(c(2,2),c(q1,xmin_h),lty=2)
  104.       lines(c(1.5,2.5),c(xmin_h,xmin_h))
  105.       lines(c(1,3),c(q2,q2))
  106.       lines(c(2,2),c(q3,xmax_h), lty=2)
  107.       lines(c(1.5,2.5),c(xmax_h,xmax_h))
  108.  
  109.       if(any(x>xmax_h)) points(2,x[x>xmax_h]);
  110.       if(any(x<xmin_h)) points(2,x[x<xmin_h]);
  111.  
  112.  
  113.  
  114. #now to test this out
  115.  
  116. x=rpois(100,5)
  117.  
  118. box(x);
  119.  
  120.  
  121. }