Skip to content

Instantly share code, notes, and snippets.

@abikoushi
Created December 14, 2024 04:04
Show Gist options
  • Select an option

  • Save abikoushi/a1c9dbc8d9f00bac3820ba1f24ec2121 to your computer and use it in GitHub Desktop.

Select an option

Save abikoushi/a1c9dbc8d9f00bac3820ba1f24ec2121 to your computer and use it in GitHub Desktop.
Singular value decomposition of datasauRus
library(datasauRus)
library(ggplot2)
library(dplyr)
X = dplyr::filter(datasaurus_dozen,dataset=="dino") %>%
dplyr::select(-dataset) %>%
as.matrix()
res_svd <- svd(as.matrix(X))
df_svd <- bind_rows(data.frame(res_svd$u, matrix="U"),
data.frame(res_svd$u%*%diag(res_svd$d), matrix="UD"),
data.frame(res_svd$u%*%res_svd$v, matrix="UV"),
data.frame(res_svd$u%*%diag(res_svd$d)%*%res_svd$v, matrix="UDV"))
ggplot(data = df_svd)+
geom_point(aes(x = X1, y = X2))+
facet_wrap(~matrix,scales = "free")+
theme_classic(16)
ggsave("dino_svd.png")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment