Multi-dimensionamiento escalar

library(ggplot2)
alimentos <- read.csv("~/Documents/WorkspaceR/univalle 2026/dimensionality_reduction/EmojisFood.csv")
#str(alimentos)
#summary(alimentos)
library(GGally)
## Registered S3 method overwritten by 'GGally':
##   method from   
##   +.gg   ggplot2
## ggpairs(alimentos[, c(-1,-2)]) # alimentos [filas,columnas]

alimentosN <- alimentos[, c(-1,-2)]
alimentosN <- scale(alimentosN) # Normalizar los datos

# distancias entre alimentos
distancias <- dist(alimentosN)

alimentosMDS <- cmdscale(distancias, k=2) # k es el número de dimensiones

colnames(alimentosMDS) <- c("Dim1", "Dim2")
# convertir a dataframe
alimentosMDS <- as.data.frame(alimentosMDS)

graficaMDS <- ggplot(alimentosMDS, aes(x=Dim1, y=Dim2, label=alimentos$name))
graficaMDS <- graficaMDS + geom_point()
graficaMDS <- graficaMDS + geom_density2d()
graficaMDS <- graficaMDS + geom_text(vjust=-0.5, angle=45, size=2,check_overlap = TRUE)
graficaMDS
## Warning: The following aesthetics were dropped during statistical transformation: label.
## ℹ This can happen when ggplot fails to infer the correct grouping structure in
##   the data.
## ℹ Did you forget to specify a `group` aesthetic or to convert a numerical
##   variable into a factor?
## Warning: <ggplot> %+% x was deprecated in ggplot2 4.0.0.
## ℹ Please use <ggplot> + x instead.
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
## generated.

T-SNE

library(tsne)
alimentosTSNE <- tsne(distancias, perplexity=5, max_iter = 2000) # k es el número de dimensiones, perplexity es un parámetro que controla la cantidad de vecinos considerados
## sigma summary: Min. : 0.286595256870955 |1st Qu. : 0.463008511424752 |Median : 0.523345835185115 |Mean : 0.561661578532212 |3rd Qu. : 0.616166861044213 |Max. : 1.04431746087659 |
## Epoch: Iteration #100 error is: 25.6577425008592
## Epoch: Iteration #200 error is: 2.94382819352806
## Epoch: Iteration #300 error is: 2.21567175132683
## Epoch: Iteration #400 error is: 1.821876431922
## Epoch: Iteration #500 error is: 1.57037593996982
## Epoch: Iteration #600 error is: 1.43332471808096
## Epoch: Iteration #700 error is: 1.23735232802058
## Epoch: Iteration #800 error is: 0.924012764660883
## Epoch: Iteration #900 error is: 0.697173928633586
## Epoch: Iteration #1000 error is: 0.615525842213478
## Epoch: Iteration #1100 error is: 0.585505263221692
## Epoch: Iteration #1200 error is: 0.552846632691564
## Epoch: Iteration #1300 error is: 0.492364212227596
## Epoch: Iteration #1400 error is: 0.3849252969622
## Epoch: Iteration #1500 error is: 0.361055565454286
## Epoch: Iteration #1600 error is: 0.312082995770174
## Epoch: Iteration #1700 error is: 0.302565309831908
## Epoch: Iteration #1800 error is: 0.297919230013401
## Epoch: Iteration #1900 error is: 0.295753386356808
## Epoch: Iteration #2000 error is: 0.294347612390328
colnames(alimentosTSNE) <- c("Dim1", "Dim2")


graficaMDS <- ggplot(alimentosTSNE, aes(x=Dim1, y=Dim2, label=alimentos$name))
graficaMDS <- graficaMDS + geom_point()
graficaMDS <- graficaMDS + geom_density2d()
graficaMDS <- graficaMDS + geom_text(vjust=-0.5, angle=45, size=2,check_overlap = TRUE)
graficaMDS
## Warning: The following aesthetics were dropped during statistical transformation: label.
## ℹ This can happen when ggplot fails to infer the correct grouping structure in
##   the data.
## ℹ Did you forget to specify a `group` aesthetic or to convert a numerical
##   variable into a factor?