如何从散点图中的每个点到(本征)向量的垂线?
How to drop a perpendicular line from each point in a scatterplot to an (Eigen)vector?
我正在创建一个可视化来说明主成分分析是如何工作的,通过绘制一些实际数据的特征值(出于目的在插图中,我将子集化为 2 个维度)。
我想要 this fantastic PCA tutorial 中这两个图的组合,仅用于我的 真实 数据。
我可以绘制向量,一切正常:
Person1 <- c(-3,1,1,-3,0,-1,-1,0,-1,-1,3,4,5,-2,1,2,-2,-1,1,-2,1,-3,4,-6,1,-3,-4,3,3,-5,0,3,0,-3,1,-2,-1,0,-3,3,-4,-4,-7,-5,-2,-2,-1,1,1,2,0,0,2,-2,4,2,1,2,2,7,0,3,2,5,2,6,0,4,0,-2,-1,2,0,-1,-2,-4,-1)
Person2 <- c(-4,-3,4,-5,-1,-1,-2,2,1,0,3,2,3,-4,2,-1,2,-1,4,-2,6,-2,-1,-2,-1,-1,-3,5,2,-1,3,3,1,-3,1,3,-3,2,-2,4,-4,-6,-4,-7,0,-3,1,-2,0,2,-5,2,-2,-1,4,1,1,0,1,5,1,0,1,1,0,2,0,7,-2,3,-1,-2,-3,0,0,0,0)
df <- data.frame(cbind(Person1, Person2))
g <- ggplot(data = df, mapping = aes(x = Person1, y = Person2))
g <- g + geom_point(alpha = 1/3) # alpha b/c of overplotting
g <- g + geom_smooth(method = "lm") # just for comparsion
g <- g + coord_fixed() # otherwise, the angles of vectors are off
corre <- cor(x = df$Person1, y = df$Person2, method = "spearman") # calculate correlation, must be spearman b/c of measurement
matrix <- matrix(c(1, corre, corre, 1), nrow = 2) # make this into a matrix
eigen <- eigen(matrix) # calculate eigenvectors and values
eigen$vectors.scaled <- eigen$vectors %*% diag(sqrt(eigen$values))
# scale eigenvectors to length = square-root
# as per http://stats.stackexchange.com/questions/9898/how-to-plot-an-ellipse-from-eigenvalues-and-eigenvectors-in-r
g <- g + stat_ellipse(type = "norm")
g <- g + stat_ellipse(type = "t")
# add ellipse, though I am not sure which is the adequate type
# as per https://github.com/hadley/ggplot2/blob/master/R/stat-ellipse.R
g <- g + geom_abline(intercept = 0, slope = eigen$vectors.scaled[1,1], colour = "green") # add slope for pc1
g <- g + geom_abline(intercept = 0, slope = eigen$vectors.scaled[1,2], colour = "red") # add slope for pc2
g <- g + geom_segment(aes(x = 0, y = 0, xend = max(df), yend = eigen$vectors.scaled[1,1] * max(df)), colour = "green", arrow = arrow(length = unit(0.2, "cm"))) # add arrow for pc1
g <- g + geom_segment(aes(x = 0, y = 0, xend = max(df), yend = eigen$vectors.scaled[1,2] * max(df)), colour = "red", arrow = arrow(length = unit(0.2, "cm"))) # add arrow for pc1
g
到目前为止一切顺利(嗯)。
我怎么知道使用 geom_segment
从每个数据点到绿色 first 主成分的垂线?
适配一个previous answer,你可以做到
perp.segment.coord <- function(x0, y0, a=0,b=1){
#finds endpoint for a perpendicular segment from the point (x0,y0) to the line
# defined by lm.mod as y=a+b*x
x1 <- (x0+b*y0-a*b)/(1+b^2)
y1 <- a + b*x1
list(x0=x0, y0=y0, x1=x1, y1=y1)
}
ss<-perp.segment.coord(df$Person1, df$Person2,0,eigen$vectors.scaled[1,1])
g + geom_segment(data=as.data.frame(ss), aes(x = x0, y = y0, xend = x1, yend = y1), colour = "blue")
我正在创建一个可视化来说明主成分分析是如何工作的,通过绘制一些实际数据的特征值(出于目的在插图中,我将子集化为 2 个维度)。
我想要 this fantastic PCA tutorial 中这两个图的组合,仅用于我的 真实 数据。
我可以绘制向量,一切正常:
Person1 <- c(-3,1,1,-3,0,-1,-1,0,-1,-1,3,4,5,-2,1,2,-2,-1,1,-2,1,-3,4,-6,1,-3,-4,3,3,-5,0,3,0,-3,1,-2,-1,0,-3,3,-4,-4,-7,-5,-2,-2,-1,1,1,2,0,0,2,-2,4,2,1,2,2,7,0,3,2,5,2,6,0,4,0,-2,-1,2,0,-1,-2,-4,-1)
Person2 <- c(-4,-3,4,-5,-1,-1,-2,2,1,0,3,2,3,-4,2,-1,2,-1,4,-2,6,-2,-1,-2,-1,-1,-3,5,2,-1,3,3,1,-3,1,3,-3,2,-2,4,-4,-6,-4,-7,0,-3,1,-2,0,2,-5,2,-2,-1,4,1,1,0,1,5,1,0,1,1,0,2,0,7,-2,3,-1,-2,-3,0,0,0,0)
df <- data.frame(cbind(Person1, Person2))
g <- ggplot(data = df, mapping = aes(x = Person1, y = Person2))
g <- g + geom_point(alpha = 1/3) # alpha b/c of overplotting
g <- g + geom_smooth(method = "lm") # just for comparsion
g <- g + coord_fixed() # otherwise, the angles of vectors are off
corre <- cor(x = df$Person1, y = df$Person2, method = "spearman") # calculate correlation, must be spearman b/c of measurement
matrix <- matrix(c(1, corre, corre, 1), nrow = 2) # make this into a matrix
eigen <- eigen(matrix) # calculate eigenvectors and values
eigen$vectors.scaled <- eigen$vectors %*% diag(sqrt(eigen$values))
# scale eigenvectors to length = square-root
# as per http://stats.stackexchange.com/questions/9898/how-to-plot-an-ellipse-from-eigenvalues-and-eigenvectors-in-r
g <- g + stat_ellipse(type = "norm")
g <- g + stat_ellipse(type = "t")
# add ellipse, though I am not sure which is the adequate type
# as per https://github.com/hadley/ggplot2/blob/master/R/stat-ellipse.R
g <- g + geom_abline(intercept = 0, slope = eigen$vectors.scaled[1,1], colour = "green") # add slope for pc1
g <- g + geom_abline(intercept = 0, slope = eigen$vectors.scaled[1,2], colour = "red") # add slope for pc2
g <- g + geom_segment(aes(x = 0, y = 0, xend = max(df), yend = eigen$vectors.scaled[1,1] * max(df)), colour = "green", arrow = arrow(length = unit(0.2, "cm"))) # add arrow for pc1
g <- g + geom_segment(aes(x = 0, y = 0, xend = max(df), yend = eigen$vectors.scaled[1,2] * max(df)), colour = "red", arrow = arrow(length = unit(0.2, "cm"))) # add arrow for pc1
g
到目前为止一切顺利(嗯)。
我怎么知道使用 geom_segment
从每个数据点到绿色 first 主成分的垂线?
适配一个previous answer,你可以做到
perp.segment.coord <- function(x0, y0, a=0,b=1){
#finds endpoint for a perpendicular segment from the point (x0,y0) to the line
# defined by lm.mod as y=a+b*x
x1 <- (x0+b*y0-a*b)/(1+b^2)
y1 <- a + b*x1
list(x0=x0, y0=y0, x1=x1, y1=y1)
}
ss<-perp.segment.coord(df$Person1, df$Person2,0,eigen$vectors.scaled[1,1])
g + geom_segment(data=as.data.frame(ss), aes(x = x0, y = y0, xend = x1, yend = y1), colour = "blue")