widely_svd {widyr} | R Documentation |
Turn into a wide matrix, perform SVD, return to tidy form
Description
This is useful for dimensionality reduction of items, especially when setting a lower nv.
Usage
widely_svd(tbl, item, feature, value, nv = NULL, weight_d = FALSE, ...)
widely_svd_(tbl, item, feature, value, nv = NULL, weight_d = FALSE, ...)
Arguments
tbl |
Table |
item |
Item to perform dimensionality reduction on; will end up in |
feature |
Column describing the feature that links one item to others. |
value |
Value |
nv |
Optional; the number of principal components to estimate. Recommended for matrices with many features. |
weight_d |
Whether to multiply each value by the |
... |
Extra arguments passed to |
Value
A tbl_df with three columns. The first is retained from the item
input,
then dimension
and value
. Each row represents one principal component
value.
Examples
library(dplyr)
library(gapminder)
# principal components driving change
gapminder_svd <- gapminder %>%
widely_svd(country, year, lifeExp)
gapminder_svd
# compare SVDs, join with other data
library(ggplot2)
library(tidyr)
gapminder_svd %>%
spread(dimension, value) %>%
inner_join(distinct(gapminder, country, continent), by = "country") %>%
ggplot(aes(`1`, `2`, label = country)) +
geom_point(aes(color = continent)) +
geom_text(vjust = 1, hjust = 1)
[Package widyr version 0.1.5 Index]