This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see http://rmarkdown.rstudio.com.
When you click the Knit button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:
summary(cars)
## speed dist
## Min. : 4.0 Min. : 2.00
## 1st Qu.:12.0 1st Qu.: 26.00
## Median :15.0 Median : 36.00
## Mean :15.4 Mean : 42.98
## 3rd Qu.:19.0 3rd Qu.: 56.00
## Max. :25.0 Max. :120.00
You can also embed plots, for example:
Note that the echo = FALSE parameter was added to the
code chunk to prevent printing of the R code that generated the
plot.
library(tidyverse) library(factoextra) library(FactoMineR) library(viridis) library(cluster) library(tidyverse) library(factoextra) library(FactoMineR) library(viridis) library(shiny) # Carga de datos wine <- read.csv(“wine.csv”) head(wine, 5) # Verificamos valores <= 0 summary(wine) # Todas las variables son positivas, no es necesario eliminar columnas.
wine_num <- wine[, -1] wine_scale <- scale(wine_num) pca_res <- prcomp(wine_num, scale. = TRUE) # Eigenvalues get_eigenvalue(pca_res) fviz_eig(pca_res, addlabels = TRUE, ylim = c(0, 50)) # Relación entre variables y componentes fviz_pca_var(pca_res, col.var = “black”) fviz_pca_var(pca_res, col.var = “cos2”, gradient.cols = viridis(3), repel = TRUE) # Matriz de distancia euclidiana dist_matrix <- dist(wine_scale, method = “euclidean”) mds_res <- cmdscale(dist_matrix) mds_df <- as.data.frame(mds_res) colnames(mds_df) <- c(“Dim1”, “Dim2”) # Visualización MDS ggplot(mds_df, aes(x = Dim1, y = Dim2, color = as.factor(wine$Cultivar))) + geom_point(size = 2) + labs(title = “Relación Relativa de los Vinos (MDS)”, color = “Cultivar”) + theme_minimal() inputPanel( numericInput(“n_clusters”, label = “Número de Clusters (k):”, value = 3, min = 2, max = 6) )
renderPlot({ set.seed(123) km_res <- kmeans(wine_scale, centers = input$n_clusters) fviz_cluster(km_res, data = wine_scale, palette = “viridis”, geom = “point”, ellipse.type = “convex”) })