#############3
#A=temperatura
#B=vibração
#A<-c(20,20,60,60,20,20,60,60,20,20,60,60)
#B<-c(5,15,5,15,5,15,5,15,5,15,5,15)
#y<-c(74,50,46,34,68,48,44,32,65,51,40,26)
A<-c(-1,-1,1,1,-1,-1,1,1,-1,-1,1,1)
B<-c(-1,1,-1,1,-1,1,-1,1,-1,1,-1,1)
y<-c(74,50,46,34,68,48,44,32,65,51,40,26)
group<-c("A","B","C","D","A","B","C","D","A","B","C","D")
falha<-data.frame(A,B,y,group)
aov.2=aov(y~A+B+A*B, data=falha)
summary(aov.2)
efeitos<-2*(aov.2$coef[2:4])
#Estimativa da variância do erro experimental (s^2)
grau_L1<-2
grau_L2<-2
grau_L3<-2
grau_L4<-2
ag <- data.frame(aggregate(. ~ group, falha, function(x) c(mean = mean(x), sd = sd(x))))
s2<-(sum(2*(ag$y[,2])^2))/(grau_L1+grau_L2+grau_L3+grau_L4)
#syi^2=s2/n onde n é o número de replicatas(3)
#sy1^2=sy2^2=sy3^2=sy4^2=syq
#sy=raiz(syq)
n<-3
sy=sqrt(s2/n)
#t(ef)=ef/s(ef)
tefA=efeitos[1]/sy
tefB=efeitos[2]/sy
tefAB=efeitos[3]/sy
#Gráfico de pareto
f1<-c(tefA,tefB,tefAB);f1
z=data.frame(2*abs(f1))
z=t(z)
v1=sy*(qt(0.975,8))
coef3=sort(z)
coef3
#png(file = "pareto_ceto.png",res=300, width = 4, height = 4, units = 'in')
g=barplot(sort(z),horiz=TRUE,names.arg=c( rev(names(f1))),col=ifelse(sort(abs(f1))>v1,"blue","gray"),xlim=c(0,25))##xlim, ylim
box("plot", col="blue")
abline(v=v1,col="red",lty=2)
s=format(v1,digits=2)
text(6,1,label=expression("S"[ef]*" x t"[crit]*"="),cex=1)
text(9,1,label=s,cex=1)
mtext("Gráfico de pareto dos efeitos", side=3, line=2, cex=1.0, col="blue", outer=FALSE)
p=format(f1,scientific = TRUE)
legend(x=16,y=2,cex=0.6,bg="yellow",box.col="blue", legend=c( "A=-22.3","B=-16.0","AB=3.33" ), col="blue", lwd=1:2, lty=c(NA,NA,NA))
#dev.off()