本文介绍了如何将散点图中每个点的垂线删除到(Eigen)向量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 我正在创建一个可视化文件来说明主成分分析是如何工作的,通过为某些实际数据绘制特征值(为了说明的目的,我是子集到2个维度)。 我想要结合这个奇妙的PCA教程,仅限于我的 real 数据。 $ Person1 Person2 df g g g< - g + geom_smooth(method =lm)#仅用于比较g< - g + coord_fixed()#否则,向量的角度是off corre< -cor(x = df $ Person1,y = df $ Person2,method =spearman)#计算相关性,必须是spearman b / c of measurement matrix eigen eigen $ vectors.scaled #根据http缩放特征向量到长度=平方根# ://stats.sta ckexchange.com/questions/9898/how-to-plot-an-ellipse-from-eigenvalues-and-eigenvectors-in-r g g < - g + stat_ellipse(type =t)#添加椭圆,但我不确定哪些是适当的类型#按照https://github.com/hadley/ggplot2/ blob / master / R / stat-ellipse.R g g g g 到目前为止这么好(好)。 如何知道如何使用 geom_segment 从每个数据点向例如绿色第一个主要组件删除垂线? 你可以做 code> perp.segment.coord #查找从点(x0,y0)到直线#由lm.mod定义为y = a + b * x x1 y1 < - a + b * x1 list(x0 = x0,y0 = y0,x1 = x1,y1 = y1)} ss $ bg + geom_segment(data = as.data.frame(ss),aes (x = x0,y = y0,xend = x1,yend = y1),color =blue) I'm creating a visualization to illustrate how Principal Components Analysis works, by plotting Eigenvalues for some actual data (for the purposes of the illustration, I'm subsetting to 2 dimensions).I'm want a combination of these two plots from this fantastic PCA tutorial, only for my real data.I can plot the vectors and all ok: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 overplottingg <- g + geom_smooth(method = "lm") # just for comparsiong <- g + coord_fixed() # otherwise, the angles of vectors are offcorre <- cor(x = df$Person1, y = df$Person2, method = "spearman") # calculate correlation, must be spearman b/c of measurementmatrix <- matrix(c(1, corre, corre, 1), nrow = 2) # make this into a matrixeigen <- eigen(matrix) # calculate eigenvectors and valueseigen$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-rg <- 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.Rg <- g + geom_abline(intercept = 0, slope = eigen$vectors.scaled[1,1], colour = "green") # add slope for pc1g <- g + geom_abline(intercept = 0, slope = eigen$vectors.scaled[1,2], colour = "red") # add slope for pc2g <- 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 pc1g <- 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 pc1gSo far so good (well).How do I know use geom_segment to drop a perpendicular from every datapoint to, say, the green first principal component? 解决方案 Adapting a previous answer, you can doperp.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") 这篇关于如何将散点图中每个点的垂线删除到(Eigen)向量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
07-27 17:43