<div dir="ltr"><br clear="all"><div><p class="MsoNormal" style="line-height:normal"><span style="font-size:12pt;font-family:"Times New Roman",serif">Boa tarde pessoal,<span></span></span></p>
<p class="MsoNormal" style="line-height:normal"><span style="font-size:12pt;font-family:"Times New Roman",serif"><span> </span></span></p>
<p class="MsoNormal" style="line-height:normal"><span style="font-size:12pt;font-family:"Times New Roman",serif">estou trabalhando com um banco de dados que possuem
várias coletas de pesos ao longo do tempo de vários bois.<span></span></span></p>
<p class="MsoNormal" style="line-height:normal"><span style="font-size:12pt;font-family:"Times New Roman",serif">O meu objetivo é deixar um ajuste
simples (pode até ser um modelo não- linear ao invés de uma regressão simples),
porém, preciso remover os possíveis pontos discrepantes que atrapalham o ajuste,
de forma recursiva (automática). O algoritmo será executado em vários momentos,
enquanto acompanha o crescimento do animal. O mais importante seria a remoção dos <i>outiliers</i> do banco de
dados. Eu criei um algoritmo para fazer
isso. <span></span></span></p>
<p class="MsoNormal" style="line-height:normal"><span style="font-size:12pt;font-family:"Times New Roman",serif">Gostaria de saber se alguém possui uma
ideia melhor ou sugestão. Segue a função criada.<span></span></span></p><p class="MsoNormal" style="line-height:normal"><span style="font-size:12pt;font-family:"Times New Roman",serif"><br></span></p><p class="MsoNormal" style="line-height:normal"><font face="Times New Roman, serif"><span style="font-size:12pt"><b>OBS:</b> Muitos pontos discrepantes ainda ficam no banco, isso devido a um erro operacional da balança que pesa os animais. Precisaria de algo mais robusto para remover os pontos </span><span style="font-size:16px">destoantes de forma recursiva para todos os animais</span><span style="font-size:12pt">.</span></font></p><p class="MsoNormal" style="line-height:normal"><br></p>
<p class="MsoNormal" style="line-height:normal"><span style="font-size:12pt;font-family:"Times New Roman",serif"><span> </span></span></p>
<p class="MsoNormal" style="line-height:normal"><span style="font-size:12pt;font-family:"Times New Roman",serif">DADOS é um data.frame com as seguintes
colunas: BRINCO (identificação ou numero do animal), DATA (dia da pesagem do
peso do animal) e PESO (peso do animal correspondente ao dia)<span></span></span></p>
<p class="MsoNormal" style="line-height:normal"><span style="font-size:12pt;font-family:"Times New Roman",serif"><span> </span></span></p>
<p class="MsoNormal" style="line-height:normal"><span lang="EN-US" style="font-size:12pt;font-family:"Times New Roman",serif">fit_linear <- function(DADOS){<span></span></span></p>
<p class="MsoNormal" style="line-height:normal"><span lang="EN-US" style="font-size:12pt;font-family:"Times New Roman",serif">fit_aux = c()<span></span></span></p>
<p class="MsoNormal" style="line-height:normal"><span style="font-size:12pt;font-family:"Times New Roman",serif">BRINCO = as.numeric(as.vector(levels(
as.factor(DADOS[,"BRINCO"])))) <span></span></span></p>
<p class="MsoNormal" style="line-height:normal"><span style="font-size:12pt;font-family:"Times New Roman",serif">pdf(file =
"ajuste%03d.pdf") # salvar os
gráficos em pdf<span></span></span></p>
<p class="MsoNormal" style="line-height:normal"><span style="font-size:12pt;font-family:"Times New Roman",serif">for (i in BRINCO){<span></span></span></p>
<p class="MsoNormal" style="line-height:normal"><span style="font-size:12pt;font-family:"Times New Roman",serif">dados = subset(DADOS, BRINCO == i, selec
= c( BRINCO, DATA, PESO))<span></span></span></p>
<p class="MsoNormal" style="line-height:normal"><span style="font-size:12pt;font-family:"Times New Roman",serif">j=1<span></span></span></p>
<p class="MsoNormal" style="line-height:normal"><span style="font-size:12pt;font-family:"Times New Roman",serif">while (j<=30){<span></span></span></p>
<p class="MsoNormal" style="line-height:normal"><span style="font-size:12pt;font-family:"Times New Roman",serif">dias =
as.vector(as.numeric(as.factor(dados[,"DATA"]))) <span></span></span></p>
<p class="MsoNormal" style="line-height:normal"><span style="font-size:12pt;font-family:"Times New Roman",serif">pesos =
as.numeric(as.vector(dados$PESO))<span></span></span></p>
<p class="MsoNormal" style="line-height:normal"><span style="font-size:12pt;font-family:"Times New Roman",serif">dad = cbind(dias, pesos)<span></span></span></p>
<p class="MsoNormal" style="line-height:normal"><span style="font-size:12pt;font-family:"Times New Roman",serif">mod <- lm(pesos~dias)<span></span></span></p>
<p class="MsoNormal" style="line-height:normal"><span lang="EN-US" style="font-size:12pt;font-family:"Times New Roman",serif">INFLUENTES = influence.measures(mod)<span></span></span></p>
<p class="MsoNormal" style="line-height:normal"><span lang="EN-US" style="font-size:12pt;font-family:"Times New Roman",serif">dados_analise = cbind(INFLUENTES$is.inf, dad)<span></span></span></p>
<p class="MsoNormal" style="line-height:normal"><span style="font-size:12pt;font-family:"Times New Roman",serif">dados_influentes =
as.data.frame(dados_analise)<span></span></span></p>
<p class="MsoNormal" style="line-height:normal"><span style="font-size:12pt;font-family:"Times New Roman",serif">colnames(dados_influentes) =
c("x1", "x2", "x3", "x4",
"x5", "x6", "DATA", "PESO")<span></span></span></p>
<p class="MsoNormal" style="line-height:normal"><span style="font-size:12pt;font-family:"Times New Roman",serif">dados = subset(dados_influentes,
(x1==FALSE & x2==FALSE & x3==FALSE & x4==FALSE & x5==FALSE
& x6==FALSE), <span></span></span></p>
<p class="MsoNormal" style="line-height:normal"><span style="font-size:12pt;font-family:"Times New Roman",serif"> selec = c(x1, x2, x3, x4, x5, x6, DATA, PESO))<span></span></span></p>
<p class="MsoNormal" style="line-height:normal"><span lang="EN-US" style="font-size:12pt;font-family:"Times New Roman",serif">j = j+1<span></span></span></p>
<p class="MsoNormal" style="line-height:normal"><span lang="EN-US" style="font-size:12pt;font-family:"Times New Roman",serif">}<span></span></span></p>
<p class="MsoNormal" style="line-height:normal"><span lang="EN-US" style="font-size:12pt;font-family:"Times New Roman",serif">res <- residuals(mod)<span></span></span></p>
<p class="MsoNormal" style="line-height:normal"><span lang="EN-US" style="font-size:12pt;font-family:"Times New Roman",serif">h <- hatvalues(mod)<span></span></span></p>
<p class="MsoNormal" style="line-height:normal"><span lang="EN-US" style="font-size:12pt;font-family:"Times New Roman",serif">P = length(mod$coefficient); N=length(peso); P; N; hc<-3*P/N;hc<span></span></span></p>
<p class="MsoNormal" style="line-height:normal"><span lang="EN-US" style="font-size:12pt;font-family:"Times New Roman",serif">limt <- list(c(DFB=2/sqrt(N),FDFits=2*sqrt(P/N),cov.r=3*P/N,<span></span></span></p>
<p class="MsoNormal" style="line-height:normal"><span lang="EN-US" style="font-size:12pt;font-family:"Times New Roman",serif"> Cook=qf(0.5,2,8, lower.tail =
TRUE, log.p = FALSE),hat=3*P/N))<span></span></span></p>
<p class="MsoNormal" style="line-height:normal"><span lang="EN-US" style="font-size:12pt;font-family:"Times New Roman",serif">N=length(peso); P=length(mod$coefficient)<span></span></span></p>
<p class="MsoNormal" style="line-height:normal"><span lang="EN-US" style="font-size:12pt;font-family:"Times New Roman",serif">rs <- rstudent(mod)<span></span></span></p>
<p class="MsoNormal" style="line-height:normal"><span lang="EN-US" style="font-size:12pt;font-family:"Times New Roman",serif">h <- lm.influence(mod)$hat; lc <- 3*P/N<span></span></span></p>
<p class="MsoNormal" style="line-height:normal"><span lang="EN-US" style="font-size:12pt;font-family:"Times New Roman",serif">minrs=min(min(rs),-3)<span></span></span></p>
<p class="MsoNormal" style="line-height:normal"><span lang="EN-US" style="font-size:12pt;font-family:"Times New Roman",serif">maxrs=max(max(rs),3)<span></span></span></p>
<p class="MsoNormal" style="line-height:normal"><span lang="EN-US" style="font-size:12pt;font-family:"Times New Roman",serif">ymin=minrs-.1<span></span></span></p>
<p class="MsoNormal" style="line-height:normal"><span lang="EN-US" style="font-size:12pt;font-family:"Times New Roman",serif">ymax=maxrs+.1<span></span></span></p>
<p class="MsoNormal" style="line-height:normal"><span lang="EN-US" style="font-size:12pt;font-family:"Times New Roman",serif">maxh=max(max(h),lc)<span></span></span></p>
<p class="MsoNormal" style="line-height:normal"><span style="font-size:12pt;font-family:"Times New Roman",serif">minh=min(h)<span></span></span></p>
<p class="MsoNormal" style="line-height:normal"><span style="font-size:12pt;font-family:"Times New Roman",serif">xmin=minh-.1<span></span></span></p>
<p class="MsoNormal" style="line-height:normal"><span lang="EN-US" style="font-size:12pt;font-family:"Times New Roman",serif">xmax=maxh+.1<span></span></span></p>
<p class="MsoNormal" style="line-height:normal"><span lang="EN-US" style="font-size:12pt;font-family:"Times New Roman",serif">par(mfrow=c(1,1))<span></span></span></p>
<p class="MsoNormal" style="line-height:normal"><span lang="EN-US" style="font-size:12pt;font-family:"Times New Roman",serif">plot(c(xmin,xmax),c(ymin,ymax), type="n", xlab="h -
leverage", ylab="RStudent", main= i)<span></span></span></p>
<p class="MsoNormal" style="line-height:normal"><span lang="EN-US" style="font-size:12pt;font-family:"Times New Roman",serif">abline(h=-2.5, col="red")<span></span></span></p>
<p class="MsoNormal" style="line-height:normal"><span lang="EN-US" style="font-size:12pt;font-family:"Times New Roman",serif">abline(h=2.5,col="red"); abline(v=lc, col="blue")<span></span></span></p>
<p class="MsoNormal" style="line-height:normal"><span lang="EN-US" style="font-size:12pt;font-family:"Times New Roman",serif">points(h,rs)<span></span></span></p>
<p class="MsoNormal" style="line-height:normal"><span lang="EN-US" style="font-size:12pt;font-family:"Times New Roman",serif">plot(dad, main= i)<span></span></span></p>
<p class="MsoNormal" style="line-height:normal"><span lang="EN-US" style="font-size:12pt;font-family:"Times New Roman",serif">abline(mod,lty=2)<span></span></span></p>
<p class="MsoNormal" style="line-height:normal"><span lang="EN-US" style="font-size:12pt;font-family:"Times New Roman",serif">}<span></span></span></p>
<p class="MsoNormal" style="line-height:normal"><span lang="EN-US" style="font-size:12pt;font-family:"Times New Roman",serif">dev.off() <span></span></span></p>
<p class="MsoNormal" style="line-height:normal"><span style="font-size:12pt;font-family:"Times New Roman",serif">}<span></span></span></p><p class="MsoNormal" style="line-height:normal"><span style="font-size:12pt;font-family:"Times New Roman",serif"><br></span></p><p class="MsoNormal" style="line-height:normal"><span style="font-size:12pt;font-family:"Times New Roman",serif">Agradeço,</span></p><p class="MsoNormal" style="line-height:normal"><span style="font-size:12pt;font-family:"Times New Roman",serif"><br></span></p></div>-- <br><div class="gmail_signature"><div dir="ltr"><div><div dir="ltr"><div><div style="font-family:times,serif;margin:0cm 0cm 0.0001pt;font-size:16px"><span style="white-space:pre"> </span>Adriele Giaretta Biase.</div><div style="font-family:times,serif;margin:0cm 0cm 0.0001pt;font-size:16px"><span style="white-space:pre"> </span>Mestre em Estatística e Experimentação Agropecuária - UFLA. <br><span style="white-space:pre"> </span>Doutora em Estatística e Experimentação Agronômica - ESALQ/ USP</div><div style="font-family:times,serif;margin:0cm 0cm 0.0001pt;font-size:16px"><span style="font-size:12pt;white-space:pre"> </span><span style="font-size:12pt">Contato: (19) 98861-0619.</span></div></div></div></div></div></div>
</div>