<div dir="auto">Leia sobre medidas repetidas e modelos de efeitos mistos</div><div class="gmail_extra"><br><div class="gmail_quote">Em 20 de jul de 2017 4:43 PM, "Adriele Giaretta Biase via R-br" <<a href="mailto:r-br@listas.c3sl.ufpr.br">r-br@listas.c3sl.ufpr.br</a>> escreveu:<br type="attribution"><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><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.<wbr>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$<wbr>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*<wbr>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)<wbr>, 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="m_-6347850660928326005gmail_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-wrap"> </span>Adriele Giaretta Biase.</div><div style="font-family:times,serif;margin:0cm 0cm 0.0001pt;font-size:16px"><span style="white-space:pre-wrap"> </span>Mestre em Estatística e Experimentação Agropecuária - UFLA. <br><span style="white-space:pre-wrap"> </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-wrap"> </span><span style="font-size:12pt">Contato: <a href="tel:(19)%2098861-0619" value="+5519988610619" target="_blank">(19) 98861-0619</a>.</span></div></div></div></div></div></div>
</div>
<br>______________________________<wbr>_________________<br>
R-br mailing list<br>
<a href="mailto:R-br@listas.c3sl.ufpr.br">R-br@listas.c3sl.ufpr.br</a><br>
<a href="https://listas.inf.ufpr.br/cgi-bin/mailman/listinfo/r-br" rel="noreferrer" target="_blank">https://listas.inf.ufpr.br/<wbr>cgi-bin/mailman/listinfo/r-br</a><br>
Leia o guia de postagem (<a href="http://www.leg.ufpr.br/r-br-guia" rel="noreferrer" target="_blank">http://www.leg.ufpr.br/r-br-<wbr>guia</a>) e forneça código mínimo reproduzível.<br></blockquote></div></div>