Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- z_test = function(n, sampleMean,pop_mean, stDev, alfa, type)
- {
- if(type==0)
- {critical_z= qnorm(alfa);}
- else
- {if(type==1)
- {critical_z = qnorm(1-alfa);}
- else
- {critical_z= qnorm(1-alfa/2);}
- }
- z_score= (sampleMean - pop_mean)/ (stDev/ sqrt(n));
- if(type==0 && z_score< critical_z)
- {print(z_score); print(critical_z); print("The null hypothesis is rejected");return(0)}
- if(type==1 && z_score > critical_z)
- {print(z_score); print(critical_z); print("The null hypothesis is rejected"); return(0);}
- if(z_score != critical_z && type==2)
- {print(z_score); print(critical_z); print("The null hypothesis is rejected"); return(0);}
- print(z_score); print(critical_z); print("The null hypothesis holds");
- return(1);
- }
- #z_test(200, 816, 810, 50,0.05,1);
- #Ex2
- #z_test(49, 88,90, 12, 0.01,0)
- #z_test(49, 88,90, 12, 0.05,0)
- #Ex4
- #z_test(100, 20.5, 21, 2.5, 0.01,0)
- T_test_file = function(fileName, pop_mean,alfa, type)
- {
- x= scan(fileName);
- if(type==0)
- {critical_t= qt(alfa,length(x)-1);}
- else
- {if(type==1)
- {critical_t = qt(1-alfa, length(x)-1);}
- else
- {critical_t= qt(1-alfa/2, length(x)-1);}
- }
- s= sd(x);
- se = s/sqrt(length(x));
- t_score = (mean(x) - pop_mean)/se;
- if(type==0 && t_score< critical_t)
- {print(t_score); print(critical_t); print("The null hypothesis is rejected");return(0)}
- if(type==1 && t_score > critical_t)
- {print(t_score); print(critical_t); print("The null hypothesis is rejected"); return(0);}
- if(t_score != critical_t && type==2)
- {print(t_score); print(critical_t); print("The null hypothesis is rejected"); return(0);}
- print(t_score); print(critical_t); print("The null hypothesis holds");
- return(1);
- }
- #T_test_file("program.txt",40, 0.05, 1)
- T_test = function(x,pop_mean,alfa, type )
- {
- if(type==0)
- {critical_t= qt(alfa,length(x)-1);}
- else
- {if(type==1)
- {critical_t = qt(1-alfa, length(x)-1);}
- else
- {critical_t= qt(1-alfa/2, length(x)-1);}
- }
- s= sd(x);
- se = s/sqrt(length(x));
- t_score = (mean(x) - pop_mean)/se;
- if(type==0 && t_score< critical_t)
- {print(t_score); print(critical_t); print("The null hypothesis is rejected");return(0)}
- if(type==1 && t_score > critical_t)
- {print(t_score); print(critical_t); print("The null hypothesis is rejected"); return(0);}
- if(t_score != critical_t && type==2)
- {print(t_score); print(critical_t); print("The null hypothesis is rejected"); return(0);}
- print(t_score); print(critical_t); print("The null hypothesis holds");
- return(1);
- }
- #Ex2
- #x= c(36, 32, 28, 33, 41, 28, 31, 26, 29, 34);
- #T_test(x,34, 0.01, 2)
- #Ex4
- #T_test_file("history.txt", 80,0.01,2)
- z_tests_mean = function (n1,n2, m0, mean1, mean2, stDev1, stDev2,alfa,type)
- {
- if(type==0)
- {critical_z= qnorm(alfa);}
- else
- {if(type==1)
- {critical_z = qnorm(1-alfa);}
- else
- {critical_z= qnorm(1-alfa/2);}
- }
- combined_sigma = sqrt(stDev1^2/n1+ stDev2^2/n2);
- z_score = (mean1-mean2 -m0)/combined_sigma;
- if(type==0 && z_score< critical_z)
- {print(z_score); print(critical_z); print("The null hypothesis is rejected");return(0)}
- if(type==1 && z_score > critical_z)
- {print(z_score); print(critical_z); print("The null hypothesis is rejected"); return(0);}
- if(z_score != critical_z && type==2)
- {print(z_score); print(critical_z); print("The null hypothesis is rejected"); return(0);}
- print(z_score); print(critical_z); print("The null hypothesis holds");
- return(1);
- }
- #z_tests_mean(100,100, 0, 48,47,4,3,0.05,2)
- #Ex2
- #z_tests_mean(80,70,0,160,155,3.24,2.55,0.01,2)
- #Ex3
- #z_tests_mean(100,100,0, 22.8,23.3,1.3,1.9,0.01,2)
- F_test_file = function(fileName, alfa, type)
- {
- x1 = read.table("programm.txt", header = TRUE)[['A']];
- x2 = read.table("programm.txt", header = TRUE)[['B']];
- mean1= mean(x1);
- mean2= mean(x2);
- n1= length(x1);
- n2= length(x2);
- s1= sd(x1);
- s2= sd(x2);
- if(type==0)
- critical_f= qf(1-alfa,n1-1,n2-1);
- if(type==1)
- {critical_f_left = qf(alfa/2,n1-1,n2-1); critical_f_right = qf(1-alfa/2,n1-1,n2-1);}
- f_score= s1^2 / s2^2;
- if(type==0 && f_score>critical_f)
- {print(f_score); print(critical_f); print("Null hyp rejected"); return(0);}
- if(type==1 &&( f_score <critical_f_left || f_score > critical_f_right))
- {print(f_score); print(critical_f_left); print(critical_f_right); print("Null hyp rejected"); return(0);}
- print("Null hyp holds");
- return(1);
- }
- #ex2
- #F_test_file("programm.txt",0.05,1)
- #F_test_file("mice.txt",0.01,1)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement