All posts by matloff

See bio at http://heather.cs.ucdavis.edu/matloff.html

Manifold Visualization: Second Example

In last night’s post, I introduced prVis(), a new visualization tool which we have invented, available in our polyreg package. Recall that prVis() is intended as a simpler alternative to recent visualization tools like t-SNE and UMAP. Here I will post another example.

The dataset is prgeng, included in the package. It consists of wage income, age, gender, and so on, of Silicon Valley programmers and engineers, from the 2000 Census. We first load the data and then choose some of the variables (age, gender, education and occupation):

getPE()
pe1 <- pe[,c(1,2,6:7,12:16)]

So, let’s plot the graph:

The graph consists of streaks, about a dozen of them. What do they represent? To investigate that question, we call another polyreg function:

addRowNums(16,z)

This will write the row numbers of 16 random points from the dataset onto the graph that I just plotted, which now looks like this:

Due to overplotting, the numbers are difficult to read, but are also output to the R console:

[1] “highlighted rows:”
[1] 2847
[1] 5016
[1] 5569
[1] 6568
[1] 6915
[1] 8604
[1] 9967
[1] 10113
[1] 10666
[1] 10744
[1] 11383
[1] 11404
[1] 11725
[1] 13335
[1] 14521
[1] 15462

Rows 2847 and 10666 seem to be on the same streak, so they must have something in common. Let’s take a look.

> pe1[2847,]
         age sex ms phd occ1 occ2 occ3 occ4 occ5
2847 32.3253   1  1   0    0    0    0    0    0
> pe1[10666,]
          age sex ms phd occ1 occ2 occ3 occ4 occ5
10666 45.36755  1  1   0    0    0    0    0    0

Aha! Except for age, these two workers are identical in terms of gender (male), education (Master’s) and occupation (occ. category 6). Now those streaks make sense; each one represents a certain combination of the categorical variables.

Well, then, let’s see what UMAP does:

plot(umap(pe1))

The result is

The pattern here, if any, is not clear.

So in both examples, both last night’s and tonight’s, prVis() was not only simpler but also much more visually interpretable than UMAP.

In fairness, I must point out:

  • I just used the default values of umap() in these examples. It would be interesting to explore other values. On the other hand, it may be that UMAP simply is not suitable for partially categorical data, as we have in this second example.
  • For most other datasets I’ve tried, prVis() and UMAP give similar results.

Even so, these two points show the virtues of using prVis() . We are getting equal or better quality while not having to worry about settings for various hypeparameters.

Advertisements

Manifold Visualization: Polynomials to the Rescue

Our arXiv paper and the associated R package polyreg caused a bit of a stir, both pro and con, when we first announced them here in June. The discussion even spread as far as Twitter, Reddit and Hacker News. We’ll be announcing a revised paper, and various new features to the package, very soon.

But the purpose of this blog post is to focus on one particular new feature, a visualization tool. Over the years a number of “nonlinear” methods generalizing Principal Components Analysis (PCA) have been proposed, such as ICA and KPCA some time ago, and more recently t-SNE and UMAP.

I’ve long felt that applying PCA to “polynomial-ized” versions of one’s data should do well too, and of course much more simply, a major virtue. So, now  that we have machinery, the polyreg package, to conveniently build multivariate polynomial models — including for categorical variables, an important case — I developed a new function in polyreg, named prVis(). I’ll illustrate it in this blog post, and compare it to UMAP.

A popular example in the “manifold visualization” (MV) business is the Swiss Roll model, which works as follows: A 4-component mixture of bivariate normals is generated, yielding a 2-column data frame whose column names are ‘x’ and ‘y’. Now derive from that a 3-column data frame, consisting triples of the form (x cos (x), y, x sin(x)).

The original (i.e. 2-column) data looks like this:

Here is the goal:

Using one of the MV methods on the 3-column data, and not knowing that the 2-column data was a 4-component mixture, could a person discern that latter fact?

We’ll try UMAP (said to be faster than t-SNE) and prVis(). But what UMAP implementation to use? Several excellent ones are available for R, such as umapr, umap and uwot. I use the last one, as it was the easiest for me to install, and especially because it includes a predict() method.

Here is the code. We read in the data from a file included in the polyreg package into sw, and change the mode of the last column to an R factor. (Code not shown.) The data frame sw actually has 4 columns, the last being the mixture component ID, 1-4.

We first try PCA:

plot(prcomp(sw[,-4])$x)

That swirl is where the Swiss Roll model gets its name, and clearly, the picture gives no hint at all as to how many components were in the original data. The model was constructed in such a way that PCA would fail.

So let’s try UMAP.

plot(umap(sw[,-4]))

We get this:

So, how many components are there? (Remember, we’re pretending we don’t know it’s 4.) On the left, for instance, does that loop consists of just1 component? 2? 3? We might go for 1. Well, let’s un-pretend now, and use the component labels, color-coded:

plot(umap(sw[,-4]),col=sw[,4])

Wow, that “loop” actually contained 2 of the original components, not just 1. We were led astray.

Let’s try prVis().

prVis(sw[,-4],labels=FALSE)

Still rather swirly, but it definitely suggests 4 components. Let’s check, revealing the color-coded actual components:

prVis(sw,labels=TRUE)

Very nice! Each of the 4 apparent components really did correspond to an actual component.

Of course, usually the difference is not this dramatic, but in applying prVis() to a number of examples, I’ve found that it does indeed do as well as, or better than, UMAP and t-SNE, but in a simpler, more easily explained manner, as noted a major virtue.

I’ll post another example, showing an interesting interpretation of a real dataset, in the next day or so.

What, No Parentheses?

I’m about to show you an R trick. Various readers may find it cool, useful and interesting, or stupid, useless and an evil deed undermining the sanctity of R’s functional programming nature (“All bow”). But I hope many of you will find the material here rather intriguing if not useful.

All this involves a trick one can employ while working in R’s top level from interactive mode, the familiar > prompt.

I’ll use as an toy example here. Let’s say I have some counting variable x that I need to increment occasionally as I work. Of course, the straightforward way to do this is

x <- x + 1

But to save typing and reduce distraction from my main thought processes, it would be nice if I were able to simply type

ix

Of course, the straightforward way to do this would be to define a function ix() (“increment x”),

ix <- function() x <<- x + 1

and then call it each time by typing

ix()

But again, I want to save typing, and don’t want to type the parentheses. How can I arrange this?

My approach here will be to exploit the fact that in R’s interactive mode, typing an expression will print the value of that expression. If I type, say

y

R will first determine the class of y, and invoke the print method for that class. If I write that method myself, I can put any R code in there that I wish, such as code to increment x above! So here goes:

> w <- list(y=3)
> class(w) <- 'ix'
> print.ix <- function(ixObject) x <<- x + 1
> x <- 88
> w
> x
[1] 89
> w
> x
[1] 90

I set up an S3 class ‘ix’, including a print method print.ix(). I created w, an instance of that class, and as you can see, each time I typed ‘w’, x did get incremented by 1.

What just happened? When I type ‘w’, the R interpreter will know that I want to print that variable. The interpreter finds that w is of class ‘ix’, so it calls the print method for that class, print.ix(), which actually doesn’t do any printing; it merely increments x, as desired.

So I don’t even need to type out even the 4 characters ‘ix()’, or even the 2 characters ‘ix’; just typing the single character ‘w’ suffices. A small thing, maybe, but very useful to me when, in the frenzy of code development and especially debugging, I am able to keep distractions from my train of thought to a minimum.

By the way, we have been doing further development on our polyreg package, with some interesting new features. More news on that soon.

Update on Polynomial Regression in Lieu of Neural Nets

There was quite a reaction to our paper, “Polynomial Regression as an Alternative to Neural Nets” (by Cheng, Khomtchouk, Matloff and Mohanty), leading to discussions/debates on Twitter, Reddit, Hacker News and so on. Accordingly, we have posted a revised version of the paper. Some of the new features:

  • Though originally we had made the disclaimer that we had not yet done any experiments with image classification, there were comments along the lines of “If the authors had included even one example of image classification, even the MNIST data, I would have been more receptive.” So our revision does exactly that, with the result that polynomial regression does well on MNIST even with only very primitive preprocessing (plain PCA).
  • We’ve elaborated on some of the theory (still quite informal, but could be made rigorous).
  • We’ve added elaboration on other aspects, e.g. overfitting.
  • We’ve added a section titled, “What This Paper Is NOT.” Hopefully those who wish to comment without reading the paper (!) this time will at least read this section. 🙂
  • Updated and expanded results of our data experiments, including more details on how they were conducted.

We are continuing to add features to our associated R package, polyreg. More news on that to come.

Thanks for the interest. Comments welcome!

Neural Networks Are Essentially Polynomial Regression

You may be interested in my new arXiv paper, joint work with Xi Cheng, an undergraduate at UC Davis (now heading to Cornell for grad school); Bohdan Khomtchouk, a post doc in biology at Stanford; and Pete Mohanty,  a Science, Engineering & Education Fellow in statistics at Stanford. The paper is of a provocative nature, and we welcome feedback.

A summary of the paper is:

  • We present a very simple, informal mathematical argument that neural networks (NNs) are in essence polynomial regression (PR). We refer to this as NNAEPR.
  • NNAEPR implies that we can use our knowledge of the “old-fashioned” method of PR to gain insight into how NNs — widely viewed somewhat warily as a “black box” — work inside.
  • One such insight is that the outputs of an NN layer will be prone to multicollinearity, with the problem becoming worse with each successive layer. This in turn may explain why convergence issues often develop in NNs. It also suggests that NN users tend to use overly large networks.
  • NNAEPR suggests that one may abandon using NNs altogether, and simply use PR instead.
  • We investigated this on a wide variety of datasets, and found that in every case PR did as well as, and often better than, NNs.
  • We have developed a feature-rich R package, polyreg, to facilitate using PR in multivariate settings.

Much work remains to be done (see paper), but our results so far are very encouraging. By using PR, one can avoid the headaches of NN, such as selecting good combinations of tuning parameters, dealing with convergence problems, and so on.

Also available are the slides for our presentation at GRAIL on this project.

Women in R

Last week I gave one of the keynote addresses at R/Finance 2018 in Chicago. I considered it an honor and a pleasure to be there, both because of the stimulating intellectual exchange and the fine level of camaraderie and hospitality that prevailed. I mentioned at the start of my talk that the success of this conference, now in its tenth year, epitomized the wonderful success enjoyed nowadays by the R language.

On the first day of the conference, one of the session chairs announced that a complaint had been made by the group R-Ladies, concerning the fact that all of the talks were given by men. The chair apologized for that, and promised efforts to remedy the situation in the future. Then on the second day, room was made in the schedule for two young women from R-Ladies to make a presentation. There also was a research paper presented by a woman, added at the last minute; she had presented work at the conference in the past.

I have been interested in status-of-women issues for a long time, and I spoke briefly with one of the R-Ladies women after the session. I suggested that she read a blog post I had written that raised some troubling related issues.

But I didn’t give the matter much further thought until Tuesday of this week, when a friend asked me about the “highly eventful” conference. That comment initially baffled me, but it turned out that he was referring to the R-Ladies controversy, which he had been following in the “tweetstorm” on the issue in #rfinance2018 . Not being a regular Twitter user, I had been unaware of this.

Again, issues of gender inequity (however defined) have been a serious, proactive concern of mine over the years. I have been quite active in championing the cases of talented female applicants for faculty positions at my university, for instance. Of my five current research students, four are women. In fact, one of them, Robin Yancey, is a coauthor with me of the partools package that played a prominent role in my talk at this conference.

That said, I must also say that those tweets criticizing the conference organizers were harsh and unfair. As that member of the program committee pointed out, other than keynote speakers, the program is comprised of papers submitted for consideration by potential authors, and it turned out that no papers had been submitted by women. Many readers of those tweets will think that the program committee is prejudiced against women, which I really doubt is the case.

The women who complained also cited lack of a Code of Conduct for the conference. This too turned out to be a misleading claim, as there had been a Code of Conduct posted by the University of Illinois at Chicago, the host of the conference.

So, apparently there was no error of commission here, but some may feel an error of omission did occur. Arguably any conference should make more proactive efforts to encourage female potential authors to submit papers for consideration in the program. Many conferences have invited talks, for instance, and R/Finance may wish to consider this.

However, there is, as is often the case, an issue of breadth of the pool. Granted, things like applicant pools are often used as excuses by, for example, employers for significant gender imbalances in their workforces. But as far as I know, the current state of affairs is:

  • The vast majority of creators (i.e. ‘cre’ status) of R packages in CRAN etc. are men.
  • The authors of the vast majority of books involving R are men.
  • The authors of the vast majority of research papers related to R are men.

It is these activities that lead to giving conference talks, and groups like R-Ladies should promote more female participation in them. We all know some outstanding women in those activities, but to truly solve the problem, many more women need to get involved.

(Some material here was updated on July 21, 2018.)

Xie Yihui, R Superstar and Mensch

Yesterday a friend told me, “Yihui has written the most remarkably open blog post, and you’ve got to read it.” I did and it was. Though my post here is not about R per se, it is about a great contributor to R, our Yihui, Dr. of Statistics and (according to him) Master of Procrastination.

I can relate to his comments personally, and indeed he has written the essay that I never had the courage to write about myself. But the big message in Yihui’s posting is that, really, that MP degree of his is far more useful than his PhD. If Yihui had been the Tiger Cub type (child of a Tiger Mom), we wouldn’t have knitr, and a lot more.

I was a strong opponent of Tiger Mom-ism long before Amy Chua coined the term. To me, it is highly counterproductive, destroying precious creativity and often causing much misery. I’m not endorsing laziness, mind you,, but as Yihui shows, creative procrastination can produce wonderful results. As I write at the above link,

I submit that innovative people tend to be dreamers. I’m certainly not advocating that parents raise lazy kids, but all that intense regimentation in Tiger Mom-land clearly gives kids no chance to breathe, let alone dream.

Yihui is a dreamer, and the R community is much better for it.

I could tell Yihui is exceptionally creative the first day I met him. Who else would have the chutzpah to name his Web site The Capital of Statistics? 🙂

As mentioned, it was quite courageous on Yihui’s part to write his essay, but he is doing a public good in doing so; many, I’m sure, will find it inspirational.

Good for him, and good for R.