[R] 3d trend line

Duncan Murdoch murdoch at stats.uwo.ca
Fri Jan 22 19:27:54 CET 2010


On 22/01/2010 8:52 AM, mlcarte3 wrote:
> Which line do you want?  There's more than one choice.  For example, you 
> could regress y and z on x, or you could find the principle axis of the 
> x, y, z point cloud.
>
> Duncan Murdoch
>
>
> Thanks for the response. I need to find the principal axis for the point
> cloud. How can I plot this in R?
>   

This will do it:

# Generate some correlated data
x <- matrix(rnorm(3000)+2:4, ncol=3)
y <- x %*% matrix(c(1,-2,3,2,3,4,3,4,4), 3,3)

# Plot it
library(rgl)
plot3d(y)

# Compute the principle components
pc <- princomp(y)

# Plot a line corresponding to the main one
centre <- pc$center
direction <- pc$loadings[,1]
segments3d(rbind(min(pc$scores[,1])*direction + centre,
                 max(pc$scores[,1])*direction + centre))



More information about the R-help mailing list