class: center, middle, inverse, title-slide # šØāš« CorrelaĆ§Ć£o e RegressĆ£o ##
š
Aula CorrelaĆ§Ć£o
### Steven Dutt Ross ### UNIRIO --- <style type="text/css"> @import url('https://fonts.googleapis.com/css2?family=Acme&display=swap'); @import url('https://fonts.googleapis.com/css2?family=Architects+Daughter&display=swap'); @import url('https://fonts.googleapis.com/css?family=Gochi+Hand|Handlee&display=swap'); body { background-color: #f7f68f; color: #1a5b78; font-family: 'Acme', sans-serif; font-size: 26px; } a:link { color: #ffffff; } .MathJax { font-size: 1.3em !important; } .huge-text { font-family: 'Montserrat', sans-serif; font-size: 600%; font-weight: bold; color: var(--monument); } .huge-number { font-family: 'Montserrat', sans-serif; font-size: 1700%; font-weight: bold; color: var(--baby-blue); } /* Font sizes */ .larger { font-size: 400% } .bitlarger { font-size: 200% } .large { font-size: 130% } .midi { font-size: 85% } .small { font-size: 70% } .xsmall { font-size: 60% } .tiny { font-size: 50% } /* Handwriting */ .hand{ font-family: 'Gochi Hand', cursive; color: var(--monument); font-size: 125%; } .hand-large{ font-family: 'Gochi Hand', cursive; color: var(--monument); font-size: 500%; } /* Footer */ div.my-footer { border-top: 1px solid var(--opal-gray); position: absolute; bottom: 0px; height: 35px; width: 90%; } div.my-footer span { font-size: 12pt; position: absolute; color: #333333; bottom: 10px; } div.my-footer a { font-size: 12pt; color: #333333; } /* Footnote */ .footnote { position: absolute; bottom: 1em; padding-right: 4em; font-size: 70%; } /*Left 70% - Right 30% */ .pull-left-wide { width: 70%; float: left; } .pull-right-narrow { width: 27%; float: right; } .pull-right-narrow ~ * { clear: both; } /*Left 30% - Right 70% */ .pull-left-narrow { width: 30%; float: left; } .pull-right-wide { width: 67%; float: right; } .pull-right-wide ~ * { clear: both; } </style> ## .hand[O Diagrama de dispersĆ£o] .hand[O Diagrama de dispersĆ£o Ć© um grĆ”fico onde pontos no espaƧo cartesiano (X,Y) sĆ£o usados para representar simultaneamente os valores de duas variĆ”veis quantitativas medidas em cada elemento do conjunto de dados.] --- <img src="img/correlacao.png" alt="correlacao" width="800"/> --- ```r x <- c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10) y <- c(100, 200,300,400,500,600, 700, 800, 900, 1000) par(mfrow=c(1,2)) plot(x,y, col = "red", pch=21,lwd = 10) y <- c(210, 50,280,400,590,540, 730, 770, 800, 1100) plot(x,y, col = "blue",pch=21, lwd = 10) ``` <img src="index_files/figure-html/dispe-1.png" style="display: block; margin: auto;" /> ```r par(mfrow=c(1,1)) ``` --- ```r idade = c(56, 30, 40, 32, 39, 23, 17, 20, 28, 16) qtdmiojo = c(17, 29, 27, 35, 27, 56, 58, 54, 50, 38) datacor = data.frame(qtdmiojo,idade) datacor ``` ``` ## qtdmiojo idade ## 1 17 56 ## 2 29 30 ## 3 27 40 ## 4 35 32 ## 5 27 39 ## 6 56 23 ## 7 58 17 ## 8 54 20 ## 9 50 28 ## 10 38 16 ``` --- Este diagrama de dispersĆ£o tem um padrĆ£o linear geral (reta), mas a relaĆ§Ć£o Ć© negativa. ![](index_files/figure-html/idade3-1.png)<!-- --> --- Essa padrĆ£o linear Ć© facil de ver. ![](index_files/figure-html/idade4-1.png)<!-- --> --- ```r #Outro exemplo: pena de morte e aborto penademorte <- c(7,7,3,0,0,10,5,7,0,10,1,8,1,9,8,8,4,10,10,9) aborto <- c(1,2,7,7,9, 3,9,5,8,4,10,3,9,2,2,6,8,6,6,8) plot(penademorte,aborto,col = "darkblue",pch=21) abline(lsfit(penademorte,aborto),col="darkred") ``` ![](index_files/figure-html/aborto-1.png)<!-- --> --- Quais variĆ”veis tĆŖm padrĆ£o linear positivo? quais tĆŖm padrĆ£o linear negativo? Quais nĆ£o tem um padrĆ£o (padrĆ£o nulo)? ![](index_files/figure-html/tipo-1.png)<!-- --> --- class: center, middle .huge-text[CorrelaĆ§Ć£o...] --- ## O que Ć© correlaĆ§Ć£o? A CorrelaĆ§Ć£o mede a **direĆ§Ć£o** (positivas ou negativas) e a **intensidade** (forƧa) da relaĆ§Ć£o linear entre duas variĆ”veis quantitativas (relacionamento entre duas variĆ”veis). Costuma-se representar a correlaĆ§Ć£o pela letra *r*. --- ## Fatos sobre a correlaĆ§Ć£o <p> **A correlaĆ§Ć£o nĆ£o faz distinĆ§Ć£o entre variĆ”vel explicativa e variĆ”vel resposta** NĆ£o faz diferenƧa alguma qual variĆ”vel vocĆŖ chama de *x* e qual vocĆŖ chama de *y*, ao calcular a correlaĆ§Ć£o. <p> <p> **r positivo indica uma associaĆ§Ć£o positiva entre as variĆ”veis**, e r negativo indica uma associaĆ§Ć£o negativa. <p> <p> **A correlaĆ§Ć£o Ć© sempre um nĆŗmero entre -1 e 1.** Valores prĆ³ximos de zero indicam uma relaĆ§Ć£o linear muito fraca. A intensidade da relaĆ§Ć£o linear cresce, Ć medida que *r* se afasta de zero em direĆ§Ć£o a -1 ou 1. Os valores de *r* prĆ³ximos de -1 ou 1 indicam que os pontos num diagrama de dispersĆ£o caem prĆ³ximos de uma reta. Os valores extremos *r= -1* e *r= 1* ocorrem apenas no caso de relaĆ§Ć£o linear perfeita , quando os pontos caem exatamente sobre a reta. --- ## Coeficiente de correlaĆ§Ć£o produto-momento de Pearson (r) <p> Mede a intensidade e a direĆ§Ć£o da relaĆ§Ć£o entre duas variĆ”veis contĆnuas ## Tipos de correlaƧƵes <p> **CorrelaĆ§Ć£o de Pearson** para variĆ”veis continuas <p> **CorrelaĆ§Ć£o de Spearman** para variĆ”veis ordinais [Para saber mais clique aqui](http://rstudio-pubs-static.s3.amazonaws.com/10539_9a0d69971efd414d96bfb4b8cc20e76f.html#/6) <p> [Fonte](http://www.seer.ufu.br/index.php/cieng/issue/view/108) --- ### InterpretaĆ§Ć£o do r - Valores de referĆŖncia #### Negativo <p> -0,2 < r < 0 baixa ou nenhuma associaĆ§Ć£o <p> -0,7 < r < -0,2 grau fraco/moderado de associaĆ§Ć£o <p> < -0,7 grau excelente de associaĆ§Ć£o #### Positivo <p> 0 < r < 0,2 baixa ou nenhuma associaĆ§Ć£o (ou -0,2 < r < 0) <p> 0,2 < r < 0,7 grau fraco/moderado de associaĆ§Ć£o (ou -0,7 < r < -0,2) <p> r > 0,7 grau excelente de associaĆ§Ć£o (ou <-0,7) --- <img src="img/correlacao.png" alt="correlacao" width="800"/> --- ## FĆ³rmula da correlaĆ§Ć£o de Pearson $$ \Huge{ r =\frac{COV(x,y)}{S_xS_y} }$$ Onde: * COV = covariĆ¢ncia * S = Desvio-padrĆ£o * CovariĆ¢ncia Ć© o nĆŗmero que reflete o grau em que duas variĆ”veis variam juntas. $$ \Huge{COV = \frac{\Sigma(X - \overline{X})(Y - \overline{Y})}{N - 1}} $$ --- ## FĆ³rmula alternativa `$$\Huge{r = \frac{n*\Sigma(X*Y) - \Sigma(X)*\Sigma(Y)}{\sqrt{n*\Sigma(X)^2-(\Sigma(X))^2}\sqrt{n*\Sigma(Y)^2-(\Sigma(Y))^2}}}$$` ## Como aplicar a FĆ³rmula em um conjunto de dados? --- ## Banco de dados #### para o cĆ”lculo da correlaĆ§Ć£o <template id="7b9abeb3-6e10-4b46-bc86-5852cb259651"><style> .tabwid table{ border-collapse:collapse; line-height:1; margin-left:auto; margin-right:auto; border-width: 0; display: table; margin-top: 1.275em; margin-bottom: 1.275em; border-spacing: 0; border-color: transparent; } .tabwid_left table{ margin-left:0; } .tabwid_right table{ margin-right:0; } .tabwid td { padding: 0; } .tabwid a { text-decoration: none; } .tabwid thead { background-color: transparent; } .tabwid tfoot { background-color: transparent; } .tabwid table tr { background-color: transparent; } </style><div class="tabwid"><style>.cl-2b9d9f5e{border-collapse:collapse;}.cl-2b8efb20{font-family:'Arial';font-size:22pt;font-weight:bold;font-style:normal;text-decoration:none;color:rgba(0, 0, 0, 1.00);background-color:transparent;}.cl-2b8efb21{font-family:'Arial';font-size:22pt;font-weight:normal;font-style:normal;text-decoration:none;color:rgba(0, 0, 0, 1.00);background-color:transparent;}.cl-2b8f22d0{margin:0;text-align:center;border-bottom: 0 solid rgba(0, 0, 0, 1.00);border-top: 0 solid rgba(0, 0, 0, 1.00);border-left: 0 solid rgba(0, 0, 0, 1.00);border-right: 0 solid rgba(0, 0, 0, 1.00);padding-bottom:5pt;padding-top:5pt;padding-left:5pt;padding-right:5pt;line-height: 1;background-color:transparent;}.cl-2b8f9760{width:54pt;background-color:transparent;vertical-align: middle;border-bottom: 1pt solid rgba(102, 102, 102, 1.00);border-top: 0 solid rgba(0, 0, 0, 1.00);border-left: 1pt solid rgba(102, 102, 102, 1.00);border-right: 1pt solid rgba(102, 102, 102, 1.00);margin-bottom:0;margin-top:0;margin-left:0;margin-right:0;}.cl-2b8f9761{width:54pt;background-color:transparent;vertical-align: middle;border-bottom: 1pt solid rgba(102, 102, 102, 1.00);border-top: 1pt solid rgba(102, 102, 102, 1.00);border-left: 1pt solid rgba(102, 102, 102, 1.00);border-right: 1pt solid rgba(102, 102, 102, 1.00);margin-bottom:0;margin-top:0;margin-left:0;margin-right:0;}</style><table class='cl-2b9d9f5e'><thead><tr style="overflow-wrap:break-word;"><td class="cl-2b8f9761"><p class="cl-2b8f22d0"><span class="cl-2b8efb20">x</span></p></td><td class="cl-2b8f9761"><p class="cl-2b8f22d0"><span class="cl-2b8efb20">y</span></p></td></tr></thead><tbody><tr style="overflow-wrap:break-word;"><td class="cl-2b8f9760"><p class="cl-2b8f22d0"><span class="cl-2b8efb21">2</span></p></td><td class="cl-2b8f9760"><p class="cl-2b8f22d0"><span class="cl-2b8efb21">4</span></p></td></tr><tr style="overflow-wrap:break-word;"><td class="cl-2b8f9761"><p class="cl-2b8f22d0"><span class="cl-2b8efb21">3</span></p></td><td class="cl-2b8f9761"><p class="cl-2b8f22d0"><span class="cl-2b8efb21">7</span></p></td></tr><tr style="overflow-wrap:break-word;"><td class="cl-2b8f9761"><p class="cl-2b8f22d0"><span class="cl-2b8efb21">4</span></p></td><td class="cl-2b8f9761"><p class="cl-2b8f22d0"><span class="cl-2b8efb21">9</span></p></td></tr><tr style="overflow-wrap:break-word;"><td class="cl-2b8f9761"><p class="cl-2b8f22d0"><span class="cl-2b8efb21">5</span></p></td><td class="cl-2b8f9761"><p class="cl-2b8f22d0"><span class="cl-2b8efb21">10</span></p></td></tr><tr style="overflow-wrap:break-word;"><td class="cl-2b8f9761"><p class="cl-2b8f22d0"><span class="cl-2b8efb21">5</span></p></td><td class="cl-2b8f9761"><p class="cl-2b8f22d0"><span class="cl-2b8efb21">11</span></p></td></tr><tr style="overflow-wrap:break-word;"><td class="cl-2b8f9761"><p class="cl-2b8f22d0"><span class="cl-2b8efb21">6</span></p></td><td class="cl-2b8f9761"><p class="cl-2b8f22d0"><span class="cl-2b8efb21">11</span></p></td></tr><tr style="overflow-wrap:break-word;"><td class="cl-2b8f9761"><p class="cl-2b8f22d0"><span class="cl-2b8efb21">7</span></p></td><td class="cl-2b8f9761"><p class="cl-2b8f22d0"><span class="cl-2b8efb21">13</span></p></td></tr><tr style="overflow-wrap:break-word;"><td class="cl-2b8f9761"><p class="cl-2b8f22d0"><span class="cl-2b8efb21">8</span></p></td><td class="cl-2b8f9761"><p class="cl-2b8f22d0"><span class="cl-2b8efb21">15</span></p></td></tr></tbody></table></div></template> <div class="flextable-shadow-host" id="813b9456-eda0-4816-9983-bae6751c3d12"></div> <script> var dest = document.getElementById("813b9456-eda0-4816-9983-bae6751c3d12"); var template = document.getElementById("7b9abeb3-6e10-4b46-bc86-5852cb259651"); var caption = template.content.querySelector("caption"); if(caption) { caption.style.cssText = "display:block;text-align:center;"; var newcapt = document.createElement("p"); newcapt.appendChild(caption) dest.parentNode.insertBefore(newcapt, dest.previousSibling); } var fantome = dest.attachShadow({mode: 'open'}); var templateContent = template.content; fantome.appendChild(templateContent); </script> --- class: center, middle .huge-text[Passo 1] .large[Fazer cĆ”lculos intermediĆ”rios] --- ## VariĆ”veis originais e cĆ”lculos intermediĆ”rios <template id="3477b540-3d08-408a-b27c-2d1a084dda41"><style> .tabwid table{ border-collapse:collapse; line-height:1; margin-left:auto; margin-right:auto; border-width: 0; display: table; margin-top: 1.275em; margin-bottom: 1.275em; border-spacing: 0; border-color: transparent; } .tabwid_left table{ margin-left:0; } .tabwid_right table{ margin-right:0; } .tabwid td { padding: 0; } .tabwid a { text-decoration: none; } .tabwid thead { background-color: transparent; } .tabwid tfoot { background-color: transparent; } .tabwid table tr { background-color: transparent; } </style><div class="tabwid"><style>.cl-2bda7992{border-collapse:collapse;}.cl-2bce57de{font-family:'Arial';font-size:22pt;font-weight:bold;font-style:normal;text-decoration:none;color:rgba(0, 0, 0, 1.00);background-color:transparent;}.cl-2bce57df{font-family:'Arial';font-size:22pt;font-weight:normal;font-style:normal;text-decoration:none;color:rgba(0, 0, 0, 1.00);background-color:transparent;}.cl-2bce7ee4{margin:0;text-align:center;border-bottom: 0 solid rgba(0, 0, 0, 1.00);border-top: 0 solid rgba(0, 0, 0, 1.00);border-left: 0 solid rgba(0, 0, 0, 1.00);border-right: 0 solid rgba(0, 0, 0, 1.00);padding-bottom:5pt;padding-top:5pt;padding-left:5pt;padding-right:5pt;line-height: 1;background-color:transparent;}.cl-2bcecd4a{width:54pt;background-color:transparent;vertical-align: middle;border-bottom: 1pt solid rgba(102, 102, 102, 1.00);border-top: 0 solid rgba(0, 0, 0, 1.00);border-left: 1pt solid rgba(102, 102, 102, 1.00);border-right: 1pt solid rgba(102, 102, 102, 1.00);margin-bottom:0;margin-top:0;margin-left:0;margin-right:0;}.cl-2bcecd4b{width:54pt;background-color:transparent;vertical-align: middle;border-bottom: 1pt solid rgba(102, 102, 102, 1.00);border-top: 1pt solid rgba(102, 102, 102, 1.00);border-left: 1pt solid rgba(102, 102, 102, 1.00);border-right: 1pt solid rgba(102, 102, 102, 1.00);margin-bottom:0;margin-top:0;margin-left:0;margin-right:0;}</style><table class='cl-2bda7992'><thead><tr style="overflow-wrap:break-word;"><td class="cl-2bcecd4b"><p class="cl-2bce7ee4"><span class="cl-2bce57de">x</span></p></td><td class="cl-2bcecd4b"><p class="cl-2bce7ee4"><span class="cl-2bce57de">y</span></p></td><td class="cl-2bcecd4b"><p class="cl-2bce7ee4"><span class="cl-2bce57de">x ao quadrado</span></p></td><td class="cl-2bcecd4b"><p class="cl-2bce7ee4"><span class="cl-2bce57de">y ao quadrado</span></p></td><td class="cl-2bcecd4b"><p class="cl-2bce7ee4"><span class="cl-2bce57de">x*y</span></p></td></tr></thead><tbody><tr style="overflow-wrap:break-word;"><td class="cl-2bcecd4a"><p class="cl-2bce7ee4"><span class="cl-2bce57df">2</span></p></td><td class="cl-2bcecd4a"><p class="cl-2bce7ee4"><span class="cl-2bce57df">4</span></p></td><td class="cl-2bcecd4a"><p class="cl-2bce7ee4"><span class="cl-2bce57df">4</span></p></td><td class="cl-2bcecd4a"><p class="cl-2bce7ee4"><span class="cl-2bce57df">16</span></p></td><td class="cl-2bcecd4a"><p class="cl-2bce7ee4"><span class="cl-2bce57df">8</span></p></td></tr><tr style="overflow-wrap:break-word;"><td class="cl-2bcecd4b"><p class="cl-2bce7ee4"><span class="cl-2bce57df">3</span></p></td><td class="cl-2bcecd4b"><p class="cl-2bce7ee4"><span class="cl-2bce57df">7</span></p></td><td class="cl-2bcecd4b"><p class="cl-2bce7ee4"><span class="cl-2bce57df">9</span></p></td><td class="cl-2bcecd4b"><p class="cl-2bce7ee4"><span class="cl-2bce57df">49</span></p></td><td class="cl-2bcecd4b"><p class="cl-2bce7ee4"><span class="cl-2bce57df">21</span></p></td></tr><tr style="overflow-wrap:break-word;"><td class="cl-2bcecd4b"><p class="cl-2bce7ee4"><span class="cl-2bce57df">4</span></p></td><td class="cl-2bcecd4b"><p class="cl-2bce7ee4"><span class="cl-2bce57df">9</span></p></td><td class="cl-2bcecd4b"><p class="cl-2bce7ee4"><span class="cl-2bce57df">16</span></p></td><td class="cl-2bcecd4b"><p class="cl-2bce7ee4"><span class="cl-2bce57df">81</span></p></td><td class="cl-2bcecd4b"><p class="cl-2bce7ee4"><span class="cl-2bce57df">36</span></p></td></tr><tr style="overflow-wrap:break-word;"><td class="cl-2bcecd4b"><p class="cl-2bce7ee4"><span class="cl-2bce57df">5</span></p></td><td class="cl-2bcecd4b"><p class="cl-2bce7ee4"><span class="cl-2bce57df">10</span></p></td><td class="cl-2bcecd4b"><p class="cl-2bce7ee4"><span class="cl-2bce57df">25</span></p></td><td class="cl-2bcecd4b"><p class="cl-2bce7ee4"><span class="cl-2bce57df">100</span></p></td><td class="cl-2bcecd4b"><p class="cl-2bce7ee4"><span class="cl-2bce57df">50</span></p></td></tr><tr style="overflow-wrap:break-word;"><td class="cl-2bcecd4b"><p class="cl-2bce7ee4"><span class="cl-2bce57df">5</span></p></td><td class="cl-2bcecd4b"><p class="cl-2bce7ee4"><span class="cl-2bce57df">11</span></p></td><td class="cl-2bcecd4b"><p class="cl-2bce7ee4"><span class="cl-2bce57df">25</span></p></td><td class="cl-2bcecd4b"><p class="cl-2bce7ee4"><span class="cl-2bce57df">121</span></p></td><td class="cl-2bcecd4b"><p class="cl-2bce7ee4"><span class="cl-2bce57df">55</span></p></td></tr><tr style="overflow-wrap:break-word;"><td class="cl-2bcecd4b"><p class="cl-2bce7ee4"><span class="cl-2bce57df">6</span></p></td><td class="cl-2bcecd4b"><p class="cl-2bce7ee4"><span class="cl-2bce57df">11</span></p></td><td class="cl-2bcecd4b"><p class="cl-2bce7ee4"><span class="cl-2bce57df">36</span></p></td><td class="cl-2bcecd4b"><p class="cl-2bce7ee4"><span class="cl-2bce57df">121</span></p></td><td class="cl-2bcecd4b"><p class="cl-2bce7ee4"><span class="cl-2bce57df">66</span></p></td></tr><tr style="overflow-wrap:break-word;"><td class="cl-2bcecd4b"><p class="cl-2bce7ee4"><span class="cl-2bce57df">7</span></p></td><td class="cl-2bcecd4b"><p class="cl-2bce7ee4"><span class="cl-2bce57df">13</span></p></td><td class="cl-2bcecd4b"><p class="cl-2bce7ee4"><span class="cl-2bce57df">49</span></p></td><td class="cl-2bcecd4b"><p class="cl-2bce7ee4"><span class="cl-2bce57df">169</span></p></td><td class="cl-2bcecd4b"><p class="cl-2bce7ee4"><span class="cl-2bce57df">91</span></p></td></tr><tr style="overflow-wrap:break-word;"><td class="cl-2bcecd4b"><p class="cl-2bce7ee4"><span class="cl-2bce57df">8</span></p></td><td class="cl-2bcecd4b"><p class="cl-2bce7ee4"><span class="cl-2bce57df">15</span></p></td><td class="cl-2bcecd4b"><p class="cl-2bce7ee4"><span class="cl-2bce57df">64</span></p></td><td class="cl-2bcecd4b"><p class="cl-2bce7ee4"><span class="cl-2bce57df">225</span></p></td><td class="cl-2bcecd4b"><p class="cl-2bce7ee4"><span class="cl-2bce57df">120</span></p></td></tr></tbody></table></div></template> <div class="flextable-shadow-host" id="55e7d91d-0ff5-4c4d-9039-8e18b0ac2674"></div> <script> var dest = document.getElementById("55e7d91d-0ff5-4c4d-9039-8e18b0ac2674"); var template = document.getElementById("3477b540-3d08-408a-b27c-2d1a084dda41"); var caption = template.content.querySelector("caption"); if(caption) { caption.style.cssText = "display:block;text-align:center;"; var newcapt = document.createElement("p"); newcapt.appendChild(caption) dest.parentNode.insertBefore(newcapt, dest.previousSibling); } var fantome = dest.attachShadow({mode: 'open'}); var templateContent = template.content; fantome.appendChild(templateContent); </script> --- class: center, middle .huge-text[Passo 2] .large[Encontrar o somatĆ³rio] --- <template id="b43c6d7d-40ee-457e-8437-1f46041b7633"><style> .tabwid table{ border-collapse:collapse; line-height:1; margin-left:auto; margin-right:auto; border-width: 0; display: table; margin-top: 1.275em; margin-bottom: 1.275em; border-spacing: 0; border-color: transparent; } .tabwid_left table{ margin-left:0; } .tabwid_right table{ margin-right:0; } .tabwid td { padding: 0; } .tabwid a { text-decoration: none; } .tabwid thead { background-color: transparent; } .tabwid tfoot { background-color: transparent; } .tabwid table tr { background-color: transparent; } </style><div class="tabwid"><style>.cl-33d3c97c{border-collapse:collapse;}.cl-33c2b506{font-family:'Arial';font-size:22pt;font-weight:bold;font-style:normal;text-decoration:none;color:rgba(0, 0, 0, 1.00);background-color:transparent;}.cl-33c2b507{font-family:'Arial';font-size:22pt;font-weight:normal;font-style:normal;text-decoration:none;color:rgba(0, 0, 0, 1.00);background-color:transparent;}.cl-33c2dc02{margin:0;text-align:center;border-bottom: 0 solid rgba(0, 0, 0, 1.00);border-top: 0 solid rgba(0, 0, 0, 1.00);border-left: 0 solid rgba(0, 0, 0, 1.00);border-right: 0 solid rgba(0, 0, 0, 1.00);padding-bottom:5pt;padding-top:5pt;padding-left:5pt;padding-right:5pt;line-height: 1;background-color:transparent;}.cl-33c3513c{width:54pt;background-color:transparent;vertical-align: middle;border-bottom: 1pt solid rgba(102, 102, 102, 1.00);border-top: 0 solid rgba(0, 0, 0, 1.00);border-left: 1pt solid rgba(102, 102, 102, 1.00);border-right: 1pt solid rgba(102, 102, 102, 1.00);margin-bottom:0;margin-top:0;margin-left:0;margin-right:0;}.cl-33c3513d{width:54pt;background-color:transparent;vertical-align: middle;border-bottom: 1pt solid rgba(102, 102, 102, 1.00);border-top: 1pt solid rgba(102, 102, 102, 1.00);border-left: 1pt solid rgba(102, 102, 102, 1.00);border-right: 1pt solid rgba(102, 102, 102, 1.00);margin-bottom:0;margin-top:0;margin-left:0;margin-right:0;}</style><table class='cl-33d3c97c'><thead><tr style="overflow-wrap:break-word;"><td class="cl-33c3513d"><p class="cl-33c2dc02"><span class="cl-33c2b506">x</span></p></td><td class="cl-33c3513d"><p class="cl-33c2dc02"><span class="cl-33c2b506">y</span></p></td><td class="cl-33c3513d"><p class="cl-33c2dc02"><span class="cl-33c2b506">x ao quadrado</span></p></td><td class="cl-33c3513d"><p class="cl-33c2dc02"><span class="cl-33c2b506">y ao quadrado</span></p></td><td class="cl-33c3513d"><p class="cl-33c2dc02"><span class="cl-33c2b506">x*y</span></p></td></tr></thead><tbody><tr style="overflow-wrap:break-word;"><td class="cl-33c3513c"><p class="cl-33c2dc02"><span class="cl-33c2b507">2</span></p></td><td class="cl-33c3513c"><p class="cl-33c2dc02"><span class="cl-33c2b507">4</span></p></td><td class="cl-33c3513c"><p class="cl-33c2dc02"><span class="cl-33c2b507">4</span></p></td><td class="cl-33c3513c"><p class="cl-33c2dc02"><span class="cl-33c2b507">16</span></p></td><td class="cl-33c3513c"><p class="cl-33c2dc02"><span class="cl-33c2b507">8</span></p></td></tr><tr style="overflow-wrap:break-word;"><td class="cl-33c3513d"><p class="cl-33c2dc02"><span class="cl-33c2b507">3</span></p></td><td class="cl-33c3513d"><p class="cl-33c2dc02"><span class="cl-33c2b507">7</span></p></td><td class="cl-33c3513d"><p class="cl-33c2dc02"><span class="cl-33c2b507">9</span></p></td><td class="cl-33c3513d"><p class="cl-33c2dc02"><span class="cl-33c2b507">49</span></p></td><td class="cl-33c3513d"><p class="cl-33c2dc02"><span class="cl-33c2b507">21</span></p></td></tr><tr style="overflow-wrap:break-word;"><td class="cl-33c3513d"><p class="cl-33c2dc02"><span class="cl-33c2b507">4</span></p></td><td class="cl-33c3513d"><p class="cl-33c2dc02"><span class="cl-33c2b507">9</span></p></td><td class="cl-33c3513d"><p class="cl-33c2dc02"><span class="cl-33c2b507">16</span></p></td><td class="cl-33c3513d"><p class="cl-33c2dc02"><span class="cl-33c2b507">81</span></p></td><td class="cl-33c3513d"><p class="cl-33c2dc02"><span class="cl-33c2b507">36</span></p></td></tr><tr style="overflow-wrap:break-word;"><td class="cl-33c3513d"><p class="cl-33c2dc02"><span class="cl-33c2b507">5</span></p></td><td class="cl-33c3513d"><p class="cl-33c2dc02"><span class="cl-33c2b507">10</span></p></td><td class="cl-33c3513d"><p class="cl-33c2dc02"><span class="cl-33c2b507">25</span></p></td><td class="cl-33c3513d"><p class="cl-33c2dc02"><span class="cl-33c2b507">100</span></p></td><td class="cl-33c3513d"><p class="cl-33c2dc02"><span class="cl-33c2b507">50</span></p></td></tr><tr style="overflow-wrap:break-word;"><td class="cl-33c3513d"><p class="cl-33c2dc02"><span class="cl-33c2b507">5</span></p></td><td class="cl-33c3513d"><p class="cl-33c2dc02"><span class="cl-33c2b507">11</span></p></td><td class="cl-33c3513d"><p class="cl-33c2dc02"><span class="cl-33c2b507">25</span></p></td><td class="cl-33c3513d"><p class="cl-33c2dc02"><span class="cl-33c2b507">121</span></p></td><td class="cl-33c3513d"><p class="cl-33c2dc02"><span class="cl-33c2b507">55</span></p></td></tr><tr style="overflow-wrap:break-word;"><td class="cl-33c3513d"><p class="cl-33c2dc02"><span class="cl-33c2b507">6</span></p></td><td class="cl-33c3513d"><p class="cl-33c2dc02"><span class="cl-33c2b507">11</span></p></td><td class="cl-33c3513d"><p class="cl-33c2dc02"><span class="cl-33c2b507">36</span></p></td><td class="cl-33c3513d"><p class="cl-33c2dc02"><span class="cl-33c2b507">121</span></p></td><td class="cl-33c3513d"><p class="cl-33c2dc02"><span class="cl-33c2b507">66</span></p></td></tr><tr style="overflow-wrap:break-word;"><td class="cl-33c3513d"><p class="cl-33c2dc02"><span class="cl-33c2b507">7</span></p></td><td class="cl-33c3513d"><p class="cl-33c2dc02"><span class="cl-33c2b507">13</span></p></td><td class="cl-33c3513d"><p class="cl-33c2dc02"><span class="cl-33c2b507">49</span></p></td><td class="cl-33c3513d"><p class="cl-33c2dc02"><span class="cl-33c2b507">169</span></p></td><td class="cl-33c3513d"><p class="cl-33c2dc02"><span class="cl-33c2b507">91</span></p></td></tr><tr style="overflow-wrap:break-word;"><td class="cl-33c3513d"><p class="cl-33c2dc02"><span class="cl-33c2b507">8</span></p></td><td class="cl-33c3513d"><p class="cl-33c2dc02"><span class="cl-33c2b507">15</span></p></td><td class="cl-33c3513d"><p class="cl-33c2dc02"><span class="cl-33c2b507">64</span></p></td><td class="cl-33c3513d"><p class="cl-33c2dc02"><span class="cl-33c2b507">225</span></p></td><td class="cl-33c3513d"><p class="cl-33c2dc02"><span class="cl-33c2b507">120</span></p></td></tr></tbody></table></div></template> <div class="flextable-shadow-host" id="184c995d-02ff-491f-86bc-057f94c42d1e"></div> <script> var dest = document.getElementById("184c995d-02ff-491f-86bc-057f94c42d1e"); var template = document.getElementById("b43c6d7d-40ee-457e-8437-1f46041b7633"); var caption = template.content.querySelector("caption"); if(caption) { caption.style.cssText = "display:block;text-align:center;"; var newcapt = document.createElement("p"); newcapt.appendChild(caption) dest.parentNode.insertBefore(newcapt, dest.previousSibling); } var fantome = dest.attachShadow({mode: 'open'}); var templateContent = template.content; fantome.appendChild(templateContent); </script> --- <template id="ac2c0302-9288-494f-bf5d-dfc978380365"><style> .tabwid table{ border-collapse:collapse; line-height:1; margin-left:auto; margin-right:auto; border-width: 0; display: table; margin-top: 1.275em; margin-bottom: 1.275em; border-spacing: 0; border-color: transparent; } .tabwid_left table{ margin-left:0; } .tabwid_right table{ margin-right:0; } .tabwid td { padding: 0; } .tabwid a { text-decoration: none; } .tabwid thead { background-color: transparent; } .tabwid tfoot { background-color: transparent; } .tabwid table tr { background-color: transparent; } </style><div class="tabwid"><style>.cl-196f4ac0{border-collapse:collapse;}.cl-1962a1e4{font-family:'Arial';font-size:22pt;font-weight:bold;font-style:normal;text-decoration:none;color:rgba(0, 0, 0, 1.00);background-color:transparent;}.cl-1962a1e5{font-family:'Arial';font-size:22pt;font-weight:normal;font-style:normal;text-decoration:none;color:rgba(0, 0, 0, 1.00);background-color:transparent;}.cl-1962c8d6{margin:0;text-align:center;border-bottom: 0 solid rgba(0, 0, 0, 1.00);border-top: 0 solid rgba(0, 0, 0, 1.00);border-left: 0 solid rgba(0, 0, 0, 1.00);border-right: 0 solid rgba(0, 0, 0, 1.00);padding-bottom:5pt;padding-top:5pt;padding-left:5pt;padding-right:5pt;line-height: 1;background-color:transparent;}.cl-196316e2{width:54pt;background-color:transparent;vertical-align: middle;border-bottom: 1pt solid rgba(102, 102, 102, 1.00);border-top: 0 solid rgba(0, 0, 0, 1.00);border-left: 1pt solid rgba(102, 102, 102, 1.00);border-right: 1pt solid rgba(102, 102, 102, 1.00);margin-bottom:0;margin-top:0;margin-left:0;margin-right:0;}.cl-196316e3{width:54pt;background-color:transparent;vertical-align: middle;border-bottom: 1pt solid rgba(102, 102, 102, 1.00);border-top: 1pt solid rgba(102, 102, 102, 1.00);border-left: 1pt solid rgba(102, 102, 102, 1.00);border-right: 1pt solid rgba(102, 102, 102, 1.00);margin-bottom:0;margin-top:0;margin-left:0;margin-right:0;}</style><table class='cl-196f4ac0'><thead><tr style="overflow-wrap:break-word;"><td class="cl-196316e3"><p class="cl-1962c8d6"><span class="cl-1962a1e4">soma x</span></p></td><td class="cl-196316e3"><p class="cl-1962c8d6"><span class="cl-1962a1e4">soma y</span></p></td><td class="cl-196316e3"><p class="cl-1962c8d6"><span class="cl-1962a1e4">soma x ao quadrado</span></p></td><td class="cl-196316e3"><p class="cl-1962c8d6"><span class="cl-1962a1e4">soma y ao quadrado</span></p></td><td class="cl-196316e3"><p class="cl-1962c8d6"><span class="cl-1962a1e4">soma x*y</span></p></td></tr></thead><tbody><tr style="overflow-wrap:break-word;"><td class="cl-196316e2"><p class="cl-1962c8d6"><span class="cl-1962a1e5">40</span></p></td><td class="cl-196316e2"><p class="cl-1962c8d6"><span class="cl-1962a1e5">80</span></p></td><td class="cl-196316e2"><p class="cl-1962c8d6"><span class="cl-1962a1e5">228</span></p></td><td class="cl-196316e2"><p class="cl-1962c8d6"><span class="cl-1962a1e5">882</span></p></td><td class="cl-196316e2"><p class="cl-1962c8d6"><span class="cl-1962a1e5">447</span></p></td></tr></tbody></table></div></template> <div class="flextable-shadow-host" id="2a1ba988-03ff-4092-b718-1fe1fbd5dfbf"></div> <script> var dest = document.getElementById("2a1ba988-03ff-4092-b718-1fe1fbd5dfbf"); var template = document.getElementById("ac2c0302-9288-494f-bf5d-dfc978380365"); var caption = template.content.querySelector("caption"); if(caption) { caption.style.cssText = "display:block;text-align:center;"; var newcapt = document.createElement("p"); newcapt.appendChild(caption) dest.parentNode.insertBefore(newcapt, dest.previousSibling); } var fantome = dest.attachShadow({mode: 'open'}); var templateContent = template.content; fantome.appendChild(templateContent); </script> --- class: center, middle .huge-text[Passo 3] .large[Aplicar a fĆ³rmula] --- $$ \Huge{r = \frac{n\cdot\Sigma(X\cdot Y) -\Sigma(X)*\Sigma(Y)}{\sqrt{n\cdot\Sigma(X)^2-(\Sigma(X))^2}\sqrt{n\cdot\Sigma(Y)^2-(\Sigma(Y))^2}}} $$ $$ \Huge{r = \frac{8(447) -40\cdot80}{\sqrt{8\cdot228-(40)^2}\sqrt{8\cdot(882)^2-((80))^2}}} $$ $$ \Huge{r = \frac{3576-3200}{\sqrt{1824-1600}\sqrt{7056-6400)}}} $$ $$ \Huge{r = \frac{376}{383,33}= 0,981 }$$ ```r dados <-data.frame(x=c(2,3,4,5,5,6,7,8), y=c(4,7,9,10,11,11,13,15)) cor(dados$x,dados$y) ``` --- ## CorrelaĆ§Ć£o entre idade e miojo ```r cor(datacor$idade, datacor$qtdmiojo) ``` ``` ## [1] -0.8262782 ``` ```r plot(datacor$idade, datacor$qtdmiojo) abline(lsfit(datacor$idade, datacor$qtdmiojo),col="darkred") ``` ![](index_files/figure-html/Coeficientes2-1.png)<!-- --> --- # CorrelaĆ§Ć£o de Pearson CorrelaĆ§Ć£o entre as variĆ”veis Kmporlitro e HP e as variĆ”veis Kmporlitro e Peso ```r cor(CARROS$Kmporlitro,CARROS$HP) ``` ``` ## [1] -0.7761684 ``` ```r cor(CARROS$Kmporlitro,CARROS$Peso) ``` ``` ## [1] -0.8676594 ``` --- ## CorrelaĆ§Ć£o de Spearman ```r var1 = c(10, 9, 5, 6, 7) var2 = c(3, 6, 10, 5, 4) cor(var1, var2, method="spearman") ``` ``` ## [1] -0.7 ``` --- ### Na prĆ”tica, fazemos uma matriz com todas as correlaƧƵes. ```r animais = c(10, 13, 14, 11, 10, 17, 10, 7, 12, 13) frutas = c(11, 11, 14, 9, 7, 14, 9, 4, 13, 12) fas = c(3, 20, 27, 26, 16, 41, 34, 13, 31, 38) dados.fv = data.frame(animais, frutas, fas) #cor(dados.fv) pairs(dados.fv) ``` ![](index_files/figure-html/Coeficientes4-1.png)<!-- --> --- ## Na prĆ”tica, fazemos uma matriz com todas as correlaƧƵes. ```r cor(CARROS[,c("Preco","RPM","HP","Kmporlitro","Amperagem_circ_eletrico","Peso")]) ``` ``` ## Preco RPM HP Kmporlitro ## Preco 1.0000000 -0.43369788 0.7909486 -0.8475514 ## RPM -0.4336979 1.00000000 -0.7082234 0.4186840 ## HP 0.7909486 -0.70822339 1.0000000 -0.7761684 ## Kmporlitro -0.8475514 0.41868403 -0.7761684 1.0000000 ## Amperagem_circ_eletrico -0.7102139 0.09120476 -0.4487591 0.6811719 ## Peso 0.8879799 -0.17471588 0.6587479 -0.8676594 ## Amperagem_circ_eletrico Peso ## Preco -0.71021393 0.8879799 ## RPM 0.09120476 -0.1747159 ## HP -0.44875912 0.6587479 ## Kmporlitro 0.68117191 -0.8676594 ## Amperagem_circ_eletrico 1.00000000 -0.7124406 ## Peso -0.71244065 1.0000000 ``` --- # No entanto, hoje em dia podemos construir uma visualizaĆ§Ć£o de dados dessa matriz. --- ## VisualizaĆ§Ć£o da Matriz de CorrelaĆ§Ć£o ![](index_files/figure-html/Coeficientes7-1.png)<!-- --> --- ## VisualizaĆ§Ć£o da Matriz de CorrelaĆ§Ć£o ```r library(corrplot) M <- cor(CARROS[,c("Preco","RPM","HP","Kmporlitro","Peso")]) corrplot(M, method="circle") ``` ![](index_files/figure-html/Coeficientes71-1.png)<!-- --> --- ## VisualizaĆ§Ć£o da Matriz de CorrelaĆ§Ć£o ```r corrplot(M, method="square") ``` ![](index_files/figure-html/MC-1.png)<!-- --> --- ## VisualizaĆ§Ć£o da Matriz de CorrelaĆ§Ć£o ```r corrplot(M, method="number") ``` ![](index_files/figure-html/MC2-1.png)<!-- --> --- ## VisualizaĆ§Ć£o da Matriz de CorrelaĆ§Ć£o ```r corrplot(M, method="color") ``` ![](index_files/figure-html/MC3-1.png)<!-- --> --- ## VisualizaĆ§Ć£o da Matriz de CorrelaĆ§Ć£o ```r corrplot(M, method="pie") ``` ![](index_files/figure-html/MC4-1.png)<!-- --> --- ### VisualizaĆ§Ć£o da Matriz de CorrelaĆ§Ć£o:CriaĆ§Ć£o de Grupos ```r corrplot(M, order="hclust", addrect=2) ``` ![](index_files/figure-html/MC5-1.png)<!-- --> --- ### VisualizaĆ§Ć£o da Matriz de CorrelaĆ§Ć£o: Democratas e Republicanos ```r col3 <- colorRampPalette(c("red", "white", "blue")) corrplot(M, order="hclust", addrect=2, col=col3(20)) ``` ![](index_files/figure-html/MC6-1.png)<!-- --> --- ## Matriz de correlaĆ§Ć£o PĆ³s-Moderna ```r wb <- c("white","black") corrplot(M, order="hclust", addrect=2, col=wb, bg="gold2") ``` ![](index_files/figure-html/MC7-1.png)<!-- --> --- ## Matriz de correlaĆ§Ć£o AnalĆtica (VersĆ£o 1) ```r corrplot.mixed(M) ``` ![](index_files/figure-html/MC8-1.png)<!-- --> --- ## Matriz de correlaĆ§Ć£o AnalĆtica (VersĆ£o 2) ```r corrplot(M,addCoef.col=TRUE,number.cex=0.7) ``` ![](index_files/figure-html/MC9-1.png)<!-- --> --- class: middle, inverse .pull-left[ .huge-text[check point] ] .pull-right[ .larger[atĆ© aqui, tudo ok?] ] --- class: center, bottom # RegressĆ£o linear --- ## RegressĆ£o linear: objetivos * Predizer observaƧƵes futuras * Avaliar o efeito as relaƧƵes da variĆ”vel independente (x) sobre uma variĆ”vel dependente (y) * Descrever a estrutura dos dados --- ## Modelo de RegressĆ£o Linear `\(\Huge{Y = \beta_0 + \beta_1 x + \epsilon}\)` Onde: Y = Ć© o valor a ser predito `\(\Huge{\beta_0}\)` = Ć© o intercepto (valor quando x = 0) `\(\Huge{\beta_1}\)` = Ć© a inclinaĆ§Ć£o da reta de regressĆ£o x = Ć© o valor da variĆ”vel preditora (preditor linear) `\(\Huge{\epsilon}\)` Ć© o erro --- ## Modelo de regressĆ£o: exemplo 1 ```r y = c(110, 120, 90, 70, 50, 80, 40, 40, 50, 30) xx = 1:10 modelo = lm(y ~ xx) modelo ``` ``` ## ## Call: ## lm(formula = y ~ xx) ## ## Coefficients: ## (Intercept) xx ## 118.667 -9.212 ``` --- ## Modelo de regressĆ£o: exemplo 1 ```r plot(y ~ xx) abline(modelo, col=2, lty=2, lwd=2) legend("top", legend=c("valores observados", "valores ajustados"), lty=c(NA,2), col=c(1,2), lwd=1:2, bty="n", pch=c(1,NA)) ``` ![](index_files/figure-html/Reg0-1.png)<!-- --> --- ## Modelo de regressĆ£o: exemplo 2 ```r renda = c(1750, 1680, 1700, 1710, 1690, 1650, 1650, 1600, 1800, 1860) anosdeestudo = c(8, 7, 6, 6, 6, 5, 5, 5, 8, 9) modelo2 = lm(renda ~ anosdeestudo) ``` --- ```r plot(renda ~ anosdeestudo) abline(modelo2, col=2, lty=2, lwd=2) legend("topleft", legend=c("valores observados", "valores ajustados"), lty=c(NA,2), col=c(1,2), lwd=1:2, bty="n", pch=c(1,NA)) ``` ![](index_files/figure-html/Reg21-1.png)<!-- --> ```r modelo2 ``` ``` ## ## Call: ## lm(formula = renda ~ anosdeestudo) ## ## Coefficients: ## (Intercept) anosdeestudo ## 1387.51 49.46 ``` --- ## Modelo de regressĆ£o: exemplo 3 ```r data("mtcars") modelo3 = lm(mpg ~ wt, data=mtcars) modelo3 ``` ``` ## ## Call: ## lm(formula = mpg ~ wt, data = mtcars) ## ## Coefficients: ## (Intercept) wt ## 37.285 -5.344 ``` --- ```r plot(mtcars$mpg ~ mtcars$wt) abline(modelo3, col=2, lty=2, lwd=2) legend("topright", legend=c("valores observados", "valores ajustados"), lty=c(NA,2), col=c(1,2), lwd=1:2, bty="n", pch=c(1,NA)) ``` ![](index_files/figure-html/Reg31-1.png)<!-- --> --- ## Modelo de regressĆ£o: exemplo 4 ```r modelo4 = lm(mpg ~ wt+cyl+disp+hp, data=mtcars) modelo4 ``` ``` ## ## Call: ## lm(formula = mpg ~ wt + cyl + disp + hp, data = mtcars) ## ## Coefficients: ## (Intercept) wt cyl disp hp ## 40.82854 -3.85390 -1.29332 0.01160 -0.02054 ``` --- ```r par(mfrow = c(2, 2)) plot(mtcars$mpg ~ mtcars$wt) plot(mtcars$mpg ~ mtcars$cyl) plot(mtcars$mpg ~ mtcars$disp) plot(mtcars$mpg ~ mtcars$hp) ``` ![](index_files/figure-html/Reg5-1.png)<!-- --> --- class: middle, inverse .pull-left[ .huge-text[check point] ] .pull-right[ .larger[ atĆ© aqui, tudo ok? ] ]