Advertisement
TroubleMaker84

Untitled

May 23rd, 2019
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
R 4.51 KB | None | 0 0
  1. z_test = function(n, sampleMean,pop_mean, stDev, alfa, type)
  2. {
  3.   if(type==0)
  4.   {critical_z= qnorm(alfa);}
  5.   else
  6.   {if(type==1)
  7.       {critical_z = qnorm(1-alfa);}
  8.   else
  9.   {critical_z= qnorm(1-alfa/2);}
  10.   }
  11.   z_score= (sampleMean - pop_mean)/ (stDev/ sqrt(n));
  12.   if(type==0 && z_score< critical_z)
  13.     {print(z_score); print(critical_z); print("The null hypothesis is rejected");return(0)}
  14.  
  15.   if(type==1 && z_score > critical_z)
  16.     {print(z_score); print(critical_z); print("The null hypothesis is rejected"); return(0);}
  17.   if(z_score != critical_z && type==2)
  18.       {print(z_score); print(critical_z); print("The null hypothesis is rejected"); return(0);}
  19.   print(z_score); print(critical_z); print("The null hypothesis holds");
  20.   return(1);
  21.  
  22. }
  23.  
  24. #z_test(200, 816, 810, 50,0.05,1);
  25.  
  26. #Ex2
  27. #z_test(49, 88,90, 12, 0.01,0)
  28. #z_test(49, 88,90, 12, 0.05,0)
  29.  
  30. #Ex4
  31. #z_test(100, 20.5, 21, 2.5, 0.01,0)
  32.  
  33. T_test_file = function(fileName, pop_mean,alfa, type)
  34. {
  35.   x= scan(fileName);
  36.   if(type==0)
  37.   {critical_t= qt(alfa,length(x)-1);}
  38.   else
  39.   {if(type==1)
  40.   {critical_t = qt(1-alfa, length(x)-1);}
  41.     else
  42.     {critical_t= qt(1-alfa/2, length(x)-1);}
  43.   }
  44.  
  45.   s= sd(x);
  46.   se = s/sqrt(length(x));
  47.   t_score = (mean(x) - pop_mean)/se;
  48.   if(type==0 && t_score< critical_t)
  49.   {print(t_score); print(critical_t); print("The null hypothesis is rejected");return(0)}
  50.  
  51.   if(type==1 && t_score > critical_t)
  52.   {print(t_score); print(critical_t); print("The null hypothesis is rejected"); return(0);}
  53.   if(t_score != critical_t && type==2)
  54.   {print(t_score); print(critical_t); print("The null hypothesis is rejected"); return(0);}
  55.   print(t_score); print(critical_t); print("The null hypothesis holds");
  56.   return(1);
  57.  
  58. }
  59.  
  60. #T_test_file("program.txt",40, 0.05, 1)
  61.  
  62. T_test = function(x,pop_mean,alfa, type )
  63. {
  64.   if(type==0)
  65.   {critical_t= qt(alfa,length(x)-1);}
  66.   else
  67.   {if(type==1)
  68.   {critical_t = qt(1-alfa, length(x)-1);}
  69.     else
  70.     {critical_t= qt(1-alfa/2, length(x)-1);}
  71.   }
  72.   s= sd(x);
  73.   se = s/sqrt(length(x));
  74.   t_score = (mean(x) - pop_mean)/se;
  75.   if(type==0 && t_score< critical_t)
  76.   {print(t_score); print(critical_t); print("The null hypothesis is rejected");return(0)}
  77.  
  78.   if(type==1 && t_score > critical_t)
  79.   {print(t_score); print(critical_t); print("The null hypothesis is rejected"); return(0);}
  80.   if(t_score != critical_t && type==2)
  81.   {print(t_score); print(critical_t); print("The null hypothesis is rejected"); return(0);}
  82.   print(t_score); print(critical_t); print("The null hypothesis holds");
  83.   return(1);
  84.  
  85. }
  86.  #Ex2
  87. #x= c(36, 32, 28, 33, 41, 28, 31, 26, 29, 34);
  88. #T_test(x,34, 0.01, 2)
  89. #Ex4
  90. #T_test_file("history.txt", 80,0.01,2)
  91.  
  92.  
  93. z_tests_mean = function (n1,n2, m0, mean1, mean2, stDev1, stDev2,alfa,type)
  94. {
  95.   if(type==0)
  96.   {critical_z= qnorm(alfa);}
  97.   else
  98.   {if(type==1)
  99.   {critical_z = qnorm(1-alfa);}
  100.     else
  101.     {critical_z= qnorm(1-alfa/2);}
  102.   }
  103.   combined_sigma = sqrt(stDev1^2/n1+ stDev2^2/n2);
  104.   z_score = (mean1-mean2 -m0)/combined_sigma;
  105.   if(type==0 && z_score< critical_z)
  106.   {print(z_score); print(critical_z); print("The null hypothesis is rejected");return(0)}
  107.  
  108.   if(type==1 && z_score > critical_z)
  109.   {print(z_score); print(critical_z); print("The null hypothesis is rejected"); return(0);}
  110.   if(z_score != critical_z && type==2)
  111.   {print(z_score); print(critical_z); print("The null hypothesis is rejected"); return(0);}
  112.   print(z_score); print(critical_z); print("The null hypothesis holds");
  113.   return(1);
  114.  
  115. }
  116.  
  117. #z_tests_mean(100,100, 0, 48,47,4,3,0.05,2)
  118.  
  119.  
  120. #Ex2
  121. #z_tests_mean(80,70,0,160,155,3.24,2.55,0.01,2)
  122.  
  123. #Ex3
  124. #z_tests_mean(100,100,0, 22.8,23.3,1.3,1.9,0.01,2)
  125.  
  126. F_test_file = function(fileName, alfa, type)
  127. {
  128.   x1 = read.table("programm.txt", header = TRUE)[['A']];
  129.   x2 = read.table("programm.txt", header = TRUE)[['B']];
  130.   mean1= mean(x1);
  131.   mean2= mean(x2);
  132.   n1= length(x1);
  133.   n2= length(x2);
  134.   s1= sd(x1);
  135.   s2= sd(x2);
  136.   if(type==0)
  137.     critical_f= qf(1-alfa,n1-1,n2-1);
  138.   if(type==1)
  139.     {critical_f_left = qf(alfa/2,n1-1,n2-1); critical_f_right = qf(1-alfa/2,n1-1,n2-1);}
  140.   f_score= s1^2 / s2^2;
  141.   if(type==0 && f_score>critical_f)
  142.   {print(f_score); print(critical_f); print("Null hyp rejected"); return(0);}
  143.   if(type==1 &&( f_score <critical_f_left || f_score > critical_f_right))
  144.   {print(f_score); print(critical_f_left); print(critical_f_right); print("Null hyp rejected"); return(0);}
  145.    print("Null hyp holds");
  146.   return(1);
  147. }  
  148.  
  149. #ex2
  150. #F_test_file("programm.txt",0.05,1)
  151. #F_test_file("mice.txt",0.01,1)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement