widely_svd: Turn into a wide matrix, perform SVD, return to tidy form

Description Usage Arguments Value Examples

View source: R/widely_svd.R

Description

This is useful for dimensionality reduction of items, especially when setting a lower nv.

Usage

1
2
3
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 item column

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 d principal component.

...

Extra arguments passed to svd (if nv is NULL) or irlba (if nv is given)

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

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
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)

widyr documentation built on April 14, 2020, 6:16 p.m.