Skip to content
Prev 69775 / 398525 Next

Mental Block with PCA of multivariate time series!

Laura Quinn wrote:
Hi Laura,

Sorry for not quite understanding the specifics, does this do what you want?

# generate some random data for this example
dat <- data.frame(var1 = rnorm(1:1000), var2 = runif(1:1000), var3 = 
rnorm(1:1000) + runif(1:1000), var4 = as.factor(rep(1:10, rep(100, 10))))
# create a list of pca loadings on axis 1, 2
temp <- by(dat[,1:3], dat$var4, function(x) prcomp(x, scale =
	TRUE)$rotation[,1:2])
# plot it
matplot(t(sapply(temp, function(x) x[,1])), type = "n")
# add the lines
matlines(t(sapply(temp, function(x) x[,1])), lty = "solid", col = 
c("red", "blue", "green"))
matlines(t(sapply(temp, function(x) x[,2])), lty = "dotted", col = 
c("red", "blue", "green"))

It isn't pretty - you'll need to calculate the x/ylims for the matplot 
call, but if it is want you are after the plotting should be fairly easy 
thing to work out.

HTH

G