Category Archives: Statistics

Mixed Models and R

Check out this webpage for a thorough overview of running mixed models in R. I wanted to pull out a few pieces of information from this article that I found useful. (If you aren’t familiar with mixed models, the following may not be too meaningful for you.)

Nested vs. Crossed Random Effects

“Before you proceed, you will also want to think about the structure of your random effects. Are your random effects nested or crossed? In the case of my study, the random effects are nested, because each observer recorded a certain number of trials, and no two observers recorded the same trial, so here Test.ID is nested within Observer. But say I had collected wasps that clustered into five different genetic lineages. The ‘genetics’ random effect would have nothing to do with observer or arena; it would be orthogonal to these other two random effects. Therefore this random effect would be crossed to the others.”

Identifying the Probability Distribution that Fits the Data

The author of the page plotted the data along various types of distributions (e.g., binomial, Poisson, gamma, log-normal).

“The y axis represents the observations and the x axis represents the quantiles modeled by the distribution. The solid red line represents a perfect distribution fit and the dashed red lines are the confidence intervals of the perfect distribution fit. You want to pick the distribution for which the largest number of observations falls between the dashed lines. In this case, that’s the lognormal distribution, in which only one observation falls outside the dashed lines. Now, armed with the knowledge of which probability distribution fits best, I can try fitting a model.”

Failure to Converge

I often encountered the error “failure to converge” when running mixed models. This article describes what now seems like an obvious way to deal with the failure to converge – systematically drop effects from the model and compare the performance. I am appreciative of how much I’ve learned and grown in my statistics knowledge because of my exposure to data science over the last year and a half.

“There is one complication you might face when fitting a linear mixed model. R may throw you a “failure to converge” error, which usually is phrased “iteration limit reached without convergence.” That means your model has too many factors and not a big enough sample size, and cannot be fit. Unfortunately, I don’t have any data that actually fail to converge on a model that I can show you, but let’s pretend that last model didn’t converge. What you should then do is drop fixed effects and random effects from the model and compare to see which fits the best. Drop fixed effects and random effects one at a time. Hold the fixed effects constant and drop random effects one at a time and find what works best. Then hold random effects constant and drop fixed effects one at a time. Here I have only one random effect, but I’ll show you by example with fixed effects.”

———

This article goes through more of the “math” of mixed models. I’m putting it here for now so I can look through it in more detail later.

Linear Mixed Effects Analyses Tutorial (in R)

One of my datasets requires mixed models linear regression analyses, so I was reading up on exactly how the analyses are done and what they mean. Found this useful-looking tutorial that walks through several examples of the mixed effects, as well as how to do it in R.

Here’s a graph of individual subjects, grouped by gender, and the distribution of their voice pitch.

Pitch of male and female voices

Pitch of male and female voices

To take into account the individual variation in each subject’s voice pitch, run pitch ~ politeness + sex + (1 | subject) + error, where (1 | subject) indicates the assumption that the intercept is different for each subject.

PS. I love box plots!

K-Means Clustering

I want to use k-means clustering for one of my studies, so in this post, I gather useful-looking links to learn how to do it!

EDIT: I made pretty good progress on my k-means clustering! Here’s a little preview to give you an idea of what I found:

kmeans clustering

kmeans clustering

Useful Information on K-Means Clustering

https://stat.ethz.ch/R-manual/R-devel/library/stats/html/kmeans.html
R documentation for kmeans

kmeans {stats}

kmeans {stats}

http://www.r-bloggers.com/k-means-clustering-is-not-a-free-lunch/
When k-means may not work but how to work around it

K-means clustering is not a free lunch

K-means clustering is not a free lunch

http://www.rdatamining.com/examples/kmeans-clustering
Simple, easy example to follow for how to use k-means clustering

k-means Clustering

k-means Clustering

http://www.r-statistics.com/2013/08/k-means-clustering-from-r-in-action/
How to determine number of clusters

K-means Clustering

K-means Clustering

http://www.statmethods.net/advstats/cluster.html
Simple reference for how to k-means cluster

Cluster Analysis

Cluster Analysis

https://rstudio-pubs-static.s3.amazonaws.com/33876_1d7794d9a86647ca90c4f182df93f0e8.html
Walks through several examples

Cluster Analysis in R

Cluster Analysis in R

http://www.improvedoutcomes.com/docs/WebSiteDocs/Clustering/K-Means_Clustering_Overview.htm
To the point overview of clustering: Pros and cons

Overview of Clustering

Overview of Clustering

http://www.norusis.com/pdf/SPC_v13.pdf
Chapter on kmeans clustering – Useful discussion on determining variables

Cluster Analysis Chapter

Cluster Analysis Chapter

http://stats.stackexchange.com/questions/31083/how-to-produce-a-pretty-plot-of-the-results-of-k-means-cluster-analysis
Plotting pairwise scatterplots of clusters

Pairwise scatter plots of clusters

Pairwise scatter plots of clusters