Este projeto contém cinco partes principais:
1️⃣ Manipulação de dados
2️⃣ Tabela interativa (DT)
3️⃣ Equações LaTeX
4️⃣ Figuras
5️⃣ Referências
Nesta seção, carregamos o conjunto de dados iris e aplicamos operações de manipulação simples.
library(dplyr)
##
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
##
## filter, lag
## The following objects are masked from 'package:base':
##
## intersect, setdiff, setequal, union
# Carregando o dataset
data(iris)
head(iris)
## Sepal.Length Sepal.Width Petal.Length Petal.Width Species
## 1 5.1 3.5 1.4 0.2 setosa
## 2 4.9 3.0 1.4 0.2 setosa
## 3 4.7 3.2 1.3 0.2 setosa
## 4 4.6 3.1 1.5 0.2 setosa
## 5 5.0 3.6 1.4 0.2 setosa
## 6 5.4 3.9 1.7 0.4 setosa
summary(iris)
## Sepal.Length Sepal.Width Petal.Length Petal.Width
## Min. :4.300 Min. :2.000 Min. :1.000 Min. :0.100
## 1st Qu.:5.100 1st Qu.:2.800 1st Qu.:1.600 1st Qu.:0.300
## Median :5.800 Median :3.000 Median :4.350 Median :1.300
## Mean :5.843 Mean :3.057 Mean :3.758 Mean :1.199
## 3rd Qu.:6.400 3rd Qu.:3.300 3rd Qu.:5.100 3rd Qu.:1.800
## Max. :7.900 Max. :4.400 Max. :6.900 Max. :2.500
## Species
## setosa :50
## versicolor:50
## virginica :50
##
##
##
# Filtragem e criação de novas variáveis
iris_filtro <- iris %>% filter(Sepal.Length > 5)
iris_ordenado <- iris_filtro %>% arrange(desc(Sepal.Length))
iris_novo <- iris_ordenado %>% mutate(Sepal.Ratio = Sepal.Length / Sepal.Width)
# Resumo por espécie
iris_resumo <- iris_novo %>%
group_by(Species) %>%
summarise(
n = n(),
mean_sepal_length = mean(Sepal.Length),
mean_sepal_ratio = mean(Sepal.Ratio)
)
head(iris_novo)
## Sepal.Length Sepal.Width Petal.Length Petal.Width Species Sepal.Ratio
## 1 7.9 3.8 6.4 2.0 virginica 2.078947
## 2 7.7 3.8 6.7 2.2 virginica 2.026316
## 3 7.7 2.6 6.9 2.3 virginica 2.961538
## 4 7.7 2.8 6.7 2.0 virginica 2.750000
## 5 7.7 3.0 6.1 2.3 virginica 2.566667
## 6 7.6 3.0 6.6 2.1 virginica 2.533333
iris_resumo
## # A tibble: 3 × 4
## Species n mean_sepal_length mean_sepal_ratio
## <fct> <int> <dbl> <dbl>
## 1 setosa 22 5.31 1.44
## 2 versicolor 47 6.00 2.16
## 3 virginica 49 6.62 2.24
Explicação dos passos: - filter() →
seleciona linhas com Sepal.Length > 5
- arrange() → ordena os dados de forma decrescente
- mutate() → cria a variável Sepal.Ratio
- summarise() → calcula médias agrupadas por espécie
Aqui utilizamos o pacote DT para criar uma tabela interativa com busca, ordenação e paginação.
library(DT)
datatable(
iris_novo,
options = list(pageLength = 10, autoWidth = TRUE),
rownames = FALSE,
filter = 'top'
)
\[ \hat{\beta}_{ridge} = \arg\min_{\beta} \left( \sum_{i=1}^{n} (y_i - x_i^T\beta)^2 + \lambda \|\beta\|_2^2 \right) \]
\[ L(\theta) = -\frac{1}{n}\sum_{i=1}^{n} [y_i\log p_\theta(x_i) + (1-y_i)\log(1-p_\theta(x_i))] \]
\[ a^{(l)} = \sigma(W^{(l)}a^{(l-1)} + b^{(l)}),\quad a^{(0)}=x \]
\[ \hat{\mu} = \frac{1}{n}\sum_{i=1}^{n}x_i,\quad \hat{\sigma}^2 = \frac{1}{n}\sum_{i=1}^{n}(x_i-\hat{\mu})^2 \]
\[ \mathcal{L}(\theta) = \frac{1}{n}\sum_{i=1}^{n}\|x_i - g_\theta(f_\theta(x_i))\|_2^2 + \alpha R(\theta) \] —
library(ggplot2)
# Gráfico 1
ggplot(iris, aes(x = Sepal.Length, y = Sepal.Width, color = Species)) +
geom_point() +
geom_smooth(method = "lm", se = FALSE) +
theme_minimal() +
labs(title = "Sepal Length vs Width por Espécie")
## `geom_smooth()` using formula = 'y ~ x'
library(reshape2)
# Gráfico 2
cor_df <- melt(cor(iris[,1:4]))
ggplot(cor_df, aes(Var1, Var2, fill = value)) +
geom_tile() +
geom_text(aes(label = round(value, 2))) +
theme_minimal() +
labs(title = "Matriz de correlação do conjunto Iris")