I am trying to plot a perceptual map in R but I encounter an error, due to the operation on the matrix I guess.
Here is my perceptions data and preferences data. For the perceptions data, there are 22 perception attributes in rows and 10 brands in columns. For the preferences data, there 1123 respondents who gave scores for each of the 10 brands.
percep <- as.data.frame(matrix(rnorm(220), nrow=22))
prefs <- as.data.frame(matrix(rnorm(11230), nrow=1123))
percep.t <- t(percep)
Now I want to do a PCA analysis, then multiply the preferences data by the PC2 and PC3 scores in order to get coordinates and create a 2 dimensional plot:
fit = prcomp(percep.t, scale.=TRUE)
pref = data.matrix(prefs)
pref3 = pref %*% fit$x[,2:3]
Finally I plot the preferences scores:
par(pty="s")
for (i in 1:nrow(pref3))
{
points(x=pref3[i,2], y=pref3[i,3], pch=".", cex=1.5*k3, col="maroon2")
}
But I get this error
Error in pref3[i, 3] : subscript out of bounds
From this post : R error type "Subscript out of bounds" I saw that the problem was due to the precedence of the operator ":" but here I don't need parenthesis since I don't increment. If I do the analysis with PC1/PC2 or PC1/PC3, it works. But it doesn't with PC2/PC3. The preceding solution doesn't seem to apply here.
Any idea ?