Created
September 22, 2013 00:00
-
-
Save JWiley/6655388 to your computer and use it in GitHub Desktop.
Clustering and interactive graph
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Interactive Graph Example | |
========================= | |
Function to do the work. Aside from the packages, to run this, just load the knitr package, and then knit2html("graph.rmd"). | |
```{r} | |
longplot <- function(dat, k, xlab, ylab, seed=4324, miter = 20) { | |
require(EMCluster) | |
require(mice) | |
require(reshape2) | |
require(ggplot2) | |
require(rCharts) | |
dat <- as.data.frame(dat) | |
set.seed(seed) | |
if (any(is.na(dat))) { | |
x <- complete(mice(as.matrix(dat), m = 1, method = "pmm", printFlag = FALSE)) | |
} else { | |
x <- as.matrix(dat) | |
} | |
mm <- init.EM(scale(x), nclass = k, method = "em.EM", min.n.iter = miter) | |
dat$id <- factor(1:nrow(dat)) | |
dat$cluster <- with(mm, factor(class, levels = 1:k, labels = sprintf("%d: n = %d", 1:k, nc))) | |
ldat <- na.omit(melt(dat, id.vars = c("id", "cluster"))) | |
ldat$variable <- as.numeric(factor(ldat$variable, levels = colnames(dat))) | |
p <- ggplot(ldat, aes(variable, value, group = id, colour = cluster)) + | |
geom_line() + facet_wrap(~cluster) + labs(x = xlab, y = ylab) | |
res <- do.call(rbind, by(ldat, ldat$cluster, function(d) { | |
out <- data.frame(Y = as.vector(tapply(d$value, d$variable, mean, na.rm=TRUE)), | |
X = unique(d$variable)) | |
colnames(out) <- c(ylab, xlab) | |
out$cluster <- unique(d$cluster) | |
return(out) | |
})) | |
return(list(Means = res, plot = p, ldat = ldat)) | |
} | |
``` | |
Get some public simulated data, set things up, and make the plot. | |
```{r} | |
wdat <- read.csv("http://joshuawiley.com/files/simwdat.csv") | |
require(rCharts) | |
options(RCHART_WIDTH = 850, RCHART_HEIGHT = 400) | |
m <- longplot(wdat[, c("prc1T3", "prc1T4", "prc1T5", "prc1T6")], k = 9, "Visit", "PerceivedRisk") | |
p <- nPlot(PerceivedRisk ~ Visit, group = "cluster", data = m$Means, type = "lineChart") | |
``` | |
Print the plot | |
```{r results='asis', comment=NA} | |
p$print('TestChart', include_assets = TRUE) | |
``` | |
Individual growth curve plot, by cluster. | |
```{r fig.width=14, fig.height=10} | |
m$plot | |
``` | |
Fin! |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment