class: center, middle, inverse, title-slide # Estatística básica e noções de inferência ### Renata Hirota | Volt Data Lab ### 26 de abril de 2019 --- class: middle, center # Sobre o Volt Data Lab <img src="https://static1.squarespace.com/static/551da417e4b05f67005d6abe/t/58f8173a1e5b6ca0cc4339f0/1541080220923/?format=100w" style=" display: block; margin-left: auto; margin-right: auto; position: absolute; top: 16px; right: 16px; opacity: 0.5"></img> --- ## Jornalismo + Dados <br> <img src="imgs/volt.png" style=" display: block; margin-left: auto; margin-right: auto;"></img> <img src="https://static1.squarespace.com/static/551da417e4b05f67005d6abe/t/58f8173a1e5b6ca0cc4339f0/1541080220923/?format=100w" style=" display: block; margin-left: auto; margin-right: auto; position: absolute; top: 16px; right: 16px; opacity: 0.5"></img> --- class: middle, center # Vamos à *estatística* <img src="imgs/yay.gif" style=" display: block; margin-left: auto; margin-right: auto;"></img> <img src="https://static1.squarespace.com/static/551da417e4b05f67005d6abe/t/58f8173a1e5b6ca0cc4339f0/1541080220923/?format=100w" style=" display: block; margin-left: auto; margin-right: auto; position: absolute; top: 16px; right: 16px; opacity: 0.5"></img> --- ## O que é **estatística**? <img src="https://static1.squarespace.com/static/551da417e4b05f67005d6abe/t/58f8173a1e5b6ca0cc4339f0/1541080220923/?format=100w" style=" display: block; margin-left: auto; margin-right: auto; position: absolute; top: 16px; right: 16px; opacity: 0.5"></img> -- <br> Organizar, descrever, analisar e interpretar dados -- <br><br> - Estatística descritiva (descrever e resumir dados) - Probabilidade (teoria matemática para estudar a incerteza) - Inferência estatística (extrapolação de informações e conclusões a partir dos dados) --- ## Falando em inferência... <img src="https://static1.squarespace.com/static/551da417e4b05f67005d6abe/t/58f8173a1e5b6ca0cc4339f0/1541080220923/?format=100w" style=" display: block; margin-left: auto; margin-right: auto; position: absolute; top: 16px; right: 16px; opacity: 0.5"></img> -- <br> Dois conceitos: **amostra** e **população** -- <br><br> ### População Conjunto de valores de uma característica observável associado a um grupo de indivíduos.<br> Exemplo: idade dos brasileiros -- <br><br> ### Amostra Qualquer subconjunto de uma população.<br> Exemplo: idade das R-Ladies --- ## Mas e daí? <img src="https://static1.squarespace.com/static/551da417e4b05f67005d6abe/t/58f8173a1e5b6ca0cc4339f0/1541080220923/?format=100w" style=" display: block; margin-left: auto; margin-right: auto; position: absolute; top: 16px; right: 16px; opacity: 0.5"></img> -- <br> Nem sempre é possível (ou desejável) obter informações diretamente da **população**. Utilizamos **amostras** para fazer suposições sobre a população e estudar suas características. -- <br><br> A **inferência** tem como objetivo estudar generalizações sobre uma população através de evidências fornecidas por uma amostra dessa população, inclusive medindo quão *incerta* é a generalização. <img src="imgs/inferencia.png" height=280 style=" display: block; margin-left: auto; margin-right: auto;"></img> --- ## Suponha que você está fazendo uma sopa... <img src="https://static1.squarespace.com/static/551da417e4b05f67005d6abe/t/58f8173a1e5b6ca0cc4339f0/1541080220923/?format=100w" style=" display: block; margin-left: auto; margin-right: auto; position: absolute; top: 16px; right: 16px; opacity: 0.5"></img> <img src="imgs/sopa.jpg" style=" display: block; margin-left: auto; margin-right: auto;"></img> --- <br><br> ### Como você sabe se a sopa precisa de mais sal? <img src="https://static1.squarespace.com/static/551da417e4b05f67005d6abe/t/58f8173a1e5b6ca0cc4339f0/1541080220923/?format=100w" style=" display: block; margin-left: auto; margin-right: auto; position: absolute; top: 16px; right: 16px; opacity: 0.5"></img> -- <br> Tomar a sopa inteira?<br> -- Experimentar uma colher? -- <br> ### Depois de adicionar o sal, o que garante que a sopa como um todo estará mais salgada? -- <br> Misturamos a sopa para que ela fique *homogênea* e, assim, qualquer *amostra* que retirarmos dela para experimentar seja representativa do todo. --- ## Resumindo: <img src="https://static1.squarespace.com/static/551da417e4b05f67005d6abe/t/58f8173a1e5b6ca0cc4339f0/1541080220923/?format=100w" style=" display: block; margin-left: auto; margin-right: auto; position: absolute; top: 16px; right: 16px; opacity: 0.5"></img> População a ser estudada <br><br> `\(\downarrow\)` <br><br> Retira-se amostra(s) <br><br> `\(\downarrow\)` <br><br> Análise do conjunto de dados e conclusões!<br><br> `\(\downarrow\)` <br><br> Extrapolação de informações e conclusões sobre a amostra à população, *com um grau de incerteza* --- ## Voltando ao exemplo da __idade__ -- <br> Qual a _idade média_ dos brasileiros? Qual a _idade média_ das RLadies? -- Se quiséssemos _inferir_ a idade de todos os brasileiros a partir de uma amostra (RLadies), quão representativa essa amostra seria? --- ## Quando o tamanho da amostra cresce... <img src="index_files/figure-html/unnamed-chunk-1-1.png" style="display: block; margin: auto;" /> --- class: middle, center # Voltando para a estatística descritiva: alguns conceitos importantes <img src="https://static1.squarespace.com/static/551da417e4b05f67005d6abe/t/58f8173a1e5b6ca0cc4339f0/1541080220923/?format=100w" style=" display: block; margin-left: auto; margin-right: auto; position: absolute; top: 16px; right: 16px; opacity: 0.5"></img> --- ## Medidas resumo <img src="https://static1.squarespace.com/static/551da417e4b05f67005d6abe/t/58f8173a1e5b6ca0cc4339f0/1541080220923/?format=100w" style=" display: block; margin-left: auto; margin-right: auto; position: absolute; top: 16px; right: 16px; opacity: 0.5"></img> -- <br> ### Média <img src="https://static1.squarespace.com/static/551da417e4b05f67005d6abe/t/58f8173a1e5b6ca0cc4339f0/1541080220923/?format=100w" style=" display: block; margin-left: auto; margin-right: auto; position: absolute; top: 16px; right: 16px; opacity: 0.5"></img> -- <br> `\(\bar X = \frac{(X_1 + X_2 + X_3 + ... + X_N)}{N}\)` -- <br><br> Qual é a média do conjunto de dados a seguir? $$ \matrix{5 & 5 & 5 & 5 & 5 & 5} $$ -- E desse? $$ \matrix{2 & 8 & 5 & -1 & 6 & 10} $$ --- ### Variância -- <br> `\(Var(X) = \frac{(X_1 - \mu)^2 + ( X_2 - \mu)^2 + ... + (X_n - \mu)^2}{N}\)` -- <br><br> Qual a variância dos dados anteriores? $$ \matrix{5 & 5 & 5 & 5 & 5 & 5} $$ <br> $$ \matrix{2 & 8 & 5 & -1 & 6 & 10} $$ -- <br> ### Correlação (Pearson) -- <br> `\(\rho_{X,Y} = \frac {cov(X,Y)}{\sqrt {var(X) \: var(Y)}} = \frac {\sum_{i=1}^n (x_i-\bar x)(y_i - \bar y)}{\sqrt {var(X) \: var(Y)}}\)` --- ## Mas, na prática... <img src="https://static1.squarespace.com/static/551da417e4b05f67005d6abe/t/58f8173a1e5b6ca0cc4339f0/1541080220923/?format=100w" style=" display: block; margin-left: auto; margin-right: auto; position: absolute; top: 16px; right: 16px; opacity: 0.5"></img> `> mean(x)`<br> `> var(x)`<br> `> cor(x,y)` <img src="imgs/correlsheets.png" style=" display: block; margin-left: auto; margin-right: auto;"></img> --- ## Resumindo: <img src="https://static1.squarespace.com/static/551da417e4b05f67005d6abe/t/58f8173a1e5b6ca0cc4339f0/1541080220923/?format=100w" style=" display: block; margin-left: auto; margin-right: auto; position: absolute; top: 16px; right: 16px; opacity: 0.5"></img> -- <br> <img src="imgs/math.gif" style=" display: block; margin-left: auto; margin-right: auto;"></img> <br><br> Qualquer calculadora faz contas. <br>O importante é entender o que está acontecendo! --- class: middle, center # Regressão linear simples <img src="https://static1.squarespace.com/static/551da417e4b05f67005d6abe/t/58f8173a1e5b6ca0cc4339f0/1541080220923/?format=100w" style=" display: block; margin-left: auto; margin-right: auto; position: absolute; top: 16px; right: 16px; opacity: 0.5"></img> --- ## O que é regressão linear? <img src="https://static1.squarespace.com/static/551da417e4b05f67005d6abe/t/58f8173a1e5b6ca0cc4339f0/1541080220923/?format=100w" style=" display: block; margin-left: auto; margin-right: auto; position: absolute; top: 16px; right: 16px; opacity: 0.5"></img> -- Técnica para __quantificar__ a mudança observada em uma variável quando variamos o valor de outra.<br> -- <img src="index_files/figure-html/unnamed-chunk-2-1.png" style="display: block; margin: auto;" /> --- ## Motivação <img src="https://static1.squarespace.com/static/551da417e4b05f67005d6abe/t/58f8173a1e5b6ca0cc4339f0/1541080220923/?format=100w" style=" display: block; margin-left: auto; margin-right: auto; position: absolute; top: 16px; right: 16px; opacity: 0.5"></img> Se sabemos que um certo município ou estado tem `TMI` igual a 20, quanto espera-se que seja a `Expectativa de vida` nessa região? -- ## O que queremos Prever valores de uma variável (resposta) em função de outra (explicativa) -- ## Ideia Ajustar uma equação de reta de forma a minimizar a soma das distâncias de cada ponto à reta --- ## Peraí, equação de reta? <img src="https://static1.squarespace.com/static/551da417e4b05f67005d6abe/t/58f8173a1e5b6ca0cc4339f0/1541080220923/?format=100w" style=" display: block; margin-left: auto; margin-right: auto; position: absolute; top: 16px; right: 16px; opacity: 0.5"></img> -- Y = **a**X + **b** em que - **a** é o *coeficiente angular* - **b** é o *coeficiente linear* ou *intercepto* -- <img src="imgs/reta1.png" style=" display: block; margin-left: auto; margin-right: auto;"></img> --- ## Peraí, equação de reta? <img src="https://static1.squarespace.com/static/551da417e4b05f67005d6abe/t/58f8173a1e5b6ca0cc4339f0/1541080220923/?format=100w" style=" display: block; margin-left: auto; margin-right: auto; position: absolute; top: 16px; right: 16px; opacity: 0.5"></img> Y = **a**X + **b** em que - **a** é o *coeficiente angular* - **b** é o *coeficiente linear* ou *intercepto* <img src="imgs/reta2.png" style=" display: block; margin-left: auto; margin-right: auto;"></img> --- ## Voltando à regressão <img src="https://static1.squarespace.com/static/551da417e4b05f67005d6abe/t/58f8173a1e5b6ca0cc4339f0/1541080220923/?format=100w" style=" display: block; margin-left: auto; margin-right: auto; position: absolute; top: 16px; right: 16px; opacity: 0.5"></img> `$$Y = a\,X + b + erro$$` O método de regressão linear estima os coeficientes `\(a\)` e `\(b\)`, a partir dos pares `\((X, Y)\)` observados, em que * __X__ é a variável explicativa * __Y__ é a variável resposta * __erro__ é a diferença entre o valor esperado e o valor observado, para cada par (X, Y) No nosso caso, queremos encontrar o valor da `Expectativa` (Y) em função de `TMI` (X) <br> ```r modelo <- lm(Total ~ TMI, dados) modelo$coefficients ``` ``` ## (Intercept) TMI ## 82.4826222 -0.5323972 ``` --- ## Mas por que __essa__ reta? <img src="https://static1.squarespace.com/static/551da417e4b05f67005d6abe/t/58f8173a1e5b6ca0cc4339f0/1541080220923/?format=100w" style=" display: block; margin-left: auto; margin-right: auto; position: absolute; top: 16px; right: 16px; opacity: 0.5"></img> <img src="index_files/figure-html/unnamed-chunk-4-1.png" style="display: block; margin: auto;" /> --- ## Não poderia ser a reta azul? <img src="https://static1.squarespace.com/static/551da417e4b05f67005d6abe/t/58f8173a1e5b6ca0cc4339f0/1541080220923/?format=100w" style=" display: block; margin-left: auto; margin-right: auto; position: absolute; top: 16px; right: 16px; opacity: 0.5"></img> <img src="index_files/figure-html/unnamed-chunk-5-1.png" style="display: block; margin: auto;" /> --- ## Ou a verde? <img src="https://static1.squarespace.com/static/551da417e4b05f67005d6abe/t/58f8173a1e5b6ca0cc4339f0/1541080220923/?format=100w" style=" display: block; margin-left: auto; margin-right: auto; position: absolute; top: 16px; right: 16px; opacity: 0.5"></img> <img src="index_files/figure-html/unnamed-chunk-6-1.png" style="display: block; margin: auto;" /> --- ## Minimizar a soma das distâncias de cada ponto <img src="https://static1.squarespace.com/static/551da417e4b05f67005d6abe/t/58f8173a1e5b6ca0cc4339f0/1541080220923/?format=100w" style=" display: block; margin-left: auto; margin-right: auto; position: absolute; top: 16px; right: 16px; opacity: 0.5"></img> A reta da regressão (vermelha) é a melhor reta entre todas, porque a soma da _distância_ de cada ponto em relação a ela é a __menor possível__. É o chamado _método de mínimos quadrados_. <br><br> <img src="imgs/minimosquadrados.png" height=340 style=" display: block; margin-left: auto; margin-right: auto;"></img> --- ## "Beleza, já entendi o que é regressão! Mãos à obra" <img src="https://static1.squarespace.com/static/551da417e4b05f67005d6abe/t/58f8173a1e5b6ca0cc4339f0/1541080220923/?format=100w" style=" display: block; margin-left: auto; margin-right: auto; position: absolute; top: 16px; right: 16px; opacity: 0.5"></img> -- <img src="imgs/wait.gif" style=" display: block; margin-left: auto; margin-right: auto;"></img> --- ## Suposições / hipóteses necessárias <img src="https://static1.squarespace.com/static/551da417e4b05f67005d6abe/t/58f8173a1e5b6ca0cc4339f0/1541080220923/?format=100w" style=" display: block; margin-left: auto; margin-right: auto; position: absolute; top: 16px; right: 16px; opacity: 0.5"></img> ### Relação *linear* entre duas variáveis numéricas Lembra do coeficiente de correlação de Pearson? Use-o. <br> ```r var_explicativa <- dados$TMI var_resposta <- dados$Total cor(var_explicativa, var_resposta, method="pearson") ``` ``` ## [1] -0.873016 ``` -- - Quanto mais próximo de __1__: mais _positivamente_ correlacionados - Quanto mais próximo de __-1__: mais _negativamente_ correlacionados - Quanto mais próximo de __0__: ausência de correlação --- ## Suposições / hipóteses necessárias <img src="https://static1.squarespace.com/static/551da417e4b05f67005d6abe/t/58f8173a1e5b6ca0cc4339f0/1541080220923/?format=100w" style=" display: block; margin-left: auto; margin-right: auto; position: absolute; top: 16px; right: 16px; opacity: 0.5"></img> ### Relação *linear* entre duas variáveis numéricas <img src="imgs/linear-nonlinear-corrrelation.jpg" height=340 style=" display: block; margin-left: auto; margin-right: auto;"></img> --- ## Suposições / hipóteses necessárias <img src="https://static1.squarespace.com/static/551da417e4b05f67005d6abe/t/58f8173a1e5b6ca0cc4339f0/1541080220923/?format=100w" style=" display: block; margin-left: auto; margin-right: auto; position: absolute; top: 16px; right: 16px; opacity: 0.5"></img> Plotar os dados também ajuda a visualizar e ter uma ideia de como eles se relacionam. -- ```r plot(x = var_explicativa, y = var_resposta) ``` <img src="index_files/figure-html/unnamed-chunk-8-1.png" style="display: block; margin: auto;" /> --- ## Suposições / hipóteses necessárias <img src="https://static1.squarespace.com/static/551da417e4b05f67005d6abe/t/58f8173a1e5b6ca0cc4339f0/1541080220923/?format=100w" style=" display: block; margin-left: auto; margin-right: auto; position: absolute; top: 16px; right: 16px; opacity: 0.5"></img> ### Distribuição normal dos erros Erros são aleatórios, com média igual a zero e variância constante ```r hist(rnorm(1000, mean=0, sd=1), prob=TRUE) curve(dnorm(x, mean=0, sd=1), add = TRUE, col="red", lwd=2) ``` <img src="index_files/figure-html/unnamed-chunk-9-1.png" style="display: block; margin: auto;" /> --- ### Distribuição normal dos erros Vamos comparar os resíduos (os erros da nossa amostra) com a distribuição normal: ```r qqnorm(modelo$residuals) qqline(modelo$residuals) ``` <img src="index_files/figure-html/unnamed-chunk-10-1.png" style="display: block; margin: auto;" /> --- <img src="https://static1.squarespace.com/static/551da417e4b05f67005d6abe/t/58f8173a1e5b6ca0cc4339f0/1541080220923/?format=100w" style=" display: block; margin-left: auto; margin-right: auto; position: absolute; top: 16px; right: 16px; opacity: 0.5"></img> <br><br><br><br><br> <img src="imgs/buguei.gif" style=" display: block; margin-left: auto; margin-right: auto;"></img> --- ## Na prática, os dados são mais ou menos assim... <img src="https://static1.squarespace.com/static/551da417e4b05f67005d6abe/t/58f8173a1e5b6ca0cc4339f0/1541080220923/?format=100w" style=" display: block; margin-left: auto; margin-right: auto; position: absolute; top: 16px; right: 16px; opacity: 0.5"></img> <img src="imgs/homoscedasticidade.png" style=" display: block; margin-left: auto; margin-right: auto;"></img> --- ## ...e não assim <img src="https://static1.squarespace.com/static/551da417e4b05f67005d6abe/t/58f8173a1e5b6ca0cc4339f0/1541080220923/?format=100w" style=" display: block; margin-left: auto; margin-right: auto; position: absolute; top: 16px; right: 16px; opacity: 0.5"></img> <img src="imgs/heteroscedasticidade.png" style=" display: block; margin-left: auto; margin-right: auto;"></img> --- ## Regressão linear? <img src="https://static1.squarespace.com/static/551da417e4b05f67005d6abe/t/58f8173a1e5b6ca0cc4339f0/1541080220923/?format=100w" style=" display: block; margin-left: auto; margin-right: auto; position: absolute; top: 16px; right: 16px; opacity: 0.5"></img> --- class: middle, center # Agora sim: vamos ao _RStudio_ ! <img src="https://static1.squarespace.com/static/551da417e4b05f67005d6abe/t/58f8173a1e5b6ca0cc4339f0/1541080220923/?format=100w" style=" display: block; margin-left: auto; margin-right: auto; position: absolute; top: 16px; right: 16px; opacity: 0.5"></img> --- ## Ex: área e valor de imóveis <img src="https://static1.squarespace.com/static/551da417e4b05f67005d6abe/t/58f8173a1e5b6ca0cc4339f0/1541080220923/?format=100w" style=" display: block; margin-left: auto; margin-right: auto; position: absolute; top: 16px; right: 16px; opacity: 0.5"></img> <table class="kable_wrapper"> <tbody> <tr> <td> <table> <thead> <tr> <th style="text-align:right;"> id </th> <th style="text-align:right;"> area </th> <th style="text-align:right;"> valor </th> </tr> </thead> <tbody> <tr> <td style="text-align:right;"> 1 </td> <td style="text-align:right;"> 69 </td> <td style="text-align:right;"> 410 </td> </tr> <tr> <td style="text-align:right;"> 2 </td> <td style="text-align:right;"> 67 </td> <td style="text-align:right;"> 415 </td> </tr> <tr> <td style="text-align:right;"> 3 </td> <td style="text-align:right;"> 63 </td> <td style="text-align:right;"> 316 </td> </tr> <tr> <td style="text-align:right;"> 4 </td> <td style="text-align:right;"> 67 </td> <td style="text-align:right;"> 480 </td> </tr> <tr> <td style="text-align:right;"> 5 </td> <td style="text-align:right;"> 65 </td> <td style="text-align:right;"> 400 </td> </tr> <tr> <td style="text-align:right;"> 6 </td> <td style="text-align:right;"> 82 </td> <td style="text-align:right;"> 640 </td> </tr> <tr> <td style="text-align:right;"> 7 </td> <td style="text-align:right;"> 53 </td> <td style="text-align:right;"> 265 </td> </tr> <tr> <td style="text-align:right;"> 8 </td> <td style="text-align:right;"> 60 </td> <td style="text-align:right;"> 350 </td> </tr> <tr> <td style="text-align:right;"> 9 </td> <td style="text-align:right;"> 62 </td> <td style="text-align:right;"> 430 </td> </tr> <tr> <td style="text-align:right;"> 10 </td> <td style="text-align:right;"> 95 </td> <td style="text-align:right;"> 640 </td> </tr> </tbody> </table> </td> <td> <table> <thead> <tr> <th style="text-align:right;"> id </th> <th style="text-align:right;"> area </th> <th style="text-align:right;"> valor </th> </tr> </thead> <tbody> <tr> <td style="text-align:right;"> 11 </td> <td style="text-align:right;"> 49 </td> <td style="text-align:right;"> 225 </td> </tr> <tr> <td style="text-align:right;"> 12 </td> <td style="text-align:right;"> 54 </td> <td style="text-align:right;"> 225 </td> </tr> <tr> <td style="text-align:right;"> 13 </td> <td style="text-align:right;"> 49 </td> <td style="text-align:right;"> 230 </td> </tr> <tr> <td style="text-align:right;"> 14 </td> <td style="text-align:right;"> 49 </td> <td style="text-align:right;"> 230 </td> </tr> <tr> <td style="text-align:right;"> 15 </td> <td style="text-align:right;"> 50 </td> <td style="text-align:right;"> 256 </td> </tr> <tr> <td style="text-align:right;"> 16 </td> <td style="text-align:right;"> 49 </td> <td style="text-align:right;"> 260 </td> </tr> <tr> <td style="text-align:right;"> 17 </td> <td style="text-align:right;"> 53 </td> <td style="text-align:right;"> 265 </td> </tr> <tr> <td style="text-align:right;"> 18 </td> <td style="text-align:right;"> 50 </td> <td style="text-align:right;"> 270 </td> </tr> <tr> <td style="text-align:right;"> 19 </td> <td style="text-align:right;"> 63 </td> <td style="text-align:right;"> 319 </td> </tr> <tr> <td style="text-align:right;"> 20 </td> <td style="text-align:right;"> 61 </td> <td style="text-align:right;"> 348 </td> </tr> </tbody> </table> </td> </tr> </tbody> </table> --- <img src="index_files/figure-html/unnamed-chunk-12-1.png" style="display: block; margin: auto;" /> --- ## Referências * __Noções de Probabilidade e Estatística__<br> Marcos Nascimento Magalhães e Antonio Carlos Pedroso de Lima<br> https://www.ime.usp.br/~noproest * __Estatística Básica__<br> Wilton O. Bussab e Pedro A.Morettin<br> [PDF](https://edisciplinas.usp.br/pluginfile.php/4445638/mod_resource/content/1/Book_EstatBas%20-%20Morettin%20%20Bussab.pdf) * __Penn State__ <br> Curso STAT 414 <br> https://onlinecourses.science.psu.edu/stat414/ * __Linear regression in real life__<br> https://link.medium.com/JTDsTwbZAR --- class: middle, center # Obrigada! @renata_mh<br> renata@voltdata.info<br> www.voltdata.info