<div dir="ltr"><div><div><div><div><div><div><div><div><div><div><div>Eduardo,<br><br></div>Sem CMR para utilizar minha sugestão é você alternar ordem que os efeitos principais entram no modelo... acompanhe:<br><span id="gmail-m_7905374866802133593yui_3_16_0_ym19_1_1498049847166_133224"><br id="gmail-m_7905374866802133593yui_3_16_0_ym19_1_1498049847166_133189">> car::Anova(</span><span id="gmail-m_7905374866802133593yui_3_16_0_ym19_1_1498049847166_133224"><span id="gmail-m_7905374866802133593yui_3_16_0_ym19_1_1498049847166_133224">lm(y ~ factorA * factorB, data = dados),</span> type = 2)<br><br></span></div><span id="gmail-m_7905374866802133593yui_3_16_0_ym19_1_1498049847166_133224">Funciona para o segundo termo (factorB). Assim, alterando a ordem na especificaçao:<br><br></span></div><span id="gmail-m_7905374866802133593yui_3_16_0_ym19_1_1498049847166_133224"></span></div><span id="gmail-m_7905374866802133593yui_3_16_0_ym19_1_1498049847166_133224">> car::Anova(</span><span id="gmail-m_7905374866802133593yui_3_16_0_ym19_1_1498049847166_133224"><span id="gmail-m_7905374866802133593yui_3_16_0_ym19_1_1498049847166_133224">lm(y ~ factorB * factorA, data = dados)</span>, type = '2)<br><br></span></div><span id="gmail-m_7905374866802133593yui_3_16_0_ym19_1_1498049847166_133224">Vai retornar a SSII correta para o factorA.<br><br></span></div><span id="gmail-m_7905374866802133593yui_3_16_0_ym19_1_1498049847166_133224">Para as SSIII é necessário (assim como no modo drop1()) definir contrastes soma zero, portanto:<br><br></span></div><span id="gmail-m_7905374866802133593yui_3_16_0_ym19_1_1498049847166_133224">> </span><span id="gmail-m_7905374866802133593yui_3_16_0_ym19_1_1498049847166_133224">car::Anova(lm(y ~ factorA * factorB, data =dados, </span><span id="gmail-m_7905374866802133593yui_3_16_0_ym19_1_1498049847166_133224"><span id="gmail-m_7905374866802133593yui_3_16_0_ym19_1_1498049847166_133224"> contrasts = list(factorA = contr.<wbr>sum, factorB = contr.sum)), type = 3)</span></span><span id="gmail-m_7905374866802133593yui_3_16_0_ym19_1_1498049847166_133224"><span id="gmail-m_7905374866802133593yui_3_16_0_ym19_1_1498049847166_133224"><br>> </span></span><span id="gmail-m_7905374866802133593yui_3_16_0_ym19_1_1498049847166_133224">car::Anova(lm(y ~ factorB * factorA, data =dados, </span><span id="gmail-m_7905374866802133593yui_3_16_0_ym19_1_1498049847166_133224"><span id="gmail-m_7905374866802133593yui_3_16_0_ym19_1_1498049847166_133224"> contrasts = list(factorA = contr.<wbr>sum, factorB = contr.sum)), type = 3)<br><br></span></span></div><span id="gmail-m_7905374866802133593yui_3_16_0_ym19_1_1498049847166_133224"><span id="gmail-m_7905374866802133593yui_3_16_0_ym19_1_1498049847166_133224">Uma explicação seria... : SSII testa um efeitos principal depois de outro (por isso a ordem importa) enquanto que na SSIII a interação "superparametriza" o modelo, por isso a necessidade da especificação dos contrastes.<br><br></span></span></div><span id="gmail-m_7905374866802133593yui_3_16_0_ym19_1_1498049847166_133224"><span id="gmail-m_7905374866802133593yui_3_16_0_ym19_1_1498049847166_133224">No aguardo da sua resposta.<br><br></span></span></div><span id="gmail-m_7905374866802133593yui_3_16_0_ym19_1_1498049847166_133224"><span id="gmail-m_7905374866802133593yui_3_16_0_ym19_1_1498049847166_133224">att,<br></span></span></div><span id="gmail-m_7905374866802133593yui_3_16_0_ym19_1_1498049847166_133224"><span id="gmail-m_7905374866802133593yui_3_16_0_ym19_1_1498049847166_133224">FH<br></span></span><div><div><div><div><div><div><div><div class="gmail_extra"><br><div class="gmail_quote">2017-06-21 9:23 GMT-05:00 Eduardo Bearzoti via R-br <span dir="ltr"><<a href="mailto:r-br@listas.c3sl.ufpr.br" target="_blank">r-br@listas.c3sl.ufpr.br</a>></span>:<br><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div><div style="color:rgb(0,0,0);background-color:rgb(255,255,255);font-family:Helvetica Neue,Helvetica,Arial,Lucida Grande,sans-serif;font-size:16px"><div id="gmail-m_7905374866802133593yui_3_16_0_ym19_1_1498049847166_132816" dir="ltr"><span id="gmail-m_7905374866802133593yui_3_16_0_ym19_1_1498049847166_133224">Bom dia, pessoal.<br id="gmail-m_7905374866802133593yui_3_16_0_ym19_1_1498049847166_133123"><br id="gmail-m_7905374866802133593yui_3_16_0_ym19_1_1498049847166_133124">Minha dúvida é particularmente sobre o cálculo de somas de quadrados tipo III (mas também do tipo II) no R com conjuntos de dados desbalanceados.<br id="gmail-m_7905374866802133593yui_3_16_0_ym19_1_1498049847166_133125"><br id="gmail-m_7905374866802133593yui_3_16_0_ym19_1_1498049847166_133126">Estou ciente da polêmica em torno do uso destas somas de quadrados na comunidade estatística, mas gostaria muito de poder calculá-las no R, e, a não ser que eu esteja enganado, nem sempre o R as calcula corretamente.<br id="gmail-m_7905374866802133593yui_3_16_0_ym19_1_1498049847166_133127"><br id="gmail-m_7905374866802133593yui_3_16_0_ym19_1_1498049847166_133128">Pesquisando na rede, descobri que em geral são apontadas duas maneiras de se calcular somas de quadrados tipo III no R: a) Utilizando a função drop1() em modelos ajustados com a especificação de contrastes contr.sum (ou contr.helmert), e b) utilizando a função Anova() (com "A" maiúsculo), do pacote car. <br id="gmail-m_7905374866802133593yui_3_16_0_ym19_1_1498049847166_133129"><br id="gmail-m_7905374866802133593yui_3_16_0_ym19_1_1498049847166_133130">Abaixo, eu apresento um exemplo simulado de um ensaio fatorial altamente desbalanceado, faltando várias combinações entre os níveis dos dois fatores. A interação entre eles, por exemplo, apresenta um único grau de liberdade, tamanho o desbalanceamento.<br id="gmail-m_7905374866802133593yui_3_16_0_ym19_1_1498049847166_133131"><br id="gmail-m_7905374866802133593yui_3_16_0_ym19_1_1498049847166_133132">dados <- data.frame(y=c(35.44627,43.<wbr>43353,40.20125,38.1061,39.<wbr>96211,44.21636,19.78237,23.<wbr>58636,31.73537,<br id="gmail-m_7905374866802133593yui_3_16_0_ym19_1_1498049847166_133133"> 36.49977,38.71573,41.00456,19.<wbr>6787,16.43436,32.05407,33.<wbr>3784,18.58266,17.74051),<br id="gmail-m_7905374866802133593yui_3_16_0_ym19_1_1498049847166_133134"> factorA=as.factor(c(2,2,4,4,4,<wbr>4,6,6,6,6,6,6,8,8,8,8,10,10)),<br id="gmail-m_7905374866802133593yui_3_16_0_ym19_1_1498049847166_133135"> factorB=as.factor(c(2,2,1.5,1.<wbr>5,2.5,2.5,1,1,2,2,3,3,1.5,1.5,<wbr>2.5,2.5,2,2)))<br id="gmail-m_7905374866802133593yui_3_16_0_ym19_1_1498049847166_133136"><br id="gmail-m_7905374866802133593yui_3_16_0_ym19_1_1498049847166_133137">Tenho em mente o modelo contendo os fatores A e B, bem como sua interação. <br id="gmail-m_7905374866802133593yui_3_16_0_ym19_1_1498049847166_133138"><br id="gmail-m_7905374866802133593yui_3_16_0_ym19_1_1498049847166_133139">No SAS, utilizando o proc glm, obtenho as seguintes somas de quadrados tipo I, tipo II e tipo III:<br id="gmail-m_7905374866802133593yui_3_16_0_ym19_1_1498049847166_133140"><br id="gmail-m_7905374866802133593yui_3_16_0_ym19_1_1498049847166_133141">Source DF Type I SS<br id="gmail-m_7905374866802133593yui_3_16_0_ym19_1_1498049847166_133142"><br id="gmail-m_7905374866802133593yui_3_16_0_ym19_1_1498049847166_133143">factorA 4 964.4164933<br id="gmail-m_7905374866802133593yui_3_16_0_ym19_1_1498049847166_133144">factorB 3 500.0771638<br id="gmail-m_7905374866802133593yui_3_16_0_ym19_1_1498049847166_133145">factorA*factorB 1 68.7277880<br id="gmail-m_7905374866802133593yui_3_16_0_ym19_1_1498049847166_133146"><br id="gmail-m_7905374866802133593yui_3_16_0_ym19_1_1498049847166_133147">Source DF Type II SS<br id="gmail-m_7905374866802133593yui_3_16_0_ym19_1_1498049847166_133148"><br id="gmail-m_7905374866802133593yui_3_16_0_ym19_1_1498049847166_133149">factorA 3 954.6730970<br id="gmail-m_7905374866802133593yui_3_16_0_ym19_1_1498049847166_133150">factorB 3 500.0771638<br id="gmail-m_7905374866802133593yui_3_16_0_ym19_1_1498049847166_133151">factorA*factorB 1 68.7277880<br id="gmail-m_7905374866802133593yui_3_16_0_ym19_1_1498049847166_133152"><br id="gmail-m_7905374866802133593yui_3_16_0_ym19_1_1498049847166_133153">Source DF Type III SS<br id="gmail-m_7905374866802133593yui_3_16_0_ym19_1_1498049847166_133154"><br id="gmail-m_7905374866802133593yui_3_16_0_ym19_1_1498049847166_133155">factorA 3 954.6730970<br id="gmail-m_7905374866802133593yui_3_16_0_ym19_1_1498049847166_133156">factorB 3 500.0771638<br id="gmail-m_7905374866802133593yui_3_16_0_ym19_1_1498049847166_133157">factorA*factorB 1 68.7277880<br id="gmail-m_7905374866802133593yui_3_16_0_ym19_1_1498049847166_133158"><br id="gmail-m_7905374866802133593yui_3_16_0_ym19_1_1498049847166_133159">Percebam que neste exemplo as somas de quadrados tipo II e III são iguais, pois as "caselas" apresentam o mesmo número de repetições (duas).<br id="gmail-m_7905374866802133593yui_3_16_0_ym19_1_1498049847166_133160"><br id="gmail-m_7905374866802133593yui_3_16_0_ym19_1_1498049847166_133161">No R, tentei reproduzir este resultado seguindo os dois procedimentos citados acima:<br id="gmail-m_7905374866802133593yui_3_16_0_ym19_1_1498049847166_133162"><br id="gmail-m_7905374866802133593yui_3_16_0_ym19_1_1498049847166_133163">1) Utilizando a função drop1()<br id="gmail-m_7905374866802133593yui_3_16_0_ym19_1_1498049847166_133164"><br id="gmail-m_7905374866802133593yui_3_16_0_ym19_1_1498049847166_133165">> mod <- lm(y ~ factorA*factorB, <br id="gmail-m_7905374866802133593yui_3_16_0_ym19_1_1498049847166_133166"> contrasts=list(factorA=contr.<wbr>sum, factorB=contr.sum),<br id="gmail-m_7905374866802133593yui_3_16_0_ym19_1_1498049847166_133167"> data=dados)<br id="gmail-m_7905374866802133593yui_3_16_0_ym19_1_1498049847166_133168"><br id="gmail-m_7905374866802133593yui_3_16_0_ym19_1_1498049847166_133169">> drop1(mod, ~.,test = "F")<br id="gmail-m_7905374866802133593yui_3_16_0_ym19_1_1498049847166_133170">Single term deletions<br id="gmail-m_7905374866802133593yui_3_16_0_ym19_1_1498049847166_133171"><br id="gmail-m_7905374866802133593yui_3_16_0_ym19_1_1498049847166_133172">Model:<br id="gmail-m_7905374866802133593yui_3_16_0_ym19_1_1498049847166_133173">y ~ factorA * factorB<br id="gmail-m_7905374866802133593yui_3_16_0_ym19_1_1498049847166_133174"> Df Sum of Sq RSS AIC F value Pr(>F) <br id="gmail-m_7905374866802133593yui_3_16_0_ym19_1_1498049847166_133175"><none> 70.841 42.661 <br id="gmail-m_7905374866802133593yui_3_16_0_ym19_1_1498049847166_133176">factorA 0 0.000 70.841 42.661 <br id="gmail-m_7905374866802133593yui_3_16_0_ym19_1_1498049847166_133177">factorB 0 0.000 70.841 42.661 <br id="gmail-m_7905374866802133593yui_3_16_0_ym19_1_1498049847166_133178">factorA:factorB 1 68.728 139.569 52.867 8.7315 0.01609 *<br id="gmail-m_7905374866802133593yui_3_16_0_ym19_1_1498049847166_133179">---<br id="gmail-m_7905374866802133593yui_3_16_0_ym19_1_1498049847166_133180">Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1<br id="gmail-m_7905374866802133593yui_3_16_0_ym19_1_1498049847166_133181"><br id="gmail-m_7905374866802133593yui_3_16_0_ym19_1_1498049847166_133182">(apenas a soma de quadrados da interação foi calculada)<br id="gmail-m_7905374866802133593yui_3_16_0_ym19_1_1498049847166_133183"><br id="gmail-m_7905374866802133593yui_3_16_0_ym19_1_1498049847166_133184"><br id="gmail-m_7905374866802133593yui_3_16_0_ym19_1_1498049847166_133185">2) Utilizando car::Anova()<br id="gmail-m_7905374866802133593yui_3_16_0_ym19_1_1498049847166_133186"><br id="gmail-m_7905374866802133593yui_3_16_0_ym19_1_1498049847166_133187">> mod <- lm(y ~ factorA*factorB, data=dados)<br id="gmail-m_7905374866802133593yui_3_16_0_ym19_1_1498049847166_133188">> library(car)<br id="gmail-m_7905374866802133593yui_3_16_0_ym19_1_1498049847166_133189">> Anova(mod,type = "II")<br id="gmail-m_7905374866802133593yui_3_16_0_ym19_1_1498049847166_133190">Anova Table (Type II tests)<br id="gmail-m_7905374866802133593yui_3_16_0_ym19_1_1498049847166_133191"><br id="gmail-m_7905374866802133593yui_3_16_0_ym19_1_1498049847166_133192">Response: y<br id="gmail-m_7905374866802133593yui_3_16_0_ym19_1_1498049847166_133193"> Sum Sq Df F value Pr(>F) <br id="gmail-m_7905374866802133593yui_3_16_0_ym19_1_1498049847166_133194">factorA 1284.10 4 40.7845 8.995e-06 ***<br id="gmail-m_7905374866802133593yui_3_16_0_ym19_1_1498049847166_133195">factorB 500.08 3 21.1774 0.0002047 ***<br id="gmail-m_7905374866802133593yui_3_16_0_ym19_1_1498049847166_133196">factorA:factorB 68.73 1 8.7315 0.0160919 * <br id="gmail-m_7905374866802133593yui_3_16_0_ym19_1_1498049847166_133197">Residuals 70.84 9 <br id="gmail-m_7905374866802133593yui_3_16_0_ym19_1_1498049847166_133198">---<br id="gmail-m_7905374866802133593yui_3_16_0_ym19_1_1498049847166_133199">Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1<br id="gmail-m_7905374866802133593yui_3_16_0_ym19_1_1498049847166_133200">> Anova(mod,type = "III")<br id="gmail-m_7905374866802133593yui_3_16_0_ym19_1_1498049847166_133201">Error in Anova.III.lm(mod, error, singular.ok = singular.ok, ...) : <br id="gmail-m_7905374866802133593yui_3_16_0_ym19_1_1498049847166_133202"> there are aliased coefficients in the model</span></div><div dir="ltr" id="gmail-m_7905374866802133593yui_3_16_0_ym19_1_1498049847166_133297"><br><span id="gmail-m_7905374866802133593yui_3_16_0_ym19_1_1498049847166_133224"></span></div><div dir="ltr" id="gmail-m_7905374866802133593yui_3_16_0_ym19_1_1498049847166_133351"><span id="gmail-m_7905374866802133593yui_3_16_0_ym19_1_1498049847166_133224">(soma de quadrados tipo II calculada errada para o fator A, e tipo III não calculadas)<br></span></div><div dir="ltr"><span id="gmail-m_7905374866802133593yui_3_16_0_ym19_1_1498049847166_133224"><br></span></div><div dir="ltr" id="gmail-m_7905374866802133593yui_3_16_0_ym19_1_1498049847166_133353"><span id="gmail-m_7905374866802133593yui_3_16_0_ym19_1_1498049847166_133224">Ou seja, não consegui reproduzir nem as somas de quadrados tipo III nem as do tipo II, por nenhum dos dois procedimentos sugeridos na rede.<br id="gmail-m_7905374866802133593yui_3_16_0_ym19_1_1498049847166_133205"><br id="gmail-m_7905374866802133593yui_3_16_0_ym19_1_1498049847166_133206">Estou utilizando:<br id="gmail-m_7905374866802133593yui_3_16_0_ym19_1_1498049847166_133207">> sessionInfo()<br id="gmail-m_7905374866802133593yui_3_16_0_ym19_1_1498049847166_133208">R version 3.4.0 (2017-04-21)<br id="gmail-m_7905374866802133593yui_3_16_0_ym19_1_1498049847166_133209">Platform: x86_64-pc-linux-gnu (64-bit)<br id="gmail-m_7905374866802133593yui_3_16_0_ym19_1_1498049847166_133210">Running under: Ubuntu 14.04.5 LTS<br id="gmail-m_7905374866802133593yui_3_16_0_ym19_1_1498049847166_133211">(...)<br id="gmail-m_7905374866802133593yui_3_16_0_ym19_1_1498049847166_133212">other attached packages:<br id="gmail-m_7905374866802133593yui_3_16_0_ym19_1_1498049847166_133213">[1] car_2.1-4<br id="gmail-m_7905374866802133593yui_3_16_0_ym19_1_1498049847166_133214"><br id="gmail-m_7905374866802133593yui_3_16_0_ym19_1_1498049847166_133215"><br id="gmail-m_7905374866802133593yui_3_16_0_ym19_1_1498049847166_133216"><br id="gmail-m_7905374866802133593yui_3_16_0_ym19_1_1498049847166_133217">Eu estaria fazendo alguma coisa equivocada? Qualquer observação, sugestão, ou indicação de alguma maneira alternativa para cálculo das somas de quadrados tipo III no R será muito bem vinda.<br id="gmail-m_7905374866802133593yui_3_16_0_ym19_1_1498049847166_133218"><br id="gmail-m_7905374866802133593yui_3_16_0_ym19_1_1498049847166_133219">Abs,<br id="gmail-m_7905374866802133593yui_3_16_0_ym19_1_1498049847166_133220"><br id="gmail-m_7905374866802133593yui_3_16_0_ym19_1_1498049847166_133221">Eduardo Bearzoti<br id="gmail-m_7905374866802133593yui_3_16_0_ym19_1_1498049847166_133222">(UFOP)</span></div><div id="gmail-m_7905374866802133593yui_3_16_0_ym19_1_1498049847166_132877"> </div><div class="gmail-m_7905374866802133593signature" id="gmail-m_7905374866802133593yui_3_16_0_ym19_1_1498049847166_132878"><br></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><br></div></div></div></div></div></div></div></div></div>