Guest User

Untitled

a guest
Jun 18th, 2018
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.98 KB | None | 0 0
  1. testes<-function(amostra1,amostra2=NULL,var1=NULL,var2=NULL,hipotese,erro=0.05,independent,iguais){
  2.  
  3.  
  4. #TH para uma amostra#
  5. if(is.null(amostra2)){
  6.  
  7. #Proporcao
  8. if( sum(ifelse(amostra1<=1,1,0))==length(amostra1)){
  9. z=(mean(amostra1) - hipotese)/(sqrt(hipotese*(1-hipotese)/length(amostra1)))
  10. k=2
  11. }else{#nao proporcoa
  12. if(!is.null(var1)){
  13.  
  14. ## formula 1
  15. z=(mean(amostra1)-hipotese)/(sqrt(var1)/sqrt(length(amostra1)))
  16. k=2
  17. } else{
  18.  
  19. ## formula 2
  20. t=(mean(amostra1)- hipotese)/(sd(amostra1)/(sqrt(length(amostra1))))
  21. k=3
  22. } #close TH mu var desconhecida
  23. }# close TH mu
  24. } else{ #Duas amostras
  25.  
  26. #segunda parte da arvore
  27. check=try(length(independent),silent=T)
  28. if(is(check,"try-error")) stop(paste("You must inform if the samples are independent=1 or not"))
  29. if(independent==0){
  30.  
  31. #remove?
  32. # if( sum(ifelse(amostra1<=1,1,0))==length(amostra1) &
  33. # sum(ifelse(amostra2<=1,1,0))==length(amostra2)){
  34. # ## formula 8
  35. # z=(mean(amostra1)-hipotese)/(var1^2/sqrt(length(amostra1)))
  36. # k=6
  37. # }
  38. # else{
  39. ## formula 7
  40. d=amostra1-amostra2
  41. t=(mean(d)-hipotese)/(sd(d)/sqrt(length(d)))
  42. k=7
  43. } else{
  44. if( sum(ifelse(amostra1<=1,1,0))==length(amostra1) &
  45. sum(ifelse(amostra1<=1,1,0))==length(amostra1)){
  46. ## formula 9
  47. p1=mean(amostra1); p2=mean(amostra2)
  48. z=(p1-p2-hipotese)/
  49. (sqrt((p1*(1-p1))/length(amostra1)+p2*(1-p2)/length(amostra2)))
  50. k=6
  51. } else{
  52. if(!is.null(var1) & !is.null(var2)){
  53.  
  54. ## formula 4
  55. z=(mean(amostra1)-mean(amostra2)-(hipotese))/
  56. sqrt(var1/length(amostra1)+var2/length(amostra2))
  57. k=6
  58. } else{
  59.  
  60. check=try(length(iguais),silent=T)
  61. if(is(check,"try-error")) stop(paste("You must inform if the variances are equal=1 or not"))
  62.  
  63. if(iguais==1){
  64. ## formula 5
  65.  
  66. sp=((length(amostra1)-1)*var(amostra1)+(length(amostra2)-1)*var(amostra2))/
  67. (length(amostra1)+length(amostra2)-2)
  68. t=(mean(amostra1)-mean(amostra2)-hipotese)/
  69. (sqrt((sp)*(1/length(amostra1) + 1/length(amostra2))))
  70. k=4
  71. } else{
  72. ## formula 6
  73. t=(mean(amostra1)-mean(amostra2)-(hipotese)/
  74. (sqrt(var(amostra1)/length(amostra1)+var(amostra2)/length(amostra2))))
  75. k=5
  76. }}}}
  77. } # fim duas amostras
  78.  
  79. if(k==2){
  80. limites=ifelse(z>-4 & z<4,4,z+0.5)
  81. r1=curve(dnorm(x),-limites,limites,ylab="Normal distribution",xlab="Z")
  82. # na qual o erro sera de 5%, 2.5% para cada lado
  83. r2=lines(c(qnorm(erro/2),qnorm(erro/2)),c(-1,0.05),col='tomato1',lwd=3)
  84. r3=lines(c(qnorm(1-erro/2),qnorm(1-erro/2)),c(-1,0.05),col='tomato1',lwd=3)
  85. r4=abline(v=z,col='orchid1',lwd=4,lty=2)
  86.  
  87. if(z< qnorm(erro/2)){
  88. print(
  89. paste(
  90. 'Reject the null hypothesis, that is, the true average is different from ',hipotese
  91. ))} else if(z> qnorm(1-erro/2)){
  92. print(
  93. paste("Reject the null hypothesis, that is, the true average is different from ",hipotese
  94. ))} else
  95. print(
  96. paste("We do not have enough information to reject the null hypothesis that the mean is equal to ",hipotese)
  97. )
  98.  
  99. }
  100.  
  101. if(k==3){limites=ifelse(t>-4 & t<4,4,t+0.5)
  102. r1=curve(dt(x,length(amostra1)-1),-limites,limites,ylab="T3 distribution",xlab="T")
  103. # na qual o erro sera de 5%, 2.5% para cada lado
  104. r2=lines(c(qt(erro/2,length(amostra1)-1),qt(erro/2,length(amostra1)-1)),c(-1,0.05),col='tomato1',lwd=3)
  105. r3=lines(c(qt(1-erro/2,length(amostra1)-1),qt(1-erro/2,length(amostra1)-1)),c(-1,0.05),col='tomato1',lwd=3)
  106. r4=abline(v=t,col='orchid1',lwd=4,lty=2)
  107.  
  108. if(t< qt(erro/2,length(amostra1)-1)){
  109. print(
  110. paste(
  111. 'Reject the null hypothesis, that is, the true average is different from ',hipotese
  112. ))}
  113. else if(t> qt(1-erro/2,length(amostra1)-1)){
  114. print(
  115. paste("Reject the null hypothesis, that is, the true average is different from ",hipotese
  116. ))} else
  117. print(
  118. paste("We do not have enough information to reject the null hypothesis that the mean is equal to ",hipotese)
  119. )
  120. }
  121.  
  122. if(k==4){
  123. limites=ifelse(t>-4 & t<4,4,t+0.5)
  124. gl=length(amostra1)+length(amostra2)-2
  125. r1=curve(dt(x,gl),-limites,limites,ylab="T4 distribution",xlab="T")
  126. # na qual o erro sera de 5%, 2.5% para cada lado
  127. r2=lines(c(qt(erro/2,gl),qt(erro/2,gl)),c(-1,0.05),col='tomato1',lwd=3)
  128. r3=lines(c(qt(1-erro/2,gl),qt(1-erro/2,gl)),c(-1,0.05),col='tomato1',lwd=3)
  129. r4=abline(v=t,col='orchid1',lwd=4,lty=2)
  130.  
  131. if(t< qt(erro/2,gl)){print(paste('The average of X and Y are different'))}
  132. if(t> qt(1-erro/2,gl)){print(
  133. paste("The average of X and Y are different"))} else
  134. print(
  135. paste("We do not have enough information to know the average of X and Y are different")
  136. )
  137.  
  138. }
  139.  
  140. if(k==5){
  141. limites=ifelse(t>-4 & t<4,4,t+0.5)
  142. gl=(var(amostra1)/length(amostra1)+var(amostra2)/length(amostra2))^2/
  143. (((var(amostra1)/length(amostra1))^2/(length(amostra1)-1))+
  144. (var(amostra2)/length(amostra2))^2/(length(amostra2)-1))
  145. r1=curve(dt(x,gl),-limites,limites,ylab="T5 distribution",xlab="T")
  146. # na qual o erro sera de 5%, 2.5% para cada lado
  147. r2=lines(c(qt(erro/2,gl),qt(erro/2,gl)),c(-1,0.05),col='tomato1',lwd=3)
  148. r3=lines(c(qt(1-erro/2,gl),qt(1-erro/2,gl)),c(-1,0.05),col='tomato1',lwd=3)
  149. r4=abline(v=t,col='orchid1',lwd=4,lty=2)
  150.  
  151. if(t< qt(erro/2,gl)){
  152. print(
  153. paste(
  154. 'The average of X and Y are different'
  155. ))}
  156. else if(t> qt(1-erro/2,gl)){
  157. print(
  158. paste("The average of X and Y are different"))} else
  159. print(
  160. paste("We do not have enough information to know the average of X and Y are different")
  161. )
  162.  
  163. }
  164.  
  165. if(k==6){
  166. limites=ifelse(z>-4 & z<4,4,z+0.5)
  167. r1=curve(dnorm(x),-limites,limites,ylab="Normal distribution",xlab="Z")
  168. # na qual o erro sera de 5%, 2.5% para cada lado
  169. r2=lines(c(qnorm(erro/2),qnorm(erro/2)),c(-1,0.05),col='tomato1',lwd=3)
  170. r3=lines(c(qnorm(1-erro/2),qnorm(1-erro/2)),c(-1,0.05),col='tomato1',lwd=3)
  171. r4=abline(v=z,col='orchid1',lwd=4,lty=2)
  172.  
  173. if(z< qnorm(erro/2)){
  174. print(
  175. paste(
  176. 'The average of X and Y are different'
  177. ))} else if(z> qnorm(1-erro/2)){
  178. print(
  179. paste("The average of X and Y are different"
  180. ))} else
  181. print(
  182. paste("We do not have enough information to know the average of X and Y are different")
  183. )
  184.  
  185. }
  186.  
  187. if(k==7){limites=ifelse(t>-4 & t<4,4,t+0.5)
  188. r1=curve(dt(x,length(amostra1)-1),-limites,limites,ylab="T7 distribution",xlab="T")
  189. # na qual o erro sera de 5%, 2.5% para cada lado
  190. r2=lines(c(qt(erro/2,length(amostra1)-1),qt(erro/2,length(amostra1)-1)),c(-1,0.05),col='tomato1',lwd=3)
  191. r3=lines(c(qt(1-erro/2,length(amostra1)-1),qt(1-erro/2,length(amostra1)-1)),c(-1,0.05),col='tomato1',lwd=3)
  192. r4=abline(v=t,col='orchid1',lwd=4,lty=2)
  193.  
  194. if(t< qt(erro/2,length(amostra1)-1)){
  195. print(
  196. paste(
  197. 'The average of X and Y are different'
  198. ))}
  199. else if(t> qt(1-erro/2,length(amostra1)-1)){
  200. print(
  201. paste("The average of X and Y are different"
  202. ))} else
  203. print(
  204. paste("We do not have enough information to know the average of X and Y are different")
  205. )
  206. }
  207.  
  208.  
  209.  
  210. }
  211.  
  212.  
  213.  
  214. print('Esse pacote foi desenvolvido para fazer testes de hipotese, por Maria Gabriela, Bruno Cintra e Giovana Aleixo. Enjoy')
Add Comment
Please, Sign In to add comment