Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- testes<-function(amostra1,amostra2=NULL,var1=NULL,var2=NULL,hipotese,erro=0.05,independent,iguais){
- #TH para uma amostra#
- if(is.null(amostra2)){
- #Proporcao
- if( sum(ifelse(amostra1<=1,1,0))==length(amostra1)){
- z=(mean(amostra1) - hipotese)/(sqrt(hipotese*(1-hipotese)/length(amostra1)))
- k=2
- }else{#nao proporcoa
- if(!is.null(var1)){
- ## formula 1
- z=(mean(amostra1)-hipotese)/(sqrt(var1)/sqrt(length(amostra1)))
- k=2
- } else{
- ## formula 2
- t=(mean(amostra1)- hipotese)/(sd(amostra1)/(sqrt(length(amostra1))))
- k=3
- } #close TH mu var desconhecida
- }# close TH mu
- } else{ #Duas amostras
- #segunda parte da arvore
- check=try(length(independent),silent=T)
- if(is(check,"try-error")) stop(paste("You must inform if the samples are independent=1 or not"))
- if(independent==0){
- #remove?
- # if( sum(ifelse(amostra1<=1,1,0))==length(amostra1) &
- # sum(ifelse(amostra2<=1,1,0))==length(amostra2)){
- # ## formula 8
- # z=(mean(amostra1)-hipotese)/(var1^2/sqrt(length(amostra1)))
- # k=6
- # }
- # else{
- ## formula 7
- d=amostra1-amostra2
- t=(mean(d)-hipotese)/(sd(d)/sqrt(length(d)))
- k=7
- } else{
- if( sum(ifelse(amostra1<=1,1,0))==length(amostra1) &
- sum(ifelse(amostra1<=1,1,0))==length(amostra1)){
- ## formula 9
- p1=mean(amostra1); p2=mean(amostra2)
- z=(p1-p2-hipotese)/
- (sqrt((p1*(1-p1))/length(amostra1)+p2*(1-p2)/length(amostra2)))
- k=6
- } else{
- if(!is.null(var1) & !is.null(var2)){
- ## formula 4
- z=(mean(amostra1)-mean(amostra2)-(hipotese))/
- sqrt(var1/length(amostra1)+var2/length(amostra2))
- k=6
- } else{
- check=try(length(iguais),silent=T)
- if(is(check,"try-error")) stop(paste("You must inform if the variances are equal=1 or not"))
- if(iguais==1){
- ## formula 5
- sp=((length(amostra1)-1)*var(amostra1)+(length(amostra2)-1)*var(amostra2))/
- (length(amostra1)+length(amostra2)-2)
- t=(mean(amostra1)-mean(amostra2)-hipotese)/
- (sqrt((sp)*(1/length(amostra1) + 1/length(amostra2))))
- k=4
- } else{
- ## formula 6
- t=(mean(amostra1)-mean(amostra2)-(hipotese)/
- (sqrt(var(amostra1)/length(amostra1)+var(amostra2)/length(amostra2))))
- k=5
- }}}}
- } # fim duas amostras
- if(k==2){
- limites=ifelse(z>-4 & z<4,4,z+0.5)
- r1=curve(dnorm(x),-limites,limites,ylab="Normal distribution",xlab="Z")
- # na qual o erro sera de 5%, 2.5% para cada lado
- r2=lines(c(qnorm(erro/2),qnorm(erro/2)),c(-1,0.05),col='tomato1',lwd=3)
- r3=lines(c(qnorm(1-erro/2),qnorm(1-erro/2)),c(-1,0.05),col='tomato1',lwd=3)
- r4=abline(v=z,col='orchid1',lwd=4,lty=2)
- if(z< qnorm(erro/2)){
- print(
- paste(
- 'Reject the null hypothesis, that is, the true average is different from ',hipotese
- ))} else if(z> qnorm(1-erro/2)){
- print(
- paste("Reject the null hypothesis, that is, the true average is different from ",hipotese
- ))} else
- print(
- paste("We do not have enough information to reject the null hypothesis that the mean is equal to ",hipotese)
- )
- }
- if(k==3){limites=ifelse(t>-4 & t<4,4,t+0.5)
- r1=curve(dt(x,length(amostra1)-1),-limites,limites,ylab="T3 distribution",xlab="T")
- # na qual o erro sera de 5%, 2.5% para cada lado
- r2=lines(c(qt(erro/2,length(amostra1)-1),qt(erro/2,length(amostra1)-1)),c(-1,0.05),col='tomato1',lwd=3)
- 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)
- r4=abline(v=t,col='orchid1',lwd=4,lty=2)
- if(t< qt(erro/2,length(amostra1)-1)){
- print(
- paste(
- 'Reject the null hypothesis, that is, the true average is different from ',hipotese
- ))}
- else if(t> qt(1-erro/2,length(amostra1)-1)){
- print(
- paste("Reject the null hypothesis, that is, the true average is different from ",hipotese
- ))} else
- print(
- paste("We do not have enough information to reject the null hypothesis that the mean is equal to ",hipotese)
- )
- }
- if(k==4){
- limites=ifelse(t>-4 & t<4,4,t+0.5)
- gl=length(amostra1)+length(amostra2)-2
- r1=curve(dt(x,gl),-limites,limites,ylab="T4 distribution",xlab="T")
- # na qual o erro sera de 5%, 2.5% para cada lado
- r2=lines(c(qt(erro/2,gl),qt(erro/2,gl)),c(-1,0.05),col='tomato1',lwd=3)
- r3=lines(c(qt(1-erro/2,gl),qt(1-erro/2,gl)),c(-1,0.05),col='tomato1',lwd=3)
- r4=abline(v=t,col='orchid1',lwd=4,lty=2)
- if(t< qt(erro/2,gl)){print(paste('The average of X and Y are different'))}
- if(t> qt(1-erro/2,gl)){print(
- paste("The average of X and Y are different"))} else
- print(
- paste("We do not have enough information to know the average of X and Y are different")
- )
- }
- if(k==5){
- limites=ifelse(t>-4 & t<4,4,t+0.5)
- gl=(var(amostra1)/length(amostra1)+var(amostra2)/length(amostra2))^2/
- (((var(amostra1)/length(amostra1))^2/(length(amostra1)-1))+
- (var(amostra2)/length(amostra2))^2/(length(amostra2)-1))
- r1=curve(dt(x,gl),-limites,limites,ylab="T5 distribution",xlab="T")
- # na qual o erro sera de 5%, 2.5% para cada lado
- r2=lines(c(qt(erro/2,gl),qt(erro/2,gl)),c(-1,0.05),col='tomato1',lwd=3)
- r3=lines(c(qt(1-erro/2,gl),qt(1-erro/2,gl)),c(-1,0.05),col='tomato1',lwd=3)
- r4=abline(v=t,col='orchid1',lwd=4,lty=2)
- if(t< qt(erro/2,gl)){
- print(
- paste(
- 'The average of X and Y are different'
- ))}
- else if(t> qt(1-erro/2,gl)){
- print(
- paste("The average of X and Y are different"))} else
- print(
- paste("We do not have enough information to know the average of X and Y are different")
- )
- }
- if(k==6){
- limites=ifelse(z>-4 & z<4,4,z+0.5)
- r1=curve(dnorm(x),-limites,limites,ylab="Normal distribution",xlab="Z")
- # na qual o erro sera de 5%, 2.5% para cada lado
- r2=lines(c(qnorm(erro/2),qnorm(erro/2)),c(-1,0.05),col='tomato1',lwd=3)
- r3=lines(c(qnorm(1-erro/2),qnorm(1-erro/2)),c(-1,0.05),col='tomato1',lwd=3)
- r4=abline(v=z,col='orchid1',lwd=4,lty=2)
- if(z< qnorm(erro/2)){
- print(
- paste(
- 'The average of X and Y are different'
- ))} else if(z> qnorm(1-erro/2)){
- print(
- paste("The average of X and Y are different"
- ))} else
- print(
- paste("We do not have enough information to know the average of X and Y are different")
- )
- }
- if(k==7){limites=ifelse(t>-4 & t<4,4,t+0.5)
- r1=curve(dt(x,length(amostra1)-1),-limites,limites,ylab="T7 distribution",xlab="T")
- # na qual o erro sera de 5%, 2.5% para cada lado
- r2=lines(c(qt(erro/2,length(amostra1)-1),qt(erro/2,length(amostra1)-1)),c(-1,0.05),col='tomato1',lwd=3)
- 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)
- r4=abline(v=t,col='orchid1',lwd=4,lty=2)
- if(t< qt(erro/2,length(amostra1)-1)){
- print(
- paste(
- 'The average of X and Y are different'
- ))}
- else if(t> qt(1-erro/2,length(amostra1)-1)){
- print(
- paste("The average of X and Y are different"
- ))} else
- print(
- paste("We do not have enough information to know the average of X and Y are different")
- )
- }
- }
- 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