Relatório Simples

1. Manipulação de dados

library(tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr     1.1.4     ✔ readr     2.1.5
## ✔ forcats   1.0.1     ✔ stringr   1.5.2
## ✔ ggplot2   4.0.0     ✔ tibble    3.3.0
## ✔ lubridate 1.9.4     ✔ tidyr     1.3.1
## ✔ purrr     1.1.0     
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag()    masks stats::lag()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
library(knitr)
data("mtcars")
df <- mtcars

# Ordenar por mpg e criar variável hp_per_wt
df <- df %>%
  arrange(desc(mpg)) %>%
  mutate(hp_per_wt = hp / wt)

kable(head(df, 6), caption = "Top 6 carros por MPG com hp_per_wt")
Top 6 carros por MPG com hp_per_wt
mpg cyl disp hp drat wt qsec vs am gear carb hp_per_wt
Toyota Corolla 33.9 4 71.1 65 4.22 1.835 19.90 1 1 4 1 35.42234
Fiat 128 32.4 4 78.7 66 4.08 2.200 19.47 1 1 4 1 30.00000
Honda Civic 30.4 4 75.7 52 4.93 1.615 18.52 1 1 4 2 32.19814
Lotus Europa 30.4 4 95.1 113 3.77 1.513 16.90 1 1 5 2 74.68605
Fiat X1-9 27.3 4 79.0 66 4.08 1.935 18.90 1 1 4 1 34.10853
Porsche 914-2 26.0 4 120.3 91 4.43 2.140 16.70 0 1 5 2 42.52336

Explicação: Ordenamos para ver os carros mais econômicos e adicionamos uma medida simples de potência relativa ao peso.

2. Tabela interativa

library(DT)
datatable(df %>% rownames_to_column("model"), options = list(pageLength = 5))

3. Equações LaTeX

3.1 Regressão linear (OLS)

\[ \hat{\beta} = (X^TX)^{-1}X^Ty \] Significado: Coeficientes que minimizam a soma dos quadrados dos resíduos.

3.2 Função sigmoide

\[ \sigma(z) = \frac{1}{1+e^{-z}} \] Significado: Transforma valores reais em probabilidades entre 0 e 1.

3.3 Ridge regression

\[ \hat{\beta}_{ridge} = (X^TX + \lambda I)^{-1} X^Ty \] Significado: Regulariza a regressão linear penalizando grandes coeficientes.

3.4 Entropia cruzada

\[ \mathcal{L} = - \sum_{i=1}^n \sum_{k=1}^K y_{ik} \log(\hat{y}_{ik}) \] Significado: Mede a diferença entre distribuições verdadeiras e preditas, usada em classificação.

3.5 PCA (Componentes Principais)

\[ \Sigma = V \Lambda V^T, \quad \Lambda = \operatorname{diag}(\lambda_1, \dots, \lambda_p), \quad V^T V = I \] Significado: Decomposição da matriz de covariância em autovalores e autovetores para redução de dimensionalidade.

4. Gráficos

ggplot(df %>% rownames_to_column("model"), aes(x=hp, y=mpg)) +
  geom_point() +
  geom_smooth(method="lm", se=FALSE, color="blue") +
  labs(title="MPG vs HP", x="Potência (hp)", y="Consumo (mpg)")
## `geom_smooth()` using formula = 'y ~ x'

ggplot(df %>% rownames_to_column("model"), aes(x=factor(cyl), y=hp)) +
  geom_boxplot(fill="lightblue") +
  labs(title="Boxplot de HP por número de cilindros", x="Cilindros", y="HP")

5. Referências

  1. Wickham, H., R for Data Science.
  2. Xie, Y., R Markdown: The Definitive Guide.
  3. RStudio, DT: DataTables em R.
  4. James, G., Witten, D., Hastie, T., & Tibshirani, R., An Introduction to Statistical Learning.
  5. Jolliffe, I., Principal Component Analysis.